Home | History | Annotate | Line # | Download | only in gen
pw_scan.c revision 1.7
      1 /*	$NetBSD: pw_scan.c,v 1.7 1998/10/14 21:13:04 kleink Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993, 1994, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 __RCSID("$NetBSD: pw_scan.c,v 1.7 1998/10/14 21:13:04 kleink Exp $");
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #if defined(_LIBC)
     42 #include "namespace.h"
     43 #endif
     44 #include <sys/types.h>
     45 #include <err.h>
     46 #include <limits.h>
     47 #include <pwd.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <unistd.h>
     52 
     53 #ifdef _LIBC
     54 #include "pw_private.h"
     55 #endif
     56 
     57 int
     58 #ifdef _LIBC
     59 __pw_scan(bp, pw, flags)
     60 #else
     61 pw_scan(bp, pw, flags)
     62 #endif
     63 	char *bp;
     64 	struct passwd *pw;
     65 	int *flags;
     66 {
     67 	unsigned long id;
     68 	int root, inflags;
     69 	char *ep;
     70 	const char *p, *sh;
     71 
     72 	inflags = 0;
     73 	if (flags != (int *)NULL) {
     74 		inflags = *flags;
     75 		*flags = 0;
     76 	}
     77 
     78 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
     79 		goto fmt;
     80 	root = !strcmp(pw->pw_name, "root");
     81 
     82 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
     83 		goto fmt;
     84 
     85 	if (!(p = strsep(&bp, ":")))			/* uid */
     86 		goto fmt;
     87 	id = strtoul(p, &ep, 10);
     88 	if (root && id) {
     89 		if (!(inflags & _PASSWORD_NOWARN))
     90 			warnx("root uid should be 0");
     91 		return (0);
     92 	}
     93 	if (id > UID_MAX || *ep != '\0') {
     94 		if (!(inflags & _PASSWORD_NOWARN))
     95 			warnx("invalid uid '%s'", p);
     96 		return (0);
     97 	}
     98 	pw->pw_uid = (uid_t)id;
     99 	if ((*p == '\0') && (flags != (int *)NULL))
    100 		*flags |= _PASSWORD_NOUID;
    101 
    102 	if (!(p = strsep(&bp, ":")))			/* gid */
    103 		goto fmt;
    104 	id = strtoul(p, &ep, 10);
    105 	if (id > GID_MAX || *ep != '\0') {
    106 		if (!(inflags & _PASSWORD_NOWARN))
    107 			warnx("invalid gid '%s'", p);
    108 		return (0);
    109 	}
    110 	pw->pw_gid = (gid_t)id;
    111 	if ((*p == '\0') && (flags != (int *)NULL))
    112 		*flags |= _PASSWORD_NOGID;
    113 
    114 	if (inflags & _PASSWORD_OLDFMT) {
    115 		pw->pw_class = "";
    116 		pw->pw_change = 0;
    117 		pw->pw_expire = 0;
    118 		*flags |= (_PASSWORD_NOCHG | _PASSWORD_NOEXP);
    119 	} else {
    120 		pw->pw_class = strsep(&bp, ":");	/* class */
    121 		if (!(p = strsep(&bp, ":")))		/* change */
    122 			goto fmt;
    123 		pw->pw_change = atol(p);
    124 		if ((*p == '\0') && (flags != (int *)NULL))
    125 			*flags |= _PASSWORD_NOCHG;
    126 		if (!(p = strsep(&bp, ":")))		/* expire */
    127 			goto fmt;
    128 		pw->pw_expire = atol(p);
    129 		if ((*p == '\0') && (flags != (int *)NULL))
    130 			*flags |= _PASSWORD_NOEXP;
    131 	}
    132 	pw->pw_gecos = strsep(&bp, ":");		/* gecos */
    133 	pw->pw_dir = strsep(&bp, ":");			/* directory */
    134 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
    135 		goto fmt;
    136 
    137 	p = pw->pw_shell;
    138 	if (root && *p)					/* empty == /bin/sh */
    139 		for (setusershell();;) {
    140 			if (!(sh = getusershell())) {
    141 				if (!(inflags & _PASSWORD_NOWARN))
    142 					warnx("warning, unknown root shell");
    143 				break;
    144 			}
    145 			if (!strcmp(p, sh))
    146 				break;
    147 		}
    148 
    149 	if ((p = strsep(&bp, ":"))) {			/* too many */
    150 fmt:
    151 		if (!(inflags & _PASSWORD_NOWARN))
    152 			warnx("corrupted entry");
    153 		return (0);
    154 	}
    155 
    156 	return (1);
    157 }
    158