| 1 |
/* |
| 2 |
HTTP session handling |
| 3 |
Copyright (C) 1999-2009, Joe Orton <[email protected]> |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Library General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Library General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Library General Public |
| 16 |
License along with this library; if not, write to the Free |
| 17 |
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 18 |
MA 02111-1307, USA |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#ifdef HAVE_STRING_H |
| 25 |
#include <string.h> |
| 26 |
#endif |
| 27 |
#ifdef HAVE_STDLIB_H |
| 28 |
#include <stdlib.h> |
| 29 |
#endif |
| 30 |
#ifdef HAVE_ERRNO_H |
| 31 |
#include <errno.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef HAVE_LIBPROXY |
| 35 |
#include <proxy.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "ne_session.h" |
| 39 |
#include "ne_alloc.h" |
| 40 |
#include "ne_utils.h" |
| 41 |
#include "ne_internal.h" |
| 42 |
#include "ne_string.h" |
| 43 |
#include "ne_dates.h" |
| 44 |
|
| 45 |
#include "ne_private.h" |
| 46 |
|
| 47 |
/* Destroy a a list of hooks. */ |
| 48 |
static void destroy_hooks(struct hook *hooks) |
| 49 |
{ |
| 50 |
struct hook *nexthk; |
| 51 |
|
| 52 |
while (hooks) { |
| 53 |
nexthk = hooks->next; |
| 54 |
ne_free(hooks); |
| 55 |
hooks = nexthk; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static void free_hostinfo(struct host_info *hi) |
| 60 |
{ |
| 61 |
if (hi->hostname) ne_free(hi->hostname); |
| 62 |
if (hi->hostport) ne_free(hi->hostport); |
| 63 |
if (hi->address) ne_addr_destroy(hi->address); |
| 64 |
} |
| 65 |
|
| 66 |
/* Destroy the sess->proxies array. */ |
| 67 |
static void free_proxies(ne_session *sess) |
| 68 |
{ |
| 69 |
struct host_info *hi, *nexthi; |
| 70 |
|
| 71 |
for (hi = sess->proxies; hi; hi = nexthi) { |
| 72 |
nexthi = hi->next; |
| 73 |
free_hostinfo(hi); |
| 74 |
ne_free(hi); |
| 75 |
} |
| 76 |
|
| 77 |
sess->any_proxy_http = 0; |
| 78 |
} |
| 79 |
|
| 80 |
void ne_session_destroy(ne_session *sess) |
| 81 |
{ |
| 82 |
struct hook *hk; |
| 83 |
|
| 84 |
NE_DEBUG(NE_DBG_CORE, "sess: Destroying session."); |
| 85 |
|
| 86 |
/* Run the destroy hooks. */ |
| 87 |
for (hk = sess->destroy_sess_hooks; hk != NULL; hk = hk->next) { |
| 88 |
ne_destroy_sess_fn fn = (ne_destroy_sess_fn)hk->fn; |
| 89 |
fn(hk->userdata); |
| 90 |
} |
| 91 |
|
| 92 |
/* Close the connection; note that the notifier callback could |
| 93 |
* still be invoked here. */ |
| 94 |
if (sess->connected) { |
| 95 |
ne_close_connection(sess); |
| 96 |
} |
| 97 |
|
| 98 |
destroy_hooks(sess->create_req_hooks); |
| 99 |
destroy_hooks(sess->pre_send_hooks); |
| 100 |
destroy_hooks(sess->post_headers_hooks); |
| 101 |
destroy_hooks(sess->post_send_hooks); |
| 102 |
destroy_hooks(sess->destroy_req_hooks); |
| 103 |
destroy_hooks(sess->destroy_sess_hooks); |
| 104 |
destroy_hooks(sess->close_conn_hooks); |
| 105 |
destroy_hooks(sess->private); |
| 106 |
|
| 107 |
ne_free(sess->scheme); |
| 108 |
|
| 109 |
free_hostinfo(&sess->server); |
| 110 |
free_proxies(sess); |
| 111 |
|
| 112 |
if (sess->user_agent) ne_free(sess->user_agent); |
| 113 |
if (sess->socks_user) ne_free(sess->socks_user); |
| 114 |
if (sess->socks_password) ne_free(sess->socks_password); |
| 115 |
|
| 116 |
#ifdef NE_HAVE_SSL |
| 117 |
if (sess->ssl_context) |
| 118 |
ne_ssl_context_destroy(sess->ssl_context); |
| 119 |
|
| 120 |
if (sess->server_cert) |
| 121 |
ne_ssl_cert_free(sess->server_cert); |
| 122 |
|
| 123 |
if (sess->client_cert) |
| 124 |
ne_ssl_clicert_free(sess->client_cert); |
| 125 |
#endif |
| 126 |
|
| 127 |
ne_free(sess); |
| 128 |
} |
| 129 |
|
| 130 |
int ne_version_pre_http11(ne_session *s) |
| 131 |
{ |
| 132 |
return !s->is_http11; |
| 133 |
} |
| 134 |
|
| 135 |
/* Stores the "hostname[:port]" segment */ |
| 136 |
static void set_hostport(struct host_info *host, unsigned int defaultport) |
| 137 |
{ |
| 138 |
size_t len = strlen(host->hostname); |
| 139 |
host->hostport = ne_malloc(len + 10); |
| 140 |
strcpy(host->hostport, host->hostname); |
| 141 |
if (host->port != defaultport) |
| 142 |
ne_snprintf(host->hostport + len, 9, ":%u", host->port); |
| 143 |
} |
| 144 |
|
| 145 |
/* Stores the hostname/port in *info, setting up the "hostport" |
| 146 |
* segment correctly. */ |
| 147 |
static void set_hostinfo(struct host_info *hi, enum proxy_type type, |
| 148 |
const char *hostname, unsigned int port) |
| 149 |
{ |
| 150 |
hi->hostname = ne_strdup(hostname); |
| 151 |
hi->port = port; |
| 152 |
hi->proxy = type; |
| 153 |
} |
| 154 |
|
| 155 |
ne_session *ne_session_create(const char *scheme, |
| 156 |
const char *hostname, unsigned int port) |
| 157 |
{ |
| 158 |
ne_session *sess = ne_calloc(sizeof *sess); |
| 159 |
|
| 160 |
NE_DEBUG(NE_DBG_HTTP, "HTTP session to %s://%s:%d begins.", |
| 161 |
scheme, hostname, port); |
| 162 |
|
| 163 |
strcpy(sess->error, "Unknown error."); |
| 164 |
|
| 165 |
/* use SSL if scheme is https */ |
| 166 |
sess->use_ssl = !strcmp(scheme, "https"); |
| 167 |
|
| 168 |
/* set the hostname/port */ |
| 169 |
set_hostinfo(&sess->server, PROXY_NONE, hostname, port); |
| 170 |
set_hostport(&sess->server, sess->use_ssl?443:80); |
| 171 |
|
| 172 |
#ifdef NE_HAVE_SSL |
| 173 |
if (sess->use_ssl) { |
| 174 |
ne_inet_addr *ia; |
| 175 |
|
| 176 |
sess->ssl_context = ne_ssl_context_create(0); |
| 177 |
sess->flags[NE_SESSFLAG_SSLv2] = 1; |
| 178 |
|
| 179 |
/* If the hostname parses as an IP address, don't |
| 180 |
* enable SNI by default. */ |
| 181 |
ia = ne_iaddr_parse(hostname, ne_iaddr_ipv4); |
| 182 |
if (ia == NULL) |
| 183 |
ia = ne_iaddr_parse(hostname, ne_iaddr_ipv6); |
| 184 |
|
| 185 |
if (ia) { |
| 186 |
ne_iaddr_free(ia); |
| 187 |
} |
| 188 |
else { |
| 189 |
sess->flags[NE_SESSFLAG_TLS_SNI] = 1; |
| 190 |
} |
| 191 |
NE_DEBUG(NE_DBG_SSL, "ssl: SNI %s by default.", |
| 192 |
sess->flags[NE_SESSFLAG_TLS_SNI] ? |
| 193 |
"enabled" : "disabled"); |
| 194 |
} |
| 195 |
#endif |
| 196 |
|
| 197 |
sess->scheme = ne_strdup(scheme); |
| 198 |
|
| 199 |
/* Set flags which default to on: */ |
| 200 |
sess->flags[NE_SESSFLAG_PERSIST] = 1; |
| 201 |
|
| 202 |
return sess; |
| 203 |
} |
| 204 |
|
| 205 |
void ne_session_proxy(ne_session *sess, const char *hostname, |
| 206 |
unsigned int port) |
| 207 |
{ |
| 208 |
free_proxies(sess); |
| 209 |
|
| 210 |
sess->proxies = ne_calloc(sizeof *sess->proxies); |
| 211 |
|
| 212 |
sess->any_proxy_http = 1; |
| 213 |
|
| 214 |
set_hostinfo(sess->proxies, PROXY_HTTP, hostname, port); |
| 215 |
} |
| 216 |
|
| 217 |
void ne_session_socks_proxy(ne_session *sess, enum ne_sock_sversion vers, |
| 218 |
const char *hostname, unsigned int port, |
| 219 |
const char *username, const char *password) |
| 220 |
{ |
| 221 |
free_proxies(sess); |
| 222 |
|
| 223 |
sess->proxies = ne_calloc(sizeof *sess->proxies); |
| 224 |
|
| 225 |
set_hostinfo(sess->proxies, PROXY_SOCKS, hostname, port); |
| 226 |
|
| 227 |
sess->socks_ver = vers; |
| 228 |
|
| 229 |
if (username) sess->socks_user = ne_strdup(username); |
| 230 |
if (password) sess->socks_password = ne_strdup(password); |
| 231 |
} |
| 232 |
|
| 233 |
void ne_session_system_proxy(ne_session *sess, unsigned int flags) |
| 234 |
{ |
| 235 |
#ifdef HAVE_LIBPROXY |
| 236 |
pxProxyFactory *pxf = px_proxy_factory_new(); |
| 237 |
struct host_info *hi, **lasthi; |
| 238 |
char *url, **proxies; |
| 239 |
ne_uri uri; |
| 240 |
unsigned n; |
| 241 |
|
| 242 |
free_proxies(sess); |
| 243 |
|
| 244 |
/* Create URI for session to pass off to libproxy */ |
| 245 |
memset(&uri, 0, sizeof uri); |
| 246 |
ne_fill_server_uri(sess, &uri); |
| 247 |
|
| 248 |
uri.path = "/"; /* make valid URI structure. */ |
| 249 |
url = ne_uri_unparse(&uri); |
| 250 |
uri.path = NULL; |
| 251 |
|
| 252 |
/* Get list of pseudo-URIs from libproxy: */ |
| 253 |
proxies = px_proxy_factory_get_proxies(pxf, url); |
| 254 |
|
| 255 |
for (n = 0, lasthi = &sess->proxies; proxies[n]; n++) { |
| 256 |
enum proxy_type ptype; |
| 257 |
|
| 258 |
ne_uri_free(&uri); |
| 259 |
|
| 260 |
NE_DEBUG(NE_DBG_HTTP, "sess: libproxy #%u=%s", |
| 261 |
n, proxies[n]); |
| 262 |
|
| 263 |
if (ne_uri_parse(proxies[n], &uri)) |
| 264 |
continue; |
| 265 |
|
| 266 |
if (!uri.scheme) continue; |
| 267 |
|
| 268 |
if (ne_strcasecmp(uri.scheme, "http") == 0) |
| 269 |
ptype = PROXY_HTTP; |
| 270 |
else if (ne_strcasecmp(uri.scheme, "socks") == 0) |
| 271 |
ptype = PROXY_SOCKS; |
| 272 |
else if (ne_strcasecmp(uri.scheme, "direct") == 0) |
| 273 |
ptype = PROXY_NONE; |
| 274 |
else |
| 275 |
continue; |
| 276 |
|
| 277 |
/* Hostname/port required for http/socks schemes. */ |
| 278 |
if (ptype != PROXY_NONE && !(uri.host && uri.port)) |
| 279 |
continue; |
| 280 |
|
| 281 |
/* Do nothing if libproxy returned only a single "direct://" |
| 282 |
* entry -- a single "direct" (noop) proxy is equivalent to |
| 283 |
* having none. */ |
| 284 |
if (n == 0 && proxies[1] == NULL && ptype == PROXY_NONE) |
| 285 |
break; |
| 286 |
|
| 287 |
NE_DEBUG(NE_DBG_HTTP, "sess: Got proxy %s://%s:%d", |
| 288 |
uri.scheme, uri.host ? uri.host : "(none)", |
| 289 |
uri.port); |
| 290 |
|
| 291 |
hi = *lasthi = ne_calloc(sizeof *hi); |
| 292 |
|
| 293 |
if (ptype == PROXY_NONE) { |
| 294 |
/* A "direct" URI requires an attempt to connect directly to |
| 295 |
* the origin server, so dup the server details. */ |
| 296 |
set_hostinfo(hi, ptype, sess->server.hostname, |
| 297 |
sess->server.port); |
| 298 |
} |
| 299 |
else { |
| 300 |
/* SOCKS/HTTP proxy. */ |
| 301 |
set_hostinfo(hi, ptype, uri.host, uri.port); |
| 302 |
|
| 303 |
if (ptype == PROXY_HTTP) |
| 304 |
sess->any_proxy_http = 1; |
| 305 |
else if (ptype == PROXY_SOCKS) |
| 306 |
sess->socks_ver = NE_SOCK_SOCKSV5; |
| 307 |
} |
| 308 |
|
| 309 |
lasthi = &hi->next; |
| 310 |
} |
| 311 |
|
| 312 |
/* Free up the proxies array: */ |
| 313 |
for (n = 0; proxies[n]; n++) |
| 314 |
free(proxies[n]); |
| 315 |
free(proxies[n]); |
| 316 |
|
| 317 |
ne_free(url); |
| 318 |
ne_uri_free(&uri); |
| 319 |
px_proxy_factory_free(pxf); |
| 320 |
#endif |
| 321 |
} |
| 322 |
|
| 323 |
void ne_set_addrlist(ne_session *sess, const ne_inet_addr **addrs, size_t n) |
| 324 |
{ |
| 325 |
struct host_info *hi, **lasthi; |
| 326 |
size_t i; |
| 327 |
|
| 328 |
free_proxies(sess); |
| 329 |
|
| 330 |
lasthi = &sess->proxies; |
| 331 |
|
| 332 |
for (i = 0; i < n; i++) { |
| 333 |
*lasthi = hi = ne_calloc(sizeof *hi); |
| 334 |
|
| 335 |
hi->proxy = PROXY_NONE; |
| 336 |
hi->network = addrs[i]; |
| 337 |
hi->port = sess->server.port; |
| 338 |
|
| 339 |
lasthi = &hi->next; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
void ne_set_localaddr(ne_session *sess, const ne_inet_addr *addr) |
| 344 |
{ |
| 345 |
sess->local_addr = addr; |
| 346 |
} |
| 347 |
|
| 348 |
void ne_set_error(ne_session *sess, const char *format, ...) |
| 349 |
{ |
| 350 |
va_list params; |
| 351 |
|
| 352 |
va_start(params, format); |
| 353 |
ne_vsnprintf(sess->error, sizeof sess->error, format, params); |
| 354 |
va_end(params); |
| 355 |
} |
| 356 |
|
| 357 |
void ne_set_session_flag(ne_session *sess, ne_session_flag flag, int value) |
| 358 |
{ |
| 359 |
if (flag < NE_SESSFLAG_LAST) { |
| 360 |
sess->flags[flag] = value; |
| 361 |
#ifdef NE_HAVE_SSL |
| 362 |
if (flag == NE_SESSFLAG_SSLv2 && sess->ssl_context) { |
| 363 |
ne_ssl_context_set_flag(sess->ssl_context, NE_SSL_CTX_SSLv2, value); |
| 364 |
} |
| 365 |
#endif |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
int ne_get_session_flag(ne_session *sess, ne_session_flag flag) |
| 370 |
{ |
| 371 |
if (flag < NE_SESSFLAG_LAST) { |
| 372 |
return sess->flags[flag]; |
| 373 |
} |
| 374 |
return -1; |
| 375 |
} |
| 376 |
|
| 377 |
static void progress_notifier(void *userdata, ne_session_status status, |
| 378 |
const ne_session_status_info *info) |
| 379 |
{ |
| 380 |
ne_session *sess = userdata; |
| 381 |
|
| 382 |
if (status == ne_status_sending || status == ne_status_recving) { |
| 383 |
sess->progress_cb(sess->progress_ud, info->sr.progress, info->sr.total); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
void ne_set_progress(ne_session *sess, ne_progress progress, void *userdata) |
| 388 |
{ |
| 389 |
if (progress) { |
| 390 |
sess->progress_cb = progress; |
| 391 |
sess->progress_ud = userdata; |
| 392 |
ne_set_notifier(sess, progress_notifier, sess); |
| 393 |
} |
| 394 |
else { |
| 395 |
ne_set_notifier(sess, NULL, NULL); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
void ne_set_notifier(ne_session *sess, |
| 400 |
ne_notify_status status, void *userdata) |
| 401 |
{ |
| 402 |
sess->notify_cb = status; |
| 403 |
sess->notify_ud = userdata; |
| 404 |
} |
| 405 |
|
| 406 |
void ne_set_read_timeout(ne_session *sess, int timeout) |
| 407 |
{ |
| 408 |
sess->rdtimeout = timeout; |
| 409 |
} |
| 410 |
|
| 411 |
void ne_set_connect_timeout(ne_session *sess, int timeout) |
| 412 |
{ |
| 413 |
sess->cotimeout = timeout; |
| 414 |
} |
| 415 |
|
| 416 |
#define UAHDR "User-Agent: " |
| 417 |
#define AGENT " neon/" NEON_VERSION "\r\n" |
| 418 |
|
| 419 |
void ne_set_useragent(ne_session *sess, const char *token) |
| 420 |
{ |
| 421 |
if (sess->user_agent) ne_free(sess->user_agent); |
| 422 |
sess->user_agent = ne_malloc(strlen(UAHDR) + strlen(AGENT) + |
| 423 |
strlen(token) + 1); |
| 424 |
#ifdef HAVE_STPCPY |
| 425 |
strcpy(stpcpy(stpcpy(sess->user_agent, UAHDR), token), AGENT); |
| 426 |
#else |
| 427 |
strcat(strcat(strcpy(sess->user_agent, UAHDR), token), AGENT); |
| 428 |
#endif |
| 429 |
} |
| 430 |
|
| 431 |
const char *ne_get_server_hostport(ne_session *sess) |
| 432 |
{ |
| 433 |
return sess->server.hostport; |
| 434 |
} |
| 435 |
|
| 436 |
const char *ne_get_scheme(ne_session *sess) |
| 437 |
{ |
| 438 |
return sess->scheme; |
| 439 |
} |
| 440 |
|
| 441 |
void ne_fill_server_uri(ne_session *sess, ne_uri *uri) |
| 442 |
{ |
| 443 |
uri->host = ne_strdup(sess->server.hostname); |
| 444 |
uri->port = sess->server.port; |
| 445 |
uri->scheme = ne_strdup(sess->scheme); |
| 446 |
} |
| 447 |
|
| 448 |
void ne_fill_proxy_uri(ne_session *sess, ne_uri *uri) |
| 449 |
{ |
| 450 |
if (sess->proxies) { |
| 451 |
struct host_info *hi = sess->nexthop ? sess->nexthop : sess->proxies; |
| 452 |
|
| 453 |
if (hi->proxy == PROXY_HTTP) { |
| 454 |
uri->host = ne_strdup(hi->hostname); |
| 455 |
uri->port = hi->port; |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
const char *ne_get_error(ne_session *sess) |
| 461 |
{ |
| 462 |
if(sess) |
| 463 |
return sess->error; |
| 464 |
return NULL; |
| 465 |
} |
| 466 |
|
| 467 |
void ne_close_connection(ne_session *sess) |
| 468 |
{ |
| 469 |
if (sess->connected) { |
| 470 |
struct hook *hk; |
| 471 |
|
| 472 |
NE_DEBUG(NE_DBG_SOCKET, "sess: Closing connection."); |
| 473 |
|
| 474 |
if (sess->notify_cb) { |
| 475 |
sess->status.cd.hostname = sess->nexthop->hostname; |
| 476 |
sess->notify_cb(sess->notify_ud, ne_status_disconnected, |
| 477 |
&sess->status); |
| 478 |
} |
| 479 |
|
| 480 |
/* Run the close_conn hooks. */ |
| 481 |
for (hk = sess->close_conn_hooks; hk != NULL; hk = hk->next) { |
| 482 |
ne_close_conn_fn fn = (ne_close_conn_fn)hk->fn; |
| 483 |
fn(hk->userdata); |
| 484 |
} |
| 485 |
|
| 486 |
ne_sock_close(sess->socket); |
| 487 |
sess->socket = NULL; |
| 488 |
NE_DEBUG(NE_DBG_SOCKET, "sess: Connection closed."); |
| 489 |
} else { |
| 490 |
NE_DEBUG(NE_DBG_SOCKET, "sess: Not closing closed connection."); |
| 491 |
} |
| 492 |
sess->connected = 0; |
| 493 |
} |
| 494 |
|
| 495 |
void ne_ssl_set_verify(ne_session *sess, ne_ssl_verify_fn fn, void *userdata) |
| 496 |
{ |
| 497 |
sess->ssl_verify_fn = fn; |
| 498 |
sess->ssl_verify_ud = userdata; |
| 499 |
} |
| 500 |
|
| 501 |
void ne_ssl_provide_clicert(ne_session *sess, |
| 502 |
ne_ssl_provide_fn fn, void *userdata) |
| 503 |
{ |
| 504 |
sess->ssl_provide_fn = fn; |
| 505 |
sess->ssl_provide_ud = userdata; |
| 506 |
} |
| 507 |
|
| 508 |
void ne_ssl_trust_cert(ne_session *sess, const ne_ssl_certificate *cert) |
| 509 |
{ |
| 510 |
#ifdef NE_HAVE_SSL |
| 511 |
if (sess->ssl_context) { |
| 512 |
ne_ssl_context_trustcert(sess->ssl_context, cert); |
| 513 |
} |
| 514 |
#endif |
| 515 |
} |
| 516 |
|
| 517 |
int ne_ssl_truse_add_ca_path(ne_session* sess, const char* path){ |
| 518 |
#ifdef NE_HAVE_SSL |
| 519 |
if (sess->ssl_context) { |
| 520 |
return ne_ssl_context_trust_add_ca_path(sess->ssl_context, path); |
| 521 |
} |
| 522 |
#endif |
| 523 |
return -1; |
| 524 |
} |
| 525 |
|
| 526 |
void ne_ssl_cert_validity(const ne_ssl_certificate *cert, char *from, char *until) |
| 527 |
{ |
| 528 |
#ifdef NE_HAVE_SSL |
| 529 |
time_t tf, tu; |
| 530 |
char *date; |
| 531 |
|
| 532 |
ne_ssl_cert_validity_time(cert, &tf, &tu); |
| 533 |
|
| 534 |
if (from) { |
| 535 |
if (tf != (time_t) -1) { |
| 536 |
date = ne_rfc1123_date(tf); |
| 537 |
ne_strnzcpy(from, date, NE_SSL_VDATELEN); |
| 538 |
ne_free(date); |
| 539 |
} |
| 540 |
else { |
| 541 |
ne_strnzcpy(from, _("[invalid date]"), NE_SSL_VDATELEN); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
if (until) { |
| 546 |
if (tu != (time_t) -1) { |
| 547 |
date = ne_rfc1123_date(tu); |
| 548 |
ne_strnzcpy(until, date, NE_SSL_VDATELEN); |
| 549 |
ne_free(date); |
| 550 |
} |
| 551 |
else { |
| 552 |
ne_strnzcpy(until, _("[invalid date]"), NE_SSL_VDATELEN); |
| 553 |
} |
| 554 |
} |
| 555 |
#endif |
| 556 |
} |
| 557 |
|
| 558 |
#ifdef NE_HAVE_SSL |
| 559 |
void ne__ssl_set_verify_err(ne_session *sess, int failures) |
| 560 |
{ |
| 561 |
static const struct { |
| 562 |
int bit; |
| 563 |
const char *str; |
| 564 |
} reasons[] = { |
| 565 |
{ NE_SSL_NOTYETVALID, N_("certificate is not yet valid") }, |
| 566 |
{ NE_SSL_EXPIRED, N_("certificate has expired") }, |
| 567 |
{ NE_SSL_IDMISMATCH, N_("certificate issued for a different hostname") }, |
| 568 |
{ NE_SSL_UNTRUSTED, N_("issuer is not trusted") }, |
| 569 |
{ NE_SSL_BADCHAIN, N_("bad certificate chain") }, |
| 570 |
{ NE_SSL_REVOKED, N_("certificate has been revoked") }, |
| 571 |
{ 0, NULL } |
| 572 |
}; |
| 573 |
int n, flag = 0; |
| 574 |
|
| 575 |
strcpy(sess->error, _("Server certificate verification failed: ")); |
| 576 |
|
| 577 |
for (n = 0; reasons[n].bit; n++) { |
| 578 |
if (failures & reasons[n].bit) { |
| 579 |
if (flag) strncat(sess->error, ", ", sizeof sess->error - 1); |
| 580 |
strncat(sess->error, _(reasons[n].str), sizeof sess->error - 1); |
| 581 |
flag = 1; |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
/* This doesn't actually implement complete RFC 2818 logic; omits |
| 587 |
* "f*.example.com" support for simplicity. */ |
| 588 |
int ne__ssl_match_hostname(const char *cn, size_t cnlen, const char *hostname) |
| 589 |
{ |
| 590 |
const char *dot; |
| 591 |
|
| 592 |
NE_DEBUG(NE_DBG_SSL, "ssl: Match common name '%s' against '%s'", |
| 593 |
cn, hostname); |
| 594 |
|
| 595 |
/* Host might be an IPv6 address. E.g.: [2001:0db8:85a3::100] |
| 596 |
* Must remove the enclosing brackets to match the CN. */ |
| 597 |
size_t hlen = strlen(hostname); |
| 598 |
|
| 599 |
if (hlen > 0 && hostname[0] == '[' && hostname[hlen - 1] == ']') { |
| 600 |
char *hostname_ipv6; |
| 601 |
ne_inet_addr *ia; |
| 602 |
int match; |
| 603 |
|
| 604 |
hostname_ipv6 = strdup(hostname + 1); |
| 605 |
hostname_ipv6[hlen - 2] = '\0'; |
| 606 |
match = 0; |
| 607 |
|
| 608 |
if ((ia = ne_iaddr_parse(hostname_ipv6, ne_iaddr_ipv6))) { |
| 609 |
match = (cnlen == hlen - 2) && !ne_strcasecmp(cn, hostname_ipv6); |
| 610 |
ne_iaddr_free(ia); |
| 611 |
free(hostname_ipv6); |
| 612 |
} |
| 613 |
|
| 614 |
if (match) { |
| 615 |
return 1; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
if (strncmp(cn, "*.", 2) == 0 && cnlen > 2 |
| 620 |
&& (dot = strchr(hostname, '.')) != NULL) { |
| 621 |
ne_inet_addr *ia; |
| 622 |
|
| 623 |
/* Prevent wildcard CN matches against anything which can be |
| 624 |
* parsed as an IP address (i.e. a CN of "*.1.1.1" should not |
| 625 |
* be match 8.1.1.1). draft-saintandre-tls-server-id-check |
| 626 |
* will require some more significant changes to cert ID |
| 627 |
* verification which will probably obviate this check, but |
| 628 |
* this is a desirable policy tightening in the meantime. */ |
| 629 |
ia = ne_iaddr_parse(hostname, ne_iaddr_ipv4); |
| 630 |
if (ia == NULL) |
| 631 |
ia = ne_iaddr_parse(hostname, ne_iaddr_ipv6); |
| 632 |
|
| 633 |
if (ia) { |
| 634 |
NE_DEBUG(NE_DBG_SSL, "ssl: Denying wildcard match for numeric " |
| 635 |
"IP address.\n"); |
| 636 |
ne_iaddr_free(ia); |
| 637 |
return 0; |
| 638 |
} |
| 639 |
|
| 640 |
hostname = dot + 1; |
| 641 |
cn += 2; |
| 642 |
cnlen -= 2; |
| 643 |
} |
| 644 |
|
| 645 |
return cnlen == strlen(hostname) && !ne_strcasecmp(cn, hostname); |
| 646 |
} |
| 647 |
|
| 648 |
#endif /* NE_HAVE_SSL */ |
| 649 |
|
| 650 |
typedef void (*void_fn)(void); |
| 651 |
|
| 652 |
#define ADD_HOOK(hooks, fn, ud) add_hook(&(hooks), NULL, (void_fn)(fn), (ud)) |
| 653 |
|
| 654 |
static void add_hook(struct hook **hooks, const char *id, void_fn fn, void *ud) |
| 655 |
{ |
| 656 |
struct hook *hk = ne_malloc(sizeof (struct hook)), *pos; |
| 657 |
|
| 658 |
if (*hooks != NULL) { |
| 659 |
for (pos = *hooks; pos->next != NULL; pos = pos->next) |
| 660 |
/* nullop */; |
| 661 |
pos->next = hk; |
| 662 |
} else { |
| 663 |
*hooks = hk; |
| 664 |
} |
| 665 |
|
| 666 |
hk->id = id; |
| 667 |
hk->fn = fn; |
| 668 |
hk->userdata = ud; |
| 669 |
hk->next = NULL; |
| 670 |
} |
| 671 |
|
| 672 |
void ne_hook_create_request(ne_session *sess, |
| 673 |
ne_create_request_fn fn, void *userdata) |
| 674 |
{ |
| 675 |
ADD_HOOK(sess->create_req_hooks, fn, userdata); |
| 676 |
} |
| 677 |
|
| 678 |
void ne_hook_pre_send(ne_session *sess, ne_pre_send_fn fn, void *userdata) |
| 679 |
{ |
| 680 |
ADD_HOOK(sess->pre_send_hooks, fn, userdata); |
| 681 |
} |
| 682 |
|
| 683 |
void ne_hook_post_send(ne_session *sess, ne_post_send_fn fn, void *userdata) |
| 684 |
{ |
| 685 |
ADD_HOOK(sess->post_send_hooks, fn, userdata); |
| 686 |
} |
| 687 |
|
| 688 |
void ne_hook_post_headers(ne_session *sess, ne_post_headers_fn fn, |
| 689 |
void *userdata) |
| 690 |
{ |
| 691 |
ADD_HOOK(sess->post_headers_hooks, fn, userdata); |
| 692 |
} |
| 693 |
|
| 694 |
void ne_hook_destroy_request(ne_session *sess, |
| 695 |
ne_destroy_req_fn fn, void *userdata) |
| 696 |
{ |
| 697 |
ADD_HOOK(sess->destroy_req_hooks, fn, userdata); |
| 698 |
} |
| 699 |
|
| 700 |
void ne_hook_destroy_session(ne_session *sess, |
| 701 |
ne_destroy_sess_fn fn, void *userdata) |
| 702 |
{ |
| 703 |
ADD_HOOK(sess->destroy_sess_hooks, fn, userdata); |
| 704 |
} |
| 705 |
|
| 706 |
void ne_hook_close_conn(ne_session *sess, |
| 707 |
ne_close_conn_fn fn, void *userdata) |
| 708 |
{ |
| 709 |
ADD_HOOK(sess->close_conn_hooks, fn, userdata); |
| 710 |
} |
| 711 |
|
| 712 |
void ne_set_session_private(ne_session *sess, const char *id, void *userdata) |
| 713 |
{ |
| 714 |
add_hook(&sess->private, id, NULL, userdata); |
| 715 |
} |
| 716 |
|
| 717 |
static void remove_hook(struct hook **hooks, void_fn fn, void *ud) |
| 718 |
{ |
| 719 |
struct hook **p = hooks; |
| 720 |
|
| 721 |
while (*p) { |
| 722 |
if ((*p)->fn == fn && (*p)->userdata == ud) { |
| 723 |
struct hook *next = (*p)->next; |
| 724 |
ne_free(*p); |
| 725 |
(*p) = next; |
| 726 |
break; |
| 727 |
} |
| 728 |
p = &(*p)->next; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
#define REMOVE_HOOK(hooks, fn, ud) remove_hook(&hooks, (void_fn)fn, ud) |
| 733 |
|
| 734 |
void ne_unhook_create_request(ne_session *sess, |
| 735 |
ne_create_request_fn fn, void *userdata) |
| 736 |
{ |
| 737 |
REMOVE_HOOK(sess->create_req_hooks, fn, userdata); |
| 738 |
} |
| 739 |
|
| 740 |
void ne_unhook_pre_send(ne_session *sess, ne_pre_send_fn fn, void *userdata) |
| 741 |
{ |
| 742 |
REMOVE_HOOK(sess->pre_send_hooks, fn, userdata); |
| 743 |
} |
| 744 |
|
| 745 |
void ne_unhook_post_headers(ne_session *sess, ne_post_headers_fn fn, |
| 746 |
void *userdata) |
| 747 |
{ |
| 748 |
REMOVE_HOOK(sess->post_headers_hooks, fn, userdata); |
| 749 |
} |
| 750 |
|
| 751 |
void ne_unhook_post_send(ne_session *sess, ne_post_send_fn fn, void *userdata) |
| 752 |
{ |
| 753 |
REMOVE_HOOK(sess->post_send_hooks, fn, userdata); |
| 754 |
} |
| 755 |
|
| 756 |
void ne_unhook_destroy_request(ne_session *sess, |
| 757 |
ne_destroy_req_fn fn, void *userdata) |
| 758 |
{ |
| 759 |
REMOVE_HOOK(sess->destroy_req_hooks, fn, userdata); |
| 760 |
} |
| 761 |
|
| 762 |
void ne_unhook_destroy_session(ne_session *sess, |
| 763 |
ne_destroy_sess_fn fn, void *userdata) |
| 764 |
{ |
| 765 |
REMOVE_HOOK(sess->destroy_sess_hooks, fn, userdata); |
| 766 |
} |
| 767 |
|
| 768 |
void ne_unhook_close_conn(ne_session *sess, |
| 769 |
ne_close_conn_fn fn, void *userdata) |
| 770 |
{ |
| 771 |
REMOVE_HOOK(sess->close_conn_hooks, fn, userdata); |
| 772 |
} |