Home | History | Annotate | Line # | Download | only in gen
pw_scan.c revision 1.1
      1 /*	$NetBSD: pw_scan.c,v 1.1 1998/06/08 03:18:00 lukem 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.1 1998/06/08 03:18:00 lukem Exp $");
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include <sys/types.h>
     42 
     43 #include <err.h>
     44 #include <limits.h>
     45 #include <pwd.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <unistd.h>
     50 
     51 int
     52 pw_scan(bp, pw, flags)
     53 	char *bp;
     54 	struct passwd *pw;
     55 	int *flags;
     56 {
     57 	unsigned long id;
     58 	int root, inflags;
     59 	char *p, *sh, *ep;
     60 
     61 	inflags = 0;
     62 	if (flags != (int *)NULL) {
     63 		inflags = *flags;
     64 		*flags = 0;
     65 	}
     66 
     67 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
     68 		goto fmt;
     69 	root = !strcmp(pw->pw_name, "root");
     70 
     71 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
     72 		goto fmt;
     73 
     74 	if (!(p = strsep(&bp, ":")))			/* uid */
     75 		goto fmt;
     76 	id = strtoul(p, &ep, 10);
     77 	if (root && id) {
     78 		if (!(inflags & _PASSWORD_NOWARN))
     79 			warnx("root uid should be 0");
     80 		return (0);
     81 	}
     82 	if (id > UID_MAX || *ep != '\0') {
     83 		if (!(inflags & _PASSWORD_NOWARN))
     84 			warnx("invalid uid '%s'", p);
     85 		return (0);
     86 	}
     87 	pw->pw_uid = (uid_t)id;
     88 	if ((*p == '\0') && (flags != (int *)NULL))
     89 		*flags |= _PASSWORD_NOUID;
     90 
     91 	if (!(p = strsep(&bp, ":")))			/* gid */
     92 		goto fmt;
     93 	id = strtoul(p, &ep, 10);
     94 	if (id > GID_MAX || *ep != '\0') {
     95 		if (!(inflags & _PASSWORD_NOWARN))
     96 			warnx("invalid gid '%s'", p);
     97 		return (0);
     98 	}
     99 	pw->pw_gid = (gid_t)id;
    100 	if ((*p == '\0') && (flags != (int *)NULL))
    101 		*flags |= _PASSWORD_NOGID;
    102 
    103 	if (inflags & _PASSWORD_OLDFMT) {
    104 		pw->pw_class = "";
    105 		pw->pw_change = 0;
    106 		pw->pw_expire = 0;
    107 		*flags |= (_PASSWORD_NOCHG | _PASSWORD_NOEXP);
    108 	} else {
    109 		pw->pw_class = strsep(&bp, ":");	/* class */
    110 		if (!(p = strsep(&bp, ":")))		/* change */
    111 			goto fmt;
    112 		pw->pw_change = atol(p);
    113 		if ((*p == '\0') && (flags != (int *)NULL))
    114 			*flags |= _PASSWORD_NOCHG;
    115 		if (!(p = strsep(&bp, ":")))		/* expire */
    116 			goto fmt;
    117 		pw->pw_expire = atol(p);
    118 		if ((*p == '\0') && (flags != (int *)NULL))
    119 			*flags |= _PASSWORD_NOEXP;
    120 	}
    121 	pw->pw_gecos = strsep(&bp, ":");		/* gecos */
    122 	pw->pw_dir = strsep(&bp, ":");			/* directory */
    123 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
    124 		goto fmt;
    125 
    126 	p = pw->pw_shell;
    127 	if (root && *p)					/* empty == /bin/sh */
    128 		for (setusershell();;) {
    129 			if (!(sh = getusershell())) {
    130 				if (!(inflags & _PASSWORD_NOWARN))
    131 					warnx("warning, unknown root shell");
    132 				break;
    133 			}
    134 			if (!strcmp(p, sh))
    135 				break;
    136 		}
    137 
    138 	if ((p = strsep(&bp, ":"))) {			/* too many */
    139 fmt:
    140 		if (!(inflags & _PASSWORD_NOWARN))
    141 			warnx("corrupted entry");
    142 		return (0);
    143 	}
    144 
    145 	return (1);
    146 }
    147