123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <security/pam_appl.h>
- #include <sys/types.h>
- #include <pwd.h>
- #include <unistd.h>
- static int
- fake_conv (int num_msg, const struct pam_message **msgm UNUSED,
- struct pam_response **response, void *appdata_ptr UNUSED)
- {
- struct pam_response *reply;
- int count;
- static int respnum = 0;
- static const char *resps[] = { "pamunix01", "TsTPAM01MAP", "TsTPAM01MAP" };
-
- if (num_msg <= 0)
- return PAM_CONV_ERR;
-
- reply = calloc (num_msg, sizeof (struct pam_response));
- if (reply == NULL)
- return PAM_CONV_ERR;
-
- for (count = 0; count < num_msg; ++count)
- {
- if (msgm[count]->msg_style == PAM_PROMPT_ECHO_OFF)
- {
- reply[count].resp_retcode = 0;
- reply[count].resp = strdup (resps[respnum % 3]);
- ++respnum;
- }
- }
-
- *response = reply;
- return PAM_SUCCESS;
- }
- static struct pam_conv conv = {
- fake_conv,
- NULL
- };
- int
- main(int argc, char *argv[])
- {
- pam_handle_t *pamh=NULL;
- const char *user="tstpamunix";
- int retval;
- int debug = 0;
- int fail;
- struct passwd *pwd;
- if (argc < 2 || (*argv[1] != 'f' &&
- *argv[1] != 'p'))
- {
- fprintf (stderr, "Need fail or pass argument.\n");
- return 2;
- }
- fail = *argv[1] == 'f';
- if (argc > 2 && strcmp (argv[2], "-d") == 0)
- debug = 1;
- pwd = getpwnam (user);
- if (pwd == NULL)
- {
- if (debug)
- fprintf (stderr, "unix4: Missing tstpamunix user.\n");
- return 2;
- }
-
- setreuid (pwd->pw_uid, -1);
- retval = pam_start("tst-pam_unix4", user, &conv, &pamh);
- if (retval != PAM_SUCCESS)
- {
- if (debug)
- fprintf (stderr, "unix4: pam_start returned %d\n", retval);
- return 1;
- }
- retval = pam_chauthtok (pamh, 0);
- if ((!fail && retval != PAM_SUCCESS) || (fail && retval == PAM_SUCCESS))
- {
- if (debug)
- fprintf (stderr, "unix4-1: pam_chauthtok returned %d\n", retval);
- return 1;
- }
- retval = pam_end (pamh,retval);
- if (retval != PAM_SUCCESS)
- {
- if (debug)
- fprintf (stderr, "unix4: pam_end returned %d\n", retval);
- return 1;
- }
- return 0;
- }
|