Make sure that fields are indicated with a non-highlighted blank.
[unix-history] / usr / src / bin / rmail / rmail.c
CommitLineData
9b7cb659
KB
1/*
2 * Copyright (c) 1981, 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
19char copyright[] =
20"@(#) Copyright (c) 1981, 1988 The Regents of the University of California.\n\
21 All rights reserved.\n";
22#endif /* not lint */
23
205a2d85 24#ifndef lint
6c59f6dd 25static char sccsid[] = "@(#)rmail.c 4.11 (Berkeley) %G%";
9b7cb659 26#endif /* not lint */
205a2d85 27
9adc8f2c 28/*
306f949d
KB
29 * RMAIL -- UUCP mail server.
30 *
31 * This program reads the >From ... remote from ... lines that
32 * UUCP is so fond of and turns them into something reasonable.
33 * It calls sendmail giving it a -f option built from these lines.
34 */
9adc8f2c 35
306f949d
KB
36#include <stdio.h>
37#include <sysexits.h>
38#include <sys/types.h>
39#include <sys/file.h>
40#include <sys/stat.h>
b5fd168f 41# include "conf.h"
9adc8f2c 42
306f949d 43typedef char bool;
44ae4c24
EA
44#define TRUE 1
45#define FALSE 0
46
306f949d
KB
47extern char *index();
48extern char *rindex();
0f5791a6 49
306f949d 50char *Domain = "UUCP"; /* Default "Domain" */
0f5791a6 51
306f949d 52#define MAILER "/usr/lib/sendmail"
9adc8f2c
EA
53
54main(argc, argv)
306f949d 55 int argc;
0f5791a6 56 char **argv;
9adc8f2c 57{
ecbd0932
JL
58 char lbuf[1024]; /* one line of the message */
59 char from[512]; /* accumulated path of sender */
60 char ufrom[512]; /* user on remote system */
61 char sys[512]; /* a system in path */
306f949d 62 char fsys[512]; /* first system in path */
ecbd0932 63 char junk[1024]; /* scratchpad */
306f949d 64 char *args[100]; /* arguments to mailer command */
0f5791a6 65 register char *cp;
306f949d 66 register char *uf = NULL; /* ptr into ufrom */
9777ef27 67 int i;
306f949d
KB
68 long position;
69 struct stat sbuf;
70#ifdef DEBUG
71 bool Debug;
9adc8f2c 72
306f949d 73 if (argc > 1 && strcmp(argv[1], "-T") == 0) {
0f5791a6
EA
74 Debug = TRUE;
75 argc--;
76 argv++;
9adc8f2c 77 }
306f949d 78#endif
9adc8f2c 79
306f949d 80 if (argc < 2) {
0f5791a6
EA
81 fprintf(stderr, "Usage: rmail user ...\n");
82 exit(EX_USAGE);
83 }
306f949d
KB
84 if (argc > 2 && strncmp(argv[1], "-D", 2) == 0) {
85 Domain = &argv[1][2];
86 argc -= 2;
87 argv += 2;
88 }
89 from[0] = '\0';
90 fsys[0] = '\0';
f9566d23 91 (void) strcpy(ufrom, "/dev/null");
b8b8df54 92
306f949d 93 for (position = 0;; position = ftell(stdin)) {
74c5fe7c 94 (void) fgets(lbuf, sizeof lbuf, stdin);
306f949d
KB
95 if (strncmp(lbuf, "From ", 5) != 0 &&
96 strncmp(lbuf, ">From ", 6) != 0)
9adc8f2c 97 break;
ed45aae1 98 (void) sscanf(lbuf, "%s %s", junk, ufrom);
9adc8f2c 99 cp = lbuf;
306f949d
KB
100 uf = ufrom;
101 for (;;) {
102 cp = index(cp + 1, 'r');
103 if (cp == NULL) {
09fa495a
EA
104 register char *p = rindex(uf, '!');
105
306f949d 106 if (p != NULL) {
09fa495a 107 *p = '\0';
f9566d23 108 (void) strcpy(sys, uf);
09fa495a
EA
109 uf = p + 1;
110 break;
111 }
306f949d
KB
112 (void) strcpy(sys, "");
113 break; /* no "remote from" found */
09fa495a 114 }
9adc8f2c 115#ifdef DEBUG
0f5791a6
EA
116 if (Debug)
117 printf("cp='%s'\n", cp);
9adc8f2c 118#endif
306f949d 119 if (strncmp(cp, "remote from ", 12) == 0)
9adc8f2c
EA
120 break;
121 }
09fa495a
EA
122 if (cp != NULL)
123 (void) sscanf(cp, "remote from %s", sys);
306f949d
KB
124 if (fsys[0] == '\0')
125 (void) strcpy(fsys, sys);
126 if (sys[0]) {
127 (void) strcat(from, sys);
128 (void) strcat(from, "!");
129 }
9adc8f2c 130#ifdef DEBUG
0f5791a6 131 if (Debug)
09fa495a 132 printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
9adc8f2c
EA
133#endif
134 }
306f949d
KB
135 if (uf == NULL) { /* No From line was provided */
136 fprintf(stderr, "No From line in rmail\n");
137 exit(EX_DATAERR);
138 }
f9566d23 139 (void) strcat(from, uf);
306f949d
KB
140 (void) fstat(0, &sbuf);
141 (void) lseek(0, position, L_SET);
142
143 /*
144 * Now we rebuild the argument list and chain to sendmail. Note that
145 * the above lseek might fail on irregular files, but we check for
146 * that case below.
147 */
148 i = 0;
149 args[i++] = MAILER;
150 args[i++] = "-ee";
151 if (fsys[0] != '\0') {
152 static char junk2[512];
153
154 if (index(fsys, '.') == NULL) {
155 (void) strcat(fsys, ".");
156 (void) strcat(fsys, Domain);
157 }
158 (void) sprintf(junk2, "-oMs%s", fsys);
159 args[i++] = junk2;
160 }
161 (void) sprintf(junk, "-oMr%s", Domain);
162 args[i++] = junk;
163 if (from[0] != '\0') {
164 static char junk2[512];
9adc8f2c 165
306f949d
KB
166 (void) sprintf(junk2, "-f%s", from);
167 args[i++] = junk2;
168 }
169 for (; *++argv != NULL; i++) {
6c59f6dd
KB
170 /*
171 * don't copy arguments beginning with - as they will
172 * be passed to sendmail and could be interpreted as flags
173 */
174 if (**argv != '-')
175 args[i] = *argv;
0f5791a6 176 }
306f949d 177 args[i] = NULL;
9adc8f2c 178#ifdef DEBUG
306f949d
KB
179 if (Debug) {
180 printf("Command:");
181 for (i = 0; args[i]; i++)
182 printf(" %s", args[i]);
183 printf("\n");
9777ef27 184 }
306f949d
KB
185#endif
186 if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
187 /*
188 * If we were not called with standard input on a regular
189 * file, then we have to fork another process to send the
190 * first line down the pipe.
191 */
192 int pipefd[2];
193# ifdef DEBUG
194 if (Debug)
195 printf("Not a regular file!\n");
196# endif DEBUG
197 if (pipe(pipefd) < 0)
198 exit(EX_OSERR);
199 if (fork() == 0) {
200 /*
201 * Child: send the message down the pipe.
202 */
203 FILE *out;
9adc8f2c 204
306f949d
KB
205 out = fdopen(pipefd[1], "w");
206 close(pipefd[0]);
207 fputs(lbuf, out);
208 while (fgets(lbuf, sizeof lbuf, stdin))
209 fputs(lbuf, out);
210 (void) fclose(out);
211 exit(EX_OK);
212 }
213 /*
214 * Parent: call sendmail with pipe as standard input
215 */
216 close(pipefd[1]);
217 dup2(pipefd[0], 0);
218 }
219 execv(MAILER, args);
220 fprintf(stderr, "Exec of %s failed!\n", MAILER);
221 exit(EX_OSERR);
9adc8f2c 222}