Annotation of src/usr.bin/chpass/edit.c, Revision 1.28
1.28 ! deraadt 1: /* $OpenBSD: edit.c,v 1.27 2003/06/03 02:56:06 millert 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.
1.27 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: #ifndef lint
34: #if 0
35: static char sccsid[] = "@(#)edit.c 8.3 (Berkeley) 4/2/94";
36: #else
1.28 ! deraadt 37: static char rcsid[] = "$OpenBSD: edit.c,v 1.27 2003/06/03 02:56:06 millert Exp $";
1.1 deraadt 38: #endif
39: #endif /* not lint */
40:
41: #include <sys/param.h>
42: #include <sys/stat.h>
43:
44: #include <ctype.h>
45: #include <err.h>
46: #include <errno.h>
1.24 millert 47: #include <fcntl.h>
1.1 deraadt 48: #include <paths.h>
49: #include <pwd.h>
50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
53: #include <unistd.h>
1.2 deraadt 54: #include <util.h>
1.1 deraadt 55:
56: #include "chpass.h"
57:
1.23 millert 58: int
1.28 ! deraadt 59: edit(char *tempname, struct passwd *pw)
1.1 deraadt 60: {
61: struct stat begin, end;
62:
63: for (;;) {
1.6 deraadt 64: if (lstat(tempname, &begin) == -1 || S_ISLNK(begin.st_mode))
1.23 millert 65: return (EDIT_ERROR);
1.2 deraadt 66: pw_edit(1, tempname);
1.7 deraadt 67: if (lstat(tempname, &end) == -1 || S_ISLNK(end.st_mode))
1.23 millert 68: return (EDIT_ERROR);
1.17 ho 69: if (begin.st_mtime == end.st_mtime &&
70: begin.st_size == end.st_size) {
1.1 deraadt 71: warnx("no changes made");
1.23 millert 72: return (EDIT_NOCHANGE);
1.1 deraadt 73: }
1.18 millert 74: if (verify(tempname, pw))
1.1 deraadt 75: break;
76: pw_prompt();
77: }
1.23 millert 78: return(EDIT_OK);
1.1 deraadt 79: }
80:
81: /*
82: * display --
83: * print out the file for the user to edit; strange side-effect:
84: * set conditional flag if the user gets to edit the shell.
85: */
86: void
1.28 ! deraadt 87: display(char *tempname, int fd, struct passwd *pw)
1.1 deraadt 88: {
89: FILE *fp;
1.13 deraadt 90: char *bp, *p;
91: char chngstr[256];
1.1 deraadt 92:
93: if (!(fp = fdopen(fd, "w")))
94: pw_error(tempname, 1, 1);
95:
96: (void)fprintf(fp,
1.20 aaron 97: "# Changing user database information for %s.\n", pw->pw_name);
1.1 deraadt 98: if (!uid) {
99: (void)fprintf(fp, "Login: %s\n", pw->pw_name);
1.4 downsj 100: (void)fprintf(fp, "Encrypted password: %s\n", pw->pw_passwd);
1.16 deraadt 101: (void)fprintf(fp, "Uid [#]: %u\n", pw->pw_uid);
102: (void)fprintf(fp, "Gid [# or name]: %u\n", pw->pw_gid);
1.1 deraadt 103: (void)fprintf(fp, "Change [month day year]: %s\n",
1.13 deraadt 104: ttoa(chngstr, sizeof(chngstr), pw->pw_change));
1.1 deraadt 105: (void)fprintf(fp, "Expire [month day year]: %s\n",
1.13 deraadt 106: ttoa(chngstr, sizeof(chngstr), pw->pw_expire));
1.1 deraadt 107: (void)fprintf(fp, "Class: %s\n", pw->pw_class);
108: (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
109: (void)fprintf(fp, "Shell: %s\n",
110: *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
111: }
112: /* Only admin can change "restricted" shells. */
113: else if (ok_shell(pw->pw_shell))
114: /*
115: * Make shell a restricted field. Ugly with a
116: * necklace, but there's not much else to do.
117: */
118: (void)fprintf(fp, "Shell: %s\n",
119: *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
120: else
121: list[E_SHELL].restricted = 1;
122: bp = pw->pw_gecos;
123: p = strsep(&bp, ",");
124: (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
125: p = strsep(&bp, ",");
1.19 aaron 126: (void)fprintf(fp, "Office Location: %s\n", p ? p : "");
1.1 deraadt 127: p = strsep(&bp, ",");
128: (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
129: p = strsep(&bp, ",");
130: (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
131:
132: (void)fchown(fd, getuid(), getgid());
133: (void)fclose(fp);
134: }
135:
136: int
1.28 ! deraadt 137: verify(char *tempname, struct passwd *pw)
1.1 deraadt 138: {
1.28 ! deraadt 139: unsigned int alen, line;
1.22 deraadt 140: static char buf[LINE_MAX];
141: struct stat sb;
142: char *p, *q;
1.1 deraadt 143: ENTRY *ep;
144: FILE *fp;
1.24 millert 145: int fd;
1.1 deraadt 146:
1.24 millert 147: if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 ||
148: (fp = fdopen(fd, "r")) == NULL)
1.1 deraadt 149: pw_error(tempname, 1, 1);
1.24 millert 150: if (fstat(fd, &sb))
1.1 deraadt 151: pw_error(tempname, 1, 1);
1.25 millert 152: if (sb.st_size == 0 || sb.st_nlink != 1 || sb.st_uid != uid) {
1.1 deraadt 153: warnx("corrupted temporary file");
154: goto bad;
155: }
1.11 deraadt 156: line = 0;
1.1 deraadt 157: while (fgets(buf, sizeof(buf), fp)) {
1.11 deraadt 158: line++;
1.1 deraadt 159: if (!buf[0] || buf[0] == '#')
160: continue;
1.26 millert 161: if ((p = strchr(buf, '\n')) != NULL)
162: *p = '\0';
163: else if (!feof(fp)) {
1.22 deraadt 164: warnx("line %u too long", line);
1.1 deraadt 165: goto bad;
166: }
167: for (ep = list;; ++ep) {
168: if (!ep->prompt) {
1.22 deraadt 169: warnx("unrecognized field on line %u", line);
1.1 deraadt 170: goto bad;
171: }
172: if (!strncasecmp(buf, ep->prompt, ep->len)) {
173: if (ep->restricted && uid) {
174: warnx(
175: "you may not change the %s field",
176: ep->prompt);
177: goto bad;
178: }
179: if (!(p = strchr(buf, ':'))) {
1.22 deraadt 180: warnx("line %u corrupted", line);
1.1 deraadt 181: goto bad;
182: }
183: while (isspace(*++p));
1.20 aaron 184: for (q = p; *q && isprint(*q); q++) {
185: if (ep->except && strchr(ep->except,*q))
186: break;
187: }
188: if (*q) {
1.1 deraadt 189: warnx(
190: "illegal character in the \"%s\" field",
191: ep->prompt);
192: goto bad;
193: }
194: if ((ep->func)(p, pw, ep)) {
195: bad: (void)fclose(fp);
196: return (0);
197: }
198: break;
199: }
200: }
201: }
202: (void)fclose(fp);
1.14 deraadt 203:
204: if (list[E_NAME].save == NULL)
205: list[E_NAME].save = "";
206: if (list[E_BPHONE].save == NULL)
207: list[E_BPHONE].save = "";
208: if (list[E_HPHONE].save == NULL)
209: list[E_HPHONE].save = "";
210: if (list[E_LOCATE].save == NULL)
211: list[E_LOCATE].save = "";
1.1 deraadt 212:
213: /* Build the gecos field. */
1.9 deraadt 214: for (alen = 0, p = list[E_NAME].save; *p; p++)
215: if (*p == '&')
216: alen = alen + strlen(pw->pw_name) - 1;
1.28 ! deraadt 217: if (asprintf(&p, "%s,%s,%s,%s", list[E_NAME].save,
! 218: list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save) == -1)
1.1 deraadt 219: err(1, NULL);
1.22 deraadt 220: pw->pw_gecos = p;
1.1 deraadt 221:
222: if (snprintf(buf, sizeof(buf),
1.16 deraadt 223: "%s:%s:%u:%u:%s:%ld:%ld:%s:%s:%s",
1.1 deraadt 224: pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
1.21 deraadt 225: (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
1.10 deraadt 226: pw->pw_shell) >= 1023 ||
227: strlen(buf) + alen >= 1023) {
1.1 deraadt 228: warnx("entries too long");
1.9 deraadt 229: free(p);
1.1 deraadt 230: return (0);
231: }
1.9 deraadt 232: free(p);
233:
1.12 kstailey 234: return (pw_scan(buf, pw, NULL));
1.1 deraadt 235: }