pwd_grp_internal.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2003 Manuel Novoa III
  3. *
  4. * Licensed under LGPL v2.1, see the file COPYING.LIB in this tarball for details.
  5. */
  6. /* Nov 6, 2003 Initial version.
  7. *
  8. * NOTE: This implementation is quite strict about requiring all
  9. * field seperators. It also does not allow leading whitespace
  10. * except when processing the numeric fields. glibc is more
  11. * lenient. See the various glibc difference comments below.
  12. *
  13. * TODO:
  14. * Move to dynamic allocation of (currently staticly allocated)
  15. * buffers; especially for the group-related functions since
  16. * large group member lists will cause error returns.
  17. *
  18. */
  19. #define _GNU_SOURCE
  20. #include <features.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <stddef.h>
  26. #include <errno.h>
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <pwd.h>
  30. #include <grp.h>
  31. #include <paths.h>
  32. #ifdef __HAS_SHADOW__
  33. #include <shadow.h>
  34. #endif
  35. #ifdef __UCLIBC_HAS_THREADS__
  36. #include <pthread.h>
  37. #endif
  38. /**********************************************************************/
  39. /* Sizes for staticly allocated buffers. */
  40. /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
  41. * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
  42. #define PWD_BUFFER_SIZE 256
  43. #define GRP_BUFFER_SIZE 256
  44. /**********************************************************************/
  45. /* Prototypes for internal functions. */
  46. extern int __parsepwent(void *pw, char *line);
  47. extern int __parsegrent(void *gr, char *line);
  48. extern int __parsespent(void *sp, char *line);
  49. extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
  50. char *__restrict line_buff, size_t buflen, FILE *f);
  51. #ifndef GETXXKEY_R_FUNC
  52. #error GETXXKEY_R_FUNC is not defined!
  53. #endif
  54. /**********************************************************************/
  55. #ifdef GETXXKEY_R_FUNC
  56. int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
  57. GETXXKEY_R_ENTTYPE *__restrict resultbuf,
  58. char *__restrict buffer, size_t buflen,
  59. GETXXKEY_R_ENTTYPE **__restrict result)
  60. {
  61. FILE *stream;
  62. int rv;
  63. *result = NULL;
  64. if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
  65. rv = errno;
  66. } else {
  67. __STDIO_SET_USER_LOCKING(stream);
  68. do {
  69. if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
  70. buffer, buflen, stream))
  71. ) {
  72. if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
  73. *result = resultbuf;
  74. break;
  75. }
  76. } else {
  77. if (rv == ENOENT) { /* end-of-file encountered. */
  78. rv = 0;
  79. }
  80. break;
  81. }
  82. } while (1);
  83. fclose(stream);
  84. }
  85. return rv;
  86. }
  87. #endif
  88. /**********************************************************************/
  89. #undef GETXXKEY_R_FUNC
  90. #undef GETXXKEY_R_PARSER
  91. #undef GETXXKEY_R_ENTTYPE
  92. #undef GETXXKEY_R_TEST
  93. #undef DO_GETXXKEY_R_KEYTYPE
  94. #undef DO_GETXXKEY_R_PATHNAME