NetBSD/usr.bin/chpass/edit.c

232 lines
6.4 KiB
C
Raw Normal View History

2002-11-16 18:59:26 +03:00
/* $NetBSD: edit.c,v 1.14 2002/11/16 15:59:27 itojun Exp $ */
1995-03-26 08:55:22 +04:00
1993-03-21 12:45:37 +03:00
/*-
1995-03-26 08:55:22 +04:00
* Copyright (c) 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1995-03-26 08:55:22 +04:00
#if 0
static char sccsid[] = "@(#)edit.c 8.3 (Berkeley) 4/2/94";
#else
2002-11-16 18:59:26 +03:00
__RCSID("$NetBSD: edit.c,v 1.14 2002/11/16 15:59:27 itojun Exp $");
1995-03-26 08:55:22 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
1995-03-26 08:55:22 +04:00
#include <ctype.h>
#include <err.h>
1993-03-21 12:45:37 +03:00
#include <errno.h>
#include <paths.h>
1995-03-26 08:55:22 +04:00
#include <pwd.h>
#include <stdio.h>
1993-03-21 12:45:37 +03:00
#include <stdlib.h>
#include <string.h>
1995-03-26 08:55:22 +04:00
#include <unistd.h>
#include <util.h>
1995-03-26 08:55:22 +04:00
1993-03-21 12:45:37 +03:00
#include "chpass.h"
void
edit(tempname, pw)
char *tempname;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
{
struct stat begin, end;
for (;;) {
if (stat(tempname, &begin))
(*Pw_error)(tempname, 1, 1);
pw_edit(1, tempname);
1993-03-21 12:45:37 +03:00
if (stat(tempname, &end))
(*Pw_error)(tempname, 1, 1);
1993-03-21 12:45:37 +03:00
if (begin.st_mtime == end.st_mtime) {
1995-03-26 08:55:22 +04:00
warnx("no changes made");
(*Pw_error)(NULL, 0, 0);
1993-03-21 12:45:37 +03:00
}
if (verify(tempname, pw))
1993-03-21 12:45:37 +03:00
break;
#ifdef YP
if (use_yp)
yppw_prompt();
else
#endif
pw_prompt();
1993-03-21 12:45:37 +03:00
}
}
/*
* display --
* print out the file for the user to edit; strange side-effect:
* set conditional flag if the user gets to edit the shell.
*/
1995-03-26 08:55:22 +04:00
void
display(tempname, fd, pw)
char *tempname;
1993-03-21 12:45:37 +03:00
int fd;
struct passwd *pw;
{
FILE *fp;
char *bp, *p;
1998-02-04 18:21:52 +03:00
char chgstr[256], expstr[256];
1993-03-21 12:45:37 +03:00
if (!(fp = fdopen(fd, "w")))
(*Pw_error)(tempname, 1, 1);
1993-03-21 12:45:37 +03:00
(void)fprintf(fp,
"#Changing user %sdatabase information for %s.\n",
use_yp ? "YP " : "", pw->pw_name);
1993-03-21 12:45:37 +03:00
if (!uid) {
(void)fprintf(fp, "Login: %s\n", pw->pw_name);
(void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
(void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
(void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
(void)fprintf(fp, "Change [month day year]: %s\n",
1998-02-04 18:21:52 +03:00
ttoa(chgstr, sizeof chgstr, pw->pw_change));
1993-03-21 12:45:37 +03:00
(void)fprintf(fp, "Expire [month day year]: %s\n",
1998-02-04 18:21:52 +03:00
ttoa(expstr, sizeof expstr, pw->pw_expire));
1993-03-21 12:45:37 +03:00
(void)fprintf(fp, "Class: %s\n", pw->pw_class);
(void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
(void)fprintf(fp, "Shell: %s\n",
*pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
}
/* Only admin can change "restricted" shells. */
else if (ok_shell(pw->pw_shell))
/*
* Make shell a restricted field. Ugly with a
* necklace, but there's not much else to do.
*/
(void)fprintf(fp, "Shell: %s\n",
*pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
else
list[E_SHELL].restricted = 1;
1998-07-27 01:25:16 +04:00
bp = strdup(pw->pw_gecos);
2002-11-16 18:59:26 +03:00
if (!bp) {
err(1, "strdup");
/*NOTREACHED*/
}
1993-03-21 12:45:37 +03:00
p = strsep(&bp, ",");
(void)fprintf(fp, "Full Name: %s\n", p ? p : "");
p = strsep(&bp, ",");
(void)fprintf(fp, "Location: %s\n", p ? p : "");
p = strsep(&bp, ",");
(void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
p = strsep(&bp, ",");
(void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
(void)fchown(fd, getuid(), getgid());
(void)fclose(fp);
}
1995-03-26 08:55:22 +04:00
int
verify(tempname, pw)
char *tempname;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
{
1995-03-26 08:55:22 +04:00
ENTRY *ep;
char *p;
struct stat sb;
1993-03-21 12:45:37 +03:00
FILE *fp;
int len;
1994-08-17 23:54:34 +04:00
static char buf[LINE_MAX];
1993-03-21 12:45:37 +03:00
if (!(fp = fopen(tempname, "r")))
(*Pw_error)(tempname, 1, 1);
1995-03-26 08:55:22 +04:00
if (fstat(fileno(fp), &sb))
(*Pw_error)(tempname, 1, 1);
1995-03-26 08:55:22 +04:00
if (sb.st_size == 0) {
warnx("corrupted temporary file");
goto bad;
}
1993-03-21 12:45:37 +03:00
while (fgets(buf, sizeof(buf), fp)) {
if (!buf[0] || buf[0] == '#')
continue;
1995-03-26 08:55:22 +04:00
if (!(p = strchr(buf, '\n'))) {
warnx("line too long");
1993-03-21 12:45:37 +03:00
goto bad;
}
*p = '\0';
for (ep = list;; ++ep) {
if (!ep->prompt) {
1995-03-26 08:55:22 +04:00
warnx("unrecognized field");
1993-03-21 12:45:37 +03:00
goto bad;
}
if (!strncasecmp(buf, ep->prompt, ep->len)) {
if (ep->restricted && uid) {
1995-03-26 08:55:22 +04:00
warnx(
"you may not change the %s field",
ep->prompt);
1993-03-21 12:45:37 +03:00
goto bad;
}
1995-03-26 08:55:22 +04:00
if (!(p = strchr(buf, ':'))) {
warnx("line corrupted");
1993-03-21 12:45:37 +03:00
goto bad;
}
while (isspace(*++p));
if (ep->except && strpbrk(p, ep->except)) {
1995-03-26 08:55:22 +04:00
warnx(
"illegal character in the \"%s\" field",
1993-03-21 12:45:37 +03:00
ep->prompt);
goto bad;
}
if ((ep->func)(p, pw, ep)) {
bad: (void)fclose(fp);
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
break;
}
}
}
(void)fclose(fp);
/* Build the gecos field. */
len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
1995-03-26 08:55:22 +04:00
if (!(p = malloc(len)))
err(1, "malloc");
1998-07-27 01:25:16 +04:00
(void)snprintf(p, len, "%s,%s,%s,%s", list[E_NAME].save,
1993-03-21 12:45:37 +03:00
list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
1998-07-27 01:25:16 +04:00
pw->pw_gecos = p;
1993-03-21 12:45:37 +03:00
if (snprintf(buf, sizeof(buf),
1997-10-19 18:05:56 +04:00
"%s:%s:%d:%d:%s:%lu:%lu:%s:%s:%s",
1993-03-21 12:45:37 +03:00
pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
1997-10-19 18:05:56 +04:00
(u_long)pw->pw_change, (u_long)pw->pw_expire, pw->pw_gecos,
pw->pw_dir, pw->pw_shell) >= sizeof(buf)) {
1995-03-26 08:55:22 +04:00
warnx("entries too long");
return (0);
1993-03-21 12:45:37 +03:00
}
return (pw_scan(buf, pw, (int *)NULL));
1993-03-21 12:45:37 +03:00
}