chpass.c revision 1.11 1 1.11 cjs /* $NetBSD: chpass.c,v 1.11 1997/01/05 10:06:40 cjs Exp $ */
2 1.5 glass
3 1.1 cgd /*-
4 1.5 glass * Copyright (c) 1988, 1993, 1994
5 1.5 glass * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.5 glass static char copyright[] =
38 1.5 glass "@(#) Copyright (c) 1988, 1993, 1994\n\
39 1.5 glass The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.5 glass #if 0
44 1.5 glass static char sccsid[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94";
45 1.5 glass #else
46 1.11 cjs static char rcsid[] = "$NetBSD: chpass.c,v 1.11 1997/01/05 10:06:40 cjs Exp $";
47 1.5 glass #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/stat.h>
52 1.1 cgd #include <sys/time.h>
53 1.1 cgd #include <sys/resource.h>
54 1.5 glass
55 1.5 glass #include <ctype.h>
56 1.5 glass #include <err.h>
57 1.5 glass #include <errno.h>
58 1.1 cgd #include <fcntl.h>
59 1.1 cgd #include <pwd.h>
60 1.1 cgd #include <stdio.h>
61 1.5 glass #include <stdlib.h>
62 1.1 cgd #include <string.h>
63 1.5 glass #include <unistd.h>
64 1.8 jtc #include <util.h>
65 1.5 glass
66 1.1 cgd #include "chpass.h"
67 1.1 cgd #include "pathnames.h"
68 1.1 cgd
69 1.9 thorpej extern char *__progname; /* from crt0.o */
70 1.9 thorpej
71 1.1 cgd char *tempname;
72 1.1 cgd uid_t uid;
73 1.9 thorpej int use_yp;
74 1.10 thorpej int yflag;
75 1.9 thorpej
76 1.9 thorpej void (*Pw_error) __P((const char *, int, int));
77 1.1 cgd
78 1.2 brezak #ifdef YP
79 1.2 brezak extern struct passwd *ypgetpwnam(), *ypgetpwuid();
80 1.9 thorpej extern int _yp_check __P((char **)); /* buried deep inside libc */
81 1.2 brezak #endif
82 1.2 brezak
83 1.5 glass void baduser __P((void));
84 1.5 glass void usage __P((void));
85 1.5 glass
86 1.5 glass int
87 1.1 cgd main(argc, argv)
88 1.1 cgd int argc;
89 1.1 cgd char **argv;
90 1.1 cgd {
91 1.5 glass enum { NEWSH, LOADENTRY, EDITENTRY } op;
92 1.5 glass struct passwd *pw, lpw;
93 1.8 jtc int ch, pfd, tfd, dfd;
94 1.10 thorpej char *arg, *username = NULL, tempname[] = "/etc/pw.XXXXXX";
95 1.1 cgd
96 1.2 brezak #ifdef YP
97 1.4 deraadt use_yp = _yp_check(NULL);
98 1.2 brezak #endif
99 1.2 brezak
100 1.1 cgd op = EDITENTRY;
101 1.2 brezak while ((ch = getopt(argc, argv, "a:s:ly")) != EOF)
102 1.1 cgd switch(ch) {
103 1.1 cgd case 'a':
104 1.1 cgd op = LOADENTRY;
105 1.1 cgd arg = optarg;
106 1.1 cgd break;
107 1.1 cgd case 's':
108 1.1 cgd op = NEWSH;
109 1.1 cgd arg = optarg;
110 1.1 cgd break;
111 1.4 deraadt case 'l':
112 1.4 deraadt use_yp = 0;
113 1.4 deraadt break;
114 1.4 deraadt case 'y':
115 1.9 thorpej #ifdef YP
116 1.9 thorpej if (!use_yp)
117 1.9 thorpej errx(1, "YP not in use.");
118 1.10 thorpej yflag = 1;
119 1.9 thorpej #else
120 1.9 thorpej errx(1, "YP support not compiled in.");
121 1.9 thorpej #endif
122 1.4 deraadt break;
123 1.1 cgd default:
124 1.1 cgd usage();
125 1.1 cgd }
126 1.1 cgd argc -= optind;
127 1.1 cgd argv += optind;
128 1.1 cgd
129 1.10 thorpej uid = getuid();
130 1.10 thorpej switch (argc) {
131 1.10 thorpej case 0:
132 1.10 thorpej /* nothing */
133 1.10 thorpej break;
134 1.10 thorpej
135 1.10 thorpej case 1:
136 1.10 thorpej username = argv[0];
137 1.10 thorpej break;
138 1.10 thorpej
139 1.10 thorpej default:
140 1.10 thorpej usage();
141 1.10 thorpej }
142 1.10 thorpej
143 1.10 thorpej #ifdef YP
144 1.10 thorpej /*
145 1.10 thorpej * We need to determine if we _really_ want to use YP.
146 1.10 thorpej * If we defaulted to YP (i.e. were not given the -y flag),
147 1.10 thorpej * and the master is not running rpc.yppasswdd, we check
148 1.10 thorpej * to see if the user exists in the local passwd database.
149 1.10 thorpej * If so, we use it, otherwise we error out.
150 1.10 thorpej */
151 1.10 thorpej if (use_yp && yflag == 0) {
152 1.10 thorpej if (check_yppasswdd()) {
153 1.10 thorpej /*
154 1.10 thorpej * We weren't able to contact rpc.yppasswdd.
155 1.10 thorpej * Check to see if we're in the local
156 1.10 thorpej * password database. If we are, use it.
157 1.10 thorpej */
158 1.10 thorpej if (username != NULL)
159 1.10 thorpej pw = getpwnam(username);
160 1.10 thorpej else
161 1.10 thorpej pw = getpwuid(uid);
162 1.10 thorpej if (pw != NULL)
163 1.10 thorpej use_yp = 0;
164 1.10 thorpej else {
165 1.10 thorpej errx(1, "master YP server not running yppasswd daemon.\n\t%s\n",
166 1.10 thorpej "Can't change password.");
167 1.10 thorpej }
168 1.10 thorpej }
169 1.10 thorpej }
170 1.10 thorpej #endif
171 1.10 thorpej
172 1.9 thorpej #ifdef YP
173 1.9 thorpej if (use_yp)
174 1.9 thorpej Pw_error = yppw_error;
175 1.9 thorpej else
176 1.9 thorpej #endif
177 1.9 thorpej Pw_error = pw_error;
178 1.9 thorpej
179 1.2 brezak #ifdef YP
180 1.5 glass if (op == LOADENTRY && use_yp)
181 1.9 thorpej errx(1, "cannot load entry using YP.\n\tUse the -l flag to load local.");
182 1.2 brezak #endif
183 1.1 cgd
184 1.10 thorpej if (op == EDITENTRY || op == NEWSH) {
185 1.11 cjs if (username != NULL) {
186 1.10 thorpej #ifdef YP
187 1.10 thorpej if (use_yp)
188 1.10 thorpej pw = ypgetpwnam(username);
189 1.10 thorpej else
190 1.10 thorpej #endif /* YP */
191 1.10 thorpej pw = getpwnam(username);
192 1.10 thorpej if (pw == NULL)
193 1.10 thorpej errx(1, "unknown user: %s", username);
194 1.10 thorpej if (uid && uid != pw->pw_uid)
195 1.10 thorpej baduser();
196 1.10 thorpej } else {
197 1.10 thorpej #ifdef YP
198 1.9 thorpej if (use_yp)
199 1.4 deraadt pw = ypgetpwuid(uid);
200 1.9 thorpej else
201 1.10 thorpej #endif /* YP */
202 1.9 thorpej pw = getpwuid(uid);
203 1.10 thorpej if (pw == NULL)
204 1.5 glass errx(1, "unknown user: uid %u\n", uid);
205 1.1 cgd }
206 1.10 thorpej }
207 1.1 cgd
208 1.1 cgd if (op == NEWSH) {
209 1.1 cgd /* protect p_shell -- it thinks NULL is /bin/sh */
210 1.1 cgd if (!arg[0])
211 1.1 cgd usage();
212 1.1 cgd if (p_shell(arg, pw, (ENTRY *)NULL))
213 1.9 thorpej (*Pw_error)((char *)NULL, 0, 1);
214 1.1 cgd }
215 1.1 cgd
216 1.1 cgd if (op == LOADENTRY) {
217 1.1 cgd if (uid)
218 1.1 cgd baduser();
219 1.1 cgd pw = &lpw;
220 1.7 phil if (!pw_scan(arg, pw, (int *)NULL))
221 1.1 cgd exit(1);
222 1.1 cgd }
223 1.1 cgd
224 1.9 thorpej if (!use_yp) {
225 1.9 thorpej /*
226 1.9 thorpej * Get the passwd lock file and open the passwd file for
227 1.9 thorpej * reading.
228 1.9 thorpej */
229 1.9 thorpej pw_init();
230 1.9 thorpej tfd = pw_lock(0);
231 1.9 thorpej if (tfd < 0)
232 1.9 thorpej errx(1, "the passwd file is busy.");
233 1.9 thorpej pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
234 1.9 thorpej if (pfd < 0)
235 1.9 thorpej pw_error(_PATH_MASTERPASSWD, 1, 1);
236 1.9 thorpej }
237 1.1 cgd
238 1.8 jtc /* Edit the user passwd information if requested. */
239 1.1 cgd if (op == EDITENTRY) {
240 1.8 jtc dfd = mkstemp(tempname);
241 1.9 thorpej if (dfd < 0) {
242 1.9 thorpej (*Pw_error)(tempname, 1, 1);
243 1.9 thorpej }
244 1.8 jtc display(tempname, dfd, pw);
245 1.8 jtc edit(tempname, pw);
246 1.1 cgd (void)unlink(tempname);
247 1.1 cgd }
248 1.8 jtc
249 1.2 brezak #ifdef YP
250 1.4 deraadt if (use_yp) {
251 1.4 deraadt if (pw_yp(pw, uid))
252 1.9 thorpej yppw_error((char *)NULL, 0, 1);
253 1.4 deraadt else
254 1.4 deraadt exit(0);
255 1.4 deraadt }
256 1.4 deraadt else
257 1.2 brezak #endif /* YP */
258 1.8 jtc
259 1.8 jtc /* Copy the passwd file to the lock file, updating pw. */
260 1.1 cgd pw_copy(pfd, tfd, pw);
261 1.1 cgd
262 1.8 jtc /* Now finish the passwd file update. */
263 1.8 jtc if (pw_mkdb() < 0)
264 1.1 cgd pw_error((char *)NULL, 0, 1);
265 1.2 brezak
266 1.1 cgd exit(0);
267 1.1 cgd }
268 1.1 cgd
269 1.5 glass void
270 1.1 cgd baduser()
271 1.1 cgd {
272 1.5 glass
273 1.5 glass errx(1, "%s", strerror(EACCES));
274 1.1 cgd }
275 1.1 cgd
276 1.5 glass void
277 1.1 cgd usage()
278 1.1 cgd {
279 1.5 glass
280 1.2 brezak #ifdef YP
281 1.2 brezak (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [-l]%s [user]\n", use_yp?" [-y]":"");
282 1.2 brezak #else
283 1.1 cgd (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
284 1.2 brezak #endif
285 1.1 cgd exit(1);
286 1.1 cgd }
287