1*dba3562dSLionel Sambuc /* $NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs Exp $ */
20c3983b2SBen Gras
30c3983b2SBen Gras /*
40c3983b2SBen Gras * Copyright (c) 1987, 1993, 1994, 1995
50c3983b2SBen Gras * The Regents of the University of California. All rights reserved.
60c3983b2SBen Gras *
70c3983b2SBen Gras * Redistribution and use in source and binary forms, with or without
80c3983b2SBen Gras * modification, are permitted provided that the following conditions
90c3983b2SBen Gras * are met:
100c3983b2SBen Gras * 1. Redistributions of source code must retain the above copyright
110c3983b2SBen Gras * notice, this list of conditions and the following disclaimer.
120c3983b2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
130c3983b2SBen Gras * notice, this list of conditions and the following disclaimer in the
140c3983b2SBen Gras * documentation and/or other materials provided with the distribution.
150c3983b2SBen Gras * 3. Neither the name of the University nor the names of its contributors
160c3983b2SBen Gras * may be used to endorse or promote products derived from this software
170c3983b2SBen Gras * without specific prior written permission.
180c3983b2SBen Gras *
190c3983b2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
200c3983b2SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
210c3983b2SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
220c3983b2SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
230c3983b2SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
240c3983b2SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
250c3983b2SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260c3983b2SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
270c3983b2SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
280c3983b2SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
290c3983b2SBen Gras * SUCH DAMAGE.
300c3983b2SBen Gras */
310c3983b2SBen Gras
320c3983b2SBen Gras #include <sys/cdefs.h>
330c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*dba3562dSLionel Sambuc __RCSID("$NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs Exp $");
350c3983b2SBen Gras #endif /* LIBC_SCCS and not lint */
360c3983b2SBen Gras
370c3983b2SBen Gras #include <sys/types.h>
380c3983b2SBen Gras #include <sys/param.h>
390c3983b2SBen Gras #include <sys/stat.h>
400c3983b2SBen Gras #include <sys/time.h>
410c3983b2SBen Gras #include <sys/resource.h>
420c3983b2SBen Gras #include <sys/wait.h>
430c3983b2SBen Gras
440c3983b2SBen Gras #include <assert.h>
450c3983b2SBen Gras #include <ctype.h>
460c3983b2SBen Gras #include <err.h>
470c3983b2SBen Gras #include <errno.h>
480c3983b2SBen Gras #include <fcntl.h>
490c3983b2SBen Gras #include <limits.h>
500c3983b2SBen Gras #include <paths.h>
510c3983b2SBen Gras #include <pwd.h>
520c3983b2SBen Gras #include <grp.h>
530c3983b2SBen Gras #include <signal.h>
540c3983b2SBen Gras #include <stdio.h>
550c3983b2SBen Gras #include <stdlib.h>
560c3983b2SBen Gras #include <string.h>
570c3983b2SBen Gras #include <unistd.h>
580c3983b2SBen Gras #include <util.h>
590c3983b2SBen Gras
600c3983b2SBen Gras static const char *pw_filename(const char *filename);
610c3983b2SBen Gras static void pw_cont(int sig);
620c3983b2SBen Gras static const char * pw_equal(char *buf, struct passwd *old_pw);
630c3983b2SBen Gras static const char *pw_default(const char *option);
640c3983b2SBen Gras static int read_line(FILE *fp, char *line, int max);
650c3983b2SBen Gras static void trim_whitespace(char *line);
660c3983b2SBen Gras
670c3983b2SBen Gras static char pw_prefix[MAXPATHLEN];
680c3983b2SBen Gras
690c3983b2SBen Gras const char *
pw_getprefix(void)700c3983b2SBen Gras pw_getprefix(void)
710c3983b2SBen Gras {
720c3983b2SBen Gras
730c3983b2SBen Gras return(pw_prefix);
740c3983b2SBen Gras }
750c3983b2SBen Gras
760c3983b2SBen Gras int
pw_setprefix(const char * new_prefix)770c3983b2SBen Gras pw_setprefix(const char *new_prefix)
780c3983b2SBen Gras {
790c3983b2SBen Gras size_t length;
800c3983b2SBen Gras
810c3983b2SBen Gras _DIAGASSERT(new_prefix != NULL);
820c3983b2SBen Gras
830c3983b2SBen Gras length = strlen(new_prefix);
840c3983b2SBen Gras if (length < sizeof(pw_prefix)) {
850c3983b2SBen Gras (void)strcpy(pw_prefix, new_prefix);
860c3983b2SBen Gras while (length > 0 && pw_prefix[length - 1] == '/')
870c3983b2SBen Gras pw_prefix[--length] = '\0';
880c3983b2SBen Gras return(0);
890c3983b2SBen Gras }
900c3983b2SBen Gras errno = ENAMETOOLONG;
910c3983b2SBen Gras return(-1);
920c3983b2SBen Gras }
930c3983b2SBen Gras
940c3983b2SBen Gras static const char *
pw_filename(const char * filename)950c3983b2SBen Gras pw_filename(const char *filename)
960c3983b2SBen Gras {
970c3983b2SBen Gras static char newfilename[MAXPATHLEN];
980c3983b2SBen Gras
990c3983b2SBen Gras _DIAGASSERT(filename != NULL);
1000c3983b2SBen Gras
1010c3983b2SBen Gras if (pw_prefix[0] == '\0')
1020c3983b2SBen Gras return filename;
1030c3983b2SBen Gras
1040c3983b2SBen Gras if (strlen(pw_prefix) + strlen(filename) < sizeof(newfilename))
1050c3983b2SBen Gras return strcat(strcpy(newfilename, pw_prefix), filename);
1060c3983b2SBen Gras
1070c3983b2SBen Gras errno = ENAMETOOLONG;
1080c3983b2SBen Gras return(NULL);
1090c3983b2SBen Gras }
1100c3983b2SBen Gras
1110c3983b2SBen Gras int
pw_lock(int retries)1120c3983b2SBen Gras pw_lock(int retries)
1130c3983b2SBen Gras {
1140c3983b2SBen Gras const char *filename;
1150c3983b2SBen Gras int i, fd;
1160c3983b2SBen Gras mode_t old_mode;
1170c3983b2SBen Gras int oerrno;
1180c3983b2SBen Gras
1190c3983b2SBen Gras /* Acquire the lock file. */
1200c3983b2SBen Gras filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
1210c3983b2SBen Gras if (filename == NULL)
1220c3983b2SBen Gras return(-1);
1230c3983b2SBen Gras old_mode = umask(0);
1240c3983b2SBen Gras fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 0600);
1250c3983b2SBen Gras for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
1260c3983b2SBen Gras sleep(1);
1270c3983b2SBen Gras fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
1280c3983b2SBen Gras 0600);
1290c3983b2SBen Gras }
1300c3983b2SBen Gras oerrno = errno;
1310c3983b2SBen Gras (void)umask(old_mode);
1320c3983b2SBen Gras errno = oerrno;
1330c3983b2SBen Gras return(fd);
1340c3983b2SBen Gras }
1350c3983b2SBen Gras
1360c3983b2SBen Gras int
pw_mkdb(const char * username,int secureonly)137*dba3562dSLionel Sambuc pw_mkdb(const char *username, int secureonly)
1380c3983b2SBen Gras {
1390c3983b2SBen Gras const char *args[9];
1400c3983b2SBen Gras int pstat, i;
1410c3983b2SBen Gras pid_t pid;
1420c3983b2SBen Gras
1430c3983b2SBen Gras pid = vfork();
1440c3983b2SBen Gras if (pid == -1)
1450c3983b2SBen Gras return -1;
1460c3983b2SBen Gras
1470c3983b2SBen Gras if (pid == 0) {
1480c3983b2SBen Gras args[0] = "pwd_mkdb";
1490c3983b2SBen Gras args[1] = "-d";
1500c3983b2SBen Gras args[2] = pw_prefix;
1510c3983b2SBen Gras args[3] = "-pl";
1520c3983b2SBen Gras i = 4;
1530c3983b2SBen Gras
1540c3983b2SBen Gras if (secureonly)
1550c3983b2SBen Gras args[i++] = "-s";
1560c3983b2SBen Gras if (username != NULL) {
1570c3983b2SBen Gras args[i++] = "-u";
1580c3983b2SBen Gras args[i++] = username;
1590c3983b2SBen Gras }
1600c3983b2SBen Gras
1610c3983b2SBen Gras args[i++] = pw_filename(_PATH_MASTERPASSWD_LOCK);
1620c3983b2SBen Gras args[i] = NULL;
1630c3983b2SBen Gras execv(_PATH_PWD_MKDB, (char * const *)__UNCONST(args));
1640c3983b2SBen Gras _exit(1);
1650c3983b2SBen Gras }
1660c3983b2SBen Gras pid = waitpid(pid, &pstat, 0);
1670c3983b2SBen Gras if (pid == -1) {
1680c3983b2SBen Gras warn("error waiting for pid %lu", (unsigned long)pid);
1690c3983b2SBen Gras return -1;
1700c3983b2SBen Gras }
1710c3983b2SBen Gras if (WIFEXITED(pstat)) {
1720c3983b2SBen Gras if (WEXITSTATUS(pstat) != 0) {
173*dba3562dSLionel Sambuc warnx("pwd_mkdb exited with status %d",
1740c3983b2SBen Gras WEXITSTATUS(pstat));
1750c3983b2SBen Gras return -1;
1760c3983b2SBen Gras }
1770c3983b2SBen Gras } else if (WIFSIGNALED(pstat)) {
1780c3983b2SBen Gras warnx("pwd_mkdb exited with signal %d", WTERMSIG(pstat));
1790c3983b2SBen Gras return -1;
1800c3983b2SBen Gras }
1810c3983b2SBen Gras return 0;
1820c3983b2SBen Gras }
1830c3983b2SBen Gras
1840c3983b2SBen Gras int
pw_abort(void)1850c3983b2SBen Gras pw_abort(void)
1860c3983b2SBen Gras {
1870c3983b2SBen Gras const char *filename;
1880c3983b2SBen Gras
1890c3983b2SBen Gras filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
1900c3983b2SBen Gras return((filename == NULL) ? -1 : unlink(filename));
1910c3983b2SBen Gras }
1920c3983b2SBen Gras
1930c3983b2SBen Gras /* Everything below this point is intended for the convenience of programs
1940c3983b2SBen Gras * which allow a user to interactively edit the passwd file. Errors in the
1950c3983b2SBen Gras * routines below will cause the process to abort. */
1960c3983b2SBen Gras
1970c3983b2SBen Gras static pid_t editpid = -1;
1980c3983b2SBen Gras
1990c3983b2SBen Gras static void
pw_cont(int sig)2000c3983b2SBen Gras pw_cont(int sig)
2010c3983b2SBen Gras {
2020c3983b2SBen Gras
2030c3983b2SBen Gras if (editpid != -1)
2040c3983b2SBen Gras kill(editpid, sig);
2050c3983b2SBen Gras }
2060c3983b2SBen Gras
2070c3983b2SBen Gras void
pw_init(void)2080c3983b2SBen Gras pw_init(void)
2090c3983b2SBen Gras {
2100c3983b2SBen Gras struct rlimit rlim;
2110c3983b2SBen Gras
2120c3983b2SBen Gras /* Unlimited resource limits. */
2130c3983b2SBen Gras rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
2140c3983b2SBen Gras (void)setrlimit(RLIMIT_CPU, &rlim);
2150c3983b2SBen Gras (void)setrlimit(RLIMIT_FSIZE, &rlim);
2160c3983b2SBen Gras (void)setrlimit(RLIMIT_STACK, &rlim);
2170c3983b2SBen Gras (void)setrlimit(RLIMIT_DATA, &rlim);
2180c3983b2SBen Gras (void)setrlimit(RLIMIT_RSS, &rlim);
2190c3983b2SBen Gras
2200c3983b2SBen Gras /* Don't drop core (not really necessary, but GP's). */
2210c3983b2SBen Gras rlim.rlim_cur = rlim.rlim_max = 0;
2220c3983b2SBen Gras (void)setrlimit(RLIMIT_CORE, &rlim);
2230c3983b2SBen Gras
2240c3983b2SBen Gras /* Turn off signals. */
2250c3983b2SBen Gras (void)signal(SIGALRM, SIG_IGN);
2260c3983b2SBen Gras (void)signal(SIGHUP, SIG_IGN);
2270c3983b2SBen Gras (void)signal(SIGINT, SIG_IGN);
2280c3983b2SBen Gras (void)signal(SIGPIPE, SIG_IGN);
2290c3983b2SBen Gras (void)signal(SIGQUIT, SIG_IGN);
2300c3983b2SBen Gras (void)signal(SIGTERM, SIG_IGN);
2310c3983b2SBen Gras (void)signal(SIGCONT, pw_cont);
2320c3983b2SBen Gras }
2330c3983b2SBen Gras
2340c3983b2SBen Gras void
pw_edit(int notsetuid,const char * filename)2350c3983b2SBen Gras pw_edit(int notsetuid, const char *filename)
2360c3983b2SBen Gras {
2370c3983b2SBen Gras int pstat;
2380c3983b2SBen Gras char *p;
2390c3983b2SBen Gras const char * volatile editor;
2400c3983b2SBen Gras const char *argp[] = { "sh", "-c", NULL, NULL };
2410c3983b2SBen Gras
2420c3983b2SBen Gras if (filename == NULL)
2430c3983b2SBen Gras filename = _PATH_MASTERPASSWD_LOCK;
2440c3983b2SBen Gras
2450c3983b2SBen Gras filename = pw_filename(filename);
2460c3983b2SBen Gras if (filename == NULL)
2470c3983b2SBen Gras return;
2480c3983b2SBen Gras
2490c3983b2SBen Gras if ((editor = getenv("EDITOR")) == NULL)
2500c3983b2SBen Gras editor = _PATH_VI;
2510c3983b2SBen Gras
2520c3983b2SBen Gras p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
2530c3983b2SBen Gras if (p == NULL)
2540c3983b2SBen Gras return;
2550c3983b2SBen Gras
2560c3983b2SBen Gras sprintf(p, "%s %s", editor, filename);
2570c3983b2SBen Gras argp[2] = p;
2580c3983b2SBen Gras
2590c3983b2SBen Gras switch(editpid = vfork()) {
2600c3983b2SBen Gras case -1:
2610c3983b2SBen Gras free(p);
2620c3983b2SBen Gras return;
2630c3983b2SBen Gras case 0:
2640c3983b2SBen Gras if (notsetuid) {
2650c3983b2SBen Gras setgid(getgid());
2660c3983b2SBen Gras setuid(getuid());
2670c3983b2SBen Gras }
2680c3983b2SBen Gras execvp(_PATH_BSHELL, (char *const *)__UNCONST(argp));
2690c3983b2SBen Gras _exit(1);
2700c3983b2SBen Gras }
2710c3983b2SBen Gras
2720c3983b2SBen Gras free(p);
2730c3983b2SBen Gras
2740c3983b2SBen Gras for (;;) {
2750c3983b2SBen Gras editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
2760c3983b2SBen Gras if (editpid == -1)
2770c3983b2SBen Gras pw_error(editor, 1, 1);
2780c3983b2SBen Gras else if (WIFSTOPPED(pstat))
2790c3983b2SBen Gras raise(WSTOPSIG(pstat));
2800c3983b2SBen Gras else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
2810c3983b2SBen Gras break;
2820c3983b2SBen Gras else
2830c3983b2SBen Gras pw_error(editor, 1, 1);
2840c3983b2SBen Gras }
2850c3983b2SBen Gras editpid = -1;
2860c3983b2SBen Gras }
2870c3983b2SBen Gras
2880c3983b2SBen Gras void
pw_prompt(void)2890c3983b2SBen Gras pw_prompt(void)
2900c3983b2SBen Gras {
2910c3983b2SBen Gras int c;
2920c3983b2SBen Gras
2930c3983b2SBen Gras (void)printf("re-edit the password file? [y]: ");
2940c3983b2SBen Gras (void)fflush(stdout);
2950c3983b2SBen Gras c = getchar();
2960c3983b2SBen Gras if (c != EOF && c != '\n')
2970c3983b2SBen Gras while (getchar() != '\n');
2980c3983b2SBen Gras if (c == 'n')
2990c3983b2SBen Gras pw_error(NULL, 0, 0);
3000c3983b2SBen Gras }
3010c3983b2SBen Gras
3020c3983b2SBen Gras /* for use in pw_copy(). Compare a pw entry to a pw struct. */
3030c3983b2SBen Gras /* returns a character string labelling the miscompared field or 0 */
3040c3983b2SBen Gras static const char *
pw_equal(char * buf,struct passwd * pw)3050c3983b2SBen Gras pw_equal(char *buf, struct passwd *pw)
3060c3983b2SBen Gras {
3070c3983b2SBen Gras struct passwd buf_pw;
3080c3983b2SBen Gras size_t len;
3090c3983b2SBen Gras
3100c3983b2SBen Gras _DIAGASSERT(buf != NULL);
3110c3983b2SBen Gras _DIAGASSERT(pw != NULL);
3120c3983b2SBen Gras
3130c3983b2SBen Gras len = strlen (buf);
3140c3983b2SBen Gras if (buf[len-1] == '\n')
3150c3983b2SBen Gras buf[len-1] = '\0';
3160c3983b2SBen Gras if (!pw_scan(buf, &buf_pw, NULL))
3170c3983b2SBen Gras return "corrupt line";
3180c3983b2SBen Gras if (strcmp(pw->pw_name, buf_pw.pw_name) != 0)
3190c3983b2SBen Gras return "name";
3200c3983b2SBen Gras if (pw->pw_uid != buf_pw.pw_uid)
3210c3983b2SBen Gras return "uid";
3220c3983b2SBen Gras if (pw->pw_gid != buf_pw.pw_gid)
3230c3983b2SBen Gras return "gid";
3240c3983b2SBen Gras if (strcmp( pw->pw_class, buf_pw.pw_class) != 0)
3250c3983b2SBen Gras return "class";
3260c3983b2SBen Gras if (pw->pw_change != buf_pw.pw_change)
3270c3983b2SBen Gras return "change";
3280c3983b2SBen Gras if (pw->pw_expire != buf_pw.pw_expire)
3290c3983b2SBen Gras return "expire";
3300c3983b2SBen Gras if (strcmp( pw->pw_gecos, buf_pw.pw_gecos) != 0)
3310c3983b2SBen Gras return "gecos";
3320c3983b2SBen Gras if (strcmp( pw->pw_dir, buf_pw.pw_dir) != 0)
3330c3983b2SBen Gras return "dir";
3340c3983b2SBen Gras if (strcmp( pw->pw_shell, buf_pw.pw_shell) != 0)
3350c3983b2SBen Gras return "shell";
3360c3983b2SBen Gras return (char *)0;
3370c3983b2SBen Gras }
3380c3983b2SBen Gras
3390c3983b2SBen Gras void
pw_copy(int ffd,int tfd,struct passwd * pw,struct passwd * old_pw)3400c3983b2SBen Gras pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
3410c3983b2SBen Gras {
3420c3983b2SBen Gras char errbuf[200];
3430c3983b2SBen Gras int rv;
3440c3983b2SBen Gras
3450c3983b2SBen Gras rv = pw_copyx(ffd, tfd, pw, old_pw, errbuf, sizeof(errbuf));
3460c3983b2SBen Gras if (rv == 0) {
3470c3983b2SBen Gras warnx("%s", errbuf);
3480c3983b2SBen Gras pw_error(NULL, 0, 1);
3490c3983b2SBen Gras }
3500c3983b2SBen Gras }
3510c3983b2SBen Gras
3520c3983b2SBen Gras static void
pw_print(FILE * to,const struct passwd * pw)3530c3983b2SBen Gras pw_print(FILE *to, const struct passwd *pw)
3540c3983b2SBen Gras {
3550c3983b2SBen Gras (void)fprintf(to, "%s:%s:%d:%d:%s:%lld:%lld:%s:%s:%s\n",
3560c3983b2SBen Gras pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
3570c3983b2SBen Gras pw->pw_class, (long long)pw->pw_change,
3580c3983b2SBen Gras (long long)pw->pw_expire,
3590c3983b2SBen Gras pw->pw_gecos, pw->pw_dir, pw->pw_shell);
3600c3983b2SBen Gras }
3610c3983b2SBen Gras
3620c3983b2SBen Gras int
pw_copyx(int ffd,int tfd,struct passwd * pw,struct passwd * old_pw,char * errbuf,size_t errbufsz)3630c3983b2SBen Gras pw_copyx(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw,
3640c3983b2SBen Gras char *errbuf, size_t errbufsz)
3650c3983b2SBen Gras {
3660c3983b2SBen Gras const char *filename;
3670c3983b2SBen Gras char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192];
3680c3983b2SBen Gras FILE *from, *to;
3690c3983b2SBen Gras int done;
3700c3983b2SBen Gras
3710c3983b2SBen Gras _DIAGASSERT(pw != NULL);
3720c3983b2SBen Gras _DIAGASSERT(errbuf != NULL);
3730c3983b2SBen Gras /* old_pw may be NULL */
3740c3983b2SBen Gras
3750c3983b2SBen Gras if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL) {
3760c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s: %s", pw_prefix,
3770c3983b2SBen Gras strerror(errno));
3780c3983b2SBen Gras return (0);
3790c3983b2SBen Gras }
3800c3983b2SBen Gras (void)strcpy(mpwd, filename);
3810c3983b2SBen Gras if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL) {
3820c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s: %s", pw_prefix,
3830c3983b2SBen Gras strerror(errno));
3840c3983b2SBen Gras return (0);
3850c3983b2SBen Gras }
3860c3983b2SBen Gras (void)strcpy(mpwdl, filename);
3870c3983b2SBen Gras
3880c3983b2SBen Gras if (!(from = fdopen(ffd, "r"))) {
3890c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s: %s", mpwd, strerror(errno));
3900c3983b2SBen Gras return (0);
3910c3983b2SBen Gras }
3920c3983b2SBen Gras if (!(to = fdopen(tfd, "w"))) {
3930c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s: %s", mpwdl, strerror(errno));
3940c3983b2SBen Gras (void)fclose(from);
3950c3983b2SBen Gras return (0);
3960c3983b2SBen Gras }
3970c3983b2SBen Gras
3980c3983b2SBen Gras for (done = 0; fgets(buf, (int)sizeof(buf), from);) {
3990c3983b2SBen Gras const char *neq;
4000c3983b2SBen Gras if (!strchr(buf, '\n')) {
4010c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s: line too long", mpwd);
4020c3983b2SBen Gras (void)fclose(from);
4030c3983b2SBen Gras (void)fclose(to);
4040c3983b2SBen Gras return (0);
4050c3983b2SBen Gras }
4060c3983b2SBen Gras if (done) {
4070c3983b2SBen Gras (void)fprintf(to, "%s", buf);
4080c3983b2SBen Gras if (ferror(to)) {
4090c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s",
4100c3983b2SBen Gras strerror(errno));
4110c3983b2SBen Gras (void)fclose(from);
4120c3983b2SBen Gras (void)fclose(to);
4130c3983b2SBen Gras return (0);
4140c3983b2SBen Gras }
4150c3983b2SBen Gras continue;
4160c3983b2SBen Gras }
4170c3983b2SBen Gras if (!(p = strchr(buf, ':'))) {
4180c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s: corrupted entry", mpwd);
4190c3983b2SBen Gras (void)fclose(from);
4200c3983b2SBen Gras (void)fclose(to);
4210c3983b2SBen Gras return (0);
4220c3983b2SBen Gras }
4230c3983b2SBen Gras *p = '\0';
4240c3983b2SBen Gras if (strcmp(buf, pw->pw_name)) {
4250c3983b2SBen Gras *p = ':';
4260c3983b2SBen Gras (void)fprintf(to, "%s", buf);
4270c3983b2SBen Gras if (ferror(to)) {
4280c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s",
4290c3983b2SBen Gras strerror(errno));
4300c3983b2SBen Gras (void)fclose(from);
4310c3983b2SBen Gras (void)fclose(to);
4320c3983b2SBen Gras return (0);
4330c3983b2SBen Gras }
4340c3983b2SBen Gras continue;
4350c3983b2SBen Gras }
4360c3983b2SBen Gras *p = ':';
4370c3983b2SBen Gras if (old_pw && (neq = pw_equal(buf, old_pw)) != NULL) {
4380c3983b2SBen Gras if (strcmp(neq, "corrupt line") == 0)
4390c3983b2SBen Gras (void)snprintf(errbuf, errbufsz,
4400c3983b2SBen Gras "%s: entry %s corrupted", mpwd,
4410c3983b2SBen Gras pw->pw_name);
4420c3983b2SBen Gras else
4430c3983b2SBen Gras (void)snprintf(errbuf, errbufsz,
4440c3983b2SBen Gras "%s: entry %s inconsistent %s",
4450c3983b2SBen Gras mpwd, pw->pw_name, neq);
4460c3983b2SBen Gras (void)fclose(from);
4470c3983b2SBen Gras (void)fclose(to);
4480c3983b2SBen Gras return (0);
4490c3983b2SBen Gras }
4500c3983b2SBen Gras pw_print(to, pw);
4510c3983b2SBen Gras done = 1;
4520c3983b2SBen Gras if (ferror(to)) {
4530c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s", strerror(errno));
4540c3983b2SBen Gras (void)fclose(from);
4550c3983b2SBen Gras (void)fclose(to);
4560c3983b2SBen Gras return (0);
4570c3983b2SBen Gras }
4580c3983b2SBen Gras }
4590c3983b2SBen Gras /* Only append a new entry if real uid is root! */
4600c3983b2SBen Gras if (!done) {
4610c3983b2SBen Gras if (getuid() == 0) {
4620c3983b2SBen Gras pw_print(to, pw);
4630c3983b2SBen Gras done = 1;
4640c3983b2SBen Gras } else {
4650c3983b2SBen Gras snprintf(errbuf, errbufsz,
4660c3983b2SBen Gras "%s: changes not made, no such entry", mpwd);
4670c3983b2SBen Gras }
4680c3983b2SBen Gras }
4690c3983b2SBen Gras
4700c3983b2SBen Gras if (ferror(to)) {
4710c3983b2SBen Gras snprintf(errbuf, errbufsz, "%s", strerror(errno));
4720c3983b2SBen Gras (void)fclose(from);
4730c3983b2SBen Gras (void)fclose(to);
4740c3983b2SBen Gras return (0);
4750c3983b2SBen Gras }
4760c3983b2SBen Gras (void)fclose(from);
4770c3983b2SBen Gras (void)fclose(to);
4780c3983b2SBen Gras
4790c3983b2SBen Gras return (done);
4800c3983b2SBen Gras }
4810c3983b2SBen Gras
4820c3983b2SBen Gras void
pw_error(const char * name,int error,int eval)4830c3983b2SBen Gras pw_error(const char *name, int error, int eval)
4840c3983b2SBen Gras {
4850c3983b2SBen Gras
4860c3983b2SBen Gras if (error) {
4870c3983b2SBen Gras if (name)
4880c3983b2SBen Gras warn("%s", name);
4890c3983b2SBen Gras else
4900c3983b2SBen Gras warn(NULL);
4910c3983b2SBen Gras }
4920c3983b2SBen Gras
4930c3983b2SBen Gras warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD);
4940c3983b2SBen Gras pw_abort();
4950c3983b2SBen Gras exit(eval);
4960c3983b2SBen Gras }
4970c3983b2SBen Gras
4980c3983b2SBen Gras /* Removes head and/or tail spaces. */
4990c3983b2SBen Gras static void
trim_whitespace(char * line)5000c3983b2SBen Gras trim_whitespace(char *line)
5010c3983b2SBen Gras {
5020c3983b2SBen Gras char *p;
5030c3983b2SBen Gras
5040c3983b2SBen Gras _DIAGASSERT(line != NULL);
5050c3983b2SBen Gras
5060c3983b2SBen Gras /* Remove leading spaces */
5070c3983b2SBen Gras p = line;
5080c3983b2SBen Gras while (isspace((unsigned char) *p))
5090c3983b2SBen Gras p++;
5100c3983b2SBen Gras memmove(line, p, strlen(p) + 1);
5110c3983b2SBen Gras
5120c3983b2SBen Gras /* Remove trailing spaces */
5130c3983b2SBen Gras p = line + strlen(line) - 1;
5140c3983b2SBen Gras while (isspace((unsigned char) *p))
5150c3983b2SBen Gras p--;
5160c3983b2SBen Gras *(p + 1) = '\0';
5170c3983b2SBen Gras }
5180c3983b2SBen Gras
5190c3983b2SBen Gras
5200c3983b2SBen Gras /* Get one line, remove spaces from front and tail */
5210c3983b2SBen Gras static int
read_line(FILE * fp,char * line,int max)5220c3983b2SBen Gras read_line(FILE *fp, char *line, int max)
5230c3983b2SBen Gras {
5240c3983b2SBen Gras char *p;
5250c3983b2SBen Gras
5260c3983b2SBen Gras _DIAGASSERT(fp != NULL);
5270c3983b2SBen Gras _DIAGASSERT(line != NULL);
5280c3983b2SBen Gras
5290c3983b2SBen Gras /* Read one line of config */
5300c3983b2SBen Gras if (fgets(line, max, fp) == NULL)
5310c3983b2SBen Gras return (0);
5320c3983b2SBen Gras
5330c3983b2SBen Gras if ((p = strchr(line, '\n')) == NULL) {
5340c3983b2SBen Gras warnx("line too long");
5350c3983b2SBen Gras return (0);
5360c3983b2SBen Gras }
5370c3983b2SBen Gras *p = '\0';
5380c3983b2SBen Gras
5390c3983b2SBen Gras /* Remove comments */
5400c3983b2SBen Gras if ((p = strchr(line, '#')) != NULL)
5410c3983b2SBen Gras *p = '\0';
5420c3983b2SBen Gras
5430c3983b2SBen Gras trim_whitespace(line);
5440c3983b2SBen Gras return (1);
5450c3983b2SBen Gras }
5460c3983b2SBen Gras
5470c3983b2SBen Gras static const char *
pw_default(const char * option)5480c3983b2SBen Gras pw_default(const char *option)
5490c3983b2SBen Gras {
5500c3983b2SBen Gras static const char *options[][2] = {
5510c3983b2SBen Gras { "localcipher", "old" },
5520c3983b2SBen Gras { "ypcipher", "old" },
5530c3983b2SBen Gras };
5540c3983b2SBen Gras size_t i;
5550c3983b2SBen Gras
5560c3983b2SBen Gras _DIAGASSERT(option != NULL);
5570c3983b2SBen Gras for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
5580c3983b2SBen Gras if (strcmp(options[i][0], option) == 0)
5590c3983b2SBen Gras return (options[i][1]);
5600c3983b2SBen Gras
5610c3983b2SBen Gras return (NULL);
5620c3983b2SBen Gras }
5630c3983b2SBen Gras
5640c3983b2SBen Gras /*
5650c3983b2SBen Gras * Retrieve password information from the /etc/passwd.conf file, at the
5660c3983b2SBen Gras * moment this is only for choosing the cipher to use. It could easily be
5670c3983b2SBen Gras * used for other authentication methods as well.
5680c3983b2SBen Gras */
5690c3983b2SBen Gras void
pw_getconf(char * data,size_t max,const char * key,const char * option)5700c3983b2SBen Gras pw_getconf(char *data, size_t max, const char *key, const char *option)
5710c3983b2SBen Gras {
5720c3983b2SBen Gras FILE *fp;
5730c3983b2SBen Gras char line[LINE_MAX], *p, *p2;
5740c3983b2SBen Gras static char result[LINE_MAX];
5750c3983b2SBen Gras int got, found;
5760c3983b2SBen Gras const char *cp;
5770c3983b2SBen Gras
5780c3983b2SBen Gras _DIAGASSERT(data != NULL);
5790c3983b2SBen Gras _DIAGASSERT(key != NULL);
5800c3983b2SBen Gras _DIAGASSERT(option != NULL);
5810c3983b2SBen Gras
5820c3983b2SBen Gras got = 0;
5830c3983b2SBen Gras found = 0;
5840c3983b2SBen Gras result[0] = '\0';
5850c3983b2SBen Gras
5860c3983b2SBen Gras if ((fp = fopen(_PATH_PASSWD_CONF, "r")) == NULL) {
5870c3983b2SBen Gras if ((cp = pw_default(option)) != NULL)
5880c3983b2SBen Gras strlcpy(data, cp, max);
5890c3983b2SBen Gras else
5900c3983b2SBen Gras data[0] = '\0';
5910c3983b2SBen Gras return;
5920c3983b2SBen Gras }
5930c3983b2SBen Gras
5940c3983b2SBen Gras while (!found && (got || read_line(fp, line, LINE_MAX))) {
5950c3983b2SBen Gras got = 0;
5960c3983b2SBen Gras
5970c3983b2SBen Gras if (strncmp(key, line, strlen(key)) != 0 ||
5980c3983b2SBen Gras line[strlen(key)] != ':')
5990c3983b2SBen Gras continue;
6000c3983b2SBen Gras
6010c3983b2SBen Gras /* Now we found our specified key */
6020c3983b2SBen Gras while (read_line(fp, line, LINE_MAX)) {
6030c3983b2SBen Gras /* Leaving key field */
6040c3983b2SBen Gras if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
6050c3983b2SBen Gras got = 1;
6060c3983b2SBen Gras break;
6070c3983b2SBen Gras }
6080c3983b2SBen Gras p2 = line;
6090c3983b2SBen Gras if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
6100c3983b2SBen Gras continue;
6110c3983b2SBen Gras trim_whitespace(p);
6120c3983b2SBen Gras
6130c3983b2SBen Gras if (!strncmp(p, option, strlen(option))) {
6140c3983b2SBen Gras trim_whitespace(p2);
6150c3983b2SBen Gras strcpy(result, p2);
6160c3983b2SBen Gras found = 1;
6170c3983b2SBen Gras break;
6180c3983b2SBen Gras }
6190c3983b2SBen Gras }
6200c3983b2SBen Gras }
6210c3983b2SBen Gras fclose(fp);
6220c3983b2SBen Gras
6230c3983b2SBen Gras if (!found)
6240c3983b2SBen Gras errno = ENOENT;
6250c3983b2SBen Gras if (!got)
6260c3983b2SBen Gras errno = ENOTDIR;
6270c3983b2SBen Gras
6280c3983b2SBen Gras /*
6290c3983b2SBen Gras * If we got no result and were looking for a default
6300c3983b2SBen Gras * value, try hard coded defaults.
6310c3983b2SBen Gras */
6320c3983b2SBen Gras
6330c3983b2SBen Gras if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
6340c3983b2SBen Gras (cp = pw_default(option)) != NULL)
6350c3983b2SBen Gras strlcpy(data, cp, max);
6360c3983b2SBen Gras else
6370c3983b2SBen Gras strlcpy(data, result, max);
6380c3983b2SBen Gras }
6390c3983b2SBen Gras
6400c3983b2SBen Gras void
pw_getpwconf(char * data,size_t max,const struct passwd * pwd,const char * option)6410c3983b2SBen Gras pw_getpwconf(char *data, size_t max, const struct passwd *pwd,
6420c3983b2SBen Gras const char *option)
6430c3983b2SBen Gras {
6440c3983b2SBen Gras char grpkey[LINE_MAX];
6450c3983b2SBen Gras struct group grs, *grp;
6460c3983b2SBen Gras char grbuf[1024];
6470c3983b2SBen Gras
6480c3983b2SBen Gras pw_getconf(data, max, pwd->pw_name, option);
6490c3983b2SBen Gras
6500c3983b2SBen Gras /* Try to find an entry for the group */
6510c3983b2SBen Gras if (*data == '\0') {
6520c3983b2SBen Gras (void)getgrgid_r(pwd->pw_gid, &grs, grbuf, sizeof(grbuf), &grp);
6530c3983b2SBen Gras if (grp != NULL) {
6540c3983b2SBen Gras (void)snprintf(grpkey, sizeof(grpkey), ":%s",
6550c3983b2SBen Gras grp->gr_name);
6560c3983b2SBen Gras pw_getconf(data, max, grpkey, option);
6570c3983b2SBen Gras }
6580c3983b2SBen Gras if (*data == '\0')
6590c3983b2SBen Gras pw_getconf(data, max, "default", option);
6600c3983b2SBen Gras }
6610c3983b2SBen Gras }
662