1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of gsignond
6 * Copyright (C) 2012 Intel Corporation.
8 * Contact: Jussi Laako <jussi.laako@linux.intel.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include "gsignond/gsignond-security-context.h"
30 * SECTION:gsignond-security-context
31 * @title: GSignondSecurityContext
32 * @short_description: security context descriptor used in access control checks
33 * @include: gsignond/gsignond-security-context.h
35 * Security context is a string tuple of system context and application context.
37 * System context can be a binary path, SMACK-label, or MSSF token.
39 * Application context identifies a script or a webpage within an application,
40 * and it's used for providing access control to runtime environments (when making an access
41 * control decision requires not only a binary identifier, but also information
42 * about what the binary is doing).
44 * When an application is trying to access the gSSO service, the system context
45 * is determined by a specific #GSignondAccessControlManager instance using
46 * system services of a specific platform. Application context is set by the
47 * application itself. Then both contexts are used by #GSignondAccessControlManager
48 * to perform an access control check.
52 * GSignondSecurityContext:
53 * @sys_ctx: system context
54 * @app_ctx: application context
56 * Security context descriptor used for access control checks. System context
57 * and application context can contain a wildcard match "*" which has special
58 * meaning in gsignond_security_context_match() and
59 * gsignond_security_context_check().
63 * GSignondSecurityContextList:
65 * GList of #GSignondSecurityContext items.
68 _security_context_free (gpointer ptr)
70 GSignondSecurityContext *ctx = (GSignondSecurityContext *) ptr;
72 gsignond_security_context_free (ctx);
76 * gsignond_security_context_new:
78 * Allocates a new security context item. System and app context are empty strings.
80 * Returns: (transfer full): allocated #GSignondSecurityContext.
82 GSignondSecurityContext *
83 gsignond_security_context_new ()
85 GSignondSecurityContext *ctx;
87 ctx = g_slice_new0 (GSignondSecurityContext);
88 ctx->sys_ctx = g_strdup ("");
89 ctx->app_ctx = g_strdup ("");
95 * gsignond_security_context_new_from_values:
96 * @system_context: system security context
97 * @application_context: application security context
99 * Allocates and initializes a new security context item.
101 * Returns: (transfer full): allocated #GSignondSecurityContext.
103 GSignondSecurityContext *
104 gsignond_security_context_new_from_values (const gchar *system_context,
105 const gchar *application_context)
107 GSignondSecurityContext *ctx;
109 g_return_val_if_fail (system_context != NULL, NULL);
111 ctx = g_slice_new0 (GSignondSecurityContext);
112 ctx->sys_ctx = g_strdup (system_context);
113 if (application_context)
114 ctx->app_ctx = g_strdup (application_context);
116 ctx->app_ctx = g_strdup ("");
122 * gsignond_security_context_copy:
123 * @src_ctx: source security context to copy.
125 * Copies a security context item.
127 * Returns: (transfer full): a copy of the #GSignondSecurityContext item.
129 GSignondSecurityContext *
130 gsignond_security_context_copy (const GSignondSecurityContext *src_ctx)
132 g_return_val_if_fail (src_ctx != NULL, NULL);
134 return gsignond_security_context_new_from_values (src_ctx->sys_ctx,
139 * gsignond_security_context_free:
140 * @ctx: #GSignondSecurityContext to be freed.
142 * Frees a security context item.
145 gsignond_security_context_free (GSignondSecurityContext *ctx)
147 if (ctx == NULL) return;
149 g_free (ctx->sys_ctx);
150 g_free (ctx->app_ctx);
151 g_slice_free (GSignondSecurityContext, ctx);
155 * gsignond_security_context_set_system_context:
156 * @ctx: #GSignondSecurityContext item.
157 * @system_context: system security context.
159 * Sets the system context part of the
160 * #GSignondSecurityContext.
163 gsignond_security_context_set_system_context (GSignondSecurityContext *ctx,
164 const gchar *system_context)
166 g_return_if_fail (ctx != NULL);
168 g_free (ctx->sys_ctx);
169 ctx->sys_ctx = (system_context) ?
170 g_strdup (system_context) : g_strdup ("");
174 * gsignond_security_context_get_system_context:
175 * @ctx: #GSignondSecurityContext item.
177 * Get the system context partof the
178 * #GSignondSecurityContext.
180 * Returns: (transfer none): system context.
183 gsignond_security_context_get_system_context (
184 const GSignondSecurityContext *ctx)
186 g_return_val_if_fail (ctx != NULL, NULL);
192 * gsignond_security_context_set_application_context:
193 * @ctx: #GSignondSecurityContext item.
194 * @application_context: application security context.
196 * Sets the application context part of
197 * the #GSignondSecurityContext.
200 gsignond_security_context_set_application_context (
201 GSignondSecurityContext *ctx,
202 const gchar *application_context)
204 g_return_if_fail (ctx != NULL);
206 g_free (ctx->app_ctx);
207 ctx->app_ctx = (application_context) ?
208 g_strdup (application_context) : g_strdup ("");
212 * gsignond_security_context_get_application_context:
213 * @ctx: #GSignondSecurityContext item.
215 * Get the application context part of
216 * the #GSignondSecurityContext.
218 * Returns: (transfer none): application context.
221 gsignond_security_context_get_application_context (
222 const GSignondSecurityContext *ctx)
224 g_return_val_if_fail (ctx != NULL, NULL);
230 * gsignond_security_context_to_variant:
231 * @ctx: #GSignondSecurityContext item.
233 * Build a GVariant of type "(ss)" from a #GSignondSecurityContext item.
235 * Returns: (transfer full): GVariant construct of a #GSignondSecurityContext.
238 gsignond_security_context_to_variant (const GSignondSecurityContext *ctx)
242 g_return_val_if_fail (ctx != NULL, NULL);
244 variant = g_variant_new ("(ss)",
245 ctx->sys_ctx ? ctx->sys_ctx : "",
246 ctx->app_ctx ? ctx->app_ctx : "");
252 * gsignond_security_context_from_variant:
253 * @variant: GVariant item with a #GSignondSecurityContext construct.
255 * Builds a #GSignondSecurityContext item from a GVariant of type "(ss)".
257 * Returns: (transfer full): #GSignondSecurityContext item.
259 GSignondSecurityContext *
260 gsignond_security_context_from_variant (GVariant *variant)
262 gchar *sys_ctx = NULL;
263 gchar *app_ctx = NULL;
264 GSignondSecurityContext *ctx;
266 g_return_val_if_fail (variant != NULL, NULL);
268 g_variant_get (variant, "(ss)", &sys_ctx, &app_ctx);
269 ctx = gsignond_security_context_new_from_values (sys_ctx, app_ctx);
276 * gsignond_security_context_compare:
277 * @ctx1: first item to compare.
278 * @ctx2: second item to compare.
280 * Compare two #GSignondSecurityContext items in a similar way to strcmp().
282 * Returns: negative if ctx1 < ctx2, 0 if ctx1 == ctx2 and positive if ctx1 > ctx2.
285 gsignond_security_context_compare (const GSignondSecurityContext *ctx1,
286 const GSignondSecurityContext *ctx2)
290 if (ctx1 == ctx2) return 0;
297 res = g_strcmp0(ctx1->sys_ctx, ctx2->sys_ctx);
299 res = g_strcmp0(ctx1->app_ctx, ctx2->app_ctx);
305 * gsignond_security_context_match:
306 * @ctx1: first item to compare.
307 * @ctx2: second item to compare.
309 * Compare two #GSignondSecurityContext items match.
311 * Returns: TRUE if contexts are equal or if either side has a wildcard match for
312 * system context, or if system contexts are equal and either side has a wildcard
313 * match for the app context,
314 * otherwise FALSE. Two NULL contexts match.
317 gsignond_security_context_match (const GSignondSecurityContext *ctx1,
318 const GSignondSecurityContext *ctx2)
320 if (ctx1 == ctx2) return TRUE;
322 if (ctx1 == NULL || ctx2 == NULL)
325 if (g_strcmp0(ctx1->sys_ctx, "*") == 0 ||
326 g_strcmp0(ctx2->sys_ctx, "*") == 0) return TRUE;
328 if (g_strcmp0(ctx1->sys_ctx, ctx2->sys_ctx) == 0) {
329 if (g_strcmp0(ctx1->app_ctx, "*") == 0 ||
330 g_strcmp0(ctx2->app_ctx, "*") == 0) return TRUE;
331 if (g_strcmp0(ctx1->app_ctx, ctx2->app_ctx) == 0) return TRUE;
338 * gsignond_security_context_check:
339 * @reference: reference security context item to check against.
340 * @test: security context item to be checked.
342 * Check if @test is covered by @reference.
344 * Returns: TRUE if contexts are equal or the @reference has a wildcard
345 * system context, or if system contexts are equal and @reference has a wildcard
346 * application context, otherwise FALSE. If either or both contexts are NULL,
350 gsignond_security_context_check (const GSignondSecurityContext *reference,
351 const GSignondSecurityContext *test)
353 if (reference == NULL || test == NULL)
356 if (g_strcmp0(reference->sys_ctx, "*") == 0) return TRUE;
357 if (g_strcmp0(reference->sys_ctx, test->sys_ctx) == 0) {
358 if (g_strcmp0(reference->app_ctx, "*") == 0) return TRUE;
359 if (g_strcmp0(reference->app_ctx, test->app_ctx) == 0) return TRUE;
366 * gsignond_security_context_list_to_variant:
367 * @list: #GSignondSecurityContextList item.
369 * Builds a GVariant of type "a(ss)" from a GList of #GSignondSecurityContext
372 * Returns: (transfer full): GVariant construct of a #GSignondSecurityContextList.
375 gsignond_security_context_list_to_variant (
376 const GSignondSecurityContextList *list)
378 GVariantBuilder builder;
380 GSignondSecurityContext *ctx;
382 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
383 for ( ; list != NULL; list = g_list_next (list)) {
384 ctx = (GSignondSecurityContext *) list->data;
385 g_variant_builder_add_value (
387 gsignond_security_context_to_variant (ctx));
389 variant = g_variant_builder_end (&builder);
395 * gsignond_security_context_list_from_variant:
396 * @variant: GVariant item with a list of security context tuples.
398 * Builds a GList of #GSignondSecurityContext items from a GVariant of type
401 * Returns: (transfer full): #GSignondSecurityContextList item.
403 GSignondSecurityContextList *
404 gsignond_security_context_list_from_variant (GVariant *variant)
406 GSignondSecurityContextList *list = NULL;
410 g_return_val_if_fail (variant != NULL, NULL);
412 g_variant_iter_init (&iter, variant);
413 while ((value = g_variant_iter_next_value (&iter))) {
414 list = g_list_append (list,
415 gsignond_security_context_from_variant (value));
416 g_variant_unref (value);
423 * gsignond_security_context_list_copy:
424 * @src_list: source #GSignondSecurityContextList.
426 * Copies a GList of #GSignondSecurityContext items.
428 * Returns: (transfer full): #GSignondSecurityContextList item.
430 GSignondSecurityContextList *
431 gsignond_security_context_list_copy (
432 const GSignondSecurityContextList *src_list)
434 GSignondSecurityContext *ctx;
435 GSignondSecurityContextList *dst_list = NULL;
437 for ( ; src_list != NULL; src_list = g_list_next (src_list)) {
438 ctx = (GSignondSecurityContext *) src_list->data;
439 dst_list = g_list_append (dst_list,
440 gsignond_security_context_copy (ctx));
447 * gsignond_security_context_list_free:
448 * @seclist: (transfer full): #GSignondSecurityContextList item.
450 * Frees all items and the GList of #GSignondSecurityContext.
453 gsignond_security_context_list_free (GSignondSecurityContextList *seclist)
455 g_list_free_full (seclist, _security_context_free);