summaryrefslogtreecommitdiffstats
path: root/imap/proxy.c
blob: 3a73cf151c64a73ea1565af61979e82c76f3bb0d (plain)
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*
** Copyright 2004-2005 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<errno.h>
#include	<ctype.h>
#include	<fcntl.h>
#include	<time.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#if	HAVE_SYS_WAIT_H
#include	<sys/wait.h>
#endif
#include	<sys/socket.h>
#include	<netinet/in.h>
#include	<arpa/inet.h>
#include	<netdb.h>
#if HAVE_SYS_SELECT_H
#include	<sys/select.h>
#endif
#include	<sys/time.h>
#if HAVE_POLL
#include	<sys/poll.h>
#endif
#include	<courierauthdebug.h>
#include	"proxy.h"


static int checkhostname(const char *host)
{
	char hostbuf[256];
	const char *proxyhostname;

	if ((proxyhostname=getenv("PROXY_HOSTNAME")) != NULL)
	{
		if (strcmp(proxyhostname, host) == 0)
		{
			courier_authdebug_printf("Proxy host %s is me, normal login.",
						 host);
			return -1;
		}
	}

	if (gethostname(hostbuf, sizeof(hostbuf)))
	{
		courier_authdebug_printf
			("gethostname failed: %s",
			 strerror(errno));
		return -1;
	}

	if (strcmp(hostbuf, host) == 0)
	{
		courier_authdebug_printf("Proxy host %s is me, normal login.",
					 host);
		return -1;
	}
	return 0;
}

static int connect_host(struct proxyinfo *pi, const char *host);

int connect_proxy(struct proxyinfo *pi)
{
	char *h=strdup(pi->host);
	char *p, *q;
	int fd;

	if (!h)
	{
		courier_authdebug_printf("%s", strerror(errno));
		return -1;
	}

	for (p=h; *p;)
	{
		if (*p == ',')
		{
			++p;
			continue;
		}

		for (q=p; *q; q++)
			if (*q == ',')
				break;
		if (*q)
			*q++=0;

		fd=connect_host(pi, p);
		if (fd >= 0)
		{
			free(h);
			return fd;
		}
		p=q;
	}
	free(h);
	return -1;
}


static int proxyconnect(struct proxyinfo *pi,
			int aftype,
			void *addr,
			size_t);

#if HAVE_GETADDRINFO

static int connect_host(struct proxyinfo *pi, const char *host)
{
	int fd;
	char portbuf[40];
	int errcode;
	struct addrinfo hints, *res, *p;

	if (checkhostname(host))
		return (0);

	sprintf(portbuf, "%d", pi->port);
	memset(&hints, 0, sizeof(hints));
	hints.ai_family=PF_UNSPEC;
	hints.ai_socktype=SOCK_STREAM;

	errcode=getaddrinfo(host, portbuf, &hints, &res);

	if (errcode)
	{
		courier_authdebug_printf
			("getaddrinfo on proxyhost %s failed: %s",
			 pi->host, gai_strerror(errcode));
		return (-1);
	}

	for (p=res; p; p=p->ai_next)
	{
		if ((fd=proxyconnect(pi, p->ai_family,
				     p->ai_addr,
				     p->ai_addrlen))
		    >= 0)
		{
			if ((*pi->connected_func)(fd, host,
						  pi->void_arg))
			{
				close(fd);
				courier_authdebug_printf
					("Failed: %s.", strerror(errno));
				continue;
			}
			freeaddrinfo(res);
			return fd;
		}
	}
	freeaddrinfo(res);
	courier_authdebug_printf
		("Connection to proxyhost %s failed.", host);
	return (-1);
}

#else
static int connect_host(struct proxyinfo *pi, const char *host)
{
	struct hostent *he;
	int i;
	int fd;

	if (checkhostname(host))
		return (0);

	he=gethostbyname(host);

	if (he == NULL)
	{
		courier_authdebug_printf
			("gethostbyname on proxyhost %s failed.",
			 host);
		return (-1);
	}

	for (i=0; he->h_addr_list[i]; i++)
	{
		switch (he->h_addrtype) {
		case AF_INET:
			{
				struct sockaddr_in sin;

				memset(&sin, 0, sizeof(sin));

				sin.sin_family=AF_INET;

				memcpy(&sin.sin_addr, he->h_addr_list[i],
				       sizeof(sin.sin_addr));
				sin.sin_port=htons(pi->port);

				fd=proxyconnect(pi, PF_INET, &sin,
						sizeof(sin));
			}
			break;
#ifdef AF_INET6
		case AF_INET6:
			{
				struct sockaddr_in6 sin6;

				memset(&sin6, 0, sizeof(sin6));

				sin6.sin6_family=AF_INET6;

				memcpy(&sin6.sin6_addr, he->h_addr_list[i],
				       sizeof(sin6.sin6_addr));

				sin6.sin6_port=htons(pi->port);
				fd=proxyconnect(pi, PF_INET6, &sin6,
						sizeof(sin6));
			}
			break;
#endif
		default:
			courier_authdebug_printf
				("Unknown address family type %d",
				 he->h_addrtype);
			continue;
		}

		if (fd >= 0)
		{
			if ((*pi->connected_func)(fd, host,
						  pi->void_arg))
			{
				close(fd);
				courier_authdebug_printf
					("Failed: %s.", strerror(errno));
				continue;
			}
			return fd;
		}
	}
	courier_authdebug_printf
		("Connection to proxyhost %s failed.", host);
	return (-1);
}
#endif

#if HAVE_POLL

static int proxy_waitfd(int fd, int waitWrite, const char *hostnamebuf)
{
	struct pollfd pfd;

	memset(&pfd, 0, sizeof(pfd));

	pfd.fd=fd;
	pfd.events= waitWrite ? POLLOUT:POLLIN;

	if (poll(&pfd, 1, 30 * 1000) < 0)
	{
		courier_authdebug_printf
			("Poll failed while waiting to connect to %s: %s",
			 hostnamebuf, strerror(errno));
		return -1;
	}

	if (pfd.revents & (POLLOUT|POLLIN))
		return 0;

	courier_authdebug_printf
		("Timeout/error connecting to %s", hostnamebuf);
	return -1;
}

#else
static int proxy_waitfd(int fd, int waitWrite, const char *hostnamebuf)
{
	fd_set fds;
	struct timeval tv;

	FD_ZERO(&fds);
	FD_SET(fd, &fds);
	tv.tv_sec=30;
	tv.tv_usec=0;

	if (select(fd+1, waitWrite ? NULL:&fds,
		   waitWrite ? &fds:NULL, NULL, &tv) < 0)
	{
		courier_authdebug_printf
			("Select failed while waiting to connect to %s: %s",
			 hostnamebuf, strerror(errno));

		return -1;
	}

	if (!FD_ISSET(fd, &fds))
	{
		courier_authdebug_printf
			("Timeout connecting to %s", hostnamebuf);
		return -1;
	}

	return 0;
}
#endif

static int proxyconnect(struct proxyinfo *pi,
			int pf,
			void *addr,
			size_t addrLen)
{
	int fd;
	char hostnamebuf[256];
	int errcode;
	socklen_t errcode_l;

	struct sockaddr *sa=(struct sockaddr *)addr;

	switch (sa->sa_family) {
	case AF_INET:
		{
			struct sockaddr_in *sin=(struct sockaddr_in *)sa;

			strcpy(hostnamebuf, inet_ntoa(sin->sin_addr));
		}
		break;

#ifdef AF_INET6
	case AF_INET6:
		{
			struct sockaddr_in6 *sin6=
				(struct sockaddr_in6 *)sa;

			if (inet_ntop(AF_INET6, &sin6->sin6_addr,
				      hostnamebuf,
				      sizeof(hostnamebuf)) == NULL)
				strcpy(hostnamebuf, "inet_ntop() failed");
		}
		break;
#endif
	}


	fd=socket(pf, SOCK_STREAM, 0);

	if (fd < 0)
	{
		courier_authdebug_printf("socket: %s", strerror(errno));
		return (-1);
	}

	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0 ||
	    fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
	{
		close(fd);
		courier_authdebug_printf("fcntl(socket): %s", strerror(errno));
	}

	if (connect(fd, addr, addrLen) == 0)
		return fd;

	if (errno != EINPROGRESS)
	{
		courier_authdebug_printf
			("Proxy connection to %s failed: %s",
			 hostnamebuf, strerror(errno));
		close(fd);
		return -1;
	}

	if (proxy_waitfd(fd, 1, hostnamebuf))
	{
		close(fd);
		return -1;
	}

	errcode_l=sizeof(errcode);

	if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &errcode_l) < 0)
	{
		courier_authdebug_printf
			("getsockopt failed: %s", strerror(errno));
		close(fd);
		return -1;
	}

	if (errcode)
	{
		courier_authdebug_printf
			("Proxy connection to %s failed: %s", hostnamebuf,
			 strerror(errcode));
		close(fd);
		return -1;
	}

	return fd;
}

static int proxy_getch(int fd, struct proxybuf *pb)
{
	if (pb->bufleft == 0)
	{
		int n;

		if (proxy_waitfd(fd, 0, "server"))
			return -1;

		pb->bufptr=pb->buffer;

		n=read(fd, pb->buffer, sizeof(pb->buffer));

		if (n < 0)
		{
			courier_authdebug_printf
				("Connection error: %s",
				 strerror(errno));
			return -1;
		}

		if (n == 0)
		{
			courier_authdebug_printf
				("Connection closed by remote host");
			return -1;
		}

		pb->bufleft=(size_t)n;
	}
	--pb->bufleft;
	return ((int)(unsigned char)*pb->bufptr++);
}

int proxy_readline(int fd, struct proxybuf *pb,
		   char *linebuf,
		   size_t linebuflen,
		   int imapmode)
{
	size_t i;
	int ch;
	int prevch;

	i=0;

	ch=0;

	do
	{
		prevch=ch;

		ch=proxy_getch(fd, pb);

		if (ch < 0)
			return -1;

		if (i < linebuflen)
			linebuf[i++]=(char)ch;
	} while (ch != '\n' || (imapmode && prevch != '\r'));

	if (i)
		linebuf[--i]=0;

	if (i && linebuf[i-1] == '\r')
		linebuf[--i]=0;

	DPRINTF("Received: %s", linebuf);

	return 0;
}

int proxy_write(int fd, const char *hostname,
		const char *buf, size_t buf_len)
{
	DPRINTF("Sending: %s", buf);

	while (buf_len)
	{
		int n;

		if (proxy_waitfd(fd, 1, hostname))
			return -1;

		n=write(fd, buf, buf_len);

		if (n < 0)
		{
			courier_authdebug_printf
				("Error sending to %s: %s",
				 hostname, strerror(errno));
			return -1;
		}

		if (n == 0)
		{
			courier_authdebug_printf
				("Connection close by %s", hostname);
			return -1;
		}

		buf_len -= n;
		buf += n;
	}
	return 0;
}

#if HAVE_POLL
void proxyloop(int fd)
{
	char stdin_buf[BUFSIZ];
	char stdout_buf[BUFSIZ];

	char *stdin_ptr=NULL;
	char *stdout_ptr=NULL;
	size_t stdin_left=0;
	size_t stdout_left=0;
	int n;

	struct pollfd pfd[2];

	if (fcntl(0, F_SETFL, O_NONBLOCK) ||
	    fcntl(1, F_SETFL, O_NONBLOCK))
	{
		courier_authdebug_printf("fcntl: %s",
					 strerror(errno));
		return;
	}

	do
	{
		memset(&pfd, 0, sizeof(pfd));

		if (stdin_left == 0)
		{
			pfd[0].fd=0;
			pfd[0].events=POLLIN;
		}
		else
		{
			pfd[0].fd=fd;
			pfd[0].events=POLLOUT;
		}

		if (stdout_left == 0)
		{
			pfd[1].fd=fd;
			pfd[1].events=POLLIN;
		}
		else
		{
			pfd[1].fd=1;
			pfd[1].events=POLLOUT;
		}

		n=1;

		if (poll(pfd, 2, -1) < 0)
		{
			courier_authdebug_printf("poll: %s",
						 strerror(errno));
			continue;
		}

		if (stdin_left == 0)
		{
			if (pfd[0].revents)
			{
				n=read(0, stdin_buf, sizeof(stdin_buf));

				if (n > 0)
				{
					stdin_ptr=stdin_buf;
					stdin_left=(size_t)n;
				}
			}
		}
		else if (pfd[0].revents)
		{
			n=write(fd, stdin_ptr, stdin_left);

			if (n > 0)
			{
				stdin_ptr += n;
				stdin_left -= n;
			}
		}

		if (n > 0)
		{
			if (stdout_left == 0)
			{
				if (pfd[1].revents)
				{
					n=read(fd, stdout_buf,
					       sizeof(stdout_buf));
				
					if (n > 0)
					{
						stdout_ptr=stdout_buf;
						stdout_left=(size_t)n;
					}
				}
			} else if (pfd[1].revents)
			{
				n=write(1, stdout_ptr, stdout_left);

				if (n > 0)
				{
					stdout_ptr += n;
					stdout_left -= n;
				}
			}
		}
	} while (n > 0);

	if (n < 0)
		courier_authdebug_printf("%s", strerror(errno));
}

#else
void proxyloop(int fd)
{
	char stdin_buf[BUFSIZ];
	char stdout_buf[BUFSIZ];

	char *stdin_ptr=NULL;
	char *stdout_ptr=NULL;
	size_t stdin_left=0;
	size_t stdout_left=0;
	int n;

	fd_set fdr, fdw;

	if (fcntl(0, F_SETFL, O_NONBLOCK) ||
	    fcntl(1, F_SETFL, O_NONBLOCK))
	{
		courier_authdebug_printf("fcntl: %s",
					 strerror(errno));
		return;
	}

	do
	{
		FD_ZERO(&fdr);
		FD_ZERO(&fdw);

		if (stdin_left == 0)
			FD_SET(0, &fdr);
		else
			FD_SET(fd, &fdw);

		if (stdout_left == 0)
			FD_SET(fd, &fdr);
		else
			FD_SET(1, &fdw);

		n=1;

		if (select(fd+1, &fdr, &fdw, NULL, NULL) < 0)
		{
			courier_authdebug_printf("select: %s",
						 strerror(errno));
			continue;
		}

		if (stdin_left == 0)
		{
			if (FD_ISSET(0, &fdr))
			{
				n=read(0, stdin_buf, sizeof(stdin_buf));

				if (n > 0)
				{
					stdin_ptr=stdin_buf;
					stdin_left=(size_t)n;
				}
			}
		}
		else if (FD_ISSET(fd, &fdw))
		{
			n=write(fd, stdin_ptr, stdin_left);

			if (n > 0)
			{
				stdin_ptr += n;
				stdin_left -= n;
			}
		}

		if (n > 0)
		{
			if (stdout_left == 0)
			{
				if (FD_ISSET(fd, &fdr))
				{
					n=read(fd, stdout_buf,
					       sizeof(stdout_buf));
				
					if (n > 0)
					{
						stdout_ptr=stdout_buf;
						stdout_left=(size_t)n;
					}
				}
			} else if (FD_ISSET(1, &fdw))
			{
				n=write(1, stdout_ptr, stdout_left);

				if (n > 0)
				{
					stdout_ptr += n;
					stdout_left -= n;
				}
			}
		}
	} while (n > 0);

	if (n < 0)
		courier_authdebug_printf("%s", strerror(errno));
}
#endif