summaryrefslogtreecommitdiff
path: root/libsoup/soup-nss.c
blob: edadbd93ebfdd71af4df79816a8a94b1fa37bd63 (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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * soup-nss.c: Asyncronous Callback-based SOAP Request Queue.
 *
 * Authors:
 *      Alex Graveley (alex@ximian.com)
 *
 * Copyright (C) 2001, Ximian, Inc.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_NSS

#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>

#include <glib.h>

#include "nss.h"
#include <ssl.h>
#include <pk11func.h>

#include "soup-nss.h"

/* NSPR Private function */
extern PRFileDesc *PR_ImportTCPSocket (int fd);

typedef struct {
	GIOChannel   channel;
	GIOChannel  *real_sock;
	PRFileDesc  *fdesc;
	gboolean     handshake_done;
} SoupNSSChannel;

static inline GIOError
translate_nss_error (int code) 
{
	switch (code) {
	case PR_INVALID_ARGUMENT_ERROR:
		return G_IO_ERROR_INVAL;
	case PR_IO_TIMEOUT_ERROR:
	case PR_WOULD_BLOCK_ERROR:
		return G_IO_ERROR_AGAIN;
	case PR_IO_ERROR:
	default:
		return G_IO_ERROR_UNKNOWN;
	}
}

static GIOError
soup_nss_read (GIOChannel   *channel,
	       gchar        *buf,
	       guint         count,
	       guint        *bytes_read)
{
	SoupNSSChannel *chan = (SoupNSSChannel *) channel;
	gint result;
	PRPollDesc pd;

	pd.fd = chan->fdesc;
	pd.in_flags = PR_POLL_READ;

	/* 
	 * FIXME: This seems to avoid blocking zero reads. 
	 *        Not sure why these aren't filtered out in soup_nss_check.
	 */
	if (PR_Poll (&pd, 1, PR_INTERVAL_NO_WAIT) != 1)
		return G_IO_ERROR_AGAIN;

	do {
		result = PR_Read (chan->fdesc, buf, count);
	} while (result < 0 && PR_GetError () == PR_PENDING_INTERRUPT_ERROR);

	if (result < 0) {
		*bytes_read = 0;
		return translate_nss_error (PR_GetError ());
	} else {
		*bytes_read = result;
		return G_IO_ERROR_NONE;
	}
}

static GIOError
soup_nss_write (GIOChannel   *channel,
		gchar        *buf,
		guint         count,
		guint        *bytes_written)
{
	SoupNSSChannel *chan = (SoupNSSChannel *) channel;
	gint result;

	do {
		result = PR_Write (chan->fdesc, buf, count);
	} while (result < 0 && PR_GetError () == PR_PENDING_INTERRUPT_ERROR);

	if (result < 0) {
		*bytes_written = 0;
		return translate_nss_error (PR_GetError ());
	} else {
		*bytes_written = result;
		return G_IO_ERROR_NONE;
	}
}

static GIOError
soup_nss_seek (GIOChannel *channel, gint offset, GSeekType type)
{
	SoupNSSChannel *chan = (SoupNSSChannel *) channel;
	int whence;
	off_t result;
	
	switch (type) {
	case G_SEEK_SET:
		whence = PR_SEEK_SET;
		break;
	case G_SEEK_CUR:
		whence = PR_SEEK_CUR;
		break;
	case G_SEEK_END:
		whence = PR_SEEK_END;
		break;
	default:
		g_warning ("soup_nss_seek: unknown seek type");
		return G_IO_ERROR_UNKNOWN;
	}
	
	result = PR_Seek (chan->fdesc, offset, whence);
	
	if (result < 0)
		return translate_nss_error (PR_GetError ());
	else
		return G_IO_ERROR_NONE;
}

static void
soup_nss_close (GIOChannel   *channel)
{
	SoupNSSChannel *chan = (SoupNSSChannel *) channel;
	PR_Close (chan->fdesc);
	g_io_channel_close (chan->real_sock);
}

static void
soup_nss_free (GIOChannel   *channel)
{
	SoupNSSChannel *chan = (SoupNSSChannel *) channel;
	g_io_channel_unref (chan->real_sock);
	g_free (chan);
}

typedef struct {
	SoupNSSChannel *channel;
	gint16          events;
	gint16          last_event;
	GIOFunc         callback;
} SoupNSSWatchData;

static gboolean 
soup_nss_prepare  (gpointer source_data, 
		   GTimeVal *current_time,
		   gint     *timeout,
		   gpointer user_data)
{
	*timeout = 0;
	return FALSE;
}

static gboolean 
soup_nss_check    (gpointer source_data,
		   GTimeVal *current_time,
		   gpointer user_data)
{
	SoupNSSWatchData *data = source_data;
	SoupNSSChannel *chan = data->channel;
	PRPollDesc pd;

	/*
	 * We must ensure the SSL handshake is completed before performing real
	 * IO otherwise NSS behaves erratically.  Unfortunately
	 * SSL_ForceHandshake blocks until handshaking is complete.  
	 */
	if (!chan->handshake_done) {
		if (SSL_ForceHandshake (chan->fdesc) == PR_FAILURE) {
			g_warning ("SSL handshake failed.");
			PR_Close (chan->fdesc);
			return FALSE;
		}
		chan->handshake_done = TRUE;
	}

	pd.fd = chan->fdesc;
	pd.in_flags = data->events;

	if (PR_Poll (&pd, 1, PR_INTERVAL_NO_WAIT) == 1) {
		data->last_event = pd.out_flags;
		return TRUE;
	}

	data->last_event = 0;
	return FALSE;
}

static gboolean
soup_nss_dispatch (gpointer source_data, 
		   GTimeVal *current_time,
		   gpointer user_data)

{
	SoupNSSWatchData *data = source_data;
	GIOCondition cond = 0;

	if (data->last_event & PR_POLL_READ)   cond |= G_IO_IN;
	if (data->last_event & PR_POLL_WRITE)  cond |= G_IO_OUT;
	if (data->last_event & PR_POLL_ERR)    cond |= G_IO_ERR;
	if (data->last_event & PR_POLL_NVAL)   cond |= G_IO_NVAL;
	if (data->last_event & PR_POLL_HUP)    cond |= G_IO_HUP;
	if (data->last_event & PR_POLL_EXCEPT) cond |= G_IO_PRI;

	return (*data->callback) ((GIOChannel *) data->channel, 
				  cond, 
				  user_data);
}

static void 
soup_nss_destroy (gpointer source_data)
{
	SoupNSSWatchData *data = source_data;

	g_io_channel_unref ((GIOChannel *) data->channel);
	g_free (data);
}

GSourceFuncs soup_nss_watch_funcs = {
	soup_nss_prepare,
	soup_nss_check,
	soup_nss_dispatch,
	soup_nss_destroy
};

static guint 
soup_nss_add_watch (GIOChannel    *channel,
		    gint           priority,
		    GIOCondition   condition,
		    GIOFunc        func,
		    gpointer       user_data,
		    GDestroyNotify notify)
{
	SoupNSSWatchData *watch = g_new0 (SoupNSSWatchData, 1);
	SoupNSSChannel *chan = (SoupNSSChannel *) channel;

	watch->channel = chan;
	g_io_channel_ref (channel);

	watch->callback = func;

	if (condition & G_IO_IN)
		watch->events |= PR_POLL_READ;

	if (condition & G_IO_OUT)
		watch->events |= PR_POLL_WRITE;

	if (condition & G_IO_PRI  ||
	    condition & G_IO_ERR  ||
	    condition & G_IO_NVAL ||
	    condition & G_IO_HUP)
		watch->events |= PR_POLL_EXCEPT;

	return g_source_add (priority, 
			     TRUE, 
			     &soup_nss_watch_funcs, 
			     watch, 
			     user_data, 
			     notify);
}

GIOFuncs soup_nss_channel_funcs = {
	soup_nss_read,
	soup_nss_write,
	soup_nss_seek,
	soup_nss_close,
	soup_nss_add_watch,
	soup_nss_free,
};

static gboolean nss_initialized = FALSE;

static SECStatus 
soup_nss_bad_cert (void *data, PRFileDesc *fd)
{
	return SECSuccess;
}

GIOChannel *
soup_nss_get_iochannel (GIOChannel *sock)
{
	SoupNSSChannel *chan;
	GIOChannel *gchan;
	PRFileDesc *fdesc;
	int sockfd;

        g_return_val_if_fail (sock != NULL, NULL);

	if (!nss_initialized && !soup_nss_init ()) goto THROW_CREATE_ERROR;
	
	sockfd = g_io_channel_unix_get_fd (sock);
	if (!sockfd) goto THROW_CREATE_ERROR;

	fdesc = PR_ImportTCPSocket (sockfd);
	if (!fdesc) {
		g_warning ("SSL socket creation failure.\n");
		goto THROW_CREATE_ERROR;
	}

	fdesc = SSL_ImportFD (NULL, fdesc);
	if (!fdesc) {
		g_warning ("SSL object creation failure.\n");
		goto THROW_CREATE_ERROR;
	}

	SSL_OptionSet (fdesc, SSL_SECURITY, PR_TRUE);
	SSL_OptionSet (fdesc, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE);
	SSL_BadCertHook (fdesc, soup_nss_bad_cert, NULL);

	if (SSL_ResetHandshake (fdesc, PR_FALSE) == PR_FAILURE) {
		g_warning ("SSL handshake failure.\n");
		PR_Close (fdesc);
                return NULL;
	}

	chan = g_new0 (SoupNSSChannel, 1);
	chan->real_sock = sock;
	chan->fdesc = fdesc;
	chan->handshake_done = FALSE;

	g_io_channel_ref (sock);

	gchan = (GIOChannel *) chan;
	gchan->funcs = &soup_nss_channel_funcs;
	g_io_channel_init (gchan);

	return gchan;

 THROW_CREATE_ERROR:
	return NULL;
}

gboolean
soup_nss_init (void)
{
	gchar *nss_dir;
	struct stat confstat;

	nss_dir = g_strconcat (g_get_home_dir (), 
			       G_DIR_SEPARATOR_S, 
			       ".soup", 
			       NULL);

	if (stat (nss_dir, &confstat) != 0) {
		if (errno == ENOENT) {
			if (mkdir (nss_dir, S_IRWXU) != 0) {
				g_warning ("Unable to create private "
					   "configuration directory \"%s\".", 
					   nss_dir);
				goto INIT_ERROR;
			}
		} else {
			g_warning ("Error accessing configuration directory "
				   "\"%s\": %s.",
				   nss_dir,
				   strerror (errno));
			goto INIT_ERROR;
		}
	} else if (!S_ISDIR (confstat.st_mode)) {
		g_warning ("Expecting \"%s\" to be a directory.", nss_dir);
		goto INIT_ERROR;
	}

	if (NSS_InitReadWrite (nss_dir) != SECSuccess) {
		if (NSS_NoDB_Init (nss_dir) == SECFailure) {
			g_warning ("Unable to initialize NSS SSL Library.");
			goto INIT_ERROR;
		}
	}

	NSS_SetDomesticPolicy ();

        SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
        SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
        SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
        SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE);

	g_free (nss_dir);

	nss_initialized = TRUE;
	return TRUE;

 INIT_ERROR:
	g_free (nss_dir);
	return FALSE;
}

void 
soup_nss_set_security_policy (SoupSecurityPolicy policy)
{
	switch (policy) {
	case SOUP_SECURITY_DOMESTIC:
		NSS_SetDomesticPolicy ();
		break;
	case SOUP_SECURITY_EXPORT:
		NSS_SetExportPolicy ();
		break;
	case SOUP_SECURITY_FRANCE:
		NSS_SetFrancePolicy ();
		break;
	}

	SSL_ClearSessionCache ();
}

#endif /*HAVE_NSS*/