133965Sjdp/*- 278828Sobrien * Copyright (c) 1990, 1993, 1994 3218822Sdim * The Regents of the University of California. All rights reserved. 4218822Sdim * Copyright (c) 2002 Networks Associates Technology, Inc. 533965Sjdp * All rights reserved. 633965Sjdp * 733965Sjdp * Portions of this software were developed for the FreeBSD Project by 833965Sjdp * ThinkSec AS and NAI Labs, the Security Research Division of Network 933965Sjdp * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 1033965Sjdp * ("CBOSS"), as part of the DARPA CHATS research program. 1133965Sjdp * 1233965Sjdp * Redistribution and use in source and binary forms, with or without 1333965Sjdp * modification, are permitted provided that the following conditions 1433965Sjdp * are met: 1533965Sjdp * 1. Redistributions of source code must retain the above copyright 1633965Sjdp * notice, this list of conditions and the following disclaimer. 1733965Sjdp * 2. Redistributions in binary form must reproduce the above copyright 1833965Sjdp * notice, this list of conditions and the following disclaimer in the 1933965Sjdp * documentation and/or other materials provided with the distribution. 2033965Sjdp * 3. All advertising materials mentioning features or use of this software 2133965Sjdp * must display the following acknowledgement: 2233965Sjdp * This product includes software developed by the University of 2333965Sjdp * California, Berkeley and its contributors. 2433965Sjdp * 4. Neither the name of the University nor the names of its contributors 2533965Sjdp * may be used to endorse or promote products derived from this software 2633965Sjdp * without specific prior written permission. 2733965Sjdp * 2833965Sjdp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2933965Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3033965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3133965Sjdp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32218822Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3333965Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3433965Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3533965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3633965Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37218822Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3833965Sjdp * SUCH DAMAGE. 3933965Sjdp */ 4033965Sjdp 4133965Sjdp#if 0 4233965Sjdp#ifndef lint 4333965Sjdpstatic char sccsid[] = "@(#)edit.c 8.3 (Berkeley) 4/2/94"; 4433965Sjdp#endif /* not lint */ 4533965Sjdp#endif 4633965Sjdp 4733965Sjdp#include <sys/cdefs.h> 4833965Sjdp__FBSDID("$FreeBSD: releng/10.2/usr.bin/chpass/edit.c 113304 2003-04-09 18:18:42Z des $"); 4933965Sjdp 5033965Sjdp#include <sys/param.h> 5133965Sjdp#include <sys/stat.h> 5233965Sjdp 5333965Sjdp#include <ctype.h> 5433965Sjdp#include <err.h> 5533965Sjdp#include <errno.h> 5633965Sjdp#include <paths.h> 5733965Sjdp#include <pwd.h> 5833965Sjdp#include <stdio.h> 5933965Sjdp#include <stdlib.h> 6033965Sjdp#include <string.h> 6133965Sjdp#include <unistd.h> 6233965Sjdp 6333965Sjdp#include <pw_scan.h> 6433965Sjdp#include <libutil.h> 6533965Sjdp 6633965Sjdp#include "chpass.h" 6733965Sjdp 68218822Sdimstatic int display(const char *tfn, struct passwd *pw); 6933965Sjdpstatic struct passwd *verify(const char *tfn, struct passwd *pw); 7089857Sobrien 7133965Sjdpstruct passwd * 7233965Sjdpedit(const char *tfn, struct passwd *pw) 7333965Sjdp{ 7433965Sjdp struct passwd *npw; 7533965Sjdp char *line; 7633965Sjdp size_t len; 7733965Sjdp 7833965Sjdp if (display(tfn, pw) == -1) 7933965Sjdp return (NULL); 8033965Sjdp for (;;) { 8133965Sjdp switch (pw_edit(1)) { 8233965Sjdp case -1: 8333965Sjdp return (NULL); 8433965Sjdp case 0: 8533965Sjdp return (pw_dup(pw)); 8633965Sjdp default: 8733965Sjdp break; 8833965Sjdp } 8933965Sjdp if ((npw = verify(tfn, pw)) != NULL) 9033965Sjdp return (npw); 9133965Sjdp free(npw); 9233965Sjdp printf("re-edit the password file? "); 9333965Sjdp fflush(stdout); 9433965Sjdp if ((line = fgetln(stdin, &len)) == NULL) { 9533965Sjdp warn("fgetln()"); 9633965Sjdp return (NULL); 9733965Sjdp } 9833965Sjdp if (len > 0 && (*line == 'N' || *line == 'n')) 9933965Sjdp return (NULL); 10033965Sjdp } 10133965Sjdp} 10233965Sjdp 10333965Sjdp/* 10433965Sjdp * display -- 10533965Sjdp * print out the file for the user to edit; strange side-effect: 106130561Sobrien * set conditional flag if the user gets to edit the shell. 10733965Sjdp */ 10833965Sjdpstatic int 109218822Sdimdisplay(const char *tfn, struct passwd *pw) 110218822Sdim{ 11133965Sjdp FILE *fp; 11233965Sjdp char *bp, *gecos, *p; 11333965Sjdp 11433965Sjdp if ((fp = fopen(tfn, "w")) == NULL) { 11533965Sjdp warn("%s", tfn); 11633965Sjdp return (-1); 11733965Sjdp } 11833965Sjdp 11933965Sjdp (void)fprintf(fp, 12033965Sjdp "#Changing user information for %s.\n", pw->pw_name); 12133965Sjdp if (master_mode) { 12233965Sjdp (void)fprintf(fp, "Login: %s\n", pw->pw_name); 12377298Sobrien (void)fprintf(fp, "Password: %s\n", pw->pw_passwd); 12477298Sobrien (void)fprintf(fp, "Uid [#]: %lu\n", (unsigned long)pw->pw_uid); 12533965Sjdp (void)fprintf(fp, "Gid [# or name]: %lu\n", 12633965Sjdp (unsigned long)pw->pw_gid); 12733965Sjdp (void)fprintf(fp, "Change [month day year]: %s\n", 12833965Sjdp ttoa(pw->pw_change)); 12933965Sjdp (void)fprintf(fp, "Expire [month day year]: %s\n", 13033965Sjdp ttoa(pw->pw_expire)); 13133965Sjdp (void)fprintf(fp, "Class: %s\n", pw->pw_class); 13233965Sjdp (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir); 13333965Sjdp (void)fprintf(fp, "Shell: %s\n", 13433965Sjdp *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 13533965Sjdp } 13633965Sjdp /* Only admin can change "restricted" shells. */ 13733965Sjdp#if 0 13833965Sjdp else if (ok_shell(pw->pw_shell)) 13933965Sjdp /* 14033965Sjdp * Make shell a restricted field. Ugly with a 14133965Sjdp * necklace, but there's not much else to do. 14233965Sjdp */ 143218822Sdim#else 144130561Sobrien else if ((!list[E_SHELL].restricted && ok_shell(pw->pw_shell)) || 145218822Sdim master_mode) 146218822Sdim /* 147130561Sobrien * If change not restrict (table.c) and standard shell 14833965Sjdp * OR if root, then allow editing of shell. 14933965Sjdp */ 15033965Sjdp#endif 15133965Sjdp (void)fprintf(fp, "Shell: %s\n", 15233965Sjdp *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 15333965Sjdp else 15433965Sjdp list[E_SHELL].restricted = 1; 15589857Sobrien 15689857Sobrien if ((bp = gecos = strdup(pw->pw_gecos)) == NULL) { 15789857Sobrien warn(NULL); 15889857Sobrien fclose(fp); 15933965Sjdp return (-1); 16033965Sjdp } 16189857Sobrien 16289857Sobrien p = strsep(&bp, ","); 16389857Sobrien p = strdup(p ? p : ""); 16489857Sobrien list[E_NAME].save = p; 16533965Sjdp if (!list[E_NAME].restricted || master_mode) 16633965Sjdp (void)fprintf(fp, "Full Name: %s\n", p); 16733965Sjdp 16877298Sobrien p = strsep(&bp, ","); 16933965Sjdp p = strdup(p ? p : ""); 170218822Sdim list[E_LOCATE].save = p; 171130561Sobrien if (!list[E_LOCATE].restricted || master_mode) 172130561Sobrien (void)fprintf(fp, "Office Location: %s\n", p); 173130561Sobrien 174130561Sobrien p = strsep(&bp, ","); 17533965Sjdp p = strdup(p ? p : ""); 176130561Sobrien list[E_BPHONE].save = p; 177130561Sobrien if (!list[E_BPHONE].restricted || master_mode) 17860484Sobrien (void)fprintf(fp, "Office Phone: %s\n", p); 17960484Sobrien 18089857Sobrien p = strsep(&bp, ","); 18160484Sobrien p = strdup(p ? p : ""); 18289857Sobrien list[E_HPHONE].save = p; 18360484Sobrien if (!list[E_HPHONE].restricted || master_mode) 18489857Sobrien (void)fprintf(fp, "Home Phone: %s\n", p); 18589857Sobrien 18689857Sobrien bp = strdup(bp ? bp : ""); 18789857Sobrien list[E_OTHER].save = bp; 18889857Sobrien if (!list[E_OTHER].restricted || master_mode) 18989857Sobrien (void)fprintf(fp, "Other information: %s\n", bp); 19089857Sobrien 19189857Sobrien free(gecos); 192218822Sdim 19389857Sobrien (void)fchown(fileno(fp), getuid(), getgid()); 19489857Sobrien (void)fclose(fp); 195218822Sdim return (0); 19633965Sjdp} 19733965Sjdp 19833965Sjdpstatic struct passwd * 19977298Sobrienverify(const char *tfn, struct passwd *pw) 20033965Sjdp{ 20133965Sjdp struct passwd *npw; 202130561Sobrien ENTRY *ep; 203130561Sobrien char *buf, *p, *val; 204130561Sobrien struct stat sb; 205130561Sobrien FILE *fp; 20633965Sjdp int line; 20789857Sobrien size_t len; 208130561Sobrien 20989857Sobrien if ((pw = pw_dup(pw)) == NULL) 21089857Sobrien return (NULL); 21189857Sobrien if ((fp = fopen(tfn, "r")) == NULL || 21289857Sobrien fstat(fileno(fp), &sb) == -1) { 21389857Sobrien warn("%s", tfn); 21489857Sobrien free(pw); 21589857Sobrien return (NULL); 21689857Sobrien } 21789857Sobrien if (sb.st_size == 0) { 21889857Sobrien warnx("corrupted temporary file"); 21989857Sobrien fclose(fp); 22089857Sobrien free(pw); 22189857Sobrien return (NULL); 22289857Sobrien } 22333965Sjdp val = NULL; 22433965Sjdp for (line = 1; (buf = fgetln(fp, &len)) != NULL; ++line) { 22533965Sjdp if (*buf == '\0' || *buf == '#') 22677298Sobrien continue; 22733965Sjdp while (len > 0 && isspace(buf[len - 1])) 22833965Sjdp --len; 229130561Sobrien for (ep = list;; ++ep) { 230130561Sobrien if (!ep->prompt) { 231130561Sobrien warnx("%s: unrecognized field on line %d", 23233965Sjdp tfn, line); 23377298Sobrien goto bad; 23433965Sjdp } 23589857Sobrien if (ep->len > len) 23689857Sobrien continue; 23789857Sobrien if (strncasecmp(buf, ep->prompt, ep->len) != 0) 23877298Sobrien continue; 23989857Sobrien if (ep->restricted && !master_mode) { 24077298Sobrien warnx("%s: you may not change the %s field", 24189857Sobrien tfn, ep->prompt); 24289857Sobrien goto bad; 24389857Sobrien } 24489857Sobrien for (p = buf; p < buf + len && *p != ':'; ++p) 24589857Sobrien /* nothing */ ; 24689857Sobrien if (*p != ':') { 24789857Sobrien warnx("%s: line %d corrupted", tfn, line); 24889857Sobrien goto bad; 24989857Sobrien } 25089857Sobrien while (++p < buf + len && isspace(*p)) 25133965Sjdp /* nothing */ ; 25233965Sjdp free(val); 25333965Sjdp asprintf(&val, "%.*s", (int)(buf + len - p), p); 25477298Sobrien if (val == NULL) 25533965Sjdp goto bad; 25633965Sjdp if (ep->except && strpbrk(val, ep->except)) { 257130561Sobrien warnx("%s: invalid character in \"%s\" field '%s'", 258130561Sobrien tfn, ep->prompt, val); 259130561Sobrien goto bad; 26033965Sjdp } 26189857Sobrien if ((ep->func)(val, pw, ep)) 26277298Sobrien goto bad; 26333965Sjdp break; 26477298Sobrien } 26589857Sobrien } 26689857Sobrien free(val); 26789857Sobrien fclose(fp); 26877298Sobrien 26989857Sobrien /* Build the gecos field. */ 27077298Sobrien len = asprintf(&p, "%s,%s,%s,%s,%s", list[E_NAME].save, 27189857Sobrien list[E_LOCATE].save, list[E_BPHONE].save, 27289857Sobrien list[E_HPHONE].save, list[E_OTHER].save); 27389857Sobrien if (p == NULL) { 27489857Sobrien warn("asprintf()"); 27589857Sobrien free(pw); 27689857Sobrien return (NULL); 27789857Sobrien } 27889857Sobrien while (len > 0 && p[len - 1] == ',') 27989857Sobrien p[--len] = '\0'; 28089857Sobrien pw->pw_gecos = p; 28189857Sobrien buf = pw_make(pw); 28289857Sobrien free(pw); 28389857Sobrien free(p); 28489857Sobrien if (buf == NULL) { 28589857Sobrien warn("pw_make()"); 28689857Sobrien return (NULL); 28733965Sjdp } 28833965Sjdp npw = pw_scan(buf, PWSCAN_WARN|PWSCAN_MASTER); 28933965Sjdp free(buf); 29077298Sobrien return (npw); 29133965Sjdpbad: 29233965Sjdp free(pw); 293130561Sobrien free(val); 294130561Sobrien fclose(fp); 295130561Sobrien return (NULL); 29633965Sjdp} 29760484Sobrien