xsh.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Andrew Morgan (morgan@kernel.org) -- an example application
  2. * that invokes a shell, based on blank.c */
  3. #include "config.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <security/pam_appl.h>
  7. #include <security/pam_misc.h>
  8. #include <pwd.h>
  9. #include <sys/types.h>
  10. #include <unistd.h>
  11. /* ------ some local (static) functions ------- */
  12. static void bail_out(pam_handle_t *pamh,int really, int code, const char *fn)
  13. {
  14. fprintf(stderr,"==> called %s()\n got: `%s'\n", fn,
  15. pam_strerror(pamh,code));
  16. if (really && code)
  17. exit (1);
  18. }
  19. /* ------ some static data objects ------- */
  20. static struct pam_conv conv = {
  21. misc_conv,
  22. NULL
  23. };
  24. /* ------- the application itself -------- */
  25. int main(int argc, char **argv)
  26. {
  27. pam_handle_t *pamh=NULL;
  28. const void *username=NULL;
  29. const char *service="xsh";
  30. int retcode;
  31. /* did the user call with a username as an argument ?
  32. * did they also */
  33. if (argc > 3) {
  34. fprintf(stderr,"usage: %s [username [service-name]]\n",argv[0]);
  35. }
  36. if ((argc >= 2) && (argv[1][0] != '-')) {
  37. username = argv[1];
  38. }
  39. if (argc == 3) {
  40. service = argv[2];
  41. }
  42. /* initialize the Linux-PAM library */
  43. retcode = pam_start(service, username, &conv, &pamh);
  44. bail_out(pamh,1,retcode,"pam_start");
  45. /* fill in the RUSER and RHOST etc. fields */
  46. {
  47. char buffer[100];
  48. struct passwd *pw;
  49. const char *tty;
  50. pw = getpwuid(getuid());
  51. if (pw != NULL) {
  52. retcode = pam_set_item(pamh, PAM_RUSER, pw->pw_name);
  53. bail_out(pamh,1,retcode,"pam_set_item(PAM_RUSER)");
  54. }
  55. retcode = gethostname(buffer, sizeof(buffer)-1);
  56. if (retcode) {
  57. perror("failed to look up hostname");
  58. retcode = pam_end(pamh, PAM_ABORT);
  59. bail_out(pamh,1,retcode,"pam_end");
  60. }
  61. retcode = pam_set_item(pamh, PAM_RHOST, buffer);
  62. bail_out(pamh,1,retcode,"pam_set_item(PAM_RHOST)");
  63. tty = ttyname(fileno(stdin));
  64. if (tty) {
  65. retcode = pam_set_item(pamh, PAM_TTY, tty);
  66. bail_out(pamh,1,retcode,"pam_set_item(PAM_RHOST)");
  67. }
  68. }
  69. /* to avoid using goto we abuse a loop here */
  70. for (;;) {
  71. /* authenticate the user --- `0' here, could have been PAM_SILENT
  72. * | PAM_DISALLOW_NULL_AUTHTOK */
  73. retcode = pam_authenticate(pamh, 0);
  74. bail_out(pamh,0,retcode,"pam_authenticate");
  75. /* has the user proved themself valid? */
  76. if (retcode != PAM_SUCCESS) {
  77. fprintf(stderr,"%s: invalid request\n",argv[0]);
  78. break;
  79. }
  80. /* the user is valid, but should they have access at this
  81. time? */
  82. retcode = pam_acct_mgmt(pamh, 0); /* `0' could be as above */
  83. bail_out(pamh,0,retcode,"pam_acct_mgmt");
  84. if (retcode == PAM_NEW_AUTHTOK_REQD) {
  85. fprintf(stderr,"Application must request new password...\n");
  86. retcode = pam_chauthtok(pamh,PAM_CHANGE_EXPIRED_AUTHTOK);
  87. bail_out(pamh,0,retcode,"pam_chauthtok");
  88. }
  89. if (retcode != PAM_SUCCESS) {
  90. fprintf(stderr,"%s: invalid request\n",argv[0]);
  91. break;
  92. }
  93. /* `0' could be as above */
  94. retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
  95. bail_out(pamh,0,retcode,"pam_setcred");
  96. if (retcode != PAM_SUCCESS) {
  97. fprintf(stderr,"%s: problem setting user credentials\n"
  98. ,argv[0]);
  99. break;
  100. }
  101. /* open a session for the user --- `0' could be PAM_SILENT */
  102. retcode = pam_open_session(pamh,0);
  103. bail_out(pamh,0,retcode,"pam_open_session");
  104. if (retcode != PAM_SUCCESS) {
  105. fprintf(stderr,"%s: problem opening a session\n",argv[0]);
  106. break;
  107. }
  108. pam_get_item(pamh, PAM_USER, &username);
  109. fprintf(stderr,
  110. "The user [%s] has been authenticated and `logged in'\n",
  111. (const char *)username);
  112. /* this is always a really bad thing for security! */
  113. retcode = system("/bin/sh");
  114. /* close a session for the user --- `0' could be PAM_SILENT
  115. * it is possible that this pam_close_call is in another program..
  116. */
  117. retcode = pam_close_session(pamh,0);
  118. bail_out(pamh,0,retcode,"pam_close_session");
  119. if (retcode != PAM_SUCCESS) {
  120. fprintf(stderr,"%s: problem closing a session\n",argv[0]);
  121. break;
  122. }
  123. /* `0' could be as above */
  124. retcode = pam_setcred(pamh, PAM_DELETE_CRED);
  125. bail_out(pamh,0,retcode,"pam_setcred");
  126. if (retcode != PAM_SUCCESS) {
  127. fprintf(stderr,"%s: problem deleting user credentials\n"
  128. ,argv[0]);
  129. break;
  130. }
  131. break; /* don't go on for ever! */
  132. }
  133. /* close the Linux-PAM library */
  134. retcode = pam_end(pamh, PAM_SUCCESS);
  135. pamh = NULL;
  136. bail_out(pamh,1,retcode,"pam_end");
  137. return (0);
  138. }