2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <glib/gprintf.h>
22 #include <glib-unix.h>
23 #include <polkit/polkit.h>
25 #include "polkit-auth.h"
27 static gboolean
is_valid_action(GList
*action_list
, const gchar
*action
) {
29 PolkitActionDescription
*action_descr
;
30 const gchar
*action_descr_id
;
35 cur
= g_list_first(action_list
);
37 while(cur
&& (action_descr
= ((PolkitActionDescription
*)(cur
->data
))) && (action_descr_id
= polkit_action_description_get_action_id(action_descr
))) {
39 if(!g_strcmp0(action
, action_descr_id
)) {
47 g_list_free(action_list
);
52 check_auth_result
polkit_try_auth(const gchar
*bus
, const gchar
*action
, gboolean prompt
) {
55 PolkitAuthority
*auth
;
57 PolkitAuthorizationResult
*result
;
58 PolkitCheckAuthorizationFlags prompt_flag
;
59 gboolean authorized
, challenge
;
65 authorized
= challenge
= FALSE
;
66 prompt_flag
= prompt
? POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION
: POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE
;
68 auth
= polkit_authority_get_sync(NULL
, NULL
); /* TODO timeout for this */
69 subj
= polkit_system_bus_name_new(bus
);
70 valid_actions
= polkit_authority_enumerate_actions_sync(auth
, NULL
, NULL
);
72 if(!auth
|| !valid_actions
)
73 return ERROR_GENERIC
; /* extremely unlikely */
76 else if(!is_valid_action(valid_actions
, action
))
77 return ERROR_BADACTION
;
79 if(!(result
= polkit_authority_check_authorization_sync(auth
, subj
, action
, NULL
, prompt_flag
, NULL
, NULL
)))
80 return ERROR_GENERIC
; /* TODO pass, check gerror and return more relevant error */
82 authorized
= polkit_authorization_result_get_is_authorized(result
);
83 challenge
= polkit_authorization_result_get_is_challenge(result
);
85 /* free()'s before return */
91 g_object_unref(result
);
96 return AUTHORIZED_BY_PROMPT
;
98 return AUTHORIZED_NATIVELY
;
101 return UNAUTHORIZED_FAILED_PROMPT
;
103 return UNAUTHORIZED_NATIVELY
;