Annotation of src/usr.bin/chpass/field.c, Revision 1.14
1.14 ! deraadt 1: /* $OpenBSD: field.c,v 1.13 2013/11/26 13:18:55 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: #include <ctype.h>
34: #include <err.h>
35: #include <errno.h>
36: #include <grp.h>
1.6 avsm 37: #include <paths.h>
1.1 deraadt 38: #include <pwd.h>
39: #include <stdio.h>
40: #include <stdlib.h>
41: #include <string.h>
42: #include <unistd.h>
1.14 ! deraadt 43: #include <limits.h>
1.1 deraadt 44:
45: #include "chpass.h"
46:
47: /* ARGSUSED */
48: int
1.4 deraadt 49: p_login(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 50: {
51: if (!*p) {
52: warnx("empty login field");
53: return (1);
54: }
55: if (*p == '-') {
56: warnx("login names may not begin with a hyphen");
57: return (1);
58: }
1.7 millert 59: /* XXX - what about truncated names? */
1.9 deraadt 60: if (strcmp(pw->pw_name, p) != 0 && getpwnam(p) != NULL) {
1.7 millert 61: warnx("login %s already exists", p);
62: return (1);
63: }
1.1 deraadt 64: if (!(pw->pw_name = strdup(p))) {
65: warnx("can't save entry");
66: return (1);
67: }
68: if (strchr(p, '.'))
69: warnx("\'.\' is dangerous in a login name");
70: for (; *p; ++p)
1.13 deraadt 71: if (isupper((unsigned char)*p)) {
1.1 deraadt 72: warnx("upper-case letters are dangerous in a login name");
73: break;
74: }
75: return (0);
76: }
77:
78: /* ARGSUSED */
79: int
1.4 deraadt 80: p_passwd(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 81: {
82: if (!*p)
83: pw->pw_passwd = ""; /* "NOLOGIN"; */
84: else if (!(pw->pw_passwd = strdup(p))) {
85: warnx("can't save password entry");
86: return (1);
87: }
1.4 deraadt 88:
1.1 deraadt 89: return (0);
90: }
91:
92: /* ARGSUSED */
93: int
1.4 deraadt 94: p_uid(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 95: {
96: uid_t id;
1.7 millert 97: const char *errstr;
1.1 deraadt 98:
99: if (!*p) {
100: warnx("empty uid field");
101: return (1);
102: }
1.8 robert 103: id = (uid_t)strtonum(p, 0, UID_MAX, &errstr);
1.7 millert 104: if (errstr) {
105: warnx("uid is %s", errstr);
1.1 deraadt 106: return (1);
107: }
108: pw->pw_uid = id;
109: return (0);
110: }
111:
112: /* ARGSUSED */
113: int
1.4 deraadt 114: p_gid(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 115: {
116: struct group *gr;
1.7 millert 117: const char *errstr;
1.1 deraadt 118: gid_t id;
119:
120: if (!*p) {
121: warnx("empty gid field");
122: return (1);
123: }
1.13 deraadt 124: if (!isdigit((unsigned char)*p)) {
1.1 deraadt 125: if (!(gr = getgrnam(p))) {
126: warnx("unknown group %s", p);
127: return (1);
128: }
129: pw->pw_gid = gr->gr_gid;
130: return (0);
131: }
1.8 robert 132: id = (uid_t)strtonum(p, 0, GID_MAX, &errstr);
1.7 millert 133: if (errstr) {
134: warnx("gid is %s", errstr);
1.1 deraadt 135: return (1);
136: }
137: pw->pw_gid = id;
138: return (0);
139: }
140:
141: /* ARGSUSED */
142: int
1.4 deraadt 143: p_class(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 144: {
145: if (!*p)
146: pw->pw_class = "";
147: else if (!(pw->pw_class = strdup(p))) {
148: warnx("can't save entry");
149: return (1);
150: }
1.4 deraadt 151:
1.1 deraadt 152: return (0);
153: }
154:
155: /* ARGSUSED */
156: int
1.4 deraadt 157: p_change(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 158: {
159: if (!atot(p, &pw->pw_change))
160: return (0);
161: warnx("illegal date for change field");
162: return (1);
163: }
164:
165: /* ARGSUSED */
166: int
1.4 deraadt 167: p_expire(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 168: {
169: if (!atot(p, &pw->pw_expire))
170: return (0);
171: warnx("illegal date for expire field");
172: return (1);
173: }
174:
175: /* ARGSUSED */
176: int
1.4 deraadt 177: p_gecos(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 178: {
179: if (!*p)
180: ep->save = "";
181: else if (!(ep->save = strdup(p))) {
182: warnx("can't save entry");
183: return (1);
184: }
185: return (0);
186: }
187:
188: /* ARGSUSED */
189: int
1.4 deraadt 190: p_hdir(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 191: {
192: if (!*p) {
193: warnx("empty home directory field");
194: return (1);
195: }
196: if (!(pw->pw_dir = strdup(p))) {
197: warnx("can't save entry");
198: return (1);
199: }
200: return (0);
201: }
202:
203: /* ARGSUSED */
204: int
1.4 deraadt 205: p_shell(char *p, struct passwd *pw, ENTRY *ep)
1.1 deraadt 206: {
1.3 downsj 207: char *t;
1.1 deraadt 208:
209: if (!*p) {
210: pw->pw_shell = _PATH_BSHELL;
211: return (0);
212: }
213: /* only admin can change from or to "restricted" shells */
1.10 millert 214: if (uid && pw->pw_shell && !ok_shell(pw->pw_shell, NULL)) {
1.1 deraadt 215: warnx("%s: current shell non-standard", pw->pw_shell);
216: return (1);
217: }
1.10 millert 218: if (!ok_shell(p, &t)) {
1.1 deraadt 219: if (uid) {
220: warnx("%s: non-standard shell", p);
221: return (1);
1.11 jacekm 222: } else
223: t = strdup(p);
1.10 millert 224: }
225: if (!(pw->pw_shell = t)) {
1.1 deraadt 226: warnx("can't save entry");
227: return (1);
228: }
229: return (0);
230: }