d1ef633239a588ace31c263b728589f1de8ea72b
[dragonfly.git] / usr.bin / chpass / edit.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2002 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * Portions of this software were developed for the FreeBSD Project by
8  * ThinkSec AS and NAI Labs, the Security Research Division of Network
9  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
10  * ("CBOSS"), as part of the DARPA CHATS research program.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the University of
23  *      California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * @(#)edit.c   8.3 (Berkeley) 4/2/94
41  * $FreeBSD: src/usr.bin/chpass/edit.c,v 1.23 2003/04/09 18:18:42 des Exp $
42  * $DragonFly: src/usr.bin/chpass/edit.c,v 1.3 2003/10/02 17:42:26 hmp Exp $
43  */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <paths.h>
52 #include <pwd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #include <pw_scan.h>
59 #include <libutil.h>
60
61 #include "chpass.h"
62
63 static int display(const char *tfn, struct passwd *pw);
64 static struct passwd *verify(const char *tfn, struct passwd *pw);
65
66 struct passwd *
67 edit(const char *tfn, struct passwd *pw)
68 {
69         struct passwd *npw;
70         char *line;
71         size_t len;
72
73         if (display(tfn, pw) == -1)
74                 return (NULL);
75         for (;;) {
76                 switch (pw_edit(1)) {
77                 case -1:
78                         return (NULL);
79                 case 0:
80                         return (pw_dup(pw));
81                 default:
82                         break;
83                 }
84                 if ((npw = verify(tfn, pw)) != NULL)
85                         return (npw);
86                 free(npw);
87                 printf("re-edit the password file? ");
88                 fflush(stdout);
89                 if ((line = fgetln(stdin, &len)) == NULL) {
90                         warn("fgetln()");
91                         return (NULL);
92                 }
93                 if (len > 0 && (*line == 'N' || *line == 'n'))
94                         return (NULL);
95         }
96 }
97
98 /*
99  * display --
100  *      print out the file for the user to edit; strange side-effect:
101  *      set conditional flag if the user gets to edit the shell.
102  */
103 static int
104 display(const char *tfn, struct passwd *pw)
105 {
106         FILE *fp;
107         char *bp, *gecos, *p;
108
109         if ((fp = fopen(tfn, "w")) == NULL) {
110                 warn("%s", tfn);
111                 return (-1);
112         }
113
114         fprintf(fp, "#Changing user information for %s.\n", pw->pw_name);
115         if (master_mode) {
116                 fprintf(fp, "Login: %s\n", pw->pw_name);
117                 fprintf(fp, "Password: %s\n", pw->pw_passwd);
118                 fprintf(fp, "Uid [#]: %lu\n", (unsigned long)pw->pw_uid);
119                 fprintf(fp, "Gid [# or name]: %lu\n",
120                     (unsigned long)pw->pw_gid);
121                 fprintf(fp, "Change [month day year]: %s\n",
122                     ttoa(pw->pw_change));
123                 fprintf(fp, "Expire [month day year]: %s\n",
124                     ttoa(pw->pw_expire));
125                 fprintf(fp, "Class: %s\n", pw->pw_class);
126                 fprintf(fp, "Home directory: %s\n", pw->pw_dir);
127                 fprintf(fp, "Shell: %s\n",
128                     *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
129         }
130         /* Only admin can change "restricted" shells. */
131 #if 0
132         else if (ok_shell(pw->pw_shell))
133                 /*
134                  * Make shell a restricted field.  Ugly with a
135                  * necklace, but there's not much else to do.
136                  */
137 #else
138         else if ((!list[E_SHELL].restricted && ok_shell(pw->pw_shell)) ||
139             master_mode)
140                 /*
141                  * If change not restrict (table.c) and standard shell
142                  *      OR if root, then allow editing of shell.
143                  */
144 #endif
145                 fprintf(fp, "Shell: %s\n",
146                     *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
147         else
148                 list[E_SHELL].restricted = 1;
149
150         if ((bp = gecos = strdup(pw->pw_gecos)) == NULL) {
151                 warn(NULL);
152                 fclose(fp);
153                 return (-1);
154         }
155
156         p = strsep(&bp, ",");
157         p = strdup(p ? p : "");
158         list[E_NAME].save = p;
159         if (!list[E_NAME].restricted || master_mode)
160                 fprintf(fp, "Full Name: %s\n", p);
161
162         p = strsep(&bp, ",");
163         p = strdup(p ? p : "");
164         list[E_LOCATE].save = p;
165         if (!list[E_LOCATE].restricted || master_mode)
166                 fprintf(fp, "Office Location: %s\n", p);
167
168         p = strsep(&bp, ",");
169         p = strdup(p ? p : "");
170         list[E_BPHONE].save = p;
171         if (!list[E_BPHONE].restricted || master_mode)
172                 fprintf(fp, "Office Phone: %s\n", p);
173
174         p = strsep(&bp, ",");
175         p = strdup(p ? p : "");
176         list[E_HPHONE].save = p;
177         if (!list[E_HPHONE].restricted || master_mode)
178                 fprintf(fp, "Home Phone: %s\n", p);
179
180         bp = strdup(bp ? bp : "");
181         list[E_OTHER].save = bp;
182         if (!list[E_OTHER].restricted || master_mode)
183                 fprintf(fp, "Other information: %s\n", bp);
184
185         free(gecos);
186
187         fchown(fileno(fp), getuid(), getgid());
188         fclose(fp);
189         return (0);
190 }
191
192 static struct passwd *
193 verify(const char *tfn, struct passwd *pw)
194 {
195         struct passwd *npw;
196         ENTRY *ep;
197         char *buf, *p, *val;
198         struct stat sb;
199         FILE *fp;
200         int line;
201         size_t len;
202
203         if ((pw = pw_dup(pw)) == NULL)
204                 return (NULL);
205         if ((fp = fopen(tfn, "r")) == NULL ||
206             fstat(fileno(fp), &sb) == -1) {
207                 warn("%s", tfn);
208                 free(pw);
209                 return (NULL);
210         }
211         if (sb.st_size == 0) {
212                 warnx("corrupted temporary file");
213                 fclose(fp);
214                 free(pw);
215                 return (NULL);
216         }
217         val = NULL;
218         for (line = 1; (buf = fgetln(fp, &len)) != NULL; ++line) {
219                 if (*buf == '\0' || *buf == '#')
220                         continue;
221                 while (len > 0 && isspace(buf[len - 1]))
222                         --len;
223                 for (ep = list;; ++ep) {
224                         if (!ep->prompt) {
225                                 warnx("%s: unrecognized field on line %d",
226                                     tfn, line);
227                                 goto bad;
228                         }
229                         if (ep->len > len)
230                                 continue;
231                         if (strncasecmp(buf, ep->prompt, ep->len) != 0)
232                                 continue;
233                         if (ep->restricted && !master_mode) {
234                                 warnx("%s: you may not change the %s field",
235                                     tfn, ep->prompt);
236                                 goto bad;
237                         }
238                         for (p = buf; p < buf + len && *p != ':'; ++p)
239                                 /* nothing */ ;
240                         if (*p != ':') {
241                                 warnx("%s: line %d corrupted", tfn, line);
242                                 goto bad;
243                         }
244                         while (++p < buf + len && isspace(*p))
245                                 /* nothing */ ;
246                         free(val);
247                         asprintf(&val, "%.*s", (int)(buf + len - p), p);
248                         if (val == NULL)
249                                 goto bad;
250                         if (ep->except && strpbrk(val, ep->except)) {
251                                 warnx("%s: invalid character in \"%s\" field '%s'",
252                                     tfn, ep->prompt, val);
253                                 goto bad;
254                         }
255                         if ((ep->func)(val, pw, ep))
256                                 goto bad;
257                         break;
258                 }
259         }
260         free(val);
261         fclose(fp);
262
263         /* Build the gecos field. */
264         len = asprintf(&p, "%s,%s,%s,%s,%s", list[E_NAME].save,
265             list[E_LOCATE].save, list[E_BPHONE].save,
266             list[E_HPHONE].save, list[E_OTHER].save);
267         if (p == NULL) {
268                 warn("asprintf()");
269                 free(pw);
270                 return (NULL);
271         }
272         while (len > 0 && p[len - 1] == ',')
273                 p[--len] = '\0';
274         pw->pw_gecos = p;
275         buf = pw_make(pw);
276         free(pw);
277         free(p);
278         if (buf == NULL) {
279                 warn("pw_make()");
280                 return (NULL);
281         }
282         npw = pw_scan(buf, PWSCAN_WARN|PWSCAN_MASTER);
283         free(buf);
284         return (npw);
285 bad:
286         free(pw);
287         free(val);
288         fclose(fp);
289         return (NULL);
290 }