1 1.22 shm /* $NetBSD: edit.c,v 1.22 2015/10/27 14:47:45 shm Exp $ */ 2 1.4 glass 3 1.1 cgd /*- 4 1.4 glass * Copyright (c) 1990, 1993, 1994 5 1.4 glass * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * Redistribution and use in source and binary forms, with or without 8 1.1 cgd * modification, are permitted provided that the following conditions 9 1.1 cgd * are met: 10 1.1 cgd * 1. Redistributions of source code must retain the above copyright 11 1.1 cgd * notice, this list of conditions and the following disclaimer. 12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer in the 14 1.1 cgd * documentation and/or other materials provided with the distribution. 15 1.16 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 cgd * may be used to endorse or promote products derived from this software 17 1.1 cgd * without specific prior written permission. 18 1.1 cgd * 19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 cgd * SUCH DAMAGE. 30 1.1 cgd */ 31 1.1 cgd 32 1.9 lukem #include <sys/cdefs.h> 33 1.1 cgd #ifndef lint 34 1.4 glass #if 0 35 1.4 glass static char sccsid[] = "@(#)edit.c 8.3 (Berkeley) 4/2/94"; 36 1.4 glass #else 37 1.22 shm __RCSID("$NetBSD: edit.c,v 1.22 2015/10/27 14:47:45 shm Exp $"); 38 1.4 glass #endif 39 1.1 cgd #endif /* not lint */ 40 1.1 cgd 41 1.1 cgd #include <sys/param.h> 42 1.1 cgd #include <sys/stat.h> 43 1.4 glass 44 1.4 glass #include <ctype.h> 45 1.4 glass #include <err.h> 46 1.4 glass #include <errno.h> 47 1.4 glass #include <paths.h> 48 1.1 cgd #include <pwd.h> 49 1.1 cgd #include <stdio.h> 50 1.1 cgd #include <stdlib.h> 51 1.1 cgd #include <string.h> 52 1.4 glass #include <unistd.h> 53 1.15 christos #include <fcntl.h> 54 1.6 jtc #include <util.h> 55 1.4 glass 56 1.1 cgd #include "chpass.h" 57 1.1 cgd 58 1.1 cgd void 59 1.18 xtraeme edit(char *tempname, struct passwd *pw) 60 1.1 cgd { 61 1.1 cgd struct stat begin, end; 62 1.1 cgd 63 1.1 cgd for (;;) { 64 1.1 cgd if (stat(tempname, &begin)) 65 1.7 thorpej (*Pw_error)(tempname, 1, 1); 66 1.6 jtc pw_edit(1, tempname); 67 1.1 cgd if (stat(tempname, &end)) 68 1.7 thorpej (*Pw_error)(tempname, 1, 1); 69 1.1 cgd if (begin.st_mtime == end.st_mtime) { 70 1.4 glass warnx("no changes made"); 71 1.7 thorpej (*Pw_error)(NULL, 0, 0); 72 1.1 cgd } 73 1.6 jtc if (verify(tempname, pw)) 74 1.1 cgd break; 75 1.7 thorpej #ifdef YP 76 1.7 thorpej if (use_yp) 77 1.7 thorpej yppw_prompt(); 78 1.7 thorpej else 79 1.7 thorpej #endif 80 1.7 thorpej pw_prompt(); 81 1.1 cgd } 82 1.1 cgd } 83 1.1 cgd 84 1.1 cgd /* 85 1.1 cgd * display -- 86 1.1 cgd * print out the file for the user to edit; strange side-effect: 87 1.1 cgd * set conditional flag if the user gets to edit the shell. 88 1.1 cgd */ 89 1.4 glass void 90 1.18 xtraeme display(char *tempname, int fd, struct passwd *pw) 91 1.1 cgd { 92 1.1 cgd FILE *fp; 93 1.9 lukem char *bp, *p; 94 1.11 christos char chgstr[256], expstr[256]; 95 1.1 cgd 96 1.1 cgd if (!(fp = fdopen(fd, "w"))) 97 1.7 thorpej (*Pw_error)(tempname, 1, 1); 98 1.1 cgd 99 1.1 cgd (void)fprintf(fp, 100 1.7 thorpej "#Changing user %sdatabase information for %s.\n", 101 1.7 thorpej use_yp ? "YP " : "", pw->pw_name); 102 1.1 cgd if (!uid) { 103 1.1 cgd (void)fprintf(fp, "Login: %s\n", pw->pw_name); 104 1.1 cgd (void)fprintf(fp, "Password: %s\n", pw->pw_passwd); 105 1.1 cgd (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid); 106 1.1 cgd (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid); 107 1.1 cgd (void)fprintf(fp, "Change [month day year]: %s\n", 108 1.11 christos ttoa(chgstr, sizeof chgstr, pw->pw_change)); 109 1.1 cgd (void)fprintf(fp, "Expire [month day year]: %s\n", 110 1.11 christos ttoa(expstr, sizeof expstr, pw->pw_expire)); 111 1.1 cgd (void)fprintf(fp, "Class: %s\n", pw->pw_class); 112 1.1 cgd (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir); 113 1.1 cgd (void)fprintf(fp, "Shell: %s\n", 114 1.1 cgd *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 115 1.1 cgd } 116 1.1 cgd /* Only admin can change "restricted" shells. */ 117 1.1 cgd else if (ok_shell(pw->pw_shell)) 118 1.1 cgd /* 119 1.1 cgd * Make shell a restricted field. Ugly with a 120 1.1 cgd * necklace, but there's not much else to do. 121 1.1 cgd */ 122 1.1 cgd (void)fprintf(fp, "Shell: %s\n", 123 1.1 cgd *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 124 1.1 cgd else 125 1.1 cgd list[E_SHELL].restricted = 1; 126 1.12 mycroft bp = strdup(pw->pw_gecos); 127 1.14 itojun if (!bp) { 128 1.14 itojun err(1, "strdup"); 129 1.14 itojun /*NOTREACHED*/ 130 1.14 itojun } 131 1.1 cgd p = strsep(&bp, ","); 132 1.1 cgd (void)fprintf(fp, "Full Name: %s\n", p ? p : ""); 133 1.1 cgd p = strsep(&bp, ","); 134 1.1 cgd (void)fprintf(fp, "Location: %s\n", p ? p : ""); 135 1.1 cgd p = strsep(&bp, ","); 136 1.1 cgd (void)fprintf(fp, "Office Phone: %s\n", p ? p : ""); 137 1.1 cgd p = strsep(&bp, ","); 138 1.1 cgd (void)fprintf(fp, "Home Phone: %s\n", p ? p : ""); 139 1.1 cgd 140 1.1 cgd (void)fchown(fd, getuid(), getgid()); 141 1.1 cgd (void)fclose(fp); 142 1.22 shm free(bp); 143 1.1 cgd } 144 1.1 cgd 145 1.4 glass int 146 1.18 xtraeme verify(char *tempname, struct passwd *pw) 147 1.1 cgd { 148 1.4 glass ENTRY *ep; 149 1.4 glass char *p; 150 1.4 glass struct stat sb; 151 1.19 lukem FILE *fp = NULL; 152 1.15 christos int len, fd; 153 1.3 deraadt static char buf[LINE_MAX]; 154 1.1 cgd 155 1.15 christos if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 || 156 1.15 christos (fp = fdopen(fd, "r")) == NULL) 157 1.7 thorpej (*Pw_error)(tempname, 1, 1); 158 1.15 christos if (fstat(fd, &sb)) 159 1.7 thorpej (*Pw_error)(tempname, 1, 1); 160 1.15 christos if (sb.st_size == 0 || sb.st_nlink != 1) { 161 1.4 glass warnx("corrupted temporary file"); 162 1.4 glass goto bad; 163 1.4 glass } 164 1.1 cgd while (fgets(buf, sizeof(buf), fp)) { 165 1.1 cgd if (!buf[0] || buf[0] == '#') 166 1.1 cgd continue; 167 1.4 glass if (!(p = strchr(buf, '\n'))) { 168 1.4 glass warnx("line too long"); 169 1.1 cgd goto bad; 170 1.1 cgd } 171 1.1 cgd *p = '\0'; 172 1.1 cgd for (ep = list;; ++ep) { 173 1.1 cgd if (!ep->prompt) { 174 1.4 glass warnx("unrecognized field"); 175 1.1 cgd goto bad; 176 1.1 cgd } 177 1.1 cgd if (!strncasecmp(buf, ep->prompt, ep->len)) { 178 1.1 cgd if (ep->restricted && uid) { 179 1.4 glass warnx( 180 1.4 glass "you may not change the %s field", 181 1.4 glass ep->prompt); 182 1.1 cgd goto bad; 183 1.1 cgd } 184 1.4 glass if (!(p = strchr(buf, ':'))) { 185 1.4 glass warnx("line corrupted"); 186 1.1 cgd goto bad; 187 1.1 cgd } 188 1.17 dsl while (isspace((unsigned char)*++p)); 189 1.1 cgd if (ep->except && strpbrk(p, ep->except)) { 190 1.4 glass warnx( 191 1.4 glass "illegal character in the \"%s\" field", 192 1.1 cgd ep->prompt); 193 1.1 cgd goto bad; 194 1.1 cgd } 195 1.1 cgd if ((ep->func)(p, pw, ep)) { 196 1.1 cgd bad: (void)fclose(fp); 197 1.4 glass return (0); 198 1.1 cgd } 199 1.1 cgd break; 200 1.1 cgd } 201 1.1 cgd } 202 1.1 cgd } 203 1.1 cgd (void)fclose(fp); 204 1.1 cgd 205 1.1 cgd /* Build the gecos field. */ 206 1.1 cgd len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) + 207 1.1 cgd strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4; 208 1.4 glass if (!(p = malloc(len))) 209 1.9 lukem err(1, "malloc"); 210 1.12 mycroft (void)snprintf(p, len, "%s,%s,%s,%s", list[E_NAME].save, 211 1.1 cgd list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save); 212 1.12 mycroft pw->pw_gecos = p; 213 1.1 cgd 214 1.1 cgd if (snprintf(buf, sizeof(buf), 215 1.10 mrg "%s:%s:%d:%d:%s:%lu:%lu:%s:%s:%s", 216 1.1 cgd pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 217 1.10 mrg (u_long)pw->pw_change, (u_long)pw->pw_expire, pw->pw_gecos, 218 1.20 lukem pw->pw_dir, pw->pw_shell) >= (int)sizeof(buf)) { 219 1.4 glass warnx("entries too long"); 220 1.4 glass return (0); 221 1.1 cgd } 222 1.21 plunky return (pw_scan(buf, pw, NULL)); 223 1.1 cgd } 224