Debian Codegraph @

src/programs/pkcheck.c

raw
1 /*
2 * Copyright (C) 2009 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 <[email protected]>
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <glib/gi18n.h>
25 #include <polkit/polkit.h>
26 #define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE
27 #include <polkitagent/polkitagent.h>
28
29 static void
30 help (void)
31 {
32 g_print (_("Usage:\n"
33 " pkcheck [OPTION...]\n"
34 "\n"
35 "Help Options:\n"
36 " -h, --help Show help options\n"
37 "\n"
38 "Application Options:\n"
39 " -a, --action-id=ACTION Check authorization to perform ACTION\n"
40 " -u, --allow-user-interaction Interact with the user if necessary\n"
41 " -d, --details=KEY VALUE Add (KEY, VALUE) to information about the action\n"
42 " --enable-internal-agent Use an internal authentication agent if necessary\n"
43 " --list-temp List temporary authorizations for current session\n"
44 " -p, --process=PID[,START_TIME,UID] Check authorization of specified process\n"
45 " --revoke-temp Revoke all temporary authorizations for current session\n"
46 " -s, --system-bus-name=BUS_NAME Check authorization of owner of BUS_NAME\n"
47 " --version Show version\n"
48 "\n"
49 "Report bugs to: %s\n"
50 "%s home page: <%s>\n"), PACKAGE_BUGREPORT, PACKAGE_NAME,
51 PACKAGE_URL);
52 }
53
54 static gchar *
55 escape_str (const gchar *str)
56 {
57 GString *s;
58 guint n;
59
60 s = g_string_new (NULL);
61 if (str == NULL)
62 goto out;
63
64 for (n = 0; str[n] != '\0'; n++)
65 {
66 guint c = str[n] & 0xff;
67
68 if (g_ascii_isalnum (c) || c=='_')
69 g_string_append_c (s, c);
70 else
71 g_string_append_printf (s, "\\%o", c);
72 }
73
74 out:
75 return g_string_free (s, FALSE);
76 }
77
78 static gchar *
79 format_reltime (gint seconds)
80 {
81 gint magnitude;
82 const gchar *ending;
83 gchar *ret;
84
85 if (seconds >= 0)
86 {
87 magnitude = seconds;
88 ending = "from now";
89 }
90 else
91 {
92 magnitude = -seconds;
93 ending = "ago";
94 }
95
96 if (magnitude >= 60)
97 {
98 ret = g_strdup_printf ("%d min %d sec %s", magnitude/60, magnitude%60, ending);
99 }
100 else
101 {
102 ret = g_strdup_printf ("%d sec %s", magnitude, ending);
103 }
104
105 return ret;
106 }
107
108 /* TODO: should probably move to PolkitSubject
109 * (also see copy in src/polkitbackend/polkitbackendinteractiveauthority.c)
110 *
111 * Also, can't really trust the cmdline... but might be useful in the logs anyway.
112 */
113 static gchar *
114 _polkit_subject_get_cmdline (PolkitSubject *subject)
115 {
116 PolkitSubject *process;
117 gchar *ret;
118 gint pid;
119 gchar *filename;
120 gchar *contents;
121 gsize contents_len;
122 GError *error;
123 guint n;
124
125 g_return_val_if_fail (subject != NULL, NULL);
126
127 error = NULL;
128
129 ret = NULL;
130 process = NULL;
131 filename = NULL;
132 contents = NULL;
133
134 if (POLKIT_IS_UNIX_PROCESS (subject))
135 {
136 process = g_object_ref (subject);
137 }
138 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
139 {
140 process = polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject),
141 NULL,
142 &error);
143 if (process == NULL)
144 {
145 g_printerr ("Error getting process for system bus name `%s': %s\n",
146 polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject)),
147 error->message);
148 g_error_free (error);
149 goto out;
150 }
151 }
152 else
153 {
154 g_warning ("Unknown subject type passed to _polkit_subject_get_cmdline()");
155 goto out;
156 }
157
158 pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (process));
159 if (pid <= 0)
160 goto out;
161
162 filename = g_strdup_printf ("/proc/%d/cmdline", pid);
163
164 if (!g_file_get_contents (filename,
165 &contents,
166 &contents_len,
167 &error))
168 {
169 g_printerr ("Error opening `%s': %s\n",
170 filename,
171 error->message);
172 g_error_free (error);
173 goto out;
174 }
175
176 if (contents == NULL || contents_len == 0)
177 {
178 goto out;
179 }
180 else
181 {
182 /* The kernel uses '\0' to separate arguments - replace those with a space. */
183 for (n = 0; n < contents_len - 1; n++)
184 {
185 if (contents[n] == '\0')
186 contents[n] = ' ';
187 }
188 ret = g_strdup (contents);
189 g_strstrip (ret);
190 }
191
192 out:
193 g_free (filename);
194 g_free (contents);
195 if (process != NULL)
196 g_object_unref (process);
197 return ret;
198 }
199
200 static gint
201 do_list_or_revoke_temp_authz (gboolean revoke)
202 {
203 gint ret;
204 PolkitAuthority *authority;
205 PolkitSubject *session;
206 GError *error;
207
208 ret = 1;
209 authority = NULL;
210 session = NULL;
211
212 error = NULL;
213 authority = polkit_authority_get_sync (NULL /* GCancellable* */, &error);
214 if (authority == NULL)
215 {
216 g_printerr ("Error getting authority: %s\n", error->message);
217 g_error_free (error);
218 goto out;
219 }
220
221 error = NULL;
222 session = polkit_unix_session_new_for_process_sync (getpid (),
223 NULL, /* GCancellable */
224 &error);
225 if (session == NULL)
226 {
227 g_printerr ("Error getting session: %s\n", error->message);
228 g_error_free (error);
229 goto out;
230 }
231
232 if (revoke)
233 {
234 if (!polkit_authority_revoke_temporary_authorizations_sync (authority,
235 session,
236 NULL, /* GCancellable */
237 &error))
238 {
239 g_printerr ("Error revoking temporary authorizations: %s\n", error->message);
240 g_error_free (error);
241 goto out;
242 }
243
244 ret = 0;
245 }
246 else
247 {
248 GList *authorizations;
249 GList *l;
250
251 error = NULL;
252 authorizations = polkit_authority_enumerate_temporary_authorizations_sync (authority,
253 session,
254 NULL, /* GCancellable */
255 &error);
256 if (error != NULL)
257 {
258 g_printerr ("Error getting temporary authorizations: %s\n", error->message);
259 g_error_free (error);
260 goto out;
261 }
262
263 for (l = authorizations; l != NULL; l = l->next)
264 {
265 PolkitTemporaryAuthorization *a = POLKIT_TEMPORARY_AUTHORIZATION (l->data);
266 const gchar *id;
267 const gchar *action_id;
268 PolkitSubject *subject;
269 gchar *subject_cmdline;
270 time_t obtained;
271 time_t expires;
272 gint64 now;
273 gchar *subject_str;
274 gchar obtained_str[64];
275 gchar expires_str[64];
276 gchar *obtained_rel_str;
277 gchar *expires_rel_str;
278 struct tm *broken_down;
279
280 id = polkit_temporary_authorization_get_id (a);
281 action_id = polkit_temporary_authorization_get_action_id (a);
282 subject = polkit_temporary_authorization_get_subject (a);
283 subject_str = polkit_subject_to_string (subject);
284 subject_cmdline = _polkit_subject_get_cmdline (subject);
285 obtained = polkit_temporary_authorization_get_time_obtained (a);
286 expires = polkit_temporary_authorization_get_time_expires (a);
287
288 now = g_get_real_time () / G_TIME_SPAN_SECOND;
289
290 broken_down = localtime (&obtained);
291 strftime (obtained_str, sizeof (obtained_str), "%c", broken_down);
292 broken_down = localtime (&expires);
293 strftime (expires_str, sizeof (expires_str), "%c", broken_down);
294
295 obtained_rel_str = format_reltime (obtained - now);
296 expires_rel_str = format_reltime (expires - now);
297
298 g_print ("authorization id: %s\n"
299 "action: %s\n"
300 "subject: %s (%s)\n"
301 "obtained: %s (%s)\n"
302 "expires: %s (%s)\n"
303 "\n",
304 id,
305 action_id,
306 subject_str, subject_cmdline != NULL ? subject_cmdline : "cannot read cmdline",
307 obtained_rel_str, obtained_str,
308 expires_rel_str, expires_str);
309
310 g_object_unref (subject);
311 g_free (subject_str);
312 g_free (subject_cmdline);
313 g_free (obtained_rel_str);
314 g_free (expires_rel_str);
315 }
316 g_list_foreach (authorizations, (GFunc) g_object_unref, NULL);
317 g_list_free (authorizations);
318
319 ret = 0;
320 }
321
322 out:
323 if (authority != NULL)
324 g_object_unref (authority);
325 if (session != NULL)
326 g_object_unref (session);
327
328 return ret;
329 }
330
331 int
332 main (int argc, char *argv[])
333 {
334 guint n;
335 guint ret;
336 gchar *action_id;
337 gboolean opt_show_help;
338 gboolean opt_show_version;
339 gboolean allow_user_interaction;
340 gboolean enable_internal_agent;
341 gboolean list_temp;
342 gboolean revoke_temp;
343 PolkitAuthority *authority;
344 PolkitAuthorizationResult *result;
345 PolkitSubject *subject;
346 PolkitDetails *details;
347 PolkitCheckAuthorizationFlags flags;
348 PolkitDetails *result_details;
349 GError *error;
350 gpointer local_agent_handle;
351
352 subject = NULL;
353 action_id = NULL;
354 details = NULL;
355 authority = NULL;
356 result = NULL;
357 allow_user_interaction = FALSE;
358 enable_internal_agent = FALSE;
359 list_temp = FALSE;
360 revoke_temp = FALSE;
361 local_agent_handle = NULL;
362 ret = 126;
363
364 if (argc < 1)
365 {
366 exit(126);
367 }
368
369 /* Disable remote file access from GIO. */
370 setenv ("GIO_USE_VFS", "local", 1);
371
372 details = polkit_details_new ();
373
374 opt_show_help = FALSE;
375 opt_show_version = FALSE;
376 g_set_prgname ("pkcheck");
377 for (n = 1; n < (guint) argc; n++)
378 {
379 if (g_strcmp0 (argv[n], "--help") == 0 || g_strcmp0 (argv[n], "-h") == 0)
380 {
381 opt_show_help = TRUE;
382 }
383 else if (g_strcmp0 (argv[n], "--version") == 0)
384 {
385 opt_show_version = TRUE;
386 }
387 else if (g_strcmp0 (argv[n], "--process") == 0 || g_strcmp0 (argv[n], "-p") == 0)
388 {
389 gint pid;
390 guint uid;
391 guint64 pid_start_time;
392
393 n++;
394 if (n >= (guint) argc)
395 {
396 g_printerr (_("%s: Argument expected after `%s'\n"),
397 g_get_prgname (), "--process, -p");
398 goto out;
399 }
400
401 if (sscanf (argv[n], "%i,%" G_GUINT64_FORMAT ",%u", &pid, &pid_start_time, &uid) == 3)
402 {
403 subject = polkit_unix_process_new_for_owner (pid, pid_start_time, uid);
404 }
405 else if (sscanf (argv[n], "%i,%" G_GUINT64_FORMAT, &pid, &pid_start_time) == 2)
406 {
407 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
408 subject = polkit_unix_process_new_full (pid, pid_start_time);
409 G_GNUC_END_IGNORE_DEPRECATIONS
410 }
411 else if (sscanf (argv[n], "%i", &pid) == 1)
412 {
413 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
414 subject = polkit_unix_process_new (pid);
415 G_GNUC_END_IGNORE_DEPRECATIONS
416 }
417 else
418 {
419 g_printerr (_("%s: Invalid --process value `%s'\n"),
420 g_get_prgname (), argv[n]);
421 goto out;
422 }
423 }
424 else if (g_strcmp0 (argv[n], "--system-bus-name") == 0 || g_strcmp0 (argv[n], "-s") == 0)
425 {
426 n++;
427 if (n >= (guint) argc)
428 {
429 g_printerr (_("%s: Argument expected after `%s'\n"),
430 g_get_prgname (), "--system-bus-name, -s");
431 goto out;
432 }
433
434 subject = polkit_system_bus_name_new (argv[n]);
435 }
436 else if (g_strcmp0 (argv[n], "--action-id") == 0 || g_strcmp0 (argv[n], "-a") == 0)
437 {
438 n++;
439 if (n >= (guint) argc)
440 {
441 g_printerr (_("%s: Argument expected after `%s'\n"),
442 g_get_prgname (), "--action-id, -a");
443 goto out;
444 }
445
446 action_id = g_strdup (argv[n]);
447 }
448 else if (g_strcmp0 (argv[n], "--detail") == 0 || g_strcmp0 (argv[n], "-d") == 0)
449 {
450 const gchar *key;
451 const gchar *value;
452
453 n++;
454 if (n >= (guint) argc)
455 {
456 g_printerr (_("%s: Two arguments expected after `--detail, -d'\n"),
457 g_get_prgname ());
458 goto out;
459 }
460 key = argv[n];
461
462 n++;
463 if (n >= (guint) argc)
464 {
465 g_printerr (_("%s: Two arguments expected after `--detail, -d'\n"),
466 g_get_prgname ());
467 goto out;
468 }
469 value = argv[n];
470
471 polkit_details_insert (details, key, value);
472 }
473 else if (g_strcmp0 (argv[n], "--allow-user-interaction") == 0 || g_strcmp0 (argv[n], "-u") == 0)
474 {
475 allow_user_interaction = TRUE;
476 }
477 else if (g_strcmp0 (argv[n], "--enable-internal-agent") == 0)
478 {
479 enable_internal_agent = TRUE;
480 }
481 else if (g_strcmp0 (argv[n], "--list-temp") == 0)
482 {
483 list_temp = TRUE;
484 }
485 else if (g_strcmp0 (argv[n], "--revoke-temp") == 0)
486 {
487 revoke_temp = TRUE;
488 }
489 else
490 {
491 break;
492 }
493 }
494 if (argv[n] != NULL)
495 {
496 g_printerr (_("%s: Unexpected argument `%s'\n"), g_get_prgname (),
497 argv[n]);
498 goto out;
499 }
500
501 if (opt_show_help)
502 {
503 help ();
504 ret = 0;
505 goto out;
506 }
507 else if (opt_show_version)
508 {
509 g_print ("pkcheck version %s\n", PACKAGE_VERSION);
510 ret = 0;
511 goto out;
512 }
513
514 if (list_temp)
515 {
516 ret = do_list_or_revoke_temp_authz (FALSE);
517 goto out;
518 }
519 else if (revoke_temp)
520 {
521 ret = do_list_or_revoke_temp_authz (TRUE);
522 goto out;
523 }
524 else if (subject == NULL)
525 {
526 g_printerr (_("%s: Subject not specified\n"), g_get_prgname ());
527 goto out;
528 }
529
530 error = NULL;
531 authority = polkit_authority_get_sync (NULL /* GCancellable* */, &error);
532 if (authority == NULL)
533 {
534 g_printerr ("Error getting authority: %s\n", error->message);
535 g_error_free (error);
536 goto out;
537 }
538
539 try_again:
540 error = NULL;
541 flags = POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE;
542 if (allow_user_interaction)
543 flags |= POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION;
544 result = polkit_authority_check_authorization_sync (authority,
545 subject,
546 action_id,
547 details,
548 flags,
549 NULL,
550 &error);
551 if (result == NULL)
552 {
553 g_printerr ("Error checking for authorization %s: %s\n",
554 action_id,
555 error ? error->message : "Could not verify; error object not present.");
556 ret = 127;
557 goto out;
558 }
559
560 result_details = polkit_authorization_result_get_details (result);
561 if (result_details != NULL)
562 {
563 gchar **keys;
564
565 keys = polkit_details_get_keys (result_details);
566 for (n = 0; keys != NULL && keys[n] != NULL; n++)
567 {
568 const gchar *key;
569 const gchar *value;
570 gchar *s;
571
572 key = keys[n];
573 value = polkit_details_lookup (result_details, key);
574
575 s = escape_str (key);
576 g_print ("%s", s);
577 g_free (s);
578 g_print ("=");
579 s = escape_str (value);
580 g_print ("%s", s);
581 g_free (s);
582 g_print ("\n");
583 }
584
585 g_strfreev (keys);
586 }
587
588 if (polkit_authorization_result_get_is_authorized (result))
589 {
590 ret = 0;
591 }
592 else if (polkit_authorization_result_get_is_challenge (result))
593 {
594 if (allow_user_interaction)
595 {
596 if (local_agent_handle == NULL && enable_internal_agent)
597 {
598 PolkitAgentListener *listener;
599 error = NULL;
600 /* this will fail if we can't find a controlling terminal */
601 listener = polkit_agent_text_listener_new (NULL, &error);
602 if (listener == NULL)
603 {
604 g_printerr ("Error creating textual authentication agent: %s\n", error->message);
605 g_error_free (error);
606 goto out;
607 }
608 local_agent_handle = polkit_agent_listener_register (listener,
609 POLKIT_AGENT_REGISTER_FLAGS_RUN_IN_THREAD,
610 subject,
611 NULL, /* object_path */
612 NULL, /* GCancellable */
613 &error);
614 g_object_unref (listener);
615 if (local_agent_handle == NULL)
616 {
617 g_printerr ("Error registering local authentication agent: %s\n", error->message);
618 g_error_free (error);
619 goto out;
620 }
621 g_object_unref (result);
622 result = NULL;
623 goto try_again;
624 }
625 else
626 {
627 g_printerr ("Authorization requires authentication but no agent is available.\n");
628 }
629 }
630 else
631 {
632 g_printerr ("Authorization requires authentication and -u wasn't passed.\n");
633 }
634 ret = 2;
635 }
636 else if (polkit_authorization_result_get_dismissed (result))
637 {
638 g_printerr ("Authentication request was dismissed.\n");
639 ret = 3;
640 }
641 else
642 {
643 g_printerr ("Not authorized.\n");
644 ret = 1;
645 }
646
647 out:
648 /* if applicable, nuke the local authentication agent */
649 if (local_agent_handle != NULL)
650 polkit_agent_listener_unregister (local_agent_handle);
651
652 if (result != NULL)
653 g_object_unref (result);
654
655 g_free (action_id);
656
657 if (details != NULL)
658 g_object_unref (details);
659
660 if (subject != NULL)
661 g_object_unref (subject);
662
663 if (authority != NULL)
664 g_object_unref (authority);
665
666 return ret;
667 }