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; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
Copyright 2012 Zbigniew Jędrzejewski-Szmek
systemd 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.
systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_GNUTLS
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#endif
#include "alloc-util.h"
#include "log.h"
#include "macro.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
#include "microhttpd-util.h"
void microhttpd_logger(void *arg, const char *fmt, va_list ap) {
char *f;
f = strjoina("microhttpd: ", fmt);
DISABLE_WARNING_FORMAT_NONLITERAL;
log_internalv(LOG_INFO, 0, NULL, 0, NULL, f, ap);
REENABLE_WARNING;
}
static int mhd_respond_internal(struct MHD_Connection *connection,
enum MHD_RequestTerminationCode code,
char *buffer,
size_t size,
enum MHD_ResponseMemoryMode mode) {
struct MHD_Response *response;
int r;
assert(connection);
response = MHD_create_response_from_buffer(size, buffer, mode);
if (!response)
return MHD_NO;
log_debug("Queing response %u: %s", code, buffer);
MHD_add_response_header(response, "Content-Type", "text/plain");
r = MHD_queue_response(connection, code, response);
MHD_destroy_response(response);
return r;
}
int mhd_respond(struct MHD_Connection *connection,
enum MHD_RequestTerminationCode code,
const char *message) {
return mhd_respond_internal(connection, code,
(char*) message, strlen(message),
MHD_RESPMEM_PERSISTENT);
}
int mhd_respond_oom(struct MHD_Connection *connection) {
return mhd_respond(connection, MHD_HTTP_SERVICE_UNAVAILABLE, "Out of memory.\n");
}
int mhd_respondf(struct MHD_Connection *connection,
enum MHD_RequestTerminationCode code,
const char *format, ...) {
char *m;
int r;
va_list ap;
assert(connection);
assert(format);
va_start(ap, format);
r = vasprintf(&m, format, ap);
va_end(ap);
if (r < 0)
return respond_oom(connection);
return mhd_respond_internal(connection, code, m, r, MHD_RESPMEM_MUST_FREE);
}
#ifdef HAVE_GNUTLS
static struct {
const char *const names[4];
int level;
bool enabled;
} gnutls_log_map[] = {
{ {"0"}, LOG_DEBUG },
{ {"1", "audit"}, LOG_WARNING, true}, /* gnutls session audit */
{ {"2", "assert"}, LOG_DEBUG }, /* gnutls assert log */
{ {"3", "hsk", "ext"}, LOG_DEBUG }, /* gnutls handshake log */
{ {"4", "rec"}, LOG_DEBUG }, /* gnutls record log */
{ {"5", "dtls"}, LOG_DEBUG }, /* gnutls DTLS log */
{ {"6", "buf"}, LOG_DEBUG },
{ {"7", "write", "read"}, LOG_DEBUG },
{ {"8"}, LOG_DEBUG },
{ {"9", "enc", "int"}, LOG_DEBUG },
};
static void log_func_gnutls(int level, const char *message) {
assert_se(message);
if (0 <= level && level < (int) ELEMENTSOF(gnutls_log_map)) {
if (gnutls_log_map[level].enabled)
log_internal(gnutls_log_map[level].level, 0, NULL, 0, NULL, "gnutls %d/%s: %s", level, gnutls_log_map[level].names[1], message);
} else {
log_debug("Received GNUTLS message with unknown level %d.", level);
log_internal(LOG_DEBUG, 0, NULL, 0, NULL, "gnutls: %s", message);
}
}
static void log_reset_gnutls_level(void) {
int i;
for (i = ELEMENTSOF(gnutls_log_map) - 1; i >= 0; i--)
if (gnutls_log_map[i].enabled) {
log_debug("Setting gnutls log level to %d", i);
gnutls_global_set_log_level(i);
break;
}
}
static int log_enable_gnutls_category(const char *cat) {
unsigned i;
if (streq(cat, "all")) {
for (i = 0; i < ELEMENTSOF(gnutls_log_map); i++)
gnutls_log_map[i].enabled = true;
log_reset_gnutls_level();
return 0;
} else
for (i = 0; i < ELEMENTSOF(gnutls_log_map); i++)
if (strv_contains((char**)gnutls_log_map[i].names, cat)) {
gnutls_log_map[i].enabled = true;
log_reset_gnutls_level();
return 0;
}
log_error("No such log category: %s", cat);
return -EINVAL;
}
int setup_gnutls_logger(char **categories) {
char **cat;
int r;
gnutls_global_set_log_function(log_func_gnutls);
if (categories) {
STRV_FOREACH(cat, categories) {
r = log_enable_gnutls_category(*cat);
if (r < 0)
return r;
}
} else
log_reset_gnutls_level();
return 0;
}
static int verify_cert_authorized(gnutls_session_t session) {
unsigned status;
gnutls_certificate_type_t type;
gnutls_datum_t out;
int r;
r = gnutls_certificate_verify_peers2(session, &status);
if (r < 0)
return log_error_errno(r, "gnutls_certificate_verify_peers2 failed: %m");
type = gnutls_certificate_type_get(session);
r = gnutls_certificate_verification_status_print(status, type, &out, 0);
if (r < 0)
return log_error_errno(r, "gnutls_certificate_verification_status_print failed: %m");
log_debug("Certificate status: %s", out.data);
gnutls_free(out.data);
return status == 0 ? 0 : -EPERM;
}
static int get_client_cert(gnutls_session_t session, gnutls_x509_crt_t *client_cert) {
const gnutls_datum_t *pcert;
unsigned listsize;
gnutls_x509_crt_t cert;
int r;
assert(session);
assert(client_cert);
pcert = gnutls_certificate_get_peers(session, &listsize);
if (!pcert || !listsize) {
log_error("Failed to retrieve certificate chain");
return -EINVAL;
}
r = gnutls_x509_crt_init(&cert);
if (r < 0) {
log_error("Failed to initialize client certificate");
return r;
}
/* Note that by passing values between 0 and listsize here, you
can get access to the CA's certs */
r = gnutls_x509_crt_import(cert, &pcert[0], GNUTLS_X509_FMT_DER);
if (r < 0) {
log_error("Failed to import client certificate");
gnutls_x509_crt_deinit(cert);
return r;
}
*client_cert = cert;
return 0;
}
static int get_auth_dn(gnutls_x509_crt_t client_cert, char **buf) {
size_t len = 0;
int r;
assert(buf);
assert(*buf == NULL);
r = gnutls_x509_crt_get_dn(client_cert, NULL, &len);
if (r != GNUTLS_E_SHORT_MEMORY_BUFFER) {
log_error("gnutls_x509_crt_get_dn failed");
return r;
}
*buf = malloc(len);
if (!*buf)
return log_oom();
gnutls_x509_crt_get_dn(client_cert, *buf, &len);
return 0;
}
static inline void gnutls_x509_crt_deinitp(gnutls_x509_crt_t *p) {
gnutls_x509_crt_deinit(*p);
}
int check_permissions(struct MHD_Connection *connection, int *code, char **hostname) {
const union MHD_ConnectionInfo *ci;
gnutls_session_t session;
_cleanup_(gnutls_x509_crt_deinitp) gnutls_x509_crt_t client_cert = NULL;
_cleanup_free_ char *buf = NULL;
int r;
assert(connection);
assert(code);
*code = 0;
ci = MHD_get_connection_info(connection,
MHD_CONNECTION_INFO_GNUTLS_SESSION);
if (!ci) {
log_error("MHD_get_connection_info failed: session is unencrypted");
*code = mhd_respond(connection, MHD_HTTP_FORBIDDEN,
"Encrypted connection is required");
return -EPERM;
}
session = ci->tls_session;
assert(session);
r = get_client_cert(session, &client_cert);
if (r < 0) {
*code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED,
"Authorization through certificate is required");
return -EPERM;
}
r = get_auth_dn(client_cert, &buf);
if (r < 0) {
*code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED,
"Failed to determine distinguished name from certificate");
return -EPERM;
}
log_debug("Connection from %s", buf);
if (hostname) {
*hostname = buf;
buf = NULL;
}
r = verify_cert_authorized(session);
if (r < 0) {
log_warning("Client is not authorized");
*code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED,
"Client certificate not signed by recognized authority");
}
return r;
}
#else
int check_permissions(struct MHD_Connection *connection, int *code, char **hostname) {
return -EPERM;
}
int setup_gnutls_logger(char **categories) {
if (categories)
log_notice("Ignoring specified gnutls logging categories — gnutls not available.");
return 0;
}
#endif
|