File: gkd-ssh-agent-process.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 (270 lines) | stat: -rw-r--r-- 6,298 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
/*
 * 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-process.h"
#include "gkd-ssh-agent-private.h"
#include "gkd-ssh-agent-util.h"

#include <gio/gunixsocketaddress.h>
#include <glib-unix.h>
#include <glib/gstdio.h>

enum {
	PROP_0,
	PROP_PATH
};

enum {
	CLOSED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

struct _GkdSshAgentProcess
{
	GObject object;
	gchar *path;
	gint output;
	GMutex lock;
	GPid pid;
	guint output_id;
	guint child_id;
	gboolean ready;
};

G_DEFINE_TYPE (GkdSshAgentProcess, gkd_ssh_agent_process, G_TYPE_OBJECT);

static void
gkd_ssh_agent_process_init (GkdSshAgentProcess *self)
{
	self->output = -1;
	g_mutex_init (&self->lock);
}

static void
gkd_ssh_agent_process_finalize (GObject *object)
{
	GkdSshAgentProcess *self = GKD_SSH_AGENT_PROCESS (object);

	if (self->output != -1)
		close (self->output);
	if (self->output_id)
		g_source_remove (self->output_id);
	if (self->child_id)
		g_source_remove (self->child_id);
	if (self->pid)
		kill (self->pid, SIGTERM);
	g_unlink (self->path);
	g_free (self->path);
	g_mutex_clear (&self->lock);

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

static void
gkd_ssh_agent_process_set_property (GObject *object,
                                    guint prop_id,
                                    const GValue *value,
                                    GParamSpec *pspec)
{
	GkdSshAgentProcess *self = GKD_SSH_AGENT_PROCESS (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_process_class_init (GkdSshAgentProcessClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	gobject_class->finalize = gkd_ssh_agent_process_finalize;
	gobject_class->set_property = gkd_ssh_agent_process_set_property;
	g_object_class_install_property (gobject_class, PROP_PATH,
		 g_param_spec_string ("path", "Path", "Path",
				      "",
				      G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
	signals[CLOSED] = g_signal_new_class_handler ("closed",
						      G_TYPE_FROM_CLASS (klass),
						      G_SIGNAL_RUN_LAST,
						      NULL, NULL, NULL, NULL,
						      G_TYPE_NONE, 0);
}

static void
on_child_watch (GPid pid,
                gint status,
                gpointer user_data)
{
	GkdSshAgentProcess *self = GKD_SSH_AGENT_PROCESS (user_data);
	GError *error = NULL;

	if (pid != self->pid)
		return;

	g_mutex_lock (&self->lock);

	self->pid = 0;
	self->output_id = 0;
	self->child_id = 0;

	if (!g_spawn_check_exit_status (status, &error)) {
		g_message ("ssh-agent: %s", error->message);
		g_error_free (error);
	}

	g_spawn_close_pid (pid);

	g_mutex_unlock (&self->lock);

	g_signal_emit (self, signals[CLOSED], 0);
}

static gboolean
on_output_watch (gint fd,
		 GIOCondition condition,
		 gpointer user_data)
{
	GkdSshAgentProcess *self = GKD_SSH_AGENT_PROCESS (user_data);
	guint8 buf[1024];
	gssize len;

	if (condition & G_IO_IN) {
		self->ready = TRUE;

		len = read (fd, buf, sizeof (buf));
		if (len < 0) {
			if (errno != EAGAIN && errno != EINTR)
				g_message ("couldn't read from ssh-agent stdout: %m");
			condition |= G_IO_ERR;
		}
	}

	if (condition & G_IO_HUP || condition & G_IO_ERR)
		return FALSE;

	return TRUE;
}

static gboolean
agent_start_inlock (GkdSshAgentProcess *self,
		    GError **error)
{
	const gchar *argv[] = { SSH_AGENT, "-D", "-a", self->path, NULL };
	GPid pid;

	if (!g_spawn_async_with_pipes ("/", (gchar **)argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
	                               NULL, NULL, &pid, NULL, &self->output, NULL, error))
		return FALSE;

	self->ready = FALSE;
	self->output_id = g_unix_fd_add (self->output,
					 G_IO_IN | G_IO_HUP | G_IO_ERR,
					 on_output_watch, self);

	self->pid = pid;
	self->child_id = g_child_watch_add (self->pid, on_child_watch, self);

	return TRUE;
}

static gboolean
on_timeout (gpointer user_data)
{
	gboolean *timedout = user_data;
	*timedout = TRUE;
	return TRUE;
}

GSocketConnection *
gkd_ssh_agent_process_connect (GkdSshAgentProcess *self,
			       GCancellable *cancellable,
			       GError **error)
{
	gboolean started = FALSE;
	gboolean timedout = FALSE;
	guint source;
	GSocketClient *client;
	GSocketAddress *address;
	GSocketConnection *connection;

	g_mutex_lock (&self->lock);

	if (self->pid == 0) {
		if (!agent_start_inlock (self, error)) {
			g_mutex_unlock (&self->lock);
			return NULL;
		}
		started = TRUE;
	}

	if (started && !self->ready) {
		source = g_timeout_add_seconds (5, on_timeout, &timedout);
		while (!self->ready && !timedout)
			g_main_context_iteration (NULL, FALSE);
		g_source_remove (source);
	}

	if (!self->ready) {
		g_mutex_unlock (&self->lock);
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
			     "ssh-agent process is not ready");
		return NULL;
	}

	address = g_unix_socket_address_new (self->path);
	client = g_socket_client_new ();

	connection = g_socket_client_connect (client,
					      G_SOCKET_CONNECTABLE (address),
					      cancellable,
					      error);
	g_object_unref (address);
	g_object_unref (client);

	g_mutex_unlock (&self->lock);

	return connection;
}

GkdSshAgentProcess *
gkd_ssh_agent_process_new (const gchar *path)
{
	g_return_val_if_fail (path, NULL);

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

GPid
gkd_ssh_agent_process_get_pid (GkdSshAgentProcess *self)
{
	return self->pid;
}