/[pkg-mixmaster]/trunk/Mix/Src/mail.c
ViewVC logotype

Contents of /trunk/Mix/Src/mail.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 537 - (show annotations) (download)
Sun Jun 29 19:12:45 2003 UTC (9 years, 10 months ago) by weaselp
File MIME type: text/plain
File size: 19028 byte(s)
Rewind message buffer after searching recipient address.
1 /* Mixmaster version 3 -- (C) 1999 Anonymizer Inc.
2
3 Mixmaster may be redistributed and modified under certain conditions.
4 This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
5 ANY KIND, either express or implied. See the file COPYRIGHT for
6 details.
7
8 Socket-based mail transport services
9 $Id: mail.c,v 1.18 2003/06/29 19:12:45 weaselp Exp $ */
10
11
12 #include "mix3.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #if defined(UNIX) && defined(USE_SOCK)
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #endif /* defined(UNIX) && defined(USE_SOCK) */
25
26 #include <fcntl.h>
27 #include <time.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30
31
32 int sendinfofile(char *name, char *logname, BUFFER *address, BUFFER *header)
33 {
34 FILE *f = NULL, *log = NULL;
35 BUFFER *msg, *addr;
36 char line[LINELEN];
37 int ret = -1;
38
39 if (bufeq(address, ANONNAME))
40 return (0); /* don't reply to our own anon messages */
41 f = mix_openfile(name, "r");
42 if (f == NULL)
43 return (-1);
44
45 addr = buf_new();
46 rfc822_addr(address, addr);
47 if (addr->length == 0)
48 buf_set(addr, address), buf_nl(addr);
49
50 if (logname != NULL) {
51 if ((log = mix_openfile(logname, "r+")) != NULL) {
52 /* log recipients to prevent mail loop */
53 while (fgets(line, sizeof(line), log) != NULL)
54 if (strieq(line, addr->data))
55 goto end;
56 } else if ((log = mix_openfile(logname, "w")) == NULL) {
57 errlog(ERRORMSG, "Can't create %s.\n", logname);
58 ret = -1;
59 goto end;
60 }
61 fprintf(log, "%s", addr->data);
62 }
63 msg = buf_new();
64 if (header)
65 buf_cat(msg, header), buf_nl(msg);
66 while (fgets(line, sizeof(line), f) != NULL) {
67 if (streq(line, "DESTINATION-BLOCK\n"))
68 buf_appendf(msg, "destination-block %b", addr);
69 else
70 buf_appends(msg, line);
71 }
72 ret = sendmail(msg, REMAILERNAME, address);
73 buf_free(msg);
74 end:
75 if (f)
76 fclose(f);
77 if (log)
78 fclose(log);
79 buf_free(addr);
80 return (ret);
81 }
82
83 int smtpsend(BUFFER *head, BUFFER *message, char *from);
84
85
86 int sendmail_loop(BUFFER *message, char *from, BUFFER *address)
87 {
88 BUFFER *msg;
89 int err;
90
91 msg = buf_new();
92 buf_appendf(msg, "X-Loop: %s\n", REMAILERADDR);
93 buf_cat(msg, message);
94 err = sendmail(msg, from, address);
95 buf_free(msg);
96
97 return(err);
98 }
99
100 int sendmail(BUFFER *message, char *from, BUFFER *address)
101 {
102 /* returns: 0: ok 1: problem, try again -1: failed */
103
104 BUFFER *head, *block, *rcpt;
105 FILE *f;
106 int err = -1;
107
108 head = buf_new();
109 rcpt = buf_new();
110
111 block = readdestblk( );
112 if ( !block ) block = buf_new( );
113
114 if (address != NULL &&
115 (address->length == 0 || doblock(address, block, 1) == -1))
116 goto end;
117
118 if (from != NULL) {
119 buf_setf(head, "From: %s", from);
120 hdr_encode(head, 255);
121 buf_nl(head);
122 }
123 if (address != NULL)
124 buf_appendf(head, "To: %b\n", address);
125
126 buf_rewind(message);
127
128 /* search the to address */
129 if (address == NULL) {
130 BUFFER *field, *content;
131 field = buf_new();
132 content = buf_new();
133
134 while (buf_getheader(message, field, content) == 0) {
135 if (bufieq(field, "to")) {
136 buf_set(rcpt, content);
137 break;
138 }
139 }
140 buf_free(field);
141 buf_free(content);
142 } else {
143 buf_set(rcpt, address);
144 }
145 buf_rewind(message);
146
147 if ( REMAIL && strcmp(REMAILERADDR, rcpt->data) == 0) {
148 buf_cat(head, message);
149 err = pool_add(head, "inf");
150 } else if (SMTPRELAY[0])
151 err = smtpsend(head, message, from);
152 else if (strieq(SENDMAIL, "outfile")) {
153 char path[PATHMAX];
154 FILE *f = NULL;
155 #ifdef SHORTNAMES
156 int i;
157 for (i = 0; i < 10000; i++) {
158 snprintf(path, PATHMAX, "%s%cout%i.txt", POOLDIR, DIRSEP, i);
159 f = fopen(path, "r");
160 if (f)
161 fclose(f);
162 else
163 break;
164 }
165 f = fopen(path, "w");
166 #else /* end of SHORTNAMES */
167 static unsigned long namecounter = 0;
168 struct stat statbuf;
169 int count;
170 char hostname[64];
171
172 hostname[0] = '\0';
173 gethostname(hostname, 63);
174 hostname[63] = '\0';
175
176 /* Step 2: Stat the file. Wait for ENOENT as a response. */
177 for (count = 0;; count++) {
178 snprintf(path, PATHMAX, "%s%cout.%lu.%u_%lu.%s,S=%lu.txt",
179 POOLDIR, DIRSEP, time(NULL), getpid(), namecounter++, hostname, head->length + message->length);
180
181 if (stat(path, &statbuf) == 0)
182 errno = EEXIST;
183 else if (errno == ENOENT) { /* create the file (at least try) */
184 f = fopen(path, "w");
185 if (f != NULL)
186 break; /* we managed to open the file */
187 }
188 if (count > 5)
189 break; /* Too many retries - give up */
190 #ifdef WIN32
191 Sleep(2000); /* sleep and retry */
192 #else /* end of WIN32 */
193 sleep(2); /* sleep and retry */
194 #endif /* else not WIN32 */
195 }
196 #endif /* else not SHORTNAMES */
197 if (f != NULL) {
198 err = buf_write(head, f);
199 err = buf_write(message, f);
200 fclose(f);
201 } else
202 errlog(ERRORMSG, "Can't create %s!\n", path);
203 } else {
204 if (SENDANONMAIL[0] != '\0' && (from == NULL || streq(from, ANONNAME)))
205 f = openpipe(SENDANONMAIL);
206 else
207 f = openpipe(SENDMAIL);
208 if (f != NULL) {
209 err = buf_write(head, f);
210 err = buf_write(message, f);
211 closepipe(f);
212 }
213 }
214 if (err != 0)
215 err = 1; /* error while sending, retry later */
216 end:
217 buf_free(block);
218 buf_free(head);
219 buf_free(rcpt);
220 return (err);
221 }
222
223 /* socket communication **********************************************/
224
225 #ifdef USE_SOCK
226 #ifdef WIN32
227 WSADATA w;
228
229 int sock_init()
230 {
231 if (WSAStartup(MAKEWORD(2, 0), &w) != 0) {
232 errlog(ERRORMSG, "Unable to initialize WINSOCK.\n");
233 return 0;
234 }
235 return 1;
236 }
237
238 void sock_exit(void)
239 {
240 WSACleanup();
241 }
242 #endif /* WIN32 */
243
244 SOCKET opensocket(char *hostname, int port)
245 {
246 struct hostent *hp;
247 struct sockaddr_in server;
248 SOCKET s;
249
250 if ((hp = gethostbyname(hostname)) == NULL)
251 return (INVALID_SOCKET);
252
253 memset((char *) &server, 0, sizeof(server));
254 server.sin_family = AF_INET;
255 server.sin_addr.s_addr = *(unsigned long *) hp->h_addr;
256 server.sin_port = htons((unsigned short) port);
257
258 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
259 if (s != INVALID_SOCKET)
260 if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
261 closesocket(s);
262 return (INVALID_SOCKET);
263 }
264 return (s);
265 }
266
267 #ifndef WIN32
268 int closesocket(SOCKET s)
269 {
270 return (close(s));
271 }
272 #endif /* ifndef WIN32 */
273
274 int sock_getline(SOCKET s, BUFFER *line)
275 {
276 char c;
277 int ok;
278
279 buf_clear(line);
280 while ((ok = recv(s, &c, 1, 0)) > 0) {
281 if (c == '\n')
282 break;
283 if (c != '\r')
284 buf_appendc(line, c);
285 }
286 if (ok <= 0)
287 return (-1);
288 if (line->length == 0)
289 return (1);
290 return (0);
291 }
292
293 int sock_cat(SOCKET s, BUFFER *b)
294 {
295 int p = 0, n;
296
297 do {
298 n = send(s, b->data, b->length, 0);
299 if (n < 0)
300 return (-1);
301 p += n;
302 } while (p < b->length);
303 return (0);
304 }
305 #else /* end of USE_SOCK */
306 SOCKET opensocket(char *hostname, int port)
307 {
308 return (INVALID_SOCKET);
309 }
310
311 int closesocket(SOCKET s)
312 {
313 return (INVALID_SOCKET);
314 }
315
316 int sock_getline(SOCKET s, BUFFER *line)
317 {
318 return (-1);
319 }
320
321 int sock_cat(SOCKET s, BUFFER *b)
322 {
323 return (-1);
324 }
325 #endif /* else not USE_SOCK */
326
327 /* send messages by SMTP ************************************************/
328
329 static int sock_getsmtp(SOCKET s, BUFFER *response)
330 {
331 int ret;
332 BUFFER *line;
333 line = buf_new();
334
335 buf_clear(response);
336 do {
337 ret = sock_getline(s, line);
338 buf_cat(response, line);
339 } while (line->length >= 4 && line->data[3] == '-');
340 buf_free(line);
341 return (ret);
342 }
343
344 SOCKET smtp_open(void)
345 {
346 int s = INVALID_SOCKET;
347 int esmtp = 0;
348 BUFFER *line;
349
350 #ifdef USE_SOCK
351 if (SMTPRELAY[0] != '\0')
352 s = opensocket(SMTPRELAY, 25);
353 if (s != INVALID_SOCKET) {
354 line = buf_new();
355 sock_getsmtp(s, line);
356 if (bufifind(line, "ESMTP"))
357 esmtp = 1;
358 if (line->data[0] != '2') {
359 errlog(ERRORMSG, "SMTP relay not ready. %b\n", line);
360 closesocket(s);
361 s = INVALID_SOCKET;
362 } else {
363 errlog(DEBUGINFO, "Opening SMTP connection.\n");
364 if (esmtp)
365 buf_sets(line, "EHLO ");
366 else
367 buf_sets(line, "HELO ");
368 if (HELONAME[0])
369 buf_appends(line, HELONAME);
370 else if (strchr(ENVFROM, '@'))
371 buf_appends(line, strchr(ENVFROM, '@') + 1);
372 else {
373 struct sockaddr_in sa;
374 int len = sizeof(sa);
375 struct hostent *hp;
376
377 if (getsockname(s, (struct sockaddr *) &sa, &len) == 0 &&
378 (hp = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr),
379 AF_INET)) != NULL)
380 buf_appends(line, (char *) hp->h_name);
381 else if (strchr(REMAILERADDR, '@'))
382 buf_appends(line, strchr(REMAILERADDR, '@') + 1);
383 else
384 buf_appends(line, SHORTNAME);
385 }
386 buf_chop(line);
387 buf_appends(line, "\r\n");
388 sock_cat(s, line);
389 sock_getsmtp(s, line);
390 if (line->data[0] != '2') {
391 errlog(ERRORMSG, "SMTP relay refuses HELO: %b\n", line);
392 closesocket(s);
393 s = INVALID_SOCKET;
394 } else if (SMTPUSERNAME[0] && esmtp && bufifind(line, "AUTH") && bufifind(line, "LOGIN")) {
395 buf_sets(line, "AUTH LOGIN\r\n");
396 sock_cat(s, line);
397 sock_getsmtp(s, line);
398 if (!bufleft(line, "334")) {
399 errlog(ERRORMSG, "SMTP AUTH fails: %b\n", line);
400 goto err;
401 }
402 buf_sets(line, SMTPUSERNAME);
403 encode(line, 0);
404 buf_appends(line, "\r\n");
405 sock_cat(s, line);
406 sock_getsmtp(s, line);
407 if (!bufleft(line, "334")) {
408 errlog(ERRORMSG, "SMTP username rejected: %b\n", line);
409 goto err;
410 }
411 buf_sets(line, SMTPPASSWORD);
412 encode(line, 0);
413 buf_appends(line, "\r\n");
414 sock_cat(s, line);
415 sock_getsmtp(s, line);
416 if (!bufleft(line, "235"))
417 errlog(ERRORMSG, "SMTP authentication failed: %b\n", line);
418 }
419 }
420 err:
421 buf_free(line);
422 }
423 #endif /* USE_SOCK */
424 return (s);
425 }
426
427 int smtp_close(SOCKET s)
428 {
429 BUFFER *line;
430 int ret = -1;
431
432 #ifdef USE_SOCK
433 line = buf_new();
434 buf_sets(line, "QUIT\r\n");
435 sock_cat(s, line);
436 if (sock_getsmtp(s, line) == 0 && line->data[0] == '2') {
437 errlog(DEBUGINFO, "Closing SMTP connection.\n");
438 ret = 0;
439 } else
440 errlog(WARNING, "SMTP quit failed: %b\n", line);
441 closesocket(s);
442 buf_free(line);
443 #endif /* USE_SOCK */
444 return (ret);
445 }
446
447 int smtp_send(SOCKET relay, BUFFER *head, BUFFER *message, char *from)
448 {
449 BUFFER *rcpt, *line, *field, *content;
450 int ret = -1;
451
452 #ifdef USE_SOCK
453 line = buf_new();
454 field = buf_new();
455 content = buf_new();
456 rcpt = buf_new();
457
458 while (buf_getheader(head, field, content) == 0)
459 if (bufieq(field, "to"))
460 #ifdef BROKEN_MTA
461 if (!bufifind(rcpt, content->data))
462 /* Do not add the same recipient twice.
463 Needed for brain-dead MTAs. */
464 #endif /* BROKEN_MTA */
465 rfc822_addr(content, rcpt);
466 buf_rewind(head);
467
468 while (buf_isheader(message) && buf_getheader(message, field, content) == 0) {
469 if (bufieq(field, "to") || bufieq(field, "cc") || bufieq(field, "bcc")) {
470 #ifdef BROKEN_MTA
471 if (!bufifind(rcpt, content->data))
472 /* Do not add the same recipient twice.
473 Needed for brain-dead MTAs. */
474 #endif /* BROKEN_MTA */
475 rfc822_addr(content, rcpt);
476 }
477 if (!bufieq(field, "bcc"))
478 buf_appendheader(head, field, content);
479 }
480 buf_nl(head);
481
482 buf_clear(content);
483 if (from) {
484 buf_sets(line, from);
485 rfc822_addr(line, content);
486 buf_chop(content);
487 }
488 if (bufieq(content, REMAILERADDR) || bufieq(content, ANONADDR))
489 buf_clear(content);
490 if (content->length == 0)
491 buf_sets(content, ENVFROM[0] ? ENVFROM : ANONADDR);
492
493 buf_setf(line, "MAIL FROM:<%b>\r\n", content);
494 sock_cat(relay, line);
495 sock_getsmtp(relay, line);
496 if (!line->data[0] == '2') {
497 errlog(ERRORMSG, "SMTP relay does not accept mail: %b\n", line);
498 goto end;
499 }
500 while (buf_getline(rcpt, content) == 0) {
501 buf_setf(line, "RCPT TO:<%b>\r\n", content);
502 sock_cat(relay, line);
503 sock_getsmtp(relay, line);
504 if (bufleft(line, "421")) {
505 errlog(ERRORMSG, "SMTP relay error: %b\n", line);
506 goto end;
507 }
508 if (bufleft(line, "530")) {
509 errlog(ERRORMSG, "SMTP authentication required: %b\n", line);
510 goto end;
511 }
512 }
513
514 buf_sets(line, "DATA\r\n");
515 sock_cat(relay, line);
516 sock_getsmtp(relay, line);
517 if (!bufleft(line, "354")) {
518 errlog(WARNING, "SMTP relay does not accept message: %b\n", line);
519 goto end;
520 }
521 while (buf_getline(head, line) >= 0) {
522 buf_appends(line, "\r\n");
523 if (bufleft(line, ".")) {
524 buf_setf(content, ".%b", line);
525 buf_move(line, content);
526 }
527 sock_cat(relay, line);
528 }
529 while (buf_getline(message, line) >= 0) {
530 buf_appends(line, "\r\n");
531 if (bufleft(line, ".")) {
532 buf_setf(content, ".%b", line);
533 buf_move(line, content);
534 }
535 sock_cat(relay, line);
536 }
537 buf_sets(line, ".\r\n");
538 sock_cat(relay, line);
539 sock_getsmtp(relay, line);
540 if (bufleft(line, "2"))
541 ret = 0;
542 else
543 errlog(WARNING, "SMTP relay will not send message: %b\n", line);
544 end:
545 buf_free(line);
546 buf_free(field);
547 buf_free(content);
548 buf_free(rcpt);
549 #endif /* USE_SOCK */
550 return (ret);
551 }
552
553 static SOCKET sendmail_state = INVALID_SOCKET;
554
555 void sendmail_begin(void)
556 {
557 /* begin mail sending session */
558 if (sendmail_state == INVALID_SOCKET)
559 sendmail_state = smtp_open();
560 }
561 void sendmail_end(void)
562 {
563 /* end mail sending session */
564 if (sendmail_state != INVALID_SOCKET) {
565 smtp_close(sendmail_state);
566 sendmail_state = INVALID_SOCKET;
567 }
568 }
569
570 int smtpsend(BUFFER *head, BUFFER *message, char *from)
571 {
572 SOCKET s;
573 int ret = -1;
574
575 if (sendmail_state != INVALID_SOCKET)
576 ret = smtp_send(sendmail_state, head, message, from);
577 else {
578 s = smtp_open();
579 if (s != INVALID_SOCKET) {
580 ret = smtp_send(s, head, message, from);
581 smtp_close(s);
582 }
583 }
584 return (ret);
585 }
586
587 /* retrieve mail with POP3 **********************************************/
588 #ifdef USE_SOCK
589 int pop3_close(SOCKET s);
590
591 #define POP3_ANY 0
592 #define POP3_APOP 1
593 #define POP3_PASS 2
594
595 SOCKET pop3_open(char *user, char *host, char *pass, int auth)
596 {
597 SOCKET server = INVALID_SOCKET;
598 BUFFER *line;
599 int authenticated = 0;
600 char md[33];
601 int c;
602
603 line = buf_new();
604 server = opensocket(host, 110);
605 if (server == INVALID_SOCKET)
606 errlog(NOTICE, "Can't connect to POP3 server %s.\n", host);
607 else {
608 sock_getline(server, line);
609 if (!bufleft(line, "+")) {
610 errlog(WARNING, "No POP3 service at %s.\n", host);
611 closesocket(server);
612 server = INVALID_SOCKET;
613 }
614 }
615 if (server != INVALID_SOCKET) {
616 errlog(DEBUGINFO, "Opening POP3 connection to %s.\n", host);
617 do
618 c = buf_getc(line);
619 while (c != '<' && c != -1);
620 while (c != '>' && c != -1) {
621 buf_appendc(line, c);
622 c = buf_getc(line);
623 }
624 if (c == '>' && (auth == POP3_ANY || auth == POP3_APOP)) {
625 buf_appendc(line, c);
626 buf_appends(line, pass);
627 digest_md5(line, line);
628 id_encode(line->data, md);
629 buf_setf(line, "APOP %s %s\r\n", user, md);
630 sock_cat(server, line);
631 sock_getline(server, line);
632 if (bufleft(line, "+"))
633 authenticated = 1;
634 else {
635 errlog(auth == POP3_APOP ? ERRORMSG : NOTICE,
636 "POP3 APOP auth at %s failed: %b\n", host, line);
637 buf_sets(line, "QUIT\r\n");
638 sock_cat(server, line);
639 closesocket(server);
640 server = pop3_open(user, host, pass, POP3_PASS);
641 goto end;
642 }
643 }
644 if (!authenticated) {
645 buf_setf(line, "USER %s\r\n", user);
646 sock_cat(server, line);
647 sock_getline(server, line);
648 if (!bufleft(line, "+"))
649 errlog(ERRORMSG, "POP3 USER command at %s failed: %b\n", host, line);
650 else {
651 buf_setf(line, "PASS %s\r\n", pass);
652 sock_cat(server, line);
653 sock_getline(server, line);
654 if (bufleft(line, "+"))
655 authenticated = 1;
656 else
657 errlog(ERRORMSG, "POP3 auth at %s failed: %b\n", host, line);
658 }
659 }
660 if (!authenticated) {
661 pop3_close(server);
662 closesocket(server);
663 server = INVALID_SOCKET;
664 }
665 }
666 end:
667 buf_free(line);
668 return (server);
669 }
670
671 int pop3_close(SOCKET s)
672 {
673 BUFFER *line;
674 int ret = -1;
675
676 line = buf_new();
677 buf_sets(line, "QUIT\r\n");
678 sock_cat(s, line);
679 sock_getline(s, line);
680 if (bufleft(line, "+")) {
681 ret = 0;
682 errlog(DEBUGINFO, "Closing POP3 connection.\n");
683 } else
684 errlog(ERRORMSG, "POP3 QUIT failed:\n", line->data);
685 buf_free(line);
686 return (ret);
687 }
688
689 int pop3_stat(SOCKET s)
690 {
691 BUFFER *line;
692 int val = -1;
693
694 line = buf_new();
695 buf_sets(line, "STAT\r\n");
696 sock_cat(s, line);
697 sock_getline(s, line);
698 if (bufleft(line, "+"))
699 sscanf(line->data, "+%*s %d", &val);
700 buf_free(line);
701 return (val);
702 }
703
704 int pop3_list(SOCKET s, int n)
705 {
706 BUFFER *line;
707 int val = -1;
708
709 line = buf_new();
710 buf_setf(line, "LIST %d\r\n", n);
711 sock_cat(s, line);
712 sock_getline(s, line);
713 if (bufleft(line, "+"))
714 sscanf(line->data, "+%*s %d", &val);
715 buf_free(line);
716 return (val);
717 }
718
719 int pop3_dele(SOCKET s, int n)
720 {
721 BUFFER *line;
722 int ret = 0;
723
724 line = buf_new();
725 buf_setf(line, "DELE %d\r\n", n);
726 sock_cat(s, line);
727 sock_getline(s, line);
728 if (!bufleft(line, "+"))
729 ret = -1;
730 buf_free(line);
731 return (ret);
732 }
733
734 int pop3_retr(SOCKET s, int n, BUFFER *msg)
735 {
736 BUFFER *line;
737 int ret = -1;
738
739 line = buf_new();
740 buf_clear(msg);
741 buf_setf(line, "RETR %d\r\n", n);
742 sock_cat(s, line);
743 sock_getline(s, line);
744 if (bufleft(line, "+")) {
745 for (;;) {
746 if (sock_getline(s, line) == -1)
747 break;
748 if (bufeq(line, ".")) {
749 ret = 0;
750 break;
751 } else if (bufleft(line, ".")) {
752 buf_append(msg, line->data + 1, line->length - 1);
753 } else
754 buf_cat(msg, line);
755 buf_nl(msg);
756 }
757 }
758 buf_free(line);
759 return (ret);
760 }
761
762 void pop3get(void)
763 {
764 FILE *f;
765 char cfg[LINELEN], user[LINELEN], host[LINELEN], pass[LINELEN], auth[5];
766 SOCKET server;
767 BUFFER *line, *msg;
768 int i = 0, num = 0;
769
770 line = buf_new();
771 msg = buf_new();
772 f = mix_openfile(POP3CONF, "r");
773 if (f != NULL)
774 while (fgets(cfg, sizeof(cfg), f) != NULL) {
775 if (cfg[0] == '#')
776 continue;
777 if (strchr(cfg, '@'))
778 strchr(cfg, '@')[0] = ' ';
779 if (sscanf(cfg, "%127s %127s %127s %4s", user, host, pass, auth) < 3)
780 continue;
781 i = POP3_ANY;
782 if (strileft(auth, "apop"))
783 i = POP3_APOP;
784 if (strileft(auth, "pass"))
785 i = POP3_PASS;
786 server = pop3_open(user, host, pass, i);
787 if (server != INVALID_SOCKET) {
788 num = pop3_stat(server);
789 if (num < 0)
790 errlog(WARNING, "POP3 protocol error at %s.\n", host);
791 else if (num == 0)
792 errlog(DEBUGINFO, "No mail at %s.\n", host);
793 else
794 for (i = 1; i <= num; i++) {
795 if (POP3SIZELIMIT > 0 &&
796 pop3_list(server, i) > POP3SIZELIMIT * 1024) {
797 errlog(WARNING, "Over size message on %s.", host);
798 if (POP3DEL == 1)
799 pop3_dele(server, i);
800 } else {
801 if (pop3_retr(server, i, msg) == 0 &&
802 pool_add(msg, "inf") == 0)
803 pop3_dele(server, i);
804 else {
805 errlog(WARNING, "POP3 error while getting mail from %s.",
806 host);
807 closesocket(server);
808 goto end;
809 }
810 }
811 }
812 pop3_close(server);
813 closesocket(server);
814 }
815 }
816 end:
817 if (f != NULL)
818 fclose(f);
819 buf_free(line);
820 buf_free(msg);
821 }
822 #endif /* USE_SOCK */

  ViewVC Help
Powered by ViewVC 1.1.5