1*84d9c625SLionel Sambuc /* $NetBSD: chpass.c,v 1.35 2011/08/31 16:24:57 plunky Exp $ */
25c007436SBen Gras
35c007436SBen Gras /*-
45c007436SBen Gras * Copyright (c) 1988, 1993, 1994
55c007436SBen Gras * The Regents of the University of California. All rights reserved.
65c007436SBen Gras *
75c007436SBen Gras * Redistribution and use in source and binary forms, with or without
85c007436SBen Gras * modification, are permitted provided that the following conditions
95c007436SBen Gras * are met:
105c007436SBen Gras * 1. Redistributions of source code must retain the above copyright
115c007436SBen Gras * notice, this list of conditions and the following disclaimer.
125c007436SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
135c007436SBen Gras * notice, this list of conditions and the following disclaimer in the
145c007436SBen Gras * documentation and/or other materials provided with the distribution.
155c007436SBen Gras * 3. Neither the name of the University nor the names of its contributors
165c007436SBen Gras * may be used to endorse or promote products derived from this software
175c007436SBen Gras * without specific prior written permission.
185c007436SBen Gras *
195c007436SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205c007436SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215c007436SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225c007436SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235c007436SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245c007436SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255c007436SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265c007436SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275c007436SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285c007436SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295c007436SBen Gras * SUCH DAMAGE.
305c007436SBen Gras */
315c007436SBen Gras
325c007436SBen Gras #include <sys/cdefs.h>
335c007436SBen Gras #ifndef lint
345c007436SBen Gras __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
355c007436SBen Gras The Regents of the University of California. All rights reserved.");
365c007436SBen Gras #endif /* not lint */
375c007436SBen Gras
385c007436SBen Gras #ifndef lint
395c007436SBen Gras #if 0
405c007436SBen Gras static char sccsid[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94";
415c007436SBen Gras #else
42*84d9c625SLionel Sambuc __RCSID("$NetBSD: chpass.c,v 1.35 2011/08/31 16:24:57 plunky Exp $");
435c007436SBen Gras #endif
445c007436SBen Gras #endif /* not lint */
455c007436SBen Gras
465c007436SBen Gras #include <sys/param.h>
475c007436SBen Gras #include <sys/stat.h>
485c007436SBen Gras #include <sys/time.h>
495c007436SBen Gras #include <sys/resource.h>
505c007436SBen Gras
515c007436SBen Gras #include <ctype.h>
525c007436SBen Gras #include <err.h>
535c007436SBen Gras #include <errno.h>
545c007436SBen Gras #include <fcntl.h>
555c007436SBen Gras #include <pwd.h>
565c007436SBen Gras #include <stdio.h>
575c007436SBen Gras #include <stdlib.h>
585c007436SBen Gras #include <string.h>
595c007436SBen Gras #include <unistd.h>
605c007436SBen Gras #include <util.h>
615c007436SBen Gras #include <libgen.h>
625c007436SBen Gras
635c007436SBen Gras #include "chpass.h"
645c007436SBen Gras #include "pathnames.h"
655c007436SBen Gras
665c007436SBen Gras static char tempname[] = "/tmp/pw.XXXXXX";
675c007436SBen Gras uid_t uid;
685c007436SBen Gras int use_yp;
695c007436SBen Gras
705c007436SBen Gras void (*Pw_error)(const char *, int, int);
715c007436SBen Gras
725c007436SBen Gras #ifdef YP
735c007436SBen Gras extern int _yp_check(char **); /* buried deep inside libc */
745c007436SBen Gras #endif
755c007436SBen Gras
76*84d9c625SLionel Sambuc __dead static void baduser(void);
77*84d9c625SLionel Sambuc static void cleanup(void);
78*84d9c625SLionel Sambuc __dead static void usage(void);
795c007436SBen Gras
805c007436SBen Gras int
main(int argc,char ** argv)815c007436SBen Gras main(int argc, char **argv)
825c007436SBen Gras {
835c007436SBen Gras enum { NEWSH, LOADENTRY, EDITENTRY } op;
845c007436SBen Gras struct passwd *pw, lpw, old_pw;
855c007436SBen Gras int ch, dfd, pfd, tfd;
865c007436SBen Gras #ifdef YP
875c007436SBen Gras int yflag = 0;
885c007436SBen Gras #endif
895c007436SBen Gras char *arg, *username = NULL;
905c007436SBen Gras
915c007436SBen Gras #ifdef __GNUC__
925c007436SBen Gras pw = NULL; /* XXX gcc -Wuninitialized */
935c007436SBen Gras arg = NULL;
945c007436SBen Gras #endif
955c007436SBen Gras #ifdef YP
965c007436SBen Gras use_yp = _yp_check(NULL);
975c007436SBen Gras #endif
985c007436SBen Gras
995c007436SBen Gras op = EDITENTRY;
1005c007436SBen Gras while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
1015c007436SBen Gras switch (ch) {
1025c007436SBen Gras case 'a':
1035c007436SBen Gras op = LOADENTRY;
1045c007436SBen Gras arg = optarg;
1055c007436SBen Gras break;
1065c007436SBen Gras case 's':
1075c007436SBen Gras op = NEWSH;
1085c007436SBen Gras arg = optarg;
1095c007436SBen Gras break;
1105c007436SBen Gras case 'l':
1115c007436SBen Gras use_yp = 0;
1125c007436SBen Gras break;
1135c007436SBen Gras case 'y':
1145c007436SBen Gras #ifdef YP
1155c007436SBen Gras if (!use_yp)
1165c007436SBen Gras errx(1, "YP not in use.");
1175c007436SBen Gras yflag = 1;
1185c007436SBen Gras #else
1195c007436SBen Gras errx(1, "YP support not compiled in.");
1205c007436SBen Gras #endif
1215c007436SBen Gras break;
1225c007436SBen Gras default:
1235c007436SBen Gras usage();
1245c007436SBen Gras }
1255c007436SBen Gras argc -= optind;
1265c007436SBen Gras argv += optind;
1275c007436SBen Gras
1285c007436SBen Gras uid = getuid();
1295c007436SBen Gras switch (argc) {
1305c007436SBen Gras case 0:
1315c007436SBen Gras /* nothing */
1325c007436SBen Gras break;
1335c007436SBen Gras
1345c007436SBen Gras case 1:
1355c007436SBen Gras username = argv[0];
1365c007436SBen Gras break;
1375c007436SBen Gras
1385c007436SBen Gras default:
1395c007436SBen Gras usage();
1405c007436SBen Gras }
1415c007436SBen Gras
1425c007436SBen Gras #ifdef YP
1435c007436SBen Gras /*
1445c007436SBen Gras * We need to determine if we _really_ want to use YP.
1455c007436SBen Gras * If we defaulted to YP (i.e. were not given the -y flag),
1465c007436SBen Gras * and the master is not running rpc.yppasswdd, we check
1475c007436SBen Gras * to see if the user exists in the local passwd database.
1485c007436SBen Gras * If so, we use it, otherwise we error out.
1495c007436SBen Gras */
1505c007436SBen Gras if (use_yp && yflag == 0) {
1515c007436SBen Gras if (check_yppasswdd()) {
1525c007436SBen Gras /*
1535c007436SBen Gras * We weren't able to contact rpc.yppasswdd.
1545c007436SBen Gras * Check to see if we're in the local
1555c007436SBen Gras * password database. If we are, use it.
1565c007436SBen Gras */
1575c007436SBen Gras if (username != NULL)
1585c007436SBen Gras pw = getpwnam(username);
1595c007436SBen Gras else
1605c007436SBen Gras pw = getpwuid(uid);
1615c007436SBen Gras if (pw != NULL)
1625c007436SBen Gras use_yp = 0;
1635c007436SBen Gras else {
1645c007436SBen Gras warnx("master YP server not running yppasswd"
1655c007436SBen Gras " daemon.");
1665c007436SBen Gras errx(1, "Can't change password.");
1675c007436SBen Gras }
1685c007436SBen Gras }
1695c007436SBen Gras }
1705c007436SBen Gras #endif
1715c007436SBen Gras
1725c007436SBen Gras #ifdef YP
1735c007436SBen Gras if (use_yp)
1745c007436SBen Gras Pw_error = yppw_error;
1755c007436SBen Gras else
1765c007436SBen Gras #endif
1775c007436SBen Gras Pw_error = pw_error;
1785c007436SBen Gras
1795c007436SBen Gras #ifdef YP
1805c007436SBen Gras if (op == LOADENTRY && use_yp)
1815c007436SBen Gras errx(1, "cannot load entry using YP.\n"
1825c007436SBen Gras "\tUse the -l flag to load local.");
1835c007436SBen Gras #endif
1845c007436SBen Gras
1855c007436SBen Gras if (op == EDITENTRY || op == NEWSH) {
1865c007436SBen Gras if (username != NULL) {
1875c007436SBen Gras pw = getpwnam(username);
1885c007436SBen Gras if (pw == NULL)
1895c007436SBen Gras errx(1, "unknown user: %s", username);
1905c007436SBen Gras if (uid && uid != pw->pw_uid)
1915c007436SBen Gras baduser();
1925c007436SBen Gras } else {
1935c007436SBen Gras pw = getpwuid(uid);
1945c007436SBen Gras if (pw == NULL)
1955c007436SBen Gras errx(1, "unknown user: uid %u", uid);
1965c007436SBen Gras }
1975c007436SBen Gras
1985c007436SBen Gras /* Make a copy for later verification */
1995c007436SBen Gras old_pw = *pw;
2005c007436SBen Gras old_pw.pw_gecos = strdup(old_pw.pw_gecos);
2015c007436SBen Gras if (!old_pw.pw_gecos) {
2025c007436SBen Gras err(1, "strdup");
2035c007436SBen Gras /*NOTREACHED*/
2045c007436SBen Gras }
2055c007436SBen Gras }
2065c007436SBen Gras
2075c007436SBen Gras if (op == NEWSH) {
2085c007436SBen Gras /* protect p_shell -- it thinks NULL is /bin/sh */
2095c007436SBen Gras if (!arg[0])
2105c007436SBen Gras usage();
2115c007436SBen Gras if (p_shell(arg, pw, NULL))
2125c007436SBen Gras (*Pw_error)(NULL, 0, 1);
2135c007436SBen Gras }
2145c007436SBen Gras
2155c007436SBen Gras if (op == LOADENTRY) {
2165c007436SBen Gras if (uid)
2175c007436SBen Gras baduser();
2185c007436SBen Gras pw = &lpw;
2195c007436SBen Gras if (!pw_scan(arg, pw, NULL))
2205c007436SBen Gras exit(1);
2215c007436SBen Gras }
2225c007436SBen Gras
2235c007436SBen Gras /* Edit the user passwd information if requested. */
2245c007436SBen Gras if (op == EDITENTRY) {
2255c007436SBen Gras struct stat sb;
2265c007436SBen Gras
2275c007436SBen Gras dfd = mkstemp(tempname);
2285c007436SBen Gras if (dfd < 0 || fcntl(dfd, F_SETFD, 1) < 0)
2295c007436SBen Gras (*Pw_error)(tempname, 1, 1);
2305c007436SBen Gras if (atexit(cleanup)) {
2315c007436SBen Gras cleanup();
2325c007436SBen Gras errx(1, "couldn't register cleanup");
2335c007436SBen Gras }
2345c007436SBen Gras if (stat(dirname(tempname), &sb) == -1)
2355c007436SBen Gras err(1, "couldn't stat `%s'", dirname(tempname));
2365c007436SBen Gras if (!(sb.st_mode & S_ISTXT))
2375c007436SBen Gras errx(1, "temporary directory `%s' is not sticky",
2385c007436SBen Gras dirname(tempname));
2395c007436SBen Gras
2405c007436SBen Gras display(tempname, dfd, pw);
2415c007436SBen Gras edit(tempname, pw);
2425c007436SBen Gras }
2435c007436SBen Gras
2445c007436SBen Gras #ifdef YP
2455c007436SBen Gras if (use_yp) {
2465c007436SBen Gras if (pw_yp(pw, uid))
247*84d9c625SLionel Sambuc yppw_error(NULL, 0, 1);
2485c007436SBen Gras else
2495c007436SBen Gras exit(0);
2505c007436SBen Gras /* Will not exit from this if. */
2515c007436SBen Gras }
2525c007436SBen Gras #endif /* YP */
2535c007436SBen Gras
2545c007436SBen Gras
2555c007436SBen Gras /*
2565c007436SBen Gras * Get the passwd lock file and open the passwd file for
2575c007436SBen Gras * reading.
2585c007436SBen Gras */
2595c007436SBen Gras pw_init();
2605c007436SBen Gras tfd = pw_lock(0);
2615c007436SBen Gras if (tfd < 0) {
2625c007436SBen Gras if (errno != EEXIST)
2635c007436SBen Gras err(1, "%s", _PATH_MASTERPASSWD_LOCK);
2645c007436SBen Gras warnx("The passwd file is busy, waiting...");
2655c007436SBen Gras tfd = pw_lock(10);
2665c007436SBen Gras if (tfd < 0) {
2675c007436SBen Gras if (errno != EEXIST)
2685c007436SBen Gras err(1, "%s", _PATH_MASTERPASSWD_LOCK);
2695c007436SBen Gras errx(1, "The passwd file is still busy, "
2705c007436SBen Gras "try again later.");
2715c007436SBen Gras }
2725c007436SBen Gras }
2735c007436SBen Gras if (fcntl(tfd, F_SETFD, 1) < 0)
2745c007436SBen Gras pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
2755c007436SBen Gras
2765c007436SBen Gras pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
2775c007436SBen Gras if (pfd < 0 || fcntl(pfd, F_SETFD, 1) < 0)
2785c007436SBen Gras pw_error(_PATH_MASTERPASSWD, 1, 1);
2795c007436SBen Gras
2805c007436SBen Gras /* Copy the passwd file to the lock file, updating pw. */
2815c007436SBen Gras pw_copy(pfd, tfd, pw, (op == LOADENTRY) ? NULL : &old_pw);
2825c007436SBen Gras
2835c007436SBen Gras close(pfd);
2845c007436SBen Gras close(tfd);
2855c007436SBen Gras
2865c007436SBen Gras /* Now finish the passwd file update. */
2875c007436SBen Gras if (pw_mkdb(username, 0) < 0)
2885c007436SBen Gras pw_error(NULL, 0, 1);
2895c007436SBen Gras
2905c007436SBen Gras exit(0);
2915c007436SBen Gras }
2925c007436SBen Gras
293*84d9c625SLionel Sambuc static void
baduser(void)2945c007436SBen Gras baduser(void)
2955c007436SBen Gras {
2965c007436SBen Gras
2975c007436SBen Gras errx(1, "%s", strerror(EACCES));
2985c007436SBen Gras }
2995c007436SBen Gras
300*84d9c625SLionel Sambuc static void
usage(void)3015c007436SBen Gras usage(void)
3025c007436SBen Gras {
3035c007436SBen Gras
3045c007436SBen Gras (void)fprintf(stderr,
3055c007436SBen Gras "usage: %s [-a list] [-s shell] [-l] [user]\n"
3065c007436SBen Gras " %s [-a list] [-s shell] [-y] [user]\n",
3075c007436SBen Gras getprogname(), getprogname());
3085c007436SBen Gras exit(1);
3095c007436SBen Gras }
3105c007436SBen Gras
311*84d9c625SLionel Sambuc static void
cleanup(void)3125c007436SBen Gras cleanup(void)
3135c007436SBen Gras {
3145c007436SBen Gras
3155c007436SBen Gras (void)unlink(tempname);
3165c007436SBen Gras }
317