2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
33 * @(#)getpwent.c 8.2 (Berkeley) 4/27/95
34 * $FreeBSD: src/lib/libc/gen/getpwent.c,v 1.53.2.2 2001/03/05 09:52:13 obrien Exp $
35 * $DragonFly: src/lib/libc/gen/getpwent.c,v 1.5 2005/01/31 22:29:15 dillon Exp $
38 #include "namespace.h"
40 #include <sys/param.h>
51 #include "un-namespace.h"
55 extern void setnetgrent ( char * );
56 extern int getnetgrent ( char **, char **, char ** );
57 extern int innetgr ( const char *, const char *, const char *, const char * );
60 * The lookup techniques and data extraction code here must be kept
61 * in sync with that in `pwd_mkdb'.
64 static struct passwd _pw_passwd; /* password structure */
65 static DB *_pw_db; /* password database */
66 static int _pw_keynum; /* key counter */
67 static int _pw_stayopen; /* keep fd's open */
70 #include <rpcsvc/yp_prot.h>
71 #include <rpcsvc/ypclnt.h>
73 static struct passwd _pw_copy;
74 static DBT empty = { NULL, 0 };
75 static DB *_ypcache = (DB *)NULL;
76 static int _yp_exclusions = 0;
77 static int _yp_enabled = -1;
78 static int _pw_stepping_yp; /* set true when stepping thru map */
79 static char _ypnam[YPMAXRECORD];
80 #define YP_HAVE_MASTER 2
81 #define YP_HAVE_ADJUNCT 1
82 #define YP_HAVE_NONE 0
83 static int _gotmaster;
84 static char *_pw_yp_domain;
85 static inline int unwind ( char * );
86 static void _ypinitdb ( void );
87 static int _havemaster (char *);
88 static int _getyppass (struct passwd *, const char *, const char * );
89 static int _nextyppass (struct passwd *);
90 static inline int lookup (const char *);
91 static inline void store (const char *);
92 static inline int ingr (const char *, const char*);
93 static inline int verf (const char *);
94 static char * _get_adjunct_pw (const char *);
96 static int __hashpw(DBT *);
97 static int __initdb(void);
103 char bf[sizeof(_pw_keynum) + 1];
106 if (!_pw_db && !__initdb())
107 return((struct passwd *)NULL);
110 if(_pw_stepping_yp) {
111 _pw_passwd = _pw_copy;
112 if (unwind((char *)&_ypnam))
119 bf[0] = _PW_KEYBYNUM;
120 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
121 key.data = (u_char *)bf;
122 key.size = sizeof(_pw_keynum) + 1;
124 if(!rv) return (struct passwd *)NULL;
126 if(_pw_passwd.pw_name[0] == '+' || _pw_passwd.pw_name[0] == '-') {
127 if (_yp_enabled == -1)
129 bzero((char *)&_ypnam, sizeof(_ypnam));
130 bcopy(_pw_passwd.pw_name, _ypnam,
131 strlen(_pw_passwd.pw_name));
132 _pw_copy = _pw_passwd;
133 if (unwind((char *)&_ypnam) == 0)
139 /* Ignore YP password file entries when YP is disabled. */
140 if(_pw_passwd.pw_name[0] == '+' || _pw_passwd.pw_name[0] == '-') {
153 char bf[UT_NAMESIZE + 2];
155 if (!_pw_db && !__initdb())
156 return((struct passwd *)NULL);
158 bf[0] = _PW_KEYBYNAME;
160 if (len > UT_NAMESIZE)
162 bcopy(name, bf + 1, len);
163 key.data = (u_char *)bf;
165 rval = __hashpw(&key);
169 if (_yp_enabled == -1)
172 rval = _getyppass(&_pw_passwd, name, "passwd.byname");
176 * Prevent login attempts when YP is not enabled but YP entries
177 * are in /etc/master.passwd.
179 if (rval && (_pw_passwd.pw_name[0] == '+'||
180 _pw_passwd.pw_name[0] == '-')) rval = 0;
184 return(rval ? &_pw_passwd : (struct passwd *)NULL);
193 char bf[sizeof(keyuid) + 1];
195 if (!_pw_db && !__initdb())
196 return((struct passwd *)NULL);
198 bf[0] = _PW_KEYBYUID;
200 bcopy(&keyuid, bf + 1, sizeof(keyuid));
201 key.data = (u_char *)bf;
202 key.size = sizeof(keyuid) + 1;
203 rval = __hashpw(&key);
207 if (_yp_enabled == -1)
210 char ypbuf[16]; /* big enough for 32-bit uids */
211 snprintf(ypbuf, sizeof ypbuf, "%u", (unsigned)uid);
212 rval = _getyppass(&_pw_passwd, ypbuf, "passwd.byuid");
217 * Prevent login attempts when YP is not enabled but YP entries
218 * are in /etc/master.passwd.
220 if (rval && (_pw_passwd.pw_name[0] == '+'||
221 _pw_passwd.pw_name[0] == '-')) rval = 0;
225 return(rval ? &_pw_passwd : (struct passwd *)NULL);
238 _pw_stayopen = stayopen;
256 (void)(_pw_db->close)(_pw_db);
261 (void)(_ypcache->close)(_ypcache);
262 _ypcache = (DB *)NULL;
265 /* Fix for PR #12008 */
276 p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
277 _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
281 syslog(LOG_ERR, "%s: %m", p);
294 if ((_pw_db->get)(_pw_db, key, &data, 0))
296 p = (char *)data.data;
298 /* Increase buffer size for long lines if necessary. */
299 if (data.size > max) {
300 max = data.size + 1024;
301 if (!(line = reallocf(line, max)))
305 /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
307 #define EXPAND(e) e = t; while ( (*t++ = *p++) );
308 #define SCALAR(v) memmove(&(v), p, sizeof v); p += sizeof v
309 EXPAND(_pw_passwd.pw_name);
310 EXPAND(_pw_passwd.pw_passwd);
311 SCALAR(_pw_passwd.pw_uid);
312 SCALAR(_pw_passwd.pw_gid);
313 SCALAR(_pw_passwd.pw_change);
314 EXPAND(_pw_passwd.pw_class);
315 EXPAND(_pw_passwd.pw_gecos);
316 EXPAND(_pw_passwd.pw_dir);
317 EXPAND(_pw_passwd.pw_shell);
318 SCALAR(_pw_passwd.pw_expire);
319 bcopy(p, (char *)&_pw_passwd.pw_fields, sizeof _pw_passwd.pw_fields);
320 p += sizeof _pw_passwd.pw_fields;
330 char buf[] = { _PW_KEYYPENABLED };
334 if ((_pw_db->get)(_pw_db, &key, &data, 0) == 0) {
335 _yp_enabled = (int)*((char *)data.data) - 2;
336 /* Don't even bother with this if we aren't root. */
339 if (yp_get_default_domain(&_pw_yp_domain))
341 _gotmaster = _havemaster(_pw_yp_domain);
342 } else _gotmaster = YP_HAVE_NONE;
344 * Create a DB hash database in memory. Bet you didn't know you
345 * could do a dbopen() with a NULL filename, did you.
347 if (_ypcache == (DB *)NULL)
348 _ypcache = dbopen(NULL, O_RDWR, 600, DB_HASH, NULL);
353 * See if a user is in the blackballed list.
364 key.data = (char *)name;
365 key.size = strlen(name);
367 if ((_ypcache->get)(_ypcache, &key, &empty, 0)) {
375 * Store a blackballed user in an in-core hash database.
389 lkey.data = (char *)key;
390 lkey.size = strlen(key);
392 (void)(_ypcache->put)(_ypcache, &lkey, &empty, R_NOOVERWRITE);
396 * Parse the + entries in the password database and do appropriate
397 * NIS lookups. While ugly to look at, this is optimized to do only
398 * as many lookups as are absolutely necessary in any given case.
399 * Basically, the getpwent() function will feed us + and - lines
400 * as they appear in the database. For + lines, we do netgroup/group
401 * and user lookups to find all usernames that match the rule and
402 * extract them from the NIS passwd maps. For - lines, we save the
403 * matching names in a database and a) exlude them, and b) make sure
404 * we don't consider them when processing other + lines that appear
411 char *user, *host, *domain;
412 static int latch = 0;
413 static struct group *gr = NULL;
417 if (strlen(grp) == 1) {
418 return(_nextyppass(&_pw_passwd));
424 if (*gr->gr_mem != NULL) {
425 if (lookup(*gr->gr_mem)) {
429 rv = _getyppass(&_pw_passwd,
446 if (getnetgrent(&host, &user, &domain) == 0) {
447 if ((gr = getgrnam(grp+2)) != NULL)
455 if (_getyppass(&_pw_passwd, user,
464 return(_getyppass(&_pw_passwd, grp+1, "passwd.byname"));
470 while(getnetgrent(&host, &user, &domain) != 0) {
474 if (!rv && (gr = getgrnam(grp+2)) != NULL) {
488 * See if a user is a member of a particular group.
497 if ((gr = getgrnam(grp)) == NULL)
501 if (!strcmp(*gr->gr_mem, name))
510 * Check a user against the +@netgroup/-@netgroup lines listed in
511 * the local password database. Also checks +user/-user lines.
512 * If no netgroup exists that matches +@netgroup/-@netgroup,
513 * try searching regular groups with the same name.
520 char bf[sizeof(_pw_keynum) + 1];
525 bf[0] = _PW_KEYYPBYNUM;
526 bcopy((char *)&keynum, bf + 1, sizeof(keynum));
527 key.data = (u_char *)bf;
528 key.size = sizeof(keynum) + 1;
529 if (!__hashpw(&key)) {
530 /* Try again using old format */
531 bf[0] = _PW_KEYBYNUM;
532 bcopy((char *)&keynum, bf + 1, sizeof(keynum));
533 key.data = (u_char *)bf;
537 if (_pw_passwd.pw_name[0] != '+' && (_pw_passwd.pw_name[0] != '-'))
539 if (_pw_passwd.pw_name[0] == '+') {
540 if (strlen(_pw_passwd.pw_name) == 1) /* Wildcard */
542 if (_pw_passwd.pw_name[1] == '@') {
543 if ((innetgr(_pw_passwd.pw_name+2, NULL, name,
545 ingr(_pw_passwd.pw_name+2, name)) && !lookup(name))
550 if (!strcmp(name, _pw_passwd.pw_name+1) &&
557 if (_pw_passwd.pw_name[0] == '-') {
558 /* Note that a minus wildcard is a no-op. */
559 if (_pw_passwd.pw_name[1] == '@') {
560 if (innetgr(_pw_passwd.pw_name+2, NULL, name,
562 ingr(_pw_passwd.pw_name+2, name)) {
568 if (!strcmp(name, _pw_passwd.pw_name+1)) {
580 _get_adjunct_pw(name)
583 static char adjunctbuf[YPMAXRECORD+2];
587 char *map = "passwd.adjunct.byname";
590 if ((rval = yp_match(_pw_yp_domain, map, name, strlen(name),
591 &result, &resultlen)))
594 strncpy(adjunctbuf, result, resultlen);
595 adjunctbuf[resultlen] = '\0';
597 result = (char *)&adjunctbuf;
599 /* Don't care about the name. */
600 if ((s = strsep(&result, ":")) == NULL)
601 return (NULL); /* name */
602 if ((s = strsep(&result, ":")) == NULL)
603 return (NULL); /* password */
609 _pw_breakout_yp(struct passwd *pw, char *res, int resultlen, int master)
612 static char resbuf[YPMAXRECORD+2];
615 * Be triple, ultra super-duper paranoid: reject entries
616 * that start with a + or -. yp_mkdb and /var/yp/Makefile
617 * are _both_ supposed to strip these out, but you never
620 if (*res == '+' || *res == '-')
624 * The NIS protocol definition limits the size of an NIS
625 * record to YPMAXRECORD bytes. We need to do a copy to
626 * a static buffer here since the memory pointed to by
627 * res will be free()ed when this function returns.
629 strncpy((char *)&resbuf, res, resultlen);
630 resbuf[resultlen] = '\0';
631 result = (char *)&resbuf;
634 * XXX Sanity check: make sure all fields are valid (no NULLs).
635 * If we find a badly formatted entry, we punt.
637 if ((s = strsep(&result, ":")) == NULL) return 0; /* name */
639 * We don't care what pw_fields says: we _always_ want the
640 * username returned to us by NIS.
643 pw->pw_fields |= _PWF_NAME;
645 if ((s = strsep(&result, ":")) == NULL) return 0; /* password */
646 if(!(pw->pw_fields & _PWF_PASSWD)) {
647 /* SunOS passwd.adjunct hack */
648 if (master == YP_HAVE_ADJUNCT && strstr(s, "##") != NULL) {
650 realpw = _get_adjunct_pw(pw->pw_name);
654 pw->pw_passwd = realpw;
658 pw->pw_fields |= _PWF_PASSWD;
661 if ((s = strsep(&result, ":")) == NULL) return 0; /* uid */
662 if(!(pw->pw_fields & _PWF_UID)) {
663 pw->pw_uid = atoi(s);
664 pw->pw_fields |= _PWF_UID;
667 if ((s = strsep(&result, ":")) == NULL) return 0; /* gid */
668 if(!(pw->pw_fields & _PWF_GID)) {
669 pw->pw_gid = atoi(s);
670 pw->pw_fields |= _PWF_GID;
673 if (master == YP_HAVE_MASTER) {
674 if ((s = strsep(&result, ":")) == NULL) return 0; /* class */
675 if(!(pw->pw_fields & _PWF_CLASS)) {
677 pw->pw_fields |= _PWF_CLASS;
680 if ((s = strsep(&result, ":")) == NULL) return 0; /* change */
681 if(!(pw->pw_fields & _PWF_CHANGE)) {
682 pw->pw_change = atol(s);
683 pw->pw_fields |= _PWF_CHANGE;
686 if ((s = strsep(&result, ":")) == NULL) return 0; /* expire */
687 if(!(pw->pw_fields & _PWF_EXPIRE)) {
688 pw->pw_expire = atol(s);
689 pw->pw_fields |= _PWF_EXPIRE;
693 if ((s = strsep(&result, ":")) == NULL) return 0; /* gecos */
694 if(!(pw->pw_fields & _PWF_GECOS)) {
696 pw->pw_fields |= _PWF_GECOS;
699 if ((s = strsep(&result, ":")) == NULL) return 0; /* dir */
700 if(!(pw->pw_fields & _PWF_DIR)) {
702 pw->pw_fields |= _PWF_DIR;
705 if ((s = strsep(&result, ":")) == NULL) return 0; /* shell */
706 if(!(pw->pw_fields & _PWF_SHELL)) {
708 pw->pw_fields |= _PWF_SHELL;
712 if ((s = strchr(pw->pw_shell, '\n'))) *s = '\0';
718 _havemaster(char *_yp_domain)
723 if (!(rval = yp_order(_yp_domain, "master.passwd.byname", &order)))
724 return(YP_HAVE_MASTER);
727 * NIS+ in YP compat mode doesn't support
728 * YPPROC_ORDER -- no point in continuing.
730 if (rval == YPERR_YPERR)
731 return(YP_HAVE_NONE);
733 /* master.passwd doesn't exist -- try passwd.adjunct */
734 if (rval == YPERR_MAP) {
735 rval = yp_order(_yp_domain, "passwd.adjunct.byname", &order);
737 return(YP_HAVE_ADJUNCT);
740 return (YP_HAVE_NONE);
744 _getyppass(struct passwd *pw, const char *name, const char *map)
749 char mastermap[YPMAXRECORD];
752 if(yp_get_default_domain(&_pw_yp_domain))
756 if (_gotmaster == YP_HAVE_MASTER)
757 snprintf(mastermap, sizeof(mastermap), "master.%s", map);
759 snprintf(mastermap, sizeof(mastermap), "%s", map);
761 if(yp_match(_pw_yp_domain, (char *)&mastermap, name, strlen(name),
762 &result, &resultlen)) {
763 if (_gotmaster != YP_HAVE_MASTER)
765 snprintf(mastermap, sizeof(mastermap), "%s", map);
766 if (yp_match(_pw_yp_domain, (char *)&mastermap,
767 name, strlen(name), &result, &resultlen))
769 _gotmaster = YP_HAVE_NONE;
772 if (!_pw_stepping_yp) {
773 s = strchr(result, ':');
777 /* Must be a malformed entry if no colons. */
788 *s = ':'; /* Put back the colon we previously replaced with a NUL. */
791 rv = _pw_breakout_yp(pw, result, resultlen, _gotmaster);
797 _nextyppass(struct passwd *pw)
801 char *lastkey, *result, *s;
804 char *map = "passwd.byname";
807 if(yp_get_default_domain(&_pw_yp_domain))
811 if (_gotmaster == YP_HAVE_MASTER)
812 map = "master.passwd.byname";
814 if(!_pw_stepping_yp) {
816 rv = yp_first(_pw_yp_domain, map,
817 &key, &keylen, &result, &resultlen);
826 rv = yp_next(_pw_yp_domain, map, key, keylen,
827 &key, &keylen, &result, &resultlen);
835 s = strchr(result, ':');
839 /* Must be a malformed entry if no colons. */
844 if (lookup(result)) {
850 *s = ':'; /* Put back the colon we previously replaced with a NUL. */
851 if (_pw_breakout_yp(pw, result, resultlen, _gotmaster)) {