#if 0
#if 0
#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1988, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
static char sccsid[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94";
#endif
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/usr.bin/chpass/chpass.c,v 1.27.8.1 2006/09/29 06:13:20 marck Exp $");
#endif
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <err.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef YP
#include <ypclnt.h>
#endif
#ifndef OPEN_DIRECTORY
#include <pw_scan.h>
#include <libutil.h>
#endif
#include "chpass.h"
int master_mode;
#ifdef OPEN_DIRECTORY
#include "open_directory.h"
char *progname = NULL;
CFStringRef DSPath = NULL;
#endif
static void baduser(void);
static void usage(void);
int
main(int argc, char *argv[])
{
enum { NEWSH, LOADENTRY, EDITENTRY, NEWPW, NEWEXP } op;
#ifndef OPEN_DIRECTORY
struct passwd lpw, *old_pw, *pw;
int ch, pfd, tfd;
const char *password;
#else
struct passwd *old_pw, *pw;
int ch, tfd;
char tfn[MAXPATHLEN];
char *tmpdir;
#endif
char *arg = NULL;
uid_t uid;
#ifdef YP
struct ypclnt *ypclnt;
const char *yp_domain = NULL, *yp_host = NULL;
#endif
#ifdef OPEN_DIRECTORY
CFStringRef username = NULL;
CFStringRef authname = NULL;
CFStringRef location = NULL;
progname = strrchr(argv[0], '/');
if (progname) progname++;
else progname = argv[0];
#endif
pw = old_pw = NULL;
op = EDITENTRY;
#ifdef OPEN_DIRECTORY
while ((ch = getopt(argc, argv, "a:s:l:u:")) != -1)
#else
#ifdef YP
while ((ch = getopt(argc, argv, "a:p:s:e:d:h:loy")) != -1)
#else
while ((ch = getopt(argc, argv, "a:p:s:e:")) != -1)
#endif
#endif
switch (ch) {
case 'a':
op = LOADENTRY;
arg = optarg;
break;
case 's':
op = NEWSH;
arg = optarg;
break;
#ifndef OPEN_DIRECTORY
case 'p':
op = NEWPW;
arg = optarg;
break;
case 'e':
op = NEWEXP;
arg = optarg;
break;
#ifdef YP
case 'd':
yp_domain = optarg;
break;
case 'h':
yp_host = optarg;
break;
case 'l':
case 'o':
case 'y':
break;
#endif
#else
case 'l':
location = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingUTF8);
break;
case 'u':
authname = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingUTF8);
break;
#endif
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc > 1)
usage();
uid = getuid();
if (op == EDITENTRY || op == NEWSH || op == NEWPW || op == NEWEXP) {
if (argc == 0) {
if ((pw = getpwuid(uid)) == NULL)
errx(1, "unknown user: uid %lu",
(unsigned long)uid);
} else {
if ((pw = getpwnam(*argv)) == NULL)
errx(1, "unknown user: %s", *argv);
#ifndef OPEN_DIRECTORY
if (uid != 0 && uid != pw->pw_uid)
baduser();
#endif
}
#ifndef OPEN_DIRECTORY
if ((pw = pw_dup(pw)) == NULL ||
(old_pw = pw_dup(pw)) == NULL)
err(1, "pw_dup");
#endif
}
#if OPEN_DIRECTORY
master_mode = (uid == 0);
username = CFStringCreateWithCString(NULL, pw->pw_name, kCFStringEncodingUTF8);
if (strcmp(progname, "chsh") == 0 || op == NEWSH) {
cfprintf(stderr, "Changing shell for %@.\n", username);
} else if (strcmp(progname, "chfn") == 0) {
cfprintf(stderr, "Changing finger information for %@.\n", username);
} else if (strcmp(progname, "chpass") == 0) {
cfprintf(stderr, "Changing account information for %@.\n", username);
}
CFDictionaryRef attrs_orig = NULL;
CFDictionaryRef attrs = NULL;
ODRecordRef rec = odGetUser(location, authname, username, &attrs_orig);
if (!rec || !attrs_orig) exit(1);
#endif
#ifdef YP
if (pw != NULL && (pw->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
master_mode = (ypclnt != NULL &&
ypclnt_connect(ypclnt) != -1 &&
ypclnt_havepasswdd(ypclnt) == 1);
ypclnt_free(ypclnt);
} else
#endif
master_mode = (uid == 0);
if (op == NEWSH) {
if (!arg[0])
usage();
if (p_shell(arg, pw, (ENTRY *)NULL) == -1)
exit(1);
#ifdef OPEN_DIRECTORY
else {
ENTRY* ep;
setrestricted(attrs_orig);
for (ep = list; ep->prompt; ep++) {
if (strncasecmp(ep->prompt, "shell", ep->len) == 0) {
if (!ep->restricted) {
CFStringRef shell = CFStringCreateWithCString(NULL, arg, kCFStringEncodingUTF8);
if (shell) {
attrs = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (attrs) CFDictionarySetValue((CFMutableDictionaryRef)attrs, kODAttributeTypeUserShell, shell);
CFRelease(shell);
}
} else {
warnx("shell is restricted");
exit(1);
}
}
}
}
#endif
}
#ifndef OPEN_DIRECTORY
if (op == NEWEXP) {
if (uid)
baduser();
if (p_expire(arg, pw, (ENTRY *)NULL) == -1)
exit(1);
}
#endif
if (op == LOADENTRY) {
if (uid)
baduser();
#ifdef OPEN_DIRECTORY
warnx("-a is not supported for Open Directory.");
exit(1);
#else
pw = &lpw;
old_pw = NULL;
if (!__pw_scan(arg, pw, _PWSCAN_WARN|_PWSCAN_MASTER))
exit(1);
#endif
}
#ifndef OPEN_DIRECTORY
if (op == NEWPW) {
if (uid)
baduser();
if (strchr(arg, ':'))
errx(1, "invalid format for password");
pw->pw_passwd = arg;
}
#endif
if (op == EDITENTRY) {
#ifdef OPEN_DIRECTORY
setrestricted(attrs_orig);
tmpdir = getenv("TMPDIR");
if (!tmpdir)
tmpdir = P_tmpdir; snprintf(tfn, sizeof(tfn), "%s/%s.XXXXXX", tmpdir, progname);
if ((tfd = mkstemp(tfn)) == -1)
err(1, "%s", tfn);
attrs = (CFMutableDictionaryRef)edit(tfn, attrs_orig);
(void)unlink(tfn);
#else
if (pw_init(NULL, NULL))
err(1, "pw_init()");
if ((tfd = pw_tmp(-1)) == -1) {
pw_fini();
err(1, "pw_tmp()");
}
free(pw);
pw = edit(pw_tempname(), old_pw);
pw_fini();
if (pw == NULL)
err(1, "edit()");
if (pw_equal(old_pw, pw) &&
strcmp(old_pw->pw_passwd, pw->pw_passwd) == 0)
errx(0, "user information unchanged");
#endif
}
#ifndef OPEN_DIRECTORY
if (old_pw && !master_mode) {
password = getpass("Password: ");
if (strcmp(crypt(password, old_pw->pw_passwd),
old_pw->pw_passwd) != 0)
baduser();
} else {
password = "";
}
#endif
#ifdef OPEN_DIRECTORY
odUpdateUser(rec, attrs_orig, attrs);
if (rec) CFRelease(rec);
exit(0);
return 0;
#else
exit(0);
if (old_pw != NULL)
pw->pw_fields |= (old_pw->pw_fields & _PWF_SOURCE);
switch (pw->pw_fields & _PWF_SOURCE) {
#ifdef YP
case _PWF_NIS:
ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
if (ypclnt == NULL ||
ypclnt_connect(ypclnt) == -1 ||
ypclnt_passwd(ypclnt, pw, password) == -1) {
warnx("%s", ypclnt->error);
ypclnt_free(ypclnt);
exit(1);
}
ypclnt_free(ypclnt);
errx(0, "NIS user information updated");
#endif
case 0:
case _PWF_FILES:
if (pw_init(NULL, NULL))
err(1, "pw_init()");
if ((pfd = pw_lock()) == -1) {
pw_fini();
err(1, "pw_lock()");
}
if ((tfd = pw_tmp(-1)) == -1) {
pw_fini();
err(1, "pw_tmp()");
}
if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
pw_fini();
err(1, "pw_copy");
}
if (pw_mkdb(pw->pw_name) == -1) {
pw_fini();
err(1, "pw_mkdb()");
}
pw_fini();
errx(0, "user information updated");
break;
default:
errx(1, "unsupported passwd source");
}
#endif
}
static void
baduser(void)
{
errx(1, "%s", strerror(EACCES));
}
static void
usage(void)
{
(void)fprintf(stderr,
"usage: chpass%s %s [user]\n",
#ifdef OPEN_DIRECTORY
"",
"[-l location] [-u authname] [-s shell]");
#else
#ifdef YP
" [-d domain] [-h host]",
#else
"",
#endif
"[-a list] [-p encpass] [-s shell] [-e mmm dd yy]");
#endif
exit(1);
}