2 * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1988, 1993, 1994
25 * The Regents of the University of California. All rights reserved.
26 * Copyright (c) 2002 Networks Associates Technology, Inc.
27 * All rights reserved.
29 * Portions of this software were developed for the FreeBSD Project by
30 * ThinkSec AS and NAI Labs, the Security Research Division of Network
31 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
32 * ("CBOSS"), as part of the DARPA CHATS research program.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 static const char copyright
[] =
67 "@(#) Copyright (c) 1988, 1993, 1994\n\
68 The Regents of the University of California. All rights reserved.\n";
72 static char sccsid
[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94";
75 #include <sys/cdefs.h>
76 __FBSDID("$FreeBSD: src/usr.bin/chpass/chpass.c,v 1.27.8.1 2006/09/29 06:13:20 marck Exp $");
79 #include <sys/param.h>
81 #include <sys/signal.h>
83 #include <sys/resource.h>
96 #ifndef OPEN_DIRECTORY
105 #ifdef OPEN_DIRECTORY
106 #include "open_directory.h"
107 char *progname
= NULL
;
108 CFStringRef DSPath
= NULL
;
109 #endif /* OPEN_DIRECTORY */
111 static void baduser(void);
112 static void usage(void);
115 main(int argc
, char *argv
[])
117 enum { NEWSH
, LOADENTRY
, EDITENTRY
, NEWPW
, NEWEXP
} op
;
118 #ifndef OPEN_DIRECTORY
119 struct passwd lpw
, *old_pw
, *pw
;
121 const char *password
;
123 struct passwd
*old_pw
, *pw
;
125 char tfn
[MAXPATHLEN
];
131 struct ypclnt
*ypclnt
;
132 const char *yp_domain
= NULL
, *yp_host
= NULL
;
134 #ifdef OPEN_DIRECTORY
135 CFStringRef username
= NULL
;
136 CFStringRef authname
= NULL
;
137 CFStringRef location
= NULL
;
139 progname
= strrchr(argv
[0], '/');
140 if (progname
) progname
++;
141 else progname
= argv
[0];
142 #endif /* OPEN_DIRECTORY */
146 #ifdef OPEN_DIRECTORY
147 while ((ch
= getopt(argc
, argv
, "a:s:l:u:")) != -1)
148 #else /* OPEN_DIRECTORY */
150 while ((ch
= getopt(argc
, argv
, "a:p:s:e:d:h:loy")) != -1)
152 while ((ch
= getopt(argc
, argv
, "a:p:s:e:")) != -1)
154 #endif /* OPEN_DIRECTORY */
164 #ifndef OPEN_DIRECTORY
186 #else /* OPEN_DIRECTORY */
188 location
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
191 authname
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
207 if (op
== EDITENTRY
|| op
== NEWSH
|| op
== NEWPW
|| op
== NEWEXP
) {
209 if ((pw
= getpwuid(uid
)) == NULL
)
210 errx(1, "unknown user: uid %lu",
213 if ((pw
= getpwnam(*argv
)) == NULL
)
214 errx(1, "unknown user: %s", *argv
);
215 #ifndef OPEN_DIRECTORY
216 if (uid
!= 0 && uid
!= pw
->pw_uid
)
221 #ifndef OPEN_DIRECTORY
222 /* Make a copy for later verification */
223 if ((pw
= pw_dup(pw
)) == NULL
||
224 (old_pw
= pw_dup(pw
)) == NULL
)
230 master_mode
= (uid
== 0);
233 * Find the user record and copy its details.
235 username
= CFStringCreateWithCString(NULL
, pw
->pw_name
, kCFStringEncodingUTF8
);
237 if (strcmp(progname
, "chsh") == 0 || op
== NEWSH
) {
238 cfprintf(stderr
, "Changing shell for %@.\n", username
);
239 } else if (strcmp(progname
, "chfn") == 0) {
240 cfprintf(stderr
, "Changing finger information for %@.\n", username
);
241 } else if (strcmp(progname
, "chpass") == 0) {
242 cfprintf(stderr
, "Changing account information for %@.\n", username
);
246 * odGetUser updates DSPath global variable, performs authentication
247 * if necessary, and extracts the attributes.
249 CFDictionaryRef attrs_orig
= NULL
;
250 CFDictionaryRef attrs
= NULL
;
251 ODRecordRef rec
= odGetUser(location
, authname
, username
, &attrs_orig
);
253 if (!rec
|| !attrs_orig
) exit(1);
254 #endif /* OPEN_DIRECTORY */
257 if (pw
!= NULL
&& (pw
->pw_fields
& _PWF_SOURCE
) == _PWF_NIS
) {
258 ypclnt
= ypclnt_new(yp_domain
, "passwd.byname", yp_host
);
259 master_mode
= (ypclnt
!= NULL
&&
260 ypclnt_connect(ypclnt
) != -1 &&
261 ypclnt_havepasswdd(ypclnt
) == 1);
265 master_mode
= (uid
== 0);
268 /* protect p_shell -- it thinks NULL is /bin/sh */
271 if (p_shell(arg
, pw
, (ENTRY
*)NULL
) == -1)
273 #ifdef OPEN_DIRECTORY
277 setrestricted(attrs_orig
);
279 for (ep
= list
; ep
->prompt
; ep
++) {
280 if (strncasecmp(ep
->prompt
, "shell", ep
->len
) == 0) {
281 if (!ep
->restricted
) {
282 CFStringRef shell
= CFStringCreateWithCString(NULL
, arg
, kCFStringEncodingUTF8
);
284 attrs
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
285 if (attrs
) CFDictionarySetValue((CFMutableDictionaryRef
)attrs
, kODAttributeTypeUserShell
, shell
);
289 warnx("shell is restricted");
298 #ifndef OPEN_DIRECTORY
300 if (uid
) /* only root can change expire */
302 if (p_expire(arg
, pw
, (ENTRY
*)NULL
) == -1)
307 if (op
== LOADENTRY
) {
310 #ifdef OPEN_DIRECTORY
311 warnx("-a is not supported for Open Directory.");
316 if (!__pw_scan(arg
, pw
, _PWSCAN_WARN
|_PWSCAN_MASTER
))
318 #endif /* OPEN_DIRECTORY */
321 #ifndef OPEN_DIRECTORY
326 if (strchr(arg
, ':'))
327 errx(1, "invalid format for password");
330 #endif /* OPEN_DIRECTORY */
332 if (op
== EDITENTRY
) {
333 #ifdef OPEN_DIRECTORY
334 setrestricted(attrs_orig
);
335 tmpdir
= getenv("TMPDIR");
337 tmpdir
= P_tmpdir
; // defined in the system headers, defaults to /tmp
338 snprintf(tfn
, sizeof(tfn
), "%s/%s.XXXXXX", tmpdir
, progname
);
339 if ((tfd
= mkstemp(tfn
)) == -1)
341 attrs
= (CFMutableDictionaryRef
)edit(tfn
, attrs_orig
);
345 * We don't really need pw_*() here, but pw_edit() (used
346 * by edit()) is just too useful...
348 if (pw_init(NULL
, NULL
))
350 if ((tfd
= pw_tmp(-1)) == -1) {
355 pw
= edit(pw_tempname(), old_pw
);
360 * pw_equal does not check for crypted passwords, so we
361 * should do it explicitly
363 if (pw_equal(old_pw
, pw
) &&
364 strcmp(old_pw
->pw_passwd
, pw
->pw_passwd
) == 0)
365 errx(0, "user information unchanged");
366 #endif /* OPEN_DIRECTORY */
369 #ifndef OPEN_DIRECTORY
370 if (old_pw
&& !master_mode
) {
371 password
= getpass("Password: ");
372 if (strcmp(crypt(password
, old_pw
->pw_passwd
),
373 old_pw
->pw_passwd
) != 0)
380 #ifdef OPEN_DIRECTORY
381 odUpdateUser(rec
, attrs_orig
, attrs
);
383 if (rec
) CFRelease(rec
);
387 #else /* OPEN_DIRECTORY */
390 pw
->pw_fields
|= (old_pw
->pw_fields
& _PWF_SOURCE
);
391 switch (pw
->pw_fields
& _PWF_SOURCE
) {
394 ypclnt
= ypclnt_new(yp_domain
, "passwd.byname", yp_host
);
395 if (ypclnt
== NULL
||
396 ypclnt_connect(ypclnt
) == -1 ||
397 ypclnt_passwd(ypclnt
, pw
, password
) == -1) {
398 warnx("%s", ypclnt
->error
);
403 errx(0, "NIS user information updated");
407 if (pw_init(NULL
, NULL
))
409 if ((pfd
= pw_lock()) == -1) {
413 if ((tfd
= pw_tmp(-1)) == -1) {
417 if (pw_copy(pfd
, tfd
, pw
, old_pw
) == -1) {
421 if (pw_mkdb(pw
->pw_name
) == -1) {
426 errx(0, "user information updated");
429 errx(1, "unsupported passwd source");
431 #endif /* OPEN_DIRECTORY */
437 errx(1, "%s", strerror(EACCES
));
443 (void)fprintf(stderr
,
444 "usage: chpass%s %s [user]\n",
445 #ifdef OPEN_DIRECTORY
447 "[-l location] [-u authname] [-s shell]");
448 #else /* OPEN_DIRECTORY */
450 " [-d domain] [-h host]",
454 "[-a list] [-p encpass] [-s shell] [-e mmm dd yy]");
455 #endif /* OPEN_DIRECTORY */