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