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 - 2014 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
53 # include <sys/socket.h>
57 # include <netinet/in.h>
59 # ifdef HAVE_ARPA_INET_H
60 # include <arpa/inet.h>
65 # include <openssl/err.h>
66 # include <openssl/rand.h>
67 # include <openssl/ssl.h>
68 # include <openssl/x509v3.h>
69 # include <openssl/x509.h>
73 FILE *s_file
; /* File we were in. */
74 enum condition s_cond
; /* Saved state of conditionals */
75 int s_loading
; /* Loading .mailrc, etc. */
78 static size_t _ssp
; /* Top of file stack */
81 /* Locate the user's mailbox file (where new, unread mail is queued) */
82 static void _findmail(char *buf
, size_t bufsize
, char const *user
,
85 /* Perform shell meta character expansion */
86 static char * _globname(char const *name
, enum fexp_mode fexpm
);
88 /* *line* is a buffer with the result of fgets().
89 * Returns the first newline or the last character read */
90 static size_t _length_of_line(const char *line
, size_t linesize
);
92 /* Read a line, one character at a time */
93 static char * _fgetline_byone(char **line
, size_t *linesize
, size_t *llen
,
94 FILE *fp
, int appendnl
, size_t n SMALLOC_DEBUG_ARGS
);
96 static void makemessage(void);
97 static void _fio_append(struct message
*mp
);
98 static enum okay
get_header(struct message
*mp
);
101 _findmail(char *buf
, size_t bufsize
, char const *user
, bool_t force
)
105 if (strcmp(user
, myname
) == 0 && ! force
&&
106 (cp
= ok_vlook(folder
)) != NULL
) {
107 switch (which_protocol(cp
)) {
109 if (strcmp(cp
, protbase(cp
)) != 0)
111 snprintf(buf
, bufsize
, "%s/INBOX", cp
);
118 if (force
|| (cp
= ok_vlook(MAIL
)) == NULL
)
119 snprintf(buf
, bufsize
, "%s/%s", MAILSPOOL
, user
);
122 n_strlcpy(buf
, cp
, bufsize
);
129 _globname(char const *name
, enum fexp_mode fexpm
)
137 /* Mac OS X Snow Leopard and Linux don't init fields on error, causing
138 * SIGSEGV in wordfree(3); so let's just always zero it ourselfs */
139 memset(&we
, 0, sizeof we
);
141 /* Some systems (notably Open UNIX 8.0.0) fork a shell for wordexp()
142 * and wait, which will fail if our SIGCHLD handler is active */
144 sigaddset(&nset
, SIGCHLD
);
145 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
146 i
= wordexp(name
, &we
, 0);
147 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
153 if (!(fexpm
& FEXP_SILENT
))
155 tr(83, "\"%s\": Expansion buffer overflow.\n"),
161 if (!(fexpm
& FEXP_SILENT
))
162 fprintf(stderr
, tr(242, "Syntax error in \"%s\"\n"),
167 switch (we
.we_wordc
) {
169 cp
= savestr(we
.we_wordv
[0]);
172 if (!(fexpm
& FEXP_SILENT
))
173 fprintf(stderr
, tr(82, "\"%s\": No match.\n"), name
);
176 if (fexpm
& FEXP_MULTIOK
) {
179 for (l
= 0, j
= 0; j
< we
.we_wordc
; ++j
)
180 l
+= strlen(we
.we_wordv
[j
]) + 1;
183 for (l
= 0, j
= 0; j
< we
.we_wordc
; ++j
) {
184 size_t x
= strlen(we
.we_wordv
[j
]);
185 memcpy(cp
+ l
, we
.we_wordv
[j
], x
);
190 } else if (!(fexpm
& FEXP_SILENT
))
191 fprintf(stderr
, tr(84, "\"%s\": Ambiguous.\n"), name
);
198 #else /* !HAVE_WORDEXP */
200 char xname
[MAXPATHLEN
], cmdbuf
[MAXPATHLEN
], /* also used for files */
202 int pivec
[2], pid
, l
, waits
;
204 if (pipe(pivec
) < 0) {
208 snprintf(cmdbuf
, sizeof cmdbuf
, "echo %s", name
);
209 if ((shellp
= ok_vlook(SHELL
)) == NULL
)
210 shellp
= UNCONST(XSHELL
);
211 pid
= start_command(shellp
, 0, -1, pivec
[1], "-c", cmdbuf
, NULL
);
220 l
= read(pivec
[0], xname
, sizeof xname
);
229 if (!wait_child(pid
, &waits
) && WTERMSIG(waits
) != SIGPIPE
) {
230 if (!(fexpm
& FEXP_SILENT
))
231 fprintf(stderr
, tr(81, "\"%s\": Expansion failed.\n"),
236 if (!(fexpm
& FEXP_SILENT
))
237 fprintf(stderr
, tr(82, "\"%s\": No match.\n"), name
);
240 if (l
== sizeof xname
) {
241 if (!(fexpm
& FEXP_SILENT
))
243 tr(83, "\"%s\": Expansion buffer overflow.\n"),
248 for (cp
= &xname
[l
- 1]; *cp
== '\n' && cp
> xname
; --cp
)
251 if (!(fexpm
& FEXP_MULTIOK
) && strchr(xname
, ' ') &&
252 stat(xname
, &sbuf
) < 0) {
253 if (!(fexpm
& FEXP_SILENT
))
254 fprintf(stderr
, tr(84, "\"%s\": Ambiguous.\n"), name
);
257 return savestr(xname
);
258 #endif /* !HAVE_WORDEXP */
262 * line is a buffer with the result of fgets(). Returns the first
263 * newline or the last character read.
266 _length_of_line(const char *line
, size_t linesize
)
270 /* Last character is always '\0' and was added by fgets() */
271 for (--linesize
, i
= 0; i
< linesize
; i
++)
274 return (i
< linesize
) ? i
+ 1 : linesize
;
278 _fgetline_byone(char **line
, size_t *linesize
, size_t *llen
,
279 FILE *fp
, int appendnl
, size_t n SMALLOC_DEBUG_ARGS
)
283 assert(*linesize
== 0 || *line
!= NULL
);
285 if (*linesize
<= LINESIZE
|| n
>= *linesize
- 128) {
286 *linesize
+= ((*line
== NULL
)
287 ? LINESIZE
+ n
+ 1 : 256);
288 *line
= (srealloc
)(*line
, *linesize
289 SMALLOC_DEBUG_ARGSCALL
);
314 (fgetline
)(char **line
, size_t *linesize
, size_t *cnt
, size_t *llen
,
315 FILE *fp
, int appendnl SMALLOC_DEBUG_ARGS
)
321 * If we have no count, we cannot determine where the
322 * characters returned by fgets() end if there was no
323 * newline. We have to read one character at one.
325 return _fgetline_byone(line
, linesize
, llen
, fp
, appendnl
, 0
326 SMALLOC_DEBUG_ARGSCALL
);
327 if (*line
== NULL
|| *linesize
< LINESIZE
)
328 *line
= (srealloc
)(*line
, *linesize
= LINESIZE
329 SMALLOC_DEBUG_ARGSCALL
);
330 sz
= *linesize
<= *cnt
? *linesize
: *cnt
+ 1;
331 if (sz
<= 1 || fgets(*line
, sz
, fp
) == NULL
)
333 * Leave llen untouched; it is used to determine whether
334 * the last line was \n-terminated in some callers.
337 i_llen
= _length_of_line(*line
, sz
);
339 while ((*line
)[i_llen
- 1] != '\n') {
340 *line
= (srealloc
)(*line
, *linesize
+= 256
341 SMALLOC_DEBUG_ARGSCALL
);
342 sz
= *linesize
- i_llen
;
343 sz
= (sz
<= *cnt
? sz
: *cnt
+ 1);
344 if (sz
<= 1 || fgets(&(*line
)[i_llen
], sz
, fp
) == NULL
) {
346 (*line
)[i_llen
++] = '\n';
347 (*line
)[i_llen
] = '\0';
351 sz
= _length_of_line(&(*line
)[i_llen
], sz
);
361 (readline_restart
)(FILE *ibuf
, char **linebuf
, size_t *linesize
, size_t n
364 /* TODO readline_restart(): always *appends* LF just to strip it again;
365 * TODO should be configurable just as for fgetline(); ..or whatevr.. */
370 * Interrupts will cause trouble if we are inside a stdio call. As
371 * this is only relevant if input comes from a terminal, we can simply
372 * bypass it by read() then.
374 if (fileno(ibuf
) == 0 && (options
& OPT_TTYIN
)) {
375 assert(*linesize
== 0 || *linebuf
!= NULL
);
377 if (*linesize
<= LINESIZE
|| n
>= *linesize
- 128) {
378 *linesize
+= ((*linebuf
== NULL
)
379 ? LINESIZE
+ n
+ 1 : 256);
380 *linebuf
= (srealloc
)(*linebuf
, *linesize
381 SMALLOC_DEBUG_ARGSCALL
);
384 sz
= read(0, *linebuf
+ n
, *linesize
- n
- 1);
387 (*linebuf
)[n
] = '\0';
388 if (n
> 0 && (*linebuf
)[n
- 1] == '\n')
391 if (sz
< 0 && errno
== EINTR
)
394 if ((*linebuf
)[n
- 1] != '\n') {
395 (*linebuf
)[n
++] = '\n';
396 (*linebuf
)[n
] = '\0';
405 * Not reading from standard input or standard input not
406 * a terminal. We read one char at a time as it is the
407 * only way to get lines with embedded NUL characters in
410 if (_fgetline_byone(linebuf
, linesize
, &n
, ibuf
, 1, n
411 SMALLOC_DEBUG_ARGSCALL
) == NULL
)
414 if (n
> 0 && (*linebuf
)[n
- 1] == '\n')
415 (*linebuf
)[--n
] = '\0';
420 (readline_input
)(char const *prompt
, bool_t nl_escape
, char **linebuf
,
421 size_t *linesize
, char const *string SMALLOC_DEBUG_ARGS
)
423 /* TODO readline: linebuf pool! */
424 FILE *ifile
= (_input
!= NULL
) ? _input
: stdin
;
425 bool_t doprompt
, dotty
;
428 doprompt
= (!sourcing
&& (options
& OPT_INTERACTIVE
));
429 dotty
= (doprompt
&& !ok_blook(line_editor_disable
));
432 else if (prompt
== NULL
)
433 prompt
= getprompt();
437 assert(ifile
== stdin
);
438 if (string
!= NULL
&& (n
= (int)strlen(string
)) > 0) {
442 *linesize
= (size_t)n
+ LINESIZE
+1;
443 *linebuf
= (srealloc
)(*linebuf
, *linesize
444 SMALLOC_DEBUG_ARGSCALL
);
445 memcpy(*linebuf
, string
, (size_t)n
+1);
448 n
= (tty_readline
)(prompt
, linebuf
, linesize
, n
449 SMALLOC_DEBUG_ARGSCALL
);
451 if (prompt
!= NULL
&& *prompt
!= '\0') {
452 fputs(prompt
, stdout
);
455 n
= (readline_restart
)(ifile
, linebuf
, linesize
, n
456 SMALLOC_DEBUG_ARGSCALL
);
461 * An unquoted <backslash> at the end of a command line shall
462 * be discarded and the next line shall continue the command */
463 if (nl_escape
&& (*linebuf
)[n
- 1] == '\\') {
464 (*linebuf
)[--n
] = '\0';
465 if (prompt
!= NULL
&& *prompt
!= '\0')
466 prompt
= ".. "; /* XXX PS2 .. */
475 readstr_input(char const *prompt
, char const *string
)
477 /* FIXME readstr_input: without linepool leaks on sigjmp */
479 char *linebuf
= NULL
, *rv
= NULL
;
482 n
= readline_input(prompt
, FAL0
, &linebuf
, &linesize
, string
);
484 rv
= savestrbuf(linebuf
, (size_t)n
+ 1);
491 * Set up the input pointers while copying the mail file into /tmp.
494 setptr(FILE *ibuf
, off_t offset
)
497 char *cp
, *linebuf
= NULL
;
500 int maybe
, inhead
, thiscnt
;
501 size_t linesize
= 0, filesize
, cnt
;
506 memset(&this, 0, sizeof this);
507 this.m_flag
= MUSED
|MNEW
|MNEWEST
;
508 filesize
= mailsize
- offset
;
509 offset
= ftell(mb
.mb_otf
);
511 if (fgetline(&linebuf
, &linesize
, &filesize
, &cnt
, ibuf
, 0)
513 this.m_xsize
= this.m_size
;
514 this.m_xlines
= this.m_lines
;
515 this.m_have
= HAVE_HEADER
|HAVE_BODY
;
524 if (linebuf
[0] == '\0')
527 /* XXX Convert CRLF to LF; this should be rethought in that
528 * XXX CRLF input should possibly end as CRLF output? */
529 if (cnt
>= 2 && linebuf
[cnt
- 1] == '\n' &&
530 linebuf
[cnt
- 2] == '\r')
531 linebuf
[--cnt
- 1] = '\n';
532 fwrite(linebuf
, sizeof *linebuf
, cnt
, mb
.mb_otf
);
533 if (ferror(mb
.mb_otf
)) {
537 if (linebuf
[cnt
- 1] == '\n')
538 linebuf
[cnt
- 1] = '\0';
539 if (maybe
&& linebuf
[0] == 'F' && is_head(linebuf
, cnt
)) {
541 * TODO char date[FROM_DATEBUF];
542 * TODO extract_date_from_from_(linebuf, cnt, date);
543 * TODO this.m_time = 10000;
545 this.m_xsize
= this.m_size
;
546 this.m_xlines
= this.m_lines
;
547 this.m_have
= HAVE_HEADER
|HAVE_BODY
;
551 this.m_flag
= MUSED
|MNEW
|MNEWEST
;
554 this.m_block
= mailx_blockof(offset
);
555 this.m_offset
= mailx_offsetof(offset
);
557 } else if (linebuf
[0] == 0) {
560 for (cp
= linebuf
, cp2
= "status";; cp
++) {
561 if ((c
= *cp2
++) == 0) {
562 while (c
= *cp
++, whitechar(c
));
565 while ((c
= *cp
++) != '\0')
567 this.m_flag
|= MREAD
;
569 this.m_flag
&= ~MNEW
;
572 if (*cp
!= c
&& *cp
!= upperconv(c
))
575 for (cp
= linebuf
, cp2
= "x-status";; cp
++) {
576 if ((c
= *cp2
++) == 0) {
577 while (c
= *cp
++, whitechar(c
));
580 while ((c
= *cp
++) != '\0')
582 this.m_flag
|= MFLAGGED
;
584 this.m_flag
|=MANSWERED
;
586 this.m_flag
|=MDRAFTED
;
589 if (*cp
!= c
&& *cp
!= upperconv(c
))
596 maybe
= linebuf
[0] == 0;
602 * Drop the passed line onto the passed output buffer.
603 * If a write error occurs, return -1, else the count of
604 * characters written, including the newline.
607 putline(FILE *obuf
, char *linebuf
, size_t cnt
)
609 fwrite(linebuf
, sizeof *linebuf
, cnt
, obuf
);
617 * Return a file buffer all ready to read up the
618 * passed message pointer.
621 setinput(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
627 if (m
->m_have
& HAVE_HEADER
)
633 if (m
->m_have
& HAVE_BODY
)
645 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
,
646 m
->m_offset
), SEEK_SET
) < 0) {
648 panic(tr(77, "temporary file seek"));
654 setdot(struct message
*mp
)
658 did_print_dot
= FAL0
;
666 * Take the data out of the passed ghost file and toss it into
667 * a dynamically allocated message structure.
675 message
[msgCount
].m_size
= 0;
676 message
[msgCount
].m_lines
= 0;
680 * Append the passed message descriptor onto the message structure.
683 _fio_append(struct message
*mp
)
685 if (msgCount
+ 1 >= msgspace
)
686 message
= srealloc(message
, (msgspace
+= 64) * sizeof *message
);
688 message
[msgCount
- 1] = *mp
;
692 * Delete a file, but only if the file is a plain file.
695 rm(char *name
) /* TODO TOCTOU; but i'm out of ideas today */
700 if (stat(name
, &sb
) < 0)
702 else if (! S_ISREG(sb
.st_mode
))
710 * Determine the size of the file possessed by
718 if (fstat(fileno(iob
), &sbuf
) < 0)
724 fexpand(char const *name
, enum fexp_mode fexpm
)
726 char cbuf
[MAXPATHLEN
], *res
;
732 * The order of evaluation is "%" and "#" expand into constants.
733 * "&" can expand into "+". "+" can expand into shell meta characters.
734 * Shell meta characters expand into constants.
735 * This way, we make no recursive expansion.
738 if (! (fexpm
& FEXP_NSHORTCUT
) && (sh
= get_shortcut(res
)) != NULL
)
741 if (fexpm
& FEXP_SHELL
) {
749 if (res
[1] == ':' && res
[2] != '\0') {
753 _findmail(cbuf
, sizeof cbuf
,
754 (res
[1] != '\0') ? res
+ 1 : myname
,
755 (res
[1] != '\0' || (options
& OPT_u_FLAG
)));
761 if (prevfile
[0] == '\0') {
762 fprintf(stderr
, tr(80, "No previous file\n"));
769 if (res
[1] == '\0') {
770 if ((res
= ok_vlook(MBOX
)) == NULL
)
771 res
= UNCONST("~/mbox");
772 else if (res
[0] != '&' || res
[1] != '\0')
778 if (res
[0] == '@' && which_protocol(mailname
) == PROTO_IMAP
) {
779 res
= str_concat_csvl(&s
,
780 protbase(mailname
), "/", res
+ 1, NULL
)->s
;
784 if (res
[0] == '+' && getfold(cbuf
, sizeof cbuf
)) {
785 size_t i
= strlen(cbuf
);
787 res
= str_concat_csvl(&s
, cbuf
,
788 (i
> 0 && cbuf
[i
- 1] == '/') ? "" : "/",
792 if (res
[0] == '%' && res
[1] == ':') {
798 /* Catch the most common shell meta character */
800 if (res
[0] == '~' && (res
[1] == '/' || res
[1] == '\0')) {
801 res
= str_concat_csvl(&s
, homedir
, res
+ 1, NULL
)->s
;
805 if (anyof(res
, "|&;<>~{}()[]*?$`'\"\\") &&
806 which_protocol(res
) == PROTO_FILE
) {
807 res
= _globname(res
, fexpm
);
813 if (fexpm
& FEXP_LOCAL
)
814 switch (which_protocol(res
)) {
816 case PROTO_MAILDIR
: /* XXX Really? ok MAILDIR for local? */
819 fprintf(stderr
, tr(280,
820 "`%s': only a local file or directory may "
835 if (ok_blook(keep
) || rm(mailname
) < 0) {
836 int fd
= open(mailname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
843 var_folder_updated(char const *name
, char **store
)
846 char *folder
, *unres
= NULL
, *res
= NULL
;
848 if ((folder
= UNCONST(name
)) == NULL
)
851 /* Expand the *folder*; skip `%:' prefix for simplicity of use */
852 /* XXX This *only* works because we do NOT
853 * XXX update environment variables via the "set" mechanism */
854 if (folder
[0] == '%' && folder
[1] == ':')
856 if ((folder
= fexpand(folder
, FEXP_FULL
)) == NULL
) /* XXX error? */
859 switch (which_protocol(folder
)) {
861 /* Ooops. This won't work */
862 fprintf(stderr
, tr(501, "`folder' cannot be set to a flat, "
863 "readonly POP3 account\n"));
867 /* Simply assign what we have, even including `%:' prefix */
872 /* Further expansion desired */
876 /* All non-absolute paths are relative to our home directory */
877 if (*folder
!= '/') {
878 size_t l1
= strlen(homedir
), l2
= strlen(folder
);
879 unres
= ac_alloc(l1
+ l2
+ 2);
880 memcpy(unres
, homedir
, l1
);
882 memcpy(unres
+ l1
+ 1, folder
, l2
);
883 unres
[l1
+ 1 + l2
] = '\0';
887 /* Since lex.c:_update_mailname() uses realpath(3) if available to
888 * avoid that we loose track of our currently open folder in case we
889 * chdir away, but still checks the leading path portion against
890 * getfold() to be able to abbreviate to the +FOLDER syntax if
891 * possible, we need to realpath(3) the folder, too */
893 res
= ac_alloc(MAXPATHLEN
);
894 if (realpath(folder
, res
) == NULL
)
895 fprintf(stderr
, tr(151, "Can't canonicalize `%s'\n"), folder
);
901 *store
= sstrdup(folder
);
912 getfold(char *name
, size_t size
)
916 if ((folder
= ok_vlook(folder
)) != NULL
)
917 (void)n_strlcpy(name
, folder
, size
);
918 return (folder
!= NULL
);
922 * Return the name of the dead.letter file.
929 if ((cp
= ok_vlook(DEAD
)) == NULL
||
930 (cp
= fexpand(cp
, FEXP_LOCAL
)) == NULL
)
931 cp
= fexpand("~/dead.letter", FEXP_LOCAL
|FEXP_SHELL
);
932 else if (*cp
!= '/') {
933 size_t sz
= strlen(cp
) + 3;
934 char *buf
= ac_alloc(sz
);
936 snprintf(buf
, sz
, "~/%s", cp
);
937 cp
= fexpand(buf
, FEXP_LOCAL
|FEXP_SHELL
);
946 get_header(struct message
*mp
)
949 switch (mb
.mb_type
) {
955 return (pop3_header(mp
));
960 return imap_header(mp
);
969 get_body(struct message
*mp
)
972 switch (mb
.mb_type
) {
978 return (pop3_body(mp
));
983 return imap_body(mp
);
992 static long xwrite(int fd
, const char *data
, size_t sz
);
995 xwrite(int fd
, const char *data
, size_t sz
)
1001 if ((wo
= write(fd
, data
+ wt
, sz
- wt
)) < 0) {
1013 sclose(struct sock
*sp
)
1018 if (sp
->s_onclose
!= NULL
)
1021 if (sp
->s_use_ssl
) {
1023 SSL_shutdown(sp
->s_ssl
);
1024 SSL_free(sp
->s_ssl
);
1026 SSL_CTX_free(sp
->s_ctx
);
1031 i
= close(sp
->s_fd
);
1041 swrite(struct sock
*sp
, const char *data
)
1043 return swrite1(sp
, data
, strlen(data
), 0);
1047 swrite1(struct sock
*sp
, const char *data
, int sz
, int use_buffer
)
1051 if (use_buffer
> 0) {
1055 if (sp
->s_wbuf
== NULL
) {
1056 sp
->s_wbufsize
= 4096;
1057 sp
->s_wbuf
= smalloc(sp
->s_wbufsize
);
1060 while (sp
->s_wbufpos
+ sz
> sp
->s_wbufsize
) {
1061 di
= sp
->s_wbufsize
- sp
->s_wbufpos
;
1063 if (sp
->s_wbufpos
> 0) {
1064 memcpy(&sp
->s_wbuf
[sp
->s_wbufpos
], data
, di
);
1065 ok
= swrite1(sp
, sp
->s_wbuf
,
1066 sp
->s_wbufsize
, -1);
1068 ok
= swrite1(sp
, data
,
1069 sp
->s_wbufsize
, -1);
1075 if (sz
== sp
->s_wbufsize
) {
1076 ok
= swrite1(sp
, data
, sp
->s_wbufsize
, -1);
1080 memcpy(&sp
->s_wbuf
[sp
->s_wbufpos
], data
, sz
);
1081 sp
->s_wbufpos
+= sz
;
1084 } else if (use_buffer
== 0 && sp
->s_wbuf
!= NULL
&&
1085 sp
->s_wbufpos
> 0) {
1088 if (swrite1(sp
, sp
->s_wbuf
, x
, -1) != OKAY
)
1094 if (sp
->s_use_ssl
) {
1095 ssl_retry
: x
= SSL_write(sp
->s_ssl
, data
, sz
);
1097 switch (SSL_get_error(sp
->s_ssl
, x
)) {
1098 case SSL_ERROR_WANT_READ
:
1099 case SSL_ERROR_WANT_WRITE
:
1106 x
= xwrite(sp
->s_fd
, data
, sz
);
1110 snprintf(o
, sizeof o
, "%s write error",
1111 sp
->s_desc
? sp
->s_desc
: "socket");
1113 sp
->s_use_ssl
? ssl_gen_err("%s", o
) : perror(o
);
1125 sopen(const char *xserver
, struct sock
*sp
, int use_ssl
,
1126 const char *uhp
, const char *portstr
, int verbose
)
1128 #ifdef HAVE_SO_SNDTIMEO
1131 #ifdef HAVE_SO_LINGER
1135 char hbuf
[NI_MAXHOST
];
1136 struct addrinfo hints
, *res0
, *res
;
1138 struct sockaddr_in servaddr
;
1139 struct in_addr
**pptr
;
1142 unsigned short port
= 0;
1146 char *server
= UNCONST(xserver
);
1150 if ((cp
= strchr(server
, ':')) != NULL
) { /* TODO URI parse! IPv6! */
1153 port
= strtol(portstr
, NULL
, 10);
1155 server
= salloc(cp
- xserver
+ 1);
1156 memcpy(server
, xserver
, cp
- xserver
);
1157 server
[cp
- xserver
] = '\0';
1160 /* Connect timeouts after 30 seconds */
1161 #ifdef HAVE_SO_SNDTIMEO
1168 fprintf(stderr
, "Resolving host %s . . .", server
);
1169 memset(&hints
, 0, sizeof hints
);
1170 hints
.ai_socktype
= SOCK_STREAM
;
1171 if (getaddrinfo(server
, portstr
, &hints
, &res0
) != 0) {
1172 fprintf(stderr
, tr(252, " lookup of `%s' failed.\n"), server
);
1175 fprintf(stderr
, tr(500, " done.\n"));
1178 for (res
= res0
; res
!= NULL
&& sockfd
< 0; res
= res
->ai_next
) {
1180 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
1181 hbuf
, sizeof hbuf
, NULL
, 0,
1182 NI_NUMERICHOST
) != 0)
1183 strcpy(hbuf
, "unknown host");
1184 fprintf(stderr
, tr(192,
1185 "%sConnecting to %s:%s . . ."),
1186 (res
== res0
) ? "" : "\n",
1189 if ((sockfd
= socket(res
->ai_family
, res
->ai_socktype
,
1190 res
->ai_protocol
)) >= 0) {
1191 # ifdef HAVE_SO_SNDTIMEO
1192 setsockopt(sockfd
, SOL_SOCKET
, SO_SNDTIMEO
,
1195 if (connect(sockfd
, res
->ai_addr
, res
->ai_addrlen
)!=0) {
1202 perror(tr(254, " could not connect"));
1208 #else /* HAVE_IPV6 */
1210 if (strcmp(portstr
, "smtp") == 0)
1212 else if (strcmp(portstr
, "smtps") == 0)
1215 else if (strcmp(portstr
, "imap") == 0)
1217 else if (strcmp(portstr
, "imaps") == 0)
1221 else if (strcmp(portstr
, "pop3") == 0)
1223 else if (strcmp(portstr
, "pop3s") == 0)
1226 else if ((ep
= getservbyname(UNCONST(portstr
), "tcp")) != NULL
)
1229 fprintf(stderr
, tr(251, "Unknown service: %s\n"),
1237 fprintf(stderr
, "Resolving host %s . . .", server
);
1238 if ((hp
= gethostbyname(server
)) == NULL
) {
1239 fprintf(stderr
, tr(252, " lookup of `%s' failed.\n"), server
);
1242 fprintf(stderr
, tr(500, " done.\n"));
1244 pptr
= (struct in_addr
**)hp
->h_addr_list
;
1245 if ((sockfd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
1246 perror(tr(253, "could not create socket"));
1249 memset(&servaddr
, 0, sizeof servaddr
);
1250 servaddr
.sin_family
= AF_INET
;
1251 servaddr
.sin_port
= port
;
1252 memcpy(&servaddr
.sin_addr
, *pptr
, sizeof(struct in_addr
));
1254 fprintf(stderr
, tr(192, "%sConnecting to %s:%d . . ."),
1255 "", inet_ntoa(**pptr
), ntohs(port
));
1257 # ifdef HAVE_SO_SNDTIMEO
1258 setsockopt(sockfd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
1260 if (connect(sockfd
, (struct sockaddr
*)&servaddr
, sizeof servaddr
)
1262 perror(tr(254, " could not connect"));
1265 #endif /* HAVE_IPV6 */
1267 fputs(tr(193, " connected.\n"), stderr
);
1269 /* And the regular timeouts */
1270 #ifdef HAVE_SO_SNDTIMEO
1273 setsockopt(sockfd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
1274 setsockopt(sockfd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof tv
);
1276 #ifdef HAVE_SO_LINGER
1279 setsockopt(sockfd
, SOL_SOCKET
, SO_LINGER
, &li
, sizeof li
);
1282 memset(sp
, 0, sizeof *sp
);
1285 if (use_ssl
&& ssl_open(server
, sp
, uhp
) != OKAY
) {
1294 (sgetline
)(char **line
, size_t *linesize
, size_t *linelen
, struct sock
*sp
1299 if (sp
->s_rsz
< 0) {
1304 if (*line
== NULL
|| lp
> &(*line
)[*linesize
- 128]) {
1305 size_t diff
= lp
- *line
;
1306 *line
= (srealloc
)(*line
, *linesize
+= 256
1307 SMALLOC_DEBUG_ARGSCALL
);
1308 lp
= &(*line
)[diff
];
1310 if (sp
->s_rbufptr
== NULL
||
1311 sp
->s_rbufptr
>= &sp
->s_rbuf
[sp
->s_rsz
]) {
1313 if (sp
->s_use_ssl
) {
1314 ssl_retry
: if ((sp
->s_rsz
= SSL_read(sp
->s_ssl
,
1316 sizeof sp
->s_rbuf
)) <= 0) {
1317 if (sp
->s_rsz
< 0) {
1319 switch(SSL_get_error(sp
->s_ssl
,
1321 case SSL_ERROR_WANT_READ
:
1322 case SSL_ERROR_WANT_WRITE
:
1325 snprintf(o
, sizeof o
, "%s",
1329 ssl_gen_err("%s", o
);
1337 again
: if ((sp
->s_rsz
= read(sp
->s_fd
, sp
->s_rbuf
,
1338 sizeof sp
->s_rbuf
)) <= 0) {
1339 if (sp
->s_rsz
< 0) {
1343 snprintf(o
, sizeof o
, "%s",
1352 sp
->s_rbufptr
= sp
->s_rbuf
;
1354 } while ((*lp
++ = *sp
->s_rbufptr
++) != '\n');
1357 *linelen
= lp
- *line
;
1360 #endif /* HAVE_SOCKETS */
1363 load(char const *name
)
1367 if (name
== NULL
|| (in
= Fopen(name
, "r")) == NULL
)
1388 if ((cp
= fexpand(*arglist
, FEXP_LOCAL
)) == NULL
)
1390 if ((fi
= Fopen(cp
, "r")) == NULL
) {
1394 if (_ssp
>= SSTACK
- 1) {
1395 fprintf(stderr
, tr(3, "Too much \"sourcing\" going on.\n"));
1400 _sstack
[_ssp
].s_file
= _input
;
1401 _sstack
[_ssp
].s_cond
= cond_state
;
1402 _sstack
[_ssp
].s_loading
= loading
;
1405 cond_state
= COND_ANY
;
1419 fprintf(stderr
, tr(4, "\"Source\" stack over-pop.\n"));
1425 if (cond_state
!= COND_ANY
)
1426 fprintf(stderr
, tr(5, "Unmatched \"if\"\n"));
1428 cond_state
= _sstack
[_ssp
].s_cond
;
1429 loading
= _sstack
[_ssp
].s_loading
;
1430 _input
= _sstack
[_ssp
].s_file
;