From ed4cf3c89bbf73021ea8a1db6ceb86f2f93683cd Mon Sep 17 00:00:00 2001 From: kremlin Date: Sun, 24 Aug 2014 22:43:53 -0500 Subject: change polkit-auth.{c,h} to util.{c,h} we are going to include the /etc/os-release changing functions in here. --- src/interfaces/hostnamed/hostnamed.c | 2 +- src/interfaces/localed/localed.c | 2 +- src/interfaces/logind/logind.c | 2 +- src/interfaces/timedated/timedated.c | 2 +- src/polkit-auth.c | 104 ----------------------------------- src/polkit-auth.h | 23 -------- src/util.c | 104 +++++++++++++++++++++++++++++++++++ src/util.h | 23 ++++++++ 8 files changed, 131 insertions(+), 131 deletions(-) delete mode 100644 src/polkit-auth.c delete mode 100644 src/polkit-auth.h create mode 100644 src/util.c create mode 100644 src/util.h (limited to 'src') diff --git a/src/interfaces/hostnamed/hostnamed.c b/src/interfaces/hostnamed/hostnamed.c index 81200e7..f3a6372 100644 --- a/src/interfaces/hostnamed/hostnamed.c +++ b/src/interfaces/hostnamed/hostnamed.c @@ -34,7 +34,7 @@ #include "hostnamed-gen.h" #include "hostnamed.h" -#include "../../polkit-auth.h" +#include "../../util.h" /* format: { * (1) string to be matched against runtime machine's sysctl output. diff --git a/src/interfaces/localed/localed.c b/src/interfaces/localed/localed.c index 33769a3..da690af 100644 --- a/src/interfaces/localed/localed.c +++ b/src/interfaces/localed/localed.c @@ -28,7 +28,7 @@ #include "localed-gen.h" #include "localed.h" -#include "../../polkit-auth.h" +#include "../../util.h" GPtrArray *localed_freeable; Locale1 *localed_interf; diff --git a/src/interfaces/logind/logind.c b/src/interfaces/logind/logind.c index c445665..02323d0 100644 --- a/src/interfaces/logind/logind.c +++ b/src/interfaces/logind/logind.c @@ -28,7 +28,7 @@ #include "logind-gen.h" #include "logind.h" -#include "../../polkit-auth.h" +#include "../../util.h" GPtrArray *logind_freeable; Login1Manager *logind_interf; diff --git a/src/interfaces/timedated/timedated.c b/src/interfaces/timedated/timedated.c index f76850c..bda7098 100644 --- a/src/interfaces/timedated/timedated.c +++ b/src/interfaces/timedated/timedated.c @@ -28,7 +28,7 @@ #include "timedated-gen.h" #include "timedated.h" -#include "../../polkit-auth.h" +#include "../../util.h" GPtrArray *timedated_freeable; Timedate1 *timedated_interf; diff --git a/src/polkit-auth.c b/src/polkit-auth.c deleted file mode 100644 index f967cd0..0000000 --- a/src/polkit-auth.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2014 Ian Sutton - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -#include -#include -#include - -#include "polkit-auth.h" - -static gboolean is_valid_action(GList *action_list, const gchar *action) { - - PolkitActionDescription *action_descr; - const gchar *action_descr_id; - GList *cur; - gboolean ret; - - ret = FALSE; - cur = g_list_first(action_list); - - while(cur && (action_descr = ((PolkitActionDescription *)(cur->data))) && (action_descr_id = polkit_action_description_get_action_id(action_descr))) { - - if(!g_strcmp0(action, action_descr_id)) { - ret = TRUE; - break; - } - - cur = cur->next; - } - - g_list_free(action_list); - - return ret; -} - -check_auth_result polkit_try_auth(const gchar *bus, const gchar *action, gboolean prompt) { - - GList *valid_actions; - PolkitAuthority *auth; - PolkitSubject *subj; - PolkitAuthorizationResult *result; - PolkitCheckAuthorizationFlags prompt_flag; - gboolean authorized, challenge; - - auth = NULL; - subj = NULL; - result = NULL; - valid_actions = NULL; - authorized = challenge = FALSE; - prompt_flag = prompt ? POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION : POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE; - - auth = polkit_authority_get_sync(NULL, NULL); /* TODO timeout for this */ - subj = polkit_system_bus_name_new(bus); - valid_actions = polkit_authority_enumerate_actions_sync(auth, NULL, NULL); - - if(!auth || !valid_actions) - return ERROR_GENERIC; /* extremely unlikely */ - else if(!subj) - return ERROR_BADBUS; - else if(!is_valid_action(valid_actions, action)) - return ERROR_BADACTION; - - if(!(result = polkit_authority_check_authorization_sync(auth, subj, action, NULL, prompt_flag, NULL, NULL))) - return ERROR_GENERIC; /* TODO pass, check gerror and return more relevant error */ - - authorized = polkit_authorization_result_get_is_authorized(result); - challenge = polkit_authorization_result_get_is_challenge(result); - - /* free()'s before return */ - if(auth) - g_object_unref(auth); - if(subj) - g_object_unref(subj); - if(result) - g_object_unref(result); - - if(authorized) { - - if(challenge) - return AUTHORIZED_BY_PROMPT; - - return AUTHORIZED_NATIVELY; - - } else if(challenge) - return UNAUTHORIZED_FAILED_PROMPT; - - return UNAUTHORIZED_NATIVELY; -} diff --git a/src/polkit-auth.h b/src/polkit-auth.h deleted file mode 100644 index 5775fb4..0000000 --- a/src/polkit-auth.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2014 Ian Sutton - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -typedef enum { - AUTHORIZED_NATIVELY, AUTHORIZED_BY_PROMPT, - UNAUTHORIZED_NATIVELY, UNAUTHORIZED_FAILED_PROMPT, - ERROR_BADBUS, ERROR_BADACTION, ERROR_GENERIC -} check_auth_result; - -check_auth_result polkit_try_auth(const gchar *bus, const gchar *action, gboolean prompt); diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..f228c9a --- /dev/null +++ b/src/util.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2014 Ian Sutton + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include + +#include "util.h" + +static gboolean is_valid_action(GList *action_list, const gchar *action) { + + PolkitActionDescription *action_descr; + const gchar *action_descr_id; + GList *cur; + gboolean ret; + + ret = FALSE; + cur = g_list_first(action_list); + + while(cur && (action_descr = ((PolkitActionDescription *)(cur->data))) && (action_descr_id = polkit_action_description_get_action_id(action_descr))) { + + if(!g_strcmp0(action, action_descr_id)) { + ret = TRUE; + break; + } + + cur = cur->next; + } + + g_list_free(action_list); + + return ret; +} + +check_auth_result polkit_try_auth(const gchar *bus, const gchar *action, gboolean prompt) { + + GList *valid_actions; + PolkitAuthority *auth; + PolkitSubject *subj; + PolkitAuthorizationResult *result; + PolkitCheckAuthorizationFlags prompt_flag; + gboolean authorized, challenge; + + auth = NULL; + subj = NULL; + result = NULL; + valid_actions = NULL; + authorized = challenge = FALSE; + prompt_flag = prompt ? POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION : POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE; + + auth = polkit_authority_get_sync(NULL, NULL); /* TODO timeout for this */ + subj = polkit_system_bus_name_new(bus); + valid_actions = polkit_authority_enumerate_actions_sync(auth, NULL, NULL); + + if(!auth || !valid_actions) + return ERROR_GENERIC; /* extremely unlikely */ + else if(!subj) + return ERROR_BADBUS; + else if(!is_valid_action(valid_actions, action)) + return ERROR_BADACTION; + + if(!(result = polkit_authority_check_authorization_sync(auth, subj, action, NULL, prompt_flag, NULL, NULL))) + return ERROR_GENERIC; /* TODO pass, check gerror and return more relevant error */ + + authorized = polkit_authorization_result_get_is_authorized(result); + challenge = polkit_authorization_result_get_is_challenge(result); + + /* free()'s before return */ + if(auth) + g_object_unref(auth); + if(subj) + g_object_unref(subj); + if(result) + g_object_unref(result); + + if(authorized) { + + if(challenge) + return AUTHORIZED_BY_PROMPT; + + return AUTHORIZED_NATIVELY; + + } else if(challenge) + return UNAUTHORIZED_FAILED_PROMPT; + + return UNAUTHORIZED_NATIVELY; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..5775fb4 --- /dev/null +++ b/src/util.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2014 Ian Sutton + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +typedef enum { + AUTHORIZED_NATIVELY, AUTHORIZED_BY_PROMPT, + UNAUTHORIZED_NATIVELY, UNAUTHORIZED_FAILED_PROMPT, + ERROR_BADBUS, ERROR_BADACTION, ERROR_GENERIC +} check_auth_result; + +check_auth_result polkit_try_auth(const gchar *bus, const gchar *action, gboolean prompt); -- cgit v1.2.3