1*f14fb602SLionel Sambuc /* $NetBSD: pw_scan.c,v 1.23 2012/03/13 21:13:36 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1987, 1993, 1994, 1995
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #if HAVE_NBTOOL_CONFIG_H
332fe8fb19SBen Gras #include "nbtool_config.h"
342fe8fb19SBen Gras #include "compat_pwd.h"
352fe8fb19SBen Gras
362fe8fb19SBen Gras #else
372fe8fb19SBen Gras #include <sys/cdefs.h>
382fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
39*f14fb602SLionel Sambuc __RCSID("$NetBSD: pw_scan.c,v 1.23 2012/03/13 21:13:36 christos Exp $");
402fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
412fe8fb19SBen Gras
422fe8fb19SBen Gras #if defined(_LIBC)
432fe8fb19SBen Gras #include "namespace.h"
442fe8fb19SBen Gras #endif
452fe8fb19SBen Gras #include <sys/types.h>
462fe8fb19SBen Gras
472fe8fb19SBen Gras #include <assert.h>
482fe8fb19SBen Gras #include <err.h>
492fe8fb19SBen Gras #include <limits.h>
502fe8fb19SBen Gras #include <pwd.h>
512fe8fb19SBen Gras #include <stdio.h>
522fe8fb19SBen Gras #include <stdlib.h>
532fe8fb19SBen Gras #include <string.h>
542fe8fb19SBen Gras #include <unistd.h>
552fe8fb19SBen Gras #include <errno.h>
562fe8fb19SBen Gras
572fe8fb19SBen Gras #ifdef _LIBC
582fe8fb19SBen Gras #include "pw_private.h"
592fe8fb19SBen Gras #endif
602fe8fb19SBen Gras #endif /* ! HAVE_NBTOOL_CONFIG_H */
612fe8fb19SBen Gras
622fe8fb19SBen Gras static int
gettime(time_t * res,const char * p,int * flags,int dowarn,int flag)63*f14fb602SLionel Sambuc gettime(time_t *res, const char *p, int *flags, int dowarn, int flag)
642fe8fb19SBen Gras {
652fe8fb19SBen Gras long long l;
662fe8fb19SBen Gras char *ep;
672fe8fb19SBen Gras const char *vp;
682fe8fb19SBen Gras
692fe8fb19SBen Gras if (*p == '\0') {
702fe8fb19SBen Gras *flags |= flag;
712fe8fb19SBen Gras *res = 0;
722fe8fb19SBen Gras return 1;
732fe8fb19SBen Gras }
742fe8fb19SBen Gras l = strtoll(p, &ep, 0);
752fe8fb19SBen Gras if (p == ep || *ep != '\0') {
762fe8fb19SBen Gras vp = "Invalid number";
772fe8fb19SBen Gras goto done;
782fe8fb19SBen Gras }
792fe8fb19SBen Gras if (errno == ERANGE && (l == LLONG_MAX || l == LLONG_MIN)) {
802fe8fb19SBen Gras vp = strerror(errno);
812fe8fb19SBen Gras goto done;
822fe8fb19SBen Gras }
83*f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(time_t, l));
84*f14fb602SLionel Sambuc *res = (time_t)l;
852fe8fb19SBen Gras return 1;
862fe8fb19SBen Gras done:
872fe8fb19SBen Gras if (dowarn) {
882fe8fb19SBen Gras warnx("%s `%s' for %s time", vp, p,
892fe8fb19SBen Gras flag == _PASSWORD_NOEXP ? "expiration" : "change");
902fe8fb19SBen Gras }
912fe8fb19SBen Gras return 0;
922fe8fb19SBen Gras
932fe8fb19SBen Gras }
942fe8fb19SBen Gras
952fe8fb19SBen Gras static int
getid(unsigned long * res,const char * p,int * flags,int dowarn,int flag)962fe8fb19SBen Gras getid(unsigned long *res, const char *p, int *flags, int dowarn, int flag)
972fe8fb19SBen Gras {
982fe8fb19SBen Gras unsigned long ul;
992fe8fb19SBen Gras char *ep;
1002fe8fb19SBen Gras
1012fe8fb19SBen Gras if (*p == '\0') {
1022fe8fb19SBen Gras *flags |= flag;
1032fe8fb19SBen Gras *res = 0;
1042fe8fb19SBen Gras return 1;
1052fe8fb19SBen Gras }
1062fe8fb19SBen Gras ul = strtoul(p, &ep, 0);
1072fe8fb19SBen Gras if (p == ep || *ep != '\0') {
1082fe8fb19SBen Gras ep = __UNCONST("Invalid number");
1092fe8fb19SBen Gras goto done;
1102fe8fb19SBen Gras }
1112fe8fb19SBen Gras if (errno == ERANGE && ul == ULONG_MAX) {
1122fe8fb19SBen Gras ep = strerror(errno);
1132fe8fb19SBen Gras goto done;
1142fe8fb19SBen Gras }
1152fe8fb19SBen Gras if (ul > *res) {
1162fe8fb19SBen Gras ep = strerror(ERANGE);
1172fe8fb19SBen Gras goto done;
1182fe8fb19SBen Gras }
1192fe8fb19SBen Gras
1202fe8fb19SBen Gras *res = ul;
1212fe8fb19SBen Gras return 1;
1222fe8fb19SBen Gras done:
1232fe8fb19SBen Gras if (dowarn)
1242fe8fb19SBen Gras warnx("%s %s `%s'", ep,
1252fe8fb19SBen Gras flag == _PASSWORD_NOUID ? "uid" : "gid", p);
1262fe8fb19SBen Gras return 0;
1272fe8fb19SBen Gras
1282fe8fb19SBen Gras }
1292fe8fb19SBen Gras
1302fe8fb19SBen Gras int
1312fe8fb19SBen Gras #ifdef _LIBC
__pw_scan(char * bp,struct passwd * pw,int * flags)1322fe8fb19SBen Gras __pw_scan(char *bp, struct passwd *pw, int *flags)
1332fe8fb19SBen Gras #else
1342fe8fb19SBen Gras pw_scan( char *bp, struct passwd *pw, int *flags)
1352fe8fb19SBen Gras #endif
1362fe8fb19SBen Gras {
1372fe8fb19SBen Gras unsigned long id;
138*f14fb602SLionel Sambuc time_t ti;
1392fe8fb19SBen Gras int root, inflags;
1402fe8fb19SBen Gras int dowarn;
1412fe8fb19SBen Gras const char *p, *sh;
1422fe8fb19SBen Gras
1432fe8fb19SBen Gras _DIAGASSERT(bp != NULL);
1442fe8fb19SBen Gras _DIAGASSERT(pw != NULL);
1452fe8fb19SBen Gras
1462fe8fb19SBen Gras if (flags) {
1472fe8fb19SBen Gras inflags = *flags;
1482fe8fb19SBen Gras *flags = 0;
1492fe8fb19SBen Gras } else {
1502fe8fb19SBen Gras inflags = 0;
1512fe8fb19SBen Gras flags = &inflags;
1522fe8fb19SBen Gras }
1532fe8fb19SBen Gras dowarn = !(inflags & _PASSWORD_NOWARN);
1542fe8fb19SBen Gras
1552fe8fb19SBen Gras if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
1562fe8fb19SBen Gras goto fmt;
1572fe8fb19SBen Gras if (strlen(pw->pw_name) > (LOGIN_NAME_MAX - 1)) {
1582fe8fb19SBen Gras if (dowarn)
1592fe8fb19SBen Gras warnx("username too long, `%s' > %d", pw->pw_name,
1602fe8fb19SBen Gras LOGIN_NAME_MAX - 1);
1612fe8fb19SBen Gras return 0;
1622fe8fb19SBen Gras }
1632fe8fb19SBen Gras
1642fe8fb19SBen Gras root = !strcmp(pw->pw_name, "root");
1652fe8fb19SBen Gras
1662fe8fb19SBen Gras if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
1672fe8fb19SBen Gras goto fmt;
1682fe8fb19SBen Gras
1692fe8fb19SBen Gras if (!(p = strsep(&bp, ":"))) /* uid */
1702fe8fb19SBen Gras goto fmt;
1712fe8fb19SBen Gras
1722fe8fb19SBen Gras id = UID_MAX;
1732fe8fb19SBen Gras if (!getid(&id, p, flags, dowarn, _PASSWORD_NOUID))
1742fe8fb19SBen Gras return 0;
1752fe8fb19SBen Gras
1762fe8fb19SBen Gras if (root && id) {
1772fe8fb19SBen Gras if (dowarn)
1782fe8fb19SBen Gras warnx("root uid should be 0");
1792fe8fb19SBen Gras return 0;
1802fe8fb19SBen Gras }
1812fe8fb19SBen Gras
1822fe8fb19SBen Gras pw->pw_uid = (uid_t)id;
1832fe8fb19SBen Gras
1842fe8fb19SBen Gras if (!(p = strsep(&bp, ":"))) /* gid */
1852fe8fb19SBen Gras goto fmt;
1862fe8fb19SBen Gras
1872fe8fb19SBen Gras id = GID_MAX;
1882fe8fb19SBen Gras if (!getid(&id, p, flags, dowarn, _PASSWORD_NOGID))
1892fe8fb19SBen Gras return 0;
1902fe8fb19SBen Gras
1912fe8fb19SBen Gras pw->pw_gid = (gid_t)id;
1922fe8fb19SBen Gras
1932fe8fb19SBen Gras if (inflags & _PASSWORD_OLDFMT) {
1942fe8fb19SBen Gras pw->pw_class = __UNCONST("");
1952fe8fb19SBen Gras pw->pw_change = 0;
1962fe8fb19SBen Gras pw->pw_expire = 0;
1972fe8fb19SBen Gras *flags |= (_PASSWORD_NOCHG | _PASSWORD_NOEXP);
1982fe8fb19SBen Gras } else {
1992fe8fb19SBen Gras pw->pw_class = strsep(&bp, ":"); /* class */
2002fe8fb19SBen Gras if (!(p = strsep(&bp, ":"))) /* change */
2012fe8fb19SBen Gras goto fmt;
2022fe8fb19SBen Gras if (!gettime(&ti, p, flags, dowarn, _PASSWORD_NOCHG))
2032fe8fb19SBen Gras return 0;
2042fe8fb19SBen Gras pw->pw_change = ti;
2052fe8fb19SBen Gras
2062fe8fb19SBen Gras if (!(p = strsep(&bp, ":"))) /* expire */
2072fe8fb19SBen Gras goto fmt;
2082fe8fb19SBen Gras if (!gettime(&ti, p, flags, dowarn, _PASSWORD_NOEXP))
2092fe8fb19SBen Gras return 0;
2102fe8fb19SBen Gras pw->pw_expire = ti;
2112fe8fb19SBen Gras }
2122fe8fb19SBen Gras
2132fe8fb19SBen Gras pw->pw_gecos = strsep(&bp, ":"); /* gecos */
2142fe8fb19SBen Gras pw->pw_dir = strsep(&bp, ":"); /* directory */
2152fe8fb19SBen Gras if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
2162fe8fb19SBen Gras goto fmt;
2172fe8fb19SBen Gras
2182fe8fb19SBen Gras #if ! HAVE_NBTOOL_CONFIG_H
2192fe8fb19SBen Gras p = pw->pw_shell;
2202fe8fb19SBen Gras if (root && *p) /* empty == /bin/sh */
2212fe8fb19SBen Gras for (setusershell();;) {
2222fe8fb19SBen Gras if (!(sh = getusershell())) {
2232fe8fb19SBen Gras if (dowarn)
2242fe8fb19SBen Gras warnx("warning, unknown root shell");
2252fe8fb19SBen Gras break;
2262fe8fb19SBen Gras }
2272fe8fb19SBen Gras if (!strcmp(p, sh))
2282fe8fb19SBen Gras break;
2292fe8fb19SBen Gras }
2302fe8fb19SBen Gras #endif
2312fe8fb19SBen Gras
2322fe8fb19SBen Gras if ((p = strsep(&bp, ":")) != NULL) { /* too many */
2332fe8fb19SBen Gras fmt:
2342fe8fb19SBen Gras if (dowarn)
2352fe8fb19SBen Gras warnx("corrupted entry");
2362fe8fb19SBen Gras return 0;
2372fe8fb19SBen Gras }
2382fe8fb19SBen Gras
2392fe8fb19SBen Gras return 1;
2402fe8fb19SBen Gras }
241