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>
53 #include <sys/ioctl.h>
58 #ifdef HAVE_NL_LANGINFO
59 # include <langinfo.h>
68 struct a_arg
*aa_next
;
72 /* Perform basic startup initialization */
73 static void _startup(void);
76 static int _grow_cpp(char const ***cpp
, int size
, int cnt
);
78 /* Initialize *tempdir*, *myname*, *homedir* */
79 static void _setup_vars(void);
81 /* We're in an interactive session - compute what the screen size for printing
82 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
83 * We use the following algorithm for the height:
84 * If baud rate < 1200, use 9
85 * If baud rate = 1200, use 14
86 * If baud rate > 1200, use 24 or ws_row
87 * Width is either 80 or ws_col */
88 static void _setscreensize(int is_sighdl
);
90 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
91 * the system mailbox, and open up the right stuff */
92 static int _rcv_mode(char const *folder
);
94 /* Interrupt printing of the headers */
95 static void _hdrstop(int signo
);
102 /* Absolutely the first thing we do is save our egid
103 * and set it to the rgid, so that we can safely run
104 * setgid. We use the sgid (saved set-gid) to allow ourselves
105 * to revert to the egid if we want (temporarily) to become
106 * privileged XXX (s-nail-)*dotlock(-program)* */
107 effectivegid
= getegid();
109 if (setgid(realgid
) < 0) {
116 if ((cp
= strrchr(progname
, '/')) != NULL
)
119 /* Set up a reasonable environment.
120 * Figure out whether we are being run interactively,
121 * start the SIGCHLD catcher, and so forth */
122 safe_signal(SIGCHLD
, sigchild
);
124 if (isatty(STDIN_FILENO
)) /* TODO should be isatty(0) && isatty(2)?? */
125 options
|= OPT_TTYIN
| OPT_INTERACTIVE
;
126 if (isatty(STDOUT_FILENO
))
127 options
|= OPT_TTYOUT
;
128 if (IS_TTY_SESSION())
129 safe_signal(SIGPIPE
, dflpipe
= SIG_IGN
);
130 assign("header", "");
133 #ifdef HAVE_SETLOCALE
134 setlocale(LC_ALL
, "");
135 mb_cur_max
= MB_CUR_MAX
;
136 # if defined HAVE_NL_LANGINFO && defined CODESET
137 if (voption("ttycharset") == NULL
&& (cp
= nl_langinfo(CODESET
)) != NULL
)
138 assign("ttycharset", cp
);
141 # if defined HAVE_MBTOWC && defined HAVE_WCTYPE_H
142 if (mb_cur_max
> 1) {
144 if (mbtowc(&wc
, "\303\266", 2) == 2 && wc
== 0xF6 &&
145 mbtowc(&wc
, "\342\202\254", 3) == 3 && wc
== 0x20AC)
147 /* Reset state - it may have been messed up; luckily this also
148 * gives us an indication wether the encoding has locking shift
150 /* TODO temporary - use option bits! */
151 enc_has_state
= mbtowc(&wc
, NULL
, mb_cur_max
);
159 # ifdef NL_CAT_LOCALE
160 catd
= catopen(CATNAME
, NL_CAT_LOCALE
);
162 catd
= catopen(CATNAME
, 0);
167 iconvd
= (iconv_t
)-1;
172 _grow_cpp(char const ***cpp
, int size
, int cnt
)
174 /* Before spreserve(): use our string pool instead of LibC heap;
175 * Increment *size* by at least 5! */
176 char const **newcpp
= salloc(sizeof(char*) * (size
+= 8));
179 memcpy(newcpp
, *cpp
, (size_t)cnt
* sizeof(char*));
187 /* Before spreserve(): use our string pool instead of LibC heap */
188 /* XXX further check paths? */
191 struct passwd
*pwuid
, *pw
;
193 tempdir
= ((cp
= getenv("TMPDIR")) != NULL
) ? savestr(cp
) : TMPDIR_FALLBACK
;
195 cp
= (myname
== NULL
) ? getenv("USER") : myname
;
197 if ((pwuid
= getpwuid(uid
)) == NULL
)
198 panic(tr(201, "Cannot associate a name with uid %lu"), (ul_it
)uid
);
200 myname
= pwuid
->pw_name
;
201 else if ((pw
= getpwnam(cp
)) == NULL
)
202 panic(tr(236, "`%s' is not a user of this system"), cp
);
204 myname
= pw
->pw_name
;
205 if (pw
->pw_uid
!= uid
)
206 options
|= OPT_u_FLAG
;
208 myname
= savestr(myname
);
209 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
211 if ((cp
= getenv("HOME")) == NULL
)
212 cp
= "."; /* XXX User and Login objects; Login: pw->pw_dir */
213 homedir
= savestr(cp
);
217 _setscreensize(int is_sighdl
)
222 #elif defined TIOCGSIZE
226 scrnheight
= realscreenheight
= scrnwidth
= 0;
228 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
229 * a little bit to be able to honour resizes during our lifetime and
230 * only honour it upon first run; abuse *is_sighdl* as an indicator */
235 if ((cp
= getenv("LINES")) != NULL
&& (i
= strtol(cp
, NULL
, 10)) > 0)
236 scrnheight
= realscreenheight
= (int)i
;
237 if ((cp
= getenv("COLUMNS")) != NULL
&& (i
= strtol(cp
, NULL
, 10)) > 0)
240 if (scrnwidth
!= 0 && scrnheight
!= 0)
245 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) < 0)
246 ws
.ws_col
= ws
.ws_row
= 0;
247 #elif defined TIOCGSIZE
248 if (ioctl(STDOUT_FILENO
, TIOCGSIZE
, &ws
) < 0)
249 ts
.ts_lines
= ts
.ts_cols
= 0;
252 if (scrnheight
== 0) {
253 speed_t ospeed
= ((tcgetattr(STDOUT_FILENO
, &tbuf
) < 0)
254 ? B9600
: cfgetospeed(&tbuf
));
258 else if (ospeed
== B1200
)
261 else if (ws
.ws_row
!= 0)
262 scrnheight
= ws
.ws_row
;
263 #elif defined TIOCGSIZE
264 else if (ts
.ts_lines
!= 0)
265 scrnheight
= ts
.ts_lines
;
270 #if defined TIOCGWINSZ || defined TIOCGSIZE
273 (realscreenheight
= ws
.ws_row
)
275 (realscreenheight
= ts
.ts_lines
)
278 realscreenheight
= 24;
282 if (scrnwidth
== 0 && 0 ==
284 (scrnwidth
= ws
.ws_col
)
285 #elif defined TIOCGSIZE
286 (scrnwidth
= ts
.ts_cols
)
293 if (is_sighdl
&& IS_TTY_SESSION())
294 tty_signal(SIGWINCH
);
298 static sigjmp_buf __hdrjmp
; /* XXX */
301 _rcv_mode(char const *folder
)
305 sighandler_type prevint
;
309 else if (*folder
== '@') {
310 /* This must be treated specially to make invocation like
311 * -A imap -f @mailbox work */
312 if ((cp
= value("folder")) != NULL
&& which_protocol(cp
) == PROTO_IMAP
)
313 (void)n_strlcpy(mailname
, cp
, MAXPATHLEN
);
316 i
= setfile(folder
, 0);
318 exit(1); /* error already reported */
319 if (options
& OPT_EXISTONLY
)
322 if (options
& OPT_HEADERSONLY
) {
324 if (mb
.mb_type
== MB_IMAP
)
325 imap_getheaders(1, msgCount
);
327 time_current_update(&time_current
, FAL0
);
328 for (i
= 1; i
<= msgCount
; ++i
)
329 printhead(i
, stdout
, 0);
333 callhook(mailname
, 0);
334 if (i
> 0 && !boption("emptystart"))
337 if (sigsetjmp(__hdrjmp
, 1) == 0) {
338 if ((prevint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
339 safe_signal(SIGINT
, _hdrstop
);
340 if (!(options
& OPT_N_FLAG
)) {
342 printf(tr(140, "%s version %s. Type ? for help.\n"),
343 (boption("bsdcompat") ? "Mail" : uagent
), version
);
347 safe_signal(SIGINT
, prevint
);
350 /* Enter the command loop */
351 if (options
& OPT_INTERACTIVE
)
354 if (options
& OPT_INTERACTIVE
)
357 if (mb
.mb_type
== MB_FILE
|| mb
.mb_type
== MB_MAILDIR
) {
358 safe_signal(SIGHUP
, SIG_IGN
);
359 safe_signal(SIGINT
, SIG_IGN
);
360 safe_signal(SIGQUIT
, SIG_IGN
);
362 if ((cp
= expand("&")) == NULL
)
364 n_strlcpy(mboxname
, cp
, sizeof(mboxname
));
375 fprintf(stderr
, tr(141, "\nInterrupt\n"));
376 siglongjmp(__hdrjmp
, 1);
379 char const weekday_names
[7 + 1][4] = {
380 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", ""
382 char const month_names
[12 + 1][4] = {
383 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
384 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""
387 char const uagent
[] = UAGENT
;
388 char const version
[] = VERSION
;
390 sighandler_type dflpipe
= SIG_DFL
;
393 main(int argc
, char *argv
[])
395 static char const optstr
[] = "A:a:Bb:c:DdEeFfHiNnO:q:Rr:S:s:tu:Vv~#",
398 " %s [-BDdEFintv~] [-A acc] [-a attachment] "
399 "[-b bcc-addr] [-c cc-addr]\n"
400 "\t [-O mtaopt [-O mtaopt-arg]] [-q file] [-r from-addr] "
402 "\t [-s subject] to-addr...\n"
403 " %s [-BDdEeHiNnRv~#] [-A acct] "
404 "[-S var[=value]] -f [file]\n"
405 " %s [-BDdEeiNnRv~#] [-A acc] [-S var[=value]] [-u user]\n";
407 struct a_arg
*a_head
= NULL
, *a_curr
= /* silence CC */ NULL
;
408 struct name
*to
= NULL
, *cc
= NULL
, *bcc
= NULL
;
409 struct attachment
*attach
= NULL
;
410 char *cp
= NULL
, *subject
= NULL
, *qf
= NULL
, *Aflag
= NULL
;
411 char const *okey
, **oargs
= NULL
, *folder
= NULL
;
412 int oargs_size
= 0, oargs_count
= 0, smopts_size
= 0, i
;
415 * Start our lengthy setup
419 unset_allow_undefined
= TRU1
;
424 /* Command line parsing */
425 while ((i
= getopt(argc
, argv
, optstr
)) >= 0) {
428 /* Execute an account command later on */
432 { struct a_arg
*nap
= ac_alloc(sizeof(struct a_arg
));
436 a_curr
->aa_next
= nap
;
438 nap
->aa_file
= optarg
;
441 options
|= OPT_SENDMODE
;
444 /* Make 0/1 line buffered */
445 setvbuf(stdin
, NULL
, _IOLBF
, 0);
446 setvbuf(stdout
, NULL
, _IOLBF
, 0);
449 /* Get Blind Carbon Copy Recipient list */
450 bcc
= cat(bcc
, checkaddrs(lextract(optarg
, GBCC
| GFULL
)));
451 options
|= OPT_SENDMODE
;
454 /* Get Carbon Copy Recipient list */
455 cc
= cat(cc
, checkaddrs(lextract(optarg
, GCC
| GFULL
)));
456 options
|= OPT_SENDMODE
;
460 okey
= "disconnected";
472 okey
= "skipemptybody";
475 options
|= OPT_EXISTONLY
;
478 options
|= OPT_F_FLAG
| OPT_SENDMODE
;
481 /* User is specifying file to "edit" with Mail, as opposed to reading
482 * system mailbox. If no argument is given, we read his mbox file.
483 * Check for remaining arguments later */
487 options
|= OPT_HEADERSONLY
;
490 /* Ignore interrupts */
494 /* Avoid initial header printing */
498 /* Don't source "unspecified system start-up file" */
499 options
|= OPT_NOSRC
;
502 /* Additional options to pass-through to MTA */
503 if (smopts_count
== (size_t)smopts_size
)
504 smopts_size
= _grow_cpp(&smopts
, smopts_size
, (int)smopts_count
);
505 smopts
[smopts_count
++] = skin(optarg
);
508 /* Quote file TODO drop? -Q with real quote?? what ? */
511 options
|= OPT_SENDMODE
;
514 /* Open folders read-only */
515 options
|= OPT_R_FLAG
;
518 /* Set From address. */
519 options
|= OPT_r_FLAG
;
520 if ((option_r_arg
= optarg
)[0] != '\0') {
521 struct name
*fa
= nalloc(optarg
, GFULL
);
523 if (is_addr_invalid(fa
, 1) || is_fileorpipe_addr(fa
)) {
524 fprintf(stderr
, tr(271, "Invalid address argument with -r\n"));
527 option_r_arg
= fa
->n_name
;
528 /* TODO -r options is set in smopts, but may
529 * TODO be overwritten by setting from= in
530 * TODO an interactive session!
531 * TODO Maybe disable setting of from?
532 * TODO Warn user? Update manual!! */
533 okey
= savecat("from=", fa
->n_fullname
);
535 /* ..and fa goes even though it is ready :/ */
539 /* Set variable. We need to do this twice, since the
540 * user surely wants the setting to take effect
541 * immediately, but also doesn't want it to be
542 * overwritten from within resource files */
544 okey
= a
[0] = optarg
;
549 if (oargs_count
== oargs_size
)
550 oargs_size
= _grow_cpp(&oargs
, oargs_size
, oargs_count
);
551 oargs
[oargs_count
++] = okey
;
556 options
|= OPT_SENDMODE
;
559 /* Read defined set of headers from mail to be send */
560 options
|= OPT_SENDMODE
| OPT_t_FLAG
;
563 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
564 * done as necessary in _setup_vars() above */
579 /* Enable tilde escapes even in non-interactive mode */
580 options
|= OPT_TILDE_FLAG
;
583 /* Work in batch mode, even if non-interactive */
584 if (oargs_count
+ 3 >= oargs_size
)
585 oargs_size
= _grow_cpp(&oargs
, oargs_size
, oargs_count
);
586 oargs
[oargs_count
++] = "dot";
587 oargs
[oargs_count
++] = "emptystart";
588 oargs
[oargs_count
++] = "noheader";
589 oargs
[oargs_count
++] = "sendwait";
590 options
|= OPT_TILDE_FLAG
| OPT_BATCH_FLAG
;
594 fprintf(stderr
, tr(135, usagestr
), progname
, progname
, progname
);
599 if (folder
!= NULL
) {
601 if (optind
+ 1 < argc
) {
602 fprintf(stderr
, tr(205, "More than one file given with -f\n"));
605 folder
= argv
[optind
];
608 for (i
= optind
; argv
[i
]; ++i
)
609 to
= cat(to
, checkaddrs(lextract(argv
[i
], GTO
| GFULL
)));
611 options
|= OPT_SENDMODE
;
614 /* Check for inconsistent arguments */
615 if (folder
!= NULL
&& to
!= NULL
) {
616 fprintf(stderr
, tr(137, "Cannot give -f and people to send to.\n"));
619 if ((options
& (OPT_SENDMODE
| OPT_t_FLAG
)) == OPT_SENDMODE
&& to
== NULL
) {
620 fprintf(stderr
, tr(138,
621 "Send options without primary recipient specified.\n"));
624 if ((options
& OPT_R_FLAG
) && to
!= NULL
) {
625 fprintf(stderr
, tr(235, "The -R option is meaningless in send mode.\n"));
630 * Likely to go, perform more setup
635 if (options
& OPT_INTERACTIVE
) {
638 # ifndef TTY_WANTS_SIGWINCH
639 if (safe_signal(SIGWINCH
, SIG_IGN
) != SIG_IGN
)
641 safe_signal(SIGWINCH
, _setscreensize
);
644 scrnheight
= realscreenheight
= 24, scrnwidth
= 80;
646 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
649 if (!(options
& OPT_NOSRC
) && getenv("NAIL_NO_SYSTEM_RC") == NULL
)
651 /* *expand() returns a savestr(), but load only uses the file name for
652 * fopen(), so it's safe to do this */
653 if ((cp
= getenv("MAILRC")) != NULL
)
654 load(file_expand(cp
));
655 else if ((cp
= getenv("NAILRC")) != NULL
)
656 load(file_expand(cp
));
658 load(file_expand(MAILRC
));
659 if (getenv("NAIL_EXTRA_RC") == NULL
&& (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"), myname
, homedir
);
689 if (!(options
& OPT_SENDMODE
)) {
690 exit_status
= _rcv_mode(folder
);
694 /* Now that full mailx(1)-style file expansion is possible handle the
695 * attachments which we had delayed due to this.
696 * This may use savestr(), but since we won't enter the command loop we
697 * don't need to care about that */
698 while (a_head
!= NULL
) {
699 attach
= add_attachment(attach
, a_head
->aa_file
, NULL
);
700 if (attach
!= NULL
) {
702 a_head
= a_head
->aa_next
;
705 perror(a_head
->aa_file
);
710 /* xxx exit_status = EXIT_OK; */
711 if (options
& OPT_INTERACTIVE
)
713 mail(to
, cc
, bcc
, subject
, attach
, qf
, ((options
& OPT_F_FLAG
) != 0));
714 if (options
& OPT_INTERACTIVE
)
720 /* vim:set fenc=utf-8:s-it-mode */