1 /*- 2 * Copyright (c) 1990, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if defined(LIBC_SCCS) && !defined(lint) 31 static char sccsid[] = "@(#)pw_scan.c 8.3 (Berkeley) 4/2/94"; 32 #endif /* LIBC_SCCS and not lint */ 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 /* 37 * This module is used to "verify" password entries by chpass(1) and 38 * pwd_mkdb(8). 39 */ 40 41 #include <sys/param.h> 42 43 #include <err.h> 44 #include <errno.h> 45 #include <fcntl.h> 46 #include <pwd.h> 47 #include <stdio.h> 48 #include <string.h> 49 #include <stdlib.h> 50 #include <unistd.h> 51 52 #include "pw_scan.h" 53 54 /* 55 * Some software assumes that IDs are short. We should emit warnings 56 * for id's which cannot be stored in a short, but we are more liberal 57 * by default, warning for IDs greater than USHRT_MAX. 58 * 59 * If pw_big_ids_warning is -1 on entry to pw_scan(), it will be set based 60 * on the existence of PW_SCAN_BIG_IDS in the environment. 61 * 62 * It is believed all baseline system software that can not handle the 63 * normal ID sizes is now gone so pw_big_ids_warning is disabled for now. 64 * But the code has been left in place in case end-users want to re-enable 65 * it and/or for the next time the ID sizes get bigger but pieces of the 66 * system lag behind. 67 */ 68 static int pw_big_ids_warning = 0; 69 70 int 71 __pw_scan(char *bp, struct passwd *pw, int flags) 72 { 73 uid_t id; 74 int root; 75 char *ep, *p, *sh; 76 unsigned long temp; 77 78 if (pw_big_ids_warning == -1) 79 pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0; 80 81 pw->pw_fields = 0; 82 if (!(pw->pw_name = strsep(&bp, ":"))) /* login */ 83 goto fmt; 84 root = !strcmp(pw->pw_name, "root"); 85 if (pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0')) 86 pw->pw_fields |= _PWF_NAME; 87 88 if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */ 89 goto fmt; 90 if (pw->pw_passwd[0]) 91 pw->pw_fields |= _PWF_PASSWD; 92 93 if (!(p = strsep(&bp, ":"))) /* uid */ 94 goto fmt; 95 if (p[0]) 96 pw->pw_fields |= _PWF_UID; 97 else { 98 if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') { 99 if (flags & _PWSCAN_WARN) 100 warnx("no uid for user %s", pw->pw_name); 101 return (0); 102 } 103 } 104 errno = 0; 105 temp = strtoul(p, &ep, 10); 106 if ((temp == ULONG_MAX && errno == ERANGE) || temp > UID_MAX) { 107 if (flags & _PWSCAN_WARN) 108 warnx("%s > max uid value (%u)", p, UID_MAX); 109 return (0); 110 } 111 id = temp; 112 if (*ep != '\0') { 113 if (flags & _PWSCAN_WARN) 114 warnx("%s uid is incorrect", p); 115 return (0); 116 } 117 if (root && id) { 118 if (flags & _PWSCAN_WARN) 119 warnx("root uid should be 0"); 120 return (0); 121 } 122 if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) { 123 warnx("%s > recommended max uid value (%u)", p, USHRT_MAX); 124 /*return (0);*/ /* THIS SHOULD NOT BE FATAL! */ 125 } 126 pw->pw_uid = id; 127 128 if (!(p = strsep(&bp, ":"))) /* gid */ 129 goto fmt; 130 if (p[0]) 131 pw->pw_fields |= _PWF_GID; 132 else { 133 if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') { 134 if (flags & _PWSCAN_WARN) 135 warnx("no gid for user %s", pw->pw_name); 136 return (0); 137 } 138 } 139 errno = 0; 140 temp = strtoul(p, &ep, 10); 141 if ((temp == ULONG_MAX && errno == ERANGE) || temp > GID_MAX) { 142 if (flags & _PWSCAN_WARN) 143 warnx("%s > max gid value (%u)", p, GID_MAX); 144 return (0); 145 } 146 id = temp; 147 if (*ep != '\0') { 148 if (flags & _PWSCAN_WARN) 149 warnx("%s gid is incorrect", p); 150 return (0); 151 } 152 if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) { 153 warnx("%s > recommended max gid value (%u)", p, USHRT_MAX); 154 /* return (0); This should not be fatal! */ 155 } 156 pw->pw_gid = id; 157 158 if (flags & _PWSCAN_MASTER ) { 159 if (!(pw->pw_class = strsep(&bp, ":"))) /* class */ 160 goto fmt; 161 if (pw->pw_class[0]) 162 pw->pw_fields |= _PWF_CLASS; 163 164 if (!(p = strsep(&bp, ":"))) /* change */ 165 goto fmt; 166 if (p[0]) 167 pw->pw_fields |= _PWF_CHANGE; 168 pw->pw_change = atol(p); 169 170 if (!(p = strsep(&bp, ":"))) /* expire */ 171 goto fmt; 172 if (p[0]) 173 pw->pw_fields |= _PWF_EXPIRE; 174 pw->pw_expire = atol(p); 175 } 176 if (!(pw->pw_gecos = strsep(&bp, ":"))) /* gecos */ 177 goto fmt; 178 if (pw->pw_gecos[0]) 179 pw->pw_fields |= _PWF_GECOS; 180 181 if (!(pw->pw_dir = strsep(&bp, ":"))) /* directory */ 182 goto fmt; 183 if (pw->pw_dir[0]) 184 pw->pw_fields |= _PWF_DIR; 185 186 if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */ 187 goto fmt; 188 189 p = pw->pw_shell; 190 if (root && *p) { /* empty == /bin/sh */ 191 for (setusershell();;) { 192 if (!(sh = getusershell())) { 193 if (flags & _PWSCAN_WARN) 194 warnx("warning, unknown root shell"); 195 break; 196 } 197 if (!strcmp(p, sh)) 198 break; 199 } 200 endusershell(); 201 } 202 if (p[0]) 203 pw->pw_fields |= _PWF_SHELL; 204 205 if ((p = strsep(&bp, ":"))) { /* too many */ 206 fmt: 207 if (flags & _PWSCAN_WARN) 208 warnx("corrupted entry"); 209 return (0); 210 } 211 return (1); 212 } 213