reenter editor if corrupted password file
[unix-history] / usr / src / usr.bin / chpass / field.c
CommitLineData
b6c7c20d
KB
1/*
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
ef97d022 19static char sccsid[] = "@(#)field.c 5.5 (Berkeley) %G%";
b6c7c20d
KB
20#endif /* not lint */
21
22#include <sys/param.h>
cac8e61f 23#include <pwd.h>
b6c7c20d
KB
24#include <grp.h>
25#include <strings.h>
26#include <stdio.h>
27#include <ctype.h>
28#include <chpass.h>
cac8e61f 29#include "pathnames.h"
b6c7c20d
KB
30
31/* ARGSUSED */
32p_login(p, pw, ep)
33 char *p;
34 struct passwd *pw;
35 struct entry *ep;
36{
37 if (!*p) {
ef97d022 38 fprintf(stderr, "chpass: empty login field");
b6c7c20d
KB
39 return(1);
40 }
e4387aeb
KB
41 if (*p == '-') {
42 fprintf(stderr,
43 "chpass: login names may not begin with a hyphen.\n");
44 return(1);
45 }
b6c7c20d
KB
46 if (!(pw->pw_name = strdup(p))) {
47 fprintf(stderr, "chpass: can't save entry");
48 return(1);
49 }
50 return(0);
51}
52
53/* ARGSUSED */
54p_uid(p, pw, ep)
55 register char *p;
56 struct passwd *pw;
57 struct entry *ep;
58{
59 int id;
60
61 if (!*p) {
ef97d022 62 fprintf(stderr, "chpass: empty uid field");
b6c7c20d
KB
63 return(1);
64 }
65 if (!isdigit(*p)) {
66 fprintf(stderr, "chpass: illegal uid");
67 return(1);
68 }
69 id = atoi(p);
70 if ((u_int)id > USHRT_MAX) {
71 fprintf(stderr, "chpass: %d > max uid value (%d)",
72 id, USHRT_MAX);
73 return(1);
74 }
75 pw->pw_uid = id;
76 return(0);
77}
78
79/* ARGSUSED */
80p_gid(p, pw, ep)
81 register char *p;
82 struct passwd *pw;
83 struct entry *ep;
84{
85 struct group *gr;
86 int id;
87
88 if (!*p) {
ef97d022 89 fprintf(stderr, "chpass: empty gid field");
b6c7c20d
KB
90 return(1);
91 }
92 if (!isdigit(*p)) {
93 if (!(gr = getgrnam(p))) {
94 fprintf(stderr, "chpass: unknown group %s", p);
95 return(1);
96 }
97 pw->pw_gid = gr->gr_gid;
98 return(0);
99 }
100 id = atoi(p);
101 if ((u_int)id > USHRT_MAX) {
102 fprintf(stderr, "chpass: %d > max gid value (%d)",
103 id, USHRT_MAX);
104 return(1);
105 }
106 pw->pw_gid = id;
107 return(0);
108}
109
110/* ARGSUSED */
111p_class(p, pw, ep)
112 char *p;
113 struct passwd *pw;
114 struct entry *ep;
115{
ef97d022
KB
116 if (!*p)
117 pw->pw_class = "";
118 else if (!(pw->pw_class = strdup(p))) {
b6c7c20d
KB
119 fprintf(stderr, "chpass: can't save entry");
120 return(1);
121 }
ef97d022 122
b6c7c20d
KB
123 return(0);
124}
125
126/* ARGSUSED */
127p_change(p, pw, ep)
128 char *p;
129 struct passwd *pw;
130 struct entry *ep;
131{
132 if (!atot(p, &pw->pw_change))
133 return(0);
134 fprintf(stderr, "chpass: illegal date for change field");
135 return(1);
136}
137
138/* ARGSUSED */
139p_expire(p, pw, ep)
140 char *p;
141 struct passwd *pw;
142 struct entry *ep;
143{
144 if (!atot(p, &pw->pw_expire))
145 return(0);
146 fprintf(stderr, "chpass: illegal date for expire field");
147 return(1);
148}
149
150/* ARGSUSED */
ef97d022 151p_gecos(p, pw, ep)
b6c7c20d
KB
152 char *p;
153 struct passwd *pw;
154 struct entry *ep;
155{
ef97d022
KB
156 if (!*p)
157 ep->save = "";
158 else if (!(ep->save = strdup(p))) {
b6c7c20d
KB
159 fprintf(stderr, "chpass: can't save entry");
160 return(1);
161 }
162 return(0);
163}
164
165/* ARGSUSED */
166p_hdir(p, pw, ep)
167 char *p;
168 struct passwd *pw;
169 struct entry *ep;
170{
171 if (!*p) {
ef97d022 172 fprintf(stderr, "chpass: empty home directory field");
b6c7c20d
KB
173 return(1);
174 }
175 if (!(pw->pw_dir = strdup(p))) {
176 fprintf(stderr, "chpass: can't save entry");
177 return(1);
178 }
179 return(0);
180}
181
182/* ARGSUSED */
183p_shell(p, pw, ep)
184 register char *p;
185 struct passwd *pw;
186 struct entry *ep;
187{
188 register char *sh, *t;
189 char *getusershell();
190
191 if (!*p) {
cac8e61f 192 pw->pw_shell = _PATH_BSHELL;
b6c7c20d
KB
193 return(0);
194 }
195 setusershell();
196 for (;;) {
197 if (!(sh = getusershell())) {
198 /* only admin can set "restricted" shells */
199 if (!uid)
200 break;
201 fprintf(stderr, "chpass: %s: non-standard shell", p);
202 return(1);
203 }
204 if (!strcmp(p, sh))
205 break;
206 /* allow just shell name */
207 if ((t = rindex(sh, '/')) && !strcmp(p, t)) {
208 p = t;
209 break;
210 }
211 }
212 if (!(pw->pw_shell = strdup(p))) {
213 fprintf(stderr, "chpass: can't save entry");
214 return(1);
215 }
216 return(0);
217}