1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
7 Copyright 2012 Zbigniew Jędrzejewski-Szmek
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include <gnutls/gnutls.h>
29 #include <gnutls/x509.h>
34 #include "string-util.h"
37 #include "microhttpd-util.h"
39 void microhttpd_logger(void *arg
, const char *fmt
, va_list ap
) {
42 f
= strjoina("microhttpd: ", fmt
);
44 DISABLE_WARNING_FORMAT_NONLITERAL
;
45 log_internalv(LOG_INFO
, 0, NULL
, 0, NULL
, f
, ap
);
50 static int mhd_respond_internal(struct MHD_Connection
*connection
,
51 enum MHD_RequestTerminationCode code
,
54 enum MHD_ResponseMemoryMode mode
) {
55 struct MHD_Response
*response
;
60 response
= MHD_create_response_from_buffer(size
, buffer
, mode
);
64 log_debug("Queing response %u: %s", code
, buffer
);
65 MHD_add_response_header(response
, "Content-Type", "text/plain");
66 r
= MHD_queue_response(connection
, code
, response
);
67 MHD_destroy_response(response
);
72 int mhd_respond(struct MHD_Connection
*connection
,
73 enum MHD_RequestTerminationCode code
,
74 const char *message
) {
76 return mhd_respond_internal(connection
, code
,
77 (char*) message
, strlen(message
),
78 MHD_RESPMEM_PERSISTENT
);
81 int mhd_respond_oom(struct MHD_Connection
*connection
) {
82 return mhd_respond(connection
, MHD_HTTP_SERVICE_UNAVAILABLE
, "Out of memory.\n");
85 int mhd_respondf(struct MHD_Connection
*connection
,
86 enum MHD_RequestTerminationCode code
,
87 const char *format
, ...) {
97 r
= vasprintf(&m
, format
, ap
);
101 return respond_oom(connection
);
103 return mhd_respond_internal(connection
, code
, m
, r
, MHD_RESPMEM_MUST_FREE
);
109 const char *const names
[4];
112 } gnutls_log_map
[] = {
113 { {"0"}, LOG_DEBUG
},
114 { {"1", "audit"}, LOG_WARNING
, true}, /* gnutls session audit */
115 { {"2", "assert"}, LOG_DEBUG
}, /* gnutls assert log */
116 { {"3", "hsk", "ext"}, LOG_DEBUG
}, /* gnutls handshake log */
117 { {"4", "rec"}, LOG_DEBUG
}, /* gnutls record log */
118 { {"5", "dtls"}, LOG_DEBUG
}, /* gnutls DTLS log */
119 { {"6", "buf"}, LOG_DEBUG
},
120 { {"7", "write", "read"}, LOG_DEBUG
},
121 { {"8"}, LOG_DEBUG
},
122 { {"9", "enc", "int"}, LOG_DEBUG
},
125 static void log_func_gnutls(int level
, const char *message
) {
128 if (0 <= level
&& level
< (int) ELEMENTSOF(gnutls_log_map
)) {
129 if (gnutls_log_map
[level
].enabled
)
130 log_internal(gnutls_log_map
[level
].level
, 0, NULL
, 0, NULL
, "gnutls %d/%s: %s", level
, gnutls_log_map
[level
].names
[1], message
);
132 log_debug("Received GNUTLS message with unknown level %d.", level
);
133 log_internal(LOG_DEBUG
, 0, NULL
, 0, NULL
, "gnutls: %s", message
);
137 static void log_reset_gnutls_level(void) {
140 for (i
= ELEMENTSOF(gnutls_log_map
) - 1; i
>= 0; i
--)
141 if (gnutls_log_map
[i
].enabled
) {
142 log_debug("Setting gnutls log level to %d", i
);
143 gnutls_global_set_log_level(i
);
148 static int log_enable_gnutls_category(const char *cat
) {
151 if (streq(cat
, "all")) {
152 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
153 gnutls_log_map
[i
].enabled
= true;
154 log_reset_gnutls_level();
157 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
158 if (strv_contains((char**)gnutls_log_map
[i
].names
, cat
)) {
159 gnutls_log_map
[i
].enabled
= true;
160 log_reset_gnutls_level();
163 log_error("No such log category: %s", cat
);
167 int setup_gnutls_logger(char **categories
) {
171 gnutls_global_set_log_function(log_func_gnutls
);
174 STRV_FOREACH(cat
, categories
) {
175 r
= log_enable_gnutls_category(*cat
);
180 log_reset_gnutls_level();
185 static int verify_cert_authorized(gnutls_session_t session
) {
187 gnutls_certificate_type_t type
;
191 r
= gnutls_certificate_verify_peers2(session
, &status
);
193 return log_error_errno(r
, "gnutls_certificate_verify_peers2 failed: %m");
195 type
= gnutls_certificate_type_get(session
);
196 r
= gnutls_certificate_verification_status_print(status
, type
, &out
, 0);
198 return log_error_errno(r
, "gnutls_certificate_verification_status_print failed: %m");
200 log_debug("Certificate status: %s", out
.data
);
201 gnutls_free(out
.data
);
203 return status
== 0 ? 0 : -EPERM
;
206 static int get_client_cert(gnutls_session_t session
, gnutls_x509_crt_t
*client_cert
) {
207 const gnutls_datum_t
*pcert
;
209 gnutls_x509_crt_t cert
;
215 pcert
= gnutls_certificate_get_peers(session
, &listsize
);
216 if (!pcert
|| !listsize
) {
217 log_error("Failed to retrieve certificate chain");
221 r
= gnutls_x509_crt_init(&cert
);
223 log_error("Failed to initialize client certificate");
227 /* Note that by passing values between 0 and listsize here, you
228 can get access to the CA's certs */
229 r
= gnutls_x509_crt_import(cert
, &pcert
[0], GNUTLS_X509_FMT_DER
);
231 log_error("Failed to import client certificate");
232 gnutls_x509_crt_deinit(cert
);
240 static int get_auth_dn(gnutls_x509_crt_t client_cert
, char **buf
) {
245 assert(*buf
== NULL
);
247 r
= gnutls_x509_crt_get_dn(client_cert
, NULL
, &len
);
248 if (r
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
249 log_error("gnutls_x509_crt_get_dn failed");
257 gnutls_x509_crt_get_dn(client_cert
, *buf
, &len
);
261 static inline void gnutls_x509_crt_deinitp(gnutls_x509_crt_t
*p
) {
262 gnutls_x509_crt_deinit(*p
);
265 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
266 const union MHD_ConnectionInfo
*ci
;
267 gnutls_session_t session
;
268 _cleanup_(gnutls_x509_crt_deinitp
) gnutls_x509_crt_t client_cert
= NULL
;
269 _cleanup_free_
char *buf
= NULL
;
277 ci
= MHD_get_connection_info(connection
,
278 MHD_CONNECTION_INFO_GNUTLS_SESSION
);
280 log_error("MHD_get_connection_info failed: session is unencrypted");
281 *code
= mhd_respond(connection
, MHD_HTTP_FORBIDDEN
,
282 "Encrypted connection is required");
285 session
= ci
->tls_session
;
288 r
= get_client_cert(session
, &client_cert
);
290 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
291 "Authorization through certificate is required");
295 r
= get_auth_dn(client_cert
, &buf
);
297 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
298 "Failed to determine distinguished name from certificate");
302 log_debug("Connection from %s", buf
);
309 r
= verify_cert_authorized(session
);
311 log_warning("Client is not authorized");
312 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
313 "Client certificate not signed by recognized authority");
319 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
323 int setup_gnutls_logger(char **categories
) {
325 log_notice("Ignoring specified gnutls logging categories — gnutls not available.");