| 1 |
/* |
| 2 |
* Copyright (C) 2008 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General |
| 15 |
* Public License along with this library; if not, write to the |
| 16 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 17 |
* Boston, MA 02111-1307, USA. |
| 18 |
* |
| 19 |
* Author: David Zeuthen <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <string.h> |
| 23 |
#include "polkitimplicitauthorization.h" |
| 24 |
#include "polkittemporaryauthorization.h" |
| 25 |
|
| 26 |
#include "polkitprivate.h" |
| 27 |
|
| 28 |
/** |
| 29 |
* SECTION:polkittemporaryauthorization |
| 30 |
* @title: PolkitTemporaryAuthorization |
| 31 |
* @short_description: Temporary Authorizations |
| 32 |
* |
| 33 |
* Object used to describe a temporary authorization. |
| 34 |
*/ |
| 35 |
|
| 36 |
/** |
| 37 |
* PolkitTemporaryAuthorization: |
| 38 |
* |
| 39 |
* The #PolkitTemporaryAuthorization struct should not be accessed directly. |
| 40 |
*/ |
| 41 |
struct _PolkitTemporaryAuthorization |
| 42 |
{ |
| 43 |
GObject parent_instance; |
| 44 |
|
| 45 |
gchar *id; |
| 46 |
gchar *action_id; |
| 47 |
PolkitSubject *subject; |
| 48 |
guint64 time_obtained; |
| 49 |
guint64 time_expires; |
| 50 |
}; |
| 51 |
|
| 52 |
struct _PolkitTemporaryAuthorizationClass |
| 53 |
{ |
| 54 |
GObjectClass parent_class; |
| 55 |
}; |
| 56 |
|
| 57 |
G_DEFINE_TYPE (PolkitTemporaryAuthorization, polkit_temporary_authorization, G_TYPE_OBJECT); |
| 58 |
|
| 59 |
static void |
| 60 |
polkit_temporary_authorization_init (PolkitTemporaryAuthorization *authorization) |
| 61 |
{ |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
polkit_temporary_authorization_finalize (GObject *object) |
| 66 |
{ |
| 67 |
PolkitTemporaryAuthorization *authorization = POLKIT_TEMPORARY_AUTHORIZATION (object); |
| 68 |
|
| 69 |
g_free (authorization->id); |
| 70 |
g_free (authorization->action_id); |
| 71 |
g_object_unref (authorization->subject); |
| 72 |
|
| 73 |
if (G_OBJECT_CLASS (polkit_temporary_authorization_parent_class)->finalize != NULL) |
| 74 |
G_OBJECT_CLASS (polkit_temporary_authorization_parent_class)->finalize (object); |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
polkit_temporary_authorization_class_init (PolkitTemporaryAuthorizationClass *klass) |
| 79 |
{ |
| 80 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 81 |
|
| 82 |
gobject_class->finalize = polkit_temporary_authorization_finalize; |
| 83 |
} |
| 84 |
|
| 85 |
PolkitTemporaryAuthorization * |
| 86 |
polkit_temporary_authorization_new (const gchar *id, |
| 87 |
const gchar *action_id, |
| 88 |
PolkitSubject *subject, |
| 89 |
guint64 time_obtained, |
| 90 |
guint64 time_expires) |
| 91 |
{ |
| 92 |
PolkitTemporaryAuthorization *authorization; |
| 93 |
authorization = POLKIT_TEMPORARY_AUTHORIZATION (g_object_new (POLKIT_TYPE_TEMPORARY_AUTHORIZATION, NULL)); |
| 94 |
authorization->id = g_strdup (id); |
| 95 |
authorization->action_id = g_strdup (action_id); |
| 96 |
authorization->subject = g_object_ref (subject); |
| 97 |
authorization->time_obtained = time_obtained; |
| 98 |
authorization->time_expires = time_expires; |
| 99 |
return authorization; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* polkit_temporary_authorization_get_id: |
| 104 |
* @authorization: A #PolkitTemporaryAuthorization. |
| 105 |
* |
| 106 |
* Gets the opaque identifier for @authorization. |
| 107 |
* |
| 108 |
* Returns: A string owned by @authorization. Do not free. |
| 109 |
*/ |
| 110 |
const gchar * |
| 111 |
polkit_temporary_authorization_get_id (PolkitTemporaryAuthorization *authorization) |
| 112 |
{ |
| 113 |
g_return_val_if_fail (POLKIT_IS_TEMPORARY_AUTHORIZATION (authorization), NULL); |
| 114 |
return authorization->id; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* polkit_temporary_authorization_get_action_id: |
| 119 |
* @authorization: A #PolkitTemporaryAuthorization. |
| 120 |
* |
| 121 |
* Gets the action that @authorization is for. |
| 122 |
* |
| 123 |
* Returns: A string owned by @authorization. Do not free. |
| 124 |
**/ |
| 125 |
const gchar * |
| 126 |
polkit_temporary_authorization_get_action_id (PolkitTemporaryAuthorization *authorization) |
| 127 |
{ |
| 128 |
g_return_val_if_fail (POLKIT_IS_TEMPORARY_AUTHORIZATION (authorization), NULL); |
| 129 |
return authorization->action_id; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* polkit_temporary_authorization_get_subject: |
| 134 |
* @authorization: A #PolkitTemporaryAuthorization. |
| 135 |
* |
| 136 |
* Gets the subject that @authorization is for. |
| 137 |
* |
| 138 |
* Returns: (transfer full): A #PolkitSubject, free with g_object_unref(). |
| 139 |
**/ |
| 140 |
PolkitSubject * |
| 141 |
polkit_temporary_authorization_get_subject (PolkitTemporaryAuthorization *authorization) |
| 142 |
{ |
| 143 |
g_return_val_if_fail (POLKIT_IS_TEMPORARY_AUTHORIZATION (authorization), NULL); |
| 144 |
return g_object_ref (authorization->subject); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* polkit_temporary_authorization_get_time_obtained: |
| 149 |
* @authorization: A #PolkitTemporaryAuthorization. |
| 150 |
* |
| 151 |
* Gets the time when @authorization was obtained. |
| 152 |
* |
| 153 |
* (Note that the PolicyKit daemon is using monotonic time internally |
| 154 |
* so the returned value may change if system time changes.) |
| 155 |
* |
| 156 |
* Returns: Seconds since the Epoch Jan 1. 1970, 0:00 UTC. |
| 157 |
**/ |
| 158 |
guint64 |
| 159 |
polkit_temporary_authorization_get_time_obtained (PolkitTemporaryAuthorization *authorization) |
| 160 |
{ |
| 161 |
g_return_val_if_fail (POLKIT_IS_TEMPORARY_AUTHORIZATION (authorization), 0); |
| 162 |
return authorization->time_obtained; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* polkit_temporary_authorization_get_time_expires: |
| 167 |
* @authorization: A #PolkitTemporaryAuthorization. |
| 168 |
* |
| 169 |
* Gets the time when @authorization will expire. |
| 170 |
* |
| 171 |
* (Note that the PolicyKit daemon is using monotonic time internally |
| 172 |
* so the returned value may change if system time changes.) |
| 173 |
* |
| 174 |
* Returns: Seconds since the Epoch Jan 1. 1970, 0:00 UTC. |
| 175 |
**/ |
| 176 |
guint64 |
| 177 |
polkit_temporary_authorization_get_time_expires (PolkitTemporaryAuthorization *authorization) |
| 178 |
{ |
| 179 |
g_return_val_if_fail (POLKIT_IS_TEMPORARY_AUTHORIZATION (authorization), 0); |
| 180 |
return authorization->time_expires; |
| 181 |
} |
| 182 |
|
| 183 |
PolkitTemporaryAuthorization * |
| 184 |
polkit_temporary_authorization_new_for_gvariant (GVariant *value, |
| 185 |
GError **error) |
| 186 |
{ |
| 187 |
PolkitTemporaryAuthorization *authorization; |
| 188 |
GVariant *subject_gvariant; |
| 189 |
|
| 190 |
authorization = POLKIT_TEMPORARY_AUTHORIZATION (g_object_new (POLKIT_TYPE_TEMPORARY_AUTHORIZATION, NULL)); |
| 191 |
g_variant_get (value, |
| 192 |
"(ss@(sa{sv})tt)", |
| 193 |
&authorization->id, |
| 194 |
&authorization->action_id, |
| 195 |
&subject_gvariant, |
| 196 |
&authorization->time_obtained, |
| 197 |
&authorization->time_expires); |
| 198 |
authorization->subject = polkit_subject_new_for_gvariant (subject_gvariant, error); |
| 199 |
if (authorization->subject == NULL) |
| 200 |
{ |
| 201 |
g_object_unref (authorization); |
| 202 |
authorization = NULL; |
| 203 |
goto out; |
| 204 |
} |
| 205 |
|
| 206 |
out: |
| 207 |
g_variant_unref (subject_gvariant); |
| 208 |
return authorization; |
| 209 |
} |
| 210 |
|
| 211 |
/* Note that this returns a floating value. */ |
| 212 |
GVariant * |
| 213 |
polkit_temporary_authorization_to_gvariant (PolkitTemporaryAuthorization *authorization) |
| 214 |
{ |
| 215 |
return g_variant_new ("(ss@(sa{sv})tt)", |
| 216 |
authorization->id, |
| 217 |
authorization->action_id, |
| 218 |
polkit_subject_to_gvariant (authorization->subject), /* A floating value */ |
| 219 |
authorization->time_obtained, |
| 220 |
authorization->time_expires); |
| 221 |
} |
| 222 |
|