]> git.saurik.com Git - apple/system_cmds.git/blob - passwd.tproj/pam_passwd.c
system_cmds-643.30.1.tar.gz
[apple/system_cmds.git] / passwd.tproj / pam_passwd.c
1 /*
2 * Copyright (c) 1999-2008 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <stdio.h>
24 #include "passwd.h"
25
26 #ifdef INFO_PAM
27
28 #include <security/pam_appl.h>
29 #include <security/openpam.h> /* for openpam_ttyconv() */
30
31 extern char* progname;
32 static pam_handle_t *pamh;
33 static struct pam_conv pamc;
34
35 int
36 pam_passwd(char* uname)
37 {
38 int retval = PAM_SUCCESS;
39
40 /* Initialize PAM. */
41 pamc.conv = &openpam_ttyconv;
42 pam_start(progname, uname, &pamc, &pamh);
43
44 /* Authenticate. */
45 if (PAM_SUCCESS != (retval = pam_authenticate(pamh, 0)))
46 goto pamerr;
47
48 /* Authorize. */
49 if (PAM_SUCCESS != (retval = pam_acct_mgmt(pamh, 0)) && PAM_NEW_AUTHTOK_REQD != retval)
50 goto pamerr;
51
52 printf("Changing password for %s.\n", uname);
53
54 /* Change the password. */
55 if (PAM_SUCCESS != (retval = pam_chauthtok(pamh, 0)))
56 goto pamerr;
57
58 /* Set the credentials. */
59 if (PAM_SUCCESS != (retval = pam_setcred(pamh, PAM_ESTABLISH_CRED)))
60 goto pamerr;
61
62 /* Open the session. */
63 if (PAM_SUCCESS != (retval = pam_open_session(pamh, 0)))
64 goto pamerr;
65
66 /* Close the session. */
67 if (PAM_SUCCESS != (retval = pam_close_session(pamh, 0)))
68 goto pamerr;
69
70 pamerr:
71 /* Print an error, if needed. */
72 if (PAM_SUCCESS != retval)
73 fprintf(stderr, "%s: %s\n", progname, pam_strerror(pamh, retval));
74
75 /* Terminate PAM. */
76 pam_end(pamh, retval);
77 return retval;
78 }
79
80 #endif /* INFO_PAM */