1 /* $OpenBSD: edit.c,v 1.27 2003/06/03 02:56:06 millert Exp $ */ 2 /* $NetBSD: edit.c,v 1.6 1996/05/15 21:50:45 jtc Exp $ */ 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. 16 * 3. Neither the name of the University nor the names of its contributors 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 37 static char rcsid[] = "$OpenBSD: edit.c,v 1.27 2003/06/03 02:56:06 millert 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 <fcntl.h> 48 #include <paths.h> 49 #include <pwd.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include <util.h> 55 56 #include "chpass.h" 57 58 int 59 edit(tempname, pw) 60 char *tempname; 61 struct passwd *pw; 62 { 63 struct stat begin, end; 64 65 for (;;) { 66 if (lstat(tempname, &begin) == -1 || S_ISLNK(begin.st_mode)) 67 return (EDIT_ERROR); 68 pw_edit(1, tempname); 69 if (lstat(tempname, &end) == -1 || S_ISLNK(end.st_mode)) 70 return (EDIT_ERROR); 71 if (begin.st_mtime == end.st_mtime && 72 begin.st_size == end.st_size) { 73 warnx("no changes made"); 74 return (EDIT_NOCHANGE); 75 } 76 if (verify(tempname, pw)) 77 break; 78 pw_prompt(); 79 } 80 return(EDIT_OK); 81 } 82 83 /* 84 * display -- 85 * print out the file for the user to edit; strange side-effect: 86 * set conditional flag if the user gets to edit the shell. 87 */ 88 void 89 display(tempname, fd, pw) 90 char *tempname; 91 int fd; 92 struct passwd *pw; 93 { 94 FILE *fp; 95 char *bp, *p; 96 char chngstr[256]; 97 98 if (!(fp = fdopen(fd, "w"))) 99 pw_error(tempname, 1, 1); 100 101 (void)fprintf(fp, 102 "# Changing user database information for %s.\n", pw->pw_name); 103 if (!uid) { 104 (void)fprintf(fp, "Login: %s\n", pw->pw_name); 105 (void)fprintf(fp, "Encrypted password: %s\n", pw->pw_passwd); 106 (void)fprintf(fp, "Uid [#]: %u\n", pw->pw_uid); 107 (void)fprintf(fp, "Gid [# or name]: %u\n", pw->pw_gid); 108 (void)fprintf(fp, "Change [month day year]: %s\n", 109 ttoa(chngstr, sizeof(chngstr), pw->pw_change)); 110 (void)fprintf(fp, "Expire [month day year]: %s\n", 111 ttoa(chngstr, sizeof(chngstr), pw->pw_expire)); 112 (void)fprintf(fp, "Class: %s\n", pw->pw_class); 113 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir); 114 (void)fprintf(fp, "Shell: %s\n", 115 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 116 } 117 /* Only admin can change "restricted" shells. */ 118 else if (ok_shell(pw->pw_shell)) 119 /* 120 * Make shell a restricted field. Ugly with a 121 * necklace, but there's not much else to do. 122 */ 123 (void)fprintf(fp, "Shell: %s\n", 124 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 125 else 126 list[E_SHELL].restricted = 1; 127 bp = pw->pw_gecos; 128 p = strsep(&bp, ","); 129 (void)fprintf(fp, "Full Name: %s\n", p ? p : ""); 130 p = strsep(&bp, ","); 131 (void)fprintf(fp, "Office Location: %s\n", p ? p : ""); 132 p = strsep(&bp, ","); 133 (void)fprintf(fp, "Office Phone: %s\n", p ? p : ""); 134 p = strsep(&bp, ","); 135 (void)fprintf(fp, "Home Phone: %s\n", p ? p : ""); 136 137 (void)fchown(fd, getuid(), getgid()); 138 (void)fclose(fp); 139 } 140 141 int 142 verify(tempname, pw) 143 char *tempname; 144 struct passwd *pw; 145 { 146 unsigned int len, alen, line; 147 static char buf[LINE_MAX]; 148 struct stat sb; 149 char *p, *q; 150 ENTRY *ep; 151 FILE *fp; 152 int fd; 153 154 if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 || 155 (fp = fdopen(fd, "r")) == NULL) 156 pw_error(tempname, 1, 1); 157 if (fstat(fd, &sb)) 158 pw_error(tempname, 1, 1); 159 if (sb.st_size == 0 || sb.st_nlink != 1 || sb.st_uid != uid) { 160 warnx("corrupted temporary file"); 161 goto bad; 162 } 163 line = 0; 164 while (fgets(buf, sizeof(buf), fp)) { 165 line++; 166 if (!buf[0] || buf[0] == '#') 167 continue; 168 if ((p = strchr(buf, '\n')) != NULL) 169 *p = '\0'; 170 else if (!feof(fp)) { 171 warnx("line %u too long", line); 172 goto bad; 173 } 174 for (ep = list;; ++ep) { 175 if (!ep->prompt) { 176 warnx("unrecognized field on line %u", line); 177 goto bad; 178 } 179 if (!strncasecmp(buf, ep->prompt, ep->len)) { 180 if (ep->restricted && uid) { 181 warnx( 182 "you may not change the %s field", 183 ep->prompt); 184 goto bad; 185 } 186 if (!(p = strchr(buf, ':'))) { 187 warnx("line %u corrupted", line); 188 goto bad; 189 } 190 while (isspace(*++p)); 191 for (q = p; *q && isprint(*q); q++) { 192 if (ep->except && strchr(ep->except,*q)) 193 break; 194 } 195 if (*q) { 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 if (list[E_NAME].save == NULL) 212 list[E_NAME].save = ""; 213 if (list[E_BPHONE].save == NULL) 214 list[E_BPHONE].save = ""; 215 if (list[E_HPHONE].save == NULL) 216 list[E_HPHONE].save = ""; 217 if (list[E_LOCATE].save == NULL) 218 list[E_LOCATE].save = ""; 219 220 /* Build the gecos field. */ 221 len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) + 222 strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4; 223 for (alen = 0, p = list[E_NAME].save; *p; p++) 224 if (*p == '&') 225 alen = alen + strlen(pw->pw_name) - 1; 226 if (!(p = malloc(len))) 227 err(1, NULL); 228 (void)snprintf(p, len, "%s,%s,%s,%s", list[E_NAME].save, 229 list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save); 230 pw->pw_gecos = p; 231 232 if (snprintf(buf, sizeof(buf), 233 "%s:%s:%u:%u:%s:%ld:%ld:%s:%s:%s", 234 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 235 (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir, 236 pw->pw_shell) >= 1023 || 237 strlen(buf) + alen >= 1023) { 238 warnx("entries too long"); 239 free(p); 240 return (0); 241 } 242 free(p); 243 244 return (pw_scan(buf, pw, NULL)); 245 } 246