Lines 1-339
Link Here
|
1 |
/* |
|
|
2 |
* Copyright (C) 2008 Red Hat, Inc. |
3 |
* |
4 |
* This library is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU Lesser General Public |
6 |
* License as published by the Free Software Foundation; either |
7 |
* version 2 of the License, or (at your option) any later version. |
8 |
* |
9 |
* This library is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
* Lesser General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU Lesser General |
15 |
* Public License along with this library; if not, write to the |
16 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
17 |
* Boston, MA 02111-1307, USA. |
18 |
* |
19 |
* Author: David Zeuthen <davidz@redhat.com> |
20 |
*/ |
21 |
|
22 |
#include "config.h" |
23 |
#include <stdio.h> |
24 |
#include <stdlib.h> |
25 |
#include <string.h> |
26 |
#include <unistd.h> |
27 |
#include <sys/types.h> |
28 |
#include <sys/stat.h> |
29 |
#include <syslog.h> |
30 |
#include <security/pam_appl.h> |
31 |
|
32 |
#include <polkit/polkit.h> |
33 |
|
34 |
#ifdef HAVE_SOLARIS |
35 |
# define LOG_AUTHPRIV (10<<3) |
36 |
#endif |
37 |
|
38 |
#ifndef HAVE_CLEARENV |
39 |
extern char **environ; |
40 |
|
41 |
static int |
42 |
clearenv (void) |
43 |
{ |
44 |
if (environ != NULL) |
45 |
environ[0] = NULL; |
46 |
return 0; |
47 |
} |
48 |
#endif |
49 |
|
50 |
/* Development aid: define PAH_DEBUG to get debugging output. Do _NOT_ |
51 |
* enable this in production builds; it may leak passwords and other |
52 |
* sensitive information. |
53 |
*/ |
54 |
#undef PAH_DEBUG |
55 |
// #define PAH_DEBUG |
56 |
|
57 |
static gboolean send_dbus_message (const char *cookie, const char *user); |
58 |
|
59 |
static int conversation_function (int n, const struct pam_message **msg, struct pam_response **resp, void *data); |
60 |
|
61 |
int |
62 |
main (int argc, char *argv[]) |
63 |
{ |
64 |
int rc; |
65 |
const char *user_to_auth; |
66 |
const char *cookie; |
67 |
struct pam_conv pam_conversation; |
68 |
pam_handle_t *pam_h; |
69 |
const void *authed_user; |
70 |
|
71 |
rc = 0; |
72 |
pam_h = NULL; |
73 |
|
74 |
/* clear the entire environment to avoid attacks using with libraries honoring environment variables */ |
75 |
if (clearenv () != 0) |
76 |
goto error; |
77 |
|
78 |
/* set a minimal environment */ |
79 |
setenv ("PATH", "/usr/sbin:/usr/bin:/sbin:/bin", 1); |
80 |
|
81 |
/* check that we are setuid root */ |
82 |
if (geteuid () != 0) |
83 |
{ |
84 |
fprintf (stderr, "polkit-agent-helper-1: needs to be setuid root\n"); |
85 |
goto error; |
86 |
} |
87 |
|
88 |
openlog ("polkit-agent-helper-1", LOG_CONS | LOG_PID, LOG_AUTHPRIV); |
89 |
|
90 |
/* check for correct invocation */ |
91 |
if (argc != 3) |
92 |
{ |
93 |
syslog (LOG_NOTICE, "inappropriate use of helper, wrong number of arguments [uid=%d]", getuid ()); |
94 |
fprintf (stderr, "polkit-agent-helper-1: wrong number of arguments. This incident has been logged.\n"); |
95 |
goto error; |
96 |
} |
97 |
|
98 |
user_to_auth = argv[1]; |
99 |
cookie = argv[2]; |
100 |
|
101 |
if (getuid () != 0) |
102 |
{ |
103 |
/* check we're running with a non-tty stdin */ |
104 |
if (isatty (STDIN_FILENO) != 0) |
105 |
{ |
106 |
syslog (LOG_NOTICE, "inappropriate use of helper, stdin is a tty [uid=%d]", getuid ()); |
107 |
fprintf (stderr, "polkit-agent-helper-1: inappropriate use of helper, stdin is a tty. This incident has been logged.\n"); |
108 |
goto error; |
109 |
} |
110 |
} |
111 |
|
112 |
#ifdef PAH_DEBUG |
113 |
fprintf (stderr, "polkit-agent-helper-1: user to auth is '%s'.\n", user_to_auth); |
114 |
#endif /* PAH_DEBUG */ |
115 |
|
116 |
pam_conversation.conv = conversation_function; |
117 |
pam_conversation.appdata_ptr = NULL; |
118 |
|
119 |
/* start the pam stack */ |
120 |
rc = pam_start ("polkit-1", |
121 |
user_to_auth, |
122 |
&pam_conversation, |
123 |
&pam_h); |
124 |
if (rc != PAM_SUCCESS) |
125 |
{ |
126 |
fprintf (stderr, "polkit-agent-helper-1: pam_start failed: %s\n", pam_strerror (pam_h, rc)); |
127 |
goto error; |
128 |
} |
129 |
|
130 |
/* set the requesting user */ |
131 |
rc = pam_set_item (pam_h, PAM_RUSER, user_to_auth); |
132 |
if (rc != PAM_SUCCESS) |
133 |
{ |
134 |
fprintf (stderr, "polkit-agent-helper-1: pam_set_item failed: %s\n", pam_strerror (pam_h, rc)); |
135 |
goto error; |
136 |
} |
137 |
|
138 |
/* is user really user? */ |
139 |
rc = pam_authenticate (pam_h, 0); |
140 |
if (rc != PAM_SUCCESS) |
141 |
{ |
142 |
fprintf (stderr, "polkit-agent-helper-1: pam_authenticated failed: %s\n", pam_strerror (pam_h, rc)); |
143 |
goto error; |
144 |
} |
145 |
|
146 |
/* permitted access? */ |
147 |
rc = pam_acct_mgmt (pam_h, 0); |
148 |
if (rc != PAM_SUCCESS) |
149 |
{ |
150 |
fprintf (stderr, "polkit-agent-helper-1: pam_acct_mgmt failed: %s\n", pam_strerror (pam_h, rc)); |
151 |
goto error; |
152 |
} |
153 |
|
154 |
/* did we auth the right user? */ |
155 |
rc = pam_get_item (pam_h, PAM_USER, &authed_user); |
156 |
if (rc != PAM_SUCCESS) |
157 |
{ |
158 |
fprintf (stderr, "polkit-agent-helper-1: pam_get_item failed: %s\n", pam_strerror (pam_h, rc)); |
159 |
goto error; |
160 |
} |
161 |
|
162 |
if (strcmp (authed_user, user_to_auth) != 0) |
163 |
{ |
164 |
fprintf (stderr, "polkit-agent-helper-1: Tried to auth user '%s' but we got auth for user '%s' instead", |
165 |
user_to_auth, (const char *) authed_user); |
166 |
goto error; |
167 |
} |
168 |
|
169 |
#ifdef PAH_DEBUG |
170 |
fprintf (stderr, "polkit-agent-helper-1: successfully authenticated user '%s'.\n", user_to_auth); |
171 |
#endif /* PAH_DEBUG */ |
172 |
|
173 |
pam_end (pam_h, rc); |
174 |
pam_h = NULL; |
175 |
|
176 |
#ifdef PAH_DEBUG |
177 |
fprintf (stderr, "polkit-agent-helper-1: sending D-Bus message to PolicyKit daemon\n"); |
178 |
#endif /* PAH_DEBUG */ |
179 |
|
180 |
/* now send a D-Bus message to the PolicyKit daemon that |
181 |
* includes a) the cookie; and b) the user we authenticated |
182 |
*/ |
183 |
if (!send_dbus_message (cookie, user_to_auth)) |
184 |
{ |
185 |
#ifdef PAH_DEBUG |
186 |
fprintf (stderr, "polkit-agent-helper-1: error sending D-Bus message to PolicyKit daemon\n"); |
187 |
#endif /* PAH_DEBUG */ |
188 |
goto error; |
189 |
} |
190 |
|
191 |
#ifdef PAH_DEBUG |
192 |
fprintf (stderr, "polkit-agent-helper-1: successfully sent D-Bus message to PolicyKit daemon\n"); |
193 |
#endif /* PAH_DEBUG */ |
194 |
|
195 |
fprintf (stdout, "SUCCESS\n"); |
196 |
fflush (stdout); |
197 |
fflush (stderr); |
198 |
usleep (10 * 1000); /* since fflush(3) seems buggy */ |
199 |
return 0; |
200 |
|
201 |
error: |
202 |
if (pam_h != NULL) |
203 |
pam_end (pam_h, rc); |
204 |
|
205 |
fprintf (stdout, "FAILURE\n"); |
206 |
fflush (stdout); |
207 |
fflush (stderr); |
208 |
usleep (10 * 1000); /* since fflush(3) seems buggy */ |
209 |
return 1; |
210 |
} |
211 |
|
212 |
static int |
213 |
conversation_function (int n, const struct pam_message **msg, struct pam_response **resp, void *data) |
214 |
{ |
215 |
struct pam_response *aresp; |
216 |
char buf[PAM_MAX_RESP_SIZE]; |
217 |
int i; |
218 |
|
219 |
data = data; |
220 |
if (n <= 0 || n > PAM_MAX_NUM_MSG) |
221 |
return PAM_CONV_ERR; |
222 |
|
223 |
if ((aresp = calloc(n, sizeof *aresp)) == NULL) |
224 |
return PAM_BUF_ERR; |
225 |
|
226 |
for (i = 0; i < n; ++i) |
227 |
{ |
228 |
aresp[i].resp_retcode = 0; |
229 |
aresp[i].resp = NULL; |
230 |
switch (msg[i]->msg_style) |
231 |
{ |
232 |
|
233 |
case PAM_PROMPT_ECHO_OFF: |
234 |
fprintf (stdout, "PAM_PROMPT_ECHO_OFF "); |
235 |
goto conv1; |
236 |
|
237 |
case PAM_PROMPT_ECHO_ON: |
238 |
fprintf (stdout, "PAM_PROMPT_ECHO_ON "); |
239 |
conv1: |
240 |
fputs (msg[i]->msg, stdout); |
241 |
if (strlen (msg[i]->msg) > 0 && msg[i]->msg[strlen (msg[i]->msg) - 1] != '\n') |
242 |
fputc ('\n', stdout); |
243 |
fflush (stdout); |
244 |
|
245 |
if (fgets (buf, sizeof buf, stdin) == NULL) |
246 |
goto error; |
247 |
|
248 |
if (strlen (buf) > 0 && |
249 |
buf[strlen (buf) - 1] == '\n') |
250 |
buf[strlen (buf) - 1] = '\0'; |
251 |
|
252 |
aresp[i].resp = strdup (buf); |
253 |
if (aresp[i].resp == NULL) |
254 |
goto error; |
255 |
break; |
256 |
|
257 |
case PAM_ERROR_MSG: |
258 |
fprintf (stdout, "PAM_ERROR_MSG "); |
259 |
goto conv2; |
260 |
|
261 |
case PAM_TEXT_INFO: |
262 |
fprintf (stdout, "PAM_TEXT_INFO "); |
263 |
conv2: |
264 |
fputs (msg[i]->msg, stdout); |
265 |
if (strlen (msg[i]->msg) > 0 && |
266 |
msg[i]->msg[strlen (msg[i]->msg) - 1] != '\n') |
267 |
fputc ('\n', stdout); |
268 |
fflush (stdout); |
269 |
break; |
270 |
|
271 |
default: |
272 |
goto error; |
273 |
} |
274 |
} |
275 |
|
276 |
*resp = aresp; |
277 |
return PAM_SUCCESS; |
278 |
|
279 |
error: |
280 |
|
281 |
for (i = 0; i < n; ++i) |
282 |
{ |
283 |
if (aresp[i].resp != NULL) { |
284 |
memset (aresp[i].resp, 0, strlen(aresp[i].resp)); |
285 |
free (aresp[i].resp); |
286 |
} |
287 |
} |
288 |
memset (aresp, 0, n * sizeof *aresp); |
289 |
*resp = NULL; |
290 |
return PAM_CONV_ERR; |
291 |
} |
292 |
|
293 |
static gboolean |
294 |
send_dbus_message (const char *cookie, const char *user) |
295 |
{ |
296 |
PolkitAuthority *authority; |
297 |
PolkitIdentity *identity; |
298 |
GError *error; |
299 |
gboolean ret; |
300 |
|
301 |
ret = FALSE; |
302 |
|
303 |
error = NULL; |
304 |
|
305 |
g_type_init (); |
306 |
|
307 |
authority = polkit_authority_get (); |
308 |
|
309 |
identity = polkit_unix_user_new_for_name (user, &error); |
310 |
if (identity == NULL) |
311 |
{ |
312 |
g_printerr ("Error constructing identity: %s\n", error->message); |
313 |
g_error_free (error); |
314 |
goto out; |
315 |
} |
316 |
|
317 |
if (!polkit_authority_authentication_agent_response_sync (authority, |
318 |
cookie, |
319 |
identity, |
320 |
NULL, |
321 |
&error)) |
322 |
{ |
323 |
g_printerr ("polkit-agent-helper-1: error response to PolicyKit daemon: %s\n", error->message); |
324 |
g_error_free (error); |
325 |
goto out; |
326 |
} |
327 |
|
328 |
ret = TRUE; |
329 |
|
330 |
out: |
331 |
|
332 |
if (identity != NULL) |
333 |
g_object_unref (identity); |
334 |
|
335 |
if (authority != NULL) |
336 |
g_object_unref (authority); |
337 |
|
338 |
return ret; |
339 |
} |