1 /* $OpenBSD: field.c,v 1.6 2003/07/01 01:01:28 avsm Exp $ */ 2 /* $NetBSD: field.c,v 1.3 1995/03/26 04:55:28 glass Exp $ */ 3 4 /* 5 * Copyright (c) 1988, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)field.c 8.4 (Berkeley) 4/2/94"; 36 #else 37 static char rcsid[] = "$OpenBSD: field.c,v 1.6 2003/07/01 01:01:28 avsm 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 <paths.h> 48 #include <pwd.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 #include "chpass.h" 55 56 /* ARGSUSED */ 57 int 58 p_login(char *p, struct passwd *pw, ENTRY *ep) 59 { 60 if (!*p) { 61 warnx("empty login field"); 62 return (1); 63 } 64 if (*p == '-') { 65 warnx("login names may not begin with a hyphen"); 66 return (1); 67 } 68 if (!(pw->pw_name = strdup(p))) { 69 warnx("can't save entry"); 70 return (1); 71 } 72 if (strchr(p, '.')) 73 warnx("\'.\' is dangerous in a login name"); 74 for (; *p; ++p) 75 if (isupper(*p)) { 76 warnx("upper-case letters are dangerous in a login name"); 77 break; 78 } 79 return (0); 80 } 81 82 /* ARGSUSED */ 83 int 84 p_passwd(char *p, struct passwd *pw, ENTRY *ep) 85 { 86 if (!*p) 87 pw->pw_passwd = ""; /* "NOLOGIN"; */ 88 else 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(char *p, struct passwd *pw, ENTRY *ep) 99 { 100 uid_t id; 101 char *np; 102 103 if (!*p) { 104 warnx("empty uid field"); 105 return (1); 106 } 107 if (!isdigit(*p)) { 108 warnx("illegal uid"); 109 return (1); 110 } 111 errno = 0; 112 id = strtoul(p, &np, 10); 113 if (*np || (id == ULONG_MAX && errno == ERANGE)) { 114 warnx("illegal uid"); 115 return (1); 116 } 117 pw->pw_uid = id; 118 return (0); 119 } 120 121 /* ARGSUSED */ 122 int 123 p_gid(char *p, struct passwd *pw, ENTRY *ep) 124 { 125 struct group *gr; 126 gid_t id; 127 char *np; 128 129 if (!*p) { 130 warnx("empty gid field"); 131 return (1); 132 } 133 if (!isdigit(*p)) { 134 if (!(gr = getgrnam(p))) { 135 warnx("unknown group %s", p); 136 return (1); 137 } 138 pw->pw_gid = gr->gr_gid; 139 return (0); 140 } 141 errno = 0; 142 id = strtoul(p, &np, 10); 143 if (*np || (id == ULONG_MAX && errno == ERANGE)) { 144 warnx("illegal gid"); 145 return (1); 146 } 147 pw->pw_gid = id; 148 return (0); 149 } 150 151 /* ARGSUSED */ 152 int 153 p_class(char *p, struct passwd *pw, ENTRY *ep) 154 { 155 if (!*p) 156 pw->pw_class = ""; 157 else if (!(pw->pw_class = strdup(p))) { 158 warnx("can't save entry"); 159 return (1); 160 } 161 162 return (0); 163 } 164 165 /* ARGSUSED */ 166 int 167 p_change(char *p, struct passwd *pw, ENTRY *ep) 168 { 169 if (!atot(p, &pw->pw_change)) 170 return (0); 171 warnx("illegal date for change field"); 172 return (1); 173 } 174 175 /* ARGSUSED */ 176 int 177 p_expire(char *p, struct passwd *pw, ENTRY *ep) 178 { 179 if (!atot(p, &pw->pw_expire)) 180 return (0); 181 warnx("illegal date for expire field"); 182 return (1); 183 } 184 185 /* ARGSUSED */ 186 int 187 p_gecos(char *p, struct passwd *pw, ENTRY *ep) 188 { 189 if (!*p) 190 ep->save = ""; 191 else if (!(ep->save = strdup(p))) { 192 warnx("can't save entry"); 193 return (1); 194 } 195 return (0); 196 } 197 198 /* ARGSUSED */ 199 int 200 p_hdir(char *p, struct passwd *pw, ENTRY *ep) 201 { 202 if (!*p) { 203 warnx("empty home directory field"); 204 return (1); 205 } 206 if (!(pw->pw_dir = strdup(p))) { 207 warnx("can't save entry"); 208 return (1); 209 } 210 return (0); 211 } 212 213 /* ARGSUSED */ 214 int 215 p_shell(char *p, struct passwd *pw, ENTRY *ep) 216 { 217 char *t; 218 219 if (!*p) { 220 pw->pw_shell = _PATH_BSHELL; 221 return (0); 222 } 223 /* only admin can change from or to "restricted" shells */ 224 if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) { 225 warnx("%s: current shell non-standard", pw->pw_shell); 226 return (1); 227 } 228 if (!(t = ok_shell(p))) { 229 if (uid) { 230 warnx("%s: non-standard shell", p); 231 return (1); 232 } 233 } else 234 p = t; 235 if (!(pw->pw_shell = strdup(p))) { 236 warnx("can't save entry"); 237 return (1); 238 } 239 return (0); 240 } 241