1 /* $NetBSD: pw_scan.c,v 1.22 2009/01/29 10:41:39 enami Exp $ */
4 * Copyright (c) 1987, 1993, 1994, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #include "compat_pwd.h"
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: pw_scan.c,v 1.22 2009/01/29 10:41:39 enami Exp $");
40 #endif /* LIBC_SCCS and not lint */
43 #include "namespace.h"
45 #include <sys/types.h>
58 #include "pw_private.h"
60 #endif /* ! HAVE_NBTOOL_CONFIG_H */
63 gettime(long long *res
, const char *p
, int *flags
, int dowarn
, int flag
)
74 l
= strtoll(p
, &ep
, 0);
75 if (p
== ep
|| *ep
!= '\0') {
76 vp
= "Invalid number";
79 if (errno
== ERANGE
&& (l
== LLONG_MAX
|| l
== LLONG_MIN
)) {
88 warnx("%s `%s' for %s time", vp
, p
,
89 flag
== _PASSWORD_NOEXP
? "expiration" : "change");
96 getid(unsigned long *res
, const char *p
, int *flags
, int dowarn
, int flag
)
106 ul
= strtoul(p
, &ep
, 0);
107 if (p
== ep
|| *ep
!= '\0') {
108 ep
= __UNCONST("Invalid number");
111 if (errno
== ERANGE
&& ul
== ULONG_MAX
) {
112 ep
= strerror(errno
);
116 ep
= strerror(ERANGE
);
124 warnx("%s %s `%s'", ep
,
125 flag
== _PASSWORD_NOUID
? "uid" : "gid", p
);
132 __pw_scan(char *bp
, struct passwd
*pw
, int *flags
)
134 pw_scan( char *bp
, struct passwd
*pw
, int *flags
)
143 _DIAGASSERT(bp
!= NULL
);
144 _DIAGASSERT(pw
!= NULL
);
153 dowarn
= !(inflags
& _PASSWORD_NOWARN
);
155 if (!(pw
->pw_name
= strsep(&bp
, ":"))) /* login */
157 if (strlen(pw
->pw_name
) > (LOGIN_NAME_MAX
- 1)) {
159 warnx("username too long, `%s' > %d", pw
->pw_name
,
164 root
= !strcmp(pw
->pw_name
, "root");
166 if (!(pw
->pw_passwd
= strsep(&bp
, ":"))) /* passwd */
169 if (!(p
= strsep(&bp
, ":"))) /* uid */
173 if (!getid(&id
, p
, flags
, dowarn
, _PASSWORD_NOUID
))
178 warnx("root uid should be 0");
182 pw
->pw_uid
= (uid_t
)id
;
184 if (!(p
= strsep(&bp
, ":"))) /* gid */
188 if (!getid(&id
, p
, flags
, dowarn
, _PASSWORD_NOGID
))
191 pw
->pw_gid
= (gid_t
)id
;
193 if (inflags
& _PASSWORD_OLDFMT
) {
194 pw
->pw_class
= __UNCONST("");
197 *flags
|= (_PASSWORD_NOCHG
| _PASSWORD_NOEXP
);
199 pw
->pw_class
= strsep(&bp
, ":"); /* class */
200 if (!(p
= strsep(&bp
, ":"))) /* change */
202 if (!gettime(&ti
, p
, flags
, dowarn
, _PASSWORD_NOCHG
))
206 if (!(p
= strsep(&bp
, ":"))) /* expire */
208 if (!gettime(&ti
, p
, flags
, dowarn
, _PASSWORD_NOEXP
))
213 pw
->pw_gecos
= strsep(&bp
, ":"); /* gecos */
214 pw
->pw_dir
= strsep(&bp
, ":"); /* directory */
215 if (!(pw
->pw_shell
= strsep(&bp
, ":"))) /* shell */
218 #if ! HAVE_NBTOOL_CONFIG_H
220 if (root
&& *p
) /* empty == /bin/sh */
221 for (setusershell();;) {
222 if (!(sh
= getusershell())) {
224 warnx("warning, unknown root shell");
232 if ((p
= strsep(&bp
, ":")) != NULL
) { /* too many */
235 warnx("corrupted entry");