5 /* Andrew Morgan (morgan@kernel.org) -- an example application
6 * that invokes a shell, based on blank.c */
11 #include <security/pam_appl.h>
12 #include <security/pam_misc.h>
14 #include <security/_pam_aconf.h>
17 #include <sys/types.h>
20 /* ------ some local (static) functions ------- */
22 static void bail_out(pam_handle_t *pamh,int really, int code, const char *fn)
24 fprintf(stderr,"==> called %s()\n got: `%s'\n", fn,
25 pam_strerror(pamh,code));
30 /* ------ some static data objects ------- */
32 static struct pam_conv conv = {
37 /* ------- the application itself -------- */
39 int main(int argc, char **argv)
41 pam_handle_t *pamh=NULL;
42 const char *username=NULL;
43 const char *service="xsh";
46 /* did the user call with a username as an argument ?
50 fprintf(stderr,"usage: %s [username [service-name]]\n",argv[0]);
59 /* initialize the Linux-PAM library */
60 retcode = pam_start(service, username, &conv, &pamh);
61 bail_out(pamh,1,retcode,"pam_start");
63 /* fill in the RUSER and RHOST fields */
68 pw = getpwuid(getuid());
70 retcode = pam_set_item(pamh, PAM_RUSER, pw->pw_name);
71 bail_out(pamh,1,retcode,"pam_set_item(PAM_RUSER)");
73 retcode = gethostname(buffer, sizeof(buffer)-1);
75 perror("failed to look up hostname");
76 retcode = pam_end(pamh, PAM_ABORT);
77 bail_out(pamh,1,retcode,"pam_end");
79 retcode = pam_set_item(pamh, PAM_RHOST, buffer);
80 bail_out(pamh,1,retcode,"pam_set_item(PAM_RHOST)");
83 /* to avoid using goto we abuse a loop here */
85 /* authenticate the user --- `0' here, could have been PAM_SILENT
86 * | PAM_DISALLOW_NULL_AUTHTOK */
88 retcode = pam_authenticate(pamh, 0);
89 bail_out(pamh,0,retcode,"pam_authenticate");
91 /* has the user proved themself valid? */
92 if (retcode != PAM_SUCCESS) {
93 fprintf(stderr,"%s: invalid request\n",argv[0]);
97 /* the user is valid, but should they have access at this
100 retcode = pam_acct_mgmt(pamh, 0); /* `0' could be as above */
101 bail_out(pamh,0,retcode,"pam_acct_mgmt");
103 if (retcode == PAM_NEW_AUTHTOK_REQD) {
104 fprintf(stderr,"Application must request new password...\n");
105 retcode = pam_chauthtok(pamh,PAM_CHANGE_EXPIRED_AUTHTOK);
106 bail_out(pamh,0,retcode,"pam_chauthtok");
109 if (retcode != PAM_SUCCESS) {
110 fprintf(stderr,"%s: invalid request\n",argv[0]);
114 /* `0' could be as above */
115 retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
116 bail_out(pamh,0,retcode,"pam_setcred");
118 if (retcode != PAM_SUCCESS) {
119 fprintf(stderr,"%s: problem setting user credentials\n"
124 /* open a session for the user --- `0' could be PAM_SILENT */
125 retcode = pam_open_session(pamh,0);
126 bail_out(pamh,0,retcode,"pam_open_session");
127 if (retcode != PAM_SUCCESS) {
128 fprintf(stderr,"%s: problem opening a session\n",argv[0]);
132 pam_get_item(pamh, PAM_USER, (const void **) &username);
134 "The user [%s] has been authenticated and `logged in'\n",
137 /* this is always a really bad thing for security! */
140 /* close a session for the user --- `0' could be PAM_SILENT
141 * it is possible that this pam_close_call is in another program..
144 retcode = pam_close_session(pamh,0);
145 bail_out(pamh,0,retcode,"pam_close_session");
146 if (retcode != PAM_SUCCESS) {
147 fprintf(stderr,"%s: problem closing a session\n",argv[0]);
151 /* `0' could be as above */
152 retcode = pam_setcred(pamh, PAM_DELETE_CRED);
153 bail_out(pamh,0,retcode,"pam_setcred");
154 if (retcode != PAM_SUCCESS) {
155 fprintf(stderr,"%s: problem deleting user credentials\n"
160 break; /* don't go on for ever! */
163 /* close the Linux-PAM library */
164 retcode = pam_end(pamh, PAM_SUCCESS);
166 bail_out(pamh,1,retcode,"pam_end");