1 /* $NetBSD: edit.c,v 1.17 2004/10/30 17:11:24 dsl 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)edit.c 8.3 (Berkeley) 4/2/94"; 36 #else 37 __RCSID("$NetBSD: edit.c,v 1.17 2004/10/30 17:11:24 dsl Exp $"); 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> 47 #include <paths.h> 48 #include <pwd.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <fcntl.h> 54 #include <util.h> 55 56 #include "chpass.h" 57 58 void 59 edit(tempname, pw) 60 char *tempname; 61 struct passwd *pw; 62 { 63 struct stat begin, end; 64 65 for (;;) { 66 if (stat(tempname, &begin)) 67 (*Pw_error)(tempname, 1, 1); 68 pw_edit(1, tempname); 69 if (stat(tempname, &end)) 70 (*Pw_error)(tempname, 1, 1); 71 if (begin.st_mtime == end.st_mtime) { 72 warnx("no changes made"); 73 (*Pw_error)(NULL, 0, 0); 74 } 75 if (verify(tempname, pw)) 76 break; 77 #ifdef YP 78 if (use_yp) 79 yppw_prompt(); 80 else 81 #endif 82 pw_prompt(); 83 } 84 } 85 86 /* 87 * display -- 88 * print out the file for the user to edit; strange side-effect: 89 * set conditional flag if the user gets to edit the shell. 90 */ 91 void 92 display(tempname, fd, pw) 93 char *tempname; 94 int fd; 95 struct passwd *pw; 96 { 97 FILE *fp; 98 char *bp, *p; 99 char chgstr[256], expstr[256]; 100 101 if (!(fp = fdopen(fd, "w"))) 102 (*Pw_error)(tempname, 1, 1); 103 104 (void)fprintf(fp, 105 "#Changing user %sdatabase information for %s.\n", 106 use_yp ? "YP " : "", pw->pw_name); 107 if (!uid) { 108 (void)fprintf(fp, "Login: %s\n", pw->pw_name); 109 (void)fprintf(fp, "Password: %s\n", pw->pw_passwd); 110 (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid); 111 (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid); 112 (void)fprintf(fp, "Change [month day year]: %s\n", 113 ttoa(chgstr, sizeof chgstr, pw->pw_change)); 114 (void)fprintf(fp, "Expire [month day year]: %s\n", 115 ttoa(expstr, sizeof expstr, pw->pw_expire)); 116 (void)fprintf(fp, "Class: %s\n", pw->pw_class); 117 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir); 118 (void)fprintf(fp, "Shell: %s\n", 119 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 120 } 121 /* Only admin can change "restricted" shells. */ 122 else if (ok_shell(pw->pw_shell)) 123 /* 124 * Make shell a restricted field. Ugly with a 125 * necklace, but there's not much else to do. 126 */ 127 (void)fprintf(fp, "Shell: %s\n", 128 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 129 else 130 list[E_SHELL].restricted = 1; 131 bp = strdup(pw->pw_gecos); 132 if (!bp) { 133 err(1, "strdup"); 134 /*NOTREACHED*/ 135 } 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, fd; 159 static char buf[LINE_MAX]; 160 161 if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 || 162 (fp = fdopen(fd, "r")) == NULL) 163 (*Pw_error)(tempname, 1, 1); 164 if (fstat(fd, &sb)) 165 (*Pw_error)(tempname, 1, 1); 166 if (sb.st_size == 0 || sb.st_nlink != 1) { 167 warnx("corrupted temporary file"); 168 goto bad; 169 } 170 while (fgets(buf, sizeof(buf), fp)) { 171 if (!buf[0] || buf[0] == '#') 172 continue; 173 if (!(p = strchr(buf, '\n'))) { 174 warnx("line too long"); 175 goto bad; 176 } 177 *p = '\0'; 178 for (ep = list;; ++ep) { 179 if (!ep->prompt) { 180 warnx("unrecognized field"); 181 goto bad; 182 } 183 if (!strncasecmp(buf, ep->prompt, ep->len)) { 184 if (ep->restricted && uid) { 185 warnx( 186 "you may not change the %s field", 187 ep->prompt); 188 goto bad; 189 } 190 if (!(p = strchr(buf, ':'))) { 191 warnx("line corrupted"); 192 goto bad; 193 } 194 while (isspace((unsigned char)*++p)); 195 if (ep->except && strpbrk(p, ep->except)) { 196 warnx( 197 "illegal character in the \"%s\" field", 198 ep->prompt); 199 goto bad; 200 } 201 if ((ep->func)(p, pw, ep)) { 202 bad: (void)fclose(fp); 203 return (0); 204 } 205 break; 206 } 207 } 208 } 209 (void)fclose(fp); 210 211 /* Build the gecos field. */ 212 len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) + 213 strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4; 214 if (!(p = malloc(len))) 215 err(1, "malloc"); 216 (void)snprintf(p, len, "%s,%s,%s,%s", list[E_NAME].save, 217 list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save); 218 pw->pw_gecos = p; 219 220 if (snprintf(buf, sizeof(buf), 221 "%s:%s:%d:%d:%s:%lu:%lu:%s:%s:%s", 222 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 223 (u_long)pw->pw_change, (u_long)pw->pw_expire, pw->pw_gecos, 224 pw->pw_dir, pw->pw_shell) >= sizeof(buf)) { 225 warnx("entries too long"); 226 return (0); 227 } 228 return (pw_scan(buf, pw, (int *)NULL)); 229 } 230