1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #ifndef HAVE_AMALGAMATION
41 # ifdef HAVE_NONBLOCKSOCK
42 /*# include <sys/types.h>*/
43 # include <sys/select.h>
44 /*# include <sys/time.h>*/
45 # include <arpa/inet.h>
46 /*# include <netinet/in.h>*/
47 /*# include <errno.h>*/
48 /*# include <fcntl.h>*/
49 /*# include <stdlib.h>*/
50 /*# include <unistd.h>*/
53 #include <sys/socket.h>
57 #include <netinet/in.h>
59 #ifdef HAVE_ARPA_INET_H
60 # include <arpa/inet.h>
64 # include <openssl/err.h>
65 # include <openssl/rand.h>
66 # include <openssl/ssl.h>
67 # include <openssl/x509v3.h>
68 # include <openssl/x509.h>
72 static bool_t
a_socket_open(struct sock
*sp
, struct url
*urlp
);
75 static int a_socket_connect(int fd
, struct sockaddr
*soap
, size_t soapl
);
77 /* Write to socket fd, restarting on EINTR, unless anything is written */
78 static long a_socket_xwrite(int fd
, char const *data
, size_t sz
);
80 static sigjmp_buf __sopen_actjmp
; /* TODO someday, we won't need it no more */
81 static int __sopen_sig
; /* TODO someday, we won't need it no more */
83 __sopen_onsig(int sig
) /* TODO someday, we won't need it no more */
85 NYD_X
; /* Signal handler */
86 if (__sopen_sig
== -1) {
87 fprintf(n_stderr
, _("\nInterrupting this operation may turn "
88 "the DNS resolver unusable\n"));
92 siglongjmp(__sopen_actjmp
, 1);
97 a_socket_open(struct sock
*sp
, struct url
*urlp
) /* TODO sigstuff; refactor */
99 # ifdef HAVE_SO_XTIMEO
102 # ifdef HAVE_SO_LINGER
105 # ifdef HAVE_GETADDRINFO
107 # define NI_MAXHOST 1025
109 char hbuf
[NI_MAXHOST
];
110 struct addrinfo hints
, *res0
= NULL
, *res
;
112 struct sockaddr_in servaddr
;
113 struct in_addr
**pptr
;
117 sighandler_type
volatile ohup
, oint
;
118 char const * volatile serv
;
119 int volatile sofd
= -1, errval
;
124 serv
= (urlp
->url_port
!= NULL
) ? urlp
->url_port
: urlp
->url_proto
;
126 if (n_poption
& n_PO_D_V
)
127 n_err(_("Resolving host %s:%s ... "), urlp
->url_host
.s
, serv
);
129 /* Signal handling (in respect to __sopen_sig dealing) is heavy, but no
130 * healing until v15.0 and i want to end up with that functionality */
133 ohup
= safe_signal(SIGHUP
, &__sopen_onsig
);
134 oint
= safe_signal(SIGINT
, &__sopen_onsig
);
135 if (sigsetjmp(__sopen_actjmp
, 0)) {
138 (__sopen_sig
== SIGHUP
? _("Hangup") : _("Interrupted")));
147 # ifdef HAVE_GETADDRINFO
149 memset(&hints
, 0, sizeof hints
);
150 hints
.ai_socktype
= SOCK_STREAM
;
152 errval
= getaddrinfo(urlp
->url_host
.s
, serv
, &hints
, &res0
);
153 if (__sopen_sig
!= -1) {
154 __sopen_sig
= SIGINT
;
161 if (n_poption
& n_PO_D_V
)
162 n_err(_("failed\n"));
163 n_err(_("Lookup of %s:%s failed: %s\n"),
164 urlp
->url_host
.s
, serv
, gai_strerror(errval
));
166 /* Error seems to depend on how "smart" the /etc/service code is: is it
167 * "able" to state whether the service as such is NONAME or does it only
168 * check for the given ai_socktype.. */
169 if (errval
== EAI_NONAME
|| errval
== EAI_SERVICE
) {
170 if (serv
== urlp
->url_proto
&&
171 (serv
= n_servbyname(urlp
->url_proto
, NULL
)) != NULL
&&
173 n_err(_(" Trying standard protocol port %s\n"), serv
);
174 n_err(_(" If that succeeds consider including the "
175 "port in the URL!\n"));
178 if (serv
!= urlp
->url_port
)
179 n_err(_(" Including a port number in the URL may "
180 "circumvent this problem\n"));
186 if (n_poption
& n_PO_D_V
)
189 for (res
= res0
; res
!= NULL
&& sofd
< 0; res
= res
->ai_next
) {
190 if (n_poption
& n_PO_D_V
) {
191 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
, sizeof hbuf
,
192 NULL
, 0, NI_NUMERICHOST
))
193 memcpy(hbuf
, "unknown host", sizeof("unknown host"));
194 n_err(_("%sConnecting to %s:%s ... "),
195 (res
== res0
? n_empty
: "\n"), hbuf
, serv
);
198 sofd
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
200 (errval
= a_socket_connect(sofd
, res
->ai_addr
, res
->ai_addrlen
)
211 # else /* HAVE_GETADDRINFO */
212 if (serv
== urlp
->url_proto
) {
213 if ((ep
= getservbyname(n_UNCONST(serv
), "tcp")) != NULL
)
214 urlp
->url_portno
= ntohs(ep
->s_port
);
216 if (n_poption
& n_PO_D_V
)
217 n_err(_("failed\n"));
218 if ((serv
= n_servbyname(urlp
->url_proto
, &urlp
->url_portno
)) != NULL
)
219 n_err(_(" Unknown service: %s\n"), urlp
->url_proto
);
220 n_err(_(" Trying standard protocol port %s\n"), serv
);
221 n_err(_(" If that succeeds consider including the "
222 "port in the URL!\n"));
224 n_err(_(" Unknown service: %s\n"), urlp
->url_proto
);
225 n_err(_(" Including a port number in the URL may "
226 "circumvent this problem\n"));
227 assert(sofd
== -1 && errval
== 0);
234 hp
= gethostbyname(urlp
->url_host
.s
);
235 if (__sopen_sig
!= -1) {
236 __sopen_sig
= SIGINT
;
244 if (n_poption
& n_PO_D_V
)
245 n_err(_("failed\n"));
247 case HOST_NOT_FOUND
: emsg
= N_("host not found"); break;
249 case TRY_AGAIN
: emsg
= N_("(maybe) try again later"); break;
250 case NO_RECOVERY
: emsg
= N_("non-recoverable server error"); break;
251 case NO_DATA
: emsg
= N_("valid name without IP address"); break;
253 n_err(_("Lookup of %s:%s failed: %s\n"),
254 urlp
->url_host
.s
, serv
, V_(emsg
));
256 } else if (n_poption
& n_PO_D_V
)
259 pptr
= (struct in_addr
**)hp
->h_addr_list
;
260 if ((sofd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
261 n_perr(_("could not create socket"), 0);
262 assert(sofd
== -1 && errval
== 0);
266 memset(&servaddr
, 0, sizeof servaddr
);
267 servaddr
.sin_family
= AF_INET
;
268 servaddr
.sin_port
= htons(urlp
->url_portno
);
269 memcpy(&servaddr
.sin_addr
, *pptr
, sizeof(struct in_addr
));
270 if (n_poption
& n_PO_D_V
)
271 n_err(_("%sConnecting to %s:%d ... "),
272 n_empty
, inet_ntoa(**pptr
), (int)urlp
->url_portno
);
273 if((errval
= a_socket_connect(sofd
, (struct sockaddr
*)&servaddr
,
274 sizeof servaddr
)) != n_ERR_NONE
)
277 # endif /* !HAVE_GETADDRINFO */
280 safe_signal(SIGINT
, oint
);
281 safe_signal(SIGHUP
, ohup
);
286 n_perr(_("Could not connect"), errval
);
292 if (n_poption
& n_PO_D_V
)
293 n_err(_("connected.\n"));
295 /* And the regular timeouts XXX configurable */
296 # ifdef HAVE_SO_XTIMEO
299 (void)setsockopt(sofd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
300 (void)setsockopt(sofd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof tv
);
302 # ifdef HAVE_SO_LINGER
305 (void)setsockopt(sofd
, SOL_SOCKET
, SO_LINGER
, &li
, sizeof li
);
308 memset(sp
, 0, sizeof *sp
);
311 /* SSL/TLS upgrade? */
315 # if defined HAVE_GETADDRINFO && defined SSL_CTRL_SET_TLSEXT_HOSTNAME /* TODO
316 * TODO the SSL_ def check should NOT be here */
317 if(urlp
->url_flags
& n_URL_TLS_MASK
){
318 memset(&hints
, 0, sizeof hints
);
319 hints
.ai_family
= AF_UNSPEC
;
320 hints
.ai_flags
= AI_NUMERICHOST
;
322 if(getaddrinfo(urlp
->url_host
.s
, NULL
, &hints
, &res0
) == 0)
325 urlp
->url_flags
|= n_URL_HOST_IS_NAME
;
329 if (urlp
->url_flags
& n_URL_TLS_REQUIRED
) {
330 ohup
= safe_signal(SIGHUP
, &__sopen_onsig
);
331 oint
= safe_signal(SIGINT
, &__sopen_onsig
);
332 if (sigsetjmp(__sopen_actjmp
, 0)) {
333 n_err(_("%s during SSL/TLS handshake\n"),
334 (__sopen_sig
== SIGHUP
? _("Hangup") : _("Interrupted")));
339 if (ssl_open(urlp
, sp
) != OKAY
) {
346 safe_signal(SIGINT
, oint
);
347 safe_signal(SIGHUP
, ohup
);
351 # endif /* HAVE_SSL */
354 /* May need to bounce the signal to the go.c trampoline (or wherever) */
355 if (__sopen_sig
!= 0) {
358 sigaddset(&cset
, __sopen_sig
);
359 sigprocmask(SIG_UNBLOCK
, &cset
, NULL
);
360 n_raise(__sopen_sig
);
367 a_socket_connect(int fd
, struct sockaddr
*soap
, size_t soapl
){
371 #ifdef HAVE_NONBLOCKSOCK
372 rv
= fcntl(fd
, F_GETFL
, 0);
373 if(rv
!= -1 && !fcntl(fd
, F_SETFL
, rv
| O_NONBLOCK
)){
375 struct timeval tv
; /* XXX configurable */
379 if(connect(fd
, soap
, soapl
) && (i
= n_err_no
) != n_ERR_INPROGRESS
){
386 if(n_poption
& n_PO_D_V
){
395 if((soe
= select(fd
+ 1, NULL
, &fdset
, NULL
, &tv
)) == 1){
398 getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &rv
, &sol
);
399 fcntl(fd
, F_SETFL
, i
);
400 if(n_poption
& n_PO_D_V
)
403 if((n_poption
& n_PO_D_V
) && --i
> 0){
409 n_err(_(" timeout\n"));
415 #endif /* HAVE_NONBLOCKSOCK */
417 if(!connect(fd
, soap
, soapl
))
420 #ifdef HAVE_NONBLOCKSOCK
424 #ifdef HAVE_NONBLOCKSOCK
427 n_perr(_("connect(2) failed:"), rv
);
435 a_socket_xwrite(int fd
, char const *data
, size_t sz
)
442 if ((wo
= write(fd
, data
+ wt
, sz
- wt
)) < 0) {
443 if (n_err_no
== n_ERR_INTR
)
457 sclose(struct sock
*sp
)
464 /* TODO NOTE: we MUST NOT close the descriptor 0 here...
465 * TODO of course this should be handled in a VMAILFS->open() .s_fd=-1,
466 * TODO but unfortunately it isn't yet */
470 if (sp
->s_onclose
!= NULL
)
472 if (sp
->s_wbuf
!= NULL
)
476 void *s_ssl
= sp
->s_ssl
;
480 while (!SSL_shutdown(s_ssl
)) /* XXX proper error handling;signals! */
492 swrite(struct sock
*sp
, char const *data
)
497 rv
= swrite1(sp
, data
, strlen(data
), 0);
503 swrite1(struct sock
*sp
, char const *data
, int sz
, int use_buffer
)
509 if (use_buffer
> 0) {
512 if (sp
->s_wbuf
== NULL
) {
513 sp
->s_wbufsize
= 4096;
514 sp
->s_wbuf
= n_alloc(sp
->s_wbufsize
);
517 while (sp
->s_wbufpos
+ sz
> sp
->s_wbufsize
) {
518 di
= sp
->s_wbufsize
- sp
->s_wbufpos
;
520 if (sp
->s_wbufpos
> 0) {
521 memcpy(sp
->s_wbuf
+ sp
->s_wbufpos
, data
, di
);
522 rv
= swrite1(sp
, sp
->s_wbuf
, sp
->s_wbufsize
, -1);
524 rv
= swrite1(sp
, data
, sp
->s_wbufsize
, -1);
530 if (sz
== sp
->s_wbufsize
) {
531 rv
= swrite1(sp
, data
, sp
->s_wbufsize
, -1);
535 memcpy(sp
->s_wbuf
+ sp
->s_wbufpos
, data
, sz
);
540 } else if (use_buffer
== 0 && sp
->s_wbuf
!= NULL
&& sp
->s_wbufpos
> 0) {
543 if ((rv
= swrite1(sp
, sp
->s_wbuf
, x
, -1)) != OKAY
)
554 x
= SSL_write(sp
->s_ssl
, data
, sz
);
556 switch (SSL_get_error(sp
->s_ssl
, x
)) {
557 case SSL_ERROR_WANT_READ
:
558 case SSL_ERROR_WANT_WRITE
:
565 x
= a_socket_xwrite(sp
->s_fd
, data
, sz
);
570 snprintf(o
, sizeof o
, "%s write error",
571 (sp
->s_desc
? sp
->s_desc
: "socket"));
574 ssl_gen_err("%s", o
);
590 sopen(struct sock
*sp
, struct url
*urlp
){
597 /* We may have a proxy configured */
598 if((cp
= xok_vlook(socks_proxy
, urlp
, OXM_ALL
)) == NULL
)
599 rv
= a_socket_open(sp
, urlp
);
601 ui8_t pbuf
[4 + 1 + 255 + 2];
606 if(!url_parse(&url2
, CPROTO_SOCKS
, cp
)){
607 n_err(_("Failed to parse *socks-proxy*: %s\n"), cp
);
610 if(urlp
->url_host
.l
> 255){
611 n_err(_("*socks-proxy*: hostname too long: %s\n"),
616 if (n_poption
& n_PO_D_V
)
617 n_err(_("Connecting via *socks-proxy* to %s:%s ...\n"),
619 (urlp
->url_port
!= NULL
? urlp
->url_port
: urlp
->url_proto
));
621 if(!a_socket_open(sp
, &url2
)){
622 n_err(_("Failed to connect to *socks-proxy*: %s\n"), cp
);
626 /* RFC 1928: version identifier/method selection message */
627 pbuf
[0] = 0x05; /* VER: protocol version: X'05' */
628 pbuf
[1] = 0x01; /* NMETHODS: 1 */
629 pbuf
[2] = 0x00; /* METHOD: X'00' NO AUTHENTICATION REQUIRED */
630 if(write(sp
->s_fd
, pbuf
, 3) != 3){
632 n_perr("*socks-proxy*", 0);
638 /* Receive greeting */
639 if(read(sp
->s_fd
, pbuf
, 2) != 2)
641 if(pbuf
[0] != 0x05 || pbuf
[1] != 0x00){
643 emsg
= N_("unexpected reply\n");
645 /* I18N: error message and failing URL */
646 n_err(_("*socks-proxy*: %s: %s\n"), V_(emsg
), cp
);
650 /* RFC 1928: CONNECT request */
651 pbuf
[0] = 0x05; /* VER: protocol version: X'05' */
652 pbuf
[1] = 0x01; /* CMD: CONNECT X'01' */
653 pbuf
[2] = 0x00; /* RESERVED */
654 pbuf
[3] = 0x03; /* ATYP: domain name */
655 pbuf
[4] = (ui8_t
)urlp
->url_host
.l
;
656 memcpy(&pbuf
[i
= 5], urlp
->url_host
.s
, urlp
->url_host
.l
);
660 x
= htons(urlp
->url_portno
);
661 memcpy(&pbuf
[i
+= urlp
->url_host
.l
], (ui8_t
*)&x
, sizeof x
);
664 if(write(sp
->s_fd
, pbuf
, i
) != (ssize_t
)i
)
668 if((i
= read(sp
->s_fd
, pbuf
, 4)) != 4)
670 /* Version 5, reserved must be 0 */
671 if(pbuf
[0] != 0x05 || pbuf
[2] != 0x00)
675 case 0x00: emsg
= NULL
; break;
676 case 0x01: emsg
= N_("SOCKS server failure"); break;
677 case 0x02: emsg
= N_("connection not allowed by ruleset"); break;
678 case 0x03: emsg
= N_("network unreachable"); break;
679 case 0x04: emsg
= N_("host unreachable"); break;
680 case 0x05: emsg
= N_("connection refused"); break;
681 case 0x06: emsg
= N_("TTL expired"); break;
682 case 0x07: emsg
= N_("command not supported"); break;
683 case 0x08: emsg
= N_("address type not supported"); break;
684 default: emsg
= N_("unknown SOCKS error code"); break;
687 goto jesocksreplymsg
;
689 /* Address type variable; read the BND.PORT with it.
690 * This is actually false since RFC 1928 says that the BND.ADDR reply to
691 * CONNECT contains the IP address, so only 0x01 and 0x04 are allowed */
693 case 0x01: i
= 4; break;
694 case 0x03: i
= 1; break;
695 case 0x04: i
= 16; break;
696 default: goto jesocksreply
;
699 if(read(sp
->s_fd
, pbuf
, i
) != (ssize_t
)i
)
701 if(i
== 1 + sizeof(ui16_t
)){
703 if(read(sp
->s_fd
, pbuf
, i
) != (ssize_t
)i
)
714 (sgetline
)(char **line
, size_t *linesize
, size_t *linelen
, struct sock
*sp
733 if (lp_base
== NULL
|| PTRCMP(lp
, >, lp_base
+ lsize
- 128)) {
734 size_t diff
= PTR2SIZE(lp
- lp_base
);
735 *linesize
= (lsize
+= 256); /* XXX magic */
736 *line
= lp_base
= (n_realloc
)(lp_base
, lsize n_MEMORY_DEBUG_ARGSCALL
);
740 if (sp
->s_rbufptr
== NULL
||
741 PTRCMP(sp
->s_rbufptr
, >=, sp
->s_rbuf
+ sp
->s_rsz
)) {
745 sp
->s_rsz
= SSL_read(sp
->s_ssl
, sp
->s_rbuf
, sizeof sp
->s_rbuf
);
746 if (sp
->s_rsz
<= 0) {
750 switch(SSL_get_error(sp
->s_ssl
, sp
->s_rsz
)) {
751 case SSL_ERROR_WANT_READ
:
752 case SSL_ERROR_WANT_WRITE
:
755 snprintf(o
, sizeof o
, "%s",
756 (sp
->s_desc
? sp
->s_desc
: "socket"));
757 ssl_gen_err("%s", o
);
765 sp
->s_rsz
= read(sp
->s_fd
, sp
->s_rbuf
, sizeof sp
->s_rbuf
);
766 if (sp
->s_rsz
<= 0) {
770 if (n_err_no
== n_ERR_INTR
)
772 snprintf(o
, sizeof o
, "%s",
773 (sp
->s_desc
? sp
->s_desc
: "socket"));
779 sp
->s_rbufptr
= sp
->s_rbuf
;
781 } while ((*lp
++ = *sp
->s_rbufptr
++) != '\n');
783 lsize
= PTR2SIZE(lp
- lp_base
);
792 #endif /* HAVE_SOCKETS */