1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ File I/O, including resource file loading etc.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifndef HAVE_AMALGAMATION
45 # include <sys/socket.h>
49 # include <netinet/in.h>
51 # ifdef HAVE_ARPA_INET_H
52 # include <arpa/inet.h>
61 # include <openssl/err.h>
62 # include <openssl/rand.h>
63 # include <openssl/ssl.h>
64 # include <openssl/x509v3.h>
65 # include <openssl/x509.h>
73 FILE *s_file
; /* File we were in. */
74 void *s_cond
; /* Saved state of conditional stack */
75 int s_loading
; /* Loading .mailrc, etc. */
78 /* Slots in ::message */
79 static size_t _message_space
;
81 /* XXX Our Popen() main() takes void, temporary global data store */
83 static enum file_lock_type _dotlock_flt
;
84 static int _dotlock_fd
;
85 struct dotlock_info
* _dotlock_dip
;
89 static struct fio_stack _fio_stack
[FIO_STACK_SIZE
];
90 static size_t _fio_stack_size
;
91 static FILE * _fio_input
;
93 /* Locate the user's mailbox file (where new, unread mail is queued) */
94 static void _findmail(char *buf
, size_t bufsize
, char const *user
,
97 /* Perform shell meta character expansion TODO obsolete (INSECURE!) */
98 static char * _globname(char const *name
, enum fexp_mode fexpm
);
100 /* line is a buffer with the result of fgets(). Returns the first newline or
101 * the last character read */
102 static size_t _length_of_line(char const *line
, size_t linesize
);
104 /* Read a line, one character at a time */
105 static char * _fgetline_byone(char **line
, size_t *linesize
, size_t *llen
,
106 FILE *fp
, int appendnl
, size_t n SMALLOC_DEBUG_ARGS
);
108 /* Take the data out of the passed ghost file and toss it into a dynamically
109 * allocated message structure */
110 static void makemessage(void);
112 static enum okay
get_header(struct message
*mp
);
115 static bool_t
_file_lock(int fd
, enum file_lock_type ft
,
116 off_t off
, off_t len
);
118 /* main() of fork(2)ed dot file locker */
120 static int _dotlock_main(void);
123 /* Write to socket fd, restarting on EINTR, unless anything is written */
125 static long xwrite(int fd
, char const *data
, size_t sz
);
128 /* `source' and `source_if' */
129 static bool_t
_source_file(char const *file
, bool_t silent_error
);
132 _findmail(char *buf
, size_t bufsize
, char const *user
, bool_t force
)
137 if (!strcmp(user
, myname
) && !force
&& (cp
= ok_vlook(folder
)) != NULL
) {
138 switch (which_protocol(cp
)) {
140 if (strcmp(cp
, protbase(cp
)))
142 snprintf(buf
, bufsize
, "%s/INBOX", cp
);
149 if (force
|| (cp
= ok_vlook(MAIL
)) == NULL
)
150 snprintf(buf
, bufsize
, "%s/%s", MAILSPOOL
, user
);
153 n_strlcpy(buf
, cp
, bufsize
);
159 _globname(char const *name
, enum fexp_mode fexpm
)
168 /* Mac OS X Snow Leopard and Linux don't init fields on error, causing
169 * SIGSEGV in wordfree(3); so let's just always zero it ourselfs */
170 memset(&we
, 0, sizeof we
);
172 /* Some systems (notably Open UNIX 8.0.0) fork a shell for wordexp()
173 * and wait, which will fail if our SIGCHLD handler is active */
175 sigaddset(&nset
, SIGCHLD
);
176 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
178 # define WRDE_NOCMD 0
180 i
= wordexp(name
, &we
, WRDE_NOCMD
);
181 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
188 if (!(fexpm
& FEXP_SILENT
))
189 n_err(_("\"%s\": Command substitution not allowed\n"), name
);
193 if (!(fexpm
& FEXP_SILENT
))
194 n_err(_("\"%s\": Expansion buffer overflow\n"), name
);
199 if (!(fexpm
& FEXP_SILENT
))
200 n_err(_("Syntax error in \"%s\"\n"), name
);
204 switch (we
.we_wordc
) {
206 cp
= savestr(we
.we_wordv
[0]);
209 if (!(fexpm
& FEXP_SILENT
))
210 n_err(_("\"%s\": No match\n"), name
);
213 if (fexpm
& FEXP_MULTIOK
) {
216 for (l
= 0, j
= 0; j
< we
.we_wordc
; ++j
)
217 l
+= strlen(we
.we_wordv
[j
]) + 1;
220 for (l
= 0, j
= 0; j
< we
.we_wordc
; ++j
) {
221 size_t x
= strlen(we
.we_wordv
[j
]);
222 memcpy(cp
+ l
, we
.we_wordv
[j
], x
);
227 } else if (!(fexpm
& FEXP_SILENT
))
228 n_err(_("\"%s\": Ambiguous\n"), name
);
236 #else /* HAVE_WORDEXP */
238 char xname
[PATH_MAX
+1], cmdbuf
[PATH_MAX
+1], /* also used for files */
240 int pivec
[2], pid
, l
, waits
;
243 if (pipe(pivec
) < 0) {
244 n_perr(_("pipe"), 0);
247 snprintf(cmdbuf
, sizeof cmdbuf
, "echo %s", name
);
248 if ((shellp
= ok_vlook(SHELL
)) == NULL
)
249 shellp
= UNCONST(XSHELL
);
250 pid
= start_command(shellp
, NULL
, COMMAND_FD_NULL
, pivec
[1],
251 "-c", cmdbuf
, NULL
, NULL
);
260 l
= read(pivec
[0], xname
, sizeof xname
);
264 n_perr(_("read"), 0);
269 if (!wait_child(pid
, &waits
) && WTERMSIG(waits
) != SIGPIPE
) {
270 if (!(fexpm
& FEXP_SILENT
))
271 n_err(_("\"%s\": Expansion failed\n"), name
);
275 if (!(fexpm
& FEXP_SILENT
))
276 n_err(_("\"%s\": No match\n"), name
);
279 if (l
== sizeof xname
) {
280 if (!(fexpm
& FEXP_SILENT
))
281 n_err(_("\"%s\": Expansion buffer overflow\n"), name
);
285 for (cp
= xname
+ l
- 1; *cp
== '\n' && cp
> xname
; --cp
)
288 if (!(fexpm
& FEXP_MULTIOK
) && strchr(xname
, ' ') != NULL
&&
289 stat(xname
, &sbuf
) < 0) {
290 if (!(fexpm
& FEXP_SILENT
))
291 n_err(_("\"%s\": Ambiguous\n"), name
);
299 #endif /* !HAVE_WORDEXP */
303 _length_of_line(char const *line
, size_t linesize
)
308 /* Last character is always '\0' and was added by fgets() */
309 for (--linesize
, i
= 0; i
< linesize
; i
++)
312 i
= (i
< linesize
) ? i
+ 1 : linesize
;
318 _fgetline_byone(char **line
, size_t *linesize
, size_t *llen
, FILE *fp
,
319 int appendnl
, size_t n SMALLOC_DEBUG_ARGS
)
325 assert(*linesize
== 0 || *line
!= NULL
);
327 if (*linesize
<= LINESIZE
|| n
>= *linesize
- 128) {
328 *linesize
+= ((rv
== NULL
) ? LINESIZE
+ n
+ 1 : 256);
329 *line
= rv
= (srealloc
)(rv
, *linesize SMALLOC_DEBUG_ARGSCALL
);
362 message_append(NULL
);
364 message
[msgCount
].m_size
= 0;
365 message
[msgCount
].m_lines
= 0;
370 get_header(struct message
*mp
)
376 switch (mb
.mb_type
) {
383 rv
= pop3_header(mp
);
389 rv
= imap_header(mp
);
402 _file_lock(int fd
, enum file_lock_type flt
, off_t off
, off_t len
)
408 memset(&flp
, 0, sizeof flp
);
412 case FLT_READ
: rv
= F_RDLCK
; break;
413 case FLT_WRITE
: rv
= F_WRLCK
; break;
417 flp
.l_whence
= SEEK_SET
;
420 rv
= (fcntl(fd
, F_SETLK
, &flp
) != -1);
429 /* Use PATH_MAX not NAME_MAX to catch those "we proclaim the minimum value"
430 * problems (SunOS), since the pathconf(3) value comes too late! */
431 char name
[PATH_MAX
+1];
432 struct dotlock_info di
;
433 struct stat stb
, fdstb
;
434 enum dotlock_state dls
;
437 enum file_lock_type flt
;
440 /* Ignore SIGPIPE, we'll see EPIPE and "fall through" */
441 safe_signal(SIGPIPE
, SIG_IGN
);
443 /* Get the arguments "passed to us" */
451 dls
= DLS_CANT_CHDIR
| DLS_ABANDON
;
453 if ((cp
= strrchr(di
.di_file_name
, '/')) != NULL
) {
454 char const *fname
= cp
+ 1;
456 while (PTRCMP(cp
- 1, >, di
.di_file_name
) && cp
[-1] == '/')
458 cp
= savestrbuf(di
.di_file_name
, PTR2SIZE(cp
- di
.di_file_name
));
462 di
.di_file_name
= fname
;
465 /* So we're here, but then again the file can be a symbolic link!
466 * This is however only true if we do not have realpath(3) available since
467 * that'll have resolved the path already otherwise; nonetheless, let
468 * readlink(2) be a precondition for dotlocking and keep this code */
469 if (lstat(cp
= di
.di_file_name
, &stb
) == -1)
471 if (S_ISLNK(stb
.st_mode
)) {
472 /* Use salloc() and hope we stay in builtin buffer.. */
477 for (x
= NULL
, i
= PATH_MAX
;; i
+= PATH_MAX
) {
479 sr
= readlink(cp
, x
, i
);
481 dls
= DLS_FISHY
| DLS_ABANDON
;
484 if (UICMP(z
, sr
, <, i
)) {
494 dls
= DLS_FISHY
| DLS_ABANDON
;
496 /* Bail out if the file has changed its identity in the meanwhile */
497 if (fstat(fd
, &fdstb
) == -1 ||
498 fdstb
.st_dev
!= stb
.st_dev
|| fdstb
.st_ino
!= stb
.st_ino
||
499 fdstb
.st_uid
!= stb
.st_uid
|| fdstb
.st_gid
!= stb
.st_gid
||
500 fdstb
.st_mode
!= stb
.st_mode
)
503 /* Be aware, even if the error is false! Note the shared code in dotlock.h
504 * *requires* that it is possible to create a filename at least one byte
505 * longer than di_lock_name! */
506 do/* while(0) breaker */{
507 # ifdef HAVE_PATHCONF
510 int i
= snprintf(name
, sizeof name
, "%s.lock", di
.di_file_name
);
512 /* fd is a file, not portable to use for _PC_NAME_MAX */
515 dls
= DLS_NAMETOOLONG
| DLS_ABANDON
;
518 # ifdef HAVE_PATHCONF
520 if((pc
= pathconf(".", _PC_NAME_MAX
)) == -1){
521 /* errno unchanged: no limit */
524 }else if(pc
- 1 >= (long)i
)
529 if(UICMP(z
, NAME_MAX
- 1, <, (uiz_t
)i
))
533 di
.di_lock_name
= name
;
535 /* We are in the directory of the mailbox for which we have to create
536 * a dotlock file for. We don't know wether we have realpath(3) available,
537 * and manually resolving the path is due especially given that S-nail
538 * supports the special "%:" syntax to warp any file into a "system
539 * mailbox"; there may also be multiple system mailbox directories...
540 * So what we do is that we fstat(2) the mailbox and check its UID and
541 * GID against that of our own process: if any of those mismatch we must
542 * either assume a directory we are not allowed to write in, or that we run
543 * via -u/$USER/%USER as someone else, in which case we favour our
544 * privilege-separated dotlock process */
545 assert(cp
!= NULL
); /* Ugly: avoid a useless var and reuse that one */
546 if (access(".", W_OK
)) {
547 /* This may however also indicate a read-only filesystem, which is not
548 * really an error from our point of view since the mailbox will degrade
549 * to a readonly one for which no dotlock is needed, then, and errors
550 * may arise only due to actions which require box modifications */
551 if (errno
== EROFS
) {
552 dls
= DLS_ROFS
| DLS_ABANDON
;
557 if (cp
== NULL
|| stb
.st_uid
!= user_id
|| stb
.st_gid
!= group_id
) {
559 char const *args
[13];
561 snprintf(itoabuf
, sizeof itoabuf
, "%" PRIuZ
, di
.di_pollmsecs
);
563 args
[ 1] = (flt
== FLT_READ
? "rdotlock" : "wdotlock");
564 args
[ 2] = "mailbox"; args
[ 3] = di
.di_file_name
;
565 args
[ 4] = "name"; args
[ 5] = di
.di_lock_name
;
566 args
[ 6] = "hostname"; args
[ 7] = di
.di_hostname
;
567 args
[ 8] = "randstr"; args
[ 9] = di
.di_randstr
;
568 args
[10] = "pollmsecs"; args
[11] = itoabuf
;
570 execv(LIBEXECDIR
"/" UAGENT
"-privsep", UNCONST(args
));
573 write(STDOUT_FILENO
, &dls
, sizeof dls
);
574 /* But fall through and try it with normal privileges! */
577 /* So let's try and call it ourselfs! Note that we don't block signals just
578 * like our privsep child does, the user will anyway be able to remove his
579 * file again, and if we're in -u/$USER mode then we are allowed to access
580 * the user's box: shall we leave behind a stale dotlock then at least we
581 * start a friendly human conversation. Since we cannot handle SIGKILL and
582 * SIGSTOP malicious things could happen whatever we do */
583 safe_signal(SIGHUP
, SIG_IGN
);
584 safe_signal(SIGINT
, SIG_IGN
);
585 safe_signal(SIGQUIT
, SIG_IGN
);
586 safe_signal(SIGTERM
, SIG_IGN
);
589 dls
= _dotlock_create(&di
);
592 /* Finally: notify our parent about the actual lock state.. */
594 write(STDOUT_FILENO
, &dls
, sizeof dls
);
595 close(STDOUT_FILENO
);
597 /* ..then eventually wait until we shall remove the lock again, which will
598 * be notified via the read returning */
599 if (dls
== DLS_NONE
) {
600 read(STDIN_FILENO
, &dls
, sizeof dls
);
607 #endif /* HAVE_DOTLOCK */
611 xwrite(int fd
, char const *data
, size_t sz
)
618 if ((wo
= write(fd
, data
+ wt
, sz
- wt
)) < 0) {
631 #endif /* HAVE_SOCKETS */
634 _source_file(char const *file
, bool_t silent_error
)
640 if ((cp
= fexpand(file
, FEXP_LOCAL
)) == NULL
)
642 if ((fi
= Fopen(cp
, "r")) == NULL
) {
643 if (!silent_error
|| (options
& OPT_D_V
))
648 if (temporary_localopts_store
!= NULL
) {
649 n_err(_("Before v15 you cannot `source' from within macros, sorry\n"));
652 if (_fio_stack_size
>= NELEM(_fio_stack
)) {
653 n_err(_("Too many `source' recursions\n"));
660 _fio_stack
[_fio_stack_size
].s_file
= _fio_input
;
661 _fio_stack
[_fio_stack_size
].s_cond
= condstack_release();
662 _fio_stack
[_fio_stack_size
].s_loading
= !!(pstate
& PS_LOADING
);
664 pstate
&= ~PS_LOADING
;
665 pstate
|= PS_SOURCING
;
673 (fgetline
)(char **line
, size_t *linesize
, size_t *cnt
, size_t *llen
, FILE *fp
,
674 int appendnl SMALLOC_DEBUG_ARGS
)
681 /* Without count, we can't determine where the chars returned by fgets()
682 * end if there's no newline. We have to read one character by one */
683 rv
= _fgetline_byone(line
, linesize
, llen
, fp
, appendnl
, 0
684 SMALLOC_DEBUG_ARGSCALL
);
688 if ((rv
= *line
) == NULL
|| *linesize
< LINESIZE
)
689 *line
= rv
= (srealloc
)(rv
, *linesize
= LINESIZE SMALLOC_DEBUG_ARGSCALL
);
690 sz
= (*linesize
<= *cnt
) ? *linesize
: *cnt
+ 1;
691 if (sz
<= 1 || fgets(rv
, sz
, fp
) == NULL
) {
692 /* Leave llen untouched; it is used to determine whether the last line
693 * was \n-terminated in some callers */
698 i_llen
= _length_of_line(rv
, sz
);
700 while (rv
[i_llen
- 1] != '\n') {
701 *line
= rv
= (srealloc
)(rv
, *linesize
+= 256 SMALLOC_DEBUG_ARGSCALL
);
702 sz
= *linesize
- i_llen
;
703 sz
= (sz
<= *cnt
) ? sz
: *cnt
+ 1;
704 if (sz
<= 1 || fgets(rv
+ i_llen
, sz
, fp
) == NULL
) {
711 sz
= _length_of_line(rv
+ i_llen
, sz
);
723 (readline_restart
)(FILE *ibuf
, char **linebuf
, size_t *linesize
, size_t n
726 /* TODO readline_restart(): always *appends* LF just to strip it again;
727 * TODO should be configurable just as for fgetline(); ..or whatever.. */
734 /* Interrupts will cause trouble if we are inside a stdio call. As this is
735 * only relevant if input is from tty, bypass it by read(), then */
736 if (fileno(ibuf
) == 0 && (options
& OPT_TTYIN
)) {
737 assert(*linesize
== 0 || *linebuf
!= NULL
);
739 if (*linesize
<= LINESIZE
|| n
>= *linesize
- 128) {
740 *linesize
+= ((*linebuf
== NULL
) ? LINESIZE
+ n
+ 1 : 256);
741 *linebuf
= (srealloc
)(*linebuf
, *linesize SMALLOC_DEBUG_ARGSCALL
);
744 sz
= read(0, *linebuf
+ n
, *linesize
- n
- 1);
747 (*linebuf
)[n
] = '\0';
748 if (n
> 0 && (*linebuf
)[n
- 1] == '\n')
751 if (sz
< 0 && errno
== EINTR
)
754 if ((*linebuf
)[n
- 1] != '\n') {
755 (*linebuf
)[n
++] = '\n';
756 (*linebuf
)[n
] = '\0';
764 /* Not reading from standard input or standard input not a terminal. We
765 * read one char at a time as it is the only way to get lines with
766 * embedded NUL characters in standard stdio */
767 if (_fgetline_byone(linebuf
, linesize
, &n
, ibuf
, 1, n
768 SMALLOC_DEBUG_ARGSCALL
) == NULL
)
771 if (n
> 0 && (*linebuf
)[n
- 1] == '\n')
772 (*linebuf
)[--n
] = '\0';
780 (readline_input
)(char const *prompt
, bool_t nl_escape
, char **linebuf
,
781 size_t *linesize
, char const *string SMALLOC_DEBUG_ARGS
)
783 /* TODO readline: linebuf pool! */
784 FILE *ifile
= (_fio_input
!= NULL
) ? _fio_input
: stdin
;
785 bool_t doprompt
, dotty
;
789 doprompt
= (!(pstate
& PS_SOURCING
) && (options
& OPT_INTERACTIVE
));
790 dotty
= (doprompt
&& !ok_blook(line_editor_disable
));
793 else if (prompt
== NULL
)
794 prompt
= getprompt();
796 /* Ensure stdout is flushed first anyway */
797 if (!dotty
&& prompt
== NULL
)
800 for (nold
= n
= 0;;) {
802 assert(ifile
== stdin
);
803 if (string
!= NULL
&& (n
= (int)strlen(string
)) > 0) {
807 *linesize
= (size_t)n
+ LINESIZE
+1;
808 *linebuf
= (srealloc
)(*linebuf
, *linesize SMALLOC_DEBUG_ARGSCALL
);
809 memcpy(*linebuf
, string
, (size_t)n
+1);
812 /* TODO if nold>0, don't redisplay the entire line!
813 * TODO needs complete redesign ... */
814 n
= (n_tty_readline
)(prompt
, linebuf
, linesize
, n
815 SMALLOC_DEBUG_ARGSCALL
);
817 if (prompt
!= NULL
) {
819 fputs(prompt
, stdout
);
822 n
= (readline_restart
)(ifile
, linebuf
, linesize
, n
823 SMALLOC_DEBUG_ARGSCALL
);
825 if (n
> 0 && nold
> 0) {
827 char const *cp
= *linebuf
+ nold
;
829 while (blankspacechar(*cp
) && nold
+ i
< n
)
832 memmove(*linebuf
+ nold
, cp
, n
- nold
- i
);
834 (*linebuf
)[n
] = '\0';
842 * An unquoted <backslash> at the end of a command line shall
843 * be discarded and the next line shall continue the command */
844 if (!nl_escape
|| n
== 0 || (*linebuf
)[n
- 1] != '\\')
846 (*linebuf
)[nold
= --n
] = '\0';
847 if (prompt
!= NULL
&& *prompt
!= '\0')
848 prompt
= ".. "; /* XXX PS2 .. */
851 if (n
>= 0 && (options
& OPT_D_VV
))
852 n_err(_("%s %d bytes <%.*s>\n"),
853 ((pstate
& PS_LOADING
) ? "LOAD"
854 : (pstate
& PS_SOURCING
) ? "SOURCE" : "READ"),
861 n_input_cp_addhist(char const *prompt
, char const *string
, bool_t isgabby
)
863 /* FIXME n_input_cp_addhist(): leaks on sigjmp without linepool */
865 char *linebuf
= NULL
, *rv
= NULL
;
869 n
= readline_input(prompt
, FAL0
, &linebuf
, &linesize
, string
);
870 if (n
> 0 && *(rv
= savestrbuf(linebuf
, (size_t)n
)) != '\0' &&
871 (options
& OPT_INTERACTIVE
))
872 n_tty_addhist(rv
, isgabby
);
881 setptr(FILE *ibuf
, off_t offset
)
884 char *cp
, *linebuf
= NULL
;
886 int c
, maybe
= 1, inhead
= 0, selfcnt
= 0;
887 size_t linesize
= 0, filesize
, cnt
;
890 memset(&self
, 0, sizeof self
);
891 self
.m_flag
= MUSED
| MNEW
| MNEWEST
;
892 filesize
= mailsize
- offset
;
893 offset
= ftell(mb
.mb_otf
);
896 if (fgetline(&linebuf
, &linesize
, &filesize
, &cnt
, ibuf
, 0) == NULL
) {
897 self
.m_xsize
= self
.m_size
;
898 self
.m_xlines
= self
.m_lines
;
899 self
.m_have
= HAVE_HEADER
| HAVE_BODY
;
901 message_append(&self
);
909 if (linebuf
[0] == '\0')
912 /* XXX Convert CRLF to LF; this should be rethought in that
913 * XXX CRLF input should possibly end as CRLF output? */
914 if (cnt
>= 2 && linebuf
[cnt
- 1] == '\n' && linebuf
[cnt
- 2] == '\r')
915 linebuf
[--cnt
- 1] = '\n';
916 fwrite(linebuf
, sizeof *linebuf
, cnt
, mb
.mb_otf
);
917 if (ferror(mb
.mb_otf
)) {
918 n_perr(_("/tmp"), 0);
921 if (linebuf
[cnt
- 1] == '\n')
922 linebuf
[cnt
- 1] = '\0';
923 if (maybe
&& linebuf
[0] == 'F' && is_head(linebuf
, cnt
, FAL0
)) {
924 /* TODO char date[FROM_DATEBUF];
925 * TODO extract_date_from_from_(linebuf, cnt, date);
926 * TODO self.m_time = 10000; */
927 self
.m_xsize
= self
.m_size
;
928 self
.m_xlines
= self
.m_lines
;
929 self
.m_have
= HAVE_HEADER
| HAVE_BODY
;
931 message_append(&self
);
933 self
.m_flag
= MUSED
| MNEW
| MNEWEST
;
936 self
.m_block
= mailx_blockof(offset
);
937 self
.m_offset
= mailx_offsetof(offset
);
939 } else if (linebuf
[0] == 0) {
942 for (cp
= linebuf
, cp2
= "status";; ++cp
) {
943 if ((c
= *cp2
++) == 0) {
944 while (c
= *cp
++, whitechar(c
))
948 while ((c
= *cp
++) != '\0')
950 self
.m_flag
|= MREAD
;
952 self
.m_flag
&= ~MNEW
;
955 if (*cp
!= c
&& *cp
!= upperconv(c
))
958 for (cp
= linebuf
, cp2
= "x-status";; ++cp
) {
959 if ((c
= *cp2
++) == 0) {
960 while ((c
= *cp
++, whitechar(c
)))
964 while ((c
= *cp
++) != '\0')
966 self
.m_flag
|= MFLAGGED
;
968 self
.m_flag
|= MANSWERED
;
970 self
.m_flag
|= MDRAFTED
;
973 if (*cp
!= c
&& *cp
!= upperconv(c
))
980 maybe
= linebuf
[0] == 0;
986 putline(FILE *obuf
, char *linebuf
, size_t cnt
)
991 fwrite(linebuf
, sizeof *linebuf
, cnt
, obuf
);
1000 setinput(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
1003 enum okay ok
= STOP
;
1008 ok
= (m
->m_have
& HAVE_HEADER
) ? OKAY
: get_header(m
);
1011 ok
= (m
->m_have
& HAVE_BODY
) ? OKAY
: get_body(m
);
1021 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
, m
->m_offset
),
1023 n_perr(_("fseek"), 0);
1024 n_panic(_("temporary file seek"));
1036 if (message
!= NULL
) {
1046 message_append(struct message
*mp
)
1049 if (UICMP(z
, msgCount
+ 1, >=, _message_space
)) {
1050 /* XXX remove _message_space magics (or use s_Vector) */
1051 _message_space
= (_message_space
>= 128 && _message_space
<= 1000000)
1052 ? _message_space
<< 1 : _message_space
+ 64;
1053 message
= srealloc(message
, _message_space
* sizeof *message
);
1057 message
[msgCount
- 1] = *mp
;
1059 memset(message
+ msgCount
- 1, 0, sizeof *message
);
1065 message_match(struct message
*mp
, struct search_expr
const *sep
,
1066 bool_t with_headers
)
1069 size_t *linesize
, cnt
;
1074 if ((fp
= Ftmp(NULL
, "mpmatch", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
1077 if (sendmp(mp
, fp
, NULL
, NULL
, SEND_TOSRCH
, NULL
) < 0)
1082 line
= &termios_state
.ts_linebuf
; /* XXX line pool */
1083 linesize
= &termios_state
.ts_linesize
; /* XXX line pool */
1086 while (fgetline(line
, linesize
, &cnt
, NULL
, fp
, 0))
1090 while (fgetline(line
, linesize
, &cnt
, NULL
, fp
, 0)) {
1092 if (sep
->ss_sexpr
== NULL
) {
1093 if (regexec(&sep
->ss_regex
, *line
, 0,NULL
, 0) == REG_NOMATCH
)
1097 if (!substr(*line
, sep
->ss_sexpr
))
1111 setdot(struct message
*mp
)
1116 pstate
&= ~PS_DID_PRINT_DOT
;
1119 uncollapse1(dot
, 0);
1125 rm(char const *name
)
1131 if (stat(name
, &sb
) < 0)
1133 else if (!S_ISREG(sb
.st_mode
))
1148 rv
= (fstat(fileno(iob
), &sbuf
) == -1) ? 0 : sbuf
.st_size
;
1154 fexpand(char const *name
, enum fexp_mode fexpm
)
1156 char cbuf
[PATH_MAX
+1];
1162 /* The order of evaluation is "%" and "#" expand into constants.
1163 * "&" can expand into "+". "+" can expand into shell meta characters.
1164 * Shell meta characters expand into constants.
1165 * This way, we make no recursive expansion */
1166 if ((fexpm
& FEXP_NSHORTCUT
) || (res
= shortcut_expand(name
)) == NULL
)
1167 res
= UNCONST(name
);
1169 if (fexpm
& FEXP_SHELL
) {
1177 if (res
[1] == ':' && res
[2] != '\0') {
1181 _findmail(cbuf
, sizeof cbuf
, (res
[1] != '\0' ? res
+ 1 : myname
),
1182 (res
[1] != '\0' || (options
& OPT_u_FLAG
)));
1188 if (prevfile
[0] == '\0') {
1189 n_err(_("No previous file\n"));
1196 if (res
[1] == '\0') {
1197 if ((res
= ok_vlook(MBOX
)) == NULL
)
1198 res
= UNCONST("~/mbox"); /* XXX no magics (POSIX though) */
1199 else if (res
[0] != '&' || res
[1] != '\0')
1205 if (res
[0] == '@' && which_protocol(mailname
) == PROTO_IMAP
) {
1206 res
= str_concat_csvl(&s
, protbase(mailname
), "/", res
+ 1, NULL
)->s
;
1210 if (res
[0] == '+' && getfold(cbuf
, sizeof cbuf
)) {
1211 size_t i
= strlen(cbuf
);
1213 res
= str_concat_csvl(&s
, cbuf
,
1214 ((i
> 0 && cbuf
[i
- 1] == '/') ? "" : "/"), res
+ 1, NULL
)->s
;
1217 if (res
[0] == '%' && res
[1] == ':') {
1223 /* Catch the most common shell meta character */
1225 if (res
[0] == '~') {
1226 res
= n_shell_expand_tilde(res
, NULL
);
1229 if (anyof(res
, "|&;<>{}()[]*?$`'\"\\"))
1230 switch (which_protocol(res
)) {
1233 res
= (fexpm
& FEXP_NSHELL
) ? n_shell_expand_var(res
, TRU1
, NULL
)
1234 : _globname(res
, fexpm
);
1241 if (fexpm
& FEXP_LOCAL
)
1242 switch (which_protocol(res
)) {
1247 n_err(_("Not a local file or directory: \"%s\"\n"), name
);
1255 return UNCONST(res
);
1259 fexpand_nshell_quote(char const *name
)
1265 for (i
= j
= 0; (c
= name
[i
]) != '\0'; ++i
)
1270 rv
= savestrbuf(name
, i
);
1272 rv
= salloc(i
+ j
+1);
1273 for (i
= j
= 0; (c
= name
[i
]) != '\0'; ++i
) {
1285 var_folder_updated(char const *name
, char **store
)
1288 char *folder
, *unres
= NULL
, *res
= NULL
;
1291 if ((folder
= UNCONST(name
)) == NULL
)
1294 /* Expand the *folder*; skip %: prefix for simplicity of use */
1295 /* XXX This *only* works because we do NOT
1296 * XXX update environment variables via the "set" mechanism */
1297 if (folder
[0] == '%' && folder
[1] == ':')
1299 if ((folder
= fexpand(folder
, FEXP_FULL
)) == NULL
) /* XXX error? */
1302 switch (which_protocol(folder
)) {
1304 n_err(_("*folder* cannot be set to a flat, readonly POP3 account\n"));
1308 /* Simply assign what we have, even including `%:' prefix */
1313 /* Further expansion desired */
1317 /* All non-absolute paths are relative to our home directory */
1318 if (*folder
!= '/') {
1319 size_t l1
= strlen(homedir
), l2
= strlen(folder
);
1320 unres
= ac_alloc(l1
+ l2
+ 1 +1);
1321 memcpy(unres
, homedir
, l1
);
1323 memcpy(unres
+ l1
+ 1, folder
, l2
);
1324 unres
[l1
+ 1 + l2
] = '\0';
1328 /* Since lex.c:_update_mailname() uses realpath(3) if available to
1329 * avoid that we loose track of our currently open folder in case we
1330 * chdir away, but still checks the leading path portion against
1331 * getfold() to be able to abbreviate to the +FOLDER syntax if
1332 * possible, we need to realpath(3) the folder, too */
1333 #ifdef HAVE_REALPATH
1334 res
= ac_alloc(PATH_MAX
+1);
1335 if (realpath(folder
, res
) == NULL
)
1336 n_err(_("Can't canonicalize \"%s\"\n"), folder
);
1342 *store
= sstrdup(folder
);
1354 getfold(char *name
, size_t size
)
1359 if ((folder
= ok_vlook(folder
)) != NULL
)
1360 n_strlcpy(name
, folder
, size
);
1362 return (folder
!= NULL
);
1366 getdeadletter(void) /* XXX should that be in auxlily.c? */
1371 if ((cp
= ok_vlook(DEAD
)) == NULL
|| (cp
= fexpand(cp
, FEXP_LOCAL
)) == NULL
)
1372 cp
= fexpand("~/dead.letter", FEXP_LOCAL
| FEXP_SHELL
);
1373 else if (*cp
!= '/') {
1374 size_t sz
= strlen(cp
) + 2 +1;
1375 char *buf
= ac_alloc(sz
);
1377 snprintf(buf
, sz
, "~/%s", cp
);
1378 cp
= fexpand(buf
, FEXP_LOCAL
| FEXP_SHELL
);
1383 cp
= "dead.letter"; /* XXX magic -> nail.h (POSIX thing though) */
1389 get_body(struct message
*mp
)
1395 switch (mb
.mb_type
) {
1421 file_lock(int fd
, enum file_lock_type flt
, off_t off
, off_t len
,
1428 for (tries
= 0; tries
<= FILE_LOCK_TRIES
; ++tries
)
1429 if ((rv
= _file_lock(fd
, flt
, off
, len
)) || pollmsecs
== 0)
1432 sleep(1); /* TODO pollmsecs -> use finer grain */
1438 dot_lock(char const *fname
, int fd
, enum file_lock_type flt
,
1439 off_t off
, off_t len
, size_t pollmsecs
)
1443 # define _DOMSG() n_err(_("Creating dotlock for \"%s\" "), fname)
1445 # define _DOMSG() n_err(_("Trying to lock file \"%s\" "), fname)
1450 struct dotlock_info di
;
1451 enum dotlock_state dls
;
1452 char const *emsg
= NULL
;
1454 int UNINIT(serrno
, 0);
1455 union {size_t tries
; int (*ptf
)(void); char const *sh
; ssize_t r
;} u
;
1456 bool_t flocked
, didmsg
= FAL0
;
1460 if (options
& OPT_D_VV
) {
1466 for (u
.tries
= 0; !_file_lock(fd
, flt
, off
, len
);)
1467 switch ((serrno
= errno
)) {
1471 if (pollmsecs
> 0 && ++u
.tries
< FILE_LOCK_TRIES
) {
1476 sleep(1); /* TODO pollmsecs -> use finer grain */
1485 #ifndef HAVE_DOTLOCK
1487 if (didmsg
== TRUM1
)
1497 /* Create control-pipe for our dot file locker process, which will remove
1498 * the lock and terminate once the pipe is closed, for whatever reason */
1499 if (pipe_cloexec(cpipe
) == -1) {
1501 emsg
= N_(" Can't create file lock control pipe\n");
1505 /* And the locker process itself; it'll be a (rather cheap) thread only
1506 * unless the lock has to be placed in the system spool and we have our
1507 * privilege-separated dotlock program available, in which case that will be
1508 * executed and do "it" with changed group-id */
1509 di
.di_file_name
= fname
;
1510 di
.di_pollmsecs
= pollmsecs
;
1511 /* Initialize some more stuff; query the two strings in the parent in order
1512 * to cache the result of the former and anyway minimalize child page-ins.
1513 * Especially uname(3) may hang for multiple seconds when it is called the
1515 di
.di_hostname
= nodename(FAL0
);
1516 di
.di_randstr
= getrandstring(16);
1521 u
.ptf
= &_dotlock_main
;
1522 rv
= Popen((char*)-1, "W", u
.sh
, NULL
, cpipe
[1]);
1528 emsg
= N_(" Can't create file lock process\n");
1532 /* Let's check wether we were able to create the dotlock file */
1534 u
.r
= read(cpipe
[0], &dls
, sizeof dls
);
1535 if (UICMP(z
, u
.r
, !=, sizeof dls
)) {
1536 serrno
= (u
.r
!= -1) ? EAGAIN
: errno
;
1537 dls
= DLS_DUNNO
| DLS_ABANDON
;
1541 if (dls
== DLS_NONE
|| (dls
& DLS_ABANDON
))
1544 switch (dls
& ~DLS_ABANDON
) {
1547 case DLS_CANT_CHDIR
:
1548 if (options
& OPT_D_V
)
1549 emsg
= N_(" Can't change to directory! Please check permissions\n");
1552 case DLS_NAMETOOLONG
:
1553 emsg
= N_("Resulting dotlock filename would be too long\n");
1557 assert(dls
& DLS_ABANDON
);
1558 if (options
& OPT_D_V
)
1559 emsg
= N_(" Read-only filesystem, not creating lock file\n");
1563 if (options
& OPT_D_V
)
1564 emsg
= N_(" Can't create a lock file! Please check permissions\n"
1565 " (Maybe setting *dotlock-ignore-error* variable helps.)\n");
1569 if (options
& OPT_D_V
)
1570 emsg
= N_(" Can't find privilege-separated file lock program\n");
1573 case DLS_PRIVFAILED
:
1574 emsg
= N_(" Privilege-separated file lock program can't change "
1579 emsg
= N_(" It seems there is a stale dotlock file?\n"
1580 " Please remove the lock file manually, then retry\n");
1584 emsg
= N_(" Fishy! Is someone trying to \"steal\" foreign files?\n"
1585 " Please check the mailbox file etc. manually, then retry\n");
1586 serrno
= EAGAIN
; /* ? Hack to ignore *dotlock-ignore-error* xxx */
1590 emsg
= N_(" Unspecified dotlock file control process error.\n"
1591 " Like broken I/O pipe; this one is unlikely to happen\n");
1592 if (serrno
!= EAGAIN
)
1608 if (didmsg
== TRUM1
)
1615 if (dls
& DLS_ABANDON
) {
1623 if (didmsg
== TRUM1
)
1626 if (flocked
&& (serrno
== EROFS
||
1627 (serrno
!= EAGAIN
&& serrno
!= EEXIST
&&
1628 ok_blook(dotlock_ignore_error
))))
1642 #endif /* HAVE_DOTLOCK */
1648 sclose(struct sock
*sp
)
1655 /* TODO NOTE: we MUST NOT close the descriptor 0 here...
1656 * TODO of course this should be handled in a VMAILFS->open() .s_fd=-1,
1657 * TODO but unfortunately it isn't yet */
1661 if (sp
->s_onclose
!= NULL
)
1663 if (sp
->s_wbuf
!= NULL
)
1665 # ifdef HAVE_OPENSSL
1666 if (sp
->s_use_ssl
) {
1667 void *s_ssl
= sp
->s_ssl
;
1671 while (!SSL_shutdown(s_ssl
)) /* XXX proper error handling;signals! */
1683 swrite(struct sock
*sp
, char const *data
)
1688 rv
= swrite1(sp
, data
, strlen(data
), 0);
1694 swrite1(struct sock
*sp
, char const *data
, int sz
, int use_buffer
)
1696 enum okay rv
= STOP
;
1700 if (use_buffer
> 0) {
1703 if (sp
->s_wbuf
== NULL
) {
1704 sp
->s_wbufsize
= 4096;
1705 sp
->s_wbuf
= smalloc(sp
->s_wbufsize
);
1708 while (sp
->s_wbufpos
+ sz
> sp
->s_wbufsize
) {
1709 di
= sp
->s_wbufsize
- sp
->s_wbufpos
;
1711 if (sp
->s_wbufpos
> 0) {
1712 memcpy(sp
->s_wbuf
+ sp
->s_wbufpos
, data
, di
);
1713 rv
= swrite1(sp
, sp
->s_wbuf
, sp
->s_wbufsize
, -1);
1715 rv
= swrite1(sp
, data
, sp
->s_wbufsize
, -1);
1721 if (sz
== sp
->s_wbufsize
) {
1722 rv
= swrite1(sp
, data
, sp
->s_wbufsize
, -1);
1726 memcpy(sp
->s_wbuf
+ sp
->s_wbufpos
, data
, sz
);
1727 sp
->s_wbufpos
+= sz
;
1731 } else if (use_buffer
== 0 && sp
->s_wbuf
!= NULL
&& sp
->s_wbufpos
> 0) {
1734 if ((rv
= swrite1(sp
, sp
->s_wbuf
, x
, -1)) != OKAY
)
1742 # ifdef HAVE_OPENSSL
1743 if (sp
->s_use_ssl
) {
1745 x
= SSL_write(sp
->s_ssl
, data
, sz
);
1747 switch (SSL_get_error(sp
->s_ssl
, x
)) {
1748 case SSL_ERROR_WANT_READ
:
1749 case SSL_ERROR_WANT_WRITE
:
1756 x
= xwrite(sp
->s_fd
, data
, sz
);
1760 snprintf(o
, sizeof o
, "%s write error",
1761 (sp
->s_desc
? sp
->s_desc
: "socket"));
1762 # ifdef HAVE_OPENSSL
1764 ssl_gen_err("%s", o
);
1779 static sigjmp_buf __sopen_actjmp
; /* TODO someday, we won't need it no more */
1780 static int __sopen_sig
; /* TODO someday, we won't need it no more */
1782 __sopen_onsig(int sig
) /* TODO someday, we won't need it no more */
1784 NYD_X
; /* Signal handler */
1785 if (__sopen_sig
< 0) {
1786 /* Of course the following doesn't belong into a signal handler XXX */
1789 if (__sopen_sig
== -1) {
1791 _("\nInterrupting it could turn the (GNU/Linux+) DNS resolver "
1793 " Wait until it's done, or do terminate the program\n"));
1795 } else if ((i
= j
= ABS(__sopen_sig
)) + 15 < scrnwidth
) {
1799 fputs("___( o)", stderr
);
1800 putc((i
& 1) ? '=' : '>', stderr
);
1808 siglongjmp(__sopen_actjmp
, 1);
1813 sopen(struct sock
*sp
, struct url
*urlp
) /* TODO sighandling; refactor */
1815 # ifdef HAVE_SO_SNDTIMEO
1818 # ifdef HAVE_SO_LINGER
1821 # ifdef HAVE_GETADDRINFO
1822 char hbuf
[NI_MAXHOST
];
1823 struct addrinfo hints
, *res0
= NULL
, *res
;
1825 struct sockaddr_in servaddr
;
1826 struct in_addr
**pptr
;
1830 sighandler_type
volatile ohup
, oint
;
1831 char const * volatile serv
;
1832 int volatile sofd
= -1, errval
;
1837 /* Connect timeouts after 30 seconds XXX configurable */
1838 # ifdef HAVE_SO_SNDTIMEO
1842 serv
= (urlp
->url_port
!= NULL
) ? urlp
->url_port
: urlp
->url_proto
;
1844 if (options
& OPT_VERB
)
1845 n_err(_("Resolving host \"%s:%s\" ... "),
1846 urlp
->url_host
.s
, serv
);
1848 /* Signal handling (in respect to __sopen_sig dealing) is heavy, but no
1849 * healing until v15.0 and i want to end up with that functionality */
1852 ohup
= safe_signal(SIGHUP
, &__sopen_onsig
);
1853 oint
= safe_signal(SIGINT
, &__sopen_onsig
);
1854 if (sigsetjmp(__sopen_actjmp
, 0)) {
1857 (__sopen_sig
== SIGHUP
? _("Hangup") : _("Interrupted")));
1866 # ifdef HAVE_GETADDRINFO
1868 memset(&hints
, 0, sizeof hints
);
1869 hints
.ai_socktype
= SOCK_STREAM
;
1871 errval
= getaddrinfo(urlp
->url_host
.s
, serv
, &hints
, &res0
);
1872 if (__sopen_sig
!= -1) {
1873 __sopen_sig
= SIGINT
;
1880 if (options
& OPT_VERB
)
1881 n_err(_("failed\n"));
1882 n_err(_("Lookup of \"%s:%s\" failed: %s\n"),
1883 urlp
->url_host
.s
, serv
, gai_strerror(errval
));
1885 /* Error seems to depend on how "smart" the /etc/service code is: is it
1886 * "able" to state wether the service as such is NONAME or does it only
1887 * check for the given ai_socktype.. */
1888 if (errval
== EAI_NONAME
|| errval
== EAI_SERVICE
) {
1889 if (serv
== urlp
->url_proto
&&
1890 (serv
= url_servbyname(urlp
, NULL
)) != NULL
) {
1891 n_err(_(" Trying standard protocol port \"%s\"\n"
1892 " If that succeeds consider including the port in the URL!\n"),
1896 if (serv
!= urlp
->url_port
)
1897 n_err(_(" Including a port number in the URL may "
1898 "circumvent this problem\n"));
1904 if (options
& OPT_VERB
)
1907 for (res
= res0
; res
!= NULL
&& sofd
< 0; res
= res
->ai_next
) {
1908 if (options
& OPT_VERB
) {
1909 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
, sizeof hbuf
,
1910 NULL
, 0, NI_NUMERICHOST
))
1911 memcpy(hbuf
, "unknown host", sizeof("unknown host"));
1912 n_err(_("%sConnecting to \"%s:%s\" ..."),
1913 (res
== res0
? "" : "\n"), hbuf
, serv
);
1916 sofd
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
1918 # ifdef HAVE_SO_SNDTIMEO
1919 (void)setsockopt(sofd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
1921 if (connect(sofd
, res
->ai_addr
, res
->ai_addrlen
)) {
1935 # else /* HAVE_GETADDRINFO */
1936 if (serv
== urlp
->url_proto
) {
1937 if ((ep
= getservbyname(UNCONST(serv
), "tcp")) != NULL
)
1938 urlp
->url_portno
= ntohs(ep
->s_port
);
1940 if (options
& OPT_VERB
)
1941 n_err(_("failed\n"));
1942 if ((serv
= url_servbyname(urlp
, &urlp
->url_portno
)) != NULL
)
1943 n_err(_(" Unknown service: \"%s\"\n"
1944 " Trying standard protocol port \"%s\"\n"
1945 " If that succeeds consider including the port in the URL!\n"),
1946 urlp
->url_proto
, serv
);
1948 n_err(_(" Unknown service: \"%s\"\n"
1949 " Including a port in the URL may circumvent this problem\n"),
1951 assert(sofd
== -1 && errval
== 0);
1958 hp
= gethostbyname(urlp
->url_host
.s
);
1959 if (__sopen_sig
!= -1) {
1960 __sopen_sig
= SIGINT
;
1968 if (options
& OPT_VERB
)
1969 n_err(_("failed\n"));
1971 case HOST_NOT_FOUND
: emsg
= N_("host not found"); break;
1973 case TRY_AGAIN
: emsg
= N_("(maybe) try again later"); break;
1974 case NO_RECOVERY
: emsg
= N_("non-recoverable server error"); break;
1975 case NO_DATA
: emsg
= N_("valid name without IP address"); break;
1977 n_err(_("Lookup of \"%s:%s\" failed: %s\n"),
1978 urlp
->url_host
.s
, serv
, V_(emsg
));
1980 } else if (options
& OPT_VERB
)
1983 pptr
= (struct in_addr
**)hp
->h_addr_list
;
1984 if ((sofd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
1985 n_perr(_("could not create socket"), 0);
1986 assert(sofd
== -1 && errval
== 0);
1990 memset(&servaddr
, 0, sizeof servaddr
);
1991 servaddr
.sin_family
= AF_INET
;
1992 servaddr
.sin_port
= htons(urlp
->url_portno
);
1993 memcpy(&servaddr
.sin_addr
, *pptr
, sizeof(struct in_addr
));
1994 if (options
& OPT_VERB
)
1995 n_err(_("%sConnecting to \"%s:%d\" ... "),
1996 "", inet_ntoa(**pptr
), (int)urlp
->url_portno
);
1997 # ifdef HAVE_SO_SNDTIMEO
1998 (void)setsockopt(sofd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
2000 if (connect(sofd
, (struct sockaddr
*)&servaddr
, sizeof servaddr
)) {
2006 # endif /* !HAVE_GETADDRINFO */
2009 safe_signal(SIGINT
, oint
);
2010 safe_signal(SIGHUP
, ohup
);
2016 n_perr(_("Could not connect"), 0);
2021 if (options
& OPT_VERB
)
2022 n_err(_("connected.\n"));
2024 /* And the regular timeouts XXX configurable */
2025 # ifdef HAVE_SO_SNDTIMEO
2028 (void)setsockopt(sofd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof tv
);
2029 (void)setsockopt(sofd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof tv
);
2031 # ifdef HAVE_SO_LINGER
2034 (void)setsockopt(sofd
, SOL_SOCKET
, SO_LINGER
, &li
, sizeof li
);
2037 memset(sp
, 0, sizeof *sp
);
2040 /* SSL/TLS upgrade? */
2042 if (urlp
->url_needs_tls
) {
2044 ohup
= safe_signal(SIGHUP
, &__sopen_onsig
);
2045 oint
= safe_signal(SIGINT
, &__sopen_onsig
);
2046 if (sigsetjmp(__sopen_actjmp
, 0)) {
2047 n_err(_("%s during SSL/TLS handshake\n"),
2048 (__sopen_sig
== SIGHUP
? _("Hangup") : _("Interrupted")));
2053 if (ssl_open(urlp
, sp
) != OKAY
) {
2060 safe_signal(SIGINT
, oint
);
2061 safe_signal(SIGHUP
, ohup
);
2064 # endif /* HAVE_SSL */
2067 /* May need to bounce the signal to the lex.c trampoline (or wherever) */
2068 if (__sopen_sig
!= 0) {
2071 sigaddset(&cset
, __sopen_sig
);
2072 sigprocmask(SIG_UNBLOCK
, &cset
, NULL
);
2073 n_raise(__sopen_sig
);
2080 (sgetline
)(char **line
, size_t *linesize
, size_t *linelen
, struct sock
*sp
2092 if (sp
->s_rsz
< 0) {
2099 if (lp_base
== NULL
|| PTRCMP(lp
, >, lp_base
+ lsize
- 128)) {
2100 size_t diff
= PTR2SIZE(lp
- lp_base
);
2101 *linesize
= (lsize
+= 256); /* XXX magic */
2102 *line
= lp_base
= (srealloc
)(lp_base
, lsize SMALLOC_DEBUG_ARGSCALL
);
2103 lp
= lp_base
+ diff
;
2106 if (sp
->s_rbufptr
== NULL
||
2107 PTRCMP(sp
->s_rbufptr
, >=, sp
->s_rbuf
+ sp
->s_rsz
)) {
2108 # ifdef HAVE_OPENSSL
2109 if (sp
->s_use_ssl
) {
2111 sp
->s_rsz
= SSL_read(sp
->s_ssl
, sp
->s_rbuf
, sizeof sp
->s_rbuf
);
2112 if (sp
->s_rsz
<= 0) {
2113 if (sp
->s_rsz
< 0) {
2115 switch(SSL_get_error(sp
->s_ssl
, sp
->s_rsz
)) {
2116 case SSL_ERROR_WANT_READ
:
2117 case SSL_ERROR_WANT_WRITE
:
2120 snprintf(o
, sizeof o
, "%s",
2121 (sp
->s_desc
? sp
->s_desc
: "socket"));
2122 ssl_gen_err("%s", o
);
2130 sp
->s_rsz
= read(sp
->s_fd
, sp
->s_rbuf
, sizeof sp
->s_rbuf
);
2131 if (sp
->s_rsz
<= 0) {
2132 if (sp
->s_rsz
< 0) {
2136 snprintf(o
, sizeof o
, "%s",
2137 (sp
->s_desc
? sp
->s_desc
: "socket"));
2143 sp
->s_rbufptr
= sp
->s_rbuf
;
2145 } while ((*lp
++ = *sp
->s_rbufptr
++) != '\n');
2147 lsize
= PTR2SIZE(lp
- lp_base
);
2156 #endif /* HAVE_SOCKETS */
2159 load(char const *name
)
2166 if (name
== NULL
|| *name
== '\0' || (in
= Fopen(name
, "r")) == NULL
)
2171 pstate
|= PS_IN_LOAD
;
2172 /* commands() may sreset(), copy over file name */
2174 n
.s
= ac_alloc(n
.l
+1);
2175 memcpy(n
.s
, name
, n
.l
+1);
2177 cond
= condstack_release();
2179 n_err(_("Stopped loading \"%s\" due to errors "
2180 "(enable *debug* for trace)\n"), n
.s
);
2181 condstack_take(cond
);
2184 pstate
&= ~PS_IN_LOAD
;
2197 rv
= _source_file(*(char**)v
, FAL0
) ? 0 : 1;
2203 c_source_if(void *v
) /* XXX obsolete?, support file tests in `if' etc.! */
2208 rv
= _source_file(*(char**)v
, TRU1
) ? 0 : 1;
2220 if (_fio_stack_size
== 0) {
2221 n_err(_("`source' stack over-pop\n"));
2222 pstate
&= ~PS_SOURCING
;
2229 if (!condstack_take(_fio_stack
[_fio_stack_size
].s_cond
))
2230 n_err(_("Unmatched \"if\"\n"));
2231 if (_fio_stack
[_fio_stack_size
].s_loading
)
2232 pstate
|= PS_LOADING
;
2234 pstate
&= ~PS_LOADING
;
2235 _fio_input
= _fio_stack
[_fio_stack_size
].s_file
;
2236 if (_fio_stack_size
== 0) {
2237 if (pstate
& PS_LOADING
)
2238 pstate
|= PS_SOURCING
;
2240 pstate
&= ~PS_SOURCING
;