1 /***************************************************************************
3 * libpolkit-rbac.c : RBAC implementation of the libpolkit API
5 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
6 * Use is subject to license terms.
8 * Licensed under the Academic Free License version 2.1
10 **************************************************************************/
12 #pragma ident "%Z%%M% %I% %E% SMI"
21 #include <sys/types.h>
26 #include <auth_attr.h>
30 #include <dbus/dbus-glib.h>
32 #include "libpolkit.h"
34 #define LIBPOLKIT_MAGIC 0x3117beef
37 #define __FUNCTION__ __func__
40 #define LIBPOLKIT_CHECK_CONTEXT(_ctx_, _ret_) \
42 if (_ctx_ == NULL) { \
43 g_warning ("%s: given LibPolKitContext is NULL", \
47 if (_ctx_->magic != LIBPOLKIT_MAGIC) { \
48 g_warning ("%s: given LibPolKitContext is invalid (read magic 0x%08x, should be 0x%08x)", \
49 __FUNCTION__, _ctx_->magic, LIBPOLKIT_MAGIC); \
55 struct LibPolKitContext_s
60 /** Get a new context.
62 * @return Pointer to new context or NULL if an error occured
65 libpolkit_new_context (DBusConnection
*connection
)
67 LibPolKitContext
*ctx
;
69 ctx
= g_new0 (LibPolKitContext
, 1);
70 ctx
->magic
= LIBPOLKIT_MAGIC
;
77 * @param ctx The context obtained from libpolkit_new_context
78 * @return Pointer to new context or NULL if an error occured
81 libpolkit_free_context (LibPolKitContext
*ctx
)
83 LIBPOLKIT_CHECK_CONTEXT (ctx
, FALSE
);
91 libpolkit_get_allowed_resources_for_privilege_for_uid (LibPolKitContext
*ctx
,
93 const char *privilege
,
96 int *num_non_temporary
)
101 char **restriction_list
;
102 int num_restrictions
;
104 LIBPOLKIT_CHECK_CONTEXT (ctx
, LIBPOLKIT_RESULT_INVALID_CONTEXT
);
106 res
= LIBPOLKIT_RESULT_ERROR
;
108 *restrictions
= NULL
;
110 res
= LIBPOLKIT_RESULT_OK
;
116 libpolkit_is_uid_allowed_for_privilege (LibPolKitContext
*ctx
,
117 const char *system_bus_unique_name
,
119 const char *privilege
,
120 const char *resource
,
121 gboolean
*out_is_allowed
,
122 gboolean
*out_is_temporary
,
123 char **out_is_privileged_but_restricted_to_system_bus_unique_name
)
126 const char *myresource
= "";
127 const char *mysystem_bus_unique_name
= "";
128 char *but_restricted_to
= NULL
;
133 gboolean authname_free
= FALSE
;
135 LIBPOLKIT_CHECK_CONTEXT (ctx
, LIBPOLKIT_RESULT_INVALID_CONTEXT
);
137 uid
= (uid_t
)atol (user
);
138 if ((pw
= getpwuid (uid
)) == NULL
) {
139 *out_is_allowed
= FALSE
;
140 *out_is_temporary
= FALSE
;
141 return LIBPOLKIT_RESULT_NO_SUCH_USER
;
144 /* map PolicyKit privilege to RBAC authorization */
145 if (strcmp (privilege
, "hal-storage-removable-mount") == 0) {
146 authname
= "solaris.device.mount.removable";
147 } else if (strcmp (privilege
, "hal-storage-removable-mount-all-options") == 0) {
148 authname
= "solaris.device.mount.alloptions.removable";
149 } else if (strcmp (privilege
, "hal-storage-fixed-mount") == 0) {
150 authname
= "solaris.device.mount.fixed";
151 } else if (strcmp (privilege
, "hal-storage-fixed-mount-all-options") == 0) {
152 authname
= "solaris.device.mount.alloptions.fixed";
153 } else if (strcmp(privilege
, "hal-power-suspend") == 0) {
154 authname
= "solaris.system.power.suspend.ram";
155 } else if (strcmp(privilege
, "hal-power-hibernate") == 0) {
156 authname
= "solaris.system.power.suspend.disk";
157 } else if ((strcmp(privilege
, "hal-power-shutdown") == 0) ||
158 (strcmp(privilege
, "hal-power-reboot") == 0)) {
159 authname
= "solaris.system.shutdown";
160 } else if (strcmp(privilege
, "hal-power-cpu") == 0) {
161 authname
= "solaris.system.power.cpu";
162 } else if (strcmp(privilege
, "hal-power-brightness") == 0) {
163 authname
= "solaris.system.power.brightness";
164 } else if (strcmp (privilege
, "hal-power-cpu") == 0) {
165 authname
= "solaris.system.power.cpu";
167 /* replace '-' with '.' */
168 authname
= g_strdup (privilege
);
169 authname_free
= TRUE
;
170 for (i
= 0; i
< strlen (authname
); i
++) {
171 if (authname
[i
] == '-') {
177 *out_is_allowed
= (chkauthattr(authname
, pw
->pw_name
) != 0);
178 *out_is_temporary
= FALSE
;
184 return LIBPOLKIT_RESULT_OK
;
188 libpolkit_get_privilege_list (LibPolKitContext
*ctx
,
192 char **privilege_list
;
193 int num_privileges
= 0;
196 LIBPOLKIT_CHECK_CONTEXT (ctx
, LIBPOLKIT_RESULT_INVALID_CONTEXT
);
200 for (i
= 0; i
< num_privileges
; i
++) {
201 *result
= g_list_append (*result
, g_strdup (privilege_list
[i
]));
204 res
= LIBPOLKIT_RESULT_OK
;
210 libpolkit_revoke_temporary_privilege (LibPolKitContext
*ctx
,
212 const char *privilege
,
213 const char *resource
,
216 return LIBPOLKIT_RESULT_OK
;