Home | History | Annotate | Line # | Download | only in chpass
field.c revision 1.11
      1 /*	$NetBSD: field.c,v 1.11 2005/02/17 17:09:48 xtraeme Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1993, 1994
      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 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)field.c	8.4 (Berkeley) 4/2/94";
     36 #else
     37 __RCSID("$NetBSD: field.c,v 1.11 2005/02/17 17:09:48 xtraeme Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 
     43 #include <ctype.h>
     44 #include <err.h>
     45 #include <errno.h>
     46 #include <grp.h>
     47 #include <pwd.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <unistd.h>
     52 
     53 #include "chpass.h"
     54 #include "pathnames.h"
     55 
     56 /* ARGSUSED */
     57 int
     58 p_login(const char *p, struct passwd *pw, ENTRY *ep)
     59 {
     60 
     61 	if (!*p) {
     62 		warnx("empty login field");
     63 		return (1);
     64 	}
     65 	if (*p == '-') {
     66 		warnx("login names may not begin with a hyphen");
     67 		return (1);
     68 	}
     69 	if (!(pw->pw_name = strdup(p))) {
     70 		warnx("can't save entry");
     71 		return (1);
     72 	}
     73 	if (strchr(p, '.'))
     74 		warnx("\'.\' is dangerous in a login name");
     75 	for (; *p; ++p)
     76 		if (isupper((unsigned char)*p)) {
     77 			warnx("upper-case letters are dangerous in a login name");
     78 			break;
     79 		}
     80 	return (0);
     81 }
     82 
     83 /* ARGSUSED */
     84 int
     85 p_passwd(const char *p, struct passwd *pw, ENTRY *ep)
     86 {
     87 
     88 	if (!*p)
     89 		pw->pw_passwd = "";	/* "NOLOGIN"; */
     90 	else if (!(pw->pw_passwd = strdup(p))) {
     91 		warnx("can't save password entry");
     92 		return (1);
     93 	}
     94 
     95 	return (0);
     96 }
     97 
     98 /* ARGSUSED */
     99 int
    100 p_uid(const char *p, struct passwd *pw, ENTRY *ep)
    101 {
    102 	unsigned long id;
    103 	char *np;
    104 
    105 	if (!*p) {
    106 		warnx("empty uid field");
    107 		return (1);
    108 	}
    109 	if (!isdigit((unsigned char)*p)) {
    110 		warnx("illegal uid");
    111 		return (1);
    112 	}
    113 	errno = 0;
    114 	id = strtoul(p, &np, 10);
    115 	/*
    116 	 * We don't need to check the return value of strtoul()
    117 	 * since ULONG_MAX is greater than UID_MAX.
    118 	 */
    119 	if (*np || id > UID_MAX) {
    120 		warnx("illegal uid");
    121 		return (1);
    122 	}
    123 	pw->pw_uid = (uid_t)id;
    124 	return (0);
    125 }
    126 
    127 /* ARGSUSED */
    128 int
    129 p_gid(const char *p, struct passwd *pw, ENTRY *ep)
    130 {
    131 	struct group *gr;
    132 	unsigned long id;
    133 	char *np;
    134 
    135 	if (!*p) {
    136 		warnx("empty gid field");
    137 		return (1);
    138 	}
    139 	if (!isdigit((unsigned char)*p)) {
    140 		if (!(gr = getgrnam(p))) {
    141 			warnx("unknown group %s", p);
    142 			return (1);
    143 		}
    144 		pw->pw_gid = gr->gr_gid;
    145 		return (0);
    146 	}
    147 	errno = 0;
    148 	id = strtoul(p, &np, 10);
    149 	/*
    150 	 * We don't need to check the return value of strtoul()
    151 	 * since ULONG_MAX is greater than GID_MAX.
    152 	 */
    153 	if (*np || id > GID_MAX) {
    154 		warnx("illegal gid");
    155 		return (1);
    156 	}
    157 	pw->pw_gid = (gid_t)id;
    158 	return (0);
    159 }
    160 
    161 /* ARGSUSED */
    162 int
    163 p_class(const char *p, struct passwd *pw, ENTRY *ep)
    164 {
    165 
    166 	if (!*p)
    167 		pw->pw_class = "";
    168 	else if (!(pw->pw_class = strdup(p))) {
    169 		warnx("can't save entry");
    170 		return (1);
    171 	}
    172 
    173 	return (0);
    174 }
    175 
    176 /* ARGSUSED */
    177 int
    178 p_change(const char *p, struct passwd *pw, ENTRY *ep)
    179 {
    180 
    181 	if (!atot(p, &pw->pw_change))
    182 		return (0);
    183 	warnx("illegal date for change field");
    184 	return (1);
    185 }
    186 
    187 /* ARGSUSED */
    188 int
    189 p_expire(const char *p, struct passwd *pw, ENTRY *ep)
    190 {
    191 
    192 	if (!atot(p, &pw->pw_expire))
    193 		return (0);
    194 	warnx("illegal date for expire field");
    195 	return (1);
    196 }
    197 
    198 /* ARGSUSED */
    199 int
    200 p_gecos(const char *p, struct passwd *pw, ENTRY *ep)
    201 {
    202 
    203 	if (!(ep->save = strdup(p))) {
    204 		warnx("can't save entry");
    205 		return (1);
    206 	}
    207 	return (0);
    208 }
    209 
    210 /* ARGSUSED */
    211 int
    212 p_hdir(const char *p, struct passwd *pw, ENTRY *ep)
    213 {
    214 
    215 	if (!*p) {
    216 		warnx("empty home directory field");
    217 		return (1);
    218 	}
    219 	if (!(pw->pw_dir = strdup(p))) {
    220 		warnx("can't save entry");
    221 		return (1);
    222 	}
    223 	return (0);
    224 }
    225 
    226 /* ARGSUSED */
    227 int
    228 p_shell(const char *p, struct passwd *pw, ENTRY *ep)
    229 {
    230 	const char *t;
    231 
    232 	if (!*p) {
    233 		pw->pw_shell = _PATH_BSHELL;
    234 		return (0);
    235 	}
    236 	/* only admin can change from or to "restricted" shells */
    237 	if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
    238 		warnx("%s: current shell non-standard", pw->pw_shell);
    239 		return (1);
    240 	}
    241 	if (!(t = ok_shell(p))) {
    242 		if (uid) {
    243 			warnx("%s: non-standard shell", p);
    244 			return (1);
    245 		}
    246 	}
    247 	else
    248 		p = t;
    249 	if (!(pw->pw_shell = strdup(p))) {
    250 		warnx("can't save entry");
    251 		return (1);
    252 	}
    253 	return (0);
    254 }
    255