openssl.c: fix compiler warnings
[s-mailx.git] / main.c
blob6518129b3b063f279e41a95066b873deb171349a
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 */
7 /*
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
13 * are met:
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
37 * SUCH DAMAGE.
40 #ifndef lint
41 #ifdef DOSCCS
42 static char copyright[]
43 = "@(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.\n";
44 static char sccsid[] = "@(#)main.c 2.51 (gritter) 10/1/07";
45 #endif /* DOSCCS */
46 #endif /* not lint */
48 /* TODO longjmp() globbering as in cmd1.c and cmd3.c (see there)
49 * TODO Problem: Popen doesn't encapsulate all cases of open failures,
50 * TODO may leave child running if fdopen() fails! */
53 * Most strcpy/sprintf functions have been changed to strncpy/snprintf to
54 * correct several buffer overruns (at least one ot them was exploitable).
55 * Sat Jun 20 04:58:09 CEST 1998 Alvaro Martinez Echevarria <alvaro@lander.es>
56 * ---
57 * Note: We set egid to realgid ... and only if we need the egid we will
58 * switch back temporary. Nevertheless, I do not like seg faults.
59 * Werner Fink, <werner@suse.de>
63 #include "config.h"
64 #ifdef HAVE_NL_LANGINFO
65 #include <langinfo.h>
66 #endif /* HAVE_NL_LANGINFO */
67 #define _MAIL_GLOBS_
68 #include "rcv.h"
69 #include "extern.h"
70 #include <sys/stat.h>
71 #include <sys/ioctl.h>
72 #include <termios.h>
73 #include <unistd.h>
74 #include <fcntl.h>
75 #ifdef HAVE_SETLOCALE
76 #include <locale.h>
77 #endif /* HAVE_SETLOCALE */
80 * Mail -- a mail program
82 * Startup -- interface with user.
85 static sigjmp_buf hdrjmp;
86 char *progname;
87 sighandler_type dflpipe = SIG_DFL;
89 static void hdrstop(int signo);
90 static void setscreensize(int dummy);
92 int
93 main(int argc, char *argv[])
95 const char optstr[] = "A:BHEFINVT:RS:a:b:c:dDefh:inqr:s:tu:v~O:";
96 int scnt, i, existonly = 0, headersonly = 0, sendflag = 0;
97 struct name *to, *cc, *bcc, *smopts;
98 struct attachment *attach;
99 char *subject, *cp, *ef, *qf = NULL, *fromaddr = NULL, *Aflag = NULL;
100 char nosrc = 0;
101 int Eflag = 0, Fflag = 0, Nflag = 0, tflag = 0;
102 sighandler_type prevint;
104 (void)&Nflag;
106 * Absolutely the first thing we do is save our egid
107 * and set it to the rgid, so that we can safely run
108 * setgid. We use the sgid (saved set-gid) to allow ourselves
109 * to revert to the egid if we want (temporarily) to become
110 * priveliged.
112 effectivegid = getegid();
113 realgid = getgid();
114 if (setgid(realgid) < 0) {
115 perror("setgid");
116 exit(1);
119 starting = 1;
120 progname = strrchr(argv[0], '/');
121 if (progname != NULL)
122 progname++;
123 else
124 progname = argv[0];
126 * Set up a reasonable environment.
127 * Figure out whether we are being run interactively,
128 * start the SIGCHLD catcher, and so forth.
130 safe_signal(SIGCHLD, sigchild);
131 is_a_tty[0] = isatty(0);
132 is_a_tty[1] = isatty(1);
133 if (is_a_tty[0]) {
134 assign("interactive", "");
135 if (is_a_tty[1])
136 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
138 assign("header", "");
139 assign("save", "");
140 #ifdef HAVE_SETLOCALE
141 setlocale(LC_CTYPE, "");
142 setlocale(LC_COLLATE, "");
143 setlocale(LC_MESSAGES, "");
144 mb_cur_max = MB_CUR_MAX;
145 #if defined (HAVE_NL_LANGINFO) && defined (CODESET)
146 if (value("ttycharset") == NULL && (cp = nl_langinfo(CODESET)) != NULL)
147 assign("ttycharset", cp);
148 #endif /* HAVE_NL_LANGINFO && CODESET */
149 #if defined (HAVE_MBTOWC) && defined (HAVE_WCTYPE_H)
150 if (mb_cur_max > 1) {
151 wchar_t wc;
152 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
153 mbtowc(&wc, "\342\202\254", 3) == 3 &&
154 wc == 0x20AC)
155 utf8 = 1;
157 #endif /* HAVE_MBTOWC && HAVE_WCTYPE_H */
158 #else /* !HAVE_SETLOCALE */
159 mb_cur_max = 1;
160 #endif /* !HAVE_SETLOCALE */
161 #ifdef HAVE_CATGETS
162 #ifdef NL_CAT_LOCALE
163 i = NL_CAT_LOCALE;
164 #else
165 i = 0;
166 #endif
167 catd = catopen(CATNAME, i);
168 #endif /* HAVE_CATGETS */
169 #ifdef HAVE_ICONV
170 iconvd = (iconv_t) -1;
171 #endif
172 image = -1;
174 * Now, determine how we are being used.
175 * We successively pick off - flags.
176 * If there is anything left, it is the base of the list
177 * of users to mail to. Argp will be set to point to the
178 * first of these users.
180 ef = NULL;
181 to = NULL;
182 cc = NULL;
183 bcc = NULL;
184 attach = NULL;
185 smopts = NULL;
186 subject = NULL;
187 scnt = 0;
188 while ((i = getopt(argc, argv, optstr)) != EOF) {
189 switch (i) {
190 case 'V':
191 puts(version);
192 exit(0);
193 /*NOTREACHED*/
194 case 'B':
195 setvbuf(stdin, NULL, _IOLBF, 0);
196 setvbuf(stdout, NULL, _IOLBF, 0);
197 break;
198 case 'H':
199 headersonly = 1;
200 break;
201 case 'E':
202 Eflag = 1;
203 break;
204 case 'F':
205 Fflag = 1;
206 sendflag++;
207 break;
208 case 'S':
209 argv[scnt++] = optarg;
210 break;
211 case 'T':
213 * Next argument is temp file to write which
214 * articles have been read/deleted for netnews.
216 Tflag = optarg;
217 if ((i = creat(Tflag, 0600)) < 0) {
218 perror(Tflag);
219 exit(1);
221 close(i);
222 /*FALLTHRU*/
223 case 'I':
225 * Show Newsgroups: field in header summary
227 Iflag = 1;
228 break;
229 case 'u':
231 * Next argument is person to pretend to be.
233 uflag = myname = optarg;
234 break;
235 case 'i':
237 * User wants to ignore interrupts.
238 * Set the variable "ignore"
240 assign("ignore", "");
241 break;
242 case 'd':
243 debug++;
244 break;
245 case 'D':
246 assign("disconnected", "");
247 break;
248 case 'e':
249 existonly++;
250 break;
251 case 's':
253 * Give a subject field for sending from
254 * non terminal
256 subject = optarg;
257 sendflag++;
258 break;
259 case 'f':
261 * User is specifying file to "edit" with Mail,
262 * as opposed to reading system mailbox.
263 * If no argument is given, we read his
264 * mbox file.
266 * Check for remaining arguments later.
268 ef = "&";
269 break;
270 case 'q':
272 * User is specifying file to quote in front of
273 * the mail to be collected.
275 if ((argv[optind]) && (argv[optind][0] != '-'))
276 qf = argv[optind++];
277 else
278 qf = NULL;
279 sendflag++;
280 break;
281 case 'n':
283 * User doesn't want to source /usr/lib/Mail.rc
285 nosrc++;
286 break;
287 case 'N':
289 * Avoid initial header printing.
291 Nflag = 1;
292 unset_internal("header");
293 break;
294 case 'v':
296 * Send mailer verbose flag
298 assign("verbose", "");
299 break;
300 case 'r':
302 * Set From address.
303 * MTA is only interested in plain address so strip
304 * anything else (but still give user the option to
305 * simply pass a fully fledged email address)
307 fromaddr = optarg;
308 smopts = cat(smopts, nalloc("-r", 0));
309 smopts = cat(smopts, nalloc(optarg, GFULL));
310 tildeflag = -1;
311 sendflag++;
312 break;
313 case 'a':
315 * Get attachment filenames
317 attach = add_attachment(attach, optarg, 0);
318 if (attach == NULL) {
319 perror(optarg);
320 exit(1);
322 sendflag++;
323 break;
324 case 'c':
326 * Get Carbon Copy Recipient list
328 cc = checkaddrs(cat(cc, extract(optarg, GCC|GFULL)));
329 sendflag++;
330 break;
331 case 'b':
333 * Get Blind Carbon Copy Recipient list
335 bcc = checkaddrs(cat(bcc, extract(optarg, GBCC|GFULL)));
336 sendflag++;
337 break;
338 case 'h':
340 * Hop count for sendmail
342 smopts = cat(smopts, nalloc("-h", 0));
343 smopts = cat(smopts, nalloc(optarg, 0));
344 sendflag++;
345 break;
346 case 'O':
348 * Additional options to pass-through to MTA
350 smopts = cat(smopts, nalloc(optarg, 0));
351 sendflag++;
352 break;
353 case '~':
354 if (tildeflag == 0)
355 tildeflag = 1;
356 break;
357 case 't':
358 sendflag = 1;
359 tflag = 1;
360 break;
361 case 'A':
362 Aflag = optarg;
363 break;
364 case 'R':
365 Rflag = 1;
366 break;
367 case '?':
368 usage:
369 fprintf(stderr, catgets(catd, CATSET, 135,
370 "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);
371 exit(2);
374 if (ef != NULL) {
375 if (optind < argc) {
376 if (optind + 1 < argc) {
377 fprintf(stderr, catgets(catd, CATSET, 205,
378 "More than one file given with -f\n"));
379 goto usage;
381 ef = argv[optind];
383 } else {
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"));
393 goto usage;
395 if (sendflag && !tflag && to == NULL) {
396 fprintf(stderr, catgets(catd, CATSET, 138,
397 "Send options without primary recipient specified.\n"));
398 goto usage;
400 if (Rflag && to != NULL) {
401 fprintf(stderr, "The -R option is meaningless in send mode.\n");
402 goto usage;
404 if (Iflag && ef == NULL) {
405 fprintf(stderr, catgets(catd, CATSET, 204,
406 "Need -f with -I.\n"));
407 goto usage;
409 tinit();
410 setscreensize(0);
411 #ifdef SIGWINCH
412 if (value("interactive"))
413 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
414 safe_signal(SIGWINCH, setscreensize);
415 #endif /* SIGWINCH */
416 input = stdin;
417 rcvmode = !to && !tflag;
418 spreserve();
419 if (!nosrc)
420 load(MAILRC);
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)
426 load(expand(cp));
427 else if ((cp = getenv("SNAILRC")) != NULL)
428 load(expand(cp));
429 else
430 load(expand("~/.mailrc"));
431 if (getenv("SNAIL_EXTRA_RC") == NULL &&
432 (cp = value("SNAIL_EXTRA_RC")) != NULL)
433 load(expand(cp));
435 * Now we can set the account.
437 if (Aflag) {
438 char *a[2];
439 a[0] = Aflag;
440 a[1] = NULL;
441 account(a);
444 * Override 'skipemptybody' if '-E' flag was given.
446 if (Eflag)
447 assign("skipemptybody", "");
449 * -S arguments override rc files.
451 for (i = 0; i < scnt; ++i) {
452 char *a[2];
453 a[0] = argv[i];
454 a[1] = NULL;
455 set(a);
458 starting = 0;
461 * From address from command line overrides rc files.
463 if (fromaddr)
464 assign("from", fromaddr);
465 if (!rcvmode) {
466 mail(to, cc, bcc, smopts, subject, attach, qf, Fflag, tflag,
467 Eflag);
469 * why wait?
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.
478 if (ef == NULL)
479 ef = "%";
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';
489 i = setfile(ef, 0);
490 if (i < 0)
491 exit(1); /* error already reported */
492 if (existonly)
493 exit(i);
494 if (headersonly) {
495 if (mb.mb_type == MB_IMAP)
496 imap_getheaders(1, msgCount);
497 for (i = 1; i <= msgCount; i++)
498 printhead(i, stdout, 0);
499 exit(exit_status);
501 callhook(mailname, 0);
502 if (i > 0 && value("emptystart") == NULL)
503 exit(1);
504 if (sigsetjmp(hdrjmp, 1) == 0) {
505 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
506 safe_signal(SIGINT, hdrstop);
507 if (Nflag == 0) {
508 if (value("quiet") == NULL)
509 printf(catgets(catd, CATSET, 140,
510 "%s version %s. "
511 "Type ? for help.\n"),
512 value("bsdcompat") ? "Mail" : "S-nail",
513 version);
514 announce(1);
515 fflush(stdout);
517 safe_signal(SIGINT, prevint);
519 commands();
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';
526 quit();
527 return exit_status;
531 * Interrupt printing of the headers.
533 /*ARGSUSED*/
534 static void
535 hdrstop(int signo)
537 (void)signo;
539 fflush(stdout);
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;
552 /*ARGSUSED*/
553 static void
554 setscreensize(int dummy)
556 struct termios tbuf;
557 #ifdef TIOCGWINSZ
558 struct winsize ws;
559 #endif
560 speed_t ospeed;
561 (void)dummy;
563 #ifdef TIOCGWINSZ
564 if (ioctl(1, TIOCGWINSZ, &ws) < 0)
565 ws.ws_col = ws.ws_row = 0;
566 #endif
567 if (tcgetattr(1, &tbuf) < 0)
568 ospeed = B9600;
569 else
570 ospeed = cfgetospeed(&tbuf);
571 if (ospeed < B1200)
572 scrnheight = 9;
573 else if (ospeed == B1200)
574 scrnheight = 14;
575 #ifdef TIOCGWINSZ
576 else if (ws.ws_row != 0)
577 scrnheight = ws.ws_row;
578 #endif
579 else
580 scrnheight = 24;
581 #ifdef TIOCGWINSZ
582 if ((realscreenheight = ws.ws_row) == 0)
583 realscreenheight = 24;
584 #endif
585 #ifdef TIOCGWINSZ
586 if ((scrnwidth = ws.ws_col) == 0)
587 #endif
588 scrnwidth = 80;