sysdb.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /** @file sysdb.c
  2. * @brief Wrappers for system database functions like getpwnam(), getgrnam().
  3. */
  4. // It must be here to include config.h before another headers
  5. #ifdef HAVE_CONFIG_H
  6. #include "config.h"
  7. #endif /* HAVE_CONFIG_H */
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <pwd.h>
  12. #include <grp.h>
  13. #include <unistd.h>
  14. #include "faux/faux.h"
  15. #include "faux/sysdb.h"
  16. #define DEFAULT_GETPW_R_SIZE_MAX 1024
  17. /** @brief Wrapper for ugly getpwnam_r() function.
  18. *
  19. * Gets passwd structure by user name. Easy to use.
  20. *
  21. * @param [in] name User name.
  22. * @return Pointer to allocated passwd structure.
  23. * @warning The resulting pointer (return value) must be freed by faux_free().
  24. */
  25. struct passwd *faux_sysdb_getpwnam(const char *name) {
  26. long int size = 0;
  27. char *buf = NULL;
  28. struct passwd *pwbuf = NULL;
  29. struct passwd *pw = NULL;
  30. int res = 0;
  31. #ifdef _SC_GETPW_R_SIZE_MAX
  32. if ((size = sysconf(_SC_GETPW_R_SIZE_MAX)) < 0)
  33. size = DEFAULT_GETPW_R_SIZE_MAX;
  34. #else
  35. size = DEFAULT_GETPW_R_SIZE_MAX;
  36. #endif
  37. pwbuf = faux_zmalloc(sizeof(*pwbuf) + size);
  38. if (!pwbuf)
  39. return NULL;
  40. buf = (char *)pwbuf + sizeof(*pwbuf);
  41. res = getpwnam_r(name, pwbuf, buf, size, &pw);
  42. if ((res != 0) || !pw) {
  43. faux_free(pwbuf);
  44. if (res != 0)
  45. errno = res;
  46. else
  47. errno = ENOENT;
  48. return NULL;
  49. }
  50. return pwbuf;
  51. }
  52. /** @brief Wrapper for ugly getpwuid_r() function.
  53. *
  54. * Gets passwd structure by UID. Easy to use.
  55. *
  56. * @param [in] uid UID.
  57. * @return Pointer to allocated passwd structure.
  58. * @warning The resulting pointer (return value) must be freed by faux_free().
  59. */
  60. struct passwd *faux_sysdb_getpwuid(uid_t uid) {
  61. long int size = 0;
  62. char *buf = NULL;
  63. struct passwd *pwbuf = NULL;
  64. struct passwd *pw = NULL;
  65. int res = 0;
  66. #ifdef _SC_GETPW_R_SIZE_MAX
  67. if ((size = sysconf(_SC_GETPW_R_SIZE_MAX)) < 0)
  68. size = DEFAULT_GETPW_R_SIZE_MAX;
  69. #else
  70. size = DEFAULT_GETPW_R_SIZE_MAX;
  71. #endif
  72. pwbuf = faux_zmalloc(sizeof(*pwbuf) + size);
  73. if (!pwbuf)
  74. return NULL;
  75. buf = (char *)pwbuf + sizeof(*pwbuf);
  76. res = getpwuid_r(uid, pwbuf, buf, size, &pw);
  77. if (!pw) {
  78. faux_free(pwbuf);
  79. if (res != 0)
  80. errno = res;
  81. else
  82. errno = ENOENT;
  83. return NULL;
  84. }
  85. return pwbuf;
  86. }
  87. /** @brief Wrapper for ugly getgrnam_r() function.
  88. *
  89. * Gets group structure by group name. Easy to use.
  90. *
  91. * @param [in] name Group name.
  92. * @return Pointer to allocated group structure.
  93. * @warning The resulting pointer (return value) must be freed by faux_free().
  94. */
  95. struct group *faux_sysdb_getgrnam(const char *name) {
  96. long int size;
  97. char *buf;
  98. struct group *grbuf;
  99. struct group *gr = NULL;
  100. int res = 0;
  101. #ifdef _SC_GETGR_R_SIZE_MAX
  102. if ((size = sysconf(_SC_GETGR_R_SIZE_MAX)) < 0)
  103. size = DEFAULT_GETPW_R_SIZE_MAX;
  104. #else
  105. size = DEFAULT_GETPW_R_SIZE_MAX;
  106. #endif
  107. grbuf = faux_zmalloc(sizeof(*grbuf) + size);
  108. if (!grbuf)
  109. return NULL;
  110. buf = (char *)grbuf + sizeof(*grbuf);
  111. res = getgrnam_r(name, grbuf, buf, size, &gr);
  112. if (!gr) {
  113. faux_free(grbuf);
  114. if (res != 0)
  115. errno = res;
  116. else
  117. errno = ENOENT;
  118. return NULL;
  119. }
  120. return grbuf;
  121. }
  122. /** @brief Wrapper for ugly getgrgid_r() function.
  123. *
  124. * Gets group structure by GID. Easy to use.
  125. *
  126. * @param [in] gid GID.
  127. * @return Pointer to allocated group structure.
  128. * @warning The resulting pointer (return value) must be freed by faux_free().
  129. */
  130. struct group *faux_sysdb_getgrgid(gid_t gid) {
  131. long int size;
  132. char *buf;
  133. struct group *grbuf;
  134. struct group *gr = NULL;
  135. int res = 0;
  136. #ifdef _SC_GETGR_R_SIZE_MAX
  137. if ((size = sysconf(_SC_GETGR_R_SIZE_MAX)) < 0)
  138. size = DEFAULT_GETPW_R_SIZE_MAX;
  139. #else
  140. size = DEFAULT_GETPW_R_SIZE_MAX;
  141. #endif
  142. grbuf = faux_zmalloc(sizeof(struct group) + size);
  143. if (!grbuf)
  144. return NULL;
  145. buf = (char *)grbuf + sizeof(struct group);
  146. res = getgrgid_r(gid, grbuf, buf, size, &gr);
  147. if (!gr) {
  148. faux_free(grbuf);
  149. if (res != 0)
  150. errno = res;
  151. else
  152. errno = ENOENT;
  153. return NULL;
  154. }
  155. return grbuf;
  156. }