File: ratSMTP.c

package info (click to toggle)
postilion 0.9.2-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 12,200 kB
  • ctags: 6,009
  • sloc: ansic: 66,879; tcl: 12,863; makefile: 1,054; sh: 242
file content (565 lines) | stat: -rw-r--r-- 14,830 bytes parent folder | download | duplicates (3)
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
 * ratSMTP.c --
 *
 *	This file contains basic support for sending messages via SMTP.
 *
 * TkRat software and its included text is Copyright 1996,1997,1998
 * by Martin Forssn
 *
 * Postilion software and its included text and images
 * Copyright (C) 1998 Nic Bernstein
 *
 * The full text of the legal notices is contained in the files called
 * COPYING and COPYRIGHT.TkRat, included with this distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "rat.h"

/*
 * Each channel has one of these structures. The channel handler is actually
 * just a pointer to this structure.
 */
typedef struct SMTPChannelPriv {
    Tcl_Channel channel;
    unsigned int mime : 1;	/* True if the peer supports 8 bit mime */
    unsigned int dsn : 1;	/* True if the peer supports DSN */
} SMTPChannelPriv;

/*
 * Local functions
 */
static char *RatTimedGets(Tcl_Interp *interp, Tcl_Channel channel, int timeout);
static int RatSendCommand(Tcl_Interp *interp, Tcl_Channel channel, char *cmd);
static int RatSendRcpt(Tcl_Interp *interp, Tcl_Channel channel,
	ADDRESS *adrPtr, DSNhandle handle, int *size, int verbose);


/*
 *----------------------------------------------------------------------
 *
 * RatTimedGets --
 *
 *      A gets with timeout. the channel must be in nonblocking mode.
 *
 * Results:
 *	Returns a pointer to a static area containing the read string.
 *
 * Side effects:
 *	The previous result is overwritten
 *
 *
 *----------------------------------------------------------------------
 */

static char*
RatTimedGets(Tcl_Interp *interp, Tcl_Channel channel, int timeout)
{
    static Tcl_DString ds;
    static int dsInit = 0;

    if (!dsInit) {
	dsInit = 1;
	Tcl_DStringInit(&ds);
    } else {
	Tcl_DStringSetLength(&ds, 0);
    }

    Tcl_SetChannelOption(interp, channel, "-blocking", "0");
    while (-1 == Tcl_Gets(channel, &ds)) {
	if (Tcl_InputBlocked(channel) && timeout) {
	    sleep(1),
	    timeout--;
	} else {
	    Tcl_SetChannelOption(interp, channel, "-blocking", "1");
	    return NULL;
	}
    }
    Tcl_SetChannelOption(interp, channel, "-blocking", "1");
    return Tcl_DStringValue(&ds);
}

/*
 *----------------------------------------------------------------------
 *
 * RatSendCommand --
 *
 *      Send a command to the SMTP peer, wait for and parse the result.
 *
 * Results:
 *	A standard Tcl result and an eventual error messages in 
 *	interp->result.
 *
 * Side effects:
 *	None.
 *
 *
 *----------------------------------------------------------------------
 */

int
RatSendCommand(Tcl_Interp *interp, Tcl_Channel channel, char *cmd)
{
    int result, timeout;
    char *reply;

    Tcl_Write(channel, cmd, -1);
    if ('\n' != cmd[strlen(cmd)-1]) {
	Tcl_Write(channel, "\r\n", -1);
    }
    Tcl_GetInt(interp, Tcl_GetVar2(interp, "option", "smtp_timeout",
	    TCL_GLOBAL_ONLY), &timeout);
    do {
	reply = RatTimedGets(interp, channel, timeout);
	if (reply) {
	    if ('2' == reply[0] || '3' == reply[0]) {
		result = TCL_OK;
	    } else {
		Tcl_SetResult(interp, reply, TCL_VOLATILE);
		result = TCL_ERROR;
	    }
	} else {
	    Tcl_SetResult(interp, "Timeout from SMTP server", TCL_STATIC);
	    result = TCL_ERROR;
	}
    } while (TCL_OK == result && '-' == reply[4]);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * RatSendRcpt --
 *
 *      Send the RCPT TO statements to the SMTP peer.
 *
 * Results:
 *	Returns the number of failed addresses. The result string of interp
 *	will contain more information in case of failure.
 *
 * Side effects:
 *	None.
 *
 *
 *----------------------------------------------------------------------
 */

static int
RatSendRcpt(Tcl_Interp *interp, Tcl_Channel channel, ADDRESS *adrPtr,
	DSNhandle handle, int *size, int verbose)
{
    char buf[1024], adr[1024], msg[1024];
    unsigned char *cPtr;
    int failures = 0, i;

    for (; adrPtr; adrPtr = adrPtr->next) {
	adr[0] = '\0';
	rfc822_address(adr, adrPtr);
	*size += strlen(adr)+8;
	sprintf(buf, "RCPT TO:<%s>", adr);
	if (handle) {
	    RatDSNAddRecipient(interp, handle,  adr);
	    sprintf(buf+strlen(buf), " NOTIFY=SUCCESS,FAILURE,DELAY");
	    sprintf(buf+strlen(buf), " ORCPT=rfc822;");
	    for (i = strlen(buf),cPtr = (unsigned char*)adr; *cPtr; cPtr++) {
		if (*cPtr < 33 || *cPtr > 126 || *cPtr == '+' || *cPtr == '=') {
		    buf[i++] = '+';
		    buf[i++] = alphabetHEX[*cPtr>>4];
		    buf[i++] = alphabetHEX[*cPtr&0xf];
		} else {
		    buf[i++] = *cPtr;
		}
	    }
	    buf[i] = '\0';
	}
	if (3 == verbose) {
	    sprintf(msg, "%s %s...", Tcl_GetVar2(interp, "t", "send_rcpt",
		    TCL_GLOBAL_ONLY), adr);
	    RatLog(interp, RAT_PARSE, msg, 1);
	    
	}
	if (TCL_OK != RatSendCommand(interp, channel, buf)) {
	    failures++;
	}
    }
    return failures;
}

/*
 *----------------------------------------------------------------------
 *
 * RatSMTPOpen --
 *
 *      Open an SMTP channel.
 *
 * Results:
 *	Returns a channel handler.
 *
 * Side effects:
 *	A new channel is created.
 *
 *
 *----------------------------------------------------------------------
 */

SMTPChannel
RatSMTPOpen (Tcl_Interp *interp, char *host, int verbose)
{
    SMTPChannelPriv *chPtr;
    char *reply, buf[1024], *msg, *cPtr;
    int port, timeout;

    strcpy(buf, host);
    if ((cPtr = strchr(buf, ':'))) {
	*cPtr++ = '\0';
	port = atoi(cPtr);
    } else {
	port = 25;	/* The default SMTP port */
    }

    if (verbose > 1) {
	msg = Tcl_GetVar2(interp, "t", "opening_connection", TCL_GLOBAL_ONLY);
	RatLog(interp, RAT_PARSE, msg, 1);
    }
    chPtr = (SMTPChannelPriv*)ckalloc(sizeof(SMTPChannelPriv));
    chPtr->mime = chPtr->dsn = 0;
    /*
     * Your compiler may complain that there are too many arguments to
     * Tcl_OpenTcpClient() below. This is a symtom of you having a prerelease
     * of tcl7.5 installed. In this case you must upgrade to the real
     * releases of tcl7.5/tk4.1 and reconfigure tkrat (this MUST be done).
     * After rerunning configure you may build tkrat.
     */
    if (NULL == (chPtr->channel = Tcl_OpenTcpClient(interp,port,buf,NULL,0,0))){
	ckfree(chPtr);
	return NULL;
    }
    Tcl_SetChannelOption(interp, chPtr->channel, "-buffering", "line");
    Tcl_SetChannelOption(interp, chPtr->channel, "-translation", "binary");

    /*
     * Get initial greeting
     */
    if (verbose > 1) {
	msg = Tcl_GetVar2(interp, "t", "wait_greeting", TCL_GLOBAL_ONLY);
	RatLog(interp, RAT_PARSE, msg, 1);
    }
    Tcl_GetInt(interp, Tcl_GetVar2(interp, "option", "smtp_timeout",
	    TCL_GLOBAL_ONLY), &timeout);
    do {
	reply = RatTimedGets(interp, chPtr->channel, timeout);
	if (!reply || '2' != reply[0]) {
	    Tcl_Close(interp, chPtr->channel);
	    ckfree(chPtr);
	    return NULL;
	}
    } while (strncmp("220 ", reply, 4));

    /*
     * Send EHLO (HELO) and get capabilities
     */
    if (verbose > 1) {
	msg = Tcl_GetVar2(interp, "t", "get_capabilities", TCL_GLOBAL_ONLY);
	RatLog(interp, RAT_PARSE, msg, 1);
    }
    sprintf(buf, "EHLO %s\r\n", currentHost);
    Tcl_Write(chPtr->channel, buf, -1);
    reply = RatTimedGets(interp, chPtr->channel, timeout);
    if (!reply || '2' != reply[0]) {
	sprintf(buf, "HELO %s.%s\r\n", currentHost, currentHost);
	Tcl_Write(chPtr->channel, buf, -1);
	reply = RatTimedGets(interp, chPtr->channel, timeout);
    }
    while (reply && strncmp("250 ", reply, 4)) {
	if (!reply) {
	    Tcl_Close(interp, chPtr->channel);
	    ckfree(chPtr);
	    return NULL;
	}
	if (!strncmp("8BITMIME", &reply[4], 8)) {
	    chPtr->mime = 1;
	} else if (!strncmp("DSN", &reply[4], 3)) {
	    chPtr->dsn = 1;
	}
	reply = RatTimedGets(interp, chPtr->channel, timeout);
    }

    if (verbose > 1) {
	RatLog(interp, RAT_PARSE, "", 1);
    }
    return (SMTPChannel)chPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * RatSMTPClose --
 *
 *      Close an SMTP channel.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	THe chanel is closed.
 *
 *
 *----------------------------------------------------------------------
 */

void
RatSMTPClose (Tcl_Interp *interp, SMTPChannel channel, int verbose)
{
    SMTPChannelPriv *chPtr = (SMTPChannelPriv*)channel;
    char *msg;

    if (verbose > 1) {
	msg = Tcl_GetVar2(interp, "t", "closing", TCL_GLOBAL_ONLY);
	RatLog(interp, RAT_PARSE, msg, 1);
    }
    Tcl_Write(chPtr->channel, "QUIT\r\n", -1);
    Tcl_Close(interp, chPtr->channel);
    if (verbose > 1) {
	RatLog(interp, RAT_PARSE, "", 1);
    }
    ckfree(chPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * RatSMTPSend --
 *
 *      Send a message with SMTP over the specified channel.
 *
 * Results:
 *	A standard Tcl result and an eventual error messages in 
 *	interp->result.
 *
 * Side effects:
 *	The DSN structures may be updated.
 *
 *
 *----------------------------------------------------------------------
 */

int
RatSMTPSend (Tcl_Interp *interp, SMTPChannel channel, ENVELOPE *envPtr,
	BODY *bodyPtr, int doDSN, int verbose)
{
    SMTPChannelPriv *chPtr = (SMTPChannelPriv*)channel;
    char buf[1024], *header, *msg;
    int failures = 0, size = 4096;
    DSNhandle handle = NULL;

    /*
     * Check input and reset stream to known status.
     */
    if (!(envPtr->to || envPtr->cc || envPtr->bcc)) {
	Tcl_SetResult(interp, "No recipients specified", TCL_STATIC);
	return TCL_ERROR;
    }
    if (TCL_OK != RatSendCommand(interp, chPtr->channel, "RSET")) {
	return TCL_ERROR;
    }

    /*
     * Check if we should request DSN
     */
    if (doDSN && !chPtr->dsn) {
	RatLog(interp, RAT_WARN, "DSN not avaliable", 0);
	doDSN = 0;
    }

    /*
     * Send envelope information.
     */
    if (verbose > 1) {
	if (verbose == 2) {
	    msg = Tcl_GetVar2(interp, "t", "send_envelope", TCL_GLOBAL_ONLY);
	} else {
	    msg = Tcl_GetVar2(interp, "t", "send_from", TCL_GLOBAL_ONLY);
	}
	RatLog(interp, RAT_PARSE, msg, 1);
    }
    sprintf(buf, "MAIL FROM:<");
    rfc822_address(buf, envPtr->return_path);
    strcat(buf, ">");
    if (chPtr->mime) {
	strcat(buf, " BODY=8BITMIME");
    }
    size += strlen(buf);
    size += (envPtr->remail?strlen(envPtr->remail):0);
    size += (envPtr->date?strlen(envPtr->date):0);
    size += (envPtr->subject?strlen(envPtr->subject):0);
    size += (envPtr->in_reply_to?strlen(envPtr->in_reply_to):0);
    size += (envPtr->message_id?strlen(envPtr->message_id):0);
    size += (envPtr->newsgroups?strlen(envPtr->newsgroups):0);
    size += (envPtr->followup_to?strlen(envPtr->followup_to):0);
    size += (envPtr->references?strlen(envPtr->references):0);
    if (doDSN) {
	RatGenId(NULL, interp, 0, NULL);
	handle = RatDSNStartMessage(interp, interp->result, envPtr->subject);
	strcat(buf, " ENVID=");
	strcat(buf, interp->result);
    }
    if (TCL_OK != RatSendCommand(interp, chPtr->channel, buf)) {
	return TCL_ERROR;
    }
    failures += RatSendRcpt(interp, chPtr->channel, envPtr->to, handle, &size,
	    verbose);
    failures += RatSendRcpt(interp, chPtr->channel, envPtr->cc, handle, &size,
	    verbose);
    failures += RatSendRcpt(interp, chPtr->channel, envPtr->bcc, handle, &size,
	    verbose);
    if (failures) {
	goto error;
    }

    /*
     * Send message data
     */
    if (verbose > 1) {
	msg = Tcl_GetVar2(interp, "t", "send_data", TCL_GLOBAL_ONLY);
	RatLog(interp, RAT_PARSE, msg, 1);
    }
    if (TCL_OK != RatSendCommand(interp, chPtr->channel, "DATA")) {
	goto error;
    }
    header = (char*)malloc(size);
    rfc822_output(header, envPtr, bodyPtr, RatTclPutsSMTP, chPtr->channel,
	    chPtr->mime);
    free(header);
    if (verbose > 1) {
	msg = Tcl_GetVar2(interp, "t", "wait_ack", TCL_GLOBAL_ONLY);
	RatLog(interp, RAT_PARSE, msg, 1);
    }
    if (TCL_OK != RatSendCommand(interp, chPtr->channel, ".")) {
	goto error;
    }
    if (handle) {
	RatDSNFinish(interp, handle);
    }

    return TCL_OK;

error:
    RatDSNAbort(interp, handle);
    RatSendCommand(interp, chPtr->channel, "RSET");
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * RatSMTPSupportDSN --
 *
 *      Check if a host supports DSN.
 *
 * Results:
 *	TCL_OK and interp->result will contain "1" if the host supports
 *	DSN and "0" otherwise.
 *
 * Side effects:
 *	None.
 *
 *
 *----------------------------------------------------------------------
 */

int
RatSMTPSupportDSN(ClientData dummy, Tcl_Interp *interp, int argc, char **argv)
{
    SMTPChannelPriv *chPrivPtr;
    SMTPChannel channel;
    int verbose, result;

    if (argc != 2) {
	Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
		" hostname\"", (char *) NULL);
	return TCL_ERROR;
    }

    Tcl_GetInt(interp, Tcl_GetVar2(interp, "option", "smtp_verbose",
	    TCL_GLOBAL_ONLY), &verbose);
    channel = RatSMTPOpen(interp, argv[1], verbose);
    if (channel) {
	chPrivPtr = (SMTPChannelPriv*)channel;
	result = chPrivPtr->dsn;
	RatSMTPClose(interp, channel, verbose);
    } else {
	result = 0;
    }
    if (verbose) {
	RatLog(interp, RAT_PARSE, "", 1);
    }
    Tcl_ResetResult(interp);
    sprintf(interp->result, "%d", result);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * RatTclPutsSMTP --
 *
 *	Like RatTclPuts but also escapes sigle dots with double dots.
 *
 * Results:
 *      Always returns 1L.
 *
 * Side effects:
 *      None.
 *
 *
 *----------------------------------------------------------------------
 */

long
RatTclPutsSMTP(void *stream_x, char *string)
{
    Tcl_Channel channel = (Tcl_Channel)stream_x;
    char *cPtr, *srcPtr;

    if ('.' == string[0]) {
	Tcl_Write(channel, ".", -1);
    }

    for (srcPtr = string; *srcPtr;) {
	if (!srcPtr[0] || !srcPtr[1] || !srcPtr[2]) {
	    break;
	}
	for (cPtr = srcPtr; cPtr[2]; cPtr++) {
	    if ('\r' == cPtr[0] && '\n' == cPtr[1] && '.' == cPtr[2]) {
		break;
	    }
	}
	if (cPtr[2]) {
	    if (-1 == Tcl_Write(channel, srcPtr, cPtr-srcPtr+3)
		    || -1 == Tcl_Write(channel, ".", -1)) {
		return 0;
	    }
	    srcPtr = cPtr+3;
	} else {
	    break;
	}
    }
    if (-1 == Tcl_Write(channel, srcPtr, -1)) {
	return 0;
    } else {
	return 1;
    }
}