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