aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/control/interfaces/xml_interface.c
blob: 8dd614493f0f38c46d674b1c37042350fc5f6a69 (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
/**
 * @file xml_interface.c
 * 
 * @brief Implementation of xml_interface_t.
 * 
 */

/*
 * Copyright (C) 2007 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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 General Public License
 * for more details.
 */

#include <stdlib.h>

#include "xml_interface.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <libxml/xmlreader.h>
#include <libxml/xmlwriter.h>

#include <library.h>
#include <daemon.h>


static struct sockaddr_un socket_addr = { AF_UNIX, "/var/run/charon.xml"};


typedef struct private_xml_interface_t private_xml_interface_t;

/**
 * Private data of an xml_interface_t object.
 */
struct private_xml_interface_t {

	/**
	 * Public part of xml_t object.
	 */
	xml_interface_t public;
	
	/**
	 * XML unix socket fd
	 */
	int socket;
	
	/**
	 * thread receiving messages
	 */
	pthread_t thread;
};

static void get(private_xml_interface_t *this, 
				xmlTextReaderPtr reader, xmlTextWriterPtr writer)
{

	if (/* <GetResponse> */
		xmlTextWriterStartElement(writer, "GetResponse") < 0 ||
		/*   <Status Code="200"><Message/></Status> */
		xmlTextWriterStartElement(writer, "Status") < 0 ||
		xmlTextWriterWriteAttribute(writer, "Code", "200") < 0  ||
		xmlTextWriterStartElement(writer, "Message") < 0 ||
		xmlTextWriterEndElement(writer) < 0 ||
		xmlTextWriterEndElement(writer) < 0 ||
		/*   <ConnectionList/> */
		xmlTextWriterStartElement(writer, "ConnectionList") < 0 ||
		xmlTextWriterEndElement(writer) < 0 ||
		/* </GetResponse> */
		xmlTextWriterEndElement(writer) < 0)
	{
		DBG1(DBG_CFG, "error writing XML document (GetResponse)");
	}
		

/*
					   DBG1(DBG_CFG, "%d %d %s %d %d %s", 
						    xmlTextReaderDepth(reader),
						    ,
						    xmlTextReaderConstName(reader),
						    xmlTextReaderIsEmptyElement(reader),
						    xmlTextReaderHasValue(reader),
						    xmlTextReaderConstValue(reader));
		*/
}

static void receive(private_xml_interface_t *this)
{
	charon->drop_capabilities(charon, TRUE);
	
	/* disable cancellation by default */
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
	
	while (TRUE)
	{
		struct sockaddr_un strokeaddr;
		int strokeaddrlen = sizeof(strokeaddr);
		int oldstate;
		int fd;
		char buffer[4096];
		size_t len;
		
		/* wait for connections, but allow thread to terminate */
		pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
		fd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen);
		pthread_setcancelstate(oldstate, NULL);
		
		if (fd < 0)
		{
			DBG1(DBG_CFG, "accepting SMP XML socket failed: %s", strerror(errno));
			continue;
		}
		DBG2(DBG_CFG, "SMP XML connection opened");
		while (TRUE)
		{
			xmlTextReaderPtr reader;
			xmlTextWriterPtr writer;
			
			pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
			len = read(fd, buffer, sizeof(buffer));
			pthread_setcancelstate(oldstate, NULL);
			if (len <= 0)
			{
				close(fd);
				DBG2(DBG_CFG, "SMP XML connection closed");
				break;
			}
			
			reader = xmlReaderForMemory(buffer, len, NULL, NULL, 0);
			if (reader == NULL)
			{
				DBG1(DBG_CFG, "opening SMP XML reader failed");
				continue;
			}
			
			writer = xmlNewTextWriter(xmlOutputBufferCreateFd(fd, NULL));
			if (writer == NULL)
			{
				xmlFreeTextReader(reader);
				DBG1(DBG_CFG, "opening SMP XML writer failed");
				continue;
			}
			
			/* create the standard message parts */
			if (xmlTextWriterStartDocument(writer, NULL, NULL, NULL) < 0 ||
				/* <SMPMessage xmlns="http://www.strongswan.org/smp/1.0"> */
				xmlTextWriterStartElement(writer, "SMPMessage") < 0 ||
				xmlTextWriterWriteAttribute(writer, "xmlns",
								"http://www.strongswan.org/smp/1.0") < 0 ||
				/* <Body> */
				xmlTextWriterStartElement(writer, "Body") < 0)
			{
				xmlFreeTextReader(reader);
				xmlFreeTextWriter(writer);
				DBG1(DBG_CFG, "creating SMP XML message failed");
				continue;
			}
			
	        while (TRUE)
	        {
	        	switch (xmlTextReaderRead(reader))
	        	{
	        		case 1:
	        		{
						if (xmlTextReaderNodeType(reader) ==
							XML_READER_TYPE_ELEMENT)
						{
							if (streq(xmlTextReaderConstName(reader), "GetRequest"))
							{
								get(this, reader, writer);
								break;
							}
						}
						continue;
					}
					case 0:
					    /* end of XML */
					    break;
					default:
					    DBG1(DBG_CFG, "parsing SMP XML message failed");
					    break;
				}
		        xmlFreeTextReader(reader);
		        break;
		    }
		    /* write </Body></SMPMessage> and close document */
		    if (xmlTextWriterEndDocument(writer) < 0)
		    {
				DBG1(DBG_CFG, "completing SMP XML message failed");
		    }
		    xmlFreeTextWriter(writer);
		    /* write a newline to indicate end of xml */
		    write(fd, "\n", 1);
		}
	}
}

/**
 * Implementation of itnerface_t.destroy.
 */
static void destroy(private_xml_interface_t *this)
{
	pthread_cancel(this->thread);
	pthread_join(this->thread, NULL);
	close(this->socket);
	unlink(socket_addr.sun_path);
	free(this);
}

/*
 * Described in header file
 */
interface_t *interface_create()
{
	private_xml_interface_t *this = malloc_thing(private_xml_interface_t);
	mode_t old;

	this->public.interface.destroy = (void (*)(interface_t*))destroy;
	
	/* set up unix socket */
	this->socket = socket(AF_UNIX, SOCK_STREAM, 0);
	if (this->socket == -1)
	{
		DBG1(DBG_CFG, "could not create XML socket");
		free(this);
		return NULL;
	}
	
	old = umask(~S_IRWXU);
	if (bind(this->socket, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) < 0)
	{
		DBG1(DBG_CFG, "could not bind XML socket: %s", strerror(errno));
		close(this->socket);
		free(this);
		return NULL;
	}
	umask(old);
	
	if (listen(this->socket, 0) < 0)
	{
		DBG1(DBG_CFG, "could not listen on XML socket: %s", strerror(errno));
		close(this->socket);
		free(this);
		return NULL;
	}
	
	if (pthread_create(&this->thread, NULL, (void*(*)(void*))receive, this) != 0)
	{
		DBG1(DBG_CFG, "could not create XML socket thread: %s", strerror(errno));
		close(this->socket);
		unlink(socket_addr.sun_path);
		free(this);
		return NULL;
	}
	
	return &this->public.interface;
}