Initial import from FreeBSD RELENG_4:
[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  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: src/usr.bin/chpass/edit.c,v 1.16.2.2 2001/08/02 01:48:22 obrien Exp $
34  */
35
36 #ifndef lint
37 static const char sccsid[] = "@(#)edit.c        8.3 (Berkeley) 4/2/94";
38 #endif /* not lint */
39
40 #include <sys/param.h>
41 #include <sys/stat.h>
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <md5.h>
47 #include <paths.h>
48 #include <pwd.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include <pw_scan.h>
55 #include <pw_util.h>
56
57 #include "chpass.h"
58 #ifdef YP
59 #include "pw_yp.h"
60 #endif /* YP */
61
62 extern char *tempname;
63
64 void
65 edit(pw)
66         struct passwd *pw;
67 {
68         struct stat begin, end;
69         char *begin_sum, *end_sum;
70
71         for (;;) {
72                 if (stat(tempname, &begin))
73                         pw_error(tempname, 1, 1);
74                 begin_sum = MD5File(tempname, (char *)NULL);
75                 pw_edit(1);
76                 if (stat(tempname, &end))
77                         pw_error(tempname, 1, 1);
78                 end_sum = MD5File(tempname, (char *)NULL);
79                 if ((begin.st_mtime == end.st_mtime) &&
80                     (strcmp(begin_sum, end_sum) == 0)) {
81                         warnx("no changes made");
82                         pw_error(NULL, 0, 0);
83                 }
84                 free(begin_sum);
85                 free(end_sum);
86                 if (verify(pw))
87                         break;
88                 pw_prompt();
89         }
90 }
91
92 /*
93  * display --
94  *      print out the file for the user to edit; strange side-effect:
95  *      set conditional flag if the user gets to edit the shell.
96  */
97 void
98 display(fd, pw)
99         int fd;
100         struct passwd *pw;
101 {
102         FILE *fp;
103         char *bp, *p, *ttoa();
104
105         if (!(fp = fdopen(fd, "w")))
106                 pw_error(tempname, 1, 1);
107
108         (void)fprintf(fp,
109 #ifdef YP
110             "#Changing %s information for %s.\n", _use_yp ? "NIS" : "user database", pw->pw_name);
111         if (!uid && (!_use_yp || suser_override)) {
112 #else
113             "#Changing user database information for %s.\n", pw->pw_name);
114         if (!uid) {
115 #endif /* YP */
116                 (void)fprintf(fp, "Login: %s\n", pw->pw_name);
117                 (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
118                 (void)fprintf(fp, "Uid [#]: %lu\n", (unsigned long)pw->pw_uid);
119                 (void)fprintf(fp, "Gid [# or name]: %lu\n",
120                     (unsigned long)pw->pw_gid);
121                 (void)fprintf(fp, "Change [month day year]: %s\n",
122                     ttoa(pw->pw_change));
123                 (void)fprintf(fp, "Expire [month day year]: %s\n",
124                     ttoa(pw->pw_expire));
125                 (void)fprintf(fp, "Class: %s\n", pw->pw_class);
126                 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
127                 (void)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)) || !uid)
139                 /*
140                  * If change not restrict (table.c) and standard shell
141                  *      OR if root, then allow editing of shell.
142                  */
143 #endif
144                 (void)fprintf(fp, "Shell: %s\n",
145                     *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
146         else
147           list[E_SHELL].restricted = 1;
148         bp = pw->pw_gecos;
149
150         p = strsep(&bp, ",");
151         p = strdup(p ? p : "");
152         list[E_NAME].save = p;
153         if (!list[E_NAME].restricted || !uid)
154           (void)fprintf(fp, "Full Name: %s\n", p);
155
156         p = strsep(&bp, ",");
157         p = strdup(p ? p : "");
158         list[E_LOCATE].save = p;
159         if (!list[E_LOCATE].restricted || !uid)
160           (void)fprintf(fp, "Office Location: %s\n", p);
161
162         p = strsep(&bp, ",");
163         p = strdup(p ? p : "");
164         list[E_BPHONE].save = p;
165         if (!list[E_BPHONE].restricted || !uid)
166           (void)fprintf(fp, "Office Phone: %s\n", p);
167
168         p = strsep(&bp, ",");
169         p = strdup(p ? p : "");
170         list[E_HPHONE].save = p;
171         if (!list[E_HPHONE].restricted || !uid)
172           (void)fprintf(fp, "Home Phone: %s\n", p);
173
174         bp = strdup(bp ? bp : "");
175         list[E_OTHER].save = bp;
176         if (!list[E_OTHER].restricted || !uid)
177           (void)fprintf(fp, "Other information: %s\n", bp);
178
179         (void)fchown(fd, getuid(), getgid());
180         (void)fclose(fp);
181 }
182
183 int
184 verify(pw)
185         struct passwd *pw;
186 {
187         ENTRY *ep;
188         char *p;
189         struct stat sb;
190         FILE *fp;
191         int len, line;
192         static char buf[LINE_MAX];
193
194         if (!(fp = fopen(tempname, "r")))
195                 pw_error(tempname, 1, 1);
196         if (fstat(fileno(fp), &sb))
197                 pw_error(tempname, 1, 1);
198         if (sb.st_size == 0) {
199                 warnx("corrupted temporary file");
200                 goto bad;
201         }
202         line = 0;
203         while (fgets(buf, sizeof(buf), fp)) {
204                 line++;
205                 if (!buf[0] || buf[0] == '#')
206                         continue;
207                 if (!(p = strchr(buf, '\n'))) {
208                         warnx("line %d too long", line);
209                         goto bad;
210                 }
211                 *p = '\0';
212                 for (ep = list;; ++ep) {
213                         if (!ep->prompt) {
214                                 warnx("unrecognized field on line %d", line);
215                                 goto bad;
216                         }
217                         if (!strncasecmp(buf, ep->prompt, ep->len)) {
218                                 if (ep->restricted && uid) {
219                                         warnx(
220                                             "you may not change the %s field",
221                                                 ep->prompt);
222                                         goto bad;
223                                 }
224                                 if (!(p = strchr(buf, ':'))) {
225                                         warnx("line %d corrupted", line);
226                                         goto bad;
227                                 }
228                                 while (isspace(*++p));
229                                 if (ep->except && strpbrk(p, ep->except)) {
230                                         warnx(
231                                    "illegal character in the \"%s\" field",
232                                             ep->prompt);
233                                         goto bad;
234                                 }
235                                 if ((ep->func)(p, pw, ep)) {
236 bad:                                    (void)fclose(fp);
237                                         return (0);
238                                 }
239                                 break;
240                         }
241                 }
242         }
243         (void)fclose(fp);
244
245         /* Build the gecos field. */
246         len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
247             strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) +
248             strlen(list[E_OTHER].save) + 5;
249         if (!(p = malloc(len)))
250                 err(1, NULL);
251         (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s,%s", list[E_NAME].save,
252             list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save,
253             list[E_OTHER].save);
254
255         while ((len = strlen(pw->pw_gecos)) && pw->pw_gecos[len - 1] == ',')
256                 pw->pw_gecos[len - 1] = '\0';
257
258         if (snprintf(buf, sizeof(buf),
259             "%s:%s:%lu:%lu:%s:%ld:%ld:%s:%s:%s",
260             pw->pw_name, pw->pw_passwd, (unsigned long)pw->pw_uid, 
261             (unsigned long)pw->pw_gid, pw->pw_class, (long)pw->pw_change,
262             (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
263             pw->pw_shell) >= sizeof(buf)) {
264                 warnx("entries too long");
265                 free(p);
266                 return (0);
267         }
268         free(p);
269         return (pw_scan(buf, pw));
270 }