Fix a couple of NULL dereferences in error paths.
[dragonfly.git] / usr.bin / chpass / chpass.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
f7e3064f
PA
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.
984263bc
MD
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.
dc71b7ab 20 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
1de703da
MD
35 *
36 * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)chpass.c 8.4 (Berkeley) 4/2/94
f7e3064f 38 * $FreeBSD: src/usr.bin/chpass/chpass.c,v 1.28 2006/09/25 15:06:24 marck Exp $
984263bc
MD
39 */
40
984263bc 41#include <sys/param.h>
984263bc 42
984263bc
MD
43#include <err.h>
44#include <errno.h>
984263bc
MD
45#include <pwd.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <unistd.h>
984263bc 50#ifdef YP
f7e3064f 51#include <ypclnt.h>
984263bc
MD
52#endif
53
f7e3064f
PA
54#include <pw_scan.h>
55#include <libutil.h>
56
984263bc 57#include "chpass.h"
984263bc 58
f7e3064f 59int master_mode;
984263bc 60
f7e3064f
PA
61static void baduser(void);
62static void usage(void);
984263bc
MD
63
64int
16777b6b 65main(int argc, char **argv)
984263bc
MD
66{
67 enum { NEWSH, LOADENTRY, EDITENTRY, NEWPW, NEWEXP } op;
f7e3064f 68 struct passwd lpw, *old_pw, *pw;
984263bc 69 int ch, pfd, tfd;
f7e3064f 70 const char *password;
2020c8fe 71 char *cryptpw;
984263bc 72 char *arg = NULL;
f7e3064f 73 uid_t uid;
984263bc 74#ifdef YP
f7e3064f
PA
75 struct ypclnt *ypclnt;
76 const char *yp_domain = NULL, *yp_host = NULL;
984263bc
MD
77#endif
78
f7e3064f 79 pw = old_pw = NULL;
984263bc
MD
80 op = EDITENTRY;
81#ifdef YP
f7e3064f 82 while ((ch = getopt(argc, argv, "a:p:s:e:d:h:loy")) != -1)
984263bc
MD
83#else
84 while ((ch = getopt(argc, argv, "a:p:s:e:")) != -1)
85#endif
f7e3064f 86 switch (ch) {
984263bc
MD
87 case 'a':
88 op = LOADENTRY;
89 arg = optarg;
90 break;
91 case 's':
92 op = NEWSH;
93 arg = optarg;
94 break;
95 case 'p':
96 op = NEWPW;
97 arg = optarg;
98 break;
99 case 'e':
100 op = NEWEXP;
101 arg = optarg;
102 break;
103#ifdef YP
984263bc 104 case 'd':
f7e3064f 105 yp_domain = optarg;
984263bc 106 break;
f7e3064f
PA
107 case 'h':
108 yp_host = optarg;
984263bc 109 break;
f7e3064f 110 case 'l':
984263bc 111 case 'o':
f7e3064f
PA
112 case 'y':
113 /* compatibility */
984263bc
MD
114 break;
115#endif
116 case '?':
117 default:
118 usage();
119 }
f7e3064f 120
984263bc
MD
121 argc -= optind;
122 argv += optind;
123
f7e3064f
PA
124 if (argc > 1)
125 usage();
126
984263bc
MD
127 uid = getuid();
128
129 if (op == EDITENTRY || op == NEWSH || op == NEWPW || op == NEWEXP) {
f7e3064f
PA
130 if (argc == 0) {
131 if ((pw = getpwuid(uid)) == NULL)
984263bc
MD
132 errx(1, "unknown user: uid %lu",
133 (unsigned long)uid);
f7e3064f
PA
134 } else {
135 if ((pw = getpwnam(*argv)) == NULL)
984263bc 136 errx(1, "unknown user: %s", *argv);
f7e3064f 137 if (uid != 0 && uid != pw->pw_uid)
984263bc 138 baduser();
984263bc
MD
139 }
140
141 /* Make a copy for later verification */
f7e3064f
PA
142 if ((pw = pw_dup(pw)) == NULL ||
143 (old_pw = pw_dup(pw)) == NULL)
144 err(1, "pw_dup");
984263bc
MD
145 }
146
f7e3064f
PA
147#ifdef YP
148 if (pw != NULL && (pw->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
149 ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
150 master_mode = (ypclnt != NULL &&
151 ypclnt_connect(ypclnt) != -1 &&
152 ypclnt_havepasswdd(ypclnt) == 1);
153 ypclnt_free(ypclnt);
154 } else
155#endif
156 master_mode = (uid == 0);
157
984263bc
MD
158 if (op == NEWSH) {
159 /* protect p_shell -- it thinks NULL is /bin/sh */
160 if (!arg[0])
161 usage();
2038fb68 162 if (p_shell(arg, pw, NULL) == -1)
f7e3064f 163 exit(1);
984263bc
MD
164 }
165
166 if (op == NEWEXP) {
167 if (uid) /* only root can change expire */
168 baduser();
2038fb68 169 if (p_expire(arg, pw, NULL) == -1)
f7e3064f 170 exit(1);
984263bc
MD
171 }
172
173 if (op == LOADENTRY) {
174 if (uid)
175 baduser();
176 pw = &lpw;
f7e3064f 177 old_pw = NULL;
ed5d5720 178 if (!__pw_scan(arg, pw, _PWSCAN_WARN|_PWSCAN_MASTER))
984263bc 179 exit(1);
984263bc 180 }
984263bc
MD
181
182 if (op == NEWPW) {
183 if (uid)
184 baduser();
185
f7e3064f 186 if (strchr(arg, ':'))
984263bc 187 errx(1, "invalid format for password");
984263bc
MD
188 pw->pw_passwd = arg;
189 }
190
984263bc 191 if (op == EDITENTRY) {
f7e3064f
PA
192 /*
193 * We don't really need pw_*() here, but pw_edit() (used
194 * by edit()) is just too useful...
195 */
196 if (pw_init(NULL, NULL))
197 err(1, "pw_init()");
198 if ((tfd = pw_tmp(-1)) == -1) {
199 pw_fini();
200 err(1, "pw_tmp()");
201 }
202 free(pw);
203 pw = edit(pw_tempname(), old_pw);
204 pw_fini();
205 if (pw == NULL)
206 err(1, "edit()");
207 /*
208 * pw_equal does not check for crypted passwords, so we
209 * should do it explicitly
210 */
211 if (pw_equal(old_pw, pw) &&
212 strcmp(old_pw->pw_passwd, pw->pw_passwd) == 0)
213 errx(0, "user information unchanged");
984263bc
MD
214 }
215
f7e3064f
PA
216 if (old_pw && !master_mode) {
217 password = getpass("Password: ");
2020c8fe
SG
218 cryptpw = crypt(password, old_pw->pw_passwd);
219 if (cryptpw == NULL || strcmp(cryptpw, old_pw->pw_passwd) != 0)
f7e3064f 220 baduser();
984263bc 221 } else {
f7e3064f
PA
222 password = "";
223 }
984263bc 224
f7e3064f
PA
225 if (old_pw != NULL)
226 pw->pw_fields |= (old_pw->pw_fields & _PWF_SOURCE);
227 switch (pw->pw_fields & _PWF_SOURCE) {
984263bc 228#ifdef YP
f7e3064f
PA
229 case _PWF_NIS:
230 ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
42e46aee
SW
231 if (ypclnt == NULL) {
232 warnx("ypclnt_new failed");
233 exit(1);
234 }
235 if (ypclnt_connect(ypclnt) == -1 ||
f7e3064f
PA
236 ypclnt_passwd(ypclnt, pw, password) == -1) {
237 warnx("%s", ypclnt->error);
238 ypclnt_free(ypclnt);
239 exit(1);
240 }
241 ypclnt_free(ypclnt);
242 errx(0, "NIS user information updated");
984263bc 243#endif /* YP */
f7e3064f
PA
244 case 0:
245 case _PWF_FILES:
246 if (pw_init(NULL, NULL))
247 err(1, "pw_init()");
248 if ((pfd = pw_lock()) == -1) {
249 pw_fini();
250 err(1, "pw_lock()");
251 }
252 if ((tfd = pw_tmp(-1)) == -1) {
253 pw_fini();
254 err(1, "pw_tmp()");
255 }
256 if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
257 pw_fini();
258 err(1, "pw_copy");
259 }
260 if (pw_mkdb(pw->pw_name) == -1) {
261 pw_fini();
262 err(1, "pw_mkdb()");
263 }
264 pw_fini();
265 errx(0, "user information updated");
266 break;
267 default:
268 errx(1, "unsupported passwd source");
269 }
984263bc
MD
270}
271
f7e3064f 272static void
16777b6b 273baduser(void)
984263bc 274{
f7e3064f 275
984263bc
MD
276 errx(1, "%s", strerror(EACCES));
277}
278
f7e3064f 279static void
16777b6b 280usage(void)
984263bc
MD
281{
282
f7e3064f
PA
283 fprintf(stderr,
284 "usage: chpass%s %s [user]\n",
984263bc 285#ifdef YP
f7e3064f 286 " [-d domain] [-h host]",
984263bc 287#else
f7e3064f 288 "",
984263bc 289#endif
f7e3064f 290 "[-a list] [-p encpass] [-s shell] [-e mmm dd yy]");
984263bc
MD
291 exit(1);
292}