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