2 * socket.c -- socket library functions
4 * Copyright 1998 by Eric S. Raymond.
5 * For license terms, see the file COPYING in this directory.
12 #include <ctype.h> /* isspace() */
15 #endif /* HAVE_MEMORY_H */
16 #include <sys/types.h>
18 #ifndef HAVE_NET_SOCKET_H
19 #include <sys/socket.h>
21 #include <net/socket.h>
24 #include <netinet/in.h>
25 #ifdef HAVE_ARPA_INET_H
26 #include <arpa/inet.h>
29 #if defined(STDC_HEADERS)
32 #if defined(HAVE_UNISTD_H)
35 #if defined(HAVE_STDARG_H)
41 #include "fetchmail.h"
44 /* Defines to allow BeOS and Cygwin to play nice... */
47 #define fm_close(a) closesocket(a)
48 #define fm_write(a,b,c) send(a,b,c,0)
49 #define fm_peek(a,b,c) recv(a,b,c,0)
50 #define fm_read(a,b,c) recv(a,b,c,0)
52 #define fm_close(a) close(a)
53 #define fm_write(a,b,c) write(a,b,c)
54 #define fm_peek(a,b,c) recv(a,b,c, MSG_PEEK)
56 #define fm_read(a,b,c) cygwin_read(a,b,c)
57 static ssize_t cygwin_read(int sock, void *buf, size_t count);
58 #else /* ! __CYGWIN__ */
59 #define fm_read(a,b,c) read(a,b,c)
60 #endif /* __CYGWIN__ */
63 /* We need to define h_errno only if it is not already */
66 #ifdef HAVE_RES_SEARCH
67 /* some versions of FreeBSD should declare this but don't */
70 /* pretend we have h_errno to avoid some #ifdef's later */
74 #endif /* ndef h_errno */
76 extern int mailserver_socket_temp; /* Socket to close if connect timeout */
79 #include <net/security.h>
80 #endif /* NET_SECURITY */
82 #ifdef HAVE_SOCKETPAIR
83 char *const *parse_plugin(const char *plugin, const char *host, const char *service)
84 { const char **argvec;
86 char *cp, *plugin_copy;
87 unsigned int plugin_copy_len;
88 unsigned int plugin_offset = 0, plugin_copy_offset = 0;
89 unsigned int i, s = 2 * sizeof(char*), host_count = 0, service_count = 0;
90 unsigned int plugin_len = strlen(plugin);
91 unsigned int host_len = strlen(host);
92 unsigned int service_len = strlen(service);
94 for (c = p = plugin; *c; c++)
95 { if (isspace(*c) && !isspace(*p))
97 if (*p == '%' && *c == 'h')
99 if (*p == '%' && *c == 'p')
104 plugin_copy_len = plugin_len + host_len * host_count + service_len * service_count;
105 plugin_copy = malloc(plugin_copy_len + 1);
108 report(stderr, GT_("fetchmail: malloc failed\n"));
112 while (plugin_copy_offset < plugin_copy_len)
113 { if ((plugin[plugin_offset] == '%') && (plugin[plugin_offset + 1] == 'h'))
114 { strcpy(plugin_copy + plugin_copy_offset, host);
116 plugin_copy_offset += host_len;
118 else if ((plugin[plugin_offset] == '%') && (plugin[plugin_offset + 1] == 'p'))
119 { strcpy(plugin_copy + plugin_copy_offset, service);
121 plugin_copy_offset += service_len;
124 { plugin_copy[plugin_copy_offset] = plugin[plugin_offset];
126 plugin_copy_offset++;
129 plugin_copy[plugin_copy_len] = 0;
134 report(stderr, GT_("fetchmail: malloc failed\n"));
137 memset(argvec, 0, s);
138 for (c = p = plugin_copy, i = 0; *c; c++)
139 { if ((!isspace(*c)) && (c == p ? 1 : isspace(*p))) {
145 for (cp = plugin_copy; *cp; cp++)
149 return (char *const*)argvec;
152 static int handle_plugin(const char *host,
153 const char *service, const char *plugin)
154 /* get a socket mediated through a given external command */
160 * The author of this code, Felix von Leitner <felix@convergence.de>, says:
161 * he chose socketpair() instead of pipe() because socketpair creates
162 * bidirectional sockets while allegedly some pipe() implementations don't.
164 if (socketpair(AF_UNIX,SOCK_STREAM,0,fds))
166 report(stderr, GT_("fetchmail: socketpair failed\n"));
172 report(stderr, GT_("fetchmail: fork failed\n"));
176 /* fds[1] is the parent's end; close it for proper EOF
178 (void) close(fds[1]);
179 if ( (dup2(fds[0],0) == -1) || (dup2(fds[0],1) == -1) ) {
180 report(stderr, GT_("dup2 failed\n"));
183 /* fds[0] is now connected to 0 and 1; close it */
184 (void) close(fds[0]);
185 if (outlevel >= O_VERBOSE)
186 report(stderr, GT_("running %s (host %s service %s)\n"), plugin, host, service);
187 argvec = parse_plugin(plugin,host,service);
188 execvp(*argvec, argvec);
189 report(stderr, GT_("execvp(%s) failed\n"), *argvec);
192 default: /* parent */
196 /* fds[0] is the child's end; close it for proper EOF detection */
197 (void) close(fds[0]);
200 #endif /* HAVE_SOCKETPAIR */
203 #include <sys/time.h>
205 int SockCheckOpen(int fd)
206 /* poll given socket; is it selectable? */
214 FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
217 tv.tv_sec = 0; tv.tv_usec = 0;
218 rt = select(fd+1, &r, &w, &e, &tv);
219 if (rt == -1 && (errno != EAGAIN && errno != EINTR))
225 #endif /* __UNUSED__ */
227 int UnixOpen(const char *path)
230 struct sockaddr_un ad;
231 memset(&ad, 0, sizeof(ad));
232 ad.sun_family = AF_UNIX;
233 strncpy(ad.sun_path, path, sizeof(ad.sun_path)-1);
235 sock = socket( AF_UNIX, SOCK_STREAM, 0 );
242 /* Socket opened saved. Usefull if connect timeout
243 * because it can be closed.
245 mailserver_socket_temp = sock;
247 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
250 fm_close(sock); /* don't use SockClose, no traffic yet */
256 /* No connect timeout, then no need to set mailserver_socket_temp */
257 mailserver_socket_temp = -1;
263 int SockOpen(const char *host, const char *service, const char *options,
266 struct addrinfo *ai, *ai0, req;
269 void *request = NULL;
271 #endif /* NET_SECURITY */
273 #ifdef HAVE_SOCKETPAIR
275 return handle_plugin(host,service,plugin);
276 #endif /* HAVE_SOCKETPAIR */
277 memset(&req, 0, sizeof(struct addrinfo));
278 req.ai_socktype = SOCK_STREAM;
280 if (getaddrinfo(host, service, &req, &ai0)) {
281 report(stderr, GT_("fetchmail: getaddrinfo(%s.%s)\n"), host,service);
289 if (net_security_strtorequest((char *)options, &request, &requestlen))
292 i = inner_connect(ai0, request, requestlen, NULL, NULL, "fetchmail", NULL);
297 #else /* NET_SECURITY */
298 #ifdef HAVE_INNER_CONNECT
299 i = inner_connect(ai0, NULL, 0, NULL, NULL, "fetchmail", NULL);
305 for (ai = ai0; ai; ai = ai->ai_next) {
306 i = socket(ai->ai_family, ai->ai_socktype, 0);
310 /* Socket opened saved. Usefull if connect timeout
311 * because it can be closed.
313 mailserver_socket_temp = i;
315 if (connect(i, (struct sockaddr *) ai->ai_addr, ai->ai_addrlen) < 0) {
321 /* No connect timeout, then no need to set mailserver_socket_temp */
322 mailserver_socket_temp = -1;
328 #endif /* NET_SECURITY */
334 #else /* INET6_ENABLE */
335 #ifndef HAVE_INET_ATON
337 #ifdef INADDR_BROADCAST
338 #define INADDR_NONE INADDR_BROADCAST
340 #define INADDR_NONE -1
343 #endif /* HAVE_INET_ATON */
345 int SockOpen(const char *host, int clientPort, const char *options,
348 int sock = -1; /* pacify -Wall */
349 #ifndef HAVE_INET_ATON
350 unsigned long inaddr;
351 #endif /* HAVE_INET_ATON */
352 struct sockaddr_in ad, **pptr;
355 #ifdef HAVE_SOCKETPAIR
359 snprintf(buf, sizeof(buf), /* Yeah, paranoic. So what? :P */
362 #endif /* HAVE_SNPRINTF */
364 return handle_plugin(host,buf,plugin);
366 #endif /* HAVE_SOCKETPAIR */
368 memset(&ad, 0, sizeof(ad));
369 ad.sin_family = AF_INET;
371 /* we'll accept a quad address */
372 #ifndef HAVE_INET_ATON
373 inaddr = inet_addr((char*)host);
374 if (inaddr != INADDR_NONE)
376 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
378 if (inet_aton(host, &ad.sin_addr))
380 #endif /* HAVE_INET_ATON */
381 ad.sin_port = htons(clientPort);
383 sock = socket(AF_INET, SOCK_STREAM, 0);
390 /* Socket opened saved. Usefull if connect timeout because
393 mailserver_socket_temp = sock;
395 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
398 fm_close(sock); /* don't use SockClose, no traffic yet */
404 /* No connect timeout, then no need to set mailserver_socket_temp */
405 mailserver_socket_temp = -1;
407 #ifndef HAVE_INET_ATON
411 #endif /* HAVE_INET_ATON */
413 hp = gethostbyname((char*)host);
421 * Add a check to make sure the address has a valid IPv4 or IPv6
422 * length. This prevents buffer spamming by a broken DNS.
424 if(hp->h_length != 4 && hp->h_length != 8)
428 GT_("fetchmail: illegal address length received for host %s\n"),host);
432 * Try all addresses of a possibly multihomed host until we get
433 * a successful connect or until we run out of addresses.
435 pptr = (struct sockaddr_in **)hp->h_addr_list;
436 for(; *pptr != NULL; pptr++)
438 sock = socket(AF_INET, SOCK_STREAM, 0);
445 /* Socket opened saved. Usefull if connect timeout because
448 mailserver_socket_temp = sock;
450 ad.sin_port = htons(clientPort);
451 memcpy(&ad.sin_addr, *pptr, sizeof(struct in_addr));
452 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) == 0) {
453 /* No connect timeout, then no need to set mailserver_socket_temp */
454 mailserver_socket_temp = -1;
457 fm_close(sock); /* don't use SockClose, no traffic yet */
458 memset(&ad, 0, sizeof(ad));
459 ad.sin_family = AF_INET;
464 fm_close(sock); /* don't use SockClose, no traffic yet */
473 #endif /* INET6_ENABLE */
476 #if defined(HAVE_STDARG_H)
477 int SockPrintf(int sock, const char* format, ...)
480 int SockPrintf(sock,format,va_alist)
489 #if defined(HAVE_STDARG_H)
490 va_start(ap, format) ;
494 #ifdef HAVE_VSNPRINTF
495 vsnprintf(buf, sizeof(buf), format, ap);
497 vsprintf(buf, format, ap);
500 return SockWrite(sock, buf, strlen(buf));
505 #include "openssl/ssl.h"
506 #include "openssl/err.h"
507 #include "openssl/pem.h"
508 #include "openssl/x509.h"
510 static SSL_CTX *_ctx = NULL;
511 static SSL *_ssl_context[FD_SETSIZE];
513 SSL *SSLGetContext( int );
514 #endif /* SSL_ENABLE */
516 int SockWrite(int sock, char *buf, int len)
526 if( NULL != ( ssl = SSLGetContext( sock ) ) )
527 n = SSL_write(ssl, buf, len);
529 #endif /* SSL_ENABLE */
530 n = fm_write(sock, buf, len);
540 int SockRead(int sock, char *buf, int len)
542 char *newline, *bp = buf;
544 int maxavailable = 0;
561 * The reason for these gymnastics is that we want two things:
562 * (1) to read \n-terminated lines,
563 * (2) to return the true length of data read, even if the
564 * data coming in has embedded NULS.
567 if( NULL != ( ssl = SSLGetContext( sock ) ) ) {
569 /* OK... SSL_peek works a little different from MSG_PEEK
570 Problem is that SSL_peek can return 0 if there
571 is no data currently available. If, on the other
572 hand, we loose the socket, we also get a zero, but
573 the SSL_read then SEGFAULTS! To deal with this,
574 we'll check the error code any time we get a return
575 of zero from SSL_peek. If we have an error, we bail.
576 If we don't, we read one character in SSL_read and
577 loop. This should continue to work even if they
578 later change the behavior of SSL_peek
579 to "fix" this problem... :-( */
580 if ((n = SSL_peek(ssl, bp, len)) < 0) {
581 (void)SSL_get_error(ssl, n);
586 /* SSL_peek says no data... Does he mean no data
587 or did the connection blow up? If we got an error
589 if( 0 != ( n = SSL_get_error(ssl, n) ) ) {
592 /* We didn't get an error so read at least one
593 character at this point and loop */
595 /* Make sure newline start out NULL!
596 * We don't have a string to pass through
597 * the strchr at this point yet */
599 } else if ((newline = memchr(bp, '\n', n)) != NULL)
600 n = newline - bp + 1;
601 /* Matthias Andree: SSL_read can return 0, in that case
602 * we must cal SSL_get_error to figure if there was
603 * an error or just a "no data" condition */
604 if ((n = SSL_read(ssl, bp, n)) <= 0) {
605 if ((n = SSL_get_error(ssl, n))) {
609 /* Check for case where our single character turned out to
610 * be a newline... (It wasn't going to get caught by
611 * the strchr above if it came from the hack... ). */
612 if( NULL == newline && 1 == n && '\n' == *bp ) {
613 /* Got our newline - this will break
614 out of the loop now */
619 #endif /* SSL_ENABLE */
623 if ((n = fm_read(sock, bp, 1)) <= 0)
625 if ((n = fm_peek(sock, bp, len)) <= 0)
629 if ((newline = memchr(bp, '\n', n)) != NULL)
630 n = newline - bp + 1;
632 if ((n = fm_read(sock, bp, n)) == -1)
634 #endif /* __BEOS__ */
642 #ifdef FORCE_STUFFING /* too ugly to live -- besides, there's IMAP */
643 /* OK, very weird hack coming up here:
644 * When POP3 servers send us a message, they're supposed to
645 * terminate the message with a line containing only a dot. To protect
646 * against lines in the real message that might contain only a dot,
647 * they're supposed to preface any line that starts with a dot with
648 * an additional dot, which will be removed on the client side. That
649 * process, called byte-stuffing (and unstuffing) is really not the
650 * concern of this low-level routine, ordinarily, but there are some
651 * POP servers (and maybe IMAP servers too, who knows) that fail to
652 * do the byte-stuffing, and this routine is the best place to try to
653 * identify and fix that fault.
655 * Since the DOT line is supposed to come only at the end of a
656 * message, the implication is that right after we see it, the server
657 * is supposed to go back to waiting for the next command. There
658 * isn't supposed to be any more data to read after we see the dot.
659 * THEREFORE, if we see more data to be read after something that
660 * looks like the dot line, then probably the server is failing to
661 * do byte-stuffing. In that case, we'll byte-pack it for them so
662 * that the higher-level routines see things as hunky-dorey.
663 * This is not a perfect test or fix by any means (it has an
664 * obvious race condition, for one thing), but it should at least
665 * reduce the nastiness that ensues when people don't know how
666 * to write POP servers.
668 if ((maxavailable > (bp-buf)) &&
675 (buf[1] == '\n')))) {
677 memmove(buf+1, buf, (bp-buf)+1);
681 #endif /* FORCE_STUFFING */
685 int SockPeek(int sock)
686 /* peek at the next socket character without actually reading it */
695 if( NULL != ( ssl = SSLGetContext( sock ) ) ) {
696 n = SSL_peek(ssl, &ch, 1);
698 (void)SSL_get_error(ssl, n);
702 /* This code really needs to implement a "hold back"
703 * to simulate a functioning SSL_peek()... sigh...
704 * Has to be coordinated with the read code above.
705 * Next on the list todo... */
707 /* SSL_peek says 0... Does that mean no data
708 or did the connection blow up? If we got an error
710 if( 0 != ( n = SSL_get_error(ssl, n) ) ) {
714 /* Haven't seen this case actually occur, but...
715 if the problem in SockRead can occur, this should
716 be possible... Just not sure what to do here.
717 This should be a safe "punt" the "peek" but don't
718 "punt" the "session"... */
720 return 0; /* Give him a '\0' character */
724 #endif /* SSL_ENABLE */
725 n = fm_peek(sock, &ch, 1);
737 static char *_ssl_server_cname = NULL;
738 static int _check_fp;
739 static char *_check_digest;
740 static char *_server_label;
741 static int _depth0ck;
743 SSL *SSLGetContext( int sock )
745 /* If SSLOpen has never initialized - just return NULL */
749 if( sock < 0 || sock > FD_SETSIZE )
751 return _ssl_context[sock];
755 int SSL_verify_callback( int ok_return, X509_STORE_CTX *ctx, int strict )
760 unsigned char digest[EVP_MAX_MD_SIZE];
761 char text[EVP_MAX_MD_SIZE * 3 + 1], *tp, *te;
763 unsigned int dsz, i, esz;
764 X509_NAME *subj, *issuer;
766 x509_cert = X509_STORE_CTX_get_current_cert(ctx);
767 err = X509_STORE_CTX_get_error(ctx);
768 depth = X509_STORE_CTX_get_error_depth(ctx);
770 subj = X509_get_subject_name(x509_cert);
771 issuer = X509_get_issuer_name(x509_cert);
776 if (outlevel == O_VERBOSE) {
777 if ((i = X509_NAME_get_text_by_NID(issuer, NID_organizationName, buf, sizeof(buf))) != -1) {
778 report(stdout, GT_("Issuer Organization: %s\n"), buf);
779 if (i >= sizeof(buf) - 1)
780 report(stdout, GT_("Warning: Issuer Organization Name too long (possibly truncated).\n"));
782 report(stdout, GT_("Unknown Organization\n"));
783 if ((i = X509_NAME_get_text_by_NID(issuer, NID_commonName, buf, sizeof(buf))) != -1) {
784 report(stdout, GT_("Issuer CommonName: %s\n"), buf);
785 if (i >= sizeof(buf) - 1)
786 report(stdout, GT_("Warning: Issuer CommonName too long (possibly truncated).\n"));
788 report(stdout, GT_("Unknown Issuer CommonName\n"));
790 if ((i = X509_NAME_get_text_by_NID(subj, NID_commonName, buf, sizeof(buf))) != -1) {
791 if (outlevel == O_VERBOSE)
792 report(stdout, GT_("Server CommonName: %s\n"), buf);
793 if (i >= sizeof(buf) - 1) {
794 /* Possible truncation. In this case, this is a DNS name, so this
795 * is really bad. We do not tolerate this even in the non-strict case. */
796 report(stderr, GT_("Bad certificate: Subject CommonName too long!\n"));
799 if (_ssl_server_cname != NULL) {
801 char *p2 = _ssl_server_cname;
806 n = strlen(p2) - strlen(p1);
810 if (0 != strcasecmp(p1, p2)) {
812 GT_("Server CommonName mismatch: %s != %s\n"),
813 buf, _ssl_server_cname );
814 if (ok_return && strict)
817 } else if (ok_return && strict) {
818 report(stderr, GT_("Server name not set, could not verify certificate!\n"));
822 if (outlevel == O_VERBOSE)
823 report(stdout, GT_("Unknown Server CommonName\n"));
824 if (ok_return && strict) {
825 report(stderr, GT_("Server name not specified in certificate!\n"));
829 /* Print the finger print. Note that on errors, we might print it more than once
830 * normally; we kluge around that by using a global variable. */
833 digest_tp = EVP_md5();
834 if (digest_tp == NULL) {
835 report(stderr, GT_("EVP_md5() failed!\n"));
838 if (!X509_digest(x509_cert, digest_tp, digest, &dsz)) {
839 report(stderr, GT_("Out of memory!\n"));
843 te = text + sizeof(text);
844 for (i = 0; i < dsz; i++) {
846 esz = snprintf(tp, te - tp, i > 0 ? ":%02X" : "%02X", digest[i]);
848 esz = sprintf(tp, i > 0 ? ":%02X" : "%02X", digest[i]);
850 if (esz >= te - tp) {
851 report(stderr, GT_("Digest text buffer too small!\n"));
856 if (outlevel > O_NORMAL)
857 report(stdout, GT_("%s key fingerprint: %s\n"), _server_label, text);
858 if (_check_digest != NULL) {
859 if (strcmp(text, _check_digest) == 0) {
860 if (outlevel > O_NORMAL)
861 report(stdout, GT_("%s fingerprints match.\n"), _server_label);
863 if (outlevel > O_SILENT)
864 report(stderr, GT_("%s fingerprints do not match!\n"), _server_label);
871 if (err != X509_V_OK && (strict || outlevel == O_VERBOSE)) {
872 report(strict ? stderr : stdout, GT_("Warning: server certificate verification: %s\n"), X509_verify_cert_error_string(err));
873 /* We gave the error code, but maybe we can add some more details for debugging */
875 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
876 X509_NAME_oneline(issuer, buf, sizeof(buf));
877 buf[sizeof(buf) - 1] = '\0';
878 report(stdout, GT_("unknown issuer (first %d characters): %s\n"), sizeof(buf), buf);
887 int SSL_nock_verify_callback( int ok_return, X509_STORE_CTX *ctx )
889 return SSL_verify_callback(ok_return, ctx, 0);
892 int SSL_ck_verify_callback( int ok_return, X509_STORE_CTX *ctx )
894 return SSL_verify_callback(ok_return, ctx, 1);
897 /* performs initial SSL handshake over the connected socket
898 * uses SSL *ssl global variable, which is currently defined
901 int SSLOpen(int sock, char *mycert, char *mykey, char *myproto, int certck, char *certpath,
902 char *fingerprint, char *servercname, char *label)
905 struct stat randstat;
908 SSL_load_error_strings();
909 SSLeay_add_ssl_algorithms();
912 if (stat("/dev/random", &randstat) &&
913 stat("/dev/urandom", &randstat)) {
914 /* Neither /dev/random nor /dev/urandom are present, so add
915 entropy to the SSL PRNG a hard way. */
916 for (i = 0; i < 10000 && ! RAND_status (); ++i) {
919 gettimeofday (&tv, 0);
920 buf[0] = tv.tv_usec & 0xF;
921 buf[2] = (tv.tv_usec & 0xF0) >> 4;
922 buf[3] = (tv.tv_usec & 0xF00) >> 8;
923 buf[1] = (tv.tv_usec & 0xF000) >> 12;
924 RAND_add (buf, sizeof buf, 0.1);
927 #endif /* SSL_ENABLE */
930 if( sock < 0 || sock > FD_SETSIZE ) {
931 report(stderr, GT_("File descriptor out of range for SSL") );
936 /* Be picky and make sure the memory is cleared */
937 memset( _ssl_context, 0, sizeof( _ssl_context ) );
939 if(!strcmp("ssl2",myproto)) {
940 _ctx = SSL_CTX_new(SSLv2_client_method());
941 } else if(!strcmp("ssl3",myproto)) {
942 _ctx = SSL_CTX_new(SSLv3_client_method());
943 } else if(!strcmp("tls1",myproto)) {
944 _ctx = SSL_CTX_new(TLSv1_client_method());
945 } else if (!strcmp("ssl23",myproto)) {
948 fprintf(stderr,GT_("Invalid SSL protocol '%s' specified, using default (SSLv23).\n"), myproto);
953 _ctx = SSL_CTX_new(SSLv23_client_method());
956 ERR_print_errors_fp(stderr);
962 SSL_CTX_set_verify(_ctx, SSL_VERIFY_PEER, SSL_ck_verify_callback);
964 SSL_CTX_load_verify_locations(_ctx, NULL, certpath);
966 /* In this case, we do not fail if verification fails. However,
967 * we provide the callback for output and possible fingerprint checks. */
968 SSL_CTX_set_verify(_ctx, SSL_VERIFY_PEER, SSL_nock_verify_callback);
971 _ssl_context[sock] = SSL_new(_ctx);
973 if(_ssl_context[sock] == NULL) {
974 ERR_print_errors_fp(stderr);
978 /* This static is for the verify callback */
979 _ssl_server_cname = servercname;
980 _server_label = label;
982 _check_digest = fingerprint;
985 if( mycert || mykey ) {
987 /* Ok... He has a certificate file defined, so lets declare it. If
988 * he does NOT have a separate certificate and private key file then
989 * assume that it's a combined key and certificate file.
995 SSL_use_certificate_file(_ssl_context[sock], mycert, SSL_FILETYPE_PEM);
996 SSL_use_RSAPrivateKey_file(_ssl_context[sock], mykey, SSL_FILETYPE_PEM);
999 SSL_set_fd(_ssl_context[sock], sock);
1001 if(SSL_connect(_ssl_context[sock]) < 1) {
1002 ERR_print_errors_fp(stderr);
1006 /* Paranoia: was the callback not called as we expected? */
1007 if ((fingerprint != NULL || certck) && !_depth0ck) {
1008 report(stderr, GT_("Certificate/fingerprint verification was somehow skipped!\n"));
1010 if( NULL != ( ssl = SSLGetContext( sock ) ) ) {
1011 /* Clean up the SSL stack */
1012 SSL_free( _ssl_context[sock] );
1013 _ssl_context[sock] = NULL;
1022 int SockClose(int sock)
1023 /* close a socket gracefully */
1028 if( NULL != ( ssl = SSLGetContext( sock ) ) ) {
1029 /* Clean up the SSL stack */
1030 SSL_free( _ssl_context[sock] );
1031 _ssl_context[sock] = NULL;
1037 * This hangs in RedHat 6.2 after fetchmail runs for a while a
1038 * FIN_WAIT2 comes up in netstat and fetchmail never returns from
1039 * the recv system call. (Reported from jtnews
1040 * <jtnews@bellatlantic.net>, Wed, 24 May 2000 21:26:02.)
1042 * Half-close the connection first so the other end gets notified.
1044 * This stops sends but allows receives (effectively, it sends a
1046 if (shutdown(sock, 1) == 0) {
1048 /* If there is any data still waiting in the queue, discard it.
1049 * Call recv() until either it returns 0 (meaning we received a FIN)
1050 * or any error occurs. This makes sure all data sent by the other
1051 * side is acknowledged at the TCP level.
1053 if (fm_peek(sock, &ch, 1) > 0)
1054 while (fm_read(sock, &ch, 1) > 0)
1057 #endif /* __UNUSED__ */
1059 /* if there's an error closing at this point, not much we can do */
1060 return(fm_close(sock)); /* this is guarded */
1065 * Workaround Microsoft Winsock recv/WSARecv(..., MSG_PEEK) bug.
1066 * See http://sources.redhat.com/ml/cygwin/2001-08/msg00628.html
1069 static ssize_t cygwin_read(int sock, void *buf, size_t count)
1074 if ((n = read(sock, bp, count)) == -1)
1079 if (outlevel >= O_VERBOSE)
1080 report(stdout, GT_("Cygwin socket read retry\n"));
1081 n2 = read(sock, bp + n, count - n);
1082 if (n2 == -1 || n + n2 != count) {
1083 report(stderr, GT_("Cygwin socket read retry failed!\n"));
1090 #endif /* __CYGWIN__ */
1094 * Use the chargen service to test input buffering directly.
1095 * You may have to uncomment the `chargen' service description in your
1096 * inetd.conf (and then SIGHUP inetd) for this to work. */
1099 int sock = SockOpen("localhost", 19, NULL);
1102 while (SockRead(sock, buf, sizeof(buf)-1))
1103 SockWrite(1, buf, strlen(buf));
1108 /* socket.c ends here */