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