secret-attributes.c 8.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/* libsecret - GLib wrapper for Secret Service
 *
 * Copyright 2011 Collabora Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: Stef Walter <stefw@gnome.org>
 */

#include "config.h"

#include "secret-attributes.h"
#include "secret-private.h"

#include <string.h>

/**
 * SECTION:secret-attributes
 * @title: Secret Attributes
 * @short_description: secret attributes
 *
 * Each item has a set of attributes, which are used to locate the item later.
 * These are not stored or transferred in a secure manner. Each attribute has
 * a string name and a string value. These attributes are represented by a
 * #GHashTable with string keys and values.
 *
 * Use secret_attributes_build() to simply build up a set of attributes.
 */

GVariant *
_secret_attributes_to_variant (GHashTable *attributes,
                               const gchar *schema_name)
{
	GHashTableIter iter;
	GVariantBuilder builder;
	const gchar *name;
	const gchar *value;

	g_return_val_if_fail (attributes != NULL, NULL);

	g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));

	g_hash_table_iter_init (&iter, attributes);
	while (g_hash_table_iter_next (&iter, (gpointer *)&name, (gpointer *)&value)) {
		if (!schema_name || !g_str_equal (name, "xdg:schema"))
			g_variant_builder_add (&builder, "{ss}", name, value);
	}

	if (schema_name)
		g_variant_builder_add (&builder, "{ss}", "xdg:schema", schema_name);

	return g_variant_builder_end (&builder);
}

GHashTable *
_secret_attributes_for_variant (GVariant *variant)
{
	GVariantIter iter;
	GHashTable *attributes;
	gchar *value;
	gchar *key;

	attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

	g_variant_iter_init (&iter, variant);
	while (g_variant_iter_next (&iter, "{ss}", &key, &value))
		g_hash_table_insert (attributes, key, value);

	return attributes;
}

/**
 * secret_attributes_build: (skip)
 * @schema: the schema for the attributes
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Build up a hash table of attribute values.
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the password
 * @schema. The list of attribtues should be terminated with a %NULL.
 *
89 90
 * Returns: (transfer full) (element-type utf8 utf8): a new table of
 *          attributes, to be released with g_hash_table_unref()
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
 */
GHashTable *
secret_attributes_build (const SecretSchema *schema,
                         ...)
{
	GHashTable *attributes;
	va_list va;

	va_start (va, schema);
	attributes = secret_attributes_buildv (schema, va);
	va_end (va);

	return attributes;
}

/**
 * secret_attributes_buildv: (skip)
 * @schema: the schema for the attributes
 * @va: the attribute keys and values, terminated with %NULL
 *
 * Build up a hash table of attribute values.
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the password
 * @schema. The list of attribtues should be terminated with a %NULL.
 *
118 119
 * Returns: (transfer full) (element-type utf8 utf8): a new table of
 *          attributes, to be released with g_hash_table_unref()
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
 */
GHashTable *
secret_attributes_buildv (const SecretSchema *schema,
                          va_list va)
{
	const gchar *attribute_name;
	SecretSchemaAttributeType type;
	GHashTable *attributes;
	const gchar *string;
	gboolean type_found;
	gchar *value = NULL;
	gboolean boolean;
	gint integer;
	gint i;

	g_return_val_if_fail (schema != NULL, NULL);

	attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

	for (;;) {
		attribute_name = va_arg (va, const gchar *);
		if (attribute_name == NULL)
			break;

		type_found = FALSE;
		for (i = 0; i < G_N_ELEMENTS (schema->attributes); ++i) {
			if (!schema->attributes[i].name)
				break;
			if (g_str_equal (schema->attributes[i].name, attribute_name)) {
				type_found = TRUE;
				type = schema->attributes[i].type;
				break;
			}
		}

		if (!type_found) {
156
			g_critical ("The attribute '%s' was not found in the password schema.", attribute_name);
157 158 159 160 161 162 163 164 165 166 167
			g_hash_table_unref (attributes);
			return NULL;
		}

		switch (type) {
		case SECRET_SCHEMA_ATTRIBUTE_BOOLEAN:
			boolean = va_arg (va, gboolean);
			value = g_strdup (boolean ? "true" : "false");
			break;
		case SECRET_SCHEMA_ATTRIBUTE_STRING:
			string = va_arg (va, gchar *);
168 169 170 171
			if (string == NULL) {
				g_critical ("The value for attribute '%s' was NULL", attribute_name);
				return NULL;
			}
172
			if (!g_utf8_validate (string, -1, NULL)) {
173
				g_critical ("The value for attribute '%s' was not a valid UTF-8 string.", attribute_name);
174 175 176 177 178 179 180 181 182 183
				g_hash_table_unref (attributes);
				return NULL;
			}
			value = g_strdup (string);
			break;
		case SECRET_SCHEMA_ATTRIBUTE_INTEGER:
			integer = va_arg (va, gint);
			value = g_strdup_printf ("%d", integer);
			break;
		default:
184
			g_critical ("The password attribute '%s' has an invalid type in the password schema.", attribute_name);
185 186 187 188 189 190 191 192 193 194 195 196
			g_hash_table_unref (attributes);
			return NULL;
		}

		g_hash_table_insert (attributes, g_strdup (attribute_name), value);
	}

	return attributes;
}

gboolean
_secret_attributes_validate (const SecretSchema *schema,
197 198 199
                             GHashTable *attributes,
                             const char *pretty_function,
                             gboolean matching)
200 201 202
{
	const SecretSchemaAttribute *attribute;
	GHashTableIter iter;
203
	gboolean any = FALSE;
204 205 206 207 208 209 210 211 212
	gchar *key;
	gchar *value;
	gchar *end;
	gint i;

	g_return_val_if_fail (schema != NULL, FALSE);

	g_hash_table_iter_init (&iter, attributes);
	while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&value)) {
213
		any = TRUE;
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
		/* If the 'xdg:schema' meta-attribute is present,
		   ensure that it is consistent with the schema
		   name. */
		if (g_str_equal (key, "xdg:schema")) {
			if (!g_str_equal (value, schema->name)) {
				g_critical ("%s: xdg:schema value %s differs from schema %s:",
					    pretty_function, value, schema->name);
				return FALSE;
			}
			continue;
		}

		/* Pass through libgnomekeyring specific attributes */
		if (g_str_has_prefix (key, "gkr:"))
			continue;

231 232 233 234 235 236 237 238 239 240 241 242
		/* Find the attribute */
		attribute = NULL;
		for (i = 0; i < G_N_ELEMENTS (schema->attributes); i++) {
			if (schema->attributes[i].name == NULL)
				break;
			if (g_str_equal (schema->attributes[i].name, key)) {
				attribute = &schema->attributes[i];
				break;
			}
		}

		if (attribute == NULL) {
243
			g_critical ("%s: invalid %s attribute for %s schema",
244
			            pretty_function, key, schema->name);
245 246 247 248 249 250
			return FALSE;
		}

		switch (attribute->type) {
		case SECRET_SCHEMA_ATTRIBUTE_BOOLEAN:
			if (!g_str_equal (value, "true") && !g_str_equal (value, "false")) {
251 252
				g_critical ("%s: invalid %s boolean value for %s schema: %s",
				            pretty_function, key, schema->name, value);
253 254 255 256 257 258 259
				return FALSE;
			}
			break;
		case SECRET_SCHEMA_ATTRIBUTE_INTEGER:
			end = NULL;
			g_ascii_strtoll (value, &end, 10);
			if (!end || end[0] != '\0') {
260 261
				g_warning ("%s: invalid %s integer value for %s schema: %s",
				           pretty_function, key, schema->name, value);
262 263 264 265 266
				return FALSE;
			}
			break;
		case SECRET_SCHEMA_ATTRIBUTE_STRING:
			if (!g_utf8_validate (value, -1, NULL)) {
267 268
				g_warning ("%s: invalid %s string value for %s schema: %s",
				           pretty_function, key, schema->name, value);
269 270 271 272
				return FALSE;
			}
			break;
		default:
273 274
			g_warning ("%s: invalid %s value type in %s schema",
			           pretty_function, key, schema->name);
275 276 277 278
			return FALSE;
		}
	}

279 280 281 282 283 284 285
	/* Nothing to match on, resulting search would match everything :S */
	if (matching && !any && schema->flags & SECRET_SCHEMA_DONT_MATCH_NAME) {
		g_warning ("%s: must specify at least one attribute to match",
		           pretty_function);
		return FALSE;
	}

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	return TRUE;
}

GHashTable *
_secret_attributes_copy (GHashTable *attributes)
{
	GHashTableIter iter;
	GHashTable *copy;
	gchar *key;
	gchar *value;

	if (attributes == NULL)
		return NULL;

	copy = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

	g_hash_table_iter_init (&iter, attributes);
	while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&value))
		g_hash_table_insert (copy, g_strdup (key), g_strdup (value));

	return copy;
}