pw_scan.c revision 5964
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1990, 1993, 1994
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
351553Srgrimesstatic char sccsid[] = "@(#)pw_scan.c	8.3 (Berkeley) 4/2/94";
361553Srgrimes#endif /* not lint */
371553Srgrimes
381553Srgrimes/*
391553Srgrimes * This module is used to "verify" password entries by chpass(1) and
401553Srgrimes * pwd_mkdb(8).
411553Srgrimes */
421553Srgrimes
431553Srgrimes#include <sys/param.h>
441553Srgrimes
451553Srgrimes#include <err.h>
461553Srgrimes#include <fcntl.h>
471553Srgrimes#include <pwd.h>
481553Srgrimes#include <errno.h>
491553Srgrimes#include <stdio.h>
501553Srgrimes#include <string.h>
511553Srgrimes#include <stdlib.h>
521553Srgrimes#include <unistd.h>
531553Srgrimes
541553Srgrimes#include "pw_scan.h"
551553Srgrimes
561553Srgrimesint
571553Srgrimespw_scan(bp, pw)
581553Srgrimes	char *bp;
591553Srgrimes	struct passwd *pw;
601553Srgrimes{
611553Srgrimes	long id;
621553Srgrimes	int root;
631553Srgrimes	char *p, *sh;
641553Srgrimes
652916Swollman	pw->pw_fields = 0;
661553Srgrimes	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
671553Srgrimes		goto fmt;
681553Srgrimes	root = !strcmp(pw->pw_name, "root");
692916Swollman	if(pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
702916Swollman		pw->pw_fields |= _PWF_NAME;
711553Srgrimes
721553Srgrimes	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
731553Srgrimes		goto fmt;
742916Swollman	if(pw->pw_passwd[0]) pw->pw_fields |= _PWF_PASSWD;
751553Srgrimes
761553Srgrimes	if (!(p = strsep(&bp, ":")))			/* uid */
771553Srgrimes		goto fmt;
782916Swollman	if(p[0]) pw->pw_fields |= _PWF_UID;
791553Srgrimes	id = atol(p);
801553Srgrimes	if (root && id) {
811553Srgrimes		warnx("root uid should be 0");
821553Srgrimes		return (0);
831553Srgrimes	}
841553Srgrimes	if (id > USHRT_MAX) {
851553Srgrimes		warnx("%s > max uid value (%d)", p, USHRT_MAX);
861553Srgrimes		return (0);
871553Srgrimes	}
881553Srgrimes	pw->pw_uid = id;
891553Srgrimes
901553Srgrimes	if (!(p = strsep(&bp, ":")))			/* gid */
911553Srgrimes		goto fmt;
922916Swollman	if(p[0]) pw->pw_fields |= _PWF_GID;
931553Srgrimes	id = atol(p);
941553Srgrimes	if (id > USHRT_MAX) {
951553Srgrimes		warnx("%s > max gid value (%d)", p, USHRT_MAX);
961553Srgrimes		return (0);
971553Srgrimes	}
981553Srgrimes	pw->pw_gid = id;
991553Srgrimes
1001553Srgrimes	pw->pw_class = strsep(&bp, ":");		/* class */
1012916Swollman	if(pw->pw_class[0]) pw->pw_fields |= _PWF_CLASS;
1022916Swollman
1031553Srgrimes	if (!(p = strsep(&bp, ":")))			/* change */
1041553Srgrimes		goto fmt;
1052916Swollman	if(p[0]) pw->pw_fields |= _PWF_CHANGE;
1061553Srgrimes	pw->pw_change = atol(p);
1072916Swollman
1081553Srgrimes	if (!(p = strsep(&bp, ":")))			/* expire */
1091553Srgrimes		goto fmt;
1102916Swollman	if(p[0]) pw->pw_fields |= _PWF_EXPIRE;
1111553Srgrimes	pw->pw_expire = atol(p);
1122916Swollman
1135964Sdg	if (!(pw->pw_gecos = strsep(&bp, ":")))		/* gecos */
1145964Sdg		goto fmt;
1152916Swollman	if(pw->pw_gecos[0]) pw->pw_fields |= _PWF_GECOS;
1162916Swollman
1175964Sdg	if (!(pw->pw_dir = strsep(&bp, ":")))			/* directory */
1185964Sdg		goto fmt;
1192916Swollman	if(pw->pw_dir[0]) pw->pw_fields |= _PWF_DIR;
1202916Swollman
1211553Srgrimes	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
1221553Srgrimes		goto fmt;
1231553Srgrimes
1241553Srgrimes	p = pw->pw_shell;
1251553Srgrimes	if (root && *p)					/* empty == /bin/sh */
1261553Srgrimes		for (setusershell();;) {
1271553Srgrimes			if (!(sh = getusershell())) {
1281553Srgrimes				warnx("warning, unknown root shell");
1291553Srgrimes				break;
1301553Srgrimes			}
1311553Srgrimes			if (!strcmp(p, sh))
1321553Srgrimes				break;
1331553Srgrimes		}
1342916Swollman	if(p[0]) pw->pw_fields |= _PWF_SHELL;
1351553Srgrimes
1361553Srgrimes	if (p = strsep(&bp, ":")) {			/* too many */
1371553Srgrimesfmt:		warnx("corrupted entry");
1381553Srgrimes		return (0);
1391553Srgrimes	}
1401553Srgrimes	return (1);
1411553Srgrimes}
142