-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpp.c
242 lines (198 loc) · 5.05 KB
/
mpp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* $Id$
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include "mailer_config.h"
char *arpadate(char *);
/* mpp msgfile outfile hostname */
int main(int argc, char *argv[])
{
FILE *f,*o;
struct stat sbuf;
char line[MAX_LINE_LEN+1];
char *message,*p;
char *longdate;
char from_addr[MAX_ADDR_LEN + 1];
char *user,*host;
time_t utime;
struct tm *ltime;
int hdr,reqhdr;
#if defined(USE_IDTAGS)
char idtag[MAX_ADDR_LEN+40];
char body_idtag[MAX_ADDR_LEN+40];
#endif /* USE_IDTAGS */
if(argc != 4)
{
fprintf(stderr,"Usage: mpp msgfile tmpfile from_addr\n");
exit(1);
}
if(stat(argv[1],&sbuf)==-1)
{
perror("stat (message)");
exit(1);
}
if(!(message=malloc((sbuf.st_size*2)+512)))
{
perror("malloc (message)");
exit(1);
}
if(!(f=fopen(argv[1],"r")))
{
perror("fopen (message)");
exit(1);
}
if(!(o=fopen(argv[2],"w")))
{
perror("fopen (temp file)");
exit(1);
}
/* break from_addr into user@host */
strcpy(from_addr, argv[3]);
host=rindex(from_addr,'@');
if(host == (char *) NULL) {
fprintf(stderr,"Couldn't parse %s into user@host\n",from_addr);
exit(1);
}
*host='\0';
++host;
user=from_addr;
p=message;
#define H_FROM 1
#define H_TO 2
#define H_SUBJ 4
#define H_REQ (H_FROM|H_TO|H_SUBJ)
hdr=1;
reqhdr=0;
/* per RFC822 4.1: preferred order of headers is: "Return-Path",
* "Received", "Date", "From", "Subject", "Sender", "To"
*/
longdate=arpadate(NULL);
utime=time(NULL);
ltime = localtime(&utime);
#if defined(USE_IDTAGS)
/* make the ID tags (used in message ID, optionally from and body)
* be careful about changing these (see do_list.c)
*/
sprintf(idtag,"%s.%04d%02d%02d.%c%s.%d", HEADER_HEADER,
ltime->tm_year+1900, ltime->tm_mon+1, ltime->tm_mday,
'\xff',"00000.000", (int) utime);
/* short/reshuffled version of idtag, embedded in the body */
sprintf(body_idtag,"%c%s%1d",
'\xff',"00000", ltime->tm_wday);
#endif /* USE_IDTAGS */
/* received header */
sprintf(p,"Received: from local (localhost)\r\n"
"\tby %s (mailer 1.10) with SMTP", host);
p+=strlen(p);
#if defined(USE_IDTAGS) && defined(TWEAK_RCVHDR)
sprintf(p," id <%s>", idtag);
p+=strlen(p);
#endif
sprintf(p,";\r\n\t%s\r\n",longdate);
p+=strlen(p);
sprintf(p,"Date: %s\r\n",longdate);
p+=strlen(p);
/* Message-Id: */
#if defined(USE_IDTAGS) && defined(TWEAK_MSGID)
sprintf(p,"Message-Id: <%s@%s>\r\n",idtag,host);
#else
sprintf(p,"Message-Id: <%s.%d@%s>\r\n",HEADER_HEADER,
(int)time(NULL),host);
#endif
p+=strlen(p);
while(fgets(line,MAX_LINE_LEN+1,f))
{
int l;
l=strlen(line);
while(l>0 && (line[l-1] == '\n' || line[l-1] == '\r'))
line[--l]='\0';
if(l > MAX_LINE_LEN-2)
{
fprintf(stderr, "Warning: line too long (>%d characters) in "
"message, truncated.\n", MAX_LINE_LEN);
}
if(l==0 && hdr) { /* blank line, end of headers */
hdr=0;
if(reqhdr != H_REQ)
{
fprintf(stderr,
"Missing required header(s) from message: %s%s%s\n",
(reqhdr & H_FROM) ? "" : "From: ",
(reqhdr & H_TO) ? "" : "To: ",
(reqhdr & H_SUBJ) ? "" : "Subject: ");
exit(1);
}
}
/* Add cr/lf */
line[l++]='\r'; line[l++]='\n'; line[l]='\0';
if(hdr)
{
char *t;
int i,tl,upcase,discard,newhdr;
if(!(t=strchr(line,':')))
{
fprintf(stderr,"Bad header: %s",line);
exit(1);
}
tl=t-line;
upcase=1;
line[0]=toupper(line[0]);
for(i=0;i<tl;++i)
{
line[i]=(upcase ? toupper (line[i])
: tolower(line[i]));
upcase=(line[i]=='-');
}
discard=newhdr=0;
if(!strncmp(line,"From: ",tl)) newhdr = H_FROM;
else if(!strncmp(line,"To: ",tl)) newhdr = H_TO;
else if(!strncmp(line,"Subject: ",tl)) newhdr = H_SUBJ;
else if(!strncmp(line,"Date: ",tl)) discard = 1;
else if(!strncasecmp(line,"Message-Id: ",tl)) discard = 1;
if((reqhdr & newhdr) || discard)
{
fprintf(stderr,"Warning: discarding extra header: %s",line); continue;
}
#if defined(USE_IDTAGS) && defined(TWEAK_FROMADDR)
if(newhdr == H_FROM) {
char *aptr;
aptr=index(line,'<');
if(aptr == (char *) NULL) {
aptr=index(line,'\r');
} else {
--aptr;
}
/* be careful that this looks like the message-id
* above!
*/
sprintf(aptr," <%s+%s@%s>\r\n", user, idtag, host);
}
#endif
reqhdr |= newhdr;
}
if(line[0]=='.') { *p='.'; ++p; }
sprintf(p,"%s",line);
p+=strlen(p);
}
#if defined(USE_IDTAGS) && defined(TWEAK_BODY)
/* add this at the end of the message. We'll strip it out later (in
* deliver() if we don't want it.
*/
sprintf(p,"<%s>\r\n",body_idtag);
p+=strlen(p);
#endif
/* EOM sentinel. Don't change this without looking at deliver.c */
sprintf(p-2,"\r\n.\r\n");
p+=strlen(p);
fprintf(o,"%s",message);
fclose(o);
fclose(f);
return 0;
}