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 - 2017 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 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #ifdef HAVE_ARPA_INET_H
48 # include <arpa/inet.h>
52 # include <openssl/err.h>
53 # include <openssl/rand.h>
54 # include <openssl/ssl.h>
55 # include <openssl/x509v3.h>
56 # include <openssl/x509.h>
60 static bool_t
a_socket_open(struct sock
*sp
, struct url
*urlp
);
62 /* Write to socket fd, restarting on EINTR, unless anything is written */
63 static long a_socket_xwrite(int fd
, char const *data
, size_t sz
);
65 static sigjmp_buf __sopen_actjmp
; /* TODO someday, we won't need it no more */
66 static int __sopen_sig
; /* TODO someday, we won't need it no more */
68 __sopen_onsig(int sig
) /* TODO someday, we won't need it no more */
70 NYD_X
; /* Signal handler */
71 if (__sopen_sig
== -1) {
72 fprintf(n_stderr
, _("\nInterrupting this operation may turn "
73 "the DNS resolver unusable\n"));
77 siglongjmp(__sopen_actjmp
, 1);
82 a_socket_open(struct sock
*sp
, struct url
*urlp
) /* TODO sigstuff; refactor */
84 # ifdef HAVE_SO_SNDTIMEO
87 # ifdef HAVE_SO_LINGER
90 # ifdef HAVE_GETADDRINFO
92 # define NI_MAXHOST 1025
94 char hbuf
[NI_MAXHOST
];
95 struct addrinfo hints
, *res0
= NULL
, *res
;
97 struct sockaddr_in servaddr
;
98 struct in_addr
**pptr
;
102 sighandler_type
volatile ohup
, oint
;
103 char const * volatile serv
;
104 int volatile sofd
= -1, errval
;
109 /* Connect timeouts after 30 seconds XXX configurable */
110 # ifdef HAVE_SO_SNDTIMEO
114 serv
= (urlp
->url_port
!= NULL
) ? urlp
->url_port
: urlp
->url_proto
;
116 if (n_poption
& n_PO_D_V
)
117 n_err(_("Resolving host %s:%s ... "), urlp
->url_host
.s
, serv
);
119 /* Signal handling (in respect to __sopen_sig dealing) is heavy, but no
120 * healing until v15.0 and i want to end up with that functionality */
123 ohup
= safe_signal(SIGHUP
, &__sopen_onsig
);
124 oint
= safe_signal(SIGINT
, &__sopen_onsig
);
125 if (sigsetjmp(__sopen_actjmp
, 0)) {
128 (__sopen_sig
== SIGHUP
? _("Hangup") : _("Interrupted")));
137 # ifdef HAVE_GETADDRINFO
139 memset(&hints
, 0, sizeof hints
);
140 hints
.ai_socktype
= SOCK_STREAM
;
142 errval
= getaddrinfo(urlp
->url_host
.s
, serv
, &hints
, &res0
);
143 if (__sopen_sig
!= -1) {
144 __sopen_sig
= SIGINT
;
151 if (n_poption
& n_PO_D_V
)
152 n_err(_("failed\n"));
153 n_err(_("Lookup of %s:%s failed: %s\n"),
154 urlp
->url_host
.s
, serv
, gai_strerror(errval
));
156 /* Error seems to depend on how "smart" the /etc/service code is: is it
157 * "able" to state whether the service as such is NONAME or does it only
158 * check for the given ai_socktype.. */
159 if (errval
== EAI_NONAME
|| errval
== EAI_SERVICE
) {
160 if (serv
== urlp
->url_proto
&&
161 (serv
= n_servbyname(urlp
->url_proto
, NULL
)) != NULL
&&
163 n_err(_(" Trying standard protocol port %s\n"), serv
);
164 n_err(_(" If that succeeds consider including the "
165 "port in the URL!\n"));
168 if (serv
!= urlp
->url_port
)
169 n_err(_(" Including a port number in the URL may "
170 "circumvent this problem\n"));
176 if (n_poption
& n_PO_D_V
)
179 for (res
= res0
; res
!= NULL
&& sofd
< 0; res
= res
->ai_next
) {
180 if (n_poption
& n_PO_D_V
) {
181 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
, sizeof hbuf
,
182 NULL
, 0, NI_NUMERICHOST
))
183 memcpy(hbuf
, "unknown host", sizeof("unknown host"));
184 n_err(_("%sConnecting to %s:%s ... "),
185 (res
== res0
? n_empty
: "\n"), hbuf
, serv
);
188 sofd
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
190 # ifdef HAVE_SO_SNDTIMEO
191 (void)setsockopt(sofd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
193 if (connect(sofd
, res
->ai_addr
, res
->ai_addrlen
)) {
194 n_perr("connect(2) failed:", errval
= n_err_no
);
207 # else /* HAVE_GETADDRINFO */
208 if (serv
== urlp
->url_proto
) {
209 if ((ep
= getservbyname(n_UNCONST(serv
), "tcp")) != NULL
)
210 urlp
->url_portno
= ntohs(ep
->s_port
);
212 if (n_poption
& n_PO_D_V
)
213 n_err(_("failed\n"));
214 if ((serv
= n_servbyname(urlp
->url_proto
, &urlp
->url_portno
)) != NULL
)
215 n_err(_(" Unknown service: %s\n"), urlp
->url_proto
);
216 n_err(_(" Trying standard protocol port %s\n"), serv
);
217 n_err(_(" If that succeeds consider including the "
218 "port in the URL!\n"));
220 n_err(_(" Unknown service: %s\n"), urlp
->url_proto
);
221 n_err(_(" Including a port number in the URL may "
222 "circumvent this problem\n"));
223 assert(sofd
== -1 && errval
== 0);
230 hp
= gethostbyname(urlp
->url_host
.s
);
231 if (__sopen_sig
!= -1) {
232 __sopen_sig
= SIGINT
;
240 if (n_poption
& n_PO_D_V
)
241 n_err(_("failed\n"));
243 case HOST_NOT_FOUND
: emsg
= N_("host not found"); break;
245 case TRY_AGAIN
: emsg
= N_("(maybe) try again later"); break;
246 case NO_RECOVERY
: emsg
= N_("non-recoverable server error"); break;
247 case NO_DATA
: emsg
= N_("valid name without IP address"); break;
249 n_err(_("Lookup of %s:%s failed: %s\n"),
250 urlp
->url_host
.s
, serv
, V_(emsg
));
252 } else if (n_poption
& n_PO_D_V
)
255 pptr
= (struct in_addr
**)hp
->h_addr_list
;
256 if ((sofd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
257 n_perr(_("could not create socket"), 0);
258 assert(sofd
== -1 && errval
== 0);
262 memset(&servaddr
, 0, sizeof servaddr
);
263 servaddr
.sin_family
= AF_INET
;
264 servaddr
.sin_port
= htons(urlp
->url_portno
);
265 memcpy(&servaddr
.sin_addr
, *pptr
, sizeof(struct in_addr
));
266 if (n_poption
& n_PO_D_V
)
267 n_err(_("%sConnecting to %s:%d ... "),
268 n_empty
, inet_ntoa(**pptr
), (int)urlp
->url_portno
);
269 # ifdef HAVE_SO_SNDTIMEO
270 (void)setsockopt(sofd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
272 if (connect(sofd
, (struct sockaddr
*)&servaddr
, sizeof servaddr
)) {
273 n_perr("connect(2) failed:", errval
= n_err_no
);
278 # endif /* !HAVE_GETADDRINFO */
281 safe_signal(SIGINT
, oint
);
282 safe_signal(SIGHUP
, ohup
);
288 n_perr(_("Could not connect"), 0);
293 if (n_poption
& n_PO_D_V
)
294 n_err(_("connected.\n"));
296 /* And the regular timeouts XXX configurable */
297 # ifdef HAVE_SO_SNDTIMEO
300 (void)setsockopt(sofd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
301 (void)setsockopt(sofd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof tv
);
303 # ifdef HAVE_SO_LINGER
306 (void)setsockopt(sofd
, SOL_SOCKET
, SO_LINGER
, &li
, sizeof li
);
309 memset(sp
, 0, sizeof *sp
);
312 /* SSL/TLS upgrade? */
316 # if defined HAVE_GETADDRINFO && defined SSL_CTRL_SET_TLSEXT_HOSTNAME /* TODO
317 * TODO the SSL_ def check should NOT be here */
318 if(urlp
->url_flags
& n_URL_TLS_MASK
){
319 memset(&hints
, 0, sizeof hints
);
320 hints
.ai_family
= AF_UNSPEC
;
321 hints
.ai_flags
= AI_NUMERICHOST
;
323 if(getaddrinfo(urlp
->url_host
.s
, NULL
, &hints
, &res0
) == 0)
326 urlp
->url_flags
|= n_URL_HOST_IS_NAME
;
330 if (urlp
->url_flags
& n_URL_TLS_REQUIRED
) {
331 ohup
= safe_signal(SIGHUP
, &__sopen_onsig
);
332 oint
= safe_signal(SIGINT
, &__sopen_onsig
);
333 if (sigsetjmp(__sopen_actjmp
, 0)) {
334 n_err(_("%s during SSL/TLS handshake\n"),
335 (__sopen_sig
== SIGHUP
? _("Hangup") : _("Interrupted")));
340 if (ssl_open(urlp
, sp
) != OKAY
) {
347 safe_signal(SIGINT
, oint
);
348 safe_signal(SIGHUP
, ohup
);
352 # endif /* HAVE_SSL */
355 /* May need to bounce the signal to the go.c trampoline (or wherever) */
356 if (__sopen_sig
!= 0) {
359 sigaddset(&cset
, __sopen_sig
);
360 sigprocmask(SIG_UNBLOCK
, &cset
, NULL
);
361 n_raise(__sopen_sig
);
368 a_socket_xwrite(int fd
, char const *data
, size_t sz
)
375 if ((wo
= write(fd
, data
+ wt
, sz
- wt
)) < 0) {
376 if (n_err_no
== n_ERR_INTR
)
390 sclose(struct sock
*sp
)
397 /* TODO NOTE: we MUST NOT close the descriptor 0 here...
398 * TODO of course this should be handled in a VMAILFS->open() .s_fd=-1,
399 * TODO but unfortunately it isn't yet */
403 if (sp
->s_onclose
!= NULL
)
405 if (sp
->s_wbuf
!= NULL
)
409 void *s_ssl
= sp
->s_ssl
;
413 while (!SSL_shutdown(s_ssl
)) /* XXX proper error handling;signals! */
425 swrite(struct sock
*sp
, char const *data
)
430 rv
= swrite1(sp
, data
, strlen(data
), 0);
436 swrite1(struct sock
*sp
, char const *data
, int sz
, int use_buffer
)
442 if (use_buffer
> 0) {
445 if (sp
->s_wbuf
== NULL
) {
446 sp
->s_wbufsize
= 4096;
447 sp
->s_wbuf
= smalloc(sp
->s_wbufsize
);
450 while (sp
->s_wbufpos
+ sz
> sp
->s_wbufsize
) {
451 di
= sp
->s_wbufsize
- sp
->s_wbufpos
;
453 if (sp
->s_wbufpos
> 0) {
454 memcpy(sp
->s_wbuf
+ sp
->s_wbufpos
, data
, di
);
455 rv
= swrite1(sp
, sp
->s_wbuf
, sp
->s_wbufsize
, -1);
457 rv
= swrite1(sp
, data
, sp
->s_wbufsize
, -1);
463 if (sz
== sp
->s_wbufsize
) {
464 rv
= swrite1(sp
, data
, sp
->s_wbufsize
, -1);
468 memcpy(sp
->s_wbuf
+ sp
->s_wbufpos
, data
, sz
);
473 } else if (use_buffer
== 0 && sp
->s_wbuf
!= NULL
&& sp
->s_wbufpos
> 0) {
476 if ((rv
= swrite1(sp
, sp
->s_wbuf
, x
, -1)) != OKAY
)
487 x
= SSL_write(sp
->s_ssl
, data
, sz
);
489 switch (SSL_get_error(sp
->s_ssl
, x
)) {
490 case SSL_ERROR_WANT_READ
:
491 case SSL_ERROR_WANT_WRITE
:
498 x
= a_socket_xwrite(sp
->s_fd
, data
, sz
);
503 snprintf(o
, sizeof o
, "%s write error",
504 (sp
->s_desc
? sp
->s_desc
: "socket"));
507 ssl_gen_err("%s", o
);
523 sopen(struct sock
*sp
, struct url
*urlp
){
530 /* We may have a proxy configured */
531 if((cp
= xok_vlook(socks_proxy
, urlp
, OXM_ALL
)) == NULL
)
532 rv
= a_socket_open(sp
, urlp
);
534 ui8_t pbuf
[4 + 1 + 255 + 2];
539 if(!url_parse(&url2
, CPROTO_SOCKS
, cp
)){
540 n_err(_("Failed to parse *socks-proxy*: %s\n"), cp
);
543 if(urlp
->url_host
.l
> 255){
544 n_err(_("*socks-proxy*: hostname too long: %s\n"),
549 if (n_poption
& n_PO_D_V
)
550 n_err(_("Connecting via *socks-proxy* to %s:%s ...\n"),
552 (urlp
->url_port
!= NULL
? urlp
->url_port
: urlp
->url_proto
));
554 if(!a_socket_open(sp
, &url2
)){
555 n_err(_("Failed to connect to *socks-proxy*: %s\n"), cp
);
559 /* RFC 1928: version identifier/method selection message */
560 pbuf
[0] = 0x05; /* VER: protocol version: X'05' */
561 pbuf
[1] = 0x01; /* NMETHODS: 1 */
562 pbuf
[2] = 0x00; /* METHOD: X'00' NO AUTHENTICATION REQUIRED */
563 if(write(sp
->s_fd
, pbuf
, 3) != 3){
565 n_perr("*socks-proxy*", 0);
571 /* Receive greeting */
572 if(read(sp
->s_fd
, pbuf
, 2) != 2)
574 if(pbuf
[0] != 0x05 || pbuf
[1] != 0x00){
576 emsg
= N_("unexpected reply\n");
578 /* I18N: error message and failing URL */
579 n_err(_("*socks-proxy*: %s: %s\n"), V_(emsg
), cp
);
583 /* RFC 1928: CONNECT request */
584 pbuf
[0] = 0x05; /* VER: protocol version: X'05' */
585 pbuf
[1] = 0x01; /* CMD: CONNECT X'01' */
586 pbuf
[2] = 0x00; /* RESERVED */
587 pbuf
[3] = 0x03; /* ATYP: domain name */
588 pbuf
[4] = (ui8_t
)urlp
->url_host
.l
;
589 memcpy(&pbuf
[i
= 5], urlp
->url_host
.s
, urlp
->url_host
.l
);
593 x
= htons(urlp
->url_portno
);
594 memcpy(&pbuf
[i
+= urlp
->url_host
.l
], (ui8_t
*)&x
, sizeof x
);
597 if(write(sp
->s_fd
, pbuf
, i
) != (ssize_t
)i
)
601 if((i
= read(sp
->s_fd
, pbuf
, 4)) != 4)
603 /* Version 5, reserved must be 0 */
604 if(pbuf
[0] != 0x05 || pbuf
[2] != 0x00)
608 case 0x00: emsg
= NULL
; break;
609 case 0x01: emsg
= N_("SOCKS server failure"); break;
610 case 0x02: emsg
= N_("connection not allowed by ruleset"); break;
611 case 0x03: emsg
= N_("network unreachable"); break;
612 case 0x04: emsg
= N_("host unreachable"); break;
613 case 0x05: emsg
= N_("connection refused"); break;
614 case 0x06: emsg
= N_("TTL expired"); break;
615 case 0x07: emsg
= N_("command not supported"); break;
616 case 0x08: emsg
= N_("address type not supported"); break;
617 default: emsg
= N_("unknown SOCKS error code"); break;
620 goto jesocksreplymsg
;
622 /* Address type variable; read the BND.PORT with it.
623 * This is actually false since RFC 1928 says that the BND.ADDR reply to
624 * CONNECT contains the IP address, so only 0x01 and 0x04 are allowed */
626 case 0x01: i
= 4; break;
627 case 0x03: i
= 1; break;
628 case 0x04: i
= 16; break;
629 default: goto jesocksreply
;
632 if(read(sp
->s_fd
, pbuf
, i
) != (ssize_t
)i
)
634 if(i
== 1 + sizeof(ui16_t
)){
636 if(read(sp
->s_fd
, pbuf
, i
) != (ssize_t
)i
)
647 (sgetline
)(char **line
, size_t *linesize
, size_t *linelen
, struct sock
*sp
666 if (lp_base
== NULL
|| PTRCMP(lp
, >, lp_base
+ lsize
- 128)) {
667 size_t diff
= PTR2SIZE(lp
- lp_base
);
668 *linesize
= (lsize
+= 256); /* XXX magic */
669 *line
= lp_base
= (n_realloc
)(lp_base
, lsize n_MEMORY_DEBUG_ARGSCALL
);
673 if (sp
->s_rbufptr
== NULL
||
674 PTRCMP(sp
->s_rbufptr
, >=, sp
->s_rbuf
+ sp
->s_rsz
)) {
678 sp
->s_rsz
= SSL_read(sp
->s_ssl
, sp
->s_rbuf
, sizeof sp
->s_rbuf
);
679 if (sp
->s_rsz
<= 0) {
683 switch(SSL_get_error(sp
->s_ssl
, sp
->s_rsz
)) {
684 case SSL_ERROR_WANT_READ
:
685 case SSL_ERROR_WANT_WRITE
:
688 snprintf(o
, sizeof o
, "%s",
689 (sp
->s_desc
? sp
->s_desc
: "socket"));
690 ssl_gen_err("%s", o
);
698 sp
->s_rsz
= read(sp
->s_fd
, sp
->s_rbuf
, sizeof sp
->s_rbuf
);
699 if (sp
->s_rsz
<= 0) {
703 if (n_err_no
== n_ERR_INTR
)
705 snprintf(o
, sizeof o
, "%s",
706 (sp
->s_desc
? sp
->s_desc
: "socket"));
712 sp
->s_rbufptr
= sp
->s_rbuf
;
714 } while ((*lp
++ = *sp
->s_rbufptr
++) != '\n');
716 lsize
= PTR2SIZE(lp
- lp_base
);
725 #endif /* HAVE_SOCKETS */