1
0

db.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* It must be here to include config.h before another headers */
  2. #include "lub/db.h"
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <pwd.h>
  7. #include <grp.h>
  8. #include <unistd.h>
  9. #define DEFAULT_GETPW_R_SIZE_MAX 1024
  10. struct passwd *lub_db_getpwnam(const char *name)
  11. {
  12. long int size;
  13. char *buf;
  14. struct passwd *pwbuf;
  15. struct passwd *pw = NULL;
  16. int res = 0;
  17. #ifdef _SC_GETPW_R_SIZE_MAX
  18. if ((size = sysconf(_SC_GETPW_R_SIZE_MAX)) < 0)
  19. size = DEFAULT_GETPW_R_SIZE_MAX;
  20. #else
  21. size = DEFAULT_GETPW_R_SIZE_MAX;
  22. #endif
  23. pwbuf = malloc(sizeof(*pwbuf) + size);
  24. if (!pwbuf)
  25. return NULL;
  26. buf = (char *)pwbuf + sizeof(*pwbuf);
  27. res = getpwnam_r(name, pwbuf, buf, size, &pw);
  28. if (res || !pw) {
  29. free(pwbuf);
  30. if (res != 0)
  31. errno = res;
  32. else
  33. errno = ENOENT;
  34. return NULL;
  35. }
  36. return pwbuf;
  37. }
  38. struct passwd *lub_db_getpwuid(uid_t uid)
  39. {
  40. long int size;
  41. char *buf;
  42. struct passwd *pwbuf;
  43. struct passwd *pw = NULL;
  44. int res = 0;
  45. #ifdef _SC_GETPW_R_SIZE_MAX
  46. if ((size = sysconf(_SC_GETPW_R_SIZE_MAX)) < 0)
  47. size = DEFAULT_GETPW_R_SIZE_MAX;
  48. #else
  49. size = DEFAULT_GETPW_R_SIZE_MAX;
  50. #endif
  51. pwbuf = malloc(sizeof(*pwbuf) + size);
  52. if (!pwbuf)
  53. return NULL;
  54. buf = (char *)pwbuf + sizeof(*pwbuf);
  55. res = getpwuid_r(uid, pwbuf, buf, size, &pw);
  56. if (NULL == pw) {
  57. free(pwbuf);
  58. if (res != 0)
  59. errno = res;
  60. else
  61. errno = ENOENT;
  62. return NULL;
  63. }
  64. return pwbuf;
  65. }
  66. struct group *lub_db_getgrnam(const char *name)
  67. {
  68. long int size;
  69. char *buf;
  70. struct group *grbuf;
  71. struct group *gr = NULL;
  72. int res = 0;
  73. #ifdef _SC_GETGR_R_SIZE_MAX
  74. if ((size = sysconf(_SC_GETGR_R_SIZE_MAX)) < 0)
  75. size = DEFAULT_GETPW_R_SIZE_MAX;
  76. #else
  77. size = DEFAULT_GETPW_R_SIZE_MAX;
  78. #endif
  79. grbuf = malloc(sizeof(*grbuf) + size);
  80. if (!grbuf)
  81. return NULL;
  82. buf = (char *)grbuf + sizeof(*grbuf);
  83. res = getgrnam_r(name, grbuf, buf, size, &gr);
  84. if (!gr) {
  85. free(grbuf);
  86. if (res != 0)
  87. errno = res;
  88. else
  89. errno = ENOENT;
  90. return NULL;
  91. }
  92. return grbuf;
  93. }
  94. struct group *lub_db_getgrgid(gid_t gid)
  95. {
  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 = malloc(sizeof(struct group) + size);
  108. if (!grbuf)
  109. return NULL;
  110. buf = (char *)grbuf + sizeof(struct group);
  111. res = getgrgid_r(gid, grbuf, buf, size, &gr);
  112. if (!gr) {
  113. free(grbuf);
  114. if (res != 0)
  115. errno = res;
  116. else
  117. errno = ENOENT;
  118. return NULL;
  119. }
  120. return grbuf;
  121. }