-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtp.cpp
547 lines (476 loc) · 14.1 KB
/
smtp.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
SoupGate Open-Source License
Copyright (c) 1999 by Tom Torfs
The SoupGate software and its documentation may freely be distributed
and used for all purposes, provided no fee is charged other than to
cover administration and distribution costs, in other words it may
not be sold for profit.
The SoupGate software may freely be modified. Source code need
not be made available for modified versions or derived programs,
but if it is not, at least a copy of this license must be included
in the program or its documentation.
Modified source code may be made available, provided this license
remains included, intact and unmodified, and the fact that changes
were made must clearly be identified in both the source code and
documentation, and a reference must be provided as to where the
original, unmodified version can be obtained.
DISCLAIMER: THE AUTHOR EXCLUDES ANY AND ALL IMPLIED
WARRANTIES, INCLUDING WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHOR
MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS
OR IMPLIED, WITH RESPECT TO THIS SOFTWARE, ITS
QUALITY, PERFORMANCE, MERCHANTABILITY, OR FITNESS
FOR A PARTICULAR PURPOSE. THE AUTHOR SHALL HAVE NO
LIABILITY FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES ARISING OUT OF OR RESULTING FROM THE USE OR
MODIFICATION OF THIS SOFTWARE.
End of License
*/
/* Spoon SMTP functions */
#include "spoon.h"
enum {SMTP_OK, SMTP_ERR};
#define MAXEMAILCOUNT 500
static char *emaillist[MAXEMAILCOUNT];
static int emailcount = 0;
static char *fromemail = NULL;
/*...sgetsmtpanswer:0:*/
/* reads & analyzes SMTP answer
SMTP_OK (1) = 1XX (positive preliminary), 2XX (positive completion)
or 3XX (positive intermediate)
SMTP_ERR (0) = 4XX (negative transient), 5XX (negative permanent)
if quiet is set to nonzero, error messages will not be displayed
-1 = error */
static int getsmtpanswer(Socket &sock, int quiet)
{
int rval;
rval = sock.RecvTeol();
if (rval==-1)
{
net_error("Receive");
return -1;
}
if (rval==0)
{
logprintf("Timeout - SMTP host did not answer\n");
return -1;
}
if (sock.szOutBuf[0]>='1' && sock.szOutBuf[0]<='3')
return SMTP_OK;
if (!quiet)
{
/* no newline, sock.szOutBuf ends in \r\n */
logprintf("SMTP host returned error: %s", sock.szOutBuf);
}
return SMTP_ERR;
}
/*...e*/
/*...sisdelimiter:0:*/
static int isdelimiter(int c)
{
return (c=='\0' || c==' ' || c=='\t'
|| c=='<' || c=='>' || c=='(' || c==')'
|| c=='\r' || c=='\n');
}
/*...e*/
/*...sprocessemail:0:*/
/* scan email address(es) from s into emaillist, updating emailcount
(unless isfrom is 1, in which case will be written into fromemail)
changes s!*/
static void processemail(char *s, int isfrom)
{
char *cp,*cp2,*ep;
int inlevel,qlevel;
char *addrpos;
int addrlen;
cp = s;
while (emailcount<MAXEMAILCOUNT && cp!=NULL)
{
/* delimit current address */
inlevel = 0;
qlevel = 0;
for (cp2=cp; *cp2!=',' || inlevel!=0 || qlevel!=0; cp2++)
{
if (*cp2=='\0')
{
cp2 = NULL;
break;
}
if (*cp2=='\"' || *cp2=='\'')
qlevel = !qlevel;
else if (*cp2=='(' || *cp2=='<')
inlevel++;
else if (*cp2==')' || *cp2=='>')
inlevel--;
}
if (cp2!=NULL)
*(cp2++) = '\0';
/* find at-sign of e-mail address */
ep = strchr(cp,'@');
if (ep!=NULL)
{
/* scan till left delimiter */
while (ep>cp)
{
if (isdelimiter(ep[-1]))
break;
ep--;
}
/* scan until right delimiter */
addrlen = 0;
addrpos = ep;
while (!isdelimiter(*ep))
{
addrlen++;
ep++;
}
if (isfrom)
{
if (fromemail != NULL)
delete fromemail;
fromemail = new char[addrlen+1];
if (fromemail != NULL)
{
memcpy(fromemail, addrpos, addrlen);
fromemail[addrlen] = '\0';
}
}
else
{
emaillist[emailcount] = new char[addrlen+1];
if (emaillist[emailcount] != NULL)
{
memcpy(emaillist[emailcount], addrpos, addrlen);
emaillist[emailcount][addrlen] = '\0';
emailcount++;
}
}
}
cp = cp2;
}
}
/*...e*/
/*...spostemail:0:*/
int postemail(void)
{
Socket sock;
char filename[FILENAME_MAX];
char buffer[256];
char *cp, *cp2;
char format;
FILE *fp = NULL, *fp2 = NULL;
int mail;
long msgsize, nextmsgpos;
int i;
int into;
int keepmsg;
if (hostname[0]=='\0')
{
logprintf("Your hostname not defined\n");
goto error;
}
if (smtphost[0]=='\0')
{
logprintf("SMTP hostname not defined\n");
goto error;
}
strcpy(filename, soupdir);
strcat(filename, "REPLIES");
fp = _fsopen(filename, "rb", SH_DENYNO);
if (fp==NULL)
{
if (verbose)
logprintf("+ No REPLIES file found, not posting any email\n");
return 1;
}
mail = 0;
while (fgets(buffer, sizeof buffer, fp) != NULL)
{
cp = strchr(buffer, '\t');
if (cp!=NULL)
{
*(cp++) = '\0';
cp2 = strchr(cp, '\t');
if (cp2!=NULL)
*(cp2++) = '\0';
}
if (cp==NULL || cp2==NULL)
{
logprintf("! Error in REPLIES file\n");
killreplies = 0;
continue;
}
if (stricmp(cp, "mail") != 0)
{
if (stricmp(cp, "news") != 0)
{
logprintf("! Unknown message kind in REPLIES\n");
killreplies = 0;
}
else if (!transfer[NEWS][POST])
{
logprintf("News found --- will keep REPLIES file\n");
killreplies = 0;
}
continue;
}
format = cp2[0];
if (format!='u' && format!='m' && format!='M'
&& format!='B' && format!='b')
{
logprintf("! Unknown message format in REPLIES\n");
killreplies = 0;
continue;
}
if (!mail)
{
if (sock.Create() == -1)
{
net_error("Open socket");
goto error;
}
sock.ulTimeout = CONNECT_TIMEOUT;
logprintf("Posting mail to %s...\n", smtphost);
if (verbose)
logprintf("+ Connecting to SMTP host %s...\n", smtphost);
if (sock.Connect(smtphost, PORT_SMTP) == -1)
{
logprintf("Cannot connect to host %s at port %d\n",
smtphost, PORT_SMTP);
goto error;
}
if (verbose)
logprintf("+ Connected, sending HELO...\n");
if (getsmtpanswer(sock, 0) != SMTP_OK)
goto error;
sprintf(sock.szOutBuf, "HELO %s\r\n", hostname);
if (sock.Send(sock.szOutBuf) == -1)
{
net_error("Send");
goto quit;
}
if (getsmtpanswer(sock, 0) != SMTP_OK)
goto quit;
sock.ulTimeout = SOCK_TIMEOUT;
mail = 1;
}
strcpy(filename, soupdir);
strcat(filename, buffer);
strcat(filename, ".MSG");
fp2 = _fsopen(filename, "rb", SH_DENYNO);
if (fp2==NULL)
{
logprintf("! Cannot open message file %s\n", filename);
continue;
}
keepmsg = 0;
nextmsgpos = 0;
for (;;)
{
fseek(fp2, nextmsgpos, SEEK_SET);
msgsize = nextmessage(fp2, format);
if (msgsize==0)
break;
if (msgsize==-1)
{
logprintf("! Error in message file %s\n", filename);
keepmsg = 1;
break; /* still terminate message etc. */
}
nextmsgpos = ftell(fp2) + msgsize;
emailcount = 0;
fromemail = NULL;
into = 0;
while (ftell(fp2) < nextmsgpos)
{
if (fgets(buffer, sizeof buffer, fp2) == NULL)
break;
if (buffer[0]=='\n')
{
/* only interested in headers */
break;
}
else if (memicmp(buffer, "From:", 5) == 0)
{
processemail(buffer+5, 1);
into = 0;
}
else if (memicmp(buffer, "To:", 3) == 0)
{
processemail(buffer+3, 0);
into = 1;
}
else if (memicmp(buffer, "Cc:", 3) == 0)
{
processemail(buffer+3, 0);
into = 1;
}
else if (memicmp(buffer, "Bcc:", 4) == 0)
{
processemail(buffer+4, 0);
into = 1;
}
else if (isspace(buffer[0]) && into)
{
processemail(buffer, 0);
/* into still 1 */
}
else
{
into = 0;
}
}
if (fromemail==NULL)
{
logprintf("! No From address specified in message - skipping\n");
for (i=0; i<emailcount; i++)
delete emaillist[i];
keepmsg = 1;
continue;
}
if (emailcount==0)
{
logprintf("! No To address specified in message - skipping\n");
if (fromemail!=NULL)
delete fromemail;
keepmsg = 1;
continue;
}
for (i=0; i<emailcount; i++)
{
if (i==0 || separatemail)
{
logprintf("Mailing from %s\n", fromemail);
sprintf(sock.szOutBuf, "MAIL FROM:%s\r\n", fromemail);
if (sock.Send(sock.szOutBuf) == -1)
{
net_error("Send");
if (fromemail!=NULL)
delete fromemail;
for (i=0; i<emailcount; i++)
delete emaillist[i];
goto quit;
}
if (getsmtpanswer(sock, 0) != SMTP_OK)
{
logprintf("! Posting to SMTP server failed\n");
if (fromemail!=NULL)
delete fromemail;
for (i=0; i<emailcount; i++)
delete emaillist[i];
goto quit;
}
}
logprintf("Mailing to %s\n", emaillist[i]);;
sprintf(sock.szOutBuf, "RCPT TO:%s\r\n", emaillist[i]);
if (sock.Send(sock.szOutBuf) == -1)
{
net_error("Send");
if (fromemail!=NULL)
delete fromemail;
for (i=0; i<emailcount; i++)
delete emaillist[i];
goto quit;
}
if (getsmtpanswer(sock, 0) != SMTP_OK)
logprintf("! Recipient refused by SMTP server\n");
}
if (fromemail!=NULL)
delete fromemail;
for (i=0; i<emailcount; i++)
delete emaillist[i];
if (sock.Send("DATA\r\n") == -1)
{
net_error("Send");
goto quit;
}
if (getsmtpanswer(sock, 0) != SMTP_OK)
{
logprintf("! SMTP server won't accept message\n");
goto quit;
}
fseek(fp2, nextmsgpos - msgsize, SEEK_SET);
while (ftell(fp2) < nextmsgpos)
{
if (fgets(buffer, sizeof buffer - 2, fp2) == NULL)
break;
cp = strchr(buffer, '\n');
if (cp!=NULL && (cp==buffer || cp[-1]!='\r'))
{
cp[0] = '\r';
cp[1] = '\n';
cp[2] = '\0';
}
if (buffer[0]=='.')
{
memmove(buffer+1, buffer, strlen(buffer) + 1);
buffer[0] = '.'; /* redundant */
}
if (sock.Send(buffer) == -1)
{
net_error("Send");
if (fromemail!=NULL)
delete fromemail;
for (i=0; i<emailcount; i++)
delete emaillist[i];
goto quit;
}
}
if (sock.Send(".\r\n") == -1)
{
net_error("Send");
goto quit;
}
if (getsmtpanswer(sock, 0) != SMTP_OK)
{
logprintf("! SMTP server did not accept message\n");
continue;
}
}
fclose(fp2);
if (!testmode && !keepmsg)
remove(filename);
}
fclose(fp);
if (mail)
{
if (verbose)
logprintf("+ Signing off...\n");
if (sock.Send("QUIT\r\n") == -1)
{
net_error("Send");
goto quit;
}
if (getsmtpanswer(sock, 0) != SMTP_OK)
goto error;
sock.Close();
fclose(fp);
if (verbose)
logprintf("+ SMTP session successfully terminated\n");
}
else
{
if (verbose)
logprintf("+ No mail messages to be posted\n");
}
if (killreplies == -1)
killreplies = 1;
return 1;
quit:
if (sock.Send("QUIT\r\n") == -1)
{
net_error("Send");
goto quit;
}
getsmtpanswer(sock, 0);
error:
killreplies = 0;
if (fp!=NULL)
fclose(fp);
if (fp2!=NULL)
fclose(fp2);
if (sock.iSock != -1)
sock.Close();
logprintf("(skipping email post)\n");
return 0;
}
/*...e*/