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