1 /* $NetBSD: getid.c,v 1.8 2013/10/16 17:27:42 christos Exp $ */
2 /* from: NetBSD: getpwent.c,v 1.48 2000/10/03 03:22:26 enami Exp */
3 /* from: NetBSD: getgrent.c,v 1.41 2002/01/12 23:51:30 lukem Exp */
6 * Copyright (c) 1987, 1988, 1989, 1993, 1994, 1995
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2002 The NetBSD Foundation, Inc.
36 * All rights reserved.
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Luke Mewburn of Wasabi Systems.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
63 #if HAVE_NBTOOL_CONFIG_H
64 #include "nbtool_config.h"
67 #include <sys/cdefs.h>
68 __RCSID("$NetBSD: getid.c,v 1.8 2013/10/16 17:27:42 christos Exp $");
70 #include <sys/param.h>
83 static struct group
* gi_getgrnam(const char *);
84 static struct group
* gi_getgrgid(gid_t
);
85 static int gi_setgroupent(int);
86 static void gi_endgrent(void);
87 static int grstart(void);
88 static int grscan(int, gid_t
, const char *);
89 static int grmatchline(int, gid_t
, const char *);
91 static struct passwd
* gi_getpwnam(const char *);
92 static struct passwd
* gi_getpwuid(uid_t
);
93 static int gi_setpassent(int);
94 static void gi_endpwent(void);
95 static int pwstart(void);
96 static int pwscan(int, uid_t
, const char *);
97 static int pwmatchline(int, uid_t
, const char *);
100 #define MAXLINELENGTH 1024
103 static struct group _gr_group
;
104 static int _gr_stayopen
;
105 static int _gr_filesdone
;
107 static struct passwd _pw_passwd
; /* password structure */
108 static int _pw_stayopen
; /* keep fd's open */
109 static int _pw_filesdone
;
111 static char grfile
[MAXPATHLEN
];
112 static char pwfile
[MAXPATHLEN
];
114 static char *members
[MAXGRP
];
115 static char grline
[MAXLINELENGTH
];
116 static char pwline
[MAXLINELENGTH
];
119 setup_getid(const char *dir
)
124 /* close existing databases */
128 /* build paths to new databases */
129 snprintf(grfile
, sizeof(grfile
), "%s/group", dir
);
130 snprintf(pwfile
, sizeof(pwfile
), "%s/master.passwd", dir
);
132 /* try to open new databases */
133 if (!grstart() || !pwstart())
136 /* switch pwcache(3) lookup functions */
137 if (pwcache_groupdb(gi_setgroupent
, gi_endgrent
,
138 gi_getgrnam
, gi_getgrgid
) == -1
139 || pwcache_userdb(gi_setpassent
, gi_endpwent
,
140 gi_getpwnam
, gi_getpwuid
) == -1)
148 * group lookup functions
151 static struct group
*
152 gi_getgrnam(const char *name
)
158 rval
= grscan(1, 0, name
);
161 return (rval
) ? &_gr_group
: NULL
;
164 static struct group
*
165 gi_getgrgid(gid_t gid
)
171 rval
= grscan(1, gid
, NULL
);
174 return (rval
) ? &_gr_group
: NULL
;
178 gi_setgroupent(int stayopen
)
183 _gr_stayopen
= stayopen
;
193 (void)fclose(_gr_fp
);
207 if (grfile
[0] == '\0') /* sanity check */
209 return (_gr_fp
= fopen(grfile
, "r")) ? 1 : 0;
214 grscan(int search
, gid_t gid
, const char *name
)
220 if (!fgets(grline
, sizeof(grline
), _gr_fp
)) {
225 /* skip lines that are too big */
226 if (!strchr(grline
, '\n')) {
229 while ((ch
= getc(_gr_fp
)) != '\n' && ch
!= EOF
)
234 if (pwline
[0] == '#')
236 if (grmatchline(search
, gid
, name
))
243 grmatchline(int search
, gid_t gid
, const char *name
)
249 /* name may be NULL if search is nonzero */
252 memset(&_gr_group
, 0, sizeof(_gr_group
));
253 _gr_group
.gr_name
= strsep(&bp
, ":\n");
254 if (search
&& name
&& strcmp(_gr_group
.gr_name
, name
))
256 _gr_group
.gr_passwd
= strsep(&bp
, ":\n");
257 if (!(cp
= strsep(&bp
, ":\n")))
259 id
= strtoul(cp
, &ep
, 10);
260 if (id
> GID_MAX
|| *ep
!= '\0')
262 _gr_group
.gr_gid
= (gid_t
)id
;
263 if (search
&& name
== NULL
&& _gr_group
.gr_gid
!= gid
)
268 for (_gr_group
.gr_mem
= m
= members
;; bp
++) {
269 if (m
== &members
[MAXGRP
- 1])
277 } else if (*bp
== '\0' || *bp
== '\n' || *bp
== ' ') {
283 } else if (cp
== NULL
)
292 * user lookup functions
295 static struct passwd
*
296 gi_getpwnam(const char *name
)
302 rval
= pwscan(1, 0, name
);
305 return (rval
) ? &_pw_passwd
: NULL
;
308 static struct passwd
*
309 gi_getpwuid(uid_t uid
)
315 rval
= pwscan(1, uid
, NULL
);
318 return (rval
) ? &_pw_passwd
: NULL
;
322 gi_setpassent(int stayopen
)
327 _pw_stayopen
= stayopen
;
337 (void)fclose(_pw_fp
);
351 if (pwfile
[0] == '\0') /* sanity check */
353 return (_pw_fp
= fopen(pwfile
, "r")) ? 1 : 0;
358 pwscan(int search
, uid_t uid
, const char *name
)
364 if (!fgets(pwline
, sizeof(pwline
), _pw_fp
)) {
369 /* skip lines that are too big */
370 if (!strchr(pwline
, '\n')) {
373 while ((ch
= getc(_pw_fp
)) != '\n' && ch
!= EOF
)
378 if (pwline
[0] == '#')
380 if (pwmatchline(search
, uid
, name
))
387 pwmatchline(int search
, uid_t uid
, const char *name
)
392 /* name may be NULL if search is nonzero */
395 memset(&_pw_passwd
, 0, sizeof(_pw_passwd
));
396 _pw_passwd
.pw_name
= strsep(&bp
, ":\n"); /* name */
397 if (search
&& name
&& strcmp(_pw_passwd
.pw_name
, name
))
400 _pw_passwd
.pw_passwd
= strsep(&bp
, ":\n"); /* passwd */
402 if (!(cp
= strsep(&bp
, ":\n"))) /* uid */
404 id
= strtoul(cp
, &ep
, 10);
405 if (id
> UID_MAX
|| *ep
!= '\0')
407 _pw_passwd
.pw_uid
= (uid_t
)id
;
408 if (search
&& name
== NULL
&& _pw_passwd
.pw_uid
!= uid
)
411 if (!(cp
= strsep(&bp
, ":\n"))) /* gid */
413 id
= strtoul(cp
, &ep
, 10);
414 if (id
> GID_MAX
|| *ep
!= '\0')
416 _pw_passwd
.pw_gid
= (gid_t
)id
;
418 if (!(ep
= strsep(&bp
, ":"))) /* class */
420 if (!(ep
= strsep(&bp
, ":"))) /* change */
422 if (!(ep
= strsep(&bp
, ":"))) /* expire */
425 if (!(_pw_passwd
.pw_gecos
= strsep(&bp
, ":\n"))) /* gecos */
427 if (!(_pw_passwd
.pw_dir
= strsep(&bp
, ":\n"))) /* directory */
429 if (!(_pw_passwd
.pw_shell
= strsep(&bp
, ":\n"))) /* shell */
432 if (strchr(bp
, ':') != NULL
)