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