1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Startup -- interface with user.
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
41 * Most strcpy/sprintf functions have been changed to strncpy/snprintf to
42 * correct several buffer overruns (at least one ot them was exploitable).
43 * Sat Jun 20 04:58:09 CEST 1998 Alvaro Martinez Echevarria <alvaro@lander.es>
45 * Note: We set egid to realgid ... and only if we need the egid we will
46 * switch back temporary. Nevertheless, I do not like seg faults.
47 * Werner Fink, <werner@suse.de>
52 #include <sys/ioctl.h>
57 #ifdef HAVE_NL_LANGINFO
58 # include <langinfo.h>
70 struct a_arg
*aa_next
;
74 /* Perform basic startup initialization */
75 static void _startup(void);
78 static int _grow_cpp(char const ***cpp
, int size
, int cnt
);
80 /* Initialize *tempdir*, *myname*, *homedir* */
81 static void _setup_vars(void);
83 /* We're in an interactive session - compute what the screen size for printing
84 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
85 * We use the following algorithm for the height:
86 * If baud rate < 1200, use 9
87 * If baud rate = 1200, use 14
88 * If baud rate > 1200, use 24 or ws_row
89 * Width is either 80 or ws_col */
90 static void _setscreensize(int is_sighdl
);
92 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
93 * the system mailbox, and open up the right stuff */
94 static int _rcv_mode(char const *folder
);
96 /* Interrupt printing of the headers */
97 static void _hdrstop(int signo
);
104 /* Absolutely the first thing we do is save our egid
105 * and set it to the rgid, so that we can safely run
106 * setgid. We use the sgid (saved set-gid) to allow ourselves
107 * to revert to the egid if we want (temporarily) to become
109 effectivegid
= getegid();
111 if (setgid(realgid
) < 0) {
118 if ((cp
= strrchr(progname
, '/')) != NULL
)
121 /* Set up a reasonable environment.
122 * Figure out whether we are being run interactively,
123 * start the SIGCHLD catcher, and so forth */
124 safe_signal(SIGCHLD
, sigchild
);
127 options
|= OPT_TTYIN
| OPT_INTERACTIVE
;
129 options
|= OPT_TTYOUT
;
130 if (IS_TTY_SESSION())
131 safe_signal(SIGPIPE
, dflpipe
= SIG_IGN
);
132 assign("header", "");
135 #ifdef HAVE_SETLOCALE
136 setlocale(LC_ALL
, "");
137 mb_cur_max
= MB_CUR_MAX
;
138 # if defined HAVE_NL_LANGINFO && defined CODESET
139 if (value("ttycharset") == NULL
&& (cp
= nl_langinfo(CODESET
)) != NULL
)
140 assign("ttycharset", cp
);
143 # if defined HAVE_MBTOWC && defined HAVE_WCTYPE_H
144 if (mb_cur_max
> 1) {
146 if (mbtowc(&wc
, "\303\266", 2) == 2 && wc
== 0xF6 &&
147 mbtowc(&wc
, "\342\202\254", 3) == 3 &&
150 /* Reset state - it may have been messed up; luckily this also
151 * gives us an indication wether the encoding has locking shift
153 /* TODO temporary - use option bits! */
154 enc_has_state
= mbtowc(&wc
, NULL
, mb_cur_max
);
162 # ifdef NL_CAT_LOCALE
163 catd
= catopen(CATNAME
, NL_CAT_LOCALE
);
165 catd
= catopen(CATNAME
, 0);
170 iconvd
= (iconv_t
)-1;
175 _grow_cpp(char const ***cpp
, int size
, int cnt
)
177 /* Before spreserve(): use our string pool instead of LibC heap;
178 * Increment *size* by at least 5! */
179 char const **newcpp
= salloc(sizeof(char*) * (size
+= 8));
182 memcpy(newcpp
, *cpp
, (size_t)cnt
* sizeof(char*));
190 /* Before spreserve(): use our string pool instead of LibC heap */
193 tempdir
= ((cp
= getenv("TMPDIR")) != NULL
) ? savestr(cp
) : "/tmp";
195 myname
= savestr(username());
197 if ((cp
= getenv("HOME")) == NULL
)
199 homedir
= savestr(cp
);
203 _setscreensize(int is_sighdl
)
208 #elif defined TIOCGSIZE
212 scrnheight
= realscreenheight
= scrnwidth
= 0;
214 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
215 * a little bit to be able to honour resizes during our lifetime and
216 * only honour it upon first run; abuse *is_sighdl* as an indicator */
217 if (is_sighdl
== 0) {
221 if ((cp
= getenv("LINES")) != NULL
&&
222 (i
= strtol(cp
, NULL
, 10)) > 0)
223 scrnheight
= realscreenheight
= (int)i
;
224 if ((cp
= getenv("COLUMNS")) != NULL
&&
225 (i
= strtol(cp
, NULL
, 10)) > 0)
228 if (scrnwidth
!= 0 && scrnheight
!= 0)
233 if (ioctl(1, TIOCGWINSZ
, &ws
) < 0)
234 ws
.ws_col
= ws
.ws_row
= 0;
235 #elif defined TIOCGSIZE
236 if (ioctl(1, TIOCGSIZE
, &ws
) < 0)
237 ts
.ts_lines
= ts
.ts_cols
= 0;
240 if (scrnheight
== 0) {
241 speed_t ospeed
= (tcgetattr(1, &tbuf
) < 0) ? B9600
:
246 else if (ospeed
== B1200
)
249 else if (ws
.ws_row
!= 0)
250 scrnheight
= ws
.ws_row
;
251 #elif defined TIOCGSIZE
252 else if (ts
.ts_lines
!= 0)
253 scrnheight
= ts
.ts_lines
;
258 #if defined TIOCGWINSZ || defined TIOCGSIZE
261 (realscreenheight
= ws
.ws_row
)
263 (realscreenheight
= ts
.ts_lines
)
266 realscreenheight
= 24;
270 if (scrnwidth
== 0 && 0 ==
272 (scrnwidth
= ws
.ws_col
)
273 #elif defined TIOCGSIZE
274 (scrnwidth
= ts
.ts_cols
)
281 if (is_sighdl
&& (options
& OPT_INTERACTIVE
))
282 tty_signal(SIGWINCH
);
286 static sigjmp_buf __hdrjmp
; /* XXX */
289 _rcv_mode(char const *folder
)
293 sighandler_type prevint
;
297 else if (*folder
== '@') {
298 /* This must be treated specially to make invocation like
299 * -A imap -f @mailbox work */
300 if ((cp
= value("folder")) != NULL
&&
301 which_protocol(cp
) == PROTO_IMAP
)
302 (void)n_strlcpy(mailname
, cp
, MAXPATHLEN
);
305 i
= setfile(folder
, 0);
307 exit(1); /* error already reported */
308 if (options
& OPT_EXISTONLY
)
311 if (options
& OPT_HEADERSONLY
) {
313 if (mb
.mb_type
== MB_IMAP
)
314 imap_getheaders(1, msgCount
);
316 time_current_update(&time_current
, FAL0
);
317 for (i
= 1; i
<= msgCount
; i
++)
318 printhead(i
, stdout
, 0);
322 callhook(mailname
, 0);
323 if (i
> 0 && value("emptystart") == NULL
)
326 if (sigsetjmp(__hdrjmp
, 1) == 0) {
327 if ((prevint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
328 safe_signal(SIGINT
, _hdrstop
);
329 if ((options
& OPT_N_FLAG
) == 0) {
330 if (! value("quiet"))
332 "%s version %s. Type ? for help.\n"),
333 value("bsdcompat") ? "Mail" : uagent
,
338 safe_signal(SIGINT
, prevint
);
341 /* Enter the command loop */
342 if (options
& OPT_INTERACTIVE
)
345 if (options
& OPT_INTERACTIVE
)
348 if (mb
.mb_type
== MB_FILE
|| mb
.mb_type
== MB_MAILDIR
) {
349 safe_signal(SIGHUP
, SIG_IGN
);
350 safe_signal(SIGINT
, SIG_IGN
);
351 safe_signal(SIGQUIT
, SIG_IGN
);
353 n_strlcpy(mboxname
, expand("&"), sizeof(mboxname
));
364 fprintf(stderr
, tr(141, "\nInterrupt\n"));
365 siglongjmp(__hdrjmp
, 1);
368 char const *const weekday_names
[7 + 1] = {
369 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
371 char const *const month_names
[12 + 1] = {
372 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
373 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
376 char const *const uagent
= UAGENT
;
377 char const *const version
= VERSION
;
379 sighandler_type dflpipe
= SIG_DFL
;
382 main(int argc
, char *argv
[])
384 static char const optstr
[] = "A:a:Bb:c:DdEeFfHiNnO:q:Rr:S:s:tu:Vv~#",
387 " %s [-BDdEFintv~] [-A acc] [-a attachment] "
388 "[-b bcc-addr] [-c cc-addr]\n"
389 "\t [-O mtaopt [-O mtaopt-arg]] [-q file] [-r from-addr] "
391 "\t [-s subject] to-addr...\n"
392 " %s [-BDdEeHiNnRv~#] [-A acct] "
393 "[-S var[=value]] -f [file]\n"
394 " %s [-BDdEeiNnRv~#] [-A acc] [-S var[=value]] [-u user]\n";
396 struct a_arg
*a_head
= NULL
, *a_curr
= /* silence CC */ NULL
;
397 struct name
*to
= NULL
, *cc
= NULL
, *bcc
= NULL
;
398 struct attachment
*attach
= NULL
;
399 char *cp
= NULL
, *subject
= NULL
, *qf
= NULL
, *Aflag
= NULL
;
400 char const *okey
, **oargs
= NULL
, *folder
= NULL
;
401 int oargs_size
= 0, oargs_count
= 0, smopts_size
= 0, i
;
404 * Start our lengthy setup
408 unset_allow_undefined
= TRU1
;
413 /* Command line parsing */
414 while ((i
= getopt(argc
, argv
, optstr
)) >= 0) {
417 /* Execute an account command later on */
421 { struct a_arg
*nap
= ac_alloc(
422 sizeof(struct a_arg
));
426 a_curr
->aa_next
= nap
;
428 nap
->aa_file
= optarg
;
431 options
|= OPT_SENDMODE
;
434 /* Make 0/1 line buffered */
435 setvbuf(stdin
, NULL
, _IOLBF
, 0);
436 setvbuf(stdout
, NULL
, _IOLBF
, 0);
439 /* Get Blind Carbon Copy Recipient list */
440 bcc
= cat(bcc
, checkaddrs(lextract(optarg
,GBCC
|GFULL
)));
441 options
|= OPT_SENDMODE
;
444 /* Get Carbon Copy Recipient list */
445 cc
= cat(cc
, checkaddrs(lextract(optarg
, GCC
|GFULL
)));
446 options
|= OPT_SENDMODE
;
450 okey
= "disconnected";
462 okey
= "skipemptybody";
465 options
|= OPT_EXISTONLY
;
468 options
|= OPT_F_FLAG
| OPT_SENDMODE
;
471 /* User is specifying file to "edit" with Mail,
472 * as opposed to reading system mailbox.
473 * If no argument is given, we read his mbox file.
474 * Check for remaining arguments later */
478 options
|= OPT_HEADERSONLY
;
481 /* Ignore interrupts */
485 /* Avoid initial header printing */
489 /* Don't source "unspecified system start-up file" */
490 options
|= OPT_NOSRC
;
493 /* Additional options to pass-through to MTA */
494 if (smopts_count
== (size_t)smopts_size
)
495 smopts_size
= _grow_cpp(&smopts
, smopts_size
,
497 smopts
[smopts_count
++] = skin(optarg
);
500 /* Quote file TODO drop? -Q with real quote?? what ? */
503 options
|= OPT_SENDMODE
;
506 /* Open folders read-only */
507 options
|= OPT_R_FLAG
;
510 /* Set From address. */
511 options
|= OPT_r_FLAG
;
512 if ((option_r_arg
= optarg
)[0] != '\0') {
513 struct name
*fa
= nalloc(optarg
, GFULL
);
515 if (is_addr_invalid(fa
, 1) ||
516 is_fileorpipe_addr(fa
)) {
517 fprintf(stderr
, tr(271,
518 "Invalid address argument "
522 option_r_arg
= fa
->n_name
;
523 /* TODO -r options is set in smopts, but may
524 * TODO be overwritten by setting from= in
525 * TODO an interactive session!
526 * TODO Maybe disable setting of from?
527 * TODO Warn user? Update manual!! */
528 okey
= savecat("from=", fa
->n_fullname
);
530 /* ..and fa goes even though it is ready :/ */
534 /* Set variable. We need to do this twice, since the
535 * user surely wants the setting to take effect
536 * immediately, but also doesn't want it to be
537 * overwritten from within resource files */
539 okey
= a
[0] = optarg
;
544 if (oargs_count
== oargs_size
)
545 oargs_size
= _grow_cpp(&oargs
, oargs_size
,
547 oargs
[oargs_count
++] = okey
;
552 options
|= OPT_SENDMODE
;
555 /* Read defined set of headers from mail to be send */
556 options
|= OPT_SENDMODE
| OPT_t_FLAG
;
559 /* Set user name to pretend to be */
560 myname
= option_u_arg
= optarg
;
574 /* Enable tilde escapes even in non-interactive mode */
575 options
|= OPT_TILDE_FLAG
;
578 /* Work in batch mode, even if non-interactive */
579 if (oargs_count
+ 3 >= oargs_size
)
580 oargs_size
= _grow_cpp(&oargs
, oargs_size
,
582 oargs
[oargs_count
++] = "dot";
583 oargs
[oargs_count
++] = "emptystart";
584 oargs
[oargs_count
++] = "noheader";
585 oargs
[oargs_count
++] = "sendwait";
586 options
|= OPT_TILDE_FLAG
| OPT_BATCH_FLAG
;
589 usage
: fprintf(stderr
, tr(135, usagestr
),
590 progname
, progname
, progname
);
595 if (folder
!= NULL
) {
597 if (optind
+ 1 < argc
) {
598 fprintf(stderr
, tr(205,
599 "More than one file given with -f\n"));
602 folder
= argv
[optind
];
605 for (i
= optind
; argv
[i
]; i
++)
606 to
= cat(to
, checkaddrs(lextract(argv
[i
], GTO
|GFULL
)));
608 options
|= OPT_SENDMODE
;
611 /* Check for inconsistent arguments */
612 if (folder
!= NULL
&& to
!= NULL
) {
613 fprintf(stderr
, tr(137,
614 "Cannot give -f and people to send to.\n"));
617 if ((options
& (OPT_SENDMODE
|OPT_t_FLAG
)) == OPT_SENDMODE
&&
619 fprintf(stderr
, tr(138,
620 "Send options without primary recipient specified.\n"));
623 if ((options
& OPT_R_FLAG
) && to
!= NULL
) {
624 fprintf(stderr
, "The -R option is meaningless in send mode.\n");
629 * Likely to go, perform more setup
634 if (options
& OPT_INTERACTIVE
) {
637 # ifndef TTY_WANTS_SIGWINCH
638 if (safe_signal(SIGWINCH
, SIG_IGN
) != SIG_IGN
)
640 safe_signal(SIGWINCH
, _setscreensize
);
643 scrnheight
= realscreenheight
= 24, scrnwidth
= 80;
645 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
648 if ((options
& OPT_NOSRC
) == 0 && getenv("NAIL_NO_SYSTEM_RC") == NULL
)
650 /* *expand() returns a savestr(), but load only uses the file name
651 * for fopen(), so it's safe to do this */
652 if ((cp
= getenv("MAILRC")) != NULL
)
653 load(file_expand(cp
));
654 else if ((cp
= getenv("NAILRC")) != NULL
)
655 load(file_expand(cp
));
657 load(file_expand("~/.mailrc"));
658 if (getenv("NAIL_EXTRA_RC") == NULL
&&
659 (cp
= value("NAIL_EXTRA_RC")) != NULL
)
660 load(file_expand(cp
));
662 /* Now we can set the account */
670 /* Ensure the -S and other command line options take precedence over
671 * anything that may have been placed in resource files */
672 for (i
= 0; i
< oargs_count
; ++i
) {
680 * We're finally completely setup and ready to go
684 unset_allow_undefined
= FAL0
;
686 if (options
& OPT_DEBUG
)
687 fprintf(stderr
, tr(199, "user = %s, homedir = %s\n"),
690 if (! (options
& OPT_SENDMODE
))
691 return _rcv_mode(folder
);
693 /* Now that full mailx(1)-style file expansion is possible handle the
694 * attachments which we had delayed due to this.
695 * This may use savestr(), but since we won't enter the command loop we
696 * don't need to care about that */
697 while (a_head
!= NULL
) {
698 attach
= add_attachment(attach
, a_head
->aa_file
, NULL
);
699 if (attach
!= NULL
) {
701 a_head
= a_head
->aa_next
;
704 perror(a_head
->aa_file
);
709 /* xxx exit_status = EXIT_OK; */
710 if (options
& OPT_INTERACTIVE
)
712 mail(to
, cc
, bcc
, subject
, attach
, qf
, ((options
& OPT_F_FLAG
) != 0));
713 if (options
& OPT_INTERACTIVE
)