xref: /csrg-svn/usr.sbin/vipw/pw_util.c (revision 47287)
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*47287Sbostic static char sccsid[] = "@(#)pw_util.c	5.3 (Berkeley) 03/13/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 
3746377Sbostic 	/* Unlimited cpu, file size. */
3846377Sbostic 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
3946377Sbostic 	(void)setrlimit(RLIMIT_CPU, &rlim);
4046377Sbostic 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
4146377Sbostic 
4246377Sbostic 	/* Don't drop core (not really necessary, but GP's). */
4346377Sbostic 	rlim.rlim_cur = rlim.rlim_max = 0;
4446377Sbostic 	(void)setrlimit(RLIMIT_CORE, &rlim);
4546377Sbostic 
4646377Sbostic 	/* Turn off usual signals. */
47*47287Sbostic 	(void)signal(SIGHUP, SIG_IGN);
48*47287Sbostic 	(void)signal(SIGINT, SIG_IGN);
49*47287Sbostic 	(void)signal(SIGQUIT, SIG_IGN);
50*47287Sbostic 	(void)signal(SIGTERM, SIG_IGN);
51*47287Sbostic 	(void)signal(SIGTSTP, SIG_IGN);
5246377Sbostic 
5346377Sbostic 	/* Create with exact permissions. */
5446377Sbostic 	(void)umask(0);
5546377Sbostic }
5646377Sbostic 
5746377Sbostic static int lockfd;
5846377Sbostic pw_lock()
5946377Sbostic {
6046377Sbostic 	/*
6146377Sbostic 	 * If the master password file doesn't exist, the system is hosed.
6246377Sbostic 	 * Might as well try to build one.
6346377Sbostic 	 * Open should allow flock'ing the file; see 4.4BSD.	XXX
6446377Sbostic 	 */
6546377Sbostic 	lockfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
6646377Sbostic 	if (lockfd < 0) {
6746377Sbostic 		(void)fprintf(stderr, "%s: %s: %s\n",
6846377Sbostic 		    progname, _PATH_MASTERPASSWD, strerror(errno));
6946377Sbostic 		exit(1);
7046377Sbostic 	}
7146955Sbostic 	if (flock(lockfd, LOCK_EX|LOCK_NB)) {
7246377Sbostic 		(void)fprintf(stderr,
7346377Sbostic 		    "%s: the password db is busy.\n", progname);
7446377Sbostic 		exit(1);
7546377Sbostic 	}
7646377Sbostic 	return(lockfd);
7746377Sbostic }
7846377Sbostic 
7946377Sbostic pw_tmp()
8046377Sbostic {
8146377Sbostic 	static char path[MAXPATHLEN] = _PATH_MASTERPASSWD;
8246377Sbostic 	int fd;
8346377Sbostic 	char *p;
8446377Sbostic 
8546377Sbostic 	if (p = rindex(path, '/'))
8646377Sbostic 		++p;
8746377Sbostic 	else
8846377Sbostic 		p = path;
8946377Sbostic 	(void)sprintf(p, "%s.XXXXXX", progname);
9046377Sbostic 	if ((fd = mkstemp(path)) == -1) {
9146377Sbostic 		(void)fprintf(stderr,
9246377Sbostic 		    "%s: %s: %s\n", progname, path, strerror(errno));
9346377Sbostic 		exit(1);
9446377Sbostic 	}
9546377Sbostic 	tempname = path;
9646377Sbostic 	return(fd);
9746377Sbostic }
9846377Sbostic 
9946377Sbostic pw_mkdb()
10046377Sbostic {
10146377Sbostic 	union wait pstat;
10246377Sbostic 	pid_t pid;
10346377Sbostic 
10446377Sbostic 	(void)printf("%s: rebuilding the database...\n", progname);
10546377Sbostic 	(void)fflush(stdout);
10646377Sbostic 	if (!(pid = vfork())) {
10746377Sbostic 		execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", tempname, NULL);
10846377Sbostic 		pw_error(_PATH_PWD_MKDB, 1, 1);
10946377Sbostic 	}
11046377Sbostic 	pid = waitpid(pid, (int *)&pstat, 0);
11146377Sbostic 	if (pid == -1 || pstat.w_status)
11246377Sbostic 		return(0);
11346377Sbostic 	(void)printf("%s: done\n", progname);
11446377Sbostic 	return(1);
11546377Sbostic }
11646377Sbostic 
11746377Sbostic pw_edit(notsetuid)
11846377Sbostic 	int notsetuid;
11946377Sbostic {
12046377Sbostic 	union wait pstat;
12146377Sbostic 	pid_t pid;
12246377Sbostic 	char *p, *editor;
12346377Sbostic 
12446377Sbostic 	if (!(editor = getenv("EDITOR")))
12546377Sbostic 		editor = _PATH_VI;
12646377Sbostic 	if (p = rindex(editor, '/'))
12746377Sbostic 		++p;
12846377Sbostic 	else
12946377Sbostic 		p = editor;
13046377Sbostic 
13146377Sbostic 	if (!(pid = vfork())) {
13246377Sbostic 		if (notsetuid) {
13346377Sbostic 			(void)setgid(getgid());
13446377Sbostic 			(void)setuid(getuid());
13546377Sbostic 		}
13646377Sbostic 		execlp(editor, p, tempname, NULL);
13746955Sbostic 		_exit(1);
13846377Sbostic 	}
13946377Sbostic 	pid = waitpid(pid, (int *)&pstat, 0);
14046955Sbostic 	if (pid == -1 || pstat.w_status)
14146955Sbostic 		pw_error(editor, 1, 1);
14246377Sbostic }
14346377Sbostic 
14446377Sbostic pw_prompt()
14546377Sbostic {
14646377Sbostic 	register int c;
14746377Sbostic 
14846377Sbostic 	for (;;) {
14946377Sbostic 		(void)printf("re-edit the password file? [y]: ");
15046377Sbostic 		(void)fflush(stdout);
15146377Sbostic 		c = getchar();
15246377Sbostic 		if (c != EOF && c != (int)'\n')
15346377Sbostic 			while (getchar() != (int)'\n');
15446377Sbostic 		if (c == (int)'n')
15546377Sbostic 			pw_error((char *)NULL, 0, 0);
15646377Sbostic 		break;
15746377Sbostic 	}
15846377Sbostic }
15946377Sbostic 
16046377Sbostic pw_error(name, err, eval)
16146377Sbostic 	char *name;
16246377Sbostic 	int err, eval;
16346377Sbostic {
16446377Sbostic 	int sverrno;
16546377Sbostic 
16646377Sbostic 	if (err) {
16746377Sbostic 		sverrno = errno;
16846377Sbostic 		(void)fprintf(stderr, "%s: ", progname);
16946377Sbostic 		if (name)
17046377Sbostic 			(void)fprintf(stderr, "%s: ", name);
17146377Sbostic 		(void)fprintf(stderr, "%s\n", strerror(sverrno));
17246377Sbostic 	}
17346377Sbostic 	(void)fprintf(stderr,
17446377Sbostic 	    "%s: %s unchanged\n", progname, _PATH_MASTERPASSWD);
17546377Sbostic 	(void)unlink(tempname);
17646377Sbostic 	exit(eval);
17746377Sbostic }
178