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>
17 #include <security/pam_appl.h>
23 static int my_conv(int num_msg, struct pam_message **msg,
24 struct pam_response **response, void *appdata_ptr);
27 static struct pam_conv pam_conv = { &my_conv, NULL };
30 static pam_handle_t *pamh;
33 static const char *service = "afstest";
34 static const char *new_envstring = "GOTHEREVIATESTPAM=1";
35 static const char *new_homestring = "HOME=/tmp";
37 #if defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV)
38 #define getpassphrase getpass
43 main(int argc, char *argv[])
45 int authenticated = 0;
50 if (argc < 2 || argc > 3) {
51 fprintf(stderr, "Usage: %s [-u] <user>\n", argv[0]);
55 if (strcmp(argv[1], "-u") != 0) {
56 fprintf(stderr, "Usage: %s [-u] <user>\n", argv[0]);
59 /* service = "unixtest"; */
67 pam_start(service, username, &pam_conv, &pamh)) != PAM_SUCCESS) {
68 fprintf(stderr, "PAM error %d\n", retcode);
72 authenticated = ((retcode = pam_authenticate(pamh, 0)) == PAM_SUCCESS);
75 fprintf(stderr, "PAM couldn't authenticate you.\n");
76 pam_end(pamh, PAM_ABORT);
80 if ((retcode = pam_acct_mgmt(pamh, 0)) != PAM_SUCCESS) {
81 fprintf(stderr, "pam_acct_mgmt returned %d.\n", retcode);
82 pam_end(pamh, PAM_ABORT);
86 /* pam_open_session */
89 if ((retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
90 fprintf(stderr, "pam_setcred returned %d.\n", retcode);
91 pam_end(pamh, PAM_ABORT);
95 if ((retcode = pam_open_session(pamh, PAM_SILENT)) != PAM_SUCCESS) {
96 fprintf(stderr, "pam_open_session returned %d.\n", retcode);
97 pam_end(pamh, PAM_ABORT);
100 pam_end(pamh, PAM_SUCCESS);
102 putenv(new_envstring);
103 putenv(new_homestring);
105 printf("Type exit to back out.\n");
106 execl("/bin/csh", "/bin/csh", NULL);
111 my_conv(int num_msg, struct pam_message **msg, struct pam_response **response,
114 struct pam_message *m;
115 struct pam_response *r;
120 *response = calloc(num_msg, sizeof(struct pam_response));
121 if (*response == NULL)
129 switch (m->msg_style) {
130 case PAM_PROMPT_ECHO_OFF:
132 /* ON HP's we still read 8 chars */
134 r->resp = strdup(getpass(m->msg));
137 r->resp = strdup(getpassphrase(m->msg));
140 case PAM_PROMPT_ECHO_ON:
141 fputs(m->msg, stdout);
143 r->resp = malloc(PAM_MAX_RESP_SIZE);
144 fgets(r->resp, PAM_MAX_RESP_SIZE, stdin);
145 r->resp[PAM_MAX_RESP_SIZE - 1] = '\0';
146 p = &r->resp[strlen(r->resp) - 1];
147 while (*p == '\n' && p >= r->resp)
152 fputs(m->msg, stderr);
155 fputs(m->msg, stdout);