Annotation of src/usr.bin/chpass/edit.c, Revision 1.17
1.17 ! ho 1: /* $OpenBSD: edit.c,v 1.16 1999/08/06 20:41:06 deraadt Exp $ */
1.2 deraadt 2: /* $NetBSD: edit.c,v 1.6 1996/05/15 21:50:45 jtc Exp $ */
1.1 deraadt 3:
4: /*-
5: * Copyright (c) 1990, 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.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)edit.c 8.3 (Berkeley) 4/2/94";
40: #else
1.17 ! ho 41: static char rcsid[] = "$OpenBSD: edit.c,v 1.16 1999/08/06 20:41:06 deraadt Exp $";
1.1 deraadt 42: #endif
43: #endif /* not lint */
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>
1.2 deraadt 57: #include <util.h>
1.1 deraadt 58:
59: #include "chpass.h"
60:
61: void
1.2 deraadt 62: edit(tempname, pw)
63: char *tempname;
1.1 deraadt 64: struct passwd *pw;
65: {
66: struct stat begin, end;
67:
68: for (;;) {
1.6 deraadt 69: if (lstat(tempname, &begin) == -1 || S_ISLNK(begin.st_mode))
1.1 deraadt 70: pw_error(tempname, 1, 1);
1.2 deraadt 71: pw_edit(1, tempname);
1.7 deraadt 72: if (lstat(tempname, &end) == -1 || S_ISLNK(end.st_mode))
1.1 deraadt 73: pw_error(tempname, 1, 1);
1.17 ! ho 74: if (begin.st_mtime == end.st_mtime &&
! 75: begin.st_size == end.st_size) {
1.1 deraadt 76: warnx("no changes made");
1.2 deraadt 77: unlink(tempname);
1.1 deraadt 78: pw_error(NULL, 0, 0);
79: }
1.15 deraadt 80: if (verify(tempname, pw)) {
81: unlink(tempname);
1.1 deraadt 82: break;
1.15 deraadt 83: }
1.1 deraadt 84: pw_prompt();
85: }
86: }
87:
88: /*
89: * display --
90: * print out the file for the user to edit; strange side-effect:
91: * set conditional flag if the user gets to edit the shell.
92: */
93: void
1.2 deraadt 94: display(tempname, fd, pw)
95: char *tempname;
1.1 deraadt 96: int fd;
97: struct passwd *pw;
98: {
99: FILE *fp;
1.13 deraadt 100: char *bp, *p;
101: char chngstr[256];
1.1 deraadt 102:
103: if (!(fp = fdopen(fd, "w")))
104: pw_error(tempname, 1, 1);
105:
106: (void)fprintf(fp,
107: "#Changing user database information for %s.\n", pw->pw_name);
108: if (!uid) {
109: (void)fprintf(fp, "Login: %s\n", pw->pw_name);
1.4 downsj 110: (void)fprintf(fp, "Encrypted password: %s\n", pw->pw_passwd);
1.16 deraadt 111: (void)fprintf(fp, "Uid [#]: %u\n", pw->pw_uid);
112: (void)fprintf(fp, "Gid [# or name]: %u\n", pw->pw_gid);
1.1 deraadt 113: (void)fprintf(fp, "Change [month day year]: %s\n",
1.13 deraadt 114: ttoa(chngstr, sizeof(chngstr), pw->pw_change));
1.1 deraadt 115: (void)fprintf(fp, "Expire [month day year]: %s\n",
1.13 deraadt 116: ttoa(chngstr, sizeof(chngstr), pw->pw_expire));
1.1 deraadt 117: (void)fprintf(fp, "Class: %s\n", pw->pw_class);
118: (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
119: (void)fprintf(fp, "Shell: %s\n",
120: *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
121: }
122: /* Only admin can change "restricted" shells. */
123: else if (ok_shell(pw->pw_shell))
124: /*
125: * Make shell a restricted field. Ugly with a
126: * necklace, but there's not much else to do.
127: */
128: (void)fprintf(fp, "Shell: %s\n",
129: *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
130: else
131: list[E_SHELL].restricted = 1;
132: bp = pw->pw_gecos;
133: p = strsep(&bp, ",");
134: (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
135: p = strsep(&bp, ",");
136: (void)fprintf(fp, "Location: %s\n", p ? p : "");
137: p = strsep(&bp, ",");
138: (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
139: p = strsep(&bp, ",");
140: (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
141:
142: (void)fchown(fd, getuid(), getgid());
143: (void)fclose(fp);
144: }
145:
146: int
1.2 deraadt 147: verify(tempname, pw)
148: char *tempname;
1.1 deraadt 149: struct passwd *pw;
150: {
151: ENTRY *ep;
152: char *p;
153: struct stat sb;
154: FILE *fp;
1.11 deraadt 155: int len, alen, line;
1.1 deraadt 156: static char buf[LINE_MAX];
157:
158: if (!(fp = fopen(tempname, "r")))
159: pw_error(tempname, 1, 1);
160: if (fstat(fileno(fp), &sb))
161: pw_error(tempname, 1, 1);
162: if (sb.st_size == 0) {
163: warnx("corrupted temporary file");
164: goto bad;
165: }
1.11 deraadt 166: line = 0;
1.1 deraadt 167: while (fgets(buf, sizeof(buf), fp)) {
1.11 deraadt 168: line++;
1.1 deraadt 169: if (!buf[0] || buf[0] == '#')
170: continue;
171: if (!(p = strchr(buf, '\n'))) {
1.11 deraadt 172: warnx("line %d too long", line);
1.1 deraadt 173: goto bad;
174: }
175: *p = '\0';
176: for (ep = list;; ++ep) {
177: if (!ep->prompt) {
1.11 deraadt 178: warnx("unrecognized field on line %d", line);
1.1 deraadt 179: goto bad;
180: }
181: if (!strncasecmp(buf, ep->prompt, ep->len)) {
182: if (ep->restricted && uid) {
183: warnx(
184: "you may not change the %s field",
185: ep->prompt);
186: goto bad;
187: }
188: if (!(p = strchr(buf, ':'))) {
1.11 deraadt 189: warnx("line %d corrupted", line);
1.1 deraadt 190: goto bad;
191: }
192: while (isspace(*++p));
193: if (ep->except && strpbrk(p, ep->except)) {
194: warnx(
195: "illegal character in the \"%s\" field",
196: ep->prompt);
197: goto bad;
198: }
199: if ((ep->func)(p, pw, ep)) {
200: bad: (void)fclose(fp);
201: return (0);
202: }
203: break;
204: }
205: }
206: }
207: (void)fclose(fp);
1.14 deraadt 208:
209: if (list[E_NAME].save == NULL)
210: list[E_NAME].save = "";
211: if (list[E_BPHONE].save == NULL)
212: list[E_BPHONE].save = "";
213: if (list[E_HPHONE].save == NULL)
214: list[E_HPHONE].save = "";
215: if (list[E_LOCATE].save == NULL)
216: list[E_LOCATE].save = "";
1.1 deraadt 217:
218: /* Build the gecos field. */
219: len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
220: strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
1.9 deraadt 221: for (alen = 0, p = list[E_NAME].save; *p; p++)
222: if (*p == '&')
223: alen = alen + strlen(pw->pw_name) - 1;
1.1 deraadt 224: if (!(p = malloc(len)))
225: err(1, NULL);
226: (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
227: list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
228:
229: if (snprintf(buf, sizeof(buf),
1.16 deraadt 230: "%s:%s:%u:%u:%s:%ld:%ld:%s:%s:%s",
1.1 deraadt 231: pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
232: pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
1.10 deraadt 233: pw->pw_shell) >= 1023 ||
234: strlen(buf) + alen >= 1023) {
1.1 deraadt 235: warnx("entries too long");
1.9 deraadt 236: free(p);
1.1 deraadt 237: return (0);
238: }
1.9 deraadt 239: free(p);
240:
1.12 kstailey 241: return (pw_scan(buf, pw, NULL));
1.1 deraadt 242: }