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 action_descr
= (PolkitActionDescription
*)g_list_first(action_list
);
32 while((action_descr
= (PolkitActionDescription
*)g_list_next(action_list
)))
33 if(!g_strcmp0(action
, polkit_action_description_get_action_id(action_descr
)))
39 check_auth_result
polkit_try_auth(const gchar
*bus
, const gchar
*action
) {
42 PolkitAuthority
*auth
;
44 PolkitAuthorizationResult
*result
;
45 gboolean authorized
, challenge
;
51 authorized
= challenge
= FALSE
;
53 auth
= polkit_authority_get_sync(NULL
, NULL
); /* TODO timeout for this */
54 subj
= polkit_system_bus_name_new(bus
);
55 valid_actions
= polkit_authority_enumerate_actions_sync(auth
, NULL
, NULL
);
57 if(!auth
|| !valid_actions
)
58 return ERROR_GENERIC
; /* extremely unlikely */
61 else if(!is_valid_action(valid_actions
, action
))
62 return ERROR_BADACTION
;
64 if(!(result
= polkit_authority_check_authorization_sync(auth
, subj
, action
, NULL
, POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION
, NULL
, NULL
)))
65 return ERROR_GENERIC
; /* TODO pass, check gerror and return more relevant error */
67 authorized
= polkit_authorization_result_get_is_authorized(result
);
68 challenge
= polkit_authorization_result_get_is_challenge(result
);
70 /* free()'s before return */
72 g_object_unref(valid_actions
);
78 g_object_unref(result
);
83 return AUTHORIZED_BY_PROMPT
;
85 return AUTHORIZED_NATIVELY
;
88 return UNAUTHORIZED_FAILED_PROMPT
;
90 return UNAUTHORIZED_NATIVELY
;