2 * Copyright (c) 1999-2006 Apple Computer, 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
];
130 struct ypclnt
*ypclnt
;
131 const char *yp_domain
= NULL
, *yp_host
= NULL
;
133 #ifdef OPEN_DIRECTORY
134 CFStringRef username
= NULL
;
135 CFStringRef authname
= NULL
;
136 CFStringRef location
= NULL
;
138 progname
= strrchr(argv
[0], '/');
139 if (progname
) progname
++;
140 else progname
= argv
[0];
141 #endif /* OPEN_DIRECTORY */
145 #ifdef OPEN_DIRECTORY
146 while ((ch
= getopt(argc
, argv
, "a:s:l:u:")) != -1)
147 #else /* OPEN_DIRECTORY */
149 while ((ch
= getopt(argc
, argv
, "a:p:s:e:d:h:loy")) != -1)
151 while ((ch
= getopt(argc
, argv
, "a:p:s:e:")) != -1)
153 #endif /* OPEN_DIRECTORY */
163 #ifndef OPEN_DIRECTORY
185 #else /* OPEN_DIRECTORY */
187 location
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
190 authname
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
206 if (op
== EDITENTRY
|| op
== NEWSH
|| op
== NEWPW
|| op
== NEWEXP
) {
208 if ((pw
= getpwuid(uid
)) == NULL
)
209 errx(1, "unknown user: uid %lu",
212 if ((pw
= getpwnam(*argv
)) == NULL
)
213 errx(1, "unknown user: %s", *argv
);
214 #ifndef OPEN_DIRECTORY
215 if (uid
!= 0 && uid
!= pw
->pw_uid
)
220 #ifndef OPEN_DIRECTORY
221 /* Make a copy for later verification */
222 if ((pw
= pw_dup(pw
)) == NULL
||
223 (old_pw
= pw_dup(pw
)) == NULL
)
229 master_mode
= (uid
== 0);
232 * Find the user record and copy its details.
234 username
= CFStringCreateWithCString(NULL
, pw
->pw_name
, kCFStringEncodingUTF8
);
236 if (strcmp(progname
, "chsh") == 0 || op
== NEWSH
) {
237 cfprintf(stderr
, "Changing shell for %@.\n", username
);
238 } else if (strcmp(progname
, "chfn") == 0) {
239 cfprintf(stderr
, "Changing finger information for %@.\n", username
);
240 } else if (strcmp(progname
, "chpass") == 0) {
241 cfprintf(stderr
, "Changing account information for %@.\n", username
);
245 * odGetUser updates DSPath global variable, performs authentication
246 * if necessary, and extracts the attributes.
248 CFDictionaryRef attrs_orig
= NULL
;
249 CFDictionaryRef attrs
= NULL
;
250 ODRecordRef rec
= odGetUser(location
, authname
, username
, &attrs_orig
);
252 if (!rec
|| !attrs_orig
) exit(1);
253 #endif /* OPEN_DIRECTORY */
256 if (pw
!= NULL
&& (pw
->pw_fields
& _PWF_SOURCE
) == _PWF_NIS
) {
257 ypclnt
= ypclnt_new(yp_domain
, "passwd.byname", yp_host
);
258 master_mode
= (ypclnt
!= NULL
&&
259 ypclnt_connect(ypclnt
) != -1 &&
260 ypclnt_havepasswdd(ypclnt
) == 1);
264 master_mode
= (uid
== 0);
267 /* protect p_shell -- it thinks NULL is /bin/sh */
270 if (p_shell(arg
, pw
, (ENTRY
*)NULL
) == -1)
272 #ifdef OPEN_DIRECTORY
276 setrestricted(attrs_orig
);
278 for (ep
= list
; ep
->prompt
; ep
++) {
279 if (strncasecmp(ep
->prompt
, "shell", ep
->len
) == 0) {
280 if (!ep
->restricted
) {
281 CFStringRef shell
= CFStringCreateWithCString(NULL
, arg
, kCFStringEncodingUTF8
);
283 attrs
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
284 if (attrs
) CFDictionarySetValue((CFMutableDictionaryRef
)attrs
, kODAttributeTypeUserShell
, shell
);
288 warnx("shell is restricted");
297 #ifndef OPEN_DIRECTORY
299 if (uid
) /* only root can change expire */
301 if (p_expire(arg
, pw
, (ENTRY
*)NULL
) == -1)
306 if (op
== LOADENTRY
) {
309 #ifdef OPEN_DIRECTORY
310 warnx("-a is not supported for Open Directory.");
315 if (!__pw_scan(arg
, pw
, _PWSCAN_WARN
|_PWSCAN_MASTER
))
317 #endif /* OPEN_DIRECTORY */
320 #ifndef OPEN_DIRECTORY
325 if (strchr(arg
, ':'))
326 errx(1, "invalid format for password");
329 #endif /* OPEN_DIRECTORY */
331 if (op
== EDITENTRY
) {
332 #ifdef OPEN_DIRECTORY
333 setrestricted(attrs_orig
);
334 snprintf(tfn
, sizeof(tfn
), "/etc/%s.XXXXXX", progname
);
335 if ((tfd
= mkstemp(tfn
)) == -1)
337 attrs
= (CFMutableDictionaryRef
)edit(tfn
, attrs_orig
);
341 * We don't really need pw_*() here, but pw_edit() (used
342 * by edit()) is just too useful...
344 if (pw_init(NULL
, NULL
))
346 if ((tfd
= pw_tmp(-1)) == -1) {
351 pw
= edit(pw_tempname(), old_pw
);
356 * pw_equal does not check for crypted passwords, so we
357 * should do it explicitly
359 if (pw_equal(old_pw
, pw
) &&
360 strcmp(old_pw
->pw_passwd
, pw
->pw_passwd
) == 0)
361 errx(0, "user information unchanged");
362 #endif /* OPEN_DIRECTORY */
365 #ifndef OPEN_DIRECTORY
366 if (old_pw
&& !master_mode
) {
367 password
= getpass("Password: ");
368 if (strcmp(crypt(password
, old_pw
->pw_passwd
),
369 old_pw
->pw_passwd
) != 0)
376 #ifdef OPEN_DIRECTORY
377 odUpdateUser(rec
, attrs_orig
, attrs
);
379 if (rec
) CFRelease(rec
);
383 #else /* OPEN_DIRECTORY */
386 pw
->pw_fields
|= (old_pw
->pw_fields
& _PWF_SOURCE
);
387 switch (pw
->pw_fields
& _PWF_SOURCE
) {
390 ypclnt
= ypclnt_new(yp_domain
, "passwd.byname", yp_host
);
391 if (ypclnt
== NULL
||
392 ypclnt_connect(ypclnt
) == -1 ||
393 ypclnt_passwd(ypclnt
, pw
, password
) == -1) {
394 warnx("%s", ypclnt
->error
);
399 errx(0, "NIS user information updated");
403 if (pw_init(NULL
, NULL
))
405 if ((pfd
= pw_lock()) == -1) {
409 if ((tfd
= pw_tmp(-1)) == -1) {
413 if (pw_copy(pfd
, tfd
, pw
, old_pw
) == -1) {
417 if (pw_mkdb(pw
->pw_name
) == -1) {
422 errx(0, "user information updated");
425 errx(1, "unsupported passwd source");
427 #endif /* OPEN_DIRECTORY */
434 errx(1, "%s", strerror(EACCES
));
441 (void)fprintf(stderr
,
442 "usage: chpass%s %s [user]\n",
443 #ifdef OPEN_DIRECTORY
445 "[-l location] [-u authname] [-s shell]");
446 #else /* OPEN_DIRECTORY */
448 " [-d domain] [-h host]",
452 "[-a list] [-p encpass] [-s shell] [-e mmm dd yy]");
453 #endif /* OPEN_DIRECTORY */