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