40#include <netinet/in.h>
49#include <netinet/tcp.h>
52#include <sys/socket.h>
59#include <gnutls/x509.h>
60#include <gnutls/gnutls.h>
70#define RESOLVER_THREAD 1
74#if GNUTLS_VERSION_NUMBER >= 0x030109
75#define SET_GNUTLS_SOCKET(ses, socket) gnutls_transport_set_int(ses, socket)
77#define SET_GNUTLS_SOCKET(ses, socket) \
78 gnutls_transport_set_ptr(ses, (gnutls_transport_ptr_t) (size_t) socket)
90 int (*check_ip)(
void *cd,
const char *a,
int len,
int type),
102#define YAZ_SOCKLEN_T int
106struct tcpip_cred_ptr {
107 gnutls_certificate_credentials_t xcred;
134 struct tcpip_cred_ptr *cred_ptr;
135 gnutls_session_t session;
136 char cert_fname[256];
151 static int initialized = 0;
160 requested = MAKEWORD(1, 1);
161 if (WSAStartup(requested, &wd))
187 sp->pipefd[0] = sp->pipefd[1] = -1;
194 strcpy(sp->cert_fname,
"yaz.pem");
250 const char *connect_host,
const char *connect_auth,
261 if (!cp || cp[1] ==
'\0')
277 if (connect_auth && strlen(connect_auth) < 40)
290 const char *connect_host,
291 const char *connect_auth,
302 const char *connect_host,
309 const char *connect_host)
315static void tcpip_create_cred(
COMSTACK cs)
318 sp->cred_ptr = (
struct tcpip_cred_ptr *)
xmalloc(
sizeof(*sp->cred_ptr));
319 sp->cred_ptr->ref = 1;
320 gnutls_certificate_allocate_credentials(&sp->cred_ptr->xcred);
337 sp->session = (gnutls_session_t) vp;
347 const char *connect_host,
348 const char *connect_auth,
349 const char *bind_host)
363 gnutls_error_is_fatal(res),
364 gnutls_strerror(res));
365 if (res == GNUTLS_E_AGAIN || res == GNUTLS_E_INTERRUPTED)
367 int dir = gnutls_record_get_direction(sp->session);
378 const char **host,
const char **port)
384 strncpy(tmp, host_port, tmp_sz-1);
402 cp1 = strchr(cp,
'/');
405 cp1 = strchr(cp,
'?');
408 cp1 = strrchr(cp,
':');
420 struct addrinfo hints, *res;
426 hints.ai_family = AF_UNSPEC;
427 hints.ai_socktype = SOCK_STREAM;
428 hints.ai_protocol = 0;
429 hints.ai_addrlen = 0;
430 hints.ai_addr = NULL;
431 hints.ai_canonname = NULL;
432 hints.ai_next = NULL;
437 if (!strcmp(
"@", host))
439 hints.ai_flags = AI_PASSIVE;
440 hints.ai_family = AF_UNSPEC;
441 error = getaddrinfo(0, port, &hints, &res);
444 else if (!strcmp(
"@4", host))
446 hints.ai_flags = AI_PASSIVE;
447 hints.ai_family = AF_INET;
448 error = getaddrinfo(0, port, &hints, &res);
451 else if (!strcmp(
"@6", host))
453 hints.ai_flags = AI_PASSIVE;
454 hints.ai_family = AF_INET6;
455 error = getaddrinfo(0, port, &hints, &res);
460 error = getaddrinfo(host, port, &hints, &res);
472 struct addrinfo *ai = 0;
475 for (ai = sp->
ai; ai; ai = ai->ai_next)
477 if (ai->ai_family == AF_INET6)
479 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
487 for (ai = sp->
ai; ai; ai = ai->ai_next)
489 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
499 if (ai->ai_family == AF_INET6 && sp->
ipv6_only >= 0 &&
512 if (setsockopt(h->
iofile, SOL_SOCKET, SO_REUSEADDR, (
char*)
513 &one,
sizeof(one)) < 0)
524 for (a = ai; a; a = a->ai_next)
526 r = bind(h->
iofile, a->ai_addr, a->ai_addrlen);
546void *resolver_thread(
void *arg)
553 freeaddrinfo(sp->
ai);
555 if (write(sp->pipefd[1],
"1", 1) != 1)
561static struct addrinfo *wait_resolver_thread(
COMSTACK h)
566 if (read(sp->pipefd[0], &buf, 1) == (ssize_t) -1)
569 close(sp->pipefd[0]);
570 close(sp->pipefd[1]);
581 const char *port =
"210";
598 if (sp->pipefd[0] != -1)
600 if (pipe(sp->pipefd) == -1)
609 freeaddrinfo(sp->
ai);
629 while (ai && (ai = ai->ai_next))
632 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
638 gnutls_bye(sp->session, GNUTLS_SHUT_WR);
639 gnutls_deinit(sp->session);
667 struct addrinfo *ai = (
struct addrinfo *) address;
678 if (sp->pipefd[0] != -1)
682 ai = wait_resolver_thread(h);
691 h->
iofile = sp->pipefd[0];
696 r = connect(h->
iofile, ai->ai_addr, ai->ai_addrlen);
701 if (WSAGetLastError() == WSAEWOULDBLOCK)
737 if (sp->pipefd[0] != -1)
739 struct addrinfo *ai = wait_resolver_thread(h);
781 const char *host = 0;
782 const char *port = 0;
785 tcpip_create_cred(h);
786 gnutls_init(&sp->session, GNUTLS_CLIENT);
788 gnutls_set_default_priority(sp->session);
789 gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE,
790 sp->cred_ptr->xcred);
793 if (!strchr(host,
':'))
794 gnutls_server_name_set(sp->session, GNUTLS_NAME_DNS,
796 SET_GNUTLS_SOCKET(sp->session, h->
iofile);
800 int res = gnutls_handshake(sp->session);
801 if (res == GNUTLS_E_SUCCESS)
803 if (ssl_check_again(h, sp, res))
823 struct addrinfo *ai = (
struct addrinfo *) address;
832 if (sp->pipefd[0] != -1)
834 ai = wait_resolver_thread(h);
843 tcpip_create_cred(h);
844 res = gnutls_certificate_set_x509_key_file(sp->cred_ptr->xcred,
847 GNUTLS_X509_FMT_PEM);
848 if (res != GNUTLS_E_SUCCESS)
850 yaz_log(
log_level,
"gnutls_certificate_set_x509_key_file r=%d fatal=%d msg=%s",
852 gnutls_error_is_fatal(res),
853 gnutls_strerror(res));
860 if (setsockopt(h->
iofile, SOL_SOCKET, SO_REUSEADDR, (
char*)
861 &one,
sizeof(one)) < 0)
867 r = bind(h->
iofile, ai->ai_addr, ai->ai_addrlen);
868 freeaddrinfo(sp->
ai);
887 int (*check_ip)(
void *cd,
const char *a,
int len,
int t),
893 struct sockaddr_in addr;
906 h->
newfd = accept(h->
iofile, (
struct sockaddr*)&addr, &len);
912 WSAGetLastError() == WSAEWOULDBLOCK
916#
if EAGAIN != EWOULDBLOCK
926 listen(h->
iofile, SOMAXCONN);
935 if (addrlen && (
size_t) (*addrlen) >=
sizeof(
struct sockaddr_in))
936 memcpy(raddr, &addr, *addrlen =
sizeof(
struct sockaddr_in));
939 if (check_ip && (*check_ip)(cd, (
const char *) &addr,
940 sizeof(addr), AF_INET))
944 closesocket(h->
newfd);
960 unsigned long tru = 1;
972 memcpy(cnew, h,
sizeof(*h));
983 closesocket(h->
newfd);
998 state->cred_ptr = st->cred_ptr;
1003 (state->cred_ptr->ref)++;
1004 gnutls_init(&state->session, GNUTLS_SERVER);
1005 if (!state->session)
1011 res = gnutls_set_default_priority(state->session);
1012 if (res != GNUTLS_E_SUCCESS)
1018 res = gnutls_credentials_set(state->session,
1019 GNUTLS_CRD_CERTIFICATE,
1020 st->cred_ptr->xcred);
1021 if (res != GNUTLS_E_SUCCESS)
1027 SET_GNUTLS_SOCKET(state->session, cnew->
iofile);
1038 int res = gnutls_handshake(state->session);
1039 if (res != GNUTLS_E_SUCCESS)
1041 if (ssl_check_again(h, state, res))
1064#define CS_TCPIP_BUFCHUNK 4096
1074 int tmpi, berlen, rest, req, tomove;
1075 int hasread = 0, res;
1091 while (!(berlen = (*sp->
complete)(*buf, hasread)))
1102 if (!(*buf =(
char *)
xrealloc(*buf, *bufsize *= 2)))
1110 res = gnutls_record_recv(sp->session, *buf + hasread,
1119 if (ssl_check_again(h, sp, res))
1143 if (WSAGetLastError() == WSAEWOULDBLOCK)
1156#
if EAGAIN != EWOULDBLOCK
1191 if (hasread > berlen)
1193 tomove = req = hasread - berlen;
1212 memcpy(sp->
altbuf, *buf + berlen, sp->
altlen = tomove);
1215 *(*buf + berlen) =
'\0';
1216 return berlen ? berlen : 1;
1238 else if (state->
towrite != size)
1248 res = gnutls_record_send(state->session,
buf + state->
written,
1252 if (ssl_check_again(h, state, res))
1276 WSAGetLastError() == WSAEWOULDBLOCK
1280#
if EAGAIN != EWOULDBLOCK
1320 if (sp->pipefd[0] != -1)
1323 close(sp->pipefd[0]);
1324 close(sp->pipefd[1]);
1331 if (sp->session && sp->use_bye)
1334 gnutls_bye(sp->session, GNUTLS_SHUT_WR);
1348 gnutls_deinit(sp->session);
1352 assert(sp->cred_ptr->ref > 0);
1354 if (--(sp->cred_ptr->ref) == 0)
1357 sp->cred_ptr->xcred);
1358 gnutls_certificate_free_credentials(sp->cred_ptr->xcred);
1359 xfree(sp->cred_ptr);
1365 freeaddrinfo(sp->
ai);
1379 struct sockaddr_storage addr;
1382 if (getpeername(h->
iofile, (
struct sockaddr *)&addr, &len) < 0)
1387 if (getnameinfo((
struct sockaddr *) &addr, len, host,
sizeof(host)-1,
1418 if (ioctlsocket(p->
iofile, FIONBIO, &flag) < 0)
1421 flag = fcntl(p->
iofile, F_GETFL, 0);
1423 flag = flag & ~O_NONBLOCK;
1426 flag = flag | O_NONBLOCK;
1427 signal(SIGPIPE, SIG_IGN);
1429 if (fcntl(p->
iofile, F_SETFL, flag) < 0)
1440#if GNUTLS_VERSION_NUMBER >= 0x020400
1441#define USE_GNUTLS_X509_CRT_PRINT 1
1443#define USE_GNUTLS_X509_CRT_PRINT 0
1447#if USE_GNUTLS_X509_CRT_PRINT
1449static const char *bin2hex(
const void *bin,
size_t bin_size)
1451 static char printable[110];
1452 const unsigned char *_bin = bin;
1458 for (i = 0; i < bin_size; i++)
1466static void x509_crt_print(gnutls_x509_crt_t cert)
1468 time_t expiration_time, activation_time;
1472 unsigned int algo, bits;
1474 expiration_time = gnutls_x509_crt_get_expiration_time(cert);
1475 activation_time = gnutls_x509_crt_get_activation_time(cert);
1477 printf(
"\tCertificate is valid since: %s", ctime(&activation_time));
1478 printf(
"\tCertificate expires: %s", ctime(&expiration_time));
1481 size =
sizeof(serial);
1482 gnutls_x509_crt_get_serial(cert, serial, &size);
1484 printf(
"\tCertificate serial number: %s\n", bin2hex(serial, size));
1488 algo = gnutls_x509_crt_get_pk_algorithm(cert, &bits);
1490 printf(
"Certificate public key: %s", gnutls_pk_algorithm_get_name(algo));
1493 printf(
"\tCertificate version: #%d\n", gnutls_x509_crt_get_version(cert));
1496 gnutls_x509_crt_get_dn(cert, dn, &size);
1497 printf(
"\tDN: %s\n", dn);
1500 gnutls_x509_crt_get_issuer_dn(cert, dn, &size);
1501 printf(
"\tIssuer's DN: %s\n", dn);
1512 const gnutls_datum_t *cert_list;
1513 unsigned i, cert_list_size;
1514 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1516 printf(
"X509 certificate\n");
1517 cert_list = gnutls_certificate_get_peers(sp->session,
1519 printf(
"Peer provided %u certificates\n", cert_list_size);
1520 for (i = 0; i < cert_list_size; i++)
1522 gnutls_x509_crt_t cert;
1523#if USE_GNUTLS_X509_CRT_PRINT
1525 gnutls_datum_t cinfo;
1527 gnutls_x509_crt_init(&cert);
1528 gnutls_x509_crt_import(cert, &cert_list[i], GNUTLS_X509_FMT_DER);
1529 printf(
"Certificate info %u:\n", i + 1);
1530#if USE_GNUTLS_X509_CRT_PRINT
1531 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL,
1535 printf(
"\t%s\n", cinfo.data);
1536 gnutls_free(cinfo.data);
1539 x509_crt_print(cert);
1541 gnutls_x509_crt_deinit(cert);
1572 strncpy(sp->cert_fname, fname,
sizeof(sp->cert_fname)-1);
1573 sp->cert_fname[
sizeof(sp->cert_fname)-1] =
'\0';
1584#if USE_GNUTLS_X509_CRT_PRINT
1588 const gnutls_datum_t *cert_list;
1589 unsigned cert_list_size;
1590 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1592 cert_list = gnutls_certificate_get_peers(sp->session, &cert_list_size);
1593 if (cert_list_size > 0)
1595 gnutls_x509_crt_t cert;
1597 gnutls_datum_t cinfo;
1599 gnutls_x509_crt_init(&cert);
1600 gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
1602 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL, &cinfo);
1606 *len = strlen(*
buf);
1607 gnutls_free(cinfo.data);
1608 gnutls_x509_crt_deinit(cert);
1611 gnutls_x509_crt_deinit(cert);
void yaz_base64encode(const char *in, char *out)
encodes Base64 string
Header for Base64 utilities.
int cs_complete_auto(const char *buf, int len)
int cs_complete_auto_head(const char *buf, int len)
struct comstack * COMSTACK
#define CS_FLAGS_DNS_NO_BLOCK
#define CS_FLAGS_BLOCKING
#define CS_FLAGS_NUMERICHOST
int yaz_errno(void)
returns errno
void yaz_set_errno(int v)
sets errno to value
Header for errno utilities.
void yaz_init_globals(void)
void yaz_log(int level, const char *fmt,...)
Writes log message.
int yaz_log_module_level(const char *name)
returns level for module
#define YLOG_WARN
log level: warning
void yaz_snprintf(char *buf, size_t size, const char *fmt,...)
Header for config file reading utilities.
int(* f_bind)(COMSTACK handle, void *address, int mode)
COMSTACK(* f_accept)(COMSTACK handle)
void(* f_close)(COMSTACK handle)
void *(* f_straddr)(COMSTACK handle, const char *str)
int(* f_listen)(COMSTACK h, char *raddr, int *addrlen, int(*check_ip)(void *cd, const char *a, int len, int type), void *cd)
int(* f_put)(COMSTACK handle, char *buf, int size)
int(* f_more)(COMSTACK handle)
int(* f_set_blocking)(COMSTACK handle, int blocking)
int(* f_rcvconnect)(COMSTACK handle)
int(* f_connect)(COMSTACK handle, void *address)
int(* f_get)(COMSTACK handle, char **buf, int *bufsize)
const char *(* f_addrstr)(COMSTACK handle)
struct addrinfo * ai_connect
char * connect_response_buf
int(* complete)(const char *buf, int len)
char * connect_request_buf
int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
void cs_print_session_info(COMSTACK cs)
static struct addrinfo * create_net_socket(COMSTACK h)
int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
static void connect_and_bind(COMSTACK p, const char *connect_host, const char *connect_auth, const char *bind_host)
struct addrinfo * tcpip_getaddrinfo(const char *str, const char *port, int *ipv6_only)
COMSTACK yaz_ssl_create(int s, int flags, int protocol, const char *connect_host, const char *connect_auth, const char *bind_host)
static void * tcpip_straddr(COMSTACK h, const char *str)
COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
COMSTACK yaz_tcpip_create2(int s, int flags, int protocol, const char *connect_host, const char *bind_host)
static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen, int(*check_ip)(void *cd, const char *a, int len, int type), void *cd)
static const char * tcpip_addrstr(COMSTACK h)
COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
static int tcpip_more(COMSTACK h)
static int tcpip_get(COMSTACK h, char **buf, int *bufsize)
#define CS_TCPIP_BUFCHUNK
static int cont_connect(COMSTACK h)
COMSTACK yaz_tcpip_create(int s, int flags, int protocol, const char *connect_host)
COMSTACK yaz_tcpip_create3(int s, int flags, int protocol, const char *connect_host, const char *connect_auth, const char *bind_host)
int cs_set_head_only(COMSTACK cs, int head_only)
static void parse_host_port(const char *host_port, char *tmp, size_t tmp_sz, const char **host, const char **port)
static int tcpip_put(COMSTACK h, char *buf, int size)
static struct tcpip_state * tcpip_state_create(void)
static void tcpip_close(COMSTACK h)
static int tcpip_init(void)
void * cs_get_ssl(COMSTACK cs)
static COMSTACK tcpip_accept(COMSTACK h)
static int tcpip_connect(COMSTACK h, void *address)
static int tcpip_bind(COMSTACK h, void *address, int mode)
static int tcpip_set_blocking(COMSTACK p, int blocking)
static int tcpip_rcvconnect(COMSTACK h)
Header for TCP/IP + SSL COMSTACK.
yaz_thread_t yaz_thread_create(void *(*start_routine)(void *p), void *arg)
create thread
void yaz_thread_join(yaz_thread_t *tp, void **value_ptr)
join thread
Implements thread creation wrappers.
struct yaz_thread * yaz_thread_t
Thread Identifier opaque pointer.
#define xstrdup(s)
utility macro which calls xstrdup_f
#define xfree(x)
utility macro which calls xfree_f
#define xrealloc(o, x)
utility macro which calls xrealloc_f
#define xmalloc(x)
utility macro which calls malloc_f
Header with fundamental macros.