Initial import to Tizen
[profile/ivi/python-PyPAM.git] / examples / pamexample.c
1 /*
2   This program was contributed by Shane Watts
3   [modifications by AGM]
4
5   You need to add the following (or equivalent) to the /etc/pam.conf file.
6   # check authorization
7   check_user   auth       required     /usr/lib/security/pam_unix_auth.so
8   check_user   account    required     /usr/lib/security/pam_unix_acct.so
9  */
10
11 #include <security/pam_appl.h>
12 #include <security/pam_misc.h>
13 #include <stdio.h>
14
15 static struct pam_conv conv = {
16     misc_conv,
17     NULL
18 };
19
20 int main(int argc, char *argv[])
21 {
22     pam_handle_t *pamh=NULL;
23     int retval;
24     const char *user="nobody";
25
26     if(argc == 2) {
27         user = argv[1];
28     }
29
30     if(argc > 2) {
31         fprintf(stderr, "Usage: check_user [username]\n");
32         exit(1);
33     }
34
35     retval = pam_start("check_user", user, &conv, &pamh);
36         
37     if (retval == PAM_SUCCESS)
38         retval = pam_authenticate(pamh, 0);    /* is user really user? */
39
40     if (retval == PAM_SUCCESS)
41         retval = pam_acct_mgmt(pamh, 0);       /* permitted access? */
42
43     /* This is where we have been authorized or not. */
44
45     if (retval == PAM_SUCCESS) {
46         fprintf(stdout, "Authenticated\n");
47     } else {
48         fprintf(stdout, "Not Authenticated\n");
49     }
50
51     if (pam_end(pamh,retval) != PAM_SUCCESS) {     /* close Linux-PAM */
52         pamh = NULL;
53         fprintf(stderr, "check_user: failed to release authenticator\n");
54         exit(1);
55     }
56
57     return ( retval == PAM_SUCCESS ? 0:1 );       /* indicate success */
58 }