]> git.cameronkatri.com Git - pw-darwin.git/blob - chpass/chpass.c
Don't try to dereference a NULL pw pointer. This would happen when
[pw-darwin.git] / chpass / chpass.c
1 /*-
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * All rights reserved.
6 *
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifndef lint
42 static const char copyright[] =
43 "@(#) Copyright (c) 1988, 1993, 1994\n\
44 The Regents of the University of California. All rights reserved.\n";
45 #endif /* not lint */
46
47 #if 0
48 #ifndef lint
49 static char sccsid[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94";
50 #endif /* not lint */
51 #endif
52
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55
56 #include <sys/param.h>
57 #include <sys/stat.h>
58 #include <sys/signal.h>
59 #include <sys/time.h>
60 #include <sys/resource.h>
61
62 #include <ctype.h>
63 #include <err.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <pwd.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71 #ifdef YP
72 #include <ypclnt.h>
73 #endif
74
75 #include <pw_scan.h>
76 #include <libutil.h>
77
78 #include "chpass.h"
79
80 int master_mode;
81
82 static void baduser(void);
83 static void usage(void);
84
85 char localhost[] = "localhost";
86
87 int
88 main(int argc, char *argv[])
89 {
90 enum { NEWSH, LOADENTRY, EDITENTRY, NEWPW, NEWEXP } op;
91 struct passwd *pw = NULL, lpw, *old_pw;
92 int ch, pfd, tfd;
93 const char *password;
94 char *arg = NULL;
95 uid_t uid;
96 #ifdef YP
97 struct ypclnt *ypclnt;
98 const char *yp_domain = NULL, *yp_host = NULL;
99 #endif
100
101 op = EDITENTRY;
102 #ifdef YP
103 while ((ch = getopt(argc, argv, "a:p:s:e:d:h:loy")) != -1)
104 #else
105 while ((ch = getopt(argc, argv, "a:p:s:e:")) != -1)
106 #endif
107 switch (ch) {
108 case 'a':
109 op = LOADENTRY;
110 arg = optarg;
111 break;
112 case 's':
113 op = NEWSH;
114 arg = optarg;
115 break;
116 case 'p':
117 op = NEWPW;
118 arg = optarg;
119 break;
120 case 'e':
121 op = NEWEXP;
122 arg = optarg;
123 break;
124 #ifdef YP
125 case 'd':
126 yp_domain = optarg;
127 break;
128 case 'h':
129 yp_host = optarg;
130 break;
131 case 'l':
132 case 'o':
133 case 'y':
134 /* compatibility */
135 break;
136 #endif
137 case '?':
138 default:
139 usage();
140 }
141
142 argc -= optind;
143 argv += optind;
144
145 if (argc > 1)
146 usage();
147
148 uid = getuid();
149
150 if (op == EDITENTRY || op == NEWSH || op == NEWPW || op == NEWEXP) {
151 if (argc == 0) {
152 if ((pw = getpwuid(uid)) == NULL)
153 errx(1, "unknown user: uid %lu",
154 (unsigned long)uid);
155 } else {
156 if ((pw = getpwnam(*argv)) == NULL)
157 errx(1, "unknown user: %s", *argv);
158 if (uid != 0 && uid != pw->pw_uid)
159 baduser();
160 }
161
162 /* Make a copy for later verification */
163 if ((pw = pw_dup(pw)) == NULL ||
164 (old_pw = pw_dup(pw)) == NULL)
165 err(1, "pw_dup");
166 }
167
168 #ifdef YP
169 if (pw != NULL && (pw->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
170 ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
171 master_mode = (ypclnt != NULL &&
172 ypclnt_connect(ypclnt) != -1 &&
173 ypclnt_havepasswdd(ypclnt) == 1);
174 ypclnt_free(ypclnt);
175 } else
176 #endif
177 master_mode = (uid == 0);
178
179 if (op == NEWSH) {
180 /* protect p_shell -- it thinks NULL is /bin/sh */
181 if (!arg[0])
182 usage();
183 if (p_shell(arg, pw, (ENTRY *)NULL) == -1)
184 exit(1);
185 }
186
187 if (op == NEWEXP) {
188 if (uid) /* only root can change expire */
189 baduser();
190 if (p_expire(arg, pw, (ENTRY *)NULL) == -1)
191 exit(1);
192 }
193
194 if (op == LOADENTRY) {
195 if (uid)
196 baduser();
197 pw = &lpw;
198 old_pw = NULL;
199 if (!__pw_scan(arg, pw, _PWSCAN_WARN|_PWSCAN_MASTER))
200 exit(1);
201 }
202
203 if (op == NEWPW) {
204 if (uid)
205 baduser();
206
207 if (strchr(arg, ':'))
208 errx(1, "invalid format for password");
209 pw->pw_passwd = arg;
210 }
211
212 if (op == EDITENTRY) {
213 /*
214 * We don't really need pw_*() here, but pw_edit() (used
215 * by edit()) is just too useful...
216 */
217 if (pw_init(NULL, NULL))
218 err(1, "pw_init()");
219 if ((tfd = pw_tmp(-1)) == -1) {
220 pw_fini();
221 err(1, "pw_tmp()");
222 }
223 free(pw);
224 pw = edit(pw_tempname(), old_pw);
225 pw_fini();
226 if (pw == NULL)
227 err(1, "edit()");
228 if (pw_equal(old_pw, pw))
229 errx(0, "user information unchanged");
230 }
231
232 if (old_pw && !master_mode) {
233 password = getpass("Password: ");
234 if (strcmp(crypt(password, old_pw->pw_passwd),
235 old_pw->pw_passwd) != 0)
236 baduser();
237 } else {
238 password = "";
239 }
240
241 if (old_pw != NULL)
242 pw->pw_fields |= (old_pw->pw_fields & _PWF_SOURCE);
243 switch (pw->pw_fields & _PWF_SOURCE) {
244 #ifdef YP
245 case _PWF_NIS:
246 ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
247 if (ypclnt == NULL ||
248 ypclnt_connect(ypclnt) == -1 ||
249 ypclnt_passwd(ypclnt, pw, password) == -1) {
250 warnx("%s", ypclnt->error);
251 ypclnt_free(ypclnt);
252 exit(1);
253 }
254 ypclnt_free(ypclnt);
255 errx(0, "NIS user information updated");
256 #endif /* YP */
257 case 0:
258 case _PWF_FILES:
259 if (pw_init(NULL, NULL))
260 err(1, "pw_init()");
261 if ((pfd = pw_lock()) == -1) {
262 pw_fini();
263 err(1, "pw_lock()");
264 }
265 if ((tfd = pw_tmp(-1)) == -1) {
266 pw_fini();
267 err(1, "pw_tmp()");
268 }
269 if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
270 pw_fini();
271 err(1, "pw_copy");
272 }
273 if (pw_mkdb(pw->pw_name) == -1) {
274 pw_fini();
275 err(1, "pw_mkdb()");
276 }
277 pw_fini();
278 errx(0, "user information updated");
279 break;
280 default:
281 errx(1, "unsupported passwd source");
282 }
283 }
284
285 static void
286 baduser(void)
287 {
288
289 errx(1, "%s", strerror(EACCES));
290 }
291
292 static void
293 usage(void)
294 {
295
296 (void)fprintf(stderr,
297 "Usage: chpass%s %s [user]\n",
298 #ifdef YP
299 " [-d domain] [-h host]",
300 #else
301 "",
302 #endif
303 "[-a list] [-p encpass] [-s shell] [-e mmm dd yy]");
304 exit(1);
305 }