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