1996-08-09 13:22:11 +04:00
|
|
|
/* $NetBSD: chpass.c,v 1.9 1996/08/09 09:22:11 thorpej 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) 1988, 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-26 08:55:22 +04:00
|
|
|
static char copyright[] =
|
|
|
|
"@(#) Copyright (c) 1988, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-26 08:55:22 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94";
|
|
|
|
#else
|
1996-08-09 13:22:11 +04:00
|
|
|
static char rcsid[] = "$NetBSD: chpass.c,v 1.9 1996/08/09 09:22:11 thorpej 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>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
1995-03-26 08:55:22 +04:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
1995-03-26 08:55:22 +04:00
|
|
|
#include <stdlib.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <string.h>
|
1995-03-26 08:55:22 +04:00
|
|
|
#include <unistd.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"
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
1996-08-09 13:22:11 +04:00
|
|
|
extern char *__progname; /* from crt0.o */
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
char *tempname;
|
|
|
|
uid_t uid;
|
1996-08-09 13:22:11 +04:00
|
|
|
int use_yp;
|
|
|
|
|
|
|
|
void (*Pw_error) __P((const char *, int, int));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-06-11 04:34:38 +04:00
|
|
|
#ifdef YP
|
|
|
|
extern struct passwd *ypgetpwnam(), *ypgetpwuid();
|
1996-08-09 13:22:11 +04:00
|
|
|
extern int _yp_check __P((char **)); /* buried deep inside libc */
|
1993-06-11 04:34:38 +04:00
|
|
|
#endif
|
|
|
|
|
1995-03-26 08:55:22 +04:00
|
|
|
void baduser __P((void));
|
|
|
|
void usage __P((void));
|
|
|
|
|
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
1995-03-26 08:55:22 +04:00
|
|
|
enum { NEWSH, LOADENTRY, EDITENTRY } op;
|
|
|
|
struct passwd *pw, lpw;
|
1996-05-16 01:50:39 +04:00
|
|
|
int ch, pfd, tfd, dfd;
|
|
|
|
char *arg, tempname[] = "/etc/pw.XXXXXX";
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-06-11 04:34:38 +04:00
|
|
|
#ifdef YP
|
1994-08-17 23:54:39 +04:00
|
|
|
use_yp = _yp_check(NULL);
|
1993-06-11 04:34:38 +04:00
|
|
|
#endif
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
op = EDITENTRY;
|
1993-06-11 04:34:38 +04:00
|
|
|
while ((ch = getopt(argc, argv, "a:s:ly")) != EOF)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch(ch) {
|
|
|
|
case 'a':
|
|
|
|
op = LOADENTRY;
|
|
|
|
arg = optarg;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
op = NEWSH;
|
|
|
|
arg = optarg;
|
|
|
|
break;
|
1994-08-17 23:54:39 +04:00
|
|
|
case 'l':
|
|
|
|
use_yp = 0;
|
|
|
|
break;
|
|
|
|
case 'y':
|
1996-08-09 13:22:11 +04:00
|
|
|
#ifdef YP
|
|
|
|
if (!use_yp)
|
|
|
|
errx(1, "YP not in use.");
|
|
|
|
#else
|
|
|
|
errx(1, "YP support not compiled in.");
|
1993-06-11 04:34:38 +04:00
|
|
|
#endif
|
1996-08-09 13:22:11 +04:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1996-08-09 13:22:11 +04:00
|
|
|
#ifdef YP
|
|
|
|
if (use_yp)
|
|
|
|
Pw_error = yppw_error;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
Pw_error = pw_error;
|
|
|
|
|
1993-06-11 04:34:38 +04:00
|
|
|
#ifdef YP
|
1995-03-26 08:55:22 +04:00
|
|
|
if (op == LOADENTRY && use_yp)
|
1996-08-09 13:22:11 +04:00
|
|
|
errx(1, "cannot load entry using YP.\n\tUse the -l flag to load local.");
|
1993-06-11 04:34:38 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
uid = getuid();
|
|
|
|
|
|
|
|
if (op == EDITENTRY || op == NEWSH)
|
|
|
|
switch(argc) {
|
|
|
|
case 0:
|
1993-06-11 04:34:38 +04:00
|
|
|
#ifdef YP
|
1996-08-09 13:22:11 +04:00
|
|
|
if (use_yp)
|
1994-08-17 23:54:39 +04:00
|
|
|
pw = ypgetpwuid(uid);
|
1996-08-09 13:22:11 +04:00
|
|
|
else
|
1993-06-11 04:34:38 +04:00
|
|
|
#endif /* YP */
|
1996-08-09 13:22:11 +04:00
|
|
|
pw = getpwuid(uid);
|
1995-03-26 08:55:22 +04:00
|
|
|
if (!pw)
|
|
|
|
errx(1, "unknown user: uid %u\n", uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
1993-06-11 04:34:38 +04:00
|
|
|
#ifdef YP
|
1996-08-09 13:22:11 +04:00
|
|
|
if (use_yp)
|
1994-08-17 23:54:39 +04:00
|
|
|
pw = ypgetpwnam(*argv);
|
1996-08-09 13:22:11 +04:00
|
|
|
else
|
1993-06-11 04:34:38 +04:00
|
|
|
#endif /* YP */
|
1996-08-09 13:22:11 +04:00
|
|
|
pw = getpwnam(*argv);
|
1995-03-26 08:55:22 +04:00
|
|
|
if (!pw)
|
|
|
|
errx(1, "unknown user: %s", *argv);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (uid && uid != pw->pw_uid)
|
|
|
|
baduser();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op == NEWSH) {
|
|
|
|
/* protect p_shell -- it thinks NULL is /bin/sh */
|
|
|
|
if (!arg[0])
|
|
|
|
usage();
|
|
|
|
if (p_shell(arg, pw, (ENTRY *)NULL))
|
1996-08-09 13:22:11 +04:00
|
|
|
(*Pw_error)((char *)NULL, 0, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (op == LOADENTRY) {
|
|
|
|
if (uid)
|
|
|
|
baduser();
|
|
|
|
pw = &lpw;
|
1995-07-28 11:01:32 +04:00
|
|
|
if (!pw_scan(arg, pw, (int *)NULL))
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1996-08-09 13:22:11 +04:00
|
|
|
if (!use_yp) {
|
|
|
|
/*
|
|
|
|
* Get the passwd lock file and open the passwd file for
|
|
|
|
* reading.
|
|
|
|
*/
|
|
|
|
pw_init();
|
|
|
|
tfd = pw_lock(0);
|
|
|
|
if (tfd < 0)
|
|
|
|
errx(1, "the passwd file is busy.");
|
|
|
|
pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
|
|
|
|
if (pfd < 0)
|
|
|
|
pw_error(_PATH_MASTERPASSWD, 1, 1);
|
|
|
|
}
|
1996-05-16 01:50:39 +04:00
|
|
|
|
|
|
|
/* Edit the user passwd information if requested. */
|
1993-03-21 12:45:37 +03:00
|
|
|
if (op == EDITENTRY) {
|
1996-05-16 01:50:39 +04:00
|
|
|
dfd = mkstemp(tempname);
|
1996-08-09 13:22:11 +04:00
|
|
|
if (dfd < 0) {
|
|
|
|
(*Pw_error)(tempname, 1, 1);
|
|
|
|
}
|
1996-05-16 01:50:39 +04:00
|
|
|
display(tempname, dfd, pw);
|
|
|
|
edit(tempname, pw);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)unlink(tempname);
|
|
|
|
}
|
1996-05-16 01:50:39 +04:00
|
|
|
|
1993-06-11 04:34:38 +04:00
|
|
|
#ifdef YP
|
1994-08-17 23:54:39 +04:00
|
|
|
if (use_yp) {
|
|
|
|
if (pw_yp(pw, uid))
|
1996-08-09 13:22:11 +04:00
|
|
|
yppw_error((char *)NULL, 0, 1);
|
1994-08-17 23:54:39 +04:00
|
|
|
else
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
else
|
1993-06-11 04:34:38 +04:00
|
|
|
#endif /* YP */
|
1996-05-16 01:50:39 +04:00
|
|
|
|
|
|
|
/* Copy the passwd file to the lock file, updating pw. */
|
1993-03-21 12:45:37 +03:00
|
|
|
pw_copy(pfd, tfd, pw);
|
|
|
|
|
1996-05-16 01:50:39 +04:00
|
|
|
/* Now finish the passwd file update. */
|
|
|
|
if (pw_mkdb() < 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
pw_error((char *)NULL, 0, 1);
|
1993-06-11 04:34:38 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1995-03-26 08:55:22 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
baduser()
|
|
|
|
{
|
1995-03-26 08:55:22 +04:00
|
|
|
|
|
|
|
errx(1, "%s", strerror(EACCES));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1995-03-26 08:55:22 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
usage()
|
|
|
|
{
|
1995-03-26 08:55:22 +04:00
|
|
|
|
1993-06-11 04:34:38 +04:00
|
|
|
#ifdef YP
|
|
|
|
(void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [-l]%s [user]\n", use_yp?" [-y]":"");
|
|
|
|
#else
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
|
1993-06-11 04:34:38 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|