2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
12 #include <security/pam_appl.h>
18 static int my_conv(int num_msg, struct pam_message **msg,
19 struct pam_response **response, void *appdata_ptr);
22 static struct pam_conv pam_conv = { &my_conv, NULL };
25 static pam_handle_t *pamh;
28 static const char *service = "afstest";
29 static const char *new_envstring = "GOTHEREVIATESTPAM=1";
30 static const char *new_homestring = "HOME=/tmp";
32 #ifdef AFS_LINUX20_ENV
33 #define getpassphrase getpass
37 void main(int argc, char *argv[])
39 int authenticated = 0;
43 if (argc < 2 || argc > 3) {
44 fprintf(stderr, "Usage: %s [-u] <user>\n", argv[0]);
48 if (strcmp(argv[1], "-u") != 0) {
49 fprintf(stderr, "Usage: %s [-u] <user>\n", argv[0]);
58 if ((retcode = pam_start(service, username,
59 &pam_conv, &pamh)) != PAM_SUCCESS) {
60 fprintf(stderr, "PAM error %d\n", retcode);
64 authenticated = ((retcode = pam_authenticate(pamh, 0)) == PAM_SUCCESS);
67 fprintf(stderr, "PAM couldn't authenticate you.\n");
68 pam_end(pamh, PAM_ABORT);
72 if ((retcode = pam_acct_mgmt(pamh, 0)) != PAM_SUCCESS) {
73 fprintf(stderr, "pam_acct_mgmt returned %d.\n", retcode);
74 pam_end(pamh, PAM_ABORT);
78 /* pam_open_session */
80 if ((retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
81 fprintf(stderr, "pam_setcred returned %d.\n", retcode);
82 pam_end(pamh, PAM_ABORT);
86 pam_end(pamh, PAM_SUCCESS);
88 putenv(new_envstring);
89 putenv(new_homestring);
91 printf("Type exit to back out.\n");
92 execl("/bin/csh", "/bin/csh", NULL);
96 static int my_conv(int num_msg, struct pam_message **msg,
97 struct pam_response **response, void *appdata_ptr)
99 struct pam_message *m;
100 struct pam_response *r;
105 *response = calloc(num_msg, sizeof(struct pam_response));
106 if (*response == NULL) return PAM_BUF_ERR;
113 switch(m->msg_style) {
114 case PAM_PROMPT_ECHO_OFF:
116 /* ON HP's we still read 8 chars */
117 if (r) r->resp = strdup(getpass(m->msg));
119 if (r) r->resp = strdup(getpassphrase(m->msg));
122 case PAM_PROMPT_ECHO_ON:
123 fputs(m->msg, stdout);
125 r->resp = malloc(PAM_MAX_RESP_SIZE);
126 fgets(r->resp, PAM_MAX_RESP_SIZE, stdin);
127 r->resp[PAM_MAX_RESP_SIZE-1] = '\0';
128 p = &r->resp[strlen(r->resp)-1];
129 while (*p == '\n' && p >= r->resp) *(p--) = '\0';
133 fputs(m->msg, stderr);
136 fputs(m->msg, stdout);