Merge from vendor branch LIBSTDC++:
[dragonfly.git] / contrib / sendmail / rmail / rmail.c
1 /*
2  * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
3  *      All rights reserved.
4  * Copyright (c) 1988, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * By using this file, you agree to the terms and conditions set
8  * forth in the LICENSE file which can be found at the top level of
9  * the sendmail distribution.
10  *
11  * $FreeBSD: src/contrib/sendmail/rmail/rmail.c,v 1.4.6.7 2002/09/03 01:50:11 gshapiro Exp $
12  * $DragonFly: src/contrib/sendmail/rmail/Attic/rmail.c,v 1.2 2003/06/17 04:24:06 dillon Exp $
13  *
14  */
15
16 #include <sm/gen.h>
17
18 SM_IDSTR(copyright,
19 "@(#) Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.\n\
20         All rights reserved.\n\
21      Copyright (c) 1988, 1993\n\
22         The Regents of the University of California.  All rights reserved.\n")
23
24 SM_IDSTR(id, "@(#)$Id: rmail.c,v 8.61 2001/09/18 21:45:29 gshapiro Exp $")
25
26 /*
27  * RMAIL -- UUCP mail server.
28  *
29  * This program reads the >From ... remote from ... lines that UUCP is so
30  * fond of and turns them into something reasonable.  It then execs sendmail
31  * with various options built from these lines.
32  *
33  * The expected syntax is:
34  *
35  *       <user> := [-a-z0-9]+
36  *       <date> := ctime format
37  *       <site> := [-a-z0-9!]+
38  * <blank line> := "^\n$"
39  *       <from> := "From" <space> <user> <space> <date>
40  *                [<space> "remote from" <space> <site>]
41  *    <forward> := ">" <from>
42  *          msg := <from> <forward>* <blank-line> <body>
43  *
44  * The output of rmail(8) compresses the <forward> lines into a single
45  * from path.
46  *
47  * The err(3) routine is included here deliberately to make this code
48  * a bit more portable.
49  */
50
51 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <sys/wait.h>
55
56 #include <ctype.h>
57 #include <fcntl.h>
58 #include <sm/io.h>
59 #include <stdlib.h>
60 #include <sm/string.h>
61 #include <unistd.h>
62 #ifdef EX_OK
63 # undef EX_OK           /* unistd.h may have another use for this */
64 #endif /* EX_OK */
65 #include <sysexits.h>
66
67 #include <sm/conf.h>
68 #include <sm/errstring.h>
69 #include <sendmail/pathnames.h>
70
71 static void err __P((int, const char *, ...));
72 static void usage __P((void));
73 static char *xalloc __P((int));
74
75 #define newstr(s)       strcpy(xalloc(strlen(s) + 1), s)
76
77 static char *
78 xalloc(sz)
79         register int sz;
80 {
81         register char *p;
82
83         /* some systems can't handle size zero mallocs */
84         if (sz <= 0)
85                 sz = 1;
86
87         p = malloc(sz);
88         if (p == NULL)
89                 err(EX_TEMPFAIL, "out of memory");
90         return (p);
91 }
92
93 int
94 main(argc, argv)
95         int argc;
96         char *argv[];
97 {
98         int ch, debug, i, pdes[2], pid, status;
99         size_t fplen = 0, fptlen = 0, len;
100         off_t offset;
101         SM_FILE_T *fp;
102         char *addrp = NULL, *domain, *p, *t;
103         char *from_path, *from_sys, *from_user;
104         char **args, buf[2048], lbuf[2048];
105         struct stat sb;
106         extern char *optarg;
107         extern int optind;
108
109         debug = 0;
110         domain = "UUCP";                /* Default "domain". */
111         while ((ch = getopt(argc, argv, "D:T")) != -1)
112         {
113                 switch (ch)
114                 {
115                   case 'T':
116                         debug = 1;
117                         break;
118
119                   case 'D':
120                         domain = optarg;
121                         break;
122
123                   case '?':
124                   default:
125                         usage();
126                 }
127         }
128
129         argc -= optind;
130         argv += optind;
131
132         if (argc < 1)
133                 usage();
134
135         from_path = from_sys = from_user = NULL;
136         for (offset = 0; ; )
137         {
138                 /* Get and nul-terminate the line. */
139                 if (sm_io_fgets(smioin, SM_TIME_DEFAULT, lbuf,
140                                 sizeof(lbuf)) == NULL)
141                         err(EX_DATAERR, "no data");
142                 if ((p = strchr(lbuf, '\n')) == NULL)
143                         err(EX_DATAERR, "line too long");
144                 *p = '\0';
145
146                 /* Parse lines until reach a non-"From" line. */
147                 if (!strncmp(lbuf, "From ", 5))
148                         addrp = lbuf + 5;
149                 else if (!strncmp(lbuf, ">From ", 6))
150                         addrp = lbuf + 6;
151                 else if (offset == 0)
152                         err(EX_DATAERR,
153                             "missing or empty From line: %s", lbuf);
154                 else
155                 {
156                         *p = '\n';
157                         break;
158                 }
159
160                 if (addrp == NULL || *addrp == '\0')
161                         err(EX_DATAERR, "corrupted From line: %s", lbuf);
162
163                 /* Use the "remote from" if it exists. */
164                 for (p = addrp; (p = strchr(p + 1, 'r')) != NULL; )
165                 {
166                         if (!strncmp(p, "remote from ", 12))
167                         {
168                                 for (t = p += 12; *t != '\0'; ++t)
169                                 {
170                                         if (isascii(*t) && isspace(*t))
171                                                 break;
172                                 }
173                                 *t = '\0';
174                                 if (debug)
175                                         (void) sm_io_fprintf(smioerr,
176                                                              SM_TIME_DEFAULT,
177                                                              "remote from: %s\n",
178                                                              p);
179                                 break;
180                         }
181                 }
182
183                 /* Else use the string up to the last bang. */
184                 if (p == NULL)
185                 {
186                         if (*addrp == '!')
187                                 err(EX_DATAERR, "bang starts address: %s",
188                                     addrp);
189                         else if ((t = strrchr(addrp, '!')) != NULL)
190                         {
191                                 *t = '\0';
192                                 p = addrp;
193                                 addrp = t + 1;
194                                 if (*addrp == '\0')
195                                         err(EX_DATAERR,
196                                             "corrupted From line: %s", lbuf);
197                                 if (debug)
198                                         (void) sm_io_fprintf(smioerr,
199                                                              SM_TIME_DEFAULT,
200                                                              "bang: %s\n", p);
201                         }
202                 }
203
204                 /* 'p' now points to any system string from this line. */
205                 if (p != NULL)
206                 {
207                         /* Nul terminate it as necessary. */
208                         for (t = p; *t != '\0'; ++t)
209                         {
210                                 if (isascii(*t) && isspace(*t))
211                                         break;
212                         }
213                         *t = '\0';
214
215                         /* If the first system, copy to the from_sys string. */
216                         if (from_sys == NULL)
217                         {
218                                 from_sys = newstr(p);
219                                 if (debug)
220                                         (void) sm_io_fprintf(smioerr,
221                                                              SM_TIME_DEFAULT,
222                                                              "from_sys: %s\n",
223                                                              from_sys);
224                         }
225
226                         /* Concatenate to the path string. */
227                         len = t - p;
228                         if (from_path == NULL)
229                         {
230                                 fplen = 0;
231                                 if ((from_path = malloc(fptlen = 256)) == NULL)
232                                         err(EX_TEMPFAIL, "out of memory");
233                         }
234                         if (fplen + len + 2 > fptlen)
235                         {
236                                 fptlen += SM_MAX(fplen + len + 2, 256);
237                                 if ((from_path = realloc(from_path,
238                                                          fptlen)) == NULL)
239                                         err(EX_TEMPFAIL, "out of memory");
240                         }
241                         memmove(from_path + fplen, p, len);
242                         fplen += len;
243                         from_path[fplen++] = '!';
244                         from_path[fplen] = '\0';
245                 }
246
247                 /* Save off from user's address; the last one wins. */
248                 for (p = addrp; *p != '\0'; ++p)
249                 {
250                         if (isascii(*p) && isspace(*p))
251                                 break;
252                 }
253                 *p = '\0';
254                 if (*addrp == '\0')
255                         addrp = "<>";
256                 if (from_user != NULL)
257                         free(from_user);
258                 from_user = newstr(addrp);
259
260                 if (debug)
261                 {
262                         if (from_path != NULL)
263                                 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
264                                                      "from_path: %s\n",
265                                                      from_path);
266                         (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
267                                              "from_user: %s\n", from_user);
268                 }
269
270                 if (offset != -1)
271                         offset = (off_t)sm_io_tell(smioin, SM_TIME_DEFAULT);
272         }
273
274
275         /* Allocate args (with room for sendmail args as well as recipients */
276         args = (char **)xalloc(sizeof(*args) * (10 + argc));
277
278         i = 0;
279         args[i++] = _PATH_SENDMAIL;     /* Build sendmail's argument list. */
280         args[i++] = "-G";               /* relay submission */
281         args[i++] = "-oee";             /* No errors, just status. */
282 #ifdef QUEUE_ONLY
283         args[i++] = "-odq";             /* Queue it, don't try to deliver. */
284 #else
285         args[i++] = "-odi";             /* Deliver in foreground. */
286 #endif
287         args[i++] = "-oi";              /* Ignore '.' on a line by itself. */
288
289         /* set from system and protocol used */
290         if (from_sys == NULL)
291                 sm_snprintf(buf, sizeof(buf), "-p%s", domain);
292         else if (strchr(from_sys, '.') == NULL)
293                 sm_snprintf(buf, sizeof(buf), "-p%s:%s.%s",
294                         domain, from_sys, domain);
295         else
296                 sm_snprintf(buf, sizeof(buf), "-p%s:%s", domain, from_sys);
297         args[i++] = newstr(buf);
298
299         /* Set name of ``from'' person. */
300         sm_snprintf(buf, sizeof(buf), "-f%s%s",
301                  from_path ? from_path : "", from_user);
302         args[i++] = newstr(buf);
303
304         /*
305         **  Don't copy arguments beginning with - as they will be
306         **  passed to sendmail and could be interpreted as flags.
307         **  To prevent confusion of sendmail wrap < and > around
308         **  the address (helps to pass addrs like @gw1,@gw2:aa@bb)
309         */
310
311         while (*argv != NULL)
312         {
313                 if (**argv == '-')
314                         err(EX_USAGE, "dash precedes argument: %s", *argv);
315
316                 if (strchr(*argv, ',') == NULL || strchr(*argv, '<') != NULL)
317                         args[i++] = *argv;
318                 else
319                 {
320                         len = strlen(*argv) + 3;
321                         if ((args[i] = malloc(len)) == NULL)
322                                 err(EX_TEMPFAIL, "Cannot malloc");
323                         sm_snprintf(args[i++], len, "<%s>", *argv);
324                 }
325                 argv++;
326                 argc--;
327
328                 /* Paranoia check, argc used for args[] bound */
329                 if (argc < 0)
330                         err(EX_SOFTWARE, "Argument count mismatch");
331         }
332         args[i] = NULL;
333
334         if (debug)
335         {
336                 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
337                                      "Sendmail arguments:\n");
338                 for (i = 0; args[i] != NULL; i++)
339                         (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
340                                              "\t%s\n", args[i]);
341         }
342
343         /*
344         **  If called with a regular file as standard input, seek to the right
345         **  position in the file and just exec sendmail.  Could probably skip
346         **  skip the stat, but it's not unreasonable to believe that a failed
347         **  seek will cause future reads to fail.
348         */
349
350         if (!fstat(STDIN_FILENO, &sb) && S_ISREG(sb.st_mode))
351         {
352                 if (lseek(STDIN_FILENO, offset, SEEK_SET) != offset)
353                         err(EX_TEMPFAIL, "stdin seek");
354                 (void) execv(_PATH_SENDMAIL, args);
355                 err(EX_OSERR, "%s", _PATH_SENDMAIL);
356         }
357
358         if (pipe(pdes) < 0)
359                 err(EX_OSERR, "pipe failed");
360
361         switch (pid = fork())
362         {
363           case -1:                              /* Err. */
364                 err(EX_OSERR, "fork failed");
365                 /* NOTREACHED */
366
367           case 0:                               /* Child. */
368                 if (pdes[0] != STDIN_FILENO)
369                 {
370                         (void) dup2(pdes[0], STDIN_FILENO);
371                         (void) close(pdes[0]);
372                 }
373                 (void) close(pdes[1]);
374                 (void) execv(_PATH_SENDMAIL, args);
375                 err(EX_UNAVAILABLE, "%s", _PATH_SENDMAIL);
376                 /* NOTREACHED */
377         }
378
379         if ((fp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT, (void *) &(pdes[1]),
380                              SM_IO_WRONLY, NULL)) == NULL)
381                 err(EX_OSERR, "sm_io_open failed");
382         (void) close(pdes[0]);
383
384         /* Copy the file down the pipe. */
385         do
386         {
387                 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s", lbuf);
388         } while (sm_io_fgets(smioin, SM_TIME_DEFAULT, lbuf,
389                              sizeof(lbuf)) != NULL);
390
391         if (sm_io_error(smioin))
392                 err(EX_TEMPFAIL, "stdin: %s", sm_errstring(errno));
393
394         if (sm_io_close(fp, SM_TIME_DEFAULT))
395                 err(EX_OSERR, "sm_io_close failed");
396
397         if ((waitpid(pid, &status, 0)) == -1)
398                 err(EX_OSERR, "%s", _PATH_SENDMAIL);
399
400         if (!WIFEXITED(status))
401                 err(EX_OSERR, "%s: did not terminate normally", _PATH_SENDMAIL);
402
403         if (WEXITSTATUS(status))
404                 err(status, "%s: terminated with %d (non-zero) status",
405                     _PATH_SENDMAIL, WEXITSTATUS(status));
406         exit(EX_OK);
407         /* NOTREACHED */
408         return EX_OK;
409 }
410
411 static void
412 usage()
413 {
414         (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
415                              "usage: rmail [-T] [-D domain] user ...\n");
416         exit(EX_USAGE);
417 }
418
419 static void
420 #ifdef __STDC__
421 err(int eval, const char *fmt, ...)
422 #else /* __STDC__ */
423 err(eval, fmt, va_alist)
424         int eval;
425         const char *fmt;
426         va_dcl
427 #endif /* __STDC__ */
428 {
429         SM_VA_LOCAL_DECL
430
431         if (fmt != NULL)
432         {
433                 SM_VA_START(ap, fmt);
434                 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "rmail: ");
435                 (void) sm_io_vfprintf(smioerr, SM_TIME_DEFAULT, fmt, ap);
436                 SM_VA_END(ap);
437                 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "\n");
438         }
439         exit(eval);
440 }