pam_inline.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2020 Dmitry V. Levin <ldv@altlinux.org>
  3. *
  4. * Handy inline functions and macros providing some convenient functionality
  5. * to libpam and its modules.
  6. */
  7. #ifndef PAM_INLINE_H
  8. #define PAM_INLINE_H
  9. #include "pam_cc_compat.h"
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. /*
  14. * Evaluates to
  15. * - a syntax error if the argument is 0,
  16. * 0, otherwise.
  17. */
  18. #define PAM_FAIL_BUILD_ON_ZERO(e_) (sizeof(int[-1 + 2 * !!(e_)]) * 0)
  19. /*
  20. * Evaluates to
  21. * 1, if the given type is known to be a non-array type
  22. * 0, otherwise.
  23. */
  24. #define PAM_IS_NOT_ARRAY(a_) PAM_IS_SAME_TYPE((a_), &(a_)[0])
  25. /*
  26. * Evaluates to
  27. * - a syntax error if the argument is not an array,
  28. * 0, otherwise.
  29. */
  30. #define PAM_MUST_BE_ARRAY(a_) PAM_FAIL_BUILD_ON_ZERO(!PAM_IS_NOT_ARRAY(a_))
  31. /* Evaluates to the number of elements in the specified array. */
  32. #define PAM_ARRAY_SIZE(a_) (sizeof(a_) / sizeof((a_)[0]) + PAM_MUST_BE_ARRAY(a_))
  33. /*
  34. * Returns NULL if STR does not start with PREFIX,
  35. * or a pointer to the first char in STR after PREFIX.
  36. * The length of PREFIX is specified by PREFIX_LEN.
  37. */
  38. static inline const char *
  39. pam_str_skip_prefix_len(const char *str, const char *prefix, size_t prefix_len)
  40. {
  41. return strncmp(str, prefix, prefix_len) ? NULL : str + prefix_len;
  42. }
  43. #define pam_str_skip_prefix(str_, prefix_) \
  44. pam_str_skip_prefix_len((str_), (prefix_), sizeof(prefix_) - 1 + PAM_MUST_BE_ARRAY(prefix_))
  45. /*
  46. * Returns NULL if STR does not start with PREFIX
  47. * (ignoring the case of the characters),
  48. * or a pointer to the first char in STR after PREFIX.
  49. * The length of PREFIX is specified by PREFIX_LEN.
  50. */
  51. static inline const char *
  52. pam_str_skip_icase_prefix_len(const char *str, const char *prefix, size_t prefix_len)
  53. {
  54. return strncasecmp(str, prefix, prefix_len) ? NULL : str + prefix_len;
  55. }
  56. #define pam_str_skip_icase_prefix(str_, prefix_) \
  57. pam_str_skip_icase_prefix_len((str_), (prefix_), sizeof(prefix_) - 1 + PAM_MUST_BE_ARRAY(prefix_))
  58. static inline int
  59. pam_read_passwords(int fd, int npass, char **passwords)
  60. {
  61. /*
  62. * The passwords array must contain npass preallocated
  63. * buffers of length PAM_MAX_RESP_SIZE + 1.
  64. */
  65. int rbytes = 0;
  66. int offset = 0;
  67. int i = 0;
  68. char *pptr;
  69. while (npass > 0) {
  70. rbytes = read(fd, passwords[i]+offset, PAM_MAX_RESP_SIZE+1-offset);
  71. if (rbytes < 0) {
  72. if (errno == EINTR) {
  73. continue;
  74. }
  75. break;
  76. }
  77. if (rbytes == 0) {
  78. break;
  79. }
  80. while (npass > 0 &&
  81. (pptr = memchr(passwords[i] + offset, '\0', rbytes)) != NULL) {
  82. ++pptr; /* skip the '\0' */
  83. rbytes -= pptr - (passwords[i] + offset);
  84. i++;
  85. offset = 0;
  86. npass--;
  87. if (rbytes > 0) {
  88. if (npass > 0) {
  89. memcpy(passwords[i], pptr, rbytes);
  90. }
  91. memset(pptr, '\0', rbytes);
  92. }
  93. }
  94. offset += rbytes;
  95. }
  96. /* clear up */
  97. if (offset > 0 && npass > 0) {
  98. memset(passwords[i], '\0', offset);
  99. }
  100. return i;
  101. }
  102. #endif /* PAM_INLINE_H */