1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
--- shadow-4.0.3/src/su.c.orig 2002-10-20 16:43:21.000000000 +0200
+++ shadow-4.0.3/src/su.c 2002-10-20 16:50:57.000000000 +0200
@@ -134,6 +134,108 @@
exit (1);
}
+#ifdef USE_PAM
+static int caught=0;
+
+/* Signal handler for parent process later */
+static void su_catch_sig(int sig)
+{
+ ++caught;
+}
+
+/* This I ripped out of su.c from sh-utils after the Mandrake pam patch
+ * have been applied. Some work was needed to get it integrated into
+ * su.c from shadow.
+ */
+static void run_shell (const char *shellstr, char *args[], int doshell)
+{
+ int child;
+ sigset_t ourset;
+ int status;
+ int ret;
+
+ child = fork();
+ if (child == 0) { /* child shell */
+ pam_end (pamh, PAM_SUCCESS);
+
+ if (doshell)
+ shell (shellstr, (char *) args[0]);
+ else
+ (void) execv (shellstr, (char **) args);
+ {
+ int exit_status = (errno == ENOENT ? 127 : 126);
+ exit (exit_status);
+ }
+ } else if (child == -1) {
+ (void) fprintf(stderr, "%s: Cannot fork user shell\n", Prog);
+ SYSLOG ((LOG_WARN, "Cannot execute %s", pwent.pw_shell));
+ closelog ();
+ exit(1);
+ }
+ /* parent only */
+ sigfillset(&ourset);
+ if (sigprocmask(SIG_BLOCK, &ourset, NULL)) {
+ (void) fprintf(stderr, "%s: signal malfunction\n", Prog);
+ caught = 1;
+ }
+ if (!caught) {
+ struct sigaction action;
+ action.sa_handler = su_catch_sig;
+ sigemptyset(&action.sa_mask);
+ action.sa_flags = 0;
+ sigemptyset(&ourset);
+
+ if (sigaddset(&ourset, SIGTERM)
+ || sigaddset(&ourset, SIGALRM)
+ || sigaction(SIGTERM, &action, NULL)
+ || sigprocmask(SIG_UNBLOCK, &ourset, NULL)
+ ) {
+ fprintf(stderr, "%s: signal masking malfunction\n", Prog);
+ caught = 1;
+ }
+ }
+
+ if (!caught) {
+ do {
+ int pid;
+
+ pid = waitpid(-1, &status, WUNTRACED);
+
+ if (WIFSTOPPED(status)) {
+ kill(getpid(), SIGSTOP);
+ /* once we get here, we must have resumed */
+ kill(pid, SIGCONT);
+ }
+ } while (WIFSTOPPED(status));
+ }
+
+ if (caught) {
+ fprintf(stderr, "\nSession terminated, killing shell...");
+ kill (child, SIGTERM);
+ }
+
+ ret = pam_close_session(pamh, 0);
+ if (ret != PAM_SUCCESS) {
+ SYSLOG ((LOG_ERR, "pam_close_session: %s",
+ pam_strerror (pamh, ret)));
+ fprintf (stderr, "%s: %s\n", Prog,
+ pam_strerror (pamh, ret));
+ pam_end (pamh, ret);
+ exit (1);
+ }
+
+ ret = pam_end(pamh, PAM_SUCCESS);
+
+ if (caught) {
+ sleep(2);
+ kill(child, SIGKILL);
+ fprintf(stderr, " ...killed.\n");
+ exit(-1);
+ }
+
+ exit (WEXITSTATUS(status));
+}
+#endif
/*
* su - switch user id
@@ -152,6 +254,7 @@
int main (int argc, char **argv)
{
char *cp;
+ char **envcp;
const char *tty = 0; /* Name of tty SU is run from */
int doshell = 0;
int fakelogin = 0;
@@ -252,6 +355,14 @@
*/
if ((cp = getenv ("TERM")))
addenv ("TERM", cp);
+ /*
+ * Also leave DISPLAY and XAUTHORITY if present, else
+ * pam_xauth will not work.
+ */
+ if ((cp = getenv ("DISPLAY")))
+ addenv ("DISPLAY", cp);
+ if ((cp = getenv ("XAUTHORITY")))
+ addenv ("XAUTHORITY", cp);
} else {
while (*envp)
addenv (*envp++, NULL);
@@ -507,7 +618,10 @@
}
#endif
+/* setup the environment for pam later on, else we run into auth problems */
+#ifndef USE_PAM
environ = newenvp; /* make new environment active */
+#endif
if (getenv ("IFS")) /* don't export user IFS ... */
addenv ("IFS= \t\n", NULL); /* ... instead, set a safe IFS */
@@ -555,6 +669,31 @@
exit (1);
}
+ ret = pam_open_session (pamh, 0);
+ if (ret != PAM_SUCCESS) {
+ SYSLOG ((LOG_ERR, "pam_open_session: %s",
+ pam_strerror (pamh, ret)));
+ fprintf (stderr, "%s: %s\n", Prog,
+ pam_strerror (pamh, ret));
+ pam_end (pamh, ret);
+ exit (1);
+ }
+
+ /* we need to setup the environment *after* pam_open_session(),
+ * else the UID is changed before stuff like pam_xauth could
+ * run, and we cannot access /etc/shadow and co
+ */
+ environ = newenvp; /* make new environment active */
+
+ /* update environment with all pam set variables */
+ envcp = pam_getenvlist(pamh);
+ if(envcp) {
+ while(*envcp) {
+ putenv(*envcp);
+ envcp++;
+ }
+ }
+
/* become the new user */
if (change_uid (&pwent)) {
pam_setcred (pamh, PAM_DELETE_CRED);
@@ -562,9 +701,6 @@
exit (1);
}
- /* now we are done using PAM */
- pam_end (pamh, PAM_SUCCESS);
-
#else /* !USE_PAM */
if (!amroot) /* no limits if su from root */
setup_limits (&pwent);
@@ -622,13 +758,21 @@
*/
argv[-1] = pwent.pw_shell;
+#ifndef USE_PAM
(void) execv (pwent.pw_shell, &argv[-1]);
+#else
+ run_shell (pwent.pw_shell, &argv[-1], 0);
+#endif
(void) fprintf (stderr, _("No shell\n"));
SYSLOG ((LOG_WARN, "Cannot execute %s", pwent.pw_shell));
closelog ();
exit (1);
}
+#ifndef USE_PAM
shell (pwent.pw_shell, cp);
+#else
+ run_shell (pwent.pw_shell, &cp, 1);
+#endif
/* NOT REACHED */
exit (1);
}
|