Removed definition "LIB= rpc". We want libc.a to contain librpc.a, not
[unix-history] / .ref-BSD-4_3_Net_2 / usr / src / usr.sbin / vipw / pw_util.c
CommitLineData
78034019
WJ
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)pw_util.c 5.4 (Berkeley) 5/21/91";
36#endif /* not lint */
37
38/*
39 * This file is used by all the "password" programs; vipw(8), chpass(1),
40 * and passwd(1).
41 */
42
43#include <sys/param.h>
44#include <sys/wait.h>
45#include <sys/time.h>
46#include <sys/resource.h>
47#include <signal.h>
48#include <fcntl.h>
49#include <pwd.h>
50#include <errno.h>
51#include <stdio.h>
52#include <paths.h>
53#include <string.h>
54#include <stdlib.h>
55
56extern char *progname;
57extern char *tempname;
58
59pw_init()
60{
61 struct rlimit rlim;
62
63 /* Unlimited resource limits. */
64 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
65 (void)setrlimit(RLIMIT_CPU, &rlim);
66 (void)setrlimit(RLIMIT_FSIZE, &rlim);
67 (void)setrlimit(RLIMIT_STACK, &rlim);
68 (void)setrlimit(RLIMIT_DATA, &rlim);
69 (void)setrlimit(RLIMIT_RSS, &rlim);
70
71 /* Don't drop core (not really necessary, but GP's). */
72 rlim.rlim_cur = rlim.rlim_max = 0;
73 (void)setrlimit(RLIMIT_CORE, &rlim);
74
75 /* Turn off signals. */
76 (void)signal(SIGALRM, SIG_IGN);
77 (void)signal(SIGHUP, SIG_IGN);
78 (void)signal(SIGINT, SIG_IGN);
79 (void)signal(SIGPIPE, SIG_IGN);
80 (void)signal(SIGQUIT, SIG_IGN);
81 (void)signal(SIGTERM, SIG_IGN);
82 (void)signal(SIGTSTP, SIG_IGN);
83 (void)signal(SIGTTOU, SIG_IGN);
84
85 /* Create with exact permissions. */
86 (void)umask(0);
87}
88
89static int lockfd;
90pw_lock()
91{
92 /*
93 * If the master password file doesn't exist, the system is hosed.
94 * Might as well try to build one.
95 * Open should allow flock'ing the file; see 4.4BSD. XXX
96 */
97 lockfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
98 if (lockfd < 0) {
99 (void)fprintf(stderr, "%s: %s: %s\n",
100 progname, _PATH_MASTERPASSWD, strerror(errno));
101 exit(1);
102 }
103 if (flock(lockfd, LOCK_EX|LOCK_NB)) {
104 (void)fprintf(stderr,
105 "%s: the password db is busy.\n", progname);
106 exit(1);
107 }
108 return(lockfd);
109}
110
111pw_tmp()
112{
113 static char path[MAXPATHLEN] = _PATH_MASTERPASSWD;
114 int fd;
115 char *p;
116
117 if (p = rindex(path, '/'))
118 ++p;
119 else
120 p = path;
121 (void)sprintf(p, "%s.XXXXXX", progname);
122 if ((fd = mkstemp(path)) == -1) {
123 (void)fprintf(stderr,
124 "%s: %s: %s\n", progname, path, strerror(errno));
125 exit(1);
126 }
127 tempname = path;
128 return(fd);
129}
130
131pw_mkdb()
132{
133 union wait pstat;
134 pid_t pid;
135
136 (void)printf("%s: rebuilding the database...\n", progname);
137 (void)fflush(stdout);
138 if (!(pid = vfork())) {
139 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", tempname, NULL);
140 pw_error(_PATH_PWD_MKDB, 1, 1);
141 }
142 pid = waitpid(pid, (int *)&pstat, 0);
143 if (pid == -1 || pstat.w_status)
144 return(0);
145 (void)printf("%s: done\n", progname);
146 return(1);
147}
148
149pw_edit(notsetuid)
150 int notsetuid;
151{
152 union wait pstat;
153 pid_t pid;
154 char *p, *editor;
155
156 if (!(editor = getenv("EDITOR")))
157 editor = _PATH_VI;
158 if (p = rindex(editor, '/'))
159 ++p;
160 else
161 p = editor;
162
163 if (!(pid = vfork())) {
164 if (notsetuid) {
165 (void)setgid(getgid());
166 (void)setuid(getuid());
167 }
168 execlp(editor, p, tempname, NULL);
169 _exit(1);
170 }
171 pid = waitpid(pid, (int *)&pstat, 0);
172 if (pid == -1 || pstat.w_status)
173 pw_error(editor, 1, 1);
174}
175
176pw_prompt()
177{
178 register int c;
179
180 for (;;) {
181 (void)printf("re-edit the password file? [y]: ");
182 (void)fflush(stdout);
183 c = getchar();
184 if (c != EOF && c != (int)'\n')
185 while (getchar() != (int)'\n');
186 if (c == (int)'n')
187 pw_error((char *)NULL, 0, 0);
188 break;
189 }
190}
191
192pw_error(name, err, eval)
193 char *name;
194 int err, eval;
195{
196 int sverrno;
197
198 if (err) {
199 sverrno = errno;
200 (void)fprintf(stderr, "%s: ", progname);
201 if (name)
202 (void)fprintf(stderr, "%s: ", name);
203 (void)fprintf(stderr, "%s\n", strerror(sverrno));
204 }
205 (void)fprintf(stderr,
206 "%s: %s unchanged\n", progname, _PATH_MASTERPASSWD);
207 (void)unlink(tempname);
208 exit(eval);
209}