Home | History | Annotate | Line # | Download | only in gen
pw_scan.c revision 1.20.34.1
      1  1.20.34.1  christos /*	$NetBSD: pw_scan.c,v 1.20.34.1 2008/12/28 01:17:41 christos Exp $	*/
      2        1.1     lukem 
      3        1.1     lukem /*
      4        1.1     lukem  * Copyright (c) 1987, 1993, 1994, 1995
      5        1.1     lukem  *	The Regents of the University of California.  All rights reserved.
      6        1.1     lukem  *
      7        1.1     lukem  * Redistribution and use in source and binary forms, with or without
      8        1.1     lukem  * modification, are permitted provided that the following conditions
      9        1.1     lukem  * are met:
     10        1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     11        1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     12        1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     14        1.1     lukem  *    documentation and/or other materials provided with the distribution.
     15       1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1     lukem  *    may be used to endorse or promote products derived from this software
     17        1.1     lukem  *    without specific prior written permission.
     18        1.1     lukem  *
     19        1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1     lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1     lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1     lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1     lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1     lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1     lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1     lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1     lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1     lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1     lukem  * SUCH DAMAGE.
     30        1.1     lukem  */
     31        1.1     lukem 
     32       1.13     lukem #if HAVE_NBTOOL_CONFIG_H
     33       1.13     lukem #include "nbtool_config.h"
     34       1.11        tv #include "compat_pwd.h"
     35       1.13     lukem 
     36       1.11        tv #else
     37        1.1     lukem #include <sys/cdefs.h>
     38        1.1     lukem #if defined(LIBC_SCCS) && !defined(lint)
     39  1.20.34.1  christos __RCSID("$NetBSD: pw_scan.c,v 1.20.34.1 2008/12/28 01:17:41 christos Exp $");
     40        1.1     lukem #endif /* LIBC_SCCS and not lint */
     41        1.1     lukem 
     42        1.6    kleink #if defined(_LIBC)
     43        1.5    kleink #include "namespace.h"
     44        1.6    kleink #endif
     45        1.1     lukem #include <sys/types.h>
     46        1.9     lukem 
     47        1.9     lukem #include <assert.h>
     48        1.1     lukem #include <err.h>
     49        1.1     lukem #include <limits.h>
     50        1.1     lukem #include <pwd.h>
     51        1.1     lukem #include <stdio.h>
     52        1.1     lukem #include <stdlib.h>
     53        1.1     lukem #include <string.h>
     54        1.1     lukem #include <unistd.h>
     55       1.15  christos #include <errno.h>
     56        1.1     lukem 
     57        1.7    kleink #ifdef _LIBC
     58        1.2   thorpej #include "pw_private.h"
     59        1.2   thorpej #endif
     60       1.13     lukem #endif /* ! HAVE_NBTOOL_CONFIG_H */
     61        1.2   thorpej 
     62       1.15  christos static int
     63  1.20.34.1  christos gettime(time_t *res, const char *p, int *flags, int dowarn, int flag)
     64       1.15  christos {
     65  1.20.34.1  christos 	long long l;
     66       1.15  christos 	char *ep;
     67  1.20.34.1  christos 	const char *vp;
     68       1.15  christos 
     69       1.15  christos 	if (*p == '\0') {
     70       1.16  christos 		*flags |= flag;
     71       1.16  christos 		*res = 0;
     72       1.15  christos 		return 1;
     73       1.15  christos 	}
     74  1.20.34.1  christos 	l = strtoll(p, &ep, 0);
     75       1.15  christos 	if (p == ep || *ep != '\0') {
     76  1.20.34.1  christos 		vp = "Invalid number";
     77       1.15  christos 		goto done;
     78       1.15  christos 	}
     79  1.20.34.1  christos 	if (errno == ERANGE && (l == LLONG_MAX || l == LLONG_MIN)) {
     80  1.20.34.1  christos 		vp = strerror(errno);
     81       1.15  christos 		goto done;
     82       1.15  christos 	}
     83       1.15  christos 
     84  1.20.34.1  christos 	*res = (time_t)l;
     85       1.15  christos 	return 1;
     86       1.15  christos done:
     87       1.15  christos 	if (dowarn) {
     88  1.20.34.1  christos 		warnx("%s `%s' for %s time", vp, p,
     89       1.15  christos 		    flag == _PASSWORD_NOEXP ? "expiration" : "change");
     90       1.15  christos 	}
     91       1.15  christos 	return 0;
     92       1.15  christos 
     93       1.15  christos }
     94       1.15  christos 
     95       1.15  christos static int
     96       1.15  christos getid(unsigned long *res, const char *p, int *flags, int dowarn, int flag)
     97       1.15  christos {
     98       1.19  christos 	unsigned long ul;
     99       1.15  christos 	char *ep;
    100       1.15  christos 
    101       1.15  christos 	if (*p == '\0') {
    102       1.16  christos 		*flags |= flag;
    103       1.15  christos 		*res = 0;
    104       1.15  christos 		return 1;
    105       1.15  christos 	}
    106       1.15  christos 	ul = strtoul(p, &ep, 0);
    107       1.15  christos 	if (p == ep || *ep != '\0') {
    108       1.15  christos 		ep = __UNCONST("Invalid number");
    109       1.15  christos 		goto done;
    110       1.15  christos 	}
    111       1.15  christos 	if (errno == ERANGE && ul == ULONG_MAX) {
    112       1.15  christos 		ep = strerror(errno);
    113       1.15  christos 		goto done;
    114       1.15  christos 	}
    115       1.15  christos 	if (ul > *res) {
    116       1.15  christos 		ep = strerror(ERANGE);
    117       1.15  christos 		goto done;
    118       1.15  christos 	}
    119       1.15  christos 
    120       1.15  christos 	*res = ul;
    121       1.15  christos 	return 1;
    122       1.15  christos done:
    123       1.15  christos 	if (dowarn)
    124       1.17  christos 		warnx("%s %s `%s'", ep,
    125       1.15  christos 		    flag == _PASSWORD_NOUID ? "uid" : "gid", p);
    126       1.15  christos 	return 0;
    127       1.15  christos 
    128       1.15  christos }
    129       1.19  christos 
    130        1.1     lukem int
    131        1.7    kleink #ifdef _LIBC
    132       1.15  christos __pw_scan(char *bp, struct passwd *pw, int *flags)
    133        1.7    kleink #else
    134       1.15  christos pw_scan( char *bp, struct passwd *pw, int *flags)
    135        1.2   thorpej #endif
    136        1.1     lukem {
    137        1.1     lukem 	unsigned long id;
    138        1.1     lukem 	int root, inflags;
    139       1.16  christos 	int dowarn;
    140        1.4   mycroft 	const char *p, *sh;
    141        1.9     lukem 
    142        1.9     lukem 	_DIAGASSERT(bp != NULL);
    143        1.9     lukem 	_DIAGASSERT(pw != NULL);
    144        1.1     lukem 
    145       1.15  christos 	if (flags) {
    146        1.1     lukem 		inflags = *flags;
    147        1.1     lukem 		*flags = 0;
    148       1.16  christos 	} else {
    149       1.16  christos 		inflags = 0;
    150       1.16  christos 		flags = &inflags;
    151        1.1     lukem 	}
    152       1.16  christos 	dowarn = !(inflags & _PASSWORD_NOWARN);
    153        1.1     lukem 
    154        1.1     lukem 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
    155        1.1     lukem 		goto fmt;
    156       1.15  christos 	if (strlen(pw->pw_name) > (LOGIN_NAME_MAX - 1)) {
    157       1.15  christos 		if (dowarn)
    158       1.15  christos 			warnx("username too long, `%s' > %d", pw->pw_name,
    159       1.15  christos 			    LOGIN_NAME_MAX - 1);
    160       1.15  christos 		return 0;
    161       1.15  christos 	}
    162       1.15  christos 
    163        1.1     lukem 	root = !strcmp(pw->pw_name, "root");
    164        1.1     lukem 
    165        1.1     lukem 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
    166        1.1     lukem 		goto fmt;
    167        1.1     lukem 
    168        1.1     lukem 	if (!(p = strsep(&bp, ":")))			/* uid */
    169        1.1     lukem 		goto fmt;
    170       1.15  christos 
    171       1.15  christos 	id = UID_MAX;
    172       1.15  christos 	if (!getid(&id, p, flags, dowarn, _PASSWORD_NOUID))
    173       1.15  christos 		return 0;
    174       1.15  christos 
    175        1.1     lukem 	if (root && id) {
    176       1.15  christos 		if (dowarn)
    177        1.1     lukem 			warnx("root uid should be 0");
    178       1.15  christos 		return 0;
    179        1.1     lukem 	}
    180       1.15  christos 
    181        1.1     lukem 	pw->pw_uid = (uid_t)id;
    182        1.1     lukem 
    183        1.1     lukem 	if (!(p = strsep(&bp, ":")))			/* gid */
    184        1.1     lukem 		goto fmt;
    185       1.15  christos 
    186       1.15  christos 	id = GID_MAX;
    187       1.15  christos 	if (!getid(&id, p, flags, dowarn, _PASSWORD_NOGID))
    188       1.15  christos 		return 0;
    189       1.15  christos 
    190        1.1     lukem 	pw->pw_gid = (gid_t)id;
    191        1.1     lukem 
    192        1.1     lukem 	if (inflags & _PASSWORD_OLDFMT) {
    193       1.14  christos 		pw->pw_class = __UNCONST("");
    194        1.1     lukem 		pw->pw_change = 0;
    195        1.1     lukem 		pw->pw_expire = 0;
    196       1.16  christos 		*flags |= (_PASSWORD_NOCHG | _PASSWORD_NOEXP);
    197        1.1     lukem 	} else {
    198        1.1     lukem 		pw->pw_class = strsep(&bp, ":");	/* class */
    199        1.1     lukem 		if (!(p = strsep(&bp, ":")))		/* change */
    200        1.1     lukem 			goto fmt;
    201  1.20.34.1  christos 		if (!gettime(&pw->pw_change, p, flags, dowarn, _PASSWORD_NOCHG))
    202       1.15  christos 			return 0;
    203       1.20  christos 
    204        1.1     lukem 		if (!(p = strsep(&bp, ":")))		/* expire */
    205        1.1     lukem 			goto fmt;
    206  1.20.34.1  christos 		if (!gettime(&pw->pw_expire, p, flags, dowarn, _PASSWORD_NOEXP))
    207       1.15  christos 			return 0;
    208        1.1     lukem 	}
    209       1.15  christos 
    210        1.1     lukem 	pw->pw_gecos = strsep(&bp, ":");		/* gecos */
    211        1.1     lukem 	pw->pw_dir = strsep(&bp, ":");			/* directory */
    212        1.1     lukem 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
    213        1.1     lukem 		goto fmt;
    214        1.1     lukem 
    215       1.13     lukem #if ! HAVE_NBTOOL_CONFIG_H
    216        1.1     lukem 	p = pw->pw_shell;
    217        1.1     lukem 	if (root && *p)					/* empty == /bin/sh */
    218        1.1     lukem 		for (setusershell();;) {
    219        1.1     lukem 			if (!(sh = getusershell())) {
    220       1.15  christos 				if (dowarn)
    221        1.1     lukem 					warnx("warning, unknown root shell");
    222        1.1     lukem 				break;
    223        1.1     lukem 			}
    224        1.1     lukem 			if (!strcmp(p, sh))
    225        1.1     lukem 				break;
    226        1.1     lukem 		}
    227       1.11        tv #endif
    228        1.1     lukem 
    229        1.8  christos 	if ((p = strsep(&bp, ":")) != NULL) {			/* too many */
    230        1.1     lukem fmt:
    231       1.15  christos 		if (dowarn)
    232        1.1     lukem 			warnx("corrupted entry");
    233       1.15  christos 		return 0;
    234        1.1     lukem 	}
    235        1.1     lukem 
    236       1.15  christos 	return 1;
    237        1.1     lukem }
    238