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 - 2013 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
= value("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
= value("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
= value("SHELL")) == NULL
)
210 shellp
= UNCONST(SHELL
);
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_safe
)(*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_safe
)(*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_safe
)(*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_safe
)(*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
)(enum lned_mode lned
, char const *prompt
, char **linebuf
,
421 size_t *linesize SMALLOC_DEBUG_ARGS
)
423 FILE *ifile
= (_input
!= NULL
) ? _input
: stdin
;
424 bool_t doprompt
, dotty
;
427 doprompt
= (!sourcing
&& (options
& OPT_INTERACTIVE
));
428 dotty
= (doprompt
&& !boption("line-editor-disable"));
431 else if (prompt
== NULL
)
432 prompt
= getprompt();
436 assert(ifile
== stdin
);
437 n
= (tty_readline
)(prompt
, linebuf
, linesize
, n
438 SMALLOC_DEBUG_ARGSCALL
);
440 if (prompt
!= NULL
&& *prompt
!= '\0') {
441 fputs(prompt
, stdout
);
444 n
= (readline_restart
)(ifile
, linebuf
, linesize
, n
445 SMALLOC_DEBUG_ARGSCALL
);
451 * An unquoted <backslash> at the end of a command line
452 * shall be discarded and the next line shall continue the
455 if ((lned
& LNED_LF_ESC
) && (*linebuf
)[n
- 1] == '\\') {
456 (*linebuf
)[--n
] = '\0';
457 if (prompt
!= NULL
&& *prompt
!= '\0')
458 prompt
= ".. "; /* XXX PS2 .. */
461 if (dotty
&& (lned
& LNED_HIST_ADD
))
462 tty_addhist(*linebuf
);
469 readstr_input(char const *prompt
, char const *string
) /* FIXME SIGS<->leaks */
471 /* TODO readstr_input(): linebuf pool */
472 size_t linesize
= 0, slen
;
473 char *linebuf
= NULL
, *rv
= NULL
;
474 bool_t doprompt
, dotty
;
476 doprompt
= (!sourcing
&& (options
& OPT_INTERACTIVE
));
477 dotty
= (doprompt
&& !boption("line-editor-disable"));
480 else if (prompt
== NULL
)
481 prompt
= getprompt();
483 /* If STDIN is not a terminal, simply read from it */
485 slen
= (string
!= NULL
) ? strlen(string
) : 0;
487 linesize
= slen
+ LINESIZE
+ 1;
488 linebuf
= smalloc_safe(linesize
);
490 memcpy(linebuf
, string
, slen
+ 1);
492 if (tty_readline(prompt
, &linebuf
, &linesize
, slen
) >= 0)
495 if (prompt
!= NULL
&& *prompt
!= '\0') {
496 fputs(prompt
, stdout
);
501 if (readline_restart(stdin
, &linebuf
, &linesize
, slen
) >= 0)
506 rv
= (*rv
== '\0') ? NULL
: savestr(rv
);
512 * Set up the input pointers while copying the mail file into /tmp.
515 setptr(FILE *ibuf
, off_t offset
)
518 char *cp
, *linebuf
= NULL
;
521 int maybe
, inhead
, thiscnt
;
522 size_t linesize
= 0, filesize
, cnt
;
527 memset(&this, 0, sizeof this);
528 this.m_flag
= MUSED
|MNEW
|MNEWEST
;
529 filesize
= mailsize
- offset
;
530 offset
= ftell(mb
.mb_otf
);
532 if (fgetline(&linebuf
, &linesize
, &filesize
, &cnt
, ibuf
, 0)
534 this.m_xsize
= this.m_size
;
535 this.m_xlines
= this.m_lines
;
536 this.m_have
= HAVE_HEADER
|HAVE_BODY
;
545 if (linebuf
[0] == '\0')
548 /* XXX Convert CRLF to LF; this should be rethought in that
549 * XXX CRLF input should possibly end as CRLF output? */
550 if (cnt
>= 2 && linebuf
[cnt
- 1] == '\n' &&
551 linebuf
[cnt
- 2] == '\r')
552 linebuf
[--cnt
- 1] = '\n';
553 fwrite(linebuf
, sizeof *linebuf
, cnt
, mb
.mb_otf
);
554 if (ferror(mb
.mb_otf
)) {
558 if (linebuf
[cnt
- 1] == '\n')
559 linebuf
[cnt
- 1] = '\0';
560 if (maybe
&& linebuf
[0] == 'F' && is_head(linebuf
, cnt
)) {
562 * TODO char date[FROM_DATEBUF];
563 * TODO extract_date_from_from_(linebuf, cnt, date);
564 * TODO this.m_time = 10000;
566 this.m_xsize
= this.m_size
;
567 this.m_xlines
= this.m_lines
;
568 this.m_have
= HAVE_HEADER
|HAVE_BODY
;
572 this.m_flag
= MUSED
|MNEW
|MNEWEST
;
575 this.m_block
= mailx_blockof(offset
);
576 this.m_offset
= mailx_offsetof(offset
);
578 } else if (linebuf
[0] == 0) {
581 for (cp
= linebuf
, cp2
= "status";; cp
++) {
582 if ((c
= *cp2
++) == 0) {
583 while (c
= *cp
++, whitechar(c
));
586 while ((c
= *cp
++) != '\0')
588 this.m_flag
|= MREAD
;
590 this.m_flag
&= ~MNEW
;
593 if (*cp
!= c
&& *cp
!= upperconv(c
))
596 for (cp
= linebuf
, cp2
= "x-status";; cp
++) {
597 if ((c
= *cp2
++) == 0) {
598 while (c
= *cp
++, whitechar(c
));
601 while ((c
= *cp
++) != '\0')
603 this.m_flag
|= MFLAGGED
;
605 this.m_flag
|=MANSWERED
;
607 this.m_flag
|=MDRAFTED
;
610 if (*cp
!= c
&& *cp
!= upperconv(c
))
617 maybe
= linebuf
[0] == 0;
623 * Drop the passed line onto the passed output buffer.
624 * If a write error occurs, return -1, else the count of
625 * characters written, including the newline.
628 putline(FILE *obuf
, char *linebuf
, size_t cnt
)
630 fwrite(linebuf
, sizeof *linebuf
, cnt
, obuf
);
638 * Return a file buffer all ready to read up the
639 * passed message pointer.
642 setinput(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
648 if (m
->m_have
& HAVE_HEADER
)
654 if (m
->m_have
& HAVE_BODY
)
666 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
,
667 m
->m_offset
), SEEK_SET
) < 0) {
669 panic(tr(77, "temporary file seek"));
675 setdot(struct message
*mp
)
679 did_print_dot
= FAL0
;
687 * Take the data out of the passed ghost file and toss it into
688 * a dynamically allocated message structure.
696 message
[msgCount
].m_size
= 0;
697 message
[msgCount
].m_lines
= 0;
701 * Append the passed message descriptor onto the message structure.
704 _fio_append(struct message
*mp
)
706 if (msgCount
+ 1 >= msgspace
)
707 message
= srealloc(message
, (msgspace
+= 64) * sizeof *message
);
709 message
[msgCount
- 1] = *mp
;
713 * Delete a file, but only if the file is a plain file.
716 rm(char *name
) /* TODO TOCTOU; but i'm out of ideas today */
721 if (stat(name
, &sb
) < 0)
723 else if (! S_ISREG(sb
.st_mode
))
731 * Determine the size of the file possessed by
739 if (fstat(fileno(iob
), &sbuf
) < 0)
745 fexpand(char const *name
, enum fexp_mode fexpm
)
747 char cbuf
[MAXPATHLEN
], *res
;
753 * The order of evaluation is "%" and "#" expand into constants.
754 * "&" can expand into "+". "+" can expand into shell meta characters.
755 * Shell meta characters expand into constants.
756 * This way, we make no recursive expansion.
759 if (! (fexpm
& FEXP_NSHORTCUT
) && (sh
= get_shortcut(res
)) != NULL
)
762 if (fexpm
& FEXP_SHELL
) {
770 if (res
[1] == ':' && res
[2] != '\0') {
774 _findmail(cbuf
, sizeof cbuf
,
775 (res
[1] != '\0') ? res
+ 1 : myname
,
776 (res
[1] != '\0' || (options
& OPT_u_FLAG
)));
782 if (prevfile
[0] == '\0') {
783 fprintf(stderr
, tr(80, "No previous file\n"));
790 if (res
[1] == '\0') {
791 if ((res
= value("MBOX")) == NULL
)
792 res
= UNCONST("~/mbox");
793 else if (res
[0] != '&' || res
[1] != '\0')
799 if (res
[0] == '@' && which_protocol(mailname
) == PROTO_IMAP
) {
800 res
= str_concat_csvl(&s
,
801 protbase(mailname
), "/", res
+ 1, NULL
)->s
;
805 if (res
[0] == '+' && getfold(cbuf
, sizeof cbuf
)) {
806 size_t i
= strlen(cbuf
);
808 res
= str_concat_csvl(&s
, cbuf
,
809 (i
> 0 && cbuf
[i
- 1] == '/') ? "" : "/",
813 if (res
[0] == '%' && res
[1] == ':') {
819 /* Catch the most common shell meta character */
821 if (res
[0] == '~' && (res
[1] == '/' || res
[1] == '\0')) {
822 res
= str_concat_csvl(&s
, homedir
, res
+ 1, NULL
)->s
;
826 if (anyof(res
, "|&;<>~{}()[]*?$`'\"\\") &&
827 which_protocol(res
) == PROTO_FILE
) {
828 res
= _globname(res
, fexpm
);
834 if (fexpm
& FEXP_LOCAL
)
835 switch (which_protocol(res
)) {
837 case PROTO_MAILDIR
: /* XXX Really? ok MAILDIR for local? */
840 fprintf(stderr
, tr(280,
841 "`%s': only a local file or directory may "
856 if (value("keep") != NULL
|| rm(mailname
) < 0) {
857 int fd
= open(mailname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
864 var_folder_updated(char const *name
, char **store
)
867 char *folder
, *unres
= NULL
, *res
= NULL
;
869 if ((folder
= UNCONST(name
)) == NULL
)
872 /* Expand the *folder*; skip `%:' prefix for simplicity of use */
873 /* XXX This *only* works because we do NOT
874 * XXX update environment variables via the "set" mechanism */
875 if (folder
[0] == '%' && folder
[1] == ':')
877 if ((folder
= fexpand(folder
, FEXP_FULL
)) == NULL
) /* XXX error? */
880 switch (which_protocol(folder
)) {
882 /* Ooops. This won't work */
883 fprintf(stderr
, tr(501, "`folder' cannot be set to a flat, "
884 "readonly POP3 account\n"));
888 /* Simply assign what we have, even including `%:' prefix */
893 /* Further expansion desired */
897 /* All non-absolute paths are relative to our home directory */
898 if (*folder
!= '/') {
899 size_t l1
= strlen(homedir
), l2
= strlen(folder
);
900 unres
= ac_alloc(l1
+ l2
+ 2);
901 memcpy(unres
, homedir
, l1
);
903 memcpy(unres
+ l1
+ 1, folder
, l2
);
904 unres
[l1
+ 1 + l2
] = '\0';
908 /* Since lex.c:_update_mailname() uses realpath(3) if available to
909 * avoid that we loose track of our currently open folder in case we
910 * chdir away, but still checks the leading path portion against
911 * getfold() to be able to abbreviate to the +FOLDER syntax if
912 * possible, we need to realpath(3) the folder, too */
914 res
= ac_alloc(MAXPATHLEN
);
915 if (realpath(folder
, res
) == NULL
)
916 fprintf(stderr
, tr(151, "Can't canonicalize `%s'\n"), folder
);
922 *store
= sstrdup(folder
);
933 getfold(char *name
, size_t size
)
937 if ((folder
= value("folder")) != NULL
)
938 (void)n_strlcpy(name
, folder
, size
);
939 return (folder
!= NULL
);
943 * Return the name of the dead.letter file.
950 if ((cp
= value("DEAD")) == NULL
||
951 (cp
= fexpand(cp
, FEXP_LOCAL
)) == NULL
)
952 cp
= fexpand("~/dead.letter", FEXP_LOCAL
|FEXP_SHELL
);
953 else if (*cp
!= '/') {
954 size_t sz
= strlen(cp
) + 3;
955 char *buf
= ac_alloc(sz
);
957 snprintf(buf
, sz
, "~/%s", cp
);
958 cp
= fexpand(buf
, FEXP_LOCAL
|FEXP_SHELL
);
967 get_header(struct message
*mp
)
970 switch (mb
.mb_type
) {
976 return (pop3_header(mp
));
981 return imap_header(mp
);
990 get_body(struct message
*mp
)
993 switch (mb
.mb_type
) {
999 return (pop3_body(mp
));
1004 return imap_body(mp
);
1013 static long xwrite(int fd
, const char *data
, size_t sz
);
1016 xwrite(int fd
, const char *data
, size_t sz
)
1022 if ((wo
= write(fd
, data
+ wt
, sz
- wt
)) < 0) {
1034 sclose(struct sock
*sp
)
1039 if (sp
->s_onclose
!= NULL
)
1042 if (sp
->s_use_ssl
) {
1044 SSL_shutdown(sp
->s_ssl
);
1045 SSL_free(sp
->s_ssl
);
1047 SSL_CTX_free(sp
->s_ctx
);
1052 i
= close(sp
->s_fd
);
1062 swrite(struct sock
*sp
, const char *data
)
1064 return swrite1(sp
, data
, strlen(data
), 0);
1068 swrite1(struct sock
*sp
, const char *data
, int sz
, int use_buffer
)
1072 if (use_buffer
> 0) {
1076 if (sp
->s_wbuf
== NULL
) {
1077 sp
->s_wbufsize
= 4096;
1078 sp
->s_wbuf
= smalloc(sp
->s_wbufsize
);
1081 while (sp
->s_wbufpos
+ sz
> sp
->s_wbufsize
) {
1082 di
= sp
->s_wbufsize
- sp
->s_wbufpos
;
1084 if (sp
->s_wbufpos
> 0) {
1085 memcpy(&sp
->s_wbuf
[sp
->s_wbufpos
], data
, di
);
1086 ok
= swrite1(sp
, sp
->s_wbuf
,
1087 sp
->s_wbufsize
, -1);
1089 ok
= swrite1(sp
, data
,
1090 sp
->s_wbufsize
, -1);
1096 if (sz
== sp
->s_wbufsize
) {
1097 ok
= swrite1(sp
, data
, sp
->s_wbufsize
, -1);
1101 memcpy(&sp
->s_wbuf
[sp
->s_wbufpos
], data
, sz
);
1102 sp
->s_wbufpos
+= sz
;
1105 } else if (use_buffer
== 0 && sp
->s_wbuf
!= NULL
&&
1106 sp
->s_wbufpos
> 0) {
1109 if (swrite1(sp
, sp
->s_wbuf
, x
, -1) != OKAY
)
1115 if (sp
->s_use_ssl
) {
1116 ssl_retry
: x
= SSL_write(sp
->s_ssl
, data
, sz
);
1118 switch (SSL_get_error(sp
->s_ssl
, x
)) {
1119 case SSL_ERROR_WANT_READ
:
1120 case SSL_ERROR_WANT_WRITE
:
1127 x
= xwrite(sp
->s_fd
, data
, sz
);
1131 snprintf(o
, sizeof o
, "%s write error",
1132 sp
->s_desc
? sp
->s_desc
: "socket");
1134 sp
->s_use_ssl
? ssl_gen_err("%s", o
) : perror(o
);
1146 sopen(const char *xserver
, struct sock
*sp
, int use_ssl
,
1147 const char *uhp
, const char *portstr
, int verbose
)
1149 #ifdef HAVE_SO_SNDTIMEO
1152 #ifdef HAVE_SO_LINGER
1156 char hbuf
[NI_MAXHOST
];
1157 struct addrinfo hints
, *res0
, *res
;
1159 struct sockaddr_in servaddr
;
1160 struct in_addr
**pptr
;
1163 unsigned short port
= 0;
1167 char *server
= UNCONST(xserver
);
1171 if ((cp
= strchr(server
, ':')) != NULL
) { /* TODO URI parse! IPv6! */
1174 port
= strtol(portstr
, NULL
, 10);
1176 server
= salloc(cp
- xserver
+ 1);
1177 memcpy(server
, xserver
, cp
- xserver
);
1178 server
[cp
- xserver
] = '\0';
1181 /* Connect timeouts after 30 seconds */
1182 #ifdef HAVE_SO_SNDTIMEO
1189 fprintf(stderr
, "Resolving host %s . . .", server
);
1190 memset(&hints
, 0, sizeof hints
);
1191 hints
.ai_socktype
= SOCK_STREAM
;
1192 if (getaddrinfo(server
, portstr
, &hints
, &res0
) != 0) {
1193 fprintf(stderr
, tr(252, " lookup of `%s' failed.\n"), server
);
1196 fprintf(stderr
, tr(500, " done.\n"));
1199 for (res
= res0
; res
!= NULL
&& sockfd
< 0; res
= res
->ai_next
) {
1201 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
1202 hbuf
, sizeof hbuf
, NULL
, 0,
1203 NI_NUMERICHOST
) != 0)
1204 strcpy(hbuf
, "unknown host");
1205 fprintf(stderr
, tr(192,
1206 "%sConnecting to %s:%s . . ."),
1207 (res
== res0
) ? "" : "\n",
1210 if ((sockfd
= socket(res
->ai_family
, res
->ai_socktype
,
1211 res
->ai_protocol
)) >= 0) {
1212 # ifdef HAVE_SO_SNDTIMEO
1213 setsockopt(sockfd
, SOL_SOCKET
, SO_SNDTIMEO
,
1216 if (connect(sockfd
, res
->ai_addr
, res
->ai_addrlen
)!=0) {
1223 perror(tr(254, " could not connect"));
1229 #else /* HAVE_IPV6 */
1231 if (strcmp(portstr
, "smtp") == 0)
1233 else if (strcmp(portstr
, "smtps") == 0)
1236 else if (strcmp(portstr
, "imap") == 0)
1238 else if (strcmp(portstr
, "imaps") == 0)
1242 else if (strcmp(portstr
, "pop3") == 0)
1244 else if (strcmp(portstr
, "pop3s") == 0)
1247 else if ((ep
= getservbyname(UNCONST(portstr
), "tcp")) != NULL
)
1250 fprintf(stderr
, tr(251, "Unknown service: %s\n"),
1258 fprintf(stderr
, "Resolving host %s . . .", server
);
1259 if ((hp
= gethostbyname(server
)) == NULL
) {
1260 fprintf(stderr
, tr(252, " lookup of `%s' failed.\n"), server
);
1263 fprintf(stderr
, tr(500, " done.\n"));
1265 pptr
= (struct in_addr
**)hp
->h_addr_list
;
1266 if ((sockfd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
1267 perror(tr(253, "could not create socket"));
1270 memset(&servaddr
, 0, sizeof servaddr
);
1271 servaddr
.sin_family
= AF_INET
;
1272 servaddr
.sin_port
= port
;
1273 memcpy(&servaddr
.sin_addr
, *pptr
, sizeof(struct in_addr
));
1275 fprintf(stderr
, tr(192, "%sConnecting to %s:%d . . ."),
1276 "", inet_ntoa(**pptr
), ntohs(port
));
1278 # ifdef HAVE_SO_SNDTIMEO
1279 setsockopt(sockfd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
1281 if (connect(sockfd
, (struct sockaddr
*)&servaddr
, sizeof servaddr
)
1283 perror(tr(254, " could not connect"));
1286 #endif /* HAVE_IPV6 */
1288 fputs(tr(193, " connected.\n"), stderr
);
1290 /* And the regular timeouts */
1291 #ifdef HAVE_SO_SNDTIMEO
1294 setsockopt(sockfd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
1295 setsockopt(sockfd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof tv
);
1297 #ifdef HAVE_SO_LINGER
1300 setsockopt(sockfd
, SOL_SOCKET
, SO_LINGER
, &li
, sizeof li
);
1303 memset(sp
, 0, sizeof *sp
);
1306 if (use_ssl
&& ssl_open(server
, sp
, uhp
) != OKAY
) {
1315 (sgetline
)(char **line
, size_t *linesize
, size_t *linelen
, struct sock
*sp
1320 if (sp
->s_rsz
< 0) {
1325 if (*line
== NULL
|| lp
> &(*line
)[*linesize
- 128]) {
1326 size_t diff
= lp
- *line
;
1327 *line
= (srealloc
)(*line
, *linesize
+= 256
1328 SMALLOC_DEBUG_ARGSCALL
);
1329 lp
= &(*line
)[diff
];
1331 if (sp
->s_rbufptr
== NULL
||
1332 sp
->s_rbufptr
>= &sp
->s_rbuf
[sp
->s_rsz
]) {
1334 if (sp
->s_use_ssl
) {
1335 ssl_retry
: if ((sp
->s_rsz
= SSL_read(sp
->s_ssl
,
1337 sizeof sp
->s_rbuf
)) <= 0) {
1338 if (sp
->s_rsz
< 0) {
1340 switch(SSL_get_error(sp
->s_ssl
,
1342 case SSL_ERROR_WANT_READ
:
1343 case SSL_ERROR_WANT_WRITE
:
1346 snprintf(o
, sizeof o
, "%s",
1350 ssl_gen_err("%s", o
);
1358 again
: if ((sp
->s_rsz
= read(sp
->s_fd
, sp
->s_rbuf
,
1359 sizeof sp
->s_rbuf
)) <= 0) {
1360 if (sp
->s_rsz
< 0) {
1364 snprintf(o
, sizeof o
, "%s",
1373 sp
->s_rbufptr
= sp
->s_rbuf
;
1375 } while ((*lp
++ = *sp
->s_rbufptr
++) != '\n');
1378 *linelen
= lp
- *line
;
1381 #endif /* HAVE_SOCKETS */
1384 load(char const *name
)
1388 if (name
== NULL
|| (in
= Fopen(name
, "r")) == NULL
)
1409 if ((cp
= fexpand(*arglist
, FEXP_LOCAL
)) == NULL
)
1411 if ((fi
= Fopen(cp
, "r")) == NULL
) {
1415 if (_ssp
>= SSTACK
- 1) {
1416 fprintf(stderr
, tr(3, "Too much \"sourcing\" going on.\n"));
1421 _sstack
[_ssp
].s_file
= _input
;
1422 _sstack
[_ssp
].s_cond
= cond
;
1423 _sstack
[_ssp
].s_loading
= loading
;
1440 fprintf(stderr
, tr(4, "\"Source\" stack over-pop.\n"));
1447 fprintf(stderr
, tr(5, "Unmatched \"if\"\n"));
1449 cond
= _sstack
[_ssp
].s_cond
;
1450 loading
= _sstack
[_ssp
].s_loading
;
1451 _input
= _sstack
[_ssp
].s_file
;