pam_selinux_check.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /******************************************************************************
  2. * A module for Linux-PAM that will set the default security context after login
  3. * via PAM.
  4. *
  5. * Copyright (c) 2003 Red Hat, Inc.
  6. * Written by Dan Walsh <dwalsh@redhat.com>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, and the entire permission notice in its entirety,
  13. * including the disclaimer of warranties.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote
  18. * products derived from this software without specific prior
  19. * written permission.
  20. *
  21. * ALTERNATIVELY, this product may be distributed under the terms of
  22. * the GNU Public License, in which case the provisions of the GPL are
  23. * required INSTEAD OF the above restrictions. (This clause is
  24. * necessary due to a potential bad interaction between the GPL and
  25. * the restrictions contained in a BSD-style copyright.)
  26. *
  27. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  31. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  37. * OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /************************************************************************
  41. *
  42. * All PAM code goes in this section.
  43. *
  44. ************************************************************************/
  45. #include "config.h"
  46. #include <errno.h>
  47. #include <syslog.h>
  48. #include <unistd.h> /* for getuid(), exit(), getopt() */
  49. #include <signal.h>
  50. #include <sys/wait.h> /* for wait() */
  51. #include <security/pam_appl.h> /* for PAM functions */
  52. #include <security/pam_misc.h> /* for misc_conv PAM utility function */
  53. #define SERVICE_NAME "pam_selinux_check" /* the name of this program for PAM */
  54. /* The file containing the context to run
  55. * the scripts under. */
  56. int authenticate_via_pam( const char *user , pam_handle_t **pamh);
  57. /* authenticate_via_pam()
  58. *
  59. * in: user
  60. * out: nothing
  61. * return: value condition
  62. * ----- ---------
  63. * 1 pam thinks that the user authenticated themselves properly
  64. * 0 otherwise
  65. *
  66. * this function uses pam to authenticate the user running this
  67. * program. this is the only function in this program that makes pam
  68. * calls.
  69. *
  70. */
  71. int authenticate_via_pam( const char *user , pam_handle_t **pamh) {
  72. struct pam_conv *conv;
  73. int result = 0; /* our result, set to 0 (not authenticated) by default */
  74. /* this is a jump table of functions for pam to use when it wants to *
  75. * communicate with the user. we'll be using misc_conv(), which is *
  76. * provided for us via pam_misc.h. */
  77. struct pam_conv pam_conversation = {
  78. misc_conv,
  79. NULL
  80. };
  81. conv = &pam_conversation;
  82. /* make `p_pam_handle' a valid pam handle so we can use it when *
  83. * calling pam functions. */
  84. if( PAM_SUCCESS != pam_start( SERVICE_NAME,
  85. user,
  86. conv,
  87. pamh ) ) {
  88. fprintf( stderr, _("failed to initialize PAM\n") );
  89. exit( -1 );
  90. }
  91. if( PAM_SUCCESS != pam_set_item(*pamh, PAM_RUSER, user))
  92. {
  93. fprintf( stderr, _("failed to pam_set_item()\n") );
  94. exit( -1 );
  95. }
  96. /* Ask PAM to authenticate the user running this program */
  97. if( PAM_SUCCESS == pam_authenticate(*pamh,0) ) {
  98. if ( PAM_SUCCESS == pam_open_session(*pamh, 0) )
  99. result = 1; /* user authenticated OK! */
  100. }
  101. return( result );
  102. } /* authenticate_via_pam() */
  103. int
  104. main (int argc, char **argv)
  105. {
  106. pam_handle_t *pamh;
  107. int childPid;
  108. if (argc < 1)
  109. exit (-1);
  110. if (!authenticate_via_pam(argv[1],&pamh))
  111. exit(-1);
  112. childPid = fork();
  113. if (childPid < 0) {
  114. /* error in fork() */
  115. fprintf(stderr, _("login: failure forking: %m"));
  116. pam_close_session(pamh, 0);
  117. /* We're done with PAM. Free `pam_handle'. */
  118. pam_end( pamh, PAM_SUCCESS );
  119. exit(0);
  120. }
  121. if (childPid) {
  122. close(0); close(1); close(2);
  123. struct sigaction sa;
  124. memset(&sa,0,sizeof(sa));
  125. sa.sa_handler = SIG_IGN;
  126. sigaction(SIGQUIT, &sa, NULL);
  127. sigaction(SIGINT, &sa, NULL);
  128. while(wait(NULL) == -1 && errno == EINTR) /**/ ;
  129. openlog("login", LOG_ODELAY, LOG_AUTHPRIV);
  130. pam_close_session(pamh, 0);
  131. /* We're done with PAM. Free `pam_handle'. */
  132. pam_end( pamh, PAM_SUCCESS );
  133. exit(0);
  134. }
  135. argv[0]=strdup ("/bin/sh");
  136. argv[1]=NULL;
  137. /* NOTE: The environment has not been sanitized. LD_PRELOAD and other fun
  138. * things could be set. */
  139. execv("/bin/sh",argv);
  140. fprintf(stderr,"Failure\n");
  141. return 0;
  142. }