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