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/>.
27 #include "microhttpd-util.h"
33 #include <gnutls/gnutls.h>
34 #include <gnutls/x509.h>
37 void microhttpd_logger(void *arg, const char *fmt, va_list ap) {
40 f = strappenda("microhttpd: ", fmt);
42 DISABLE_WARNING_FORMAT_NONLITERAL;
43 log_metav(LOG_INFO, NULL, 0, NULL, f, ap);
48 static int mhd_respond_internal(struct MHD_Connection *connection,
49 enum MHD_RequestTerminationCode code,
52 enum MHD_ResponseMemoryMode mode) {
53 struct MHD_Response *response;
58 response = MHD_create_response_from_buffer(size, buffer, mode);
62 log_debug("Queing response %u: %s", code, buffer);
63 MHD_add_response_header(response, "Content-Type", "text/plain");
64 r = MHD_queue_response(connection, code, response);
65 MHD_destroy_response(response);
70 int mhd_respond(struct MHD_Connection *connection,
71 enum MHD_RequestTerminationCode code,
72 const char *message) {
74 return mhd_respond_internal(connection, code,
75 (char*) message, strlen(message),
76 MHD_RESPMEM_PERSISTENT);
79 int mhd_respond_oom(struct MHD_Connection *connection) {
80 return mhd_respond(connection, MHD_HTTP_SERVICE_UNAVAILABLE, "Out of memory.\n");
83 int mhd_respondf(struct MHD_Connection *connection,
84 enum MHD_RequestTerminationCode code,
85 const char *format, ...) {
95 r = vasprintf(&m, format, ap);
99 return respond_oom(connection);
101 return mhd_respond_internal(connection, code, m, r, MHD_RESPMEM_MUST_FREE);
106 static int log_level_map[] = {
108 LOG_WARNING, /* gnutls session audit */
109 LOG_DEBUG, /* gnutls debug log */
110 LOG_WARNING, /* gnutls assert log */
111 LOG_INFO, /* gnutls handshake log */
112 LOG_DEBUG, /* gnutls record log */
113 LOG_DEBUG, /* gnutls dtls log */
117 LOG_DEBUG, /* gnutls hard log */
118 LOG_DEBUG, /* gnutls read log */
119 LOG_DEBUG, /* gnutls write log */
120 LOG_DEBUG, /* gnutls io log */
121 LOG_DEBUG, /* gnutls buffers log */
124 void log_func_gnutls(int level, const char *message) {
129 if (0 <= level && level < (int) ELEMENTSOF(log_level_map))
130 ourlevel = log_level_map[level];
132 ourlevel = LOG_DEBUG;
134 log_meta(ourlevel, NULL, 0, NULL, "gnutls: %s", message);
137 static int verify_cert_authorized(gnutls_session_t session) {
139 gnutls_certificate_type_t type;
143 r = gnutls_certificate_verify_peers2(session, &status);
145 log_error("gnutls_certificate_verify_peers2 failed: %s", strerror(-r));
149 type = gnutls_certificate_type_get(session);
150 r = gnutls_certificate_verification_status_print(status, type, &out, 0);
152 log_error("gnutls_certificate_verification_status_print failed: %s", strerror(-r));
156 log_info("Certificate status: %s", out.data);
158 return status == 0 ? 0 : -EPERM;
161 static int get_client_cert(gnutls_session_t session, gnutls_x509_crt_t *client_cert) {
162 const gnutls_datum_t *pcert;
164 gnutls_x509_crt_t cert;
170 pcert = gnutls_certificate_get_peers(session, &listsize);
171 if (!pcert || !listsize) {
172 log_error("Failed to retrieve certificate chain");
176 r = gnutls_x509_crt_init(&cert);
178 log_error("Failed to initialize client certificate");
182 /* Note that by passing values between 0 and listsize here, you
183 can get access to the CA's certs */
184 r = gnutls_x509_crt_import(cert, &pcert[0], GNUTLS_X509_FMT_DER);
186 log_error("Failed to import client certificate");
187 gnutls_x509_crt_deinit(cert);
195 static int get_auth_dn(gnutls_x509_crt_t client_cert, char **buf) {
200 assert(*buf == NULL);
202 r = gnutls_x509_crt_get_dn(client_cert, NULL, &len);
203 if (r != GNUTLS_E_SHORT_MEMORY_BUFFER) {
204 log_error("gnutls_x509_crt_get_dn failed");
212 gnutls_x509_crt_get_dn(client_cert, *buf, &len);
216 int check_permissions(struct MHD_Connection *connection, int *code) {
217 const union MHD_ConnectionInfo *ci;
218 gnutls_session_t session;
219 gnutls_x509_crt_t client_cert;
220 char _cleanup_free_ *buf = NULL;
228 ci = MHD_get_connection_info(connection,
229 MHD_CONNECTION_INFO_GNUTLS_SESSION);
231 log_error("MHD_get_connection_info failed: session is unencrypted");
232 *code = mhd_respond(connection, MHD_HTTP_FORBIDDEN,
233 "Encrypted connection is required");
236 session = ci->tls_session;
239 r = get_client_cert(session, &client_cert);
241 *code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED,
242 "Authorization through certificate is required");
246 r = get_auth_dn(client_cert, &buf);
248 *code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED,
249 "Failed to determine distinguished name from certificate");
253 log_info("Connection from %s", buf);
255 r = verify_cert_authorized(session);
257 log_warning("Client is not authorized");
258 *code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED,
259 "Client certificate not signed by recognized authority");
265 int check_permissions(struct MHD_Connection *connection, int *code) {