1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * 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 char sccsid[] = "from: @(#)edit.c 5.2 (Berkeley) 3/3/91";*/ 36 static char rcsid[] = "$Id: edit.c,v 1.3 1994/08/17 19:54:34 deraadt Exp $"; 37 #endif /* not lint */ 38 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 #include <pwd.h> 42 #include <errno.h> 43 #include <stdio.h> 44 #include <paths.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include "chpass.h" 48 49 extern char *tempname; 50 51 void 52 edit(pw) 53 struct passwd *pw; 54 { 55 struct stat begin, end; 56 57 for (;;) { 58 if (stat(tempname, &begin)) 59 pw_error(tempname, 1, 1); 60 pw_edit(1); 61 if (stat(tempname, &end)) 62 pw_error(tempname, 1, 1); 63 if (begin.st_mtime == end.st_mtime) { 64 (void)fprintf(stderr, "chpass: no changes made\n"); 65 pw_error((char *)NULL, 0, 0); 66 } 67 if (verify(pw)) 68 break; 69 pw_prompt(); 70 } 71 } 72 73 /* 74 * display -- 75 * print out the file for the user to edit; strange side-effect: 76 * set conditional flag if the user gets to edit the shell. 77 */ 78 display(fd, pw) 79 int fd; 80 struct passwd *pw; 81 { 82 register char *p; 83 FILE *fp; 84 char *bp, *ok_shell(), *ttoa(); 85 86 if (!(fp = fdopen(fd, "w"))) 87 pw_error(tempname, 1, 1); 88 89 (void)fprintf(fp, 90 "#Changing user database information for %s.\n", pw->pw_name); 91 if (!uid) { 92 (void)fprintf(fp, "Login: %s\n", pw->pw_name); 93 (void)fprintf(fp, "Password: %s\n", pw->pw_passwd); 94 (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid); 95 (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid); 96 (void)fprintf(fp, "Change [month day year]: %s\n", 97 ttoa(pw->pw_change)); 98 (void)fprintf(fp, "Expire [month day year]: %s\n", 99 ttoa(pw->pw_expire)); 100 (void)fprintf(fp, "Class: %s\n", pw->pw_class); 101 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir); 102 (void)fprintf(fp, "Shell: %s\n", 103 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 104 } 105 /* Only admin can change "restricted" shells. */ 106 else if (ok_shell(pw->pw_shell)) 107 /* 108 * Make shell a restricted field. Ugly with a 109 * necklace, but there's not much else to do. 110 */ 111 (void)fprintf(fp, "Shell: %s\n", 112 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 113 else 114 list[E_SHELL].restricted = 1; 115 bp = pw->pw_gecos; 116 p = strsep(&bp, ","); 117 (void)fprintf(fp, "Full Name: %s\n", p ? p : ""); 118 p = strsep(&bp, ","); 119 (void)fprintf(fp, "Location: %s\n", p ? p : ""); 120 p = strsep(&bp, ","); 121 (void)fprintf(fp, "Office Phone: %s\n", p ? p : ""); 122 p = strsep(&bp, ","); 123 (void)fprintf(fp, "Home Phone: %s\n", p ? p : ""); 124 125 (void)fchown(fd, getuid(), getgid()); 126 (void)fclose(fp); 127 } 128 129 verify(pw) 130 struct passwd *pw; 131 { 132 register ENTRY *ep; 133 register char *p; 134 FILE *fp; 135 int len; 136 static char buf[LINE_MAX]; 137 138 if (!(fp = fopen(tempname, "r"))) 139 pw_error(tempname, 1, 1); 140 while (fgets(buf, sizeof(buf), fp)) { 141 if (!buf[0] || buf[0] == '#') 142 continue; 143 if (!(p = index(buf, '\n'))) { 144 (void)fprintf(stderr, "chpass: line too long.\n"); 145 goto bad; 146 } 147 *p = '\0'; 148 for (ep = list;; ++ep) { 149 if (!ep->prompt) { 150 (void)fprintf(stderr, 151 "chpass: unrecognized field.\n"); 152 goto bad; 153 } 154 if (!strncasecmp(buf, ep->prompt, ep->len)) { 155 if (ep->restricted && uid) { 156 (void)fprintf(stderr, 157 "chpass: you may not change the %s field.\n", 158 ep->prompt); 159 goto bad; 160 } 161 if (!(p = index(buf, ':'))) { 162 (void)fprintf(stderr, 163 "chpass: line corrupted.\n"); 164 goto bad; 165 } 166 while (isspace(*++p)); 167 if (ep->except && strpbrk(p, ep->except)) { 168 (void)fprintf(stderr, 169 "chpass: illegal character in the \"%s\" field.\n", 170 ep->prompt); 171 goto bad; 172 } 173 if ((ep->func)(p, pw, ep)) { 174 bad: (void)fclose(fp); 175 return(0); 176 } 177 break; 178 } 179 } 180 } 181 (void)fclose(fp); 182 183 /* Build the gecos field. */ 184 len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) + 185 strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4; 186 if (!(p = malloc(len))) { 187 (void)fprintf(stderr, "chpass: %s\n", strerror(errno)); 188 exit(1); 189 } 190 (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save, 191 list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save); 192 193 if (snprintf(buf, sizeof(buf), 194 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", 195 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 196 pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir, 197 pw->pw_shell) >= sizeof(buf)) { 198 (void)fprintf(stderr, "chpass: entries too long\n"); 199 return(0); 200 } 201 return(pw_scan(buf, pw)); 202 } 203