2 * 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 Steffen "Daode" Nurpmeso.
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 /* TODO longjmp() globbering as in cmd1.c and cmd3.c (see there)
41 * TODO Problem: Popen doesn't encapsulate all cases of open failures,
42 * TODO may leave child running if fdopen() fails! */
45 * Most strcpy/sprintf functions have been changed to strncpy/snprintf to
46 * correct several buffer overruns (at least one ot them was exploitable).
47 * Sat Jun 20 04:58:09 CEST 1998 Alvaro Martinez Echevarria <alvaro@lander.es>
49 * Note: We set egid to realgid ... and only if we need the egid we will
50 * switch back temporary. Nevertheless, I do not like seg faults.
51 * Werner Fink, <werner@suse.de>
56 #ifdef HAVE_NL_LANGINFO
58 #endif /* HAVE_NL_LANGINFO */
63 #include <sys/ioctl.h>
69 #endif /* HAVE_SETLOCALE */
72 * Mail -- a mail program
74 * Startup -- interface with user.
77 static sigjmp_buf hdrjmp
;
79 sighandler_type dflpipe
= SIG_DFL
;
81 static void hdrstop(int signo
);
82 static void setscreensize(int dummy
);
85 main(int argc
, char *argv
[])
87 const char optstr
[] = "A:BHEFINVT:RS:a:b:c:dDefh:inqr:s:tu:v~O:";
88 int scnt
, i
, existonly
= 0, headersonly
= 0, sendflag
= 0;
89 struct name
*to
, *cc
, *bcc
, *smopts
;
90 struct attachment
*attach
;
91 char *subject
, *cp
, *ef
, *qf
= NULL
, *fromaddr
= NULL
, *Aflag
= NULL
;
93 int Eflag
= 0, Fflag
= 0, Nflag
= 0, tflag
= 0;
94 sighandler_type prevint
;
98 * Absolutely the first thing we do is save our egid
99 * and set it to the rgid, so that we can safely run
100 * setgid. We use the sgid (saved set-gid) to allow ourselves
101 * to revert to the egid if we want (temporarily) to become
104 effectivegid
= getegid();
106 if (setgid(realgid
) < 0) {
112 progname
= strrchr(argv
[0], '/');
113 if (progname
!= NULL
)
118 * Set up a reasonable environment.
119 * Figure out whether we are being run interactively,
120 * start the SIGCHLD catcher, and so forth.
122 safe_signal(SIGCHLD
, sigchild
);
123 is_a_tty
[0] = isatty(0);
124 is_a_tty
[1] = isatty(1);
126 assign("interactive", "");
128 safe_signal(SIGPIPE
, dflpipe
= SIG_IGN
);
130 assign("header", "");
132 #ifdef HAVE_SETLOCALE
133 setlocale(LC_CTYPE
, "");
134 setlocale(LC_COLLATE
, "");
135 setlocale(LC_MESSAGES
, "");
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
);
140 #endif /* HAVE_NL_LANGINFO && CODESET */
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 &&
149 #endif /* HAVE_MBTOWC && HAVE_WCTYPE_H */
150 #else /* !HAVE_SETLOCALE */
152 #endif /* !HAVE_SETLOCALE */
159 catd
= catopen(CATNAME
, i
);
160 #endif /* HAVE_CATGETS */
162 iconvd
= (iconv_t
) -1;
166 * Now, determine how we are being used.
167 * We successively pick off - flags.
168 * If there is anything left, it is the base of the list
169 * of users to mail to. Argp will be set to point to the
170 * first of these users.
180 while ((i
= getopt(argc
, argv
, optstr
)) != EOF
) {
187 setvbuf(stdin
, NULL
, _IOLBF
, 0);
188 setvbuf(stdout
, NULL
, _IOLBF
, 0);
201 argv
[scnt
++] = optarg
;
205 * Next argument is temp file to write which
206 * articles have been read/deleted for netnews.
209 if ((i
= creat(Tflag
, 0600)) < 0) {
217 * Show Newsgroups: field in header summary
223 * Next argument is person to pretend to be.
225 uflag
= myname
= optarg
;
229 * User wants to ignore interrupts.
230 * Set the variable "ignore"
232 assign("ignore", "");
238 assign("disconnected", "");
245 * Give a subject field for sending from
253 * User is specifying file to "edit" with Mail,
254 * as opposed to reading system mailbox.
255 * If no argument is given, we read his
258 * Check for remaining arguments later.
264 * User is specifying file to quote in front of
265 * the mail to be collected.
267 if ((argv
[optind
]) && (argv
[optind
][0] != '-'))
275 * User doesn't want to source /usr/lib/Mail.rc
281 * Avoid initial header printing.
284 unset_internal("header");
288 * Send mailer verbose flag
290 assign("verbose", "");
295 * MTA is only interested in plain address so strip
296 * anything else (but still give user the option to
297 * simply pass a fully fledged email address)
301 smopts
= cat(smopts
, nalloc("-r", 0));
302 smopts
= cat(smopts
, sextract(optarg
, GFULL
));
303 if (count(smopts
) != i
+ 2) {
304 fprintf(stderr
, tr(271,
305 "More than one address "
314 * Get attachment filenames
316 attach
= add_attachment(attach
, optarg
, 0);
317 if (attach
== NULL
) {
325 * Get Carbon Copy Recipient list
327 cc
= checkaddrs(cat(cc
, sextract(optarg
, GCC
|GFULL
)));
332 * Get Blind Carbon Copy Recipient list
334 bcc
= checkaddrs(cat(bcc
,
335 sextract(optarg
, GBCC
|GFULL
)));
340 * Hop count for sendmail
342 smopts
= cat(smopts
, nalloc("-h", 0));
343 smopts
= cat(smopts
, nalloc(optarg
, 0));
348 * Additional options to pass-through to MTA
350 smopts
= cat(smopts
, nalloc(optarg
, 0));
369 fprintf(stderr
, tr(135,
370 "Usage: %s -eiIUdEFntBDNHRV~ -T FILE -u USER -O MTAARG -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users\n"), progname
);
376 if (optind
+ 1 < argc
) {
377 fprintf(stderr
, catgets(catd
, CATSET
, 205,
378 "More than one file given with -f\n"));
384 for (i
= optind
; argv
[i
]; i
++)
385 to
= checkaddrs(cat(to
, extract(argv
[i
], GTO
|GFULL
)));
388 * Check for inconsistent arguments.
390 if (ef
!= NULL
&& to
!= NULL
) {
391 fprintf(stderr
, catgets(catd
, CATSET
, 137,
392 "Cannot give -f and people to send to.\n"));
395 if (sendflag
&& !tflag
&& to
== NULL
) {
396 fprintf(stderr
, catgets(catd
, CATSET
, 138,
397 "Send options without primary recipient specified.\n"));
400 if (Rflag
&& to
!= NULL
) {
401 fprintf(stderr
, "The -R option is meaningless in send mode.\n");
404 if (Iflag
&& ef
== NULL
) {
405 fprintf(stderr
, catgets(catd
, CATSET
, 204,
406 "Need -f with -I.\n"));
412 if (value("interactive"))
413 if (safe_signal(SIGWINCH
, SIG_IGN
) != SIG_IGN
)
414 safe_signal(SIGWINCH
, setscreensize
);
415 #endif /* SIGWINCH */
417 rcvmode
= !to
&& !tflag
;
422 * Expand returns a savestr, but load only uses the file name
423 * for fopen, so it's safe to do this.
425 if ((cp
= getenv("MAILRC")) != NULL
)
427 else if ((cp
= getenv("NAILRC")) != NULL
)
430 load(expand("~/.mailrc"));
431 if (getenv("NAIL_EXTRA_RC") == NULL
&&
432 (cp
= value("NAIL_EXTRA_RC")) != NULL
)
435 * Now we can set the account.
444 * Override 'skipemptybody' if '-E' flag was given.
447 assign("skipemptybody", "");
449 * -S arguments override rc files.
451 for (i
= 0; i
< scnt
; ++i
) {
461 * From address from command line overrides rc files.
464 assign("from", fromaddr
);
466 mail(to
, cc
, bcc
, smopts
, subject
, attach
, qf
, Fflag
, tflag
,
471 exit(senderr
? 1 : 0);
474 * Ok, we are reading mail.
475 * Decide whether we are editing a mailbox or reading
476 * the system mailbox, and open up the right stuff.
480 else if (*ef
== '@') {
482 * This must be treated specially to make invocation like
483 * -A imap -f @mailbox work.
485 if ((cp
= value("folder")) != NULL
&&
486 which_protocol(cp
) == PROTO_IMAP
)
487 strncpy(mailname
, cp
, PATHSIZE
)[PATHSIZE
-1] = '\0';
491 exit(1); /* error already reported */
495 if (mb
.mb_type
== MB_IMAP
)
496 imap_getheaders(1, msgCount
);
497 for (i
= 1; i
<= msgCount
; i
++)
498 printhead(i
, stdout
, 0);
501 callhook(mailname
, 0);
502 if (i
> 0 && value("emptystart") == NULL
)
504 if (sigsetjmp(hdrjmp
, 1) == 0) {
505 if ((prevint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
506 safe_signal(SIGINT
, hdrstop
);
508 if (value("quiet") == NULL
)
509 printf(catgets(catd
, CATSET
, 140,
511 "Type ? for help.\n"),
512 value("bsdcompat") ? "Mail" : uagent
,
517 safe_signal(SIGINT
, prevint
);
520 if (mb
.mb_type
== MB_FILE
|| mb
.mb_type
== MB_MAILDIR
) {
521 safe_signal(SIGHUP
, SIG_IGN
);
522 safe_signal(SIGINT
, SIG_IGN
);
523 safe_signal(SIGQUIT
, SIG_IGN
);
525 strncpy(mboxname
, expand("&"), sizeof mboxname
)[sizeof mboxname
-1]='\0';
531 * Interrupt printing of the headers.
540 fprintf(stderr
, catgets(catd
, CATSET
, 141, "\nInterrupt\n"));
541 siglongjmp(hdrjmp
, 1);
545 * Compute what the screen size for printing headers should be.
546 * We use the following algorithm for the height:
547 * If baud rate < 1200, use 9
548 * If baud rate = 1200, use 14
549 * If baud rate > 1200, use 24 or ws_row
550 * Width is either 80 or ws_col;
554 setscreensize(int dummy
)
564 if (ioctl(1, TIOCGWINSZ
, &ws
) < 0)
565 ws
.ws_col
= ws
.ws_row
= 0;
567 if (tcgetattr(1, &tbuf
) < 0)
570 ospeed
= cfgetospeed(&tbuf
);
573 else if (ospeed
== B1200
)
576 else if (ws
.ws_row
!= 0)
577 scrnheight
= ws
.ws_row
;
582 if ((realscreenheight
= ws
.ws_row
) == 0)
583 realscreenheight
= 24;
586 if ((scrnwidth
= ws
.ws_col
) == 0)