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 LIBPOLKIT_CHECK_CONTEXT(_ctx_, _ret_) \
39 if (_ctx_ == NULL) { \
40 g_warning ("%s: given LibPolKitContext is NULL", \
44 if (_ctx_->magic != LIBPOLKIT_MAGIC) { \
45 g_warning ("%s: given LibPolKitContext is invalid (read magic 0x%08x, should be 0x%08x)", \
46 __FUNCTION__, _ctx_->magic, LIBPOLKIT_MAGIC); \
52 struct LibPolKitContext_s
57 /** Get a new context.
59 * @return Pointer to new context or NULL if an error occured
62 libpolkit_new_context (DBusConnection
*connection
)
64 LibPolKitContext
*ctx
;
66 ctx
= g_new0 (LibPolKitContext
, 1);
67 ctx
->magic
= LIBPOLKIT_MAGIC
;
74 * @param ctx The context obtained from libpolkit_new_context
75 * @return Pointer to new context or NULL if an error occured
78 libpolkit_free_context (LibPolKitContext
*ctx
)
80 LIBPOLKIT_CHECK_CONTEXT (ctx
, FALSE
);
88 libpolkit_get_allowed_resources_for_privilege_for_uid (LibPolKitContext
*ctx
,
90 const char *privilege
,
93 int *num_non_temporary
)
98 char **restriction_list
;
101 LIBPOLKIT_CHECK_CONTEXT (ctx
, LIBPOLKIT_RESULT_INVALID_CONTEXT
);
103 res
= LIBPOLKIT_RESULT_ERROR
;
105 *restrictions
= NULL
;
107 res
= LIBPOLKIT_RESULT_OK
;
113 libpolkit_is_uid_allowed_for_privilege (LibPolKitContext
*ctx
,
114 const char *system_bus_unique_name
,
116 const char *privilege
,
117 const char *resource
,
118 gboolean
*out_is_allowed
,
119 gboolean
*out_is_temporary
,
120 char **out_is_privileged_but_restricted_to_system_bus_unique_name
)
123 const char *myresource
= "";
124 const char *mysystem_bus_unique_name
= "";
125 char *but_restricted_to
= NULL
;
130 gboolean authname_free
= FALSE
;
132 LIBPOLKIT_CHECK_CONTEXT (ctx
, LIBPOLKIT_RESULT_INVALID_CONTEXT
);
134 uid
= (uid_t
)atol (user
);
135 if ((pw
= getpwuid (uid
)) == NULL
) {
136 *out_is_allowed
= FALSE
;
137 *out_is_temporary
= FALSE
;
138 return LIBPOLKIT_RESULT_NO_SUCH_USER
;
141 /* map PolicyKit privilege to RBAC authorization */
142 if (strcmp (privilege
, "hal-storage-removable-mount") == 0) {
143 authname
= "solaris.device.mount.removable";
144 } else if (strcmp (privilege
, "hal-storage-removable-mount-all-options") == 0) {
145 authname
= "solaris.device.mount.alloptions.removable";
146 } else if (strcmp (privilege
, "hal-storage-fixed-mount") == 0) {
147 authname
= "solaris.device.mount.fixed";
148 } else if (strcmp (privilege
, "hal-storage-fixed-mount-all-options") == 0) {
149 authname
= "solaris.device.mount.alloptions.fixed";
150 } else if (strcmp(privilege
, "hal-power-suspend") == 0) {
151 authname
= "solaris.system.power.suspend.ram";
152 } else if (strcmp(privilege
, "hal-power-hibernate") == 0) {
153 authname
= "solaris.system.power.suspend.disk";
154 } else if ((strcmp(privilege
, "hal-power-shutdown") == 0) ||
155 (strcmp(privilege
, "hal-power-reboot") == 0)) {
156 authname
= "solaris.system.shutdown";
157 } else if (strcmp(privilege
, "hal-power-cpu") == 0) {
158 authname
= "solaris.system.power.cpu";
159 } else if (strcmp(privilege
, "hal-power-brightness") == 0) {
160 authname
= "solaris.system.power.brightness";
161 } else if (strcmp (privilege
, "hal-power-cpu") == 0) {
162 authname
= "solaris.system.power.cpu";
164 /* replace '-' with '.' */
165 authname
= g_strdup (privilege
);
166 authname_free
= TRUE
;
167 for (i
= 0; i
< strlen (authname
); i
++) {
168 if (authname
[i
] == '-') {
174 *out_is_allowed
= (chkauthattr(authname
, pw
->pw_name
) != 0);
175 *out_is_temporary
= FALSE
;
181 return LIBPOLKIT_RESULT_OK
;
185 libpolkit_get_privilege_list (LibPolKitContext
*ctx
,
189 char **privilege_list
;
190 int num_privileges
= 0;
193 LIBPOLKIT_CHECK_CONTEXT (ctx
, LIBPOLKIT_RESULT_INVALID_CONTEXT
);
197 for (i
= 0; i
< num_privileges
; i
++) {
198 *result
= g_list_append (*result
, g_strdup (privilege_list
[i
]));
201 res
= LIBPOLKIT_RESULT_OK
;
207 libpolkit_revoke_temporary_privilege (LibPolKitContext
*ctx
,
209 const char *privilege
,
210 const char *resource
,
213 return LIBPOLKIT_RESULT_OK
;