]>
Commit | Line | Data |
---|---|---|
ef8ad44b A |
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 | ||
25 | #include <security/pam_appl.h> | |
26 | #include <security/openpam.h> /* for openpam_ttyconv() */ | |
27 | ||
28 | extern char* progname; | |
29 | static pam_handle_t *pamh; | |
30 | static struct pam_conv pamc; | |
31 | ||
32 | int | |
33 | pam_passwd(char* uname) | |
34 | { | |
35 | int retval = PAM_SUCCESS; | |
36 | ||
37 | /* Initialize PAM. */ | |
38 | pamc.conv = &openpam_ttyconv; | |
39 | pam_start(progname, uname, &pamc, &pamh); | |
40 | ||
41 | /* Authenticate. */ | |
42 | if (PAM_SUCCESS != (retval = pam_authenticate(pamh, 0))) | |
43 | goto pamerr; | |
44 | ||
45 | /* Authorize. */ | |
46 | if (PAM_SUCCESS != (retval = pam_acct_mgmt(pamh, 0)) && PAM_NEW_AUTHTOK_REQD != retval) | |
47 | goto pamerr; | |
48 | ||
49 | printf("Changing password for %s.\n", uname); | |
50 | ||
51 | /* Change the password. */ | |
52 | if (PAM_SUCCESS != (retval = pam_chauthtok(pamh, 0))) | |
53 | goto pamerr; | |
54 | ||
55 | /* Set the credentials. */ | |
56 | if (PAM_SUCCESS != (retval = pam_setcred(pamh, PAM_ESTABLISH_CRED))) | |
57 | goto pamerr; | |
58 | ||
59 | /* Open the session. */ | |
60 | if (PAM_SUCCESS != (retval = pam_open_session(pamh, 0))) | |
61 | goto pamerr; | |
62 | ||
63 | /* Close the session. */ | |
64 | if (PAM_SUCCESS != (retval = pam_close_session(pamh, 0))) | |
65 | goto pamerr; | |
66 | ||
67 | pamerr: | |
68 | /* Print an error, if needed. */ | |
69 | if (PAM_SUCCESS != retval) | |
70 | fprintf(stderr, "%s: %s\n", progname, pam_strerror(pamh, retval)); | |
71 | ||
72 | /* Terminate PAM. */ | |
73 | pam_end(pamh, retval); | |
74 | return retval; | |
75 | } |