sysdb.c 3.7 KB

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