| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "sd-dlopen.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "gnutls-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "microhttpd-util.h" |
| 11 |
#include "string-util.h" |
| 12 |
#include "strv.h" |
| 13 |
|
| 14 |
#if HAVE_MICROHTTPD |
| 15 |
DLSYM_PROTOTYPE(MHD_add_response_header) = NULL; |
| 16 |
DLSYM_PROTOTYPE(MHD_create_response_from_buffer) = NULL; |
| 17 |
DLSYM_PROTOTYPE(MHD_create_response_from_callback) = NULL; |
| 18 |
#if MHD_VERSION < 0x00094203 |
| 19 |
DLSYM_PROTOTYPE(MHD_create_response_from_fd_at_offset) = NULL; |
| 20 |
#else |
| 21 |
DLSYM_PROTOTYPE(MHD_create_response_from_fd_at_offset64) = NULL; |
| 22 |
#endif |
| 23 |
DLSYM_PROTOTYPE(MHD_destroy_response) = NULL; |
| 24 |
DLSYM_PROTOTYPE(MHD_get_connection_info) = NULL; |
| 25 |
DLSYM_PROTOTYPE(MHD_get_connection_values) = NULL; |
| 26 |
DLSYM_PROTOTYPE(MHD_get_daemon_info) = NULL; |
| 27 |
DLSYM_PROTOTYPE(MHD_get_timeout) = NULL; |
| 28 |
DLSYM_PROTOTYPE(MHD_lookup_connection_value) = NULL; |
| 29 |
DLSYM_PROTOTYPE(MHD_queue_response) = NULL; |
| 30 |
DLSYM_PROTOTYPE(MHD_run) = NULL; |
| 31 |
DLSYM_PROTOTYPE(MHD_start_daemon) = NULL; |
| 32 |
DLSYM_PROTOTYPE(MHD_stop_daemon) = NULL; |
| 33 |
#endif |
| 34 |
|
| 35 |
int dlopen_microhttpd(int log_level) { |
| 36 |
#if HAVE_MICROHTTPD |
| 37 |
static void *microhttpd_dl = NULL; |
| 38 |
|
| 39 |
MICROHTTPD_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED); |
| 40 |
|
| 41 |
return dlopen_many_sym_or_warn( |
| 42 |
µhttpd_dl, |
| 43 |
"libmicrohttpd.so.12", |
| 44 |
log_level, |
| 45 |
DLSYM_ARG(MHD_add_response_header), |
| 46 |
DLSYM_ARG(MHD_create_response_from_buffer), |
| 47 |
DLSYM_ARG(MHD_create_response_from_callback), |
| 48 |
#if MHD_VERSION < 0x00094203 |
| 49 |
DLSYM_ARG(MHD_create_response_from_fd_at_offset), |
| 50 |
#else |
| 51 |
DLSYM_ARG(MHD_create_response_from_fd_at_offset64), |
| 52 |
#endif |
| 53 |
DLSYM_ARG(MHD_destroy_response), |
| 54 |
DLSYM_ARG(MHD_get_connection_info), |
| 55 |
DLSYM_ARG(MHD_get_connection_values), |
| 56 |
DLSYM_ARG(MHD_get_daemon_info), |
| 57 |
DLSYM_ARG(MHD_get_timeout), |
| 58 |
DLSYM_ARG(MHD_lookup_connection_value), |
| 59 |
DLSYM_ARG(MHD_queue_response), |
| 60 |
DLSYM_ARG(MHD_run), |
| 61 |
DLSYM_ARG(MHD_start_daemon), |
| 62 |
DLSYM_ARG(MHD_stop_daemon)); |
| 63 |
#else |
| 64 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 65 |
"libmicrohttpd support is not compiled in."); |
| 66 |
#endif |
| 67 |
} |
| 68 |
|
| 69 |
#if HAVE_MICROHTTPD |
| 70 |
|
| 71 |
void microhttpd_logger(void *arg, const char *fmt, va_list ap) { |
| 72 |
char *f; |
| 73 |
|
| 74 |
f = strjoina("microhttpd: ", fmt); |
| 75 |
|
| 76 |
DISABLE_WARNING_FORMAT_NONLITERAL; |
| 77 |
log_internalv(LOG_INFO, 0, NULL, 0, NULL, f, ap); |
| 78 |
REENABLE_WARNING; |
| 79 |
} |
| 80 |
|
| 81 |
int mhd_respond_internal( |
| 82 |
struct MHD_Connection *connection, |
| 83 |
enum MHD_RequestTerminationCode code, |
| 84 |
const char *encoding, |
| 85 |
const char *buffer, |
| 86 |
size_t size, |
| 87 |
enum MHD_ResponseMemoryMode mode) { |
| 88 |
|
| 89 |
assert(connection); |
| 90 |
|
| 91 |
_cleanup_(MHD_destroy_responsep) struct MHD_Response *response |
| 92 |
= sym_MHD_create_response_from_buffer(size, (char*) buffer, mode); |
| 93 |
if (!response) |
| 94 |
return MHD_NO; |
| 95 |
|
| 96 |
log_debug("Queueing response %u: %s", code, buffer); |
| 97 |
if (encoding) |
| 98 |
if (sym_MHD_add_response_header(response, "Accept-Encoding", encoding) == MHD_NO) |
| 99 |
return MHD_NO; |
| 100 |
|
| 101 |
if (sym_MHD_add_response_header(response, "Content-Type", "text/plain") == MHD_NO) |
| 102 |
return MHD_NO; |
| 103 |
return sym_MHD_queue_response(connection, code, response); |
| 104 |
} |
| 105 |
|
| 106 |
int mhd_respond_oom(struct MHD_Connection *connection) { |
| 107 |
return mhd_respond(connection, MHD_HTTP_SERVICE_UNAVAILABLE, "Out of memory."); |
| 108 |
} |
| 109 |
|
| 110 |
int mhd_respondf_internal( |
| 111 |
struct MHD_Connection *connection, |
| 112 |
int error, |
| 113 |
enum MHD_RequestTerminationCode code, |
| 114 |
const char *encoding, |
| 115 |
const char *format, ...) { |
| 116 |
|
| 117 |
char *m; |
| 118 |
int r; |
| 119 |
va_list ap; |
| 120 |
|
| 121 |
assert(connection); |
| 122 |
assert(format); |
| 123 |
|
| 124 |
errno = ERRNO_VALUE(error); |
| 125 |
va_start(ap, format); |
| 126 |
r = vasprintf(&m, format, ap); |
| 127 |
va_end(ap); |
| 128 |
|
| 129 |
if (r < 0) |
| 130 |
return respond_oom(connection); |
| 131 |
|
| 132 |
return mhd_respond_internal(connection, code, encoding, m, r, MHD_RESPMEM_MUST_FREE); |
| 133 |
} |
| 134 |
|
| 135 |
#if HAVE_GNUTLS |
| 136 |
|
| 137 |
static struct { |
| 138 |
const char *const names[4]; |
| 139 |
int level; |
| 140 |
bool enabled; |
| 141 |
} gnutls_log_map[] = { |
| 142 |
{ {"0"}, LOG_DEBUG }, |
| 143 |
{ {"1", "audit"}, LOG_WARNING, true}, /* gnutls session audit */ |
| 144 |
{ {"2", "assert"}, LOG_DEBUG }, /* gnutls assert log */ |
| 145 |
{ {"3", "hsk", "ext"}, LOG_DEBUG }, /* gnutls handshake log */ |
| 146 |
{ {"4", "rec"}, LOG_DEBUG }, /* gnutls record log */ |
| 147 |
{ {"5", "dtls"}, LOG_DEBUG }, /* gnutls DTLS log */ |
| 148 |
{ {"6", "buf"}, LOG_DEBUG }, |
| 149 |
{ {"7", "write", "read"}, LOG_DEBUG }, |
| 150 |
{ {"8"}, LOG_DEBUG }, |
| 151 |
{ {"9", "enc", "int"}, LOG_DEBUG }, |
| 152 |
}; |
| 153 |
|
| 154 |
static void log_func_gnutls(int level, const char *message) { |
| 155 |
assert_se(message); |
| 156 |
|
| 157 |
if (0 <= level && level < (int) ELEMENTSOF(gnutls_log_map)) { |
| 158 |
if (gnutls_log_map[level].enabled) |
| 159 |
log_internal(gnutls_log_map[level].level, 0, NULL, 0, NULL, "gnutls %d/%s: %s", level, gnutls_log_map[level].names[1], message); |
| 160 |
} else { |
| 161 |
log_debug("Received GNUTLS message with unknown level %d.", level); |
| 162 |
log_internal(LOG_DEBUG, 0, NULL, 0, NULL, "gnutls: %s", message); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
static void log_reset_gnutls_level(void) { |
| 167 |
int i; |
| 168 |
|
| 169 |
for (i = ELEMENTSOF(gnutls_log_map) - 1; i >= 0; i--) |
| 170 |
if (gnutls_log_map[i].enabled) { |
| 171 |
log_debug("Setting gnutls log level to %d", i); |
| 172 |
sym_gnutls_global_set_log_level(i); |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static int log_enable_gnutls_category(const char *cat) { |
| 178 |
if (streq(cat, "all")) { |
| 179 |
FOREACH_ELEMENT(entry, gnutls_log_map) |
| 180 |
entry->enabled = true; |
| 181 |
log_reset_gnutls_level(); |
| 182 |
return 0; |
| 183 |
} else |
| 184 |
FOREACH_ELEMENT(entry, gnutls_log_map) |
| 185 |
if (strv_contains((char**)entry->names, cat)) { |
| 186 |
entry->enabled = true; |
| 187 |
log_reset_gnutls_level(); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No such log category: %s", cat); |
| 191 |
} |
| 192 |
|
| 193 |
int setup_gnutls_logger(char **categories) { |
| 194 |
int r; |
| 195 |
|
| 196 |
r = dlopen_gnutls(LOG_DEBUG); |
| 197 |
if (r < 0) { |
| 198 |
if (categories) |
| 199 |
log_notice("Ignoring specified gnutls logging categories -- gnutls not available."); |
| 200 |
else |
| 201 |
log_debug("GnuTLS not available, skipping logger setup."); |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
sym_gnutls_global_set_log_function(log_func_gnutls); |
| 206 |
|
| 207 |
if (categories) |
| 208 |
STRV_FOREACH(cat, categories) { |
| 209 |
r = log_enable_gnutls_category(*cat); |
| 210 |
if (r < 0) |
| 211 |
return r; |
| 212 |
} |
| 213 |
else |
| 214 |
log_reset_gnutls_level(); |
| 215 |
|
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
static int verify_cert_authorized(gnutls_session_t session) { |
| 220 |
unsigned status; |
| 221 |
gnutls_certificate_type_t type; |
| 222 |
gnutls_datum_t out; |
| 223 |
int r; |
| 224 |
|
| 225 |
r = sym_gnutls_certificate_verify_peers2(session, &status); |
| 226 |
if (r < 0) |
| 227 |
return log_error_errno(r, "gnutls_certificate_verify_peers2 failed: %m"); |
| 228 |
|
| 229 |
type = sym_gnutls_certificate_type_get(session); |
| 230 |
r = sym_gnutls_certificate_verification_status_print(status, type, &out, 0); |
| 231 |
if (r < 0) |
| 232 |
return log_error_errno(r, "gnutls_certificate_verification_status_print failed: %m"); |
| 233 |
|
| 234 |
log_debug("Certificate status: %s", out.data); |
| 235 |
/* gnutls_free is declared as a function pointer variable (not a function), so sym_gnutls_free |
| 236 |
* ends up as a pointer-to-function-pointer and must be explicitly dereferenced to be called. */ |
| 237 |
(*sym_gnutls_free)(out.data); |
| 238 |
|
| 239 |
return status == 0 ? 0 : -EPERM; |
| 240 |
} |
| 241 |
|
| 242 |
static int get_client_cert(gnutls_session_t session, gnutls_x509_crt_t *client_cert) { |
| 243 |
const gnutls_datum_t *pcert; |
| 244 |
unsigned listsize; |
| 245 |
gnutls_x509_crt_t cert; |
| 246 |
int r; |
| 247 |
|
| 248 |
assert(session); |
| 249 |
assert(client_cert); |
| 250 |
|
| 251 |
pcert = sym_gnutls_certificate_get_peers(session, &listsize); |
| 252 |
if (!pcert || !listsize) |
| 253 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 254 |
"Failed to retrieve certificate chain"); |
| 255 |
|
| 256 |
r = sym_gnutls_x509_crt_init(&cert); |
| 257 |
if (r < 0) { |
| 258 |
log_error("Failed to initialize client certificate"); |
| 259 |
return r; |
| 260 |
} |
| 261 |
|
| 262 |
/* Note that by passing values between 0 and listsize here, you |
| 263 |
can get access to the CA's certs */ |
| 264 |
r = sym_gnutls_x509_crt_import(cert, &pcert[0], GNUTLS_X509_FMT_DER); |
| 265 |
if (r < 0) { |
| 266 |
log_error("Failed to import client certificate"); |
| 267 |
sym_gnutls_x509_crt_deinit(cert); |
| 268 |
return r; |
| 269 |
} |
| 270 |
|
| 271 |
*client_cert = cert; |
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
static int get_auth_dn(gnutls_x509_crt_t client_cert, char **buf) { |
| 276 |
size_t len = 0; |
| 277 |
int r; |
| 278 |
|
| 279 |
assert(buf); |
| 280 |
assert(*buf == NULL); |
| 281 |
|
| 282 |
r = sym_gnutls_x509_crt_get_dn(client_cert, NULL, &len); |
| 283 |
if (r != GNUTLS_E_SHORT_MEMORY_BUFFER) { |
| 284 |
log_error("gnutls_x509_crt_get_dn failed"); |
| 285 |
return r; |
| 286 |
} |
| 287 |
|
| 288 |
*buf = malloc(len); |
| 289 |
if (!*buf) |
| 290 |
return log_oom(); |
| 291 |
|
| 292 |
sym_gnutls_x509_crt_get_dn(client_cert, *buf, &len); |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
static void gnutls_x509_crt_deinitp(gnutls_x509_crt_t *p) { |
| 297 |
assert(p); |
| 298 |
|
| 299 |
if (*p) |
| 300 |
sym_gnutls_x509_crt_deinit(*p); |
| 301 |
} |
| 302 |
|
| 303 |
int check_permissions(struct MHD_Connection *connection, int *code, char **hostname) { |
| 304 |
const union MHD_ConnectionInfo *ci; |
| 305 |
gnutls_session_t session; |
| 306 |
_cleanup_(gnutls_x509_crt_deinitp) gnutls_x509_crt_t client_cert = NULL; |
| 307 |
_cleanup_free_ char *buf = NULL; |
| 308 |
int r; |
| 309 |
|
| 310 |
assert(connection); |
| 311 |
assert(code); |
| 312 |
|
| 313 |
*code = 0; |
| 314 |
|
| 315 |
r = dlopen_gnutls(LOG_ERR); |
| 316 |
if (r < 0) |
| 317 |
return r; |
| 318 |
|
| 319 |
ci = sym_MHD_get_connection_info(connection, |
| 320 |
MHD_CONNECTION_INFO_GNUTLS_SESSION); |
| 321 |
if (!ci) { |
| 322 |
log_error("MHD_get_connection_info failed: session is unencrypted"); |
| 323 |
*code = mhd_respond(connection, MHD_HTTP_FORBIDDEN, |
| 324 |
"Encrypted connection is required"); |
| 325 |
return -EPERM; |
| 326 |
} |
| 327 |
session = ci->tls_session; |
| 328 |
assert(session); |
| 329 |
|
| 330 |
r = get_client_cert(session, &client_cert); |
| 331 |
if (r < 0) { |
| 332 |
*code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED, |
| 333 |
"Authorization through certificate is required"); |
| 334 |
return -EPERM; |
| 335 |
} |
| 336 |
|
| 337 |
r = get_auth_dn(client_cert, &buf); |
| 338 |
if (r < 0) { |
| 339 |
*code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED, |
| 340 |
"Failed to determine distinguished name from certificate"); |
| 341 |
return -EPERM; |
| 342 |
} |
| 343 |
|
| 344 |
log_debug("Connection from %s", buf); |
| 345 |
|
| 346 |
if (hostname) |
| 347 |
*hostname = TAKE_PTR(buf); |
| 348 |
|
| 349 |
r = verify_cert_authorized(session); |
| 350 |
if (r < 0) { |
| 351 |
log_warning("Client is not authorized"); |
| 352 |
*code = mhd_respond(connection, MHD_HTTP_UNAUTHORIZED, |
| 353 |
"Client certificate not signed by recognized authority"); |
| 354 |
} |
| 355 |
return r; |
| 356 |
} |
| 357 |
|
| 358 |
#else |
| 359 |
_noreturn_ int check_permissions(struct MHD_Connection *connection, int *code, char **hostname) { |
| 360 |
assert_not_reached(); |
| 361 |
} |
| 362 |
|
| 363 |
int setup_gnutls_logger(char **categories) { |
| 364 |
if (categories) |
| 365 |
log_notice("Ignoring specified gnutls logging categories — gnutls not available."); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
#endif |
| 369 |
|
| 370 |
#endif |