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