check_user.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. $Id$
  3. This program was contributed by Shane Watts <shane@icarus.bofh.asn.au>
  4. slight modifications by AGM.
  5. You need to add the following (or equivalent) to the /etc/pam.conf file.
  6. # check authorization
  7. check auth required pam_unix_auth.so
  8. check account required pam_unix_acct.so
  9. */
  10. #include <security/pam_appl.h>
  11. #include <security/pam_misc.h>
  12. #include <stdio.h>
  13. static struct pam_conv conv = {
  14. misc_conv,
  15. NULL
  16. };
  17. int main(int argc, char *argv[])
  18. {
  19. pam_handle_t *pamh=NULL;
  20. int retval;
  21. const char *user="nobody";
  22. if(argc == 2) {
  23. user = argv[1];
  24. }
  25. if(argc > 2) {
  26. fprintf(stderr, "Usage: check_user [username]\n");
  27. exit(1);
  28. }
  29. retval = pam_start("check", user, &conv, &pamh);
  30. if (retval == PAM_SUCCESS)
  31. retval = pam_authenticate(pamh, 0); /* is user really user? */
  32. if (retval == PAM_SUCCESS)
  33. retval = pam_acct_mgmt(pamh, 0); /* permitted access? */
  34. /* This is where we have been authorized or not. */
  35. if (retval == PAM_SUCCESS) {
  36. fprintf(stdout, "Authenticated\n");
  37. } else {
  38. fprintf(stdout, "Not Authenticated\n");
  39. }
  40. if (pam_end(pamh,retval) != PAM_SUCCESS) { /* close Linux-PAM */
  41. pamh = NULL;
  42. fprintf(stderr, "check_user: failed to release authenticator\n");
  43. exit(1);
  44. }
  45. return ( retval == PAM_SUCCESS ? 0:1 ); /* indicate success */
  46. }