Annotation of src/usr.bin/chpass/field.c, Revision 1.10
1.10 ! millert 1: /* $OpenBSD: field.c,v 1.9 2006/03/31 00:29:13 deraadt Exp $ */
1.1 deraadt 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.
1.5 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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";
1.4 deraadt 36: #else
1.10 ! millert 37: static char rcsid[] = "$OpenBSD: field.c,v 1.9 2006/03/31 00:29:13 deraadt Exp $";
1.1 deraadt 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>
1.6 avsm 47: #include <paths.h>
1.1 deraadt 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
1.4 deraadt 58: p_login(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 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: }
1.7 millert 68: /* XXX - what about truncated names? */
1.9 deraadt 69: if (strcmp(pw->pw_name, p) != 0 && getpwnam(p) != NULL) {
1.7 millert 70: warnx("login %s already exists", p);
71: return (1);
72: }
1.1 deraadt 73: if (!(pw->pw_name = strdup(p))) {
74: warnx("can't save entry");
75: return (1);
76: }
77: if (strchr(p, '.'))
78: warnx("\'.\' is dangerous in a login name");
79: for (; *p; ++p)
80: if (isupper(*p)) {
81: warnx("upper-case letters are dangerous in a login name");
82: break;
83: }
84: return (0);
85: }
86:
87: /* ARGSUSED */
88: int
1.4 deraadt 89: p_passwd(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 90: {
91: if (!*p)
92: pw->pw_passwd = ""; /* "NOLOGIN"; */
93: else if (!(pw->pw_passwd = strdup(p))) {
94: warnx("can't save password entry");
95: return (1);
96: }
1.4 deraadt 97:
1.1 deraadt 98: return (0);
99: }
100:
101: /* ARGSUSED */
102: int
1.4 deraadt 103: p_uid(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 104: {
105: uid_t id;
1.7 millert 106: const char *errstr;
1.1 deraadt 107:
108: if (!*p) {
109: warnx("empty uid field");
110: return (1);
111: }
1.8 robert 112: id = (uid_t)strtonum(p, 0, UID_MAX, &errstr);
1.7 millert 113: if (errstr) {
114: warnx("uid is %s", errstr);
1.1 deraadt 115: return (1);
116: }
117: pw->pw_uid = id;
118: return (0);
119: }
120:
121: /* ARGSUSED */
122: int
1.4 deraadt 123: p_gid(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 124: {
125: struct group *gr;
1.7 millert 126: const char *errstr;
1.1 deraadt 127: gid_t id;
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: }
1.8 robert 141: id = (uid_t)strtonum(p, 0, GID_MAX, &errstr);
1.7 millert 142: if (errstr) {
143: warnx("gid is %s", errstr);
1.1 deraadt 144: return (1);
145: }
146: pw->pw_gid = id;
147: return (0);
148: }
149:
150: /* ARGSUSED */
151: int
1.4 deraadt 152: p_class(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 153: {
154: if (!*p)
155: pw->pw_class = "";
156: else if (!(pw->pw_class = strdup(p))) {
157: warnx("can't save entry");
158: return (1);
159: }
1.4 deraadt 160:
1.1 deraadt 161: return (0);
162: }
163:
164: /* ARGSUSED */
165: int
1.4 deraadt 166: p_change(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 167: {
168: if (!atot(p, &pw->pw_change))
169: return (0);
170: warnx("illegal date for change field");
171: return (1);
172: }
173:
174: /* ARGSUSED */
175: int
1.4 deraadt 176: p_expire(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 177: {
178: if (!atot(p, &pw->pw_expire))
179: return (0);
180: warnx("illegal date for expire field");
181: return (1);
182: }
183:
184: /* ARGSUSED */
185: int
1.4 deraadt 186: p_gecos(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 187: {
188: if (!*p)
189: ep->save = "";
190: else if (!(ep->save = strdup(p))) {
191: warnx("can't save entry");
192: return (1);
193: }
194: return (0);
195: }
196:
197: /* ARGSUSED */
198: int
1.4 deraadt 199: p_hdir(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 200: {
201: if (!*p) {
202: warnx("empty home directory field");
203: return (1);
204: }
205: if (!(pw->pw_dir = strdup(p))) {
206: warnx("can't save entry");
207: return (1);
208: }
209: return (0);
210: }
211:
212: /* ARGSUSED */
213: int
1.4 deraadt 214: p_shell(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 215: {
1.3 downsj 216: char *t;
1.1 deraadt 217:
218: if (!*p) {
219: pw->pw_shell = _PATH_BSHELL;
220: return (0);
221: }
222: /* only admin can change from or to "restricted" shells */
1.10 ! millert 223: if (uid && pw->pw_shell && !ok_shell(pw->pw_shell, NULL)) {
1.1 deraadt 224: warnx("%s: current shell non-standard", pw->pw_shell);
225: return (1);
226: }
1.10 ! millert 227: if (!ok_shell(p, &t)) {
1.1 deraadt 228: if (uid) {
229: warnx("%s: non-standard shell", p);
230: return (1);
231: }
1.10 ! millert 232: }
! 233: if (!(pw->pw_shell = t)) {
1.1 deraadt 234: warnx("can't save entry");
235: return (1);
236: }
237: return (0);
238: }