Debian Codegraph @

modules/pam_selinux/pam_selinux_check.c

raw symbols SCIP
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 <[email protected]>
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 *
43 * All PAM code goes in this section.
44 *
45 ************************************************************************/
46
47 #include "config.h"
48
49 #include <errno.h>
50 #include <syslog.h>
51 #include <unistd.h> /* for getuid(), exit(), getopt() */
52 #include <signal.h>
53 #include <sys/wait.h> /* for wait() */
54
55 #include <security/pam_appl.h> /* for PAM functions */
56 #include <security/pam_misc.h> /* for misc_conv PAM utility function */
57
58 #include "pam_i18n.h"
59
60 #define SERVICE_NAME "pam_selinux_check" /* the name of this program for PAM */
61 /* The file containing the context to run
62 * the scripts under. */
63 int authenticate_via_pam( const char *user , pam_handle_t **pamh);
64
65 /* authenticate_via_pam()
66 *
67 * in: user
68 * out: nothing
69 * return: value condition
70 * ----- ---------
71 * 1 pam thinks that the user authenticated themselves properly
72 * 0 otherwise
73 *
74 * this function uses pam to authenticate the user running this
75 * program. this is the only function in this program that makes pam
76 * calls.
77 *
78 */
79
80 int authenticate_via_pam( const char *user , pam_handle_t **pamh) {
81
82 struct pam_conv *conv;
83 int result = 0; /* our result, set to 0 (not authenticated) by default */
84
85 /* this is a jump table of functions for pam to use when it wants to *
86 * communicate with the user. we'll be using misc_conv(), which is *
87 * provided for us via pam_misc.h. */
88 struct pam_conv pam_conversation = {
89 misc_conv,
90 NULL
91 };
92 conv = &pam_conversation;
93
94
95 /* make `p_pam_handle' a valid pam handle so we can use it when *
96 * calling pam functions. */
97 if( PAM_SUCCESS != pam_start( SERVICE_NAME,
98 user,
99 conv,
100 pamh ) ) {
101 fprintf( stderr, _("failed to initialize PAM\n") );
102 exit( -1 );
103 }
104
105 if( PAM_SUCCESS != pam_set_item(*pamh, PAM_RUSER, user))
106 {
107 fprintf( stderr, _("failed to pam_set_item()\n") );
108 exit( -1 );
109 }
110
111 /* Ask PAM to authenticate the user running this program */
112 if( PAM_SUCCESS == pam_authenticate(*pamh,0) ) {
113 if ( PAM_SUCCESS == pam_open_session(*pamh, 0) )
114 result = 1; /* user authenticated OK! */
115 }
116 return( result );
117
118 } /* authenticate_via_pam() */
119
120 int
121 main (int argc, char **argv)
122 {
123 pam_handle_t *pamh;
124 int childPid;
125
126 if (argc < 1)
127 exit (-1);
128
129 if (!authenticate_via_pam(argv[1],&pamh))
130 exit(-1);
131
132 childPid = fork();
133 if (childPid < 0) {
134 /* error in fork() */
135 fprintf(stderr, _("login: failure forking: %m"));
136 pam_close_session(pamh, 0);
137 /* We're done with PAM. Free `pam_handle'. */
138 pam_end( pamh, PAM_SUCCESS );
139 exit(0);
140 }
141 if (childPid) {
142 close(0); close(1); close(2);
143 struct sigaction sa;
144 memset(&sa,0,sizeof(sa));
145 sa.sa_handler = SIG_IGN;
146 sigaction(SIGQUIT, &sa, NULL);
147 sigaction(SIGINT, &sa, NULL);
148 while(wait(NULL) == -1 && errno == EINTR) /**/ ;
149 openlog("login", LOG_ODELAY, LOG_AUTHPRIV);
150 pam_close_session(pamh, 0);
151 /* We're done with PAM. Free `pam_handle'. */
152 pam_end( pamh, PAM_SUCCESS );
153 exit(0);
154 }
155 argv[0]=strdup ("/bin/sh");
156 argv[1]=NULL;
157
158 /* NOTE: The environment has not been sanitized. LD_PRELOAD and other fun
159 * things could be set. */
160 execv("/bin/sh",argv);
161 fprintf(stderr,"Failure\n");
162 return 0;
163 }