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
|
/*
This file is part of TALER
Copyright (C) 2014--2019 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* @file mhd_config.c
* @brief functions to configure and setup MHD
* @author Florian Dold
* @author Benedikt Mueller
* @author Christian Grothoff
*/
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include "taler_mhd_lib.h"
/**
* Backlog for listen operation on UNIX domain sockets.
*/
#define UNIX_BACKLOG 500
/**
* Parse the configuration to determine on which port
* or UNIX domain path we should run an HTTP service.
*
* @param cfg configuration to parse
* @param section section of the configuration to parse (usually "exchange")
* @param[out] rport set to the port number, or 0 for none
* @param[out] unix_path set to the UNIX path, or NULL for none
* @param[out] unix_mode set to the mode to be used for @a unix_path
* @return #GNUNET_OK on success
*/
int
TALER_MHD_parse_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
const char *section,
uint16_t *rport,
char **unix_path,
mode_t *unix_mode)
{
const char *choices[] = {"tcp", "unix"};
const char *serve_type;
unsigned long long port;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_choice (cfg,
section,
"serve",
choices,
&serve_type))
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"serve",
"serve type required");
return GNUNET_SYSERR;
}
if (0 == strcasecmp (serve_type, "tcp"))
{
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_number (cfg,
section,
"port",
&port))
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"port",
"port number required");
return GNUNET_SYSERR;
}
if ( (0 == port) ||
(port > UINT16_MAX) )
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"port",
"value not in [1,65535]");
return GNUNET_SYSERR;
}
*rport = (uint16_t) port;
*unix_path = NULL;
return GNUNET_OK;
}
if (0 == strcmp (serve_type, "unix"))
{
struct sockaddr_un s_un;
char *modestring;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_filename (cfg,
section,
"unixpath",
unix_path))
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"unixpath",
"unixpath required");
return GNUNET_SYSERR;
}
if (strlen (*unix_path) >= sizeof (s_un.sun_path))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"unixpath `%s' is too long\n",
*unix_path);
return GNUNET_SYSERR;
}
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string (cfg,
section,
"UNIXPATH_MODE",
&modestring))
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
section,
"UNIXPATH_MODE");
return GNUNET_SYSERR;
}
errno = 0;
*unix_mode = (mode_t) strtoul (modestring, NULL, 8);
if (0 != errno)
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"UNIXPATH_MODE",
"must be octal number");
GNUNET_free (modestring);
return GNUNET_SYSERR;
}
GNUNET_free (modestring);
return GNUNET_OK;
}
/* not reached */
GNUNET_assert (0);
return GNUNET_SYSERR;
}
/**
* Function called for logging by MHD.
*
* @param cls closure, NULL
* @param fm format string (`printf()`-style)
* @param ap arguments to @a fm
*/
void
TALER_MHD_handle_logs (void *cls,
const char *fm,
va_list ap)
{
static int cache;
char buf[2048];
(void) cls;
if (-1 == cache)
return;
if (0 == cache)
{
if (0 ==
GNUNET_get_log_call_status (GNUNET_ERROR_TYPE_INFO,
"libmicrohttpd",
__FILE__,
__FUNCTION__,
__LINE__))
{
cache = -1;
return;
}
}
cache = 1;
vsnprintf (buf,
sizeof (buf),
fm,
ap);
GNUNET_log_from_nocheck (GNUNET_ERROR_TYPE_INFO,
"libmicrohttpd",
"%s",
buf);
}
/**
* Open UNIX domain socket for listining at @a unix_path with
* permissions @a unix_mode.
*
* @param unix_path where to listen
* @param unix_mode access permissions to set
* @return -1 on error, otherwise the listen socket
*/
int
TALER_MHD_open_unix_path (const char *unix_path,
mode_t unix_mode)
{
struct GNUNET_NETWORK_Handle *nh;
struct sockaddr_un *un;
int fd;
if (sizeof (un->sun_path) <= strlen (unix_path))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"unixpath `%s' is too long\n",
unix_path);
return -1;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Creating listen socket '%s' with mode %o\n",
unix_path,
unix_mode);
if (GNUNET_OK !=
GNUNET_DISK_directory_create_for_file (unix_path))
{
GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
"mkdir",
unix_path);
}
un = GNUNET_new (struct sockaddr_un);
un->sun_family = AF_UNIX;
strncpy (un->sun_path,
unix_path,
sizeof (un->sun_path) - 1);
GNUNET_NETWORK_unix_precheck (un);
if (NULL == (nh = GNUNET_NETWORK_socket_create (AF_UNIX,
SOCK_STREAM,
0)))
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
"socket");
GNUNET_free (un);
return -1;
}
if (GNUNET_OK !=
GNUNET_NETWORK_socket_bind (nh,
(void *) un,
sizeof (struct sockaddr_un)))
{
GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
"bind",
unix_path);
GNUNET_free (un);
GNUNET_NETWORK_socket_close (nh);
return -1;
}
GNUNET_free (un);
if (GNUNET_OK !=
GNUNET_NETWORK_socket_listen (nh,
UNIX_BACKLOG))
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
"listen");
GNUNET_NETWORK_socket_close (nh);
return -1;
}
if (0 != chmod (unix_path,
unix_mode))
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
"chmod");
GNUNET_NETWORK_socket_close (nh);
return -1;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"set socket '%s' to mode %o\n",
unix_path,
unix_mode);
fd = GNUNET_NETWORK_get_fd (nh);
GNUNET_NETWORK_socket_free_memory_only_ (nh);
return fd;
}
/**
* Bind a listen socket to the UNIX domain path
* or the TCP port and IP address as specified
* in @a cfg in section @a section. IF only a
* port was specified, set @a port and return -1.
* Otherwise, return the bound file descriptor.
*
* @param cfg configuration to parse
* @param section configuration section to use
* @param[out] port port to set, if TCP without BINDTO
* @return -1 and a port of zero on error, otherwise
* either -1 and a port, or a bound stream socket
*/
int
TALER_MHD_bind (const struct GNUNET_CONFIGURATION_Handle *cfg,
const char *section,
uint16_t *port)
{
char *bind_to;
char *serve_unixpath;
mode_t unixpath_mode;
int fh;
char port_str[6];
struct addrinfo hints;
struct addrinfo *res;
int ec;
struct GNUNET_NETWORK_Handle *nh;
*port = 0;
if (GNUNET_OK !=
TALER_MHD_parse_config (cfg,
section,
port,
&serve_unixpath,
&unixpath_mode))
return -1;
if (NULL != serve_unixpath)
return TALER_MHD_open_unix_path (serve_unixpath,
unixpath_mode);
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string (cfg,
section,
"BIND_TO",
&bind_to))
return -1; /* only set port */
/* let's have fun binding... */
GNUNET_snprintf (port_str,
sizeof (port_str),
"%u",
(unsigned int) *port);
*port = 0; /* do NOT return port in case of errors */
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE
#ifdef AI_IDN
| AI_IDN
#endif
;
if (0 !=
(ec = getaddrinfo (bind_to,
port_str,
&hints,
&res)))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to resolve BIND_TO address `%s': %s\n",
bind_to, gai_strerror (ec));
GNUNET_free (bind_to);
return -1;
}
GNUNET_free (bind_to);
if (NULL == (nh = GNUNET_NETWORK_socket_create (res->ai_family,
res->ai_socktype,
res->ai_protocol)))
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
"socket");
freeaddrinfo (res);
return -1;
}
if (GNUNET_OK !=
GNUNET_NETWORK_socket_bind (nh,
res->ai_addr,
res->ai_addrlen))
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
"bind");
freeaddrinfo (res);
return -1;
}
freeaddrinfo (res);
if (GNUNET_OK !=
GNUNET_NETWORK_socket_listen (nh,
UNIX_BACKLOG))
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
"listen");
GNUNET_SCHEDULER_shutdown ();
return -1;
}
fh = GNUNET_NETWORK_get_fd (nh);
GNUNET_NETWORK_socket_free_memory_only_ (nh);
return fh;
}
|