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. {
  27. long int size = 0;
  28. char *buf = NULL;
  29. struct passwd *pwbuf = NULL;
  30. struct passwd *pw = NULL;
  31. int res = 0;
  32. #ifdef _SC_GETPW_R_SIZE_MAX
  33. if ((size = sysconf(_SC_GETPW_R_SIZE_MAX)) < 0)
  34. size = DEFAULT_GETPW_R_SIZE_MAX;
  35. #else
  36. size = DEFAULT_GETPW_R_SIZE_MAX;
  37. #endif
  38. pwbuf = faux_zmalloc(sizeof(*pwbuf) + size);
  39. if (!pwbuf)
  40. return NULL;
  41. buf = (char *)pwbuf + sizeof(*pwbuf);
  42. res = getpwnam_r(name, pwbuf, buf, size, &pw);
  43. if ((res != 0) || !pw) {
  44. faux_free(pwbuf);
  45. if (res != 0)
  46. errno = res;
  47. else
  48. errno = ENOENT;
  49. return NULL;
  50. }
  51. return pwbuf;
  52. }
  53. /** @brief Wrapper for ugly getpwuid_r() function.
  54. *
  55. * Gets passwd structure by UID. Easy to use.
  56. *
  57. * @param [in] uid UID.
  58. * @return Pointer to allocated passwd structure.
  59. * @warning The resulting pointer (return value) must be freed by faux_free().
  60. */
  61. struct passwd *faux_sysdb_getpwuid(uid_t uid)
  62. {
  63. long int size = 0;
  64. char *buf = NULL;
  65. struct passwd *pwbuf = NULL;
  66. struct passwd *pw = NULL;
  67. int res = 0;
  68. #ifdef _SC_GETPW_R_SIZE_MAX
  69. if ((size = sysconf(_SC_GETPW_R_SIZE_MAX)) < 0)
  70. size = DEFAULT_GETPW_R_SIZE_MAX;
  71. #else
  72. size = DEFAULT_GETPW_R_SIZE_MAX;
  73. #endif
  74. pwbuf = faux_zmalloc(sizeof(*pwbuf) + size);
  75. if (!pwbuf)
  76. return NULL;
  77. buf = (char *)pwbuf + sizeof(*pwbuf);
  78. res = getpwuid_r(uid, pwbuf, buf, size, &pw);
  79. if (!pw) {
  80. faux_free(pwbuf);
  81. if (res != 0)
  82. errno = res;
  83. else
  84. errno = ENOENT;
  85. return NULL;
  86. }
  87. return pwbuf;
  88. }
  89. /** @brief Wrapper for ugly getgrnam_r() function.
  90. *
  91. * Gets group structure by group name. Easy to use.
  92. *
  93. * @param [in] name Group name.
  94. * @return Pointer to allocated group structure.
  95. * @warning The resulting pointer (return value) must be freed by faux_free().
  96. */
  97. struct group *faux_sysdb_getgrnam(const char *name)
  98. {
  99. long int size;
  100. char *buf;
  101. struct group *grbuf;
  102. struct group *gr = NULL;
  103. int res = 0;
  104. #ifdef _SC_GETGR_R_SIZE_MAX
  105. if ((size = sysconf(_SC_GETGR_R_SIZE_MAX)) < 0)
  106. size = DEFAULT_GETPW_R_SIZE_MAX;
  107. #else
  108. size = DEFAULT_GETPW_R_SIZE_MAX;
  109. #endif
  110. grbuf = faux_zmalloc(sizeof(*grbuf) + size);
  111. if (!grbuf)
  112. return NULL;
  113. buf = (char *)grbuf + sizeof(*grbuf);
  114. res = getgrnam_r(name, grbuf, buf, size, &gr);
  115. if (!gr) {
  116. faux_free(grbuf);
  117. if (res != 0)
  118. errno = res;
  119. else
  120. errno = ENOENT;
  121. return NULL;
  122. }
  123. return grbuf;
  124. }
  125. /** @brief Wrapper for ugly getgrgid_r() function.
  126. *
  127. * Gets group structure by GID. Easy to use.
  128. *
  129. * @param [in] gid GID.
  130. * @return Pointer to allocated group structure.
  131. * @warning The resulting pointer (return value) must be freed by faux_free().
  132. */
  133. struct group *faux_sysdb_getgrgid(gid_t gid)
  134. {
  135. long int size;
  136. char *buf;
  137. struct group *grbuf;
  138. struct group *gr = NULL;
  139. int res = 0;
  140. #ifdef _SC_GETGR_R_SIZE_MAX
  141. if ((size = sysconf(_SC_GETGR_R_SIZE_MAX)) < 0)
  142. size = DEFAULT_GETPW_R_SIZE_MAX;
  143. #else
  144. size = DEFAULT_GETPW_R_SIZE_MAX;
  145. #endif
  146. grbuf = faux_zmalloc(sizeof(struct group) + size);
  147. if (!grbuf)
  148. return NULL;
  149. buf = (char *)grbuf + sizeof(struct group);
  150. res = getgrgid_r(gid, grbuf, buf, size, &gr);
  151. if (!gr) {
  152. faux_free(grbuf);
  153. if (res != 0)
  154. errno = res;
  155. else
  156. errno = ENOENT;
  157. return NULL;
  158. }
  159. return grbuf;
  160. }