2005-06-02 05:42:11 +04:00
|
|
|
/* $NetBSD: edit.c,v 1.19 2005/06/02 01:42:11 lukem 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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-10-18 16:48:40 +04:00
|
|
|
#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
|
2005-06-02 05:42:11 +04:00
|
|
|
__RCSID("$NetBSD: edit.c,v 1.19 2005/06/02 01:42:11 lukem 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>
|
2003-02-03 20:45:20 +03:00
|
|
|
#include <fcntl.h>
|
1996-05-16 01:50:39 +04:00
|
|
|
#include <util.h>
|
1995-03-26 08:55:22 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "chpass.h"
|
|
|
|
|
|
|
|
void
|
2005-02-17 19:53:45 +03:00
|
|
|
edit(char *tempname, struct passwd *pw)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct stat begin, end;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (stat(tempname, &begin))
|
1996-08-09 13:22:11 +04:00
|
|
|
(*Pw_error)(tempname, 1, 1);
|
1996-05-16 01:50:39 +04:00
|
|
|
pw_edit(1, tempname);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (stat(tempname, &end))
|
1996-08-09 13:22:11 +04:00
|
|
|
(*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");
|
1996-08-09 13:22:11 +04:00
|
|
|
(*Pw_error)(NULL, 0, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-05-16 01:50:39 +04:00
|
|
|
if (verify(tempname, pw))
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1996-08-09 13:22:11 +04:00
|
|
|
#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
|
2005-02-17 19:53:45 +03:00
|
|
|
display(char *tempname, int fd, struct passwd *pw)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
FILE *fp;
|
1997-10-18 16:48:40 +04:00
|
|
|
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")))
|
1996-08-09 13:22:11 +04:00
|
|
|
(*Pw_error)(tempname, 1, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(void)fprintf(fp,
|
1996-08-09 13:22:11 +04:00
|
|
|
"#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
|
2005-02-17 19:53:45 +03:00
|
|
|
verify(char *tempname, struct passwd *pw)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1995-03-26 08:55:22 +04:00
|
|
|
ENTRY *ep;
|
|
|
|
char *p;
|
|
|
|
struct stat sb;
|
2005-06-02 05:42:11 +04:00
|
|
|
FILE *fp = NULL;
|
2003-02-03 20:45:20 +03:00
|
|
|
int len, fd;
|
1994-08-17 23:54:34 +04:00
|
|
|
static char buf[LINE_MAX];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-02-03 20:45:20 +03:00
|
|
|
if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 ||
|
|
|
|
(fp = fdopen(fd, "r")) == NULL)
|
1996-08-09 13:22:11 +04:00
|
|
|
(*Pw_error)(tempname, 1, 1);
|
2003-02-03 20:45:20 +03:00
|
|
|
if (fstat(fd, &sb))
|
1996-08-09 13:22:11 +04:00
|
|
|
(*Pw_error)(tempname, 1, 1);
|
2003-02-03 20:45:20 +03:00
|
|
|
if (sb.st_size == 0 || sb.st_nlink != 1) {
|
1995-03-26 08:55:22 +04:00
|
|
|
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;
|
|
|
|
}
|
2004-10-30 21:08:12 +04:00
|
|
|
while (isspace((unsigned char)*++p));
|
1993-03-21 12:45:37 +03:00
|
|
|
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)))
|
1997-10-18 16:48:40 +04:00
|
|
|
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
|
|
|
}
|
1995-07-28 11:01:32 +04:00
|
|
|
return (pw_scan(buf, pw, (int *)NULL));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|