2 * Heirloom mailx - 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.
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char copyright
[]
44 = "@(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid
[] = "@(#)main.c 2.51 (gritter) 10/1/07";
50 * Most strcpy/sprintf functions have been changed to strncpy/snprintf to
51 * correct several buffer overruns (at least one ot them was exploitable).
52 * Sat Jun 20 04:58:09 CEST 1998 Alvaro Martinez Echevarria <alvaro@lander.es>
54 * Note: We set egid to realgid ... and only if we need the egid we will
55 * switch back temporary. Nevertheless, I do not like seg faults.
56 * Werner Fink, <werner@suse.de>
61 #ifdef HAVE_NL_LANGINFO
63 #endif /* HAVE_NL_LANGINFO */
68 #include <sys/ioctl.h>
74 #endif /* HAVE_SETLOCALE */
77 * Mail -- a mail program
79 * Startup -- interface with user.
82 static sigjmp_buf hdrjmp
;
84 sighandler_type dflpipe
= SIG_DFL
;
86 static void hdrstop(int signo
);
87 static void setscreensize(int dummy
);
90 main(int argc
, char *argv
[])
92 const char optstr
[] = "A:BHEFINVT:RS:a:b:c:dDefh:inqr:s:tu:v~O:";
93 int i
, existonly
= 0, headersonly
= 0, sendflag
= 0;
94 struct name
*to
, *cc
, *bcc
, *smopts
;
95 struct attachment
*attach
;
96 char *subject
, *cp
, *ef
, *qf
= NULL
, *fromaddr
= NULL
, *Aflag
= NULL
;
98 int Eflag
= 0, Fflag
= 0, Nflag
= 0, tflag
= 0;
99 sighandler_type prevint
;
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
109 effectivegid
= getegid();
111 if (setgid(realgid
) < 0) {
117 progname
= strrchr(argv
[0], '/');
118 if (progname
!= NULL
)
123 * Set up a reasonable environment.
124 * Figure out whether we are being run interactively,
125 * start the SIGCHLD catcher, and so forth.
127 safe_signal(SIGCHLD
, sigchild
);
128 is_a_tty
[0] = isatty(0);
129 is_a_tty
[1] = isatty(1);
131 assign("interactive", "");
133 safe_signal(SIGPIPE
, dflpipe
= SIG_IGN
);
135 assign("header", "");
137 #ifdef HAVE_SETLOCALE
138 setlocale(LC_CTYPE
, "");
139 setlocale(LC_COLLATE
, "");
140 setlocale(LC_MESSAGES
, "");
141 mb_cur_max
= MB_CUR_MAX
;
142 #if defined (HAVE_NL_LANGINFO) && defined (CODESET)
143 if (value("ttycharset") == NULL
&& (cp
= nl_langinfo(CODESET
)) != NULL
)
144 assign("ttycharset", cp
);
145 #endif /* HAVE_NL_LANGINFO && CODESET */
146 #if defined (HAVE_MBTOWC) && defined (HAVE_WCTYPE_H)
147 if (mb_cur_max
> 1) {
149 if (mbtowc(&wc
, "\303\266", 2) == 2 && wc
== 0xF6 &&
150 mbtowc(&wc
, "\342\202\254", 3) == 3 &&
154 #endif /* HAVE_MBTOWC && HAVE_WCTYPE_H */
155 #else /* !HAVE_SETLOCALE */
157 #endif /* !HAVE_SETLOCALE */
164 catd
= catopen(CATNAME
, i
);
165 #endif /* HAVE_CATGETS */
167 iconvd
= (iconv_t
) -1;
171 * Now, determine how we are being used.
172 * We successively pick off - flags.
173 * If there is anything left, it is the base of the list
174 * of users to mail to. Argp will be set to point to the
175 * first of these users.
184 while ((i
= getopt(argc
, argv
, optstr
)) != EOF
) {
191 setvbuf(stdin
, NULL
, _IOLBF
, 0);
192 setvbuf(stdout
, NULL
, _IOLBF
, 0);
205 char *args
[] = { NULL
, NULL
};
212 * Next argument is temp file to write which
213 * articles have been read/deleted for netnews.
216 if ((i
= creat(Tflag
, 0600)) < 0) {
224 * Show Newsgroups: field in header summary
230 * Next argument is person to pretend to be.
232 uflag
= myname
= optarg
;
236 * User wants to ignore interrupts.
237 * Set the variable "ignore"
239 assign("ignore", "");
245 assign("disconnected", "");
252 * Give a subject field for sending from
260 * User is specifying file to "edit" with Mail,
261 * as opposed to reading system mailbox.
262 * If no argument is given, we read his
265 * Check for remaining arguments later.
271 * User is specifying file to quote in front of
272 * the mail to be collected.
274 if ((argv
[optind
]) && (argv
[optind
][0] != '-'))
282 * User doesn't want to source /usr/lib/Mail.rc
288 * Avoid initial header printing.
291 unset_internal("header");
295 * Send mailer verbose flag
297 assign("verbose", "");
304 smopts
= cat(smopts
, nalloc("-r", 0));
305 smopts
= cat(smopts
, nalloc(optarg
, 0));
311 * Get attachment filenames
313 attach
= add_attachment(attach
, optarg
, 0);
314 if (attach
== NULL
) {
322 * Get Carbon Copy Recipient list
324 cc
= checkaddrs(cat(cc
, extract(optarg
, GCC
|GFULL
)));
329 * Get Blind Carbon Copy Recipient list
331 bcc
= checkaddrs(cat(bcc
, extract(optarg
, GBCC
|GFULL
)));
336 * Hop count for sendmail
338 smopts
= cat(smopts
, nalloc("-h", 0));
339 smopts
= cat(smopts
, nalloc(optarg
, 0));
344 * Additional options to pass-through to MTA
346 smopts
= cat(smopts
, nalloc(optarg
, 0));
365 fprintf(stderr
, catgets(catd
, CATSET
, 135,
366 "Usage: %s -eiIUdEFntBDNHRV~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users\n"), progname
);
372 if (optind
+ 1 < argc
) {
373 fprintf(stderr
, catgets(catd
, CATSET
, 205,
374 "More than one file given with -f\n"));
380 for (i
= optind
; argv
[i
]; i
++)
381 to
= checkaddrs(cat(to
, extract(argv
[i
], GTO
|GFULL
)));
384 * Check for inconsistent arguments.
386 if (ef
!= NULL
&& to
!= NULL
) {
387 fprintf(stderr
, catgets(catd
, CATSET
, 137,
388 "Cannot give -f and people to send to.\n"));
391 if (sendflag
&& !tflag
&& to
== NULL
) {
392 fprintf(stderr
, catgets(catd
, CATSET
, 138,
393 "Send options without primary recipient specified.\n"));
396 if (Rflag
&& to
!= NULL
) {
397 fprintf(stderr
, "The -R option is meaningless in send mode.\n");
400 if (Iflag
&& ef
== NULL
) {
401 fprintf(stderr
, catgets(catd
, CATSET
, 204,
402 "Need -f with -I.\n"));
408 if (value("interactive"))
409 if (safe_signal(SIGWINCH
, SIG_IGN
) != SIG_IGN
)
410 safe_signal(SIGWINCH
, setscreensize
);
411 #endif /* SIGWINCH */
413 rcvmode
= !to
&& !tflag
;
418 * Expand returns a savestr, but load only uses the file name
419 * for fopen, so it's safe to do this.
421 if ((cp
= getenv("MAILRC")) != NULL
)
423 else if ((cp
= getenv("SNAILRC")) != NULL
)
426 load(expand("~/.mailrc"));
427 if (getenv("SNAIL_EXTRA_RC") == NULL
&&
428 (cp
= value("SNAIL_EXTRA_RC")) != NULL
)
431 * Now we can set the account.
441 * Override 'skipemptybody' if '-E' flag was given.
444 assign("skipemptybody", "");
449 * From address from command line overrides rc files.
452 assign("from", fromaddr
);
454 mail(to
, cc
, bcc
, smopts
, subject
, attach
, qf
, Fflag
, tflag
,
459 exit(senderr
? 1 : 0);
462 * Ok, we are reading mail.
463 * Decide whether we are editing a mailbox or reading
464 * the system mailbox, and open up the right stuff.
468 else if (*ef
== '@') {
470 * This must be treated specially to make invocation like
471 * -A imap -f @mailbox work.
473 if ((cp
= value("folder")) != NULL
&&
474 which_protocol(cp
) == PROTO_IMAP
)
475 strncpy(mailname
, cp
, PATHSIZE
)[PATHSIZE
-1] = '\0';
479 exit(1); /* error already reported */
483 if (mb
.mb_type
== MB_IMAP
)
484 imap_getheaders(1, msgCount
);
485 for (i
= 1; i
<= msgCount
; i
++)
486 printhead(i
, stdout
, 0);
489 callhook(mailname
, 0);
490 if (i
> 0 && value("emptystart") == NULL
)
492 if (sigsetjmp(hdrjmp
, 1) == 0) {
493 if ((prevint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
494 safe_signal(SIGINT
, hdrstop
);
496 if (value("quiet") == NULL
)
497 printf(catgets(catd
, CATSET
, 140,
499 "Type ? for help.\n"),
500 value("bsdcompat") ? "Mail" : "S-nail",
505 safe_signal(SIGINT
, prevint
);
508 if (mb
.mb_type
== MB_FILE
|| mb
.mb_type
== MB_MAILDIR
) {
509 safe_signal(SIGHUP
, SIG_IGN
);
510 safe_signal(SIGINT
, SIG_IGN
);
511 safe_signal(SIGQUIT
, SIG_IGN
);
513 strncpy(mboxname
, expand("&"), sizeof mboxname
)[sizeof mboxname
-1]='\0';
519 * Interrupt printing of the headers.
527 fprintf(stderr
, catgets(catd
, CATSET
, 141, "\nInterrupt\n"));
528 siglongjmp(hdrjmp
, 1);
532 * Compute what the screen size for printing headers should be.
533 * We use the following algorithm for the height:
534 * If baud rate < 1200, use 9
535 * If baud rate = 1200, use 14
536 * If baud rate > 1200, use 24 or ws_row
537 * Width is either 80 or ws_col;
541 setscreensize(int dummy
)
550 if (ioctl(1, TIOCGWINSZ
, &ws
) < 0)
551 ws
.ws_col
= ws
.ws_row
= 0;
553 if (tcgetattr(1, &tbuf
) < 0)
556 ospeed
= cfgetospeed(&tbuf
);
559 else if (ospeed
== B1200
)
562 else if (ws
.ws_row
!= 0)
563 scrnheight
= ws
.ws_row
;
568 if ((realscreenheight
= ws
.ws_row
) == 0)
569 realscreenheight
= 24;
572 if ((scrnwidth
= ws
.ws_col
) == 0)