1dea673e9SRodney W. Grimes /*- 2dea673e9SRodney W. Grimes * Copyright (c) 1990, 1993, 1994 3dea673e9SRodney W. Grimes * The Regents of the University of California. All rights reserved. 4dea673e9SRodney W. Grimes * 5dea673e9SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6dea673e9SRodney W. Grimes * modification, are permitted provided that the following conditions 7dea673e9SRodney W. Grimes * are met: 8dea673e9SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10dea673e9SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12dea673e9SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13dea673e9SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14dea673e9SRodney W. Grimes * may be used to endorse or promote products derived from this software 15dea673e9SRodney W. Grimes * without specific prior written permission. 16dea673e9SRodney W. Grimes * 17dea673e9SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18dea673e9SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19dea673e9SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20dea673e9SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21dea673e9SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22dea673e9SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23dea673e9SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24dea673e9SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25dea673e9SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26dea673e9SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27dea673e9SRodney W. Grimes * SUCH DAMAGE. 28dea673e9SRodney W. Grimes */ 29dea673e9SRodney W. Grimes 30ea8d448aSDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint) 31dea673e9SRodney W. Grimes static char sccsid[] = "@(#)pw_scan.c 8.3 (Berkeley) 4/2/94"; 32ea8d448aSDavid E. O'Brien #endif /* LIBC_SCCS and not lint */ 33ea8d448aSDavid E. O'Brien #include <sys/cdefs.h> 34ea8d448aSDavid E. O'Brien __FBSDID("$FreeBSD$"); 35dea673e9SRodney W. Grimes 36dea673e9SRodney W. Grimes /* 37dea673e9SRodney W. Grimes * This module is used to "verify" password entries by chpass(1) and 38dea673e9SRodney W. Grimes * pwd_mkdb(8). 39dea673e9SRodney W. Grimes */ 40dea673e9SRodney W. Grimes 41dea673e9SRodney W. Grimes #include <sys/param.h> 42dea673e9SRodney W. Grimes 43dea673e9SRodney W. Grimes #include <err.h> 44cd7b8d78SPaul Richards #include <errno.h> 45dea673e9SRodney W. Grimes #include <fcntl.h> 46dea673e9SRodney W. Grimes #include <pwd.h> 47dea673e9SRodney W. Grimes #include <stdio.h> 48dea673e9SRodney W. Grimes #include <string.h> 49dea673e9SRodney W. Grimes #include <stdlib.h> 50dea673e9SRodney W. Grimes #include <unistd.h> 51dea673e9SRodney W. Grimes 52dea673e9SRodney W. Grimes #include "pw_scan.h" 53dea673e9SRodney W. Grimes 5418138b08SSheldon Hearn /* 5518138b08SSheldon Hearn * Some software assumes that IDs are short. We should emit warnings 5618138b08SSheldon Hearn * for id's which cannot be stored in a short, but we are more liberal 5718138b08SSheldon Hearn * by default, warning for IDs greater than USHRT_MAX. 589a602accSSheldon Hearn * 59235d6772SDima Dorfman * If pw_big_ids_warning is -1 on entry to pw_scan(), it will be set based 60235d6772SDima Dorfman * on the existence of PW_SCAN_BIG_IDS in the environment. 61764eca25SKen Smith * 62764eca25SKen Smith * It is believed all baseline system software that can not handle the 63764eca25SKen Smith * normal ID sizes is now gone so pw_big_ids_warning is disabled for now. 64764eca25SKen Smith * But the code has been left in place in case end-users want to re-enable 65764eca25SKen Smith * it and/or for the next time the ID sizes get bigger but pieces of the 66764eca25SKen Smith * system lag behind. 6718138b08SSheldon Hearn */ 68764eca25SKen Smith static int pw_big_ids_warning = 0; 6918138b08SSheldon Hearn 70dea673e9SRodney W. Grimes int 71248aee62SJacques Vidrine __pw_scan(char *bp, struct passwd *pw, int flags) 72dea673e9SRodney W. Grimes { 73cd7b8d78SPaul Richards uid_t id; 74dea673e9SRodney W. Grimes int root; 7505a7daf5SMaxim Konovalov char *ep, *p, *sh; 76764eca25SKen Smith unsigned long temp; 77dea673e9SRodney W. Grimes 789a602accSSheldon Hearn if (pw_big_ids_warning == -1) 799a602accSSheldon Hearn pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0; 809a602accSSheldon Hearn 8128ca3091SGarrett Wollman pw->pw_fields = 0; 82dea673e9SRodney W. Grimes if (!(pw->pw_name = strsep(&bp, ":"))) /* login */ 83dea673e9SRodney W. Grimes goto fmt; 84dea673e9SRodney W. Grimes root = !strcmp(pw->pw_name, "root"); 8528ca3091SGarrett Wollman if (pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0')) 8628ca3091SGarrett Wollman pw->pw_fields |= _PWF_NAME; 87dea673e9SRodney W. Grimes 88dea673e9SRodney W. Grimes if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */ 89dea673e9SRodney W. Grimes goto fmt; 906478822fSDag-Erling Smørgrav if (pw->pw_passwd[0]) 916478822fSDag-Erling Smørgrav pw->pw_fields |= _PWF_PASSWD; 92dea673e9SRodney W. Grimes 93dea673e9SRodney W. Grimes if (!(p = strsep(&bp, ":"))) /* uid */ 94dea673e9SRodney W. Grimes goto fmt; 95aa510d67SEivind Eklund if (p[0]) 96aa510d67SEivind Eklund pw->pw_fields |= _PWF_UID; 97aa510d67SEivind Eklund else { 98637bc596SEivind Eklund if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') { 99248aee62SJacques Vidrine if (flags & _PWSCAN_WARN) 100aa510d67SEivind Eklund warnx("no uid for user %s", pw->pw_name); 101aa510d67SEivind Eklund return (0); 102aa510d67SEivind Eklund } 103637bc596SEivind Eklund } 104764eca25SKen Smith errno = 0; 105764eca25SKen Smith temp = strtoul(p, &ep, 10); 106764eca25SKen Smith if ((temp == ULONG_MAX && errno == ERANGE) || temp > UID_MAX) { 107248aee62SJacques Vidrine if (flags & _PWSCAN_WARN) 108764eca25SKen Smith warnx("%s > max uid value (%u)", p, UID_MAX); 109cd7b8d78SPaul Richards return (0); 110cd7b8d78SPaul Richards } 111764eca25SKen Smith id = temp; 1129b7c7ff8SMaxim Konovalov if (*ep != '\0') { 11305a7daf5SMaxim Konovalov if (flags & _PWSCAN_WARN) 11405a7daf5SMaxim Konovalov warnx("%s uid is incorrect", p); 11505a7daf5SMaxim Konovalov return (0); 11605a7daf5SMaxim Konovalov } 117dea673e9SRodney W. Grimes if (root && id) { 118248aee62SJacques Vidrine if (flags & _PWSCAN_WARN) 119dea673e9SRodney W. Grimes warnx("root uid should be 0"); 120dea673e9SRodney W. Grimes return (0); 121dea673e9SRodney W. Grimes } 122248aee62SJacques Vidrine if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) { 123cd7b8d78SPaul Richards warnx("%s > recommended max uid value (%u)", p, USHRT_MAX); 12433d37c13SSheldon Hearn /*return (0);*/ /* THIS SHOULD NOT BE FATAL! */ 125dea673e9SRodney W. Grimes } 126dea673e9SRodney W. Grimes pw->pw_uid = id; 127dea673e9SRodney W. Grimes 128dea673e9SRodney W. Grimes if (!(p = strsep(&bp, ":"))) /* gid */ 129dea673e9SRodney W. Grimes goto fmt; 1306478822fSDag-Erling Smørgrav if (p[0]) 1316478822fSDag-Erling Smørgrav pw->pw_fields |= _PWF_GID; 13265edeb23SMaxim Konovalov else { 13365edeb23SMaxim Konovalov if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') { 13465edeb23SMaxim Konovalov if (flags & _PWSCAN_WARN) 13565edeb23SMaxim Konovalov warnx("no gid for user %s", pw->pw_name); 13665edeb23SMaxim Konovalov return (0); 13765edeb23SMaxim Konovalov } 13865edeb23SMaxim Konovalov } 139764eca25SKen Smith errno = 0; 140764eca25SKen Smith temp = strtoul(p, &ep, 10); 141764eca25SKen Smith if ((temp == ULONG_MAX && errno == ERANGE) || temp > GID_MAX) { 142248aee62SJacques Vidrine if (flags & _PWSCAN_WARN) 143764eca25SKen Smith warnx("%s > max gid value (%u)", p, GID_MAX); 144cd7b8d78SPaul Richards return (0); 145cd7b8d78SPaul Richards } 146764eca25SKen Smith id = temp; 1479b7c7ff8SMaxim Konovalov if (*ep != '\0') { 14805a7daf5SMaxim Konovalov if (flags & _PWSCAN_WARN) 14905a7daf5SMaxim Konovalov warnx("%s gid is incorrect", p); 15005a7daf5SMaxim Konovalov return (0); 15105a7daf5SMaxim Konovalov } 152248aee62SJacques Vidrine if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) { 153cd7b8d78SPaul Richards warnx("%s > recommended max gid value (%u)", p, USHRT_MAX); 15433d37c13SSheldon Hearn /* return (0); This should not be fatal! */ 155dea673e9SRodney W. Grimes } 156dea673e9SRodney W. Grimes pw->pw_gid = id; 157dea673e9SRodney W. Grimes 158248aee62SJacques Vidrine if (flags & _PWSCAN_MASTER ) { 159a8adfe18SDag-Erling Smørgrav if (!(pw->pw_class = strsep(&bp, ":"))) /* class */ 160a8adfe18SDag-Erling Smørgrav goto fmt; 1616478822fSDag-Erling Smørgrav if (pw->pw_class[0]) 1626478822fSDag-Erling Smørgrav pw->pw_fields |= _PWF_CLASS; 16328ca3091SGarrett Wollman 164dea673e9SRodney W. Grimes if (!(p = strsep(&bp, ":"))) /* change */ 165dea673e9SRodney W. Grimes goto fmt; 1666478822fSDag-Erling Smørgrav if (p[0]) 1676478822fSDag-Erling Smørgrav pw->pw_fields |= _PWF_CHANGE; 168dea673e9SRodney W. Grimes pw->pw_change = atol(p); 16928ca3091SGarrett Wollman 170dea673e9SRodney W. Grimes if (!(p = strsep(&bp, ":"))) /* expire */ 171dea673e9SRodney W. Grimes goto fmt; 1726478822fSDag-Erling Smørgrav if (p[0]) 1736478822fSDag-Erling Smørgrav pw->pw_fields |= _PWF_EXPIRE; 174dea673e9SRodney W. Grimes pw->pw_expire = atol(p); 175248aee62SJacques Vidrine } 176cc6f6281SDavid Greenman if (!(pw->pw_gecos = strsep(&bp, ":"))) /* gecos */ 177cc6f6281SDavid Greenman goto fmt; 1786478822fSDag-Erling Smørgrav if (pw->pw_gecos[0]) 1796478822fSDag-Erling Smørgrav pw->pw_fields |= _PWF_GECOS; 18028ca3091SGarrett Wollman 181cc6f6281SDavid Greenman if (!(pw->pw_dir = strsep(&bp, ":"))) /* directory */ 182cc6f6281SDavid Greenman goto fmt; 1836478822fSDag-Erling Smørgrav if (pw->pw_dir[0]) 1846478822fSDag-Erling Smørgrav pw->pw_fields |= _PWF_DIR; 18528ca3091SGarrett Wollman 186dea673e9SRodney W. Grimes if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */ 187dea673e9SRodney W. Grimes goto fmt; 188dea673e9SRodney W. Grimes 189dea673e9SRodney W. Grimes p = pw->pw_shell; 190c4fe9d66SPhilippe Charnier if (root && *p) { /* empty == /bin/sh */ 191dea673e9SRodney W. Grimes for (setusershell();;) { 192dea673e9SRodney W. Grimes if (!(sh = getusershell())) { 193248aee62SJacques Vidrine if (flags & _PWSCAN_WARN) 194dea673e9SRodney W. Grimes warnx("warning, unknown root shell"); 195dea673e9SRodney W. Grimes break; 196dea673e9SRodney W. Grimes } 197dea673e9SRodney W. Grimes if (!strcmp(p, sh)) 198dea673e9SRodney W. Grimes break; 199dea673e9SRodney W. Grimes } 200c4fe9d66SPhilippe Charnier endusershell(); 201c4fe9d66SPhilippe Charnier } 2026478822fSDag-Erling Smørgrav if (p[0]) 2036478822fSDag-Erling Smørgrav pw->pw_fields |= _PWF_SHELL; 204dea673e9SRodney W. Grimes 2058b76e1d7SPhilippe Charnier if ((p = strsep(&bp, ":"))) { /* too many */ 206248aee62SJacques Vidrine fmt: 207248aee62SJacques Vidrine if (flags & _PWSCAN_WARN) 208248aee62SJacques Vidrine warnx("corrupted entry"); 209dea673e9SRodney W. Grimes return (0); 210dea673e9SRodney W. Grimes } 211dea673e9SRodney W. Grimes return (1); 212dea673e9SRodney W. Grimes } 213