pamtest3.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * pamtest3.c - PAM client with a custom config directory (pam_start_confdir),
  3. * for testing pam_fprintd without touching /etc/pam.d or hitting faillock.
  4. *
  5. * Build: gcc -O2 -Wall -o pamtest3 pamtest3.c -lpam
  6. * Run: ./pamtest3 -d confdir -s fprintd-test [username]
  7. */
  8. #include <security/pam_appl.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. static int silent_conv(int n, const struct pam_message **m,
  13. struct pam_response **r, void *d)
  14. {
  15. (void)n;
  16. (void)m;
  17. (void)r;
  18. (void)d;
  19. return PAM_CONV_ERR;
  20. }
  21. int main(int argc, char **argv)
  22. {
  23. const char *service = "fptest";
  24. const char *user = getenv("USER");
  25. const char *confdir = NULL;
  26. struct pam_conv conv = { silent_conv, NULL };
  27. pam_handle_t *pamh = NULL;
  28. int ret, i;
  29. for (i = 1; i < argc; i++) {
  30. if (strcmp(argv[i], "-s") == 0 && i + 1 < argc)
  31. service = argv[++i];
  32. else if (strcmp(argv[i], "-d") == 0 && i + 1 < argc)
  33. confdir = argv[++i];
  34. else
  35. user = argv[i];
  36. }
  37. if (!user)
  38. user = "nobody";
  39. if (confdir)
  40. ret = pam_start_confdir(service, user, &conv, confdir, &pamh);
  41. else
  42. ret = pam_start(service, user, &conv, &pamh);
  43. if (ret != PAM_SUCCESS) {
  44. printf("pam_start%s: %s\n", confdir ? "_confdir" : "",
  45. pam_strerror(pamh, ret));
  46. return 2;
  47. }
  48. printf("service=%s user=%s confdir=%s\n", service, user,
  49. confdir ? confdir : "(default)");
  50. ret = pam_authenticate(pamh, 0);
  51. printf("\npam_authenticate => %d (%s)\n", ret, pam_strerror(pamh, ret));
  52. pam_end(pamh, ret);
  53. return ret == PAM_SUCCESS ? 0 : 1;
  54. }