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