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>
69 struct a_arg
*aa_next
;
73 /* Perform basic startup initialization */
74 static void _startup(void);
77 static int _grow_cpp(char const ***cpp
, int size
, int count
);
79 /* Initialize *tempdir*, *myname*, *homedir* */
80 static void _setup_vars(void);
82 /* We're in an interactive session - compute what the screen size for printing
83 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
84 * We use the following algorithm for the height:
85 * If baud rate < 1200, use 9
86 * If baud rate = 1200, use 14
87 * If baud rate > 1200, use 24 or ws_row
88 * Width is either 80 or ws_col */
89 static void _setscreensize(int is_sighdl
);
91 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
92 * the system mailbox, and open up the right stuff */
93 static int _rcv_mode(char const *folder
);
95 /* Interrupt printing of the headers */
96 static void _hdrstop(int signo
);
103 /* Absolutely the first thing we do is save our egid
104 * and set it to the rgid, so that we can safely run
105 * setgid. We use the sgid (saved set-gid) to allow ourselves
106 * to revert to the egid if we want (temporarily) to become
108 effectivegid
= getegid();
110 if (setgid(realgid
) < 0) {
117 if ((cp
= strrchr(progname
, '/')) != NULL
)
120 /* Set up a reasonable environment.
121 * Figure out whether we are being run interactively,
122 * start the SIGCHLD catcher, and so forth */
123 safe_signal(SIGCHLD
, sigchild
);
126 options
|= OPT_TTYIN
| OPT_INTERACTIVE
;
128 options
|= OPT_TTYOUT
;
129 if (IS_TTY_SESSION())
130 safe_signal(SIGPIPE
, dflpipe
= SIG_IGN
);
131 assign("header", "");
134 #ifdef HAVE_SETLOCALE
135 setlocale(LC_ALL
, "");
136 mb_cur_max
= MB_CUR_MAX
;
137 # if defined HAVE_NL_LANGINFO && defined CODESET
138 if (value("ttycharset") == NULL
&& (cp
= nl_langinfo(CODESET
)) != NULL
)
139 assign("ttycharset", cp
);
142 # if defined HAVE_MBTOWC && defined HAVE_WCTYPE_H
143 if (mb_cur_max
> 1) {
145 if (mbtowc(&wc
, "\303\266", 2) == 2 && wc
== 0xF6 &&
146 mbtowc(&wc
, "\342\202\254", 3) == 3 &&
149 /* Reset state - it may have been messed up; luckily this also
150 * gives us an indication wether the encoding has locking shift
152 /* TODO temporary - use option bits! */
153 enc_has_state
= mbtowc(&wc
, NULL
, mb_cur_max
);
161 # ifdef NL_CAT_LOCALE
162 catd
= catopen(CATNAME
, NL_CAT_LOCALE
);
164 catd
= catopen(CATNAME
, 0);
169 iconvd
= (iconv_t
)-1;
174 _grow_cpp(char const ***cpp
, int size
, int count
)
176 /* Before spreserve(): use our string pool instead of LibC heap;
177 * Increment *size* by at least 5! */
178 char const **newcpp
= salloc(sizeof(char*) * (size
+= 8));
181 memcpy(newcpp
, *cpp
, count
* sizeof(char*));
189 /* Before spreserve(): use our string pool instead of LibC heap */
192 tempdir
= ((cp
= getenv("TMPDIR")) != NULL
) ? savestr(cp
) : "/tmp";
194 myname
= savestr(username());
196 if ((cp
= getenv("HOME")) == NULL
)
198 homedir
= savestr(cp
);
202 _setscreensize(int is_sighdl
)
207 #elif defined TIOCGSIZE
211 scrnheight
= realscreenheight
= scrnwidth
= 0;
213 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
214 * a little bit to be able to honour resizes during our lifetime and
215 * only honour it upon first run; abuse *is_sighdl* as an indicator */
216 if (is_sighdl
== 0) {
220 if ((cp
= getenv("LINES")) != NULL
&&
221 (i
= strtol(cp
, NULL
, 10)) > 0)
222 scrnheight
= realscreenheight
= (int)i
;
223 if ((cp
= getenv("COLUMNS")) != NULL
&&
224 (i
= strtol(cp
, NULL
, 10)) > 0)
227 if (scrnwidth
!= 0 && scrnheight
!= 0)
232 if (ioctl(1, TIOCGWINSZ
, &ws
) < 0)
233 ws
.ws_col
= ws
.ws_row
= 0;
234 #elif defined TIOCGSIZE
235 if (ioctl(1, TIOCGSIZE
, &ws
) < 0)
236 ts
.ts_lines
= ts
.ts_cols
= 0;
239 if (scrnheight
== 0) {
240 speed_t ospeed
= (tcgetattr(1, &tbuf
) < 0) ? B9600
:
245 else if (ospeed
== B1200
)
248 else if (ws
.ws_row
!= 0)
249 scrnheight
= ws
.ws_row
;
250 #elif defined TIOCGSIZE
251 else if (ts
.ts_lines
!= 0)
252 scrnheight
= ts
.ts_lines
;
257 #if defined TIOCGWINSZ || defined TIOCGSIZE
260 (realscreenheight
= ws
.ws_row
)
262 (realscreenheight
= ts
.ts_lines
)
265 realscreenheight
= 24;
269 if (scrnwidth
== 0 && 0 ==
271 (scrnwidth
= ws
.ws_col
)
272 #elif defined TIOCGSIZE
273 (scrnwidth
= ts
.ts_cols
)
280 if (is_sighdl
&& (options
& OPT_INTERACTIVE
))
281 tty_signal(SIGWINCH
);
285 static sigjmp_buf __hdrjmp
; /* XXX */
288 _rcv_mode(char const *folder
)
292 sighandler_type prevint
;
296 else if (*folder
== '@') {
297 /* This must be treated specially to make invocation like
298 * -A imap -f @mailbox work */
299 if ((cp
= value("folder")) != NULL
&&
300 which_protocol(cp
) == PROTO_IMAP
)
301 (void)n_strlcpy(mailname
, cp
, MAXPATHLEN
);
304 i
= setfile(folder
, 0);
306 exit(1); /* error already reported */
307 if (options
& OPT_EXISTONLY
)
310 if (options
& OPT_HEADERSONLY
) {
312 if (mb
.mb_type
== MB_IMAP
)
313 imap_getheaders(1, msgCount
);
315 time_current_update(&time_current
, FAL0
);
316 for (i
= 1; i
<= msgCount
; i
++)
317 printhead(i
, stdout
, 0);
321 callhook(mailname
, 0);
322 if (i
> 0 && value("emptystart") == NULL
)
325 if (sigsetjmp(__hdrjmp
, 1) == 0) {
326 if ((prevint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
327 safe_signal(SIGINT
, _hdrstop
);
328 if ((options
& OPT_N_FLAG
) == 0) {
329 if (! value("quiet"))
331 "%s version %s. Type ? for help.\n"),
332 value("bsdcompat") ? "Mail" : uagent
,
337 safe_signal(SIGINT
, prevint
);
340 /* Enter the command loop */
341 if (options
& OPT_INTERACTIVE
)
344 if (options
& OPT_INTERACTIVE
)
347 if (mb
.mb_type
== MB_FILE
|| mb
.mb_type
== MB_MAILDIR
) {
348 safe_signal(SIGHUP
, SIG_IGN
);
349 safe_signal(SIGINT
, SIG_IGN
);
350 safe_signal(SIGQUIT
, SIG_IGN
);
352 n_strlcpy(mboxname
, expand("&"), sizeof(mboxname
));
363 fprintf(stderr
, tr(141, "\nInterrupt\n"));
364 siglongjmp(__hdrjmp
, 1);
367 char const *const weekday_names
[7 + 1] = {
368 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
370 char const *const month_names
[12 + 1] = {
371 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
372 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
375 sighandler_type dflpipe
= SIG_DFL
;
378 main(int argc
, char *argv
[])
380 static char const optstr
[] = "A:a:Bb:c:DdEeFfHiNnO:q:Rr:S:s:tu:Vv~#",
383 " %s [-BDdEFintv~] [-A acc] [-a attachment] "
384 "[-b bcc-addr] [-c cc-addr]\n"
385 "\t [-O mtaopt [-O mtaopt-arg]] [-q file] [-r from-addr] "
387 "\t [-s subject] to-addr...\n"
388 " %s [-BDdEeHiNnRv~#] [-A acct] "
389 "[-S var[=value]] -f [file]\n"
390 " %s [-BDdEeiNnRv~#] [-A acc] [-S var[=value]] [-u user]\n";
392 struct a_arg
*a_head
= NULL
, *a_curr
= /* silence CC */ NULL
;
393 struct name
*to
= NULL
, *cc
= NULL
, *bcc
= NULL
;
394 struct attachment
*attach
= NULL
;
395 char *cp
= NULL
, *subject
= NULL
, *qf
= NULL
, *Aflag
= NULL
;
396 char const *okey
, **oargs
= NULL
, *folder
= NULL
;
397 int oargs_size
= 0, oargs_count
= 0, smopts_size
= 0, i
;
400 * Start our lengthy setup
404 unset_allow_undefined
= TRU1
;
409 /* Command line parsing */
410 while ((i
= getopt(argc
, argv
, optstr
)) >= 0) {
413 /* Execute an account command later on */
417 { struct a_arg
*nap
= ac_alloc(
418 sizeof(struct a_arg
));
422 a_curr
->aa_next
= nap
;
424 nap
->aa_file
= optarg
;
427 options
|= OPT_SENDMODE
;
430 /* Make 0/1 line buffered */
431 setvbuf(stdin
, NULL
, _IOLBF
, 0);
432 setvbuf(stdout
, NULL
, _IOLBF
, 0);
435 /* Get Blind Carbon Copy Recipient list */
436 bcc
= cat(bcc
, checkaddrs(lextract(optarg
,GBCC
|GFULL
)));
437 options
|= OPT_SENDMODE
;
440 /* Get Carbon Copy Recipient list */
441 cc
= cat(cc
, checkaddrs(lextract(optarg
, GCC
|GFULL
)));
442 options
|= OPT_SENDMODE
;
446 okey
= "disconnected";
458 okey
= "skipemptybody";
461 options
|= OPT_EXISTONLY
;
464 options
|= OPT_F_FLAG
| OPT_SENDMODE
;
467 /* User is specifying file to "edit" with Mail,
468 * as opposed to reading system mailbox.
469 * If no argument is given, we read his mbox file.
470 * Check for remaining arguments later */
474 options
|= OPT_HEADERSONLY
;
477 /* Ignore interrupts */
481 /* Avoid initial header printing */
485 /* Don't source "unspecified system start-up file" */
486 options
|= OPT_NOSRC
;
489 /* Additional options to pass-through to MTA */
490 if (smopts_count
== (size_t)smopts_size
)
491 smopts_size
= _grow_cpp(&smopts
, smopts_size
,
493 smopts
[smopts_count
++] = skin(optarg
);
496 /* Quote file TODO drop? -Q with real quote?? what ? */
499 options
|= OPT_SENDMODE
;
502 /* Open folders read-only */
503 options
|= OPT_R_FLAG
;
506 /* Set From address. */
507 options
|= OPT_r_FLAG
;
508 if ((option_r_arg
= optarg
)[0] != '\0') {
509 struct name
*fa
= nalloc(optarg
, GFULL
);
511 if (is_addr_invalid(fa
, 1) ||
512 is_fileorpipe_addr(fa
)) {
513 fprintf(stderr
, tr(271,
514 "Invalid address argument "
518 option_r_arg
= fa
->n_name
;
519 /* TODO -r options is set in smopts, but may
520 * TODO be overwritten by setting from= in
521 * TODO an interactive session!
522 * TODO Maybe disable setting of from?
523 * TODO Warn user? Update manual!! */
524 okey
= savecat("from=", fa
->n_fullname
);
526 /* ..and fa goes even though it is ready :/ */
530 /* Set variable. We need to do this twice, since the
531 * user surely wants the setting to take effect
532 * immediately, but also doesn't want it to be
533 * overwritten from within resource files */
535 okey
= a
[0] = optarg
;
540 if (oargs_count
== oargs_size
)
541 oargs_size
= _grow_cpp(&oargs
, oargs_size
,
543 oargs
[oargs_count
++] = okey
;
548 options
|= OPT_SENDMODE
;
551 /* Read defined set of headers from mail to be send */
552 options
|= OPT_SENDMODE
| OPT_t_FLAG
;
555 /* Set user name to pretend to be */
556 myname
= option_u_arg
= optarg
;
570 /* Enable tilde escapes even in non-interactive mode */
571 options
|= OPT_TILDE_FLAG
;
574 /* Work in batch mode, even if non-interactive */
575 if (oargs_count
+ 3 >= oargs_size
)
576 oargs_size
= _grow_cpp(&oargs
, oargs_size
,
578 oargs
[oargs_count
++] = "dot";
579 oargs
[oargs_count
++] = "emptystart";
580 oargs
[oargs_count
++] = "noheader";
581 oargs
[oargs_count
++] = "sendwait";
582 options
|= OPT_TILDE_FLAG
| OPT_BATCH_FLAG
;
585 usage
: fprintf(stderr
, tr(135, usagestr
),
586 progname
, progname
, progname
);
591 if (folder
!= NULL
) {
593 if (optind
+ 1 < argc
) {
594 fprintf(stderr
, tr(205,
595 "More than one file given with -f\n"));
598 folder
= argv
[optind
];
601 for (i
= optind
; argv
[i
]; i
++)
602 to
= cat(to
, checkaddrs(lextract(argv
[i
], GTO
|GFULL
)));
604 options
|= OPT_SENDMODE
;
607 /* Check for inconsistent arguments */
608 if (folder
!= NULL
&& to
!= NULL
) {
609 fprintf(stderr
, tr(137,
610 "Cannot give -f and people to send to.\n"));
613 if ((options
& (OPT_SENDMODE
|OPT_t_FLAG
)) == OPT_SENDMODE
&&
615 fprintf(stderr
, tr(138,
616 "Send options without primary recipient specified.\n"));
619 if ((options
& OPT_R_FLAG
) && to
!= NULL
) {
620 fprintf(stderr
, "The -R option is meaningless in send mode.\n");
625 * Likely to go, perform more setup
630 if (options
& OPT_INTERACTIVE
) {
633 # ifndef TTY_WANTS_SIGWINCH
634 if (safe_signal(SIGWINCH
, SIG_IGN
) != SIG_IGN
)
636 safe_signal(SIGWINCH
, _setscreensize
);
639 scrnheight
= realscreenheight
= 24, scrnwidth
= 80;
641 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
644 if ((options
& OPT_NOSRC
) == 0)
646 /* *expand() returns a savestr(), but load only uses the file name
647 * for fopen(), so it's safe to do this */
648 if ((cp
= getenv("MAILRC")) != NULL
)
649 load(file_expand(cp
));
650 else if ((cp
= getenv("NAILRC")) != NULL
)
651 load(file_expand(cp
));
653 load(file_expand("~/.mailrc"));
654 if (getenv("NAIL_EXTRA_RC") == NULL
&&
655 (cp
= value("NAIL_EXTRA_RC")) != NULL
)
656 load(file_expand(cp
));
658 /* Now we can set the account */
666 /* Ensure the -S and other command line options take precedence over
667 * anything that may have been placed in resource files */
668 for (i
= 0; i
< oargs_count
; ++i
) {
676 * We're finally completely setup and ready to go
680 unset_allow_undefined
= FAL0
;
682 if (options
& OPT_DEBUG
)
683 fprintf(stderr
, tr(199, "user = %s, homedir = %s\n"),
686 if (! (options
& OPT_SENDMODE
))
687 return _rcv_mode(folder
);
689 /* Now that full mailx(1)-style file expansion is possible handle the
690 * attachments which we had delayed due to this.
691 * This may use savestr(), but since we won't enter the command loop we
692 * don't need to care about that */
693 while (a_head
!= NULL
) {
694 attach
= add_attachment(attach
, a_head
->aa_file
, NULL
);
695 if (attach
!= NULL
) {
697 a_head
= a_head
->aa_next
;
700 perror(a_head
->aa_file
);
705 /* xxx exit_status = EXIT_OK; */
706 if (options
& OPT_INTERACTIVE
)
708 mail(to
, cc
, bcc
, subject
, attach
, qf
, ((options
& OPT_F_FLAG
) != 0));
709 if (options
& OPT_INTERACTIVE
)