File: gkd-ssh-agent-preload.c

package info (click to toggle)
gnome-keyring 3.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,404 kB
  • sloc: ansic: 77,137; sh: 4,856; makefile: 1,334; xml: 420; python: 331; sed: 16
file content (279 lines) | stat: -rw-r--r-- 7,777 bytes parent folder | download | duplicates (2)
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
/*
 * gnome-keyring
 *
 * Copyright (C) 2014 Stef Walter
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * 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 License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Stef Walter <stef@thewalter.net>, Daiki Ueno
 */

#include "config.h"

#include "gkd-ssh-agent-preload.h"
#include "gkd-ssh-agent-util.h"

#include "egg/egg-file-tracker.h"
#include <string.h>

enum {
	PROP_0,
	PROP_PATH
};

struct _GkdSshAgentPreload
{
	GObject object;

	gchar *path;
	GHashTable *keys_by_public_filename;
	GHashTable *keys_by_public_key;
	EggFileTracker *file_tracker;
	GMutex lock;
};

G_DEFINE_TYPE (GkdSshAgentPreload, gkd_ssh_agent_preload, G_TYPE_OBJECT);

void
gkd_ssh_agent_key_info_free (gpointer boxed)
{
	GkdSshAgentKeyInfo *info = boxed;
	if (!info)
		return;
	g_bytes_unref (info->public_key);
	g_free (info->comment);
	g_free (info->filename);
	g_free (info);
}

gpointer
gkd_ssh_agent_key_info_copy (gpointer boxed)
{
	GkdSshAgentKeyInfo *info = boxed;
	GkdSshAgentKeyInfo *copy = g_new0 (GkdSshAgentKeyInfo, 1);
	copy->public_key = g_bytes_ref (info->public_key);
	copy->comment = g_strdup (info->comment);
	copy->filename = g_strdup (info->filename);
	return copy;
}

static void file_load_inlock   (EggFileTracker *tracker,
                                const gchar *path,
                                gpointer user_data);
static void file_remove_inlock (EggFileTracker *tracker,
                                const gchar *path,
                                gpointer user_data);

static void
gkd_ssh_agent_preload_init (GkdSshAgentPreload *self)
{
	self->keys_by_public_filename = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	self->keys_by_public_key = g_hash_table_new_full (g_bytes_hash, g_bytes_equal, NULL, gkd_ssh_agent_key_info_free);
}

static void
gkd_ssh_agent_preload_constructed (GObject *object)
{
	GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (object);

	self->file_tracker = egg_file_tracker_new (self->path, "*.pub", NULL);
	g_signal_connect (self->file_tracker, "file-added", G_CALLBACK (file_load_inlock), self);
	g_signal_connect (self->file_tracker, "file-removed", G_CALLBACK (file_remove_inlock), self);
	g_signal_connect (self->file_tracker, "file-changed", G_CALLBACK (file_load_inlock), self);

	G_OBJECT_CLASS (gkd_ssh_agent_preload_parent_class)->constructed (object);
}

static void
gkd_ssh_agent_preload_set_property (GObject *object,
                                    guint prop_id,
                                    const GValue *value,
                                    GParamSpec *pspec)
{
	GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (object);

	switch (prop_id) {
	case PROP_PATH:
		self->path = g_value_dup_string (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gkd_ssh_agent_preload_finalize (GObject *object)
{
	GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (object);

	g_free (self->path);
	g_clear_pointer (&self->keys_by_public_key, (GDestroyNotify) g_hash_table_unref);
	g_clear_pointer (&self->keys_by_public_filename, (GDestroyNotify) g_hash_table_unref);
	g_clear_object (&self->file_tracker);

	g_mutex_clear (&self->lock);

	G_OBJECT_CLASS (gkd_ssh_agent_preload_parent_class)->finalize (object);
}

static void
gkd_ssh_agent_preload_class_init (GkdSshAgentPreloadClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	gobject_class->constructed = gkd_ssh_agent_preload_constructed;
	gobject_class->set_property = gkd_ssh_agent_preload_set_property;
	gobject_class->finalize = gkd_ssh_agent_preload_finalize;
	g_object_class_install_property (gobject_class, PROP_PATH,
		 g_param_spec_string ("path", "Path", "Path",
				      "",
				      G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
}

static gchar *
private_path_for_public (const gchar *public_path)
{
	if (g_str_has_suffix (public_path, ".pub"))
		return g_strndup (public_path, strlen (public_path) - 4);

	return NULL;
}

static GBytes *
file_get_contents (const gchar *path,
                   gboolean must_be_present)
{
	GError *error = NULL;
	gchar *contents;
	gsize length;

	if (!g_file_get_contents (path, &contents, &length, &error)) {
		if (must_be_present || error->code != G_FILE_ERROR_NOENT)
			g_message ("couldn't read file: %s: %s", path, error->message);
		g_error_free (error);
		return NULL;
	}

	return g_bytes_new_take (contents, length);
}

static void
file_remove_inlock (EggFileTracker *tracker,
                    const gchar *path,
                    gpointer user_data)
{
	GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (user_data);
	GkdSshAgentKeyInfo *info;

	info = g_hash_table_lookup (self->keys_by_public_filename, path);
	if (info) {
		g_hash_table_remove (self->keys_by_public_filename, path);
		g_hash_table_remove (self->keys_by_public_key, info->public_key);
	}
}

static void
file_load_inlock (EggFileTracker *tracker,
                  const gchar *path,
                  gpointer user_data)
{
	GkdSshAgentPreload *self = GKD_SSH_AGENT_PRELOAD (user_data);
	gchar *private_path;
	GBytes *private_bytes;
	GBytes *public_bytes;
	GBytes *public_key;
	GkdSshAgentKeyInfo *info;
	gchar *comment;

	file_remove_inlock (tracker, path, user_data);

	private_path = private_path_for_public (path);

	private_bytes = file_get_contents (private_path, FALSE);
	if (!private_bytes) {
		g_debug ("no private key present for public key: %s", path);
		g_free (private_path);
		return;
	}

	public_bytes = file_get_contents (path, TRUE);
	if (public_bytes) {
		public_key = _gkd_ssh_agent_parse_public_key (public_bytes, &comment);
		if (public_key) {
			info = g_new0 (GkdSshAgentKeyInfo, 1);
			info->filename = private_path;
			private_path = NULL;
			info->public_key = public_key;
			info->comment = comment;
			g_hash_table_replace (self->keys_by_public_filename, g_strdup (path), info);
			g_hash_table_replace (self->keys_by_public_key, info->public_key, info);
		} else {
			g_message ("failed to parse ssh public key: %s", path);
		}

		g_bytes_unref (public_bytes);
	}

	g_bytes_unref (private_bytes);
	g_free (private_path);
}

GkdSshAgentPreload *
gkd_ssh_agent_preload_new (const gchar *path)
{
	g_return_val_if_fail (path, NULL);

	return g_object_new (GKD_TYPE_SSH_AGENT_PRELOAD, "path", path, NULL);
}

GList *
gkd_ssh_agent_preload_get_keys (GkdSshAgentPreload *self)
{
	GList *keys = NULL;
	GHashTableIter iter;
	GkdSshAgentKeyInfo *info;

	g_mutex_lock (&self->lock);

	egg_file_tracker_refresh (self->file_tracker, FALSE);

	g_hash_table_iter_init (&iter, self->keys_by_public_key);
	while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&info))
		keys = g_list_prepend (keys, gkd_ssh_agent_key_info_copy (info));

	g_mutex_unlock (&self->lock);

	return keys;
}

GkdSshAgentKeyInfo *
gkd_ssh_agent_preload_lookup_by_public_key (GkdSshAgentPreload *self,
					    GBytes *public_key)
{
	GkdSshAgentKeyInfo *info;

	g_mutex_lock (&self->lock);

	egg_file_tracker_refresh (self->file_tracker, FALSE);

	info = g_hash_table_lookup (self->keys_by_public_key, public_key);
	if (info)
		info = gkd_ssh_agent_key_info_copy (info);

	g_mutex_unlock (&self->lock);

	return info;
}