pwd_grp_internal.c 2.8 KB

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