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);
803 if (ssl_check_error(h, sp, res))
818 struct addrinfo *ai = (
struct addrinfo *) address;
827 if (sp->pipefd[0] != -1)
829 ai = wait_resolver_thread(h);
838 tcpip_create_cred(h);
839 res = gnutls_certificate_set_x509_key_file(sp->cred_ptr->xcred,
842 GNUTLS_X509_FMT_PEM);
843 if (res != GNUTLS_E_SUCCESS)
845 yaz_log(
log_level,
"gnutls_certificate_set_x509_key_file r=%d fatal=%d msg=%s",
847 gnutls_error_is_fatal(res),
848 gnutls_strerror(res));
855 if (setsockopt(h->
iofile, SOL_SOCKET, SO_REUSEADDR, (
char*)
856 &one,
sizeof(one)) < 0)
862 r = bind(h->
iofile, ai->ai_addr, ai->ai_addrlen);
863 freeaddrinfo(sp->
ai);
882 int (*check_ip)(
void *cd,
const char *a,
int len,
int t),
888 struct sockaddr_in addr;
901 h->
newfd = accept(h->
iofile, (
struct sockaddr*)&addr, &len);
907 WSAGetLastError() == WSAEWOULDBLOCK
911#
if EAGAIN != EWOULDBLOCK
921 listen(h->
iofile, SOMAXCONN);
930 if (addrlen && (
size_t) (*addrlen) >=
sizeof(
struct sockaddr_in))
931 memcpy(raddr, &addr, *addrlen =
sizeof(
struct sockaddr_in));
934 if (check_ip && (*check_ip)(cd, (
const char *) &addr,
935 sizeof(addr), AF_INET))
939 closesocket(h->
newfd);
955 unsigned long tru = 1;
967 memcpy(cnew, h,
sizeof(*h));
978 closesocket(h->
newfd);
993 state->cred_ptr = st->cred_ptr;
998 (state->cred_ptr->ref)++;
999 gnutls_init(&state->session, GNUTLS_SERVER);
1000 if (!state->session)
1006 res = gnutls_set_default_priority(state->session);
1007 if (res != GNUTLS_E_SUCCESS)
1013 res = gnutls_credentials_set(state->session,
1014 GNUTLS_CRD_CERTIFICATE,
1015 st->cred_ptr->xcred);
1016 if (res != GNUTLS_E_SUCCESS)
1022 SET_GNUTLS_SOCKET(state->session, cnew->
iofile);
1033 int res = gnutls_handshake(state->session);
1036 if (ssl_check_error(h, state, res))
1059#define CS_TCPIP_BUFCHUNK 4096
1069 int tmpi, berlen, rest, req, tomove;
1070 int hasread = 0, res;
1086 while (!(berlen = (*sp->
complete)(*buf, hasread)))
1097 if (!(*buf =(
char *)
xrealloc(*buf, *bufsize *= 2)))
1105 res = gnutls_record_recv(sp->session, *buf + hasread,
1114 if (ssl_check_error(h, sp, res))
1134 if (WSAGetLastError() == WSAEWOULDBLOCK)
1147#
if EAGAIN != EWOULDBLOCK
1182 if (hasread > berlen)
1184 tomove = req = hasread - berlen;
1203 memcpy(sp->
altbuf, *buf + berlen, sp->
altlen = tomove);
1206 *(*buf + berlen) =
'\0';
1207 return berlen ? berlen : 1;
1229 else if (state->
towrite != size)
1239 res = gnutls_record_send(state->session,
buf + state->
written,
1243 if (ssl_check_error(h, state, res))
1263 WSAGetLastError() == WSAEWOULDBLOCK
1267#
if EAGAIN != EWOULDBLOCK
1307 if (sp->pipefd[0] != -1)
1310 close(sp->pipefd[0]);
1311 close(sp->pipefd[1]);
1318 if (sp->session && sp->use_bye)
1321 gnutls_bye(sp->session, GNUTLS_SHUT_WR);
1335 gnutls_deinit(sp->session);
1339 assert(sp->cred_ptr->ref > 0);
1341 if (--(sp->cred_ptr->ref) == 0)
1344 sp->cred_ptr->xcred);
1345 gnutls_certificate_free_credentials(sp->cred_ptr->xcred);
1346 xfree(sp->cred_ptr);
1352 freeaddrinfo(sp->
ai);
1366 struct sockaddr_storage addr;
1369 if (getpeername(h->
iofile, (
struct sockaddr *)&addr, &len) < 0)
1374 if (getnameinfo((
struct sockaddr *) &addr, len, host,
sizeof(host)-1,
1405 if (ioctlsocket(p->
iofile, FIONBIO, &flag) < 0)
1408 flag = fcntl(p->
iofile, F_GETFL, 0);
1410 flag = flag & ~O_NONBLOCK;
1413 flag = flag | O_NONBLOCK;
1414 signal(SIGPIPE, SIG_IGN);
1416 if (fcntl(p->
iofile, F_SETFL, flag) < 0)
1427#if GNUTLS_VERSION_NUMBER >= 0x020400
1428#define USE_GNUTLS_X509_CRT_PRINT 1
1430#define USE_GNUTLS_X509_CRT_PRINT 0
1434#if USE_GNUTLS_X509_CRT_PRINT
1436static const char *bin2hex(
const void *bin,
size_t bin_size)
1438 static char printable[110];
1439 const unsigned char *_bin = bin;
1445 for (i = 0; i < bin_size; i++)
1453static void x509_crt_print(gnutls_x509_crt_t cert)
1455 time_t expiration_time, activation_time;
1459 unsigned int algo, bits;
1461 expiration_time = gnutls_x509_crt_get_expiration_time(cert);
1462 activation_time = gnutls_x509_crt_get_activation_time(cert);
1464 printf(
"\tCertificate is valid since: %s", ctime(&activation_time));
1465 printf(
"\tCertificate expires: %s", ctime(&expiration_time));
1468 size =
sizeof(serial);
1469 gnutls_x509_crt_get_serial(cert, serial, &size);
1471 printf(
"\tCertificate serial number: %s\n", bin2hex(serial, size));
1475 algo = gnutls_x509_crt_get_pk_algorithm(cert, &bits);
1477 printf(
"Certificate public key: %s", gnutls_pk_algorithm_get_name(algo));
1480 printf(
"\tCertificate version: #%d\n", gnutls_x509_crt_get_version(cert));
1483 gnutls_x509_crt_get_dn(cert, dn, &size);
1484 printf(
"\tDN: %s\n", dn);
1487 gnutls_x509_crt_get_issuer_dn(cert, dn, &size);
1488 printf(
"\tIssuer's DN: %s\n", dn);
1499 const gnutls_datum_t *cert_list;
1500 unsigned i, cert_list_size;
1501 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1503 printf(
"X509 certificate\n");
1504 cert_list = gnutls_certificate_get_peers(sp->session,
1506 printf(
"Peer provided %u certificates\n", cert_list_size);
1507 for (i = 0; i < cert_list_size; i++)
1509 gnutls_x509_crt_t cert;
1510#if USE_GNUTLS_X509_CRT_PRINT
1512 gnutls_datum_t cinfo;
1514 gnutls_x509_crt_init(&cert);
1515 gnutls_x509_crt_import(cert, &cert_list[i], GNUTLS_X509_FMT_DER);
1516 printf(
"Certificate info %u:\n", i + 1);
1517#if USE_GNUTLS_X509_CRT_PRINT
1518 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL,
1522 printf(
"\t%s\n", cinfo.data);
1523 gnutls_free(cinfo.data);
1526 x509_crt_print(cert);
1528 gnutls_x509_crt_deinit(cert);
1559 strncpy(sp->cert_fname, fname,
sizeof(sp->cert_fname)-1);
1560 sp->cert_fname[
sizeof(sp->cert_fname)-1] =
'\0';
1571#if USE_GNUTLS_X509_CRT_PRINT
1575 const gnutls_datum_t *cert_list;
1576 unsigned cert_list_size;
1577 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1579 cert_list = gnutls_certificate_get_peers(sp->session, &cert_list_size);
1580 if (cert_list_size > 0)
1582 gnutls_x509_crt_t cert;
1584 gnutls_datum_t cinfo;
1586 gnutls_x509_crt_init(&cert);
1587 gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
1589 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL, &cinfo);
1593 *len = strlen(*
buf);
1594 gnutls_free(cinfo.data);
1595 gnutls_x509_crt_deinit(cert);
1598 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.