xref: /csrg-svn/usr.sbin/vipw/pw_util.c (revision 49830)
146377Sbostic /*-
246377Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346377Sbostic  * All rights reserved.
446377Sbostic  *
546377Sbostic  * %sccs.include.redist.c%
646377Sbostic  */
746377Sbostic 
846377Sbostic #ifndef lint
9*49830Sbostic static char sccsid[] = "@(#)pw_util.c	5.4 (Berkeley) 05/21/91";
1046377Sbostic #endif /* not lint */
1146377Sbostic 
1246377Sbostic /*
1346377Sbostic  * This file is used by all the "password" programs; vipw(8), chpass(1),
1446377Sbostic  * and passwd(1).
1546377Sbostic  */
1646377Sbostic 
1746377Sbostic #include <sys/param.h>
1846377Sbostic #include <sys/wait.h>
1946377Sbostic #include <sys/time.h>
2046377Sbostic #include <sys/resource.h>
2146377Sbostic #include <signal.h>
2246377Sbostic #include <fcntl.h>
2346377Sbostic #include <pwd.h>
2446377Sbostic #include <errno.h>
2546377Sbostic #include <stdio.h>
2646377Sbostic #include <paths.h>
2746377Sbostic #include <string.h>
2846377Sbostic #include <stdlib.h>
2946377Sbostic 
3046377Sbostic extern char *progname;
3146377Sbostic extern char *tempname;
3246377Sbostic 
3346377Sbostic pw_init()
3446377Sbostic {
3546377Sbostic 	struct rlimit rlim;
3646377Sbostic 
37*49830Sbostic 	/* Unlimited resource limits. */
3846377Sbostic 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
3946377Sbostic 	(void)setrlimit(RLIMIT_CPU, &rlim);
4046377Sbostic 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
41*49830Sbostic 	(void)setrlimit(RLIMIT_STACK, &rlim);
42*49830Sbostic 	(void)setrlimit(RLIMIT_DATA, &rlim);
43*49830Sbostic 	(void)setrlimit(RLIMIT_RSS, &rlim);
4446377Sbostic 
4546377Sbostic 	/* Don't drop core (not really necessary, but GP's). */
4646377Sbostic 	rlim.rlim_cur = rlim.rlim_max = 0;
4746377Sbostic 	(void)setrlimit(RLIMIT_CORE, &rlim);
4846377Sbostic 
49*49830Sbostic 	/* Turn off signals. */
50*49830Sbostic 	(void)signal(SIGALRM, SIG_IGN);
5147287Sbostic 	(void)signal(SIGHUP, SIG_IGN);
5247287Sbostic 	(void)signal(SIGINT, SIG_IGN);
53*49830Sbostic 	(void)signal(SIGPIPE, SIG_IGN);
5447287Sbostic 	(void)signal(SIGQUIT, SIG_IGN);
5547287Sbostic 	(void)signal(SIGTERM, SIG_IGN);
5647287Sbostic 	(void)signal(SIGTSTP, SIG_IGN);
57*49830Sbostic 	(void)signal(SIGTTOU, SIG_IGN);
5846377Sbostic 
5946377Sbostic 	/* Create with exact permissions. */
6046377Sbostic 	(void)umask(0);
6146377Sbostic }
6246377Sbostic 
6346377Sbostic static int lockfd;
6446377Sbostic pw_lock()
6546377Sbostic {
6646377Sbostic 	/*
6746377Sbostic 	 * If the master password file doesn't exist, the system is hosed.
6846377Sbostic 	 * Might as well try to build one.
6946377Sbostic 	 * Open should allow flock'ing the file; see 4.4BSD.	XXX
7046377Sbostic 	 */
7146377Sbostic 	lockfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
7246377Sbostic 	if (lockfd < 0) {
7346377Sbostic 		(void)fprintf(stderr, "%s: %s: %s\n",
7446377Sbostic 		    progname, _PATH_MASTERPASSWD, strerror(errno));
7546377Sbostic 		exit(1);
7646377Sbostic 	}
7746955Sbostic 	if (flock(lockfd, LOCK_EX|LOCK_NB)) {
7846377Sbostic 		(void)fprintf(stderr,
7946377Sbostic 		    "%s: the password db is busy.\n", progname);
8046377Sbostic 		exit(1);
8146377Sbostic 	}
8246377Sbostic 	return(lockfd);
8346377Sbostic }
8446377Sbostic 
8546377Sbostic pw_tmp()
8646377Sbostic {
8746377Sbostic 	static char path[MAXPATHLEN] = _PATH_MASTERPASSWD;
8846377Sbostic 	int fd;
8946377Sbostic 	char *p;
9046377Sbostic 
9146377Sbostic 	if (p = rindex(path, '/'))
9246377Sbostic 		++p;
9346377Sbostic 	else
9446377Sbostic 		p = path;
9546377Sbostic 	(void)sprintf(p, "%s.XXXXXX", progname);
9646377Sbostic 	if ((fd = mkstemp(path)) == -1) {
9746377Sbostic 		(void)fprintf(stderr,
9846377Sbostic 		    "%s: %s: %s\n", progname, path, strerror(errno));
9946377Sbostic 		exit(1);
10046377Sbostic 	}
10146377Sbostic 	tempname = path;
10246377Sbostic 	return(fd);
10346377Sbostic }
10446377Sbostic 
10546377Sbostic pw_mkdb()
10646377Sbostic {
10746377Sbostic 	union wait pstat;
10846377Sbostic 	pid_t pid;
10946377Sbostic 
11046377Sbostic 	(void)printf("%s: rebuilding the database...\n", progname);
11146377Sbostic 	(void)fflush(stdout);
11246377Sbostic 	if (!(pid = vfork())) {
11346377Sbostic 		execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", tempname, NULL);
11446377Sbostic 		pw_error(_PATH_PWD_MKDB, 1, 1);
11546377Sbostic 	}
11646377Sbostic 	pid = waitpid(pid, (int *)&pstat, 0);
11746377Sbostic 	if (pid == -1 || pstat.w_status)
11846377Sbostic 		return(0);
11946377Sbostic 	(void)printf("%s: done\n", progname);
12046377Sbostic 	return(1);
12146377Sbostic }
12246377Sbostic 
12346377Sbostic pw_edit(notsetuid)
12446377Sbostic 	int notsetuid;
12546377Sbostic {
12646377Sbostic 	union wait pstat;
12746377Sbostic 	pid_t pid;
12846377Sbostic 	char *p, *editor;
12946377Sbostic 
13046377Sbostic 	if (!(editor = getenv("EDITOR")))
13146377Sbostic 		editor = _PATH_VI;
13246377Sbostic 	if (p = rindex(editor, '/'))
13346377Sbostic 		++p;
13446377Sbostic 	else
13546377Sbostic 		p = editor;
13646377Sbostic 
13746377Sbostic 	if (!(pid = vfork())) {
13846377Sbostic 		if (notsetuid) {
13946377Sbostic 			(void)setgid(getgid());
14046377Sbostic 			(void)setuid(getuid());
14146377Sbostic 		}
14246377Sbostic 		execlp(editor, p, tempname, NULL);
14346955Sbostic 		_exit(1);
14446377Sbostic 	}
14546377Sbostic 	pid = waitpid(pid, (int *)&pstat, 0);
14646955Sbostic 	if (pid == -1 || pstat.w_status)
14746955Sbostic 		pw_error(editor, 1, 1);
14846377Sbostic }
14946377Sbostic 
15046377Sbostic pw_prompt()
15146377Sbostic {
15246377Sbostic 	register int c;
15346377Sbostic 
15446377Sbostic 	for (;;) {
15546377Sbostic 		(void)printf("re-edit the password file? [y]: ");
15646377Sbostic 		(void)fflush(stdout);
15746377Sbostic 		c = getchar();
15846377Sbostic 		if (c != EOF && c != (int)'\n')
15946377Sbostic 			while (getchar() != (int)'\n');
16046377Sbostic 		if (c == (int)'n')
16146377Sbostic 			pw_error((char *)NULL, 0, 0);
16246377Sbostic 		break;
16346377Sbostic 	}
16446377Sbostic }
16546377Sbostic 
16646377Sbostic pw_error(name, err, eval)
16746377Sbostic 	char *name;
16846377Sbostic 	int err, eval;
16946377Sbostic {
17046377Sbostic 	int sverrno;
17146377Sbostic 
17246377Sbostic 	if (err) {
17346377Sbostic 		sverrno = errno;
17446377Sbostic 		(void)fprintf(stderr, "%s: ", progname);
17546377Sbostic 		if (name)
17646377Sbostic 			(void)fprintf(stderr, "%s: ", name);
17746377Sbostic 		(void)fprintf(stderr, "%s\n", strerror(sverrno));
17846377Sbostic 	}
17946377Sbostic 	(void)fprintf(stderr,
18046377Sbostic 	    "%s: %s unchanged\n", progname, _PATH_MASTERPASSWD);
18146377Sbostic 	(void)unlink(tempname);
18246377Sbostic 	exit(eval);
18346377Sbostic }
184