File: receive.c

package info (click to toggle)
dnprogs 2.43.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,936 kB
  • ctags: 3,872
  • sloc: ansic: 24,686; cpp: 10,608; makefile: 769; sh: 551; awk: 13
file content (608 lines) | stat: -rw-r--r-- 14,675 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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/******************************************************************************
    (c) 1998-2000 P.J. Caulfield               patrick@tykepenguin.cix.co.uk
    
    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
    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.
 ******************************************************************************
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>
#include <syslog.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
// Horrible hack for glibc 2.1+ which defines getnodebyname
#if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || __GLIBC__ >= 3
#define getnodebyname ipv6_getnodebyname
#endif

#include <netdb.h>

#if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || __GLIBC__ >= 3
#undef getnodebyname 
#endif

#include <netdnet/dnetdb.h>
#include <netdnet/dn.h>
#include <netinet/in.h>
#include <uudeview.h>
#include <dn_endian.h>
#include "configfile.h"

#ifndef SENDMAIL_COMMAND
#define SENDMAIL_COMMAND "/usr/sbin/sendmail -oem"
#endif

#define FB$UDF   0
#define FB$FIX   1
#define FB$VAR   2
#define FB$VFC   3
#define FB$STM   4
#define FB$STMLF 5
#define FB$STMCR 6

//RATs:
#define FB$FTN   0
#define FB$CR    1
#define FB$PRN   2
#define FB$BLK   3
#define FB$LSA   6



#define read(x,y,z) dnet_recv(x,y,z,MSG_EOR)

struct config_data
{
    unsigned char protocol_ver;
    unsigned char eco;
    unsigned char custeco;
    unsigned char os;
    unsigned int  options; /* Little-endian */
    unsigned int  iomode;  /* Little-endian */
    unsigned char rfm;
    unsigned char rat;
};
extern int block_mode;

char response[1024];
int send_smtp(int sock,
	      char *addressees,
	      char *cc_addressees,
	      char *remote_hostname,
	      char *remote_user,
	      char *subject,
	      char *full_user
	      );

int send_smail(int sock,
	       char *addressees,
	       char *cc_addressees,
	       char *remote_hostname,
	       char *remote_user,
	       char *subject,
	       char *full_user
	      );
int send_body(int dnsock, FILE *unixfile);

int smtp_response(FILE *sockstream);
int smtp_error(FILE *sockstream);


extern int verbosity;
static int is_binary = 0;
static struct config_data *config;

// Receive a message from VMS and pipe it into sendmail
void receive_mail(int sock)
{
    char   remote_user[256]; // VMS only sends 12 but...just in case!
    char   local_user[256];
    char   addressees[65536];
    char   cc_addressees[65536];
    char   full_user[256];
    char   subject[256];
    char   remote_hostname[256];
    struct sockaddr_dn sockaddr;
    struct optdata_dn  optdata;
    int    num_addressees=0;
    int    i;
    int    stat;
    int    namlen = sizeof(sockaddr);
    int    optlen = sizeof(optdata);

    // See if we are being sent binary data.
    // We classify binary data as anything with fixed length records
    // or an undefined record type.

#ifdef DSO_CONDATA
    config=(struct config_data *)optdata.opt_data;
    getsockopt(sock, DNPROTO_NSP, DSO_CONDATA, &optdata, &optlen);
    if (config->rfm == FB$FIX || config->rfm == FB$UDF)
    {
	fprintf(stderr, "rfm=%d, rat=%d\n", config->rfm, config->rat);
	is_binary = 1;
    }
#else
    config->rfm=FB$VAR;
#endif

    
    // Get the remote host name
    stat = getpeername(sock, (struct sockaddr *)&sockaddr, &namlen);
    if (!stat)
    {
        strcpy(remote_hostname, dnet_htoa(&sockaddr.sdn_add));
    }
    else
    {
	sprintf(remote_hostname, "%d.%d",
		(sockaddr.sdn_add.a_addr[1] >> 2),
		(((sockaddr.sdn_add.a_addr[1] & 0x03) << 8) |
		 sockaddr.sdn_add.a_addr[0]));
    }
    
    // Read the remote user name - this comes in padded with spaces to
    // 12 characters
    stat = read(sock, remote_user, sizeof(remote_user)); 
    if (stat < 0)
    {
	DNETLOG((LOG_ERR, "Error reading remote user: %m\n"));
	return;
    }

    // Trim the remote user.
    remote_user[stat] = '\0'; // Just in case its the full buffer
    i=stat;
    while (remote_user[i] == ' ') remote_user[i--] = '\0';
    
    // The rest of the message should be the local user names. These could
    // be a real local user or an internet mail name. They are not
    // padded.
    addressees[0] = '\0';
    do
    {
	stat = read(sock, local_user, sizeof(local_user));
	if (stat == -1)
	{
	    DNETLOG((LOG_ERR, "Error reading local user: %m\n"));
	    return;
	}
	if (local_user[0] != '\0')
	{
	    local_user[stat] = '\0';
	    DNETLOG((LOG_DEBUG, "got local user: %s\n", local_user));
	    strcat(addressees, local_user);
	    strcat(addressees, ",");
	    
	    // Send acknowledge
	    write(sock, "\001\000\000\000", 4);
	    num_addressees++;
	}
    }
    while (local_user[0] != '\0');

    // Remove trailing comma
    addressees[strlen(addressees)-1] = '\0';

    // TODO: This should be more intelligent and only lower-case the
    // addressable part of the email name.
    for (i=0; i<strlen(addressees); i++)
	addressees[i] = tolower(addressees[i]);
    
    // This is the collected list of users to send the message to,
    // as entered by the user.
    // We enter it in an extension header
    stat = read(sock, full_user, sizeof(full_user));
    if (stat == -1)
    {
	DNETLOG((LOG_ERR, "Error reading full_user: %m\n"));
	return;
    }
    full_user[stat] = '\0';
   
#ifdef DSO_CONDATA 
    stat = read(sock, cc_addressees, sizeof(cc_addressees));
    cc_addressees[stat] = '\0';
    for (i=0; i<strlen(cc_addressees); i++)
	cc_addressees[i] = tolower(cc_addressees[i]);
#else
    cc_addressees[0] = '\0';
#endif

    // Get the subject
    stat = read(sock, subject, sizeof(subject));
    if (stat == -1)
    {
	DNETLOG((LOG_ERR, "Error reading subject: %m\n"));
	return;
    }
    subject[stat] = '\0';

/* Not sure about this so just swallow it */
    { char bcc[255];
    stat = read(sock, bcc, sizeof(bcc));
    bcc[stat]='\0';
    }

    
    DNETLOG((LOG_INFO, "Forwarding mail from %s::%s to %s\n",
	     remote_hostname, remote_user, addressees));

// Decide on sendmail or SMTP

    if (config_smtphost[0] == '\0')
	stat = send_smail(sock, addressees, cc_addressees,
			  remote_hostname, remote_user,
			  subject, full_user);
    else
	stat = send_smtp(sock, addressees, cc_addressees,
			 remote_hostname, remote_user,
			 subject, full_user);
	
    if (stat == 0)
    {
	// Send acknowledge - one for each addressee
	for (i=0; i<num_addressees; i++)
	{
	    write(sock, "\001\000\000\000", 4);
	}
    }
    close(sock);
}

int send_smtp(int sock,
	      char *addressees,
	      char *cc_addressees,
	      char *remote_hostname,
	      char *remote_user,
	      char *subject,
	      char *full_user
	      )
{
    FILE              *sockfile;
    struct sockaddr_in serv_addr;
    struct hostent    *host_info;
    struct servent    *servent;
    int                port;
    char              *addr;
    int                stat;
    int                numbytes;
    int                smtpsock;

    smtpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (smtpsock == -1)
    {
	DNETLOG((LOG_ERR, "Can't open socket for SMTP: %m\n"));
	return -1;
    }
    
    host_info = gethostbyname(config_smtphost);
    if (host_info == NULL)
    {
        DNETLOG((LOG_ERR, "Cannot resolve host name: %s\n", config_smtphost));
        return -1;
    }
    
    servent = getservbyname("smtp", "tcp");
    if (servent != NULL)
    {
	port = ntohs(servent->s_port);
    }
    if (port == 0) port = 25;

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);
    
    serv_addr.sin_addr.s_addr = *(int*)host_info->h_addr_list[0];
    
    if (connect(smtpsock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1)
    {
	DNETLOG((LOG_ERR, "Cannot connect to SMTP server: %m\n"));
	return -1;
    }

/* Send initial SMTP commands and swallow responses */
    sockfile = fdopen(smtpsock, "a+");
    if (smtp_response(sockfile) != 220) return smtp_error(sockfile);
	
    fprintf(sockfile, "HELO %s\n", config_hostname);
    stat = smtp_response(sockfile);
    if (stat == 220)
	stat = smtp_response(sockfile);
    if (stat != 250) return smtp_error(sockfile);
    
    fprintf(sockfile, "MAIL FROM: <%s@%s>\n",
	    config_vmsmailuser, config_hostname);
    if (smtp_response(sockfile) != 250) return smtp_error(sockfile);

    addr = strtok(addressees, ",");
    while(addr)
    {
	fprintf(sockfile, "RCPT TO:<%s>\n", addr);
	if (smtp_response(sockfile) != 250) return smtp_error(sockfile);
	
	addr = strtok(NULL, ",");
    }

    addr = strtok(cc_addressees, ",");
    while(addr)
    {
	fprintf(sockfile, "RCPT TO:<%s>\n", addr);
	if (smtp_response(sockfile) != 250) return smtp_error(sockfile);
	
	addr = strtok(NULL, ",");
    }
    
    fprintf(sockfile, "DATA\n");
    if (smtp_response(sockfile) != 354) return smtp_error(sockfile);

    // Send the header
    fprintf(sockfile, "From: %s@%s (%s::%s)\n",
	    config_vmsmailuser, config_hostname, remote_hostname,
	    remote_user);
    fprintf(sockfile, "Subject: %s\n", subject);
    fprintf(sockfile, "To: %s\n", addressees);
    fprintf(sockfile, "Cc: %s\n", cc_addressees);
    if (is_binary)
    {
	fprintf(sockfile, "Mime-Version: 1.0\n");
	fprintf(sockfile, "Content-Type: application/octet-stream\n");
	fprintf(sockfile, "Content-Transfer-Encoding: base64\n");
    }
    fprintf(sockfile, "X-VMSmail: %s\n", full_user);
    
    fprintf(sockfile, "\n");
    
    send_body(sock, sockfile);

    fprintf(sockfile, ".\n");
    if (smtp_response(sockfile) != 250) return smtp_error(sockfile);
    
    fprintf(sockfile, "QUIT\n");
    if (smtp_response(sockfile) != 221) return smtp_error(sockfile);

    fclose(sockfile);
    return 0;
}


int send_smail(int sock,
	       char *addressees,
	       char *cc_addressees,
	       char *remote_hostname,
	       char *remote_user,
	       char *subject,
	       char *full_user
    )
{
    char   buf[65536];
    FILE  *mailpipe;
    int    stat;
     
    // Open a pipe to sendmail.
    sprintf(buf, "%s '%s'" , SENDMAIL_COMMAND, addressees);
    mailpipe = popen(buf, "w");
    if (mailpipe != NULL)
    {
	// Send the header
	fprintf(mailpipe, "From: %s@%s (%s::%s)\n",
		config_vmsmailuser, config_hostname, remote_hostname,
		remote_user);
	fprintf(mailpipe, "Subject: %s\n", subject);
	fprintf(mailpipe, "To: %s\n", addressees);
	fprintf(mailpipe, "Cc: %s\n", cc_addressees);
	if (is_binary)
	{
	    fprintf(mailpipe, "Mime-Version: 1.0\n");
	    fprintf(mailpipe, "Content-Type: application/octet-stream\n");
	    fprintf(mailpipe, "Content-Transfer-Encoding: base64\n");
	}
	fprintf(mailpipe, "X-VMSmail: %s\n", full_user);

	fprintf(mailpipe, "\n");

	send_body(sock, mailpipe);
	
	pclose(mailpipe);
    }
    else
    {
	DNETLOG((LOG_ERR, "Can't open pipe to sendmail: %m\n"));
	return -1;
    }
    return 0;
}


/*
  Read a response from the MTA and return the code number
  and the text of the response.
 */
int smtp_response(FILE *sockstream)
{
    int   status;
    int   len;
    char  codestring[5];

    if (fgets(response, sizeof(response), sockstream) == NULL)
    {
	strcpy(response, strerror(errno));
	return 0;
    }
    
    strncpy(codestring, response, 3);
    codestring[3] = '\0';
    status = atoi(codestring);

    return status;
}

int smtp_error(FILE *sockstream)
{
    DNETLOG((LOG_ERR, "SMTP Error: %s\n", response));
    fclose(sockstream);
    return -1;
}

int send_with_nlreplacement(FILE *f, char *buf, int len, char nlchar)
{
    int i,j;
    char newbuf[len];

    for (i=0, j=0; i<len; i++)
    {
	if (buf[i] == nlchar)
	    newbuf[j++] = '\n';
	else
	    newbuf[j++] = buf[i];	    
    }
    return fwrite(newbuf, j, 1, f);    
}

/* Convert RMS formatted text to Unix StreamLF */
int write_text(FILE *f, char *buf, int len)
{
    int reclen;
    int done,i;
    char *ptr = buf;
    char nlchar;
    static int carry = 0;
    
    if (block_mode)
    {
	switch (config->rfm)
	{
	case FB$UDF:
	case FB$FIX:
	case FB$STMLF:
	default:
	    return fwrite(buf, len, 1, f);
	    break;
	    
	case FB$VAR:
	case FB$VFC:
	    done = 0;
	    if (carry)
	    {
		fprintf(f, "%.*s\n", carry, ptr);
		ptr += carry;
		if (carry%2) ptr++;
		carry = 0;
	    }

	    /* Process the buffer */
	    while (done < len)
	    {
		reclen = dn_ntohs(*(unsigned short *)ptr);
		ptr++;
		ptr++;
		/* VFC files have two extra bytes at the start of each
		   record */
		if (config->rfm == FB$VFC)
		{
		    reclen -= 2;
		    ptr += 2;
		}

		/* Check for end of block */
		if ((ptr-buf)+reclen > len)
		{
		    carry = (done+reclen) - len + 4;
		    reclen = len-done-2;
		    fprintf(f, "%.*s", reclen, ptr);
		    return 0;
		}
		fprintf(f, "%.*s\n", reclen, ptr);
		
		/* Records are word-padded */
		if (reclen%2) reclen++;
		ptr += reclen;
		done = ptr-buf;
	    }
	    break;
	    
	case FB$STMCR:
	    send_with_nlreplacement(f, buf, len, '\r');
	    break;
	}
    }
    else
    {
	fprintf(f, "%.*s\n", len, buf);
    }
    return 0;
}

// Sends the message body, converting binary messages to MIME if necessary
int send_body(int dnsock, FILE *unixfile)
{
    char  buf[65535];
    int   stat, i;
    int   finished = 0;
    
    // Get the text of the message
    if (!is_binary)
    {
	do
	{
	    stat = read(dnsock, buf, sizeof(buf));
            if (stat == -1 && errno == EINTR)
                continue;

	    if (stat == -1 && errno != ENOTCONN)
	    {
		DNETLOG((LOG_ERR, "Error reading message text: %m\n"));
		return -1;
	    }

	    // VMS sends a lone NUL as the end of the message
	    if ((stat == 1 && buf[0] == '\0')) finished = 1;
	    if (stat > 0 && !finished)
	    {
		/* Interpret text format*/
		write_text(unixfile, buf, stat);
	    }
	}
	while (stat >= 0 && !finished);
    }
    else
    {
	int len;
	char tempname[PATH_MAX];
	int tempfile;

	sprintf(tempname, "/tmp/vmsmailXXXXXX");
	tempfile = mkstemp(tempname);
	if (tempfile < 0)
	{
	    DNETLOG((LOG_ERR, "Failed to make temp file: %s\n", strerror(errno)));
	    return -1;
	}
	while ( (len=read(dnsock, buf, sizeof(buf))) > 1)
	{	    
	    write(tempfile, buf, len);
	}
	close (tempfile);

	// Base64 encode it.
	UUInitialize();
	UUEncodeToStream(unixfile, NULL, tempname, B64ENCODED, "temp.bin", 0);
	unlink(tempname);
    }
    return 0;
}