1 /* $NetBSD: edit.c,v 1.10 1997/10/19 14:05:56 mrg 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.10 1997/10/19 14:05:56 mrg 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 104 if (!(fp = fdopen(fd, "w"))) 105 (*Pw_error)(tempname, 1, 1); 106 107 (void)fprintf(fp, 108 "#Changing user %sdatabase information for %s.\n", 109 use_yp ? "YP " : "", pw->pw_name); 110 if (!uid) { 111 (void)fprintf(fp, "Login: %s\n", pw->pw_name); 112 (void)fprintf(fp, "Password: %s\n", pw->pw_passwd); 113 (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid); 114 (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid); 115 (void)fprintf(fp, "Change [month day year]: %s\n", 116 ttoa(pw->pw_change)); 117 (void)fprintf(fp, "Expire [month day year]: %s\n", 118 ttoa(pw->pw_expire)); 119 (void)fprintf(fp, "Class: %s\n", pw->pw_class); 120 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir); 121 (void)fprintf(fp, "Shell: %s\n", 122 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 123 } 124 /* Only admin can change "restricted" shells. */ 125 else if (ok_shell(pw->pw_shell)) 126 /* 127 * Make shell a restricted field. Ugly with a 128 * necklace, but there's not much else to do. 129 */ 130 (void)fprintf(fp, "Shell: %s\n", 131 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 132 else 133 list[E_SHELL].restricted = 1; 134 bp = pw->pw_gecos; 135 p = strsep(&bp, ","); 136 (void)fprintf(fp, "Full Name: %s\n", p ? p : ""); 137 p = strsep(&bp, ","); 138 (void)fprintf(fp, "Location: %s\n", p ? p : ""); 139 p = strsep(&bp, ","); 140 (void)fprintf(fp, "Office Phone: %s\n", p ? p : ""); 141 p = strsep(&bp, ","); 142 (void)fprintf(fp, "Home Phone: %s\n", p ? p : ""); 143 144 (void)fchown(fd, getuid(), getgid()); 145 (void)fclose(fp); 146 } 147 148 int 149 verify(tempname, pw) 150 char *tempname; 151 struct passwd *pw; 152 { 153 ENTRY *ep; 154 char *p; 155 struct stat sb; 156 FILE *fp; 157 int len; 158 static char buf[LINE_MAX]; 159 160 if (!(fp = fopen(tempname, "r"))) 161 (*Pw_error)(tempname, 1, 1); 162 if (fstat(fileno(fp), &sb)) 163 (*Pw_error)(tempname, 1, 1); 164 if (sb.st_size == 0) { 165 warnx("corrupted temporary file"); 166 goto bad; 167 } 168 while (fgets(buf, sizeof(buf), fp)) { 169 if (!buf[0] || buf[0] == '#') 170 continue; 171 if (!(p = strchr(buf, '\n'))) { 172 warnx("line too long"); 173 goto bad; 174 } 175 *p = '\0'; 176 for (ep = list;; ++ep) { 177 if (!ep->prompt) { 178 warnx("unrecognized field"); 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, ':'))) { 189 warnx("line corrupted"); 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); 208 209 /* Build the gecos field. */ 210 len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) + 211 strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4; 212 if (!(p = malloc(len))) 213 err(1, "malloc"); 214 (void)snprintf(pw->pw_gecos = p, len, "%s,%s,%s,%s", list[E_NAME].save, 215 list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save); 216 217 if (snprintf(buf, sizeof(buf), 218 "%s:%s:%d:%d:%s:%lu:%lu:%s:%s:%s", 219 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 220 (u_long)pw->pw_change, (u_long)pw->pw_expire, pw->pw_gecos, 221 pw->pw_dir, pw->pw_shell) >= sizeof(buf)) { 222 warnx("entries too long"); 223 return (0); 224 } 225 return (pw_scan(buf, pw, (int *)NULL)); 226 } 227