Annotation of src/usr.bin/chpass/edit.c, Revision 1.29
1.29 ! robert 1: /* $OpenBSD: edit.c,v 1.28 2005/11/14 15:31:11 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: #ifndef lint
34: #if 0
35: static char sccsid[] = "@(#)edit.c 8.3 (Berkeley) 4/2/94";
36: #else
1.29 ! robert 37: static char rcsid[] = "$OpenBSD: edit.c,v 1.28 2005/11/14 15:31:11 deraadt 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.29 ! robert 139: unsigned int line;
! 140: size_t alen;
1.22 deraadt 141: static char buf[LINE_MAX];
142: struct stat sb;
143: char *p, *q;
1.1 deraadt 144: ENTRY *ep;
145: FILE *fp;
1.24 millert 146: int fd;
1.1 deraadt 147:
1.24 millert 148: if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 ||
149: (fp = fdopen(fd, "r")) == NULL)
1.1 deraadt 150: pw_error(tempname, 1, 1);
1.24 millert 151: if (fstat(fd, &sb))
1.1 deraadt 152: pw_error(tempname, 1, 1);
1.25 millert 153: if (sb.st_size == 0 || sb.st_nlink != 1 || sb.st_uid != uid) {
1.1 deraadt 154: warnx("corrupted temporary file");
155: goto bad;
156: }
1.11 deraadt 157: line = 0;
1.1 deraadt 158: while (fgets(buf, sizeof(buf), fp)) {
1.11 deraadt 159: line++;
1.1 deraadt 160: if (!buf[0] || buf[0] == '#')
161: continue;
1.26 millert 162: if ((p = strchr(buf, '\n')) != NULL)
163: *p = '\0';
164: else if (!feof(fp)) {
1.22 deraadt 165: warnx("line %u too long", line);
1.1 deraadt 166: goto bad;
167: }
168: for (ep = list;; ++ep) {
169: if (!ep->prompt) {
1.22 deraadt 170: warnx("unrecognized field on line %u", line);
1.1 deraadt 171: goto bad;
172: }
173: if (!strncasecmp(buf, ep->prompt, ep->len)) {
174: if (ep->restricted && uid) {
175: warnx(
176: "you may not change the %s field",
177: ep->prompt);
178: goto bad;
179: }
180: if (!(p = strchr(buf, ':'))) {
1.22 deraadt 181: warnx("line %u corrupted", line);
1.1 deraadt 182: goto bad;
183: }
184: while (isspace(*++p));
1.20 aaron 185: for (q = p; *q && isprint(*q); q++) {
186: if (ep->except && strchr(ep->except,*q))
187: break;
188: }
189: if (*q) {
1.1 deraadt 190: warnx(
191: "illegal character in the \"%s\" field",
192: ep->prompt);
193: goto bad;
194: }
195: if ((ep->func)(p, pw, ep)) {
196: bad: (void)fclose(fp);
197: return (0);
198: }
199: break;
200: }
201: }
202: }
203: (void)fclose(fp);
1.14 deraadt 204:
205: if (list[E_NAME].save == NULL)
206: list[E_NAME].save = "";
207: if (list[E_BPHONE].save == NULL)
208: list[E_BPHONE].save = "";
209: if (list[E_HPHONE].save == NULL)
210: list[E_HPHONE].save = "";
211: if (list[E_LOCATE].save == NULL)
212: list[E_LOCATE].save = "";
1.1 deraadt 213:
214: /* Build the gecos field. */
1.9 deraadt 215: for (alen = 0, p = list[E_NAME].save; *p; p++)
216: if (*p == '&')
217: alen = alen + strlen(pw->pw_name) - 1;
1.28 deraadt 218: if (asprintf(&p, "%s,%s,%s,%s", list[E_NAME].save,
219: list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save) == -1)
1.1 deraadt 220: err(1, NULL);
1.22 deraadt 221: pw->pw_gecos = p;
1.1 deraadt 222:
223: if (snprintf(buf, sizeof(buf),
1.16 deraadt 224: "%s:%s:%u:%u:%s:%ld:%ld:%s:%s:%s",
1.1 deraadt 225: pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
1.21 deraadt 226: (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
1.10 deraadt 227: pw->pw_shell) >= 1023 ||
228: strlen(buf) + alen >= 1023) {
1.1 deraadt 229: warnx("entries too long");
1.9 deraadt 230: free(p);
1.1 deraadt 231: return (0);
232: }
1.9 deraadt 233: free(p);
234:
1.12 kstailey 235: return (pw_scan(buf, pw, NULL));
1.1 deraadt 236: }