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