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