Home | History | Annotate | Line # | Download | only in chpass
      1 /*	$NetBSD: field.c,v 1.12 2009/04/11 12:10:02 lukem 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.12 2009/04/11 12:10:02 lukem 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 (!(pw->pw_passwd = strdup(p))) {
     89 		warnx("can't save password entry");
     90 		return (1);
     91 	}
     92 
     93 	return (0);
     94 }
     95 
     96 /* ARGSUSED */
     97 int
     98 p_uid(const char *p, struct passwd *pw, ENTRY *ep)
     99 {
    100 	unsigned long id;
    101 	char *np;
    102 
    103 	if (!*p) {
    104 		warnx("empty uid field");
    105 		return (1);
    106 	}
    107 	if (!isdigit((unsigned char)*p)) {
    108 		warnx("illegal uid");
    109 		return (1);
    110 	}
    111 	errno = 0;
    112 	id = strtoul(p, &np, 10);
    113 	/*
    114 	 * We don't need to check the return value of strtoul()
    115 	 * since ULONG_MAX is greater than UID_MAX.
    116 	 */
    117 	if (*np || id > UID_MAX) {
    118 		warnx("illegal uid");
    119 		return (1);
    120 	}
    121 	pw->pw_uid = (uid_t)id;
    122 	return (0);
    123 }
    124 
    125 /* ARGSUSED */
    126 int
    127 p_gid(const char *p, struct passwd *pw, ENTRY *ep)
    128 {
    129 	struct group *gr;
    130 	unsigned long id;
    131 	char *np;
    132 
    133 	if (!*p) {
    134 		warnx("empty gid field");
    135 		return (1);
    136 	}
    137 	if (!isdigit((unsigned char)*p)) {
    138 		if (!(gr = getgrnam(p))) {
    139 			warnx("unknown group %s", p);
    140 			return (1);
    141 		}
    142 		pw->pw_gid = gr->gr_gid;
    143 		return (0);
    144 	}
    145 	errno = 0;
    146 	id = strtoul(p, &np, 10);
    147 	/*
    148 	 * We don't need to check the return value of strtoul()
    149 	 * since ULONG_MAX is greater than GID_MAX.
    150 	 */
    151 	if (*np || id > GID_MAX) {
    152 		warnx("illegal gid");
    153 		return (1);
    154 	}
    155 	pw->pw_gid = (gid_t)id;
    156 	return (0);
    157 }
    158 
    159 /* ARGSUSED */
    160 int
    161 p_class(const char *p, struct passwd *pw, ENTRY *ep)
    162 {
    163 
    164 	if (!(pw->pw_class = strdup(p))) {
    165 		warnx("can't save entry");
    166 		return (1);
    167 	}
    168 
    169 	return (0);
    170 }
    171 
    172 /* ARGSUSED */
    173 int
    174 p_change(const char *p, struct passwd *pw, ENTRY *ep)
    175 {
    176 
    177 	if (!atot(p, &pw->pw_change))
    178 		return (0);
    179 	warnx("illegal date for change field");
    180 	return (1);
    181 }
    182 
    183 /* ARGSUSED */
    184 int
    185 p_expire(const char *p, struct passwd *pw, ENTRY *ep)
    186 {
    187 
    188 	if (!atot(p, &pw->pw_expire))
    189 		return (0);
    190 	warnx("illegal date for expire field");
    191 	return (1);
    192 }
    193 
    194 /* ARGSUSED */
    195 int
    196 p_gecos(const char *p, struct passwd *pw, ENTRY *ep)
    197 {
    198 
    199 	if (!(ep->save = strdup(p))) {
    200 		warnx("can't save entry");
    201 		return (1);
    202 	}
    203 	return (0);
    204 }
    205 
    206 /* ARGSUSED */
    207 int
    208 p_hdir(const char *p, struct passwd *pw, ENTRY *ep)
    209 {
    210 
    211 	if (!*p) {
    212 		warnx("empty home directory field");
    213 		return (1);
    214 	}
    215 	if (!(pw->pw_dir = strdup(p))) {
    216 		warnx("can't save entry");
    217 		return (1);
    218 	}
    219 	return (0);
    220 }
    221 
    222 /* ARGSUSED */
    223 int
    224 p_shell(const char *p, struct passwd *pw, ENTRY *ep)
    225 {
    226 	const char *t;
    227 
    228 	if (!*p) {
    229 		if (!(pw->pw_shell = strdup(_PATH_BSHELL))) {
    230 			warnx("can't save entry");
    231 			return (1);
    232 		}
    233 		return (0);
    234 	}
    235 	/* only admin can change from or to "restricted" shells */
    236 	if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
    237 		warnx("%s: current shell non-standard", pw->pw_shell);
    238 		return (1);
    239 	}
    240 	if (!(t = ok_shell(p))) {
    241 		if (uid) {
    242 			warnx("%s: non-standard shell", p);
    243 			return (1);
    244 		}
    245 	}
    246 	else
    247 		p = t;
    248 	if (!(pw->pw_shell = strdup(p))) {
    249 		warnx("can't save entry");
    250 		return (1);
    251 	}
    252 	return (0);
    253 }
    254