summaryrefslogtreecommitdiff
path: root/libsecret/secret-attributes.c
blob: 603ef7a4d4edebc6bc5adf5e293d836cf1ccb375 (plain)
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
89
90
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
118
119
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* 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>

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 attributes should be terminated with a %NULL.
 *
 * Returns: (transfer full) (element-type utf8 utf8): a new table of
 *   attributes, to be released with [func@GLib.HashTable.unref]
 */
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 attributes should be terminated with a %NULL.
 *
 * Returns: (transfer full) (element-type utf8 utf8): a new table of
 *   attributes, to be released with [func@GLib.HashTable.unref]
 */
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) {
			g_critical ("The attribute '%s' was not found in the password schema.", attribute_name);
			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 *);
			if (string == NULL) {
				g_critical ("The value for attribute '%s' was NULL", attribute_name);
				return NULL;
			}
			if (!g_utf8_validate (string, -1, NULL)) {
				g_critical ("The value for attribute '%s' was not a valid UTF-8 string.", attribute_name);
				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:
			g_critical ("The password attribute '%s' has an invalid type in the password schema.", attribute_name);
			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,
                             GHashTable *attributes,
                             const char *pretty_function,
                             gboolean matching)
{
	const SecretSchemaAttribute *attribute;
	GHashTableIter iter;
	gboolean any = FALSE;
	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)) {
		any = TRUE;

		/* 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;

		/* 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) {
			g_critical ("%s: invalid %s attribute for %s schema",
			            pretty_function, key, schema->name);
			return FALSE;
		}

		switch (attribute->type) {
		case SECRET_SCHEMA_ATTRIBUTE_BOOLEAN:
			if (!g_str_equal (value, "true") && !g_str_equal (value, "false")) {
				g_critical ("%s: invalid %s boolean value for %s schema: %s",
				            pretty_function, key, schema->name, value);
				return FALSE;
			}
			break;
		case SECRET_SCHEMA_ATTRIBUTE_INTEGER:
			end = NULL;
			g_ascii_strtoll (value, &end, 10);
			if (!end || end[0] != '\0') {
				g_warning ("%s: invalid %s integer value for %s schema: %s",
				           pretty_function, key, schema->name, value);
				return FALSE;
			}
			break;
		case SECRET_SCHEMA_ATTRIBUTE_STRING:
			if (!g_utf8_validate (value, -1, NULL)) {
				g_warning ("%s: invalid %s string value for %s schema: %s",
				           pretty_function, key, schema->name, value);
				return FALSE;
			}
			break;
		default:
			g_warning ("%s: invalid %s value type in %s schema",
			           pretty_function, key, schema->name);
			return FALSE;
		}
	}

	/* 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;
	}

	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;
}