File: gkr-ssh-daemon-io.c

package info (click to toggle)
gnome-keyring 2.22.3-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,612 kB
  • ctags: 4,441
  • sloc: ansic: 39,473; sh: 9,382; xml: 4,044; makefile: 586
file content (329 lines) | stat: -rw-r--r-- 7,952 bytes parent folder | download
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gkr-ssh-daemon-io.c - handles SSH i/o from the clients

   Copyright (C) 2007 Stefan Walter

   Gnome keyring is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   Gnome keyring 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
   General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "gkr-ssh-daemon.h"
#include "gkr-ssh-private.h"

#include "common/gkr-async.h"
#include "common/gkr-buffer.h"
#include "common/gkr-cleanup.h"
#include "common/gkr-daemon-util.h"
#include "common/gkr-secure-memory.h"

#include "pk/gkr-pk-object-storage.h"

#ifndef HAVE_SOCKLEN_T
#define socklen_t int
#endif

typedef struct {
	GkrAsyncWorker *worker;
	int sock;

	GkrBuffer input_buffer;
	GkrBuffer output_buffer;
} SshClient;

static char socket_path[1024] = { 0, };

static gboolean
yield_and_read_all (int fd, guchar *buf, int len)
{
	int all = len;
	int res;
	
	while (len > 0) {
		
		/* Is this worker stopping? */
		if (gkr_async_is_stopping ())
			return FALSE;
			
		/* Don't block other threads during the read */
		gkr_async_begin_concurrent ();
		
			res = read (fd, buf, len);
			
		gkr_async_end_concurrent ();
		
		if (res <= 0) {
			if (errno == EAGAIN && errno == EINTR)
				continue;
			if (res < 0)
				g_warning ("couldn't read %u bytes from client: %s", all, 
				           g_strerror (errno));
			return FALSE;
		} else  {
			len -= res;
			buf += res;
		}
	}
	
	return TRUE;
}

static gboolean
yield_and_write_all (int fd, const guchar *buf, int len)
{
	int all = len;
	int res;
	
	while (len > 0) {
		
		/* Is this worker stopping? */
		if (gkr_async_is_stopping ())
			return FALSE;
			
		/* Don't block other threads during the read */
		gkr_async_begin_concurrent ();

			res = write (fd, buf, len);
			
		gkr_async_end_concurrent ();
		
		if (res <= 0) {
			if (errno == EAGAIN && errno == EINTR)
				continue;
			g_warning ("couldn't write %u bytes to client: %s", all, 
			           res < 0 ? g_strerror (errno) : "");
			return FALSE;
		} else  {
			len -= res;
			buf += res;
		}
	}
	
	return TRUE;
}

static gboolean
read_packet_with_size (SshClient *client)
{
	int fd;
	guint32 packet_size;

	fd = client->sock;
	
	gkr_buffer_resize (&client->input_buffer, 4);
	if (!yield_and_read_all (fd, client->input_buffer.buf, 4))
		return FALSE;

	if (!gkr_buffer_get_uint32 (&client->input_buffer, 0, NULL, &packet_size) || 
	    packet_size < 1) {
	    	g_warning ("invalid packet size from client");
		return FALSE;
	}

	gkr_buffer_resize (&client->input_buffer, packet_size + 4);
	if (!yield_and_read_all (fd, client->input_buffer.buf + 4, packet_size))
		return FALSE;

	return TRUE;
}

static void
close_fd (gpointer data)
{
	int *fd = (int*)data;
	g_assert (fd);

	/* If we're waiting anywhere this makes the thread stop */
	shutdown (*fd, SHUT_RDWR);
}

static gpointer
client_worker_main (gpointer user_data)
{
	SshClient *client = (SshClient*)user_data;
	guchar op;
	
	/* This array needs to be laid out properly */
	g_assert ((sizeof (gkr_ssh_operations) / sizeof (gkr_ssh_operations[0])) == GKR_SSH_OP_MAX);

	/* This helps any reads wakeup when this worker is stopping */
	gkr_async_register_cancel (close_fd, &client->sock);
	
	/* Make sure everything is in sync for this connection */
	gkr_pk_object_storage_refresh (NULL);
	
	while (!gkr_async_is_stopping ()) {
		
		/* 1. Read in the request */
		if (!read_packet_with_size (client))
			break;

		/* 2. Now decode the operation */
		if (!gkr_buffer_get_byte (&client->input_buffer, 4, NULL, &op))
			break; 
		if (op >= GKR_SSH_OP_MAX)
			break;
		g_assert (gkr_ssh_operations[op]);
		
		/* 3. Execute the right operation */
		gkr_buffer_reset (&client->output_buffer);
		gkr_buffer_add_uint32 (&client->output_buffer, 0);
		if (!(gkr_ssh_operations[op]) (&client->input_buffer, &client->output_buffer))
			break;
		if (!gkr_buffer_set_uint32 (&client->output_buffer, 0,
		                            client->output_buffer.len - 4))
			break;

		/* 4. Write the reply back out */
		if (!yield_and_write_all (client->sock, client->output_buffer.buf,
		                          client->output_buffer.len))
			break;
	} 

	/* All done */
	shutdown (client->sock, SHUT_RDWR);
	return NULL;
}

static void
client_worker_done (GkrAsyncWorker *worker, gpointer result, gpointer user_data)
{
	SshClient *client = (SshClient*)user_data;

	gkr_buffer_uninit (&client->input_buffer);
	gkr_buffer_uninit (&client->output_buffer);

	if (client->sock != -1)
		close (client->sock);
	g_free (client);
}

static void
client_new (int fd)
{
	SshClient *client;

	client = g_new0 (SshClient, 1);
	client->sock = fd;
	
	/* 
	 * We really have no idea what operation the client will send, 
	 * so we err on the side of caution and use secure memory in case
	 * keys are involved.
	 */
	/* TODO: Switch to gkr_secure_memory */
	gkr_buffer_init_full (&client->input_buffer, 128, gkr_secure_realloc);
	gkr_buffer_init_full (&client->output_buffer, 128, (GkrBufferAllocator)g_realloc);

	client->worker = gkr_async_worker_start (client_worker_main, 
	                                         client_worker_done, client);
	g_assert (client->worker);
	
	/* 
	 * The worker thread is tracked in a global list, and is guaranteed to 
	 * be cleaned up, either when it exits, or when the application closes.
	 */
}

static gboolean
accept_client (GIOChannel *channel, GIOCondition cond,
               gpointer callback_data)
{
	int fd;
	int new_fd;
	struct sockaddr_un addr;
	socklen_t addrlen;
  
	fd = g_io_channel_unix_get_fd (channel);
	
	addrlen = sizeof (addr);
	new_fd = accept (fd, (struct sockaddr *) &addr, &addrlen);
	
	if (new_fd >= 0) 
		client_new (new_fd);
	return TRUE;
}

static void
cleanup_socket_dir (gpointer data)
{
	if(*socket_path)
		unlink (socket_path);
}

gboolean
gkr_daemon_ssh_io_initialize (void)
{
	const gchar *tmp_dir;
	int sock;
	struct sockaddr_un addr;
	GIOChannel *channel;

	tmp_dir = gkr_daemon_util_get_master_directory ();
	g_return_val_if_fail (tmp_dir, FALSE);
		
	snprintf (socket_path, sizeof (socket_path), "%s/ssh", tmp_dir);
	
#ifdef WITH_TESTS
	if (g_getenv ("GNOME_KEYRING_TEST_PATH"))
		unlink (socket_path);
#endif

	gkr_cleanup_register (cleanup_socket_dir, NULL);
	
	sock = socket (AF_UNIX, SOCK_STREAM, 0);
	if (sock < 0) {
		g_warning ("couldn't create socket: %s", g_strerror (errno));
		return FALSE;
	}
	
	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path));
	if (bind (sock, (struct sockaddr *) & addr, sizeof (addr)) < 0) {
		g_warning ("couldn't bind to socket: %s", g_strerror (errno));
		return FALSE;
	}
	
	if (listen (sock, 128) < 0) {
		g_warning ("couldn't listen on socket: %s", g_strerror (errno));
		return FALSE;
	}

	channel = g_io_channel_unix_new (sock);
	g_io_add_watch (channel, G_IO_IN | G_IO_HUP, accept_client, NULL);
	g_io_channel_unref (channel);
	
	if (g_getenv ("SSH_AUTH_SOCK"))
		g_message ("another SSH agent is running at: %s", g_getenv ("SSH_AUTH_SOCK")); 
		
	/* TODO: Do we need to push SSH_AGENT_PID? */
	gkr_daemon_util_push_environment ("SSH_AUTH_SOCK", socket_path);
	
	return TRUE;
}