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 <afsconfig.h>
11 #include <afs/param.h>
15 #include <security/pam_appl.h>
21 static int my_conv(int num_msg, PAM_CONST struct pam_message **msg,
22 struct pam_response **response, void *appdata_ptr);
25 static struct pam_conv pam_conv = { &my_conv, NULL };
28 static pam_handle_t *pamh;
31 static const char *service = "afstest";
32 static const char *new_envstring = "GOTHEREVIATESTPAM=1";
33 static const char *new_homestring = "HOME=/tmp";
35 #if defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV)
36 #define getpassphrase getpass
41 main(int argc, char *argv[])
43 int authenticated = 0;
48 if (argc < 2 || argc > 3) {
49 fprintf(stderr, "Usage: %s [-u] <user>\n", argv[0]);
53 if (strcmp(argv[1], "-u") != 0) {
54 fprintf(stderr, "Usage: %s [-u] <user>\n", argv[0]);
57 /* service = "unixtest"; */
65 pam_start(service, username, &pam_conv, &pamh)) != PAM_SUCCESS) {
66 fprintf(stderr, "PAM error %d\n", retcode);
70 authenticated = ((retcode = pam_authenticate(pamh, 0)) == PAM_SUCCESS);
73 fprintf(stderr, "PAM couldn't authenticate you.\n");
74 pam_end(pamh, PAM_ABORT);
78 if ((retcode = pam_acct_mgmt(pamh, 0)) != PAM_SUCCESS) {
79 fprintf(stderr, "pam_acct_mgmt returned %d.\n", retcode);
80 pam_end(pamh, PAM_ABORT);
84 /* pam_open_session */
87 if ((retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
88 fprintf(stderr, "pam_setcred returned %d.\n", retcode);
89 pam_end(pamh, PAM_ABORT);
93 if ((retcode = pam_open_session(pamh, PAM_SILENT)) != PAM_SUCCESS) {
94 fprintf(stderr, "pam_open_session returned %d.\n", retcode);
95 pam_end(pamh, PAM_ABORT);
98 pam_end(pamh, PAM_SUCCESS);
100 putenv((char *)new_envstring);
101 putenv((char *)new_homestring);
103 printf("Type exit to back out.\n");
104 execl("/bin/csh", "/bin/csh", NULL);
109 my_conv(int num_msg, PAM_CONST struct pam_message **msg, struct pam_response **response,
112 PAM_CONST struct pam_message *m;
113 struct pam_response *r;
118 *response = calloc(num_msg, sizeof(struct pam_response));
119 if (*response == NULL)
127 switch (m->msg_style) {
128 case PAM_PROMPT_ECHO_OFF:
130 /* ON HP's we still read 8 chars */
132 r->resp = strdup(getpass(m->msg));
135 r->resp = strdup(getpassphrase(m->msg));
138 case PAM_PROMPT_ECHO_ON:
139 fputs(m->msg, stdout);
141 r->resp = malloc(PAM_MAX_RESP_SIZE);
142 fgets(r->resp, PAM_MAX_RESP_SIZE, stdin);
143 r->resp[PAM_MAX_RESP_SIZE - 1] = '\0';
144 p = &r->resp[strlen(r->resp) - 1];
145 while (*p == '\n' && p >= r->resp)
150 fputs(m->msg, stderr);
153 fputs(m->msg, stdout);