19b50d902SRodney W. Grimes /*-
2*df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1988, 1993, 1994
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
6f1d05925SDag-Erling Smørgrav * Copyright (c) 2002 Networks Associates Technology, Inc.
7f1d05925SDag-Erling Smørgrav * All rights reserved.
8f1d05925SDag-Erling Smørgrav *
9f1d05925SDag-Erling Smørgrav * Portions of this software were developed for the FreeBSD Project by
10f1d05925SDag-Erling Smørgrav * ThinkSec AS and NAI Labs, the Security Research Division of Network
11f1d05925SDag-Erling Smørgrav * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
12f1d05925SDag-Erling Smørgrav * ("CBOSS"), as part of the DARPA CHATS research program.
139b50d902SRodney W. Grimes *
149b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
159b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
169b50d902SRodney W. Grimes * are met:
179b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
189b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
199b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
209b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
219b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
229b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software
239b50d902SRodney W. Grimes * must display the following acknowledgement:
249b50d902SRodney W. Grimes * This product includes software developed by the University of
259b50d902SRodney W. Grimes * California, Berkeley and its contributors.
269b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors
279b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
289b50d902SRodney W. Grimes * without specific prior written permission.
299b50d902SRodney W. Grimes *
309b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
319b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
329b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
339b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
349b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
359b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
369b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
379b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
389b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
399b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
409b50d902SRodney W. Grimes * SUCH DAMAGE.
419b50d902SRodney W. Grimes */
429b50d902SRodney W. Grimes
439b50d902SRodney W. Grimes #include <sys/param.h>
449b50d902SRodney W. Grimes
459b50d902SRodney W. Grimes #include <err.h>
469b50d902SRodney W. Grimes #include <errno.h>
479b50d902SRodney W. Grimes #include <pwd.h>
489b50d902SRodney W. Grimes #include <stdio.h>
499b50d902SRodney W. Grimes #include <stdlib.h>
509b50d902SRodney W. Grimes #include <string.h>
519b50d902SRodney W. Grimes #include <unistd.h>
5236715722SBill Paul #ifdef YP
53f1d05925SDag-Erling Smørgrav #include <ypclnt.h>
5436715722SBill Paul #endif
559b50d902SRodney W. Grimes
56f1d05925SDag-Erling Smørgrav #include <pw_scan.h>
57f1d05925SDag-Erling Smørgrav #include <libutil.h>
58f1d05925SDag-Erling Smørgrav
599b50d902SRodney W. Grimes #include "chpass.h"
609b50d902SRodney W. Grimes
61f1d05925SDag-Erling Smørgrav int master_mode;
629b50d902SRodney W. Grimes
63f1d05925SDag-Erling Smørgrav static void baduser(void);
64f1d05925SDag-Erling Smørgrav static void usage(void);
659b50d902SRodney W. Grimes
669b50d902SRodney W. Grimes int
main(int argc,char * argv[])675ea73378SMark Murray main(int argc, char *argv[])
689b50d902SRodney W. Grimes {
69366982a5SPeter Wemm enum { NEWSH, LOADENTRY, EDITENTRY, NEWPW, NEWEXP } op;
7024c05aeeSDavid E. O'Brien struct passwd lpw, *old_pw, *pw;
719b50d902SRodney W. Grimes int ch, pfd, tfd;
72f1d05925SDag-Erling Smørgrav const char *password;
733cd1f909SConrad Meyer char *arg = NULL, *cryptpw;
74f1d05925SDag-Erling Smørgrav uid_t uid;
7592194236SBill Paul #ifdef YP
76f1d05925SDag-Erling Smørgrav struct ypclnt *ypclnt;
77f1d05925SDag-Erling Smørgrav const char *yp_domain = NULL, *yp_host = NULL;
7892194236SBill Paul #endif
799b50d902SRodney W. Grimes
8024c05aeeSDavid E. O'Brien pw = old_pw = NULL;
819b50d902SRodney W. Grimes op = EDITENTRY;
8292194236SBill Paul #ifdef YP
83f1d05925SDag-Erling Smørgrav while ((ch = getopt(argc, argv, "a:p:s:e:d:h:loy")) != -1)
8492194236SBill Paul #else
851c8af878SWarner Losh while ((ch = getopt(argc, argv, "a:p:s:e:")) != -1)
8692194236SBill Paul #endif
879b50d902SRodney W. Grimes switch (ch) {
889b50d902SRodney W. Grimes case 'a':
899b50d902SRodney W. Grimes op = LOADENTRY;
909b50d902SRodney W. Grimes arg = optarg;
919b50d902SRodney W. Grimes break;
929b50d902SRodney W. Grimes case 's':
939b50d902SRodney W. Grimes op = NEWSH;
949b50d902SRodney W. Grimes arg = optarg;
959b50d902SRodney W. Grimes break;
960e10ef2eSGarrett Wollman case 'p':
970e10ef2eSGarrett Wollman op = NEWPW;
980e10ef2eSGarrett Wollman arg = optarg;
990e10ef2eSGarrett Wollman break;
100366982a5SPeter Wemm case 'e':
101366982a5SPeter Wemm op = NEWEXP;
102366982a5SPeter Wemm arg = optarg;
103366982a5SPeter Wemm break;
10492194236SBill Paul #ifdef YP
105c2dfe9feSBill Paul case 'd':
106c2dfe9feSBill Paul yp_domain = optarg;
107f1d05925SDag-Erling Smørgrav break;
108f1d05925SDag-Erling Smørgrav case 'h':
109f1d05925SDag-Erling Smørgrav yp_host = optarg;
110c2dfe9feSBill Paul break;
11192194236SBill Paul case 'l':
112c2dfe9feSBill Paul case 'o':
113f1d05925SDag-Erling Smørgrav case 'y':
114f1d05925SDag-Erling Smørgrav /* compatibility */
11592194236SBill Paul break;
11692194236SBill Paul #endif
1179b50d902SRodney W. Grimes case '?':
1189b50d902SRodney W. Grimes default:
1199b50d902SRodney W. Grimes usage();
1209b50d902SRodney W. Grimes }
121f1d05925SDag-Erling Smørgrav
1229b50d902SRodney W. Grimes argc -= optind;
1239b50d902SRodney W. Grimes argv += optind;
1249b50d902SRodney W. Grimes
125f1d05925SDag-Erling Smørgrav if (argc > 1)
126f1d05925SDag-Erling Smørgrav usage();
127f1d05925SDag-Erling Smørgrav
1289b50d902SRodney W. Grimes uid = getuid();
1299b50d902SRodney W. Grimes
1308c44e779SCrist J. Clark if (op == EDITENTRY || op == NEWSH || op == NEWPW || op == NEWEXP) {
131f1d05925SDag-Erling Smørgrav if (argc == 0) {
132f1d05925SDag-Erling Smørgrav if ((pw = getpwuid(uid)) == NULL)
133f458f48bSMike Barcroft errx(1, "unknown user: uid %lu",
134f458f48bSMike Barcroft (unsigned long)uid);
135f1d05925SDag-Erling Smørgrav } else {
136f1d05925SDag-Erling Smørgrav if ((pw = getpwnam(*argv)) == NULL)
1379b50d902SRodney W. Grimes errx(1, "unknown user: %s", *argv);
138f1d05925SDag-Erling Smørgrav if (uid != 0 && uid != pw->pw_uid)
1399b50d902SRodney W. Grimes baduser();
1409b50d902SRodney W. Grimes }
1418c44e779SCrist J. Clark
1428c44e779SCrist J. Clark /* Make a copy for later verification */
143f1d05925SDag-Erling Smørgrav if ((pw = pw_dup(pw)) == NULL ||
144f1d05925SDag-Erling Smørgrav (old_pw = pw_dup(pw)) == NULL)
145f1d05925SDag-Erling Smørgrav err(1, "pw_dup");
1468c44e779SCrist J. Clark }
1478c44e779SCrist J. Clark
148f1d05925SDag-Erling Smørgrav #ifdef YP
1496aa4e03fSJoerg Wunsch if (pw != NULL && (pw->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
150f1d05925SDag-Erling Smørgrav ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
151f1d05925SDag-Erling Smørgrav master_mode = (ypclnt != NULL &&
152f1d05925SDag-Erling Smørgrav ypclnt_connect(ypclnt) != -1 &&
153f1d05925SDag-Erling Smørgrav ypclnt_havepasswdd(ypclnt) == 1);
154f1d05925SDag-Erling Smørgrav ypclnt_free(ypclnt);
155f1d05925SDag-Erling Smørgrav } else
156f1d05925SDag-Erling Smørgrav #endif
157f1d05925SDag-Erling Smørgrav master_mode = (uid == 0);
158f1d05925SDag-Erling Smørgrav
159049ceb63SJoerg Wunsch if (op == NEWSH) {
160049ceb63SJoerg Wunsch /* protect p_shell -- it thinks NULL is /bin/sh */
161049ceb63SJoerg Wunsch if (!arg[0])
162049ceb63SJoerg Wunsch usage();
163f1d05925SDag-Erling Smørgrav if (p_shell(arg, pw, (ENTRY *)NULL) == -1)
164f1d05925SDag-Erling Smørgrav exit(1);
165049ceb63SJoerg Wunsch }
166049ceb63SJoerg Wunsch
167366982a5SPeter Wemm if (op == NEWEXP) {
168366982a5SPeter Wemm if (uid) /* only root can change expire */
169366982a5SPeter Wemm baduser();
170f1d05925SDag-Erling Smørgrav if (p_expire(arg, pw, (ENTRY *)NULL) == -1)
171f1d05925SDag-Erling Smørgrav exit(1);
172366982a5SPeter Wemm }
173366982a5SPeter Wemm
174049ceb63SJoerg Wunsch if (op == LOADENTRY) {
175049ceb63SJoerg Wunsch if (uid)
176049ceb63SJoerg Wunsch baduser();
177049ceb63SJoerg Wunsch pw = &lpw;
178f1d05925SDag-Erling Smørgrav old_pw = NULL;
179248aee62SJacques Vidrine if (!__pw_scan(arg, pw, _PWSCAN_WARN|_PWSCAN_MASTER))
180049ceb63SJoerg Wunsch exit(1);
181049ceb63SJoerg Wunsch }
182049ceb63SJoerg Wunsch
183049ceb63SJoerg Wunsch if (op == NEWPW) {
184049ceb63SJoerg Wunsch if (uid)
185049ceb63SJoerg Wunsch baduser();
186049ceb63SJoerg Wunsch
187f1d05925SDag-Erling Smørgrav if (strchr(arg, ':'))
188049ceb63SJoerg Wunsch errx(1, "invalid format for password");
189049ceb63SJoerg Wunsch pw->pw_passwd = arg;
190049ceb63SJoerg Wunsch }
191049ceb63SJoerg Wunsch
1929b50d902SRodney W. Grimes if (op == EDITENTRY) {
193f1d05925SDag-Erling Smørgrav /*
194f1d05925SDag-Erling Smørgrav * We don't really need pw_*() here, but pw_edit() (used
195f1d05925SDag-Erling Smørgrav * by edit()) is just too useful...
196f1d05925SDag-Erling Smørgrav */
197f1d05925SDag-Erling Smørgrav if (pw_init(NULL, NULL))
198f1d05925SDag-Erling Smørgrav err(1, "pw_init()");
199f1d05925SDag-Erling Smørgrav if ((tfd = pw_tmp(-1)) == -1) {
200f1d05925SDag-Erling Smørgrav pw_fini();
201f1d05925SDag-Erling Smørgrav err(1, "pw_tmp()");
202f1d05925SDag-Erling Smørgrav }
203f1d05925SDag-Erling Smørgrav free(pw);
204f1d05925SDag-Erling Smørgrav pw = edit(pw_tempname(), old_pw);
205f1d05925SDag-Erling Smørgrav pw_fini();
206f1d05925SDag-Erling Smørgrav if (pw == NULL)
207f1d05925SDag-Erling Smørgrav err(1, "edit()");
20850789a2fSDmitry Morozovsky /*
20950789a2fSDmitry Morozovsky * pw_equal does not check for crypted passwords, so we
21050789a2fSDmitry Morozovsky * should do it explicitly
21150789a2fSDmitry Morozovsky */
21250789a2fSDmitry Morozovsky if (pw_equal(old_pw, pw) &&
21350789a2fSDmitry Morozovsky strcmp(old_pw->pw_passwd, pw->pw_passwd) == 0)
214f1d05925SDag-Erling Smørgrav errx(0, "user information unchanged");
2159b50d902SRodney W. Grimes }
2169b50d902SRodney W. Grimes
217f1d05925SDag-Erling Smørgrav if (old_pw && !master_mode) {
218f1d05925SDag-Erling Smørgrav password = getpass("Password: ");
2193cd1f909SConrad Meyer cryptpw = crypt(password, old_pw->pw_passwd);
2203cd1f909SConrad Meyer if (cryptpw == NULL || strcmp(cryptpw, old_pw->pw_passwd) != 0)
221f1d05925SDag-Erling Smørgrav baduser();
22236715722SBill Paul } else {
223f1d05925SDag-Erling Smørgrav password = "";
224f1d05925SDag-Erling Smørgrav }
2259b50d902SRodney W. Grimes
226f1d05925SDag-Erling Smørgrav if (old_pw != NULL)
227f1d05925SDag-Erling Smørgrav pw->pw_fields |= (old_pw->pw_fields & _PWF_SOURCE);
228f1d05925SDag-Erling Smørgrav switch (pw->pw_fields & _PWF_SOURCE) {
22936715722SBill Paul #ifdef YP
230f1d05925SDag-Erling Smørgrav case _PWF_NIS:
231f1d05925SDag-Erling Smørgrav ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
232fd773ca3SEitan Adler if (ypclnt == NULL) {
233fd773ca3SEitan Adler warnx("ypclnt_new failed");
234fd773ca3SEitan Adler exit(1);
235fd773ca3SEitan Adler }
236fd773ca3SEitan Adler if (ypclnt_connect(ypclnt) == -1 ||
237f1d05925SDag-Erling Smørgrav ypclnt_passwd(ypclnt, pw, password) == -1) {
238f1d05925SDag-Erling Smørgrav warnx("%s", ypclnt->error);
239f1d05925SDag-Erling Smørgrav ypclnt_free(ypclnt);
240f1d05925SDag-Erling Smørgrav exit(1);
24136715722SBill Paul }
242f1d05925SDag-Erling Smørgrav ypclnt_free(ypclnt);
243f1d05925SDag-Erling Smørgrav errx(0, "NIS user information updated");
24436715722SBill Paul #endif /* YP */
245f1d05925SDag-Erling Smørgrav case 0:
246f1d05925SDag-Erling Smørgrav case _PWF_FILES:
247f1d05925SDag-Erling Smørgrav if (pw_init(NULL, NULL))
248f1d05925SDag-Erling Smørgrav err(1, "pw_init()");
249f1d05925SDag-Erling Smørgrav if ((pfd = pw_lock()) == -1) {
250f1d05925SDag-Erling Smørgrav pw_fini();
251f1d05925SDag-Erling Smørgrav err(1, "pw_lock()");
252f1d05925SDag-Erling Smørgrav }
253f1d05925SDag-Erling Smørgrav if ((tfd = pw_tmp(-1)) == -1) {
254f1d05925SDag-Erling Smørgrav pw_fini();
255f1d05925SDag-Erling Smørgrav err(1, "pw_tmp()");
256f1d05925SDag-Erling Smørgrav }
257f1d05925SDag-Erling Smørgrav if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
258f1d05925SDag-Erling Smørgrav pw_fini();
259f1d05925SDag-Erling Smørgrav err(1, "pw_copy");
260f1d05925SDag-Erling Smørgrav }
261f1d05925SDag-Erling Smørgrav if (pw_mkdb(pw->pw_name) == -1) {
262f1d05925SDag-Erling Smørgrav pw_fini();
263f1d05925SDag-Erling Smørgrav err(1, "pw_mkdb()");
264f1d05925SDag-Erling Smørgrav }
265f1d05925SDag-Erling Smørgrav pw_fini();
266f1d05925SDag-Erling Smørgrav errx(0, "user information updated");
267f1d05925SDag-Erling Smørgrav break;
268f1d05925SDag-Erling Smørgrav default:
269f1d05925SDag-Erling Smørgrav errx(1, "unsupported passwd source");
270f1d05925SDag-Erling Smørgrav }
2719b50d902SRodney W. Grimes }
2729b50d902SRodney W. Grimes
273f1d05925SDag-Erling Smørgrav static void
baduser(void)2745ea73378SMark Murray baduser(void)
2759b50d902SRodney W. Grimes {
276f1d05925SDag-Erling Smørgrav
2779b50d902SRodney W. Grimes errx(1, "%s", strerror(EACCES));
2789b50d902SRodney W. Grimes }
2799b50d902SRodney W. Grimes
280f1d05925SDag-Erling Smørgrav static void
usage(void)2815ea73378SMark Murray usage(void)
2829b50d902SRodney W. Grimes {
2839b50d902SRodney W. Grimes
2840e10ef2eSGarrett Wollman (void)fprintf(stderr,
285612956f6SPhilippe Charnier "usage: chpass%s %s [user]\n",
28692194236SBill Paul #ifdef YP
287f1d05925SDag-Erling Smørgrav " [-d domain] [-h host]",
28892194236SBill Paul #else
289f1d05925SDag-Erling Smørgrav "",
29092194236SBill Paul #endif
291f1d05925SDag-Erling Smørgrav "[-a list] [-p encpass] [-s shell] [-e mmm dd yy]");
2929b50d902SRodney W. Grimes exit(1);
2939b50d902SRodney W. Grimes }
294