blank.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * $Id$
  3. */
  4. /* Andrew Morgan (morgan@parc.power.net) -- a self contained `blank'
  5. * application
  6. *
  7. * I am not very proud of this code. It makes use of a possibly ill-
  8. * defined pamh pointer to call pam_strerror() with. The reason that
  9. * I was sloppy with this is historical (pam_strerror, prior to 0.59,
  10. * did not require a pamh argument) and if this program is used as a
  11. * model for anything, I should wish that you will take this error into
  12. * account.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <security/pam_appl.h>
  17. #include <security/pam_misc.h>
  18. /* ------ some local (static) functions ------- */
  19. static void bail_out(pam_handle_t *pamh, int really, int code, const char *fn)
  20. {
  21. fprintf(stderr,"==> called %s()\n got: `%s'\n", fn,
  22. pam_strerror(pamh, code));
  23. if (really && code)
  24. exit (1);
  25. }
  26. /* ------ some static data objects ------- */
  27. static struct pam_conv conv = {
  28. misc_conv,
  29. NULL
  30. };
  31. /* ------- the application itself -------- */
  32. int main(int argc, char **argv)
  33. {
  34. pam_handle_t *pamh=NULL;
  35. char *username=NULL;
  36. int retcode;
  37. /* did the user call with a username as an argument ? */
  38. if (argc > 2) {
  39. fprintf(stderr,"usage: %s [username]\n",argv[0]);
  40. } else if (argc == 2) {
  41. username = argv[1];
  42. }
  43. /* initialize the Linux-PAM library */
  44. retcode = pam_start("blank", username, &conv, &pamh);
  45. bail_out(pamh,1,retcode,"pam_start");
  46. /* test the environment stuff */
  47. {
  48. #define MAXENV 15
  49. const char *greek[MAXENV] = {
  50. "a=alpha", "b=beta", "c=gamma", "d=delta", "e=epsilon",
  51. "f=phi", "g=psi", "h=eta", "i=iota", "j=mu", "k=nu",
  52. "l=zeta", "h=", "d", "k=xi"
  53. };
  54. char **env;
  55. int i;
  56. for (i=0; i<MAXENV; ++i) {
  57. retcode = pam_putenv(pamh,greek[i]);
  58. bail_out(pamh,0,retcode,"pam_putenv");
  59. }
  60. env = pam_getenvlist(pamh);
  61. if (env)
  62. env = pam_misc_drop_env(env);
  63. else
  64. fprintf(stderr,"???\n");
  65. fprintf(stderr,"a test: c=[%s], j=[%s]\n"
  66. , pam_getenv(pamh, "c"), pam_getenv(pamh, "j"));
  67. }
  68. /* to avoid using goto we abuse a loop here */
  69. for (;;) {
  70. /* authenticate the user --- `0' here, could have been PAM_SILENT
  71. * | PAM_DISALLOW_NULL_AUTHTOK */
  72. retcode = pam_authenticate(pamh, 0);
  73. bail_out(pamh,0,retcode,"pam_authenticate");
  74. /* has the user proved themself valid? */
  75. if (retcode != PAM_SUCCESS) {
  76. fprintf(stderr,"%s: invalid request\n",argv[0]);
  77. break;
  78. }
  79. /* the user is valid, but should they have access at this
  80. time? */
  81. retcode = pam_acct_mgmt(pamh, 0); /* `0' could be as above */
  82. bail_out(pamh,0,retcode,"pam_acct_mgmt");
  83. if (retcode == PAM_NEW_AUTHTOK_REQD) {
  84. fprintf(stderr,"Application must request new password...\n");
  85. retcode = pam_chauthtok(pamh,PAM_CHANGE_EXPIRED_AUTHTOK);
  86. bail_out(pamh,0,retcode,"pam_chauthtok");
  87. }
  88. if (retcode != PAM_SUCCESS) {
  89. fprintf(stderr,"%s: invalid request\n",argv[0]);
  90. break;
  91. }
  92. /* `0' could be as above */
  93. retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
  94. bail_out(pamh,0,retcode,"pam_setcred1");
  95. if (retcode != PAM_SUCCESS) {
  96. fprintf(stderr,"%s: problem setting user credentials\n"
  97. ,argv[0]);
  98. break;
  99. }
  100. /* open a session for the user --- `0' could be PAM_SILENT */
  101. retcode = pam_open_session(pamh,0);
  102. bail_out(pamh,0,retcode,"pam_open_session");
  103. if (retcode != PAM_SUCCESS) {
  104. fprintf(stderr,"%s: problem opening a session\n",argv[0]);
  105. break;
  106. }
  107. fprintf(stderr,"The user has been authenticated and `logged in'\n");
  108. /* close a session for the user --- `0' could be PAM_SILENT
  109. * it is possible that this pam_close_call is in another program..
  110. */
  111. retcode = pam_close_session(pamh,0);
  112. bail_out(pamh,0,retcode,"pam_close_session");
  113. if (retcode != PAM_SUCCESS) {
  114. fprintf(stderr,"%s: problem closing a session\n",argv[0]);
  115. break;
  116. }
  117. retcode = pam_setcred(pamh, PAM_DELETE_CRED);
  118. bail_out(pamh,0,retcode,"pam_setcred2");
  119. break; /* don't go on for ever! */
  120. }
  121. /* close the Linux-PAM library */
  122. retcode = pam_end(pamh, PAM_SUCCESS);
  123. pamh = NULL;
  124. bail_out(pamh,1,retcode,"pam_end");
  125. exit(0);
  126. }