mime.c: fix compiler warnings
[s-mailx.git] / main.c
blob567b06d7f04c2088076255043e08cc23f71dd121
1 /*
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.
6 * All rights reserved.
7 */
8 /*
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
14 * are met:
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
38 * SUCH DAMAGE.
41 #ifndef lint
42 #ifdef DOSCCS
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";
46 #endif /* DOSCCS */
47 #endif /* not lint */
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>
53 * ---
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>
60 #include "config.h"
61 #ifdef HAVE_NL_LANGINFO
62 #include <langinfo.h>
63 #endif /* HAVE_NL_LANGINFO */
64 #define _MAIL_GLOBS_
65 #include "rcv.h"
66 #include "extern.h"
67 #include <sys/stat.h>
68 #include <sys/ioctl.h>
69 #include <termios.h>
70 #include <unistd.h>
71 #include <fcntl.h>
72 #ifdef HAVE_SETLOCALE
73 #include <locale.h>
74 #endif /* HAVE_SETLOCALE */
77 * Mail -- a mail program
79 * Startup -- interface with user.
82 static sigjmp_buf hdrjmp;
83 char *progname;
84 sighandler_type dflpipe = SIG_DFL;
86 static void hdrstop(int signo);
87 static void setscreensize(int dummy);
89 int
90 main(int argc, char *argv[])
92 const char optstr[] = "A:BHEFINVT:RS:a:b:c:dDefh:inqr:s:tu:v~O:";
93 int scnt, 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;
97 char nosrc = 0;
98 int Eflag = 0, Fflag = 0, Nflag = 0, tflag = 0;
99 sighandler_type prevint;
101 (void)&Nflag;
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
107 * priveliged.
109 effectivegid = getegid();
110 realgid = getgid();
111 if (setgid(realgid) < 0) {
112 perror("setgid");
113 exit(1);
116 starting = 1;
117 progname = strrchr(argv[0], '/');
118 if (progname != NULL)
119 progname++;
120 else
121 progname = argv[0];
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);
130 if (is_a_tty[0]) {
131 assign("interactive", "");
132 if (is_a_tty[1])
133 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
135 assign("header", "");
136 assign("save", "");
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) {
148 wchar_t wc;
149 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
150 mbtowc(&wc, "\342\202\254", 3) == 3 &&
151 wc == 0x20AC)
152 utf8 = 1;
154 #endif /* HAVE_MBTOWC && HAVE_WCTYPE_H */
155 #else /* !HAVE_SETLOCALE */
156 mb_cur_max = 1;
157 #endif /* !HAVE_SETLOCALE */
158 #ifdef HAVE_CATGETS
159 #ifdef NL_CAT_LOCALE
160 i = NL_CAT_LOCALE;
161 #else
162 i = 0;
163 #endif
164 catd = catopen(CATNAME, i);
165 #endif /* HAVE_CATGETS */
166 #ifdef HAVE_ICONV
167 iconvd = (iconv_t) -1;
168 #endif
169 image = -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.
177 ef = NULL;
178 to = NULL;
179 cc = NULL;
180 bcc = NULL;
181 attach = NULL;
182 smopts = NULL;
183 subject = NULL;
184 scnt = 0;
185 while ((i = getopt(argc, argv, optstr)) != EOF) {
186 switch (i) {
187 case 'V':
188 puts(version);
189 exit(0);
190 /*NOTREACHED*/
191 case 'B':
192 setvbuf(stdin, NULL, _IOLBF, 0);
193 setvbuf(stdout, NULL, _IOLBF, 0);
194 break;
195 case 'H':
196 headersonly = 1;
197 break;
198 case 'E':
199 Eflag = 1;
200 break;
201 case 'F':
202 Fflag = 1;
203 sendflag++;
204 break;
205 case 'S':
206 argv[scnt++] = optarg;
207 break;
208 case 'T':
210 * Next argument is temp file to write which
211 * articles have been read/deleted for netnews.
213 Tflag = optarg;
214 if ((i = creat(Tflag, 0600)) < 0) {
215 perror(Tflag);
216 exit(1);
218 close(i);
219 /*FALLTHRU*/
220 case 'I':
222 * Show Newsgroups: field in header summary
224 Iflag = 1;
225 break;
226 case 'u':
228 * Next argument is person to pretend to be.
230 uflag = myname = optarg;
231 break;
232 case 'i':
234 * User wants to ignore interrupts.
235 * Set the variable "ignore"
237 assign("ignore", "");
238 break;
239 case 'd':
240 debug++;
241 break;
242 case 'D':
243 assign("disconnected", "");
244 break;
245 case 'e':
246 existonly++;
247 break;
248 case 's':
250 * Give a subject field for sending from
251 * non terminal
253 subject = optarg;
254 sendflag++;
255 break;
256 case 'f':
258 * User is specifying file to "edit" with Mail,
259 * as opposed to reading system mailbox.
260 * If no argument is given, we read his
261 * mbox file.
263 * Check for remaining arguments later.
265 ef = "&";
266 break;
267 case 'q':
269 * User is specifying file to quote in front of
270 * the mail to be collected.
272 if ((argv[optind]) && (argv[optind][0] != '-'))
273 qf = argv[optind++];
274 else
275 qf = NULL;
276 sendflag++;
277 break;
278 case 'n':
280 * User doesn't want to source /usr/lib/Mail.rc
282 nosrc++;
283 break;
284 case 'N':
286 * Avoid initial header printing.
288 Nflag = 1;
289 unset_internal("header");
290 break;
291 case 'v':
293 * Send mailer verbose flag
295 assign("verbose", "");
296 break;
297 case 'r':
299 * Set From address.
300 * MTA is only interested in plain address so strip
301 * anything else (but still give user the option to
302 * simply pass a fully fledged email address)
304 fromaddr = optarg;
305 smopts = cat(smopts, nalloc("-r", 0));
306 smopts = cat(smopts, nalloc(optarg, GFULL));
307 tildeflag = -1;
308 sendflag++;
309 break;
310 case 'a':
312 * Get attachment filenames
314 attach = add_attachment(attach, optarg, 0);
315 if (attach == NULL) {
316 perror(optarg);
317 exit(1);
319 sendflag++;
320 break;
321 case 'c':
323 * Get Carbon Copy Recipient list
325 cc = checkaddrs(cat(cc, extract(optarg, GCC|GFULL)));
326 sendflag++;
327 break;
328 case 'b':
330 * Get Blind Carbon Copy Recipient list
332 bcc = checkaddrs(cat(bcc, extract(optarg, GBCC|GFULL)));
333 sendflag++;
334 break;
335 case 'h':
337 * Hop count for sendmail
339 smopts = cat(smopts, nalloc("-h", 0));
340 smopts = cat(smopts, nalloc(optarg, 0));
341 sendflag++;
342 break;
343 case 'O':
345 * Additional options to pass-through to MTA
347 smopts = cat(smopts, nalloc(optarg, 0));
348 sendflag++;
349 break;
350 case '~':
351 if (tildeflag == 0)
352 tildeflag = 1;
353 break;
354 case 't':
355 sendflag = 1;
356 tflag = 1;
357 break;
358 case 'A':
359 Aflag = optarg;
360 break;
361 case 'R':
362 Rflag = 1;
363 break;
364 case '?':
365 usage:
366 fprintf(stderr, catgets(catd, CATSET, 135,
367 "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);
368 exit(2);
371 if (ef != NULL) {
372 if (optind < argc) {
373 if (optind + 1 < argc) {
374 fprintf(stderr, catgets(catd, CATSET, 205,
375 "More than one file given with -f\n"));
376 goto usage;
378 ef = argv[optind];
380 } else {
381 for (i = optind; argv[i]; i++)
382 to = checkaddrs(cat(to, extract(argv[i], GTO|GFULL)));
385 * Check for inconsistent arguments.
387 if (ef != NULL && to != NULL) {
388 fprintf(stderr, catgets(catd, CATSET, 137,
389 "Cannot give -f and people to send to.\n"));
390 goto usage;
392 if (sendflag && !tflag && to == NULL) {
393 fprintf(stderr, catgets(catd, CATSET, 138,
394 "Send options without primary recipient specified.\n"));
395 goto usage;
397 if (Rflag && to != NULL) {
398 fprintf(stderr, "The -R option is meaningless in send mode.\n");
399 goto usage;
401 if (Iflag && ef == NULL) {
402 fprintf(stderr, catgets(catd, CATSET, 204,
403 "Need -f with -I.\n"));
404 goto usage;
406 tinit();
407 setscreensize(0);
408 #ifdef SIGWINCH
409 if (value("interactive"))
410 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
411 safe_signal(SIGWINCH, setscreensize);
412 #endif /* SIGWINCH */
413 input = stdin;
414 rcvmode = !to && !tflag;
415 spreserve();
416 if (!nosrc)
417 load(MAILRC);
419 * Expand returns a savestr, but load only uses the file name
420 * for fopen, so it's safe to do this.
422 if ((cp = getenv("MAILRC")) != NULL)
423 load(expand(cp));
424 else if ((cp = getenv("SNAILRC")) != NULL)
425 load(expand(cp));
426 else
427 load(expand("~/.mailrc"));
428 if (getenv("SNAIL_EXTRA_RC") == NULL &&
429 (cp = value("SNAIL_EXTRA_RC")) != NULL)
430 load(expand(cp));
432 * Now we can set the account.
434 if (Aflag) {
435 char *a[2];
436 a[0] = Aflag;
437 a[1] = NULL;
438 account(a);
441 * Override 'skipemptybody' if '-E' flag was given.
443 if (Eflag)
444 assign("skipemptybody", "");
446 * -S arguments override rc files.
448 for (i = 0; i < scnt; ++i) {
449 char *args[] = { argv[i], NULL };
450 set(args);
453 starting = 0;
456 * From address from command line overrides rc files.
458 if (fromaddr)
459 assign("from", fromaddr);
460 if (!rcvmode) {
461 mail(to, cc, bcc, smopts, subject, attach, qf, Fflag, tflag,
462 Eflag);
464 * why wait?
466 exit(senderr ? 1 : 0);
469 * Ok, we are reading mail.
470 * Decide whether we are editing a mailbox or reading
471 * the system mailbox, and open up the right stuff.
473 if (ef == NULL)
474 ef = "%";
475 else if (*ef == '@') {
477 * This must be treated specially to make invocation like
478 * -A imap -f @mailbox work.
480 if ((cp = value("folder")) != NULL &&
481 which_protocol(cp) == PROTO_IMAP)
482 strncpy(mailname, cp, PATHSIZE)[PATHSIZE-1] = '\0';
484 i = setfile(ef, 0);
485 if (i < 0)
486 exit(1); /* error already reported */
487 if (existonly)
488 exit(i);
489 if (headersonly) {
490 if (mb.mb_type == MB_IMAP)
491 imap_getheaders(1, msgCount);
492 for (i = 1; i <= msgCount; i++)
493 printhead(i, stdout, 0);
494 exit(exit_status);
496 callhook(mailname, 0);
497 if (i > 0 && value("emptystart") == NULL)
498 exit(1);
499 if (sigsetjmp(hdrjmp, 1) == 0) {
500 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
501 safe_signal(SIGINT, hdrstop);
502 if (Nflag == 0) {
503 if (value("quiet") == NULL)
504 printf(catgets(catd, CATSET, 140,
505 "%s version %s. "
506 "Type ? for help.\n"),
507 value("bsdcompat") ? "Mail" : "S-nail",
508 version);
509 announce(1);
510 fflush(stdout);
512 safe_signal(SIGINT, prevint);
514 commands();
515 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
516 safe_signal(SIGHUP, SIG_IGN);
517 safe_signal(SIGINT, SIG_IGN);
518 safe_signal(SIGQUIT, SIG_IGN);
520 strncpy(mboxname, expand("&"), sizeof mboxname)[sizeof mboxname-1]='\0';
521 quit();
522 return exit_status;
526 * Interrupt printing of the headers.
528 /*ARGSUSED*/
529 static void
530 hdrstop(int signo)
533 fflush(stdout);
534 fprintf(stderr, catgets(catd, CATSET, 141, "\nInterrupt\n"));
535 siglongjmp(hdrjmp, 1);
539 * Compute what the screen size for printing headers should be.
540 * We use the following algorithm for the height:
541 * If baud rate < 1200, use 9
542 * If baud rate = 1200, use 14
543 * If baud rate > 1200, use 24 or ws_row
544 * Width is either 80 or ws_col;
546 /*ARGSUSED*/
547 static void
548 setscreensize(int dummy)
550 struct termios tbuf;
551 #ifdef TIOCGWINSZ
552 struct winsize ws;
553 #endif
554 speed_t ospeed;
556 #ifdef TIOCGWINSZ
557 if (ioctl(1, TIOCGWINSZ, &ws) < 0)
558 ws.ws_col = ws.ws_row = 0;
559 #endif
560 if (tcgetattr(1, &tbuf) < 0)
561 ospeed = B9600;
562 else
563 ospeed = cfgetospeed(&tbuf);
564 if (ospeed < B1200)
565 scrnheight = 9;
566 else if (ospeed == B1200)
567 scrnheight = 14;
568 #ifdef TIOCGWINSZ
569 else if (ws.ws_row != 0)
570 scrnheight = ws.ws_row;
571 #endif
572 else
573 scrnheight = 24;
574 #ifdef TIOCGWINSZ
575 if ((realscreenheight = ws.ws_row) == 0)
576 realscreenheight = 24;
577 #endif
578 #ifdef TIOCGWINSZ
579 if ((scrnwidth = ws.ws_col) == 0)
580 #endif
581 scrnwidth = 80;