-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync.c
1622 lines (1401 loc) · 35.5 KB
/
sync.c
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: sync.c,v 1.86 2009/05/23 22:50:53 manu Exp $ */
/*
* Copyright (c) 2004-2007 Emmanuel Dreyfus
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Emmanuel Dreyfus
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$Id: sync.c,v 1.86 2009/05/23 22:50:53 manu Exp $");
#endif
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <pthread.h>
#include <syslog.h>
#include <sysexits.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "pending.h"
#include "sync.h"
#include "conf.h"
#include "milter-greylist.h"
#ifdef USE_DMALLOC
#include <dmalloc.h>
#endif
#define SYNC_PROTO_CURRENT 3
struct sync_master_sock {
int runs;
int sock;
};
#define SMS_RUNNING 1
#define SMS_NOTRUNNING 0
#define SMS_DISABLED -1
/* errors returned by stdio that should not cause an exit */
static int sync_ignored_errno[] = {
#ifdef EAGAIN
EAGAIN,
#endif /* EAGAIN */
#ifdef ECONNABORTED
ECONNABORTED,
#endif /* ECONNABORTED */
#ifdef EMFILE
EMFILE,
#endif /* EMFILE */
#ifdef ENFILE
ENFILE,
#endif /* ENFILE */
#ifdef ENOBUFS
ENOBUFS,
#endif /* ENOBUFS */
#ifdef ENOMEM
ENOMEM,
#endif /* ENOMEM */
#ifdef ENOSR
ENOSR,
#endif /* ENOSR */
#ifdef EWOULDBLOCK
EWOULDBLOCK,
#endif /* EWOULDBLOCK */
0,
};
static pthread_mutex_t sync_master_lock = PTHREAD_MUTEX_INITIALIZER;
struct sync_master_sock sync_master4 = { 0, -1 };
struct sync_master_sock sync_master6 = { 0, -1 };
struct peerlist peer_head;
pthread_rwlock_t peer_lock; /* For the peer list */
static pthread_mutex_t sync_dirty_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t sync_sleepflag = PTHREAD_COND_INITIALIZER;
static int sync_dirty = 0;
static void sync_listen(char *, char *, struct sync_master_sock *);
static int local_addr(const struct sockaddr *sa, const socklen_t salen);
static int sync_queue_poke(struct peer *, struct sync *);
static struct sync * sync_queue_peek(struct peer *);
static int select_protocol(struct peer *, int, FILE *);
static void sync_vers(FILE *, int);
static int is_fatal(int);
void
peer_init(void) {
int error;
LIST_INIT(&peer_head);
if ((error = pthread_rwlock_init(&peer_lock, NULL)) != 0) {
mg_log(LOG_ERR,
"pthread_rwlock_init failed: %s", strerror(error));
exit(EX_OSERR);
}
return;
}
void
peer_clear(void) {
struct peer *peer;
struct sync *sync;
PEER_WRLOCK;
while(!LIST_EMPTY(&peer_head)) {
peer = LIST_FIRST(&peer_head);
while((sync = sync_queue_peek(peer)) != NULL)
sync_free(sync);
if (peer->p_stream != NULL)
Fclose(peer->p_stream);
LIST_REMOVE(peer, p_list);
pthread_mutex_destroy(&peer->p_mtx);
free(peer->p_name);
free(peer);
}
PEER_UNLOCK;
return;
}
void
peer_add(peername, sockettimeout)
char *peername;
time_t sockettimeout;
{
struct peer *peer;
if ((peer = malloc(sizeof(*peer))) == NULL ||
(peer->p_name = strdup(peername)) == NULL) {
mg_log(LOG_ERR, "cannot add peer: %s", strerror(errno));
exit(EX_OSERR);
}
peer->p_qlen = 0;
peer->p_stream = NULL;
peer->p_flags = 0;
peer->p_socket_timeout = sockettimeout;
TAILQ_INIT(&peer->p_deferred);
pthread_mutex_init(&peer->p_mtx, NULL);
PEER_WRLOCK;
LIST_INSERT_HEAD(&peer_head, peer, p_list);
PEER_UNLOCK;
if (conf.c_debug)
mg_log(LOG_DEBUG, "load peer %s", peer->p_name);
return;
}
void
peer_create(pending)
struct pending *pending;
{
struct peer *peer;
PEER_RDLOCK;
if (LIST_EMPTY(&peer_head))
goto out;
LIST_FOREACH(peer, &peer_head, p_list)
sync_queue(peer, PS_CREATE, pending, -1); /* -1: unused */
out:
PEER_UNLOCK;
return;
}
void
peer_delete(pending, autowhite)
struct pending *pending;
time_t autowhite;
{
struct peer *peer;
PEER_RDLOCK;
if (LIST_EMPTY(&peer_head))
goto out;
LIST_FOREACH(peer, &peer_head, p_list)
sync_queue(peer, PS_DELETE, pending, autowhite);
out:
PEER_UNLOCK;
return;
}
static int
sync_queue_poke(peer, sync)
struct peer *peer;
struct sync *sync;
{
int r = 0;
pthread_mutex_lock(&peer->p_mtx);
if (peer->p_qlen < conf.c_syncmaxqlen) {
TAILQ_INSERT_HEAD(&peer->p_deferred, sync, s_list);
peer->p_qlen++;
r = 1;
}
pthread_mutex_unlock(&peer->p_mtx);
return r;
}
static struct sync *
sync_queue_peek(peer)
struct peer *peer;
{
struct sync *sync;
pthread_mutex_lock(&peer->p_mtx);
sync = TAILQ_FIRST(&peer->p_deferred);
if (!TAILQ_EMPTY(&peer->p_deferred)) {
TAILQ_REMOVE(&peer->p_deferred, sync, s_list);
peer->p_qlen--;
}
pthread_mutex_unlock(&peer->p_mtx);
return sync;
}
int
sync_send(peer, type, pending, autowhite) /* peer list is read-locked */
struct peer *peer;
peer_sync_t type;
struct pending *pending;
time_t autowhite;
{
char sep[] = " \n\t\r";
char *replystr;
int replycode;
char line[LINELEN + 1];
char *cookie = NULL;
char *keyw;
char awstr[LINELEN + 1];
int bw;
int errcount = 0;
if ((peer->p_stream == NULL) && (peer_connect(peer) != 0))
return -1;
*line = '\0';
switch(type) {
case PS_FLUSH:
bw = snprintf(line, LINELEN, "flush addr %s\r\n",
pending->p_addr);
break;
case PS_CREATE:
bw = snprintf(line, LINELEN, "add addr %s from %s "
"rcpt %s date %ld\r\n", pending->p_addr,
pending->p_from, pending->p_rcpt,
(long)pending->p_tv.tv_sec);
break;
default:
if (peer->p_vers >= 2) {
keyw = "del2";
snprintf(awstr, LINELEN, " aw %ld", (long)autowhite);
} else {
keyw = "del";
awstr[0] = '\0';
}
bw = snprintf(line, LINELEN, "%s addr %s from %s "
"rcpt %s date %ld%s\r\n", keyw, pending->p_addr,
pending->p_from, pending->p_rcpt,
(long)pending->p_tv.tv_sec, awstr);
break;
}
if (bw > LINELEN) {
mg_log(LOG_ERR, "closing connection with peer %s: "
"send buffer would overflow (%d entries queued)",
peer->p_name, peer->p_qlen);
Fclose(peer->p_stream);
peer->p_stream = NULL;
return -1;
}
bw = fprintf(peer->p_stream, "%s", line);
if (bw != strlen(line)) {
mg_log(LOG_ERR, "closing connection with peer %s: "
"%s (%d entries queued) - I was unable to send "
"complete line \"%s\" - bytes written: %i",
peer->p_name, strerror(errno), peer->p_qlen,
line, bw);
Fclose(peer->p_stream);
peer->p_stream = NULL;
return -1;
}
fflush(peer->p_stream);
/*
* Check the return code
*/
get_more:
sync_waitdata(peer->p_socket, peer->p_socket_timeout);
if (fgets(line, LINELEN, peer->p_stream) == NULL) {
if (errno == EAGAIN) {
errcount++;
if ( feof(peer->p_stream) ) {
mg_log(LOG_ERR, "lost connection with peer %s: "
"%s (%d entries queued)",
peer->p_name, strerror(errno), peer->p_qlen);
Fclose(peer->p_stream);
peer->p_stream = NULL;
return -1;
}
if (errcount < 5) {
goto get_more;
} else {
mg_log(LOG_ERR, "too many read error with peer %s", peer->p_name);
}
}
mg_log(LOG_ERR, "lost connection with peer %s: "
"%s (%d entries queued)",
peer->p_name, strerror(errno), peer->p_qlen);
Fclose(peer->p_stream);
peer->p_stream = NULL;
return -1;
}
/*
* On some systems, opening a stream on a socket introduce
* weird behavior: the in and out buffers get mixed up.
* By calling fflush() after each read operation, we fix that
*/
fflush(peer->p_stream);
if ((replystr = strtok_r(line, sep, &cookie)) == NULL) {
mg_log(LOG_ERR, "Unexpected reply \"%s\" from %s, "
"closing connection (%d entries queued)",
line, peer->p_name, peer->p_qlen);
Fclose(peer->p_stream);
peer->p_stream = NULL;
return -1;
}
replycode = atoi(replystr);
if (replycode != 201) {
mg_log(LOG_ERR, "Unexpected reply \"%s\" from %s, "
"closing connection (%d entries queued)",
line, peer->p_name, peer->p_qlen);
Fclose(peer->p_stream);
peer->p_stream = NULL;
return -1;
}
if (conf.c_debug)
mg_log(LOG_DEBUG, "sync one entry with %s", peer->p_name);
return 0;
}
int
peer_connect(peer) /* peer list is read-locked */
struct peer *peer;
{
struct servent *se;
#ifdef HAVE_GETADDRINFO
struct addrinfo hints, *res0, *res;
int err;
#else
struct protoent *pe;
int proto;
sockaddr_t raddr;
socklen_t raddrlen;
#endif
sockaddr_t laddr;
socklen_t laddrlen;
char *laddrstr;
int service;
int s = -1;
char *replystr;
int replycode;
FILE *stream;
char sep[] = " \n\t\r";
char line[LINELEN + 1];
int param;
char *cookie = NULL;
if (peer->p_stream != NULL)
mg_log(LOG_ERR, "peer_connect called and peer->p_stream != 0");
if (conf.c_syncport != NULL) {
service = htons(atoi(conf.c_syncport));
} else {
if ((se = getservbyname(MXGLSYNC_NAME, "tcp")) == NULL)
service = htons(atoi(MXGLSYNC_PORT));
else
service = se->s_port;
}
#ifdef HAVE_GETADDRINFO
bzero(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((err = getaddrinfo(peer->p_name, "0", &hints, &res0)) != 0) {
mg_log(LOG_ERR, "cannot sync with peer %s, "
"getaddrinfo failed: %s (%d entries queued)",
peer->p_name, gai_strerror(err), peer->p_qlen);
return -1;
}
for (res = res0; res; res = res->ai_next) {
/*We only test an address family which kernel supports. */
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s == -1)
continue;
close(s);
if (local_addr(res->ai_addr, res->ai_addrlen)) {
peer->p_flags |= P_LOCAL;
freeaddrinfo(res0);
return -1;
}
}
for (res = res0; res; res = res->ai_next) {
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s == -1)
continue;
switch (res->ai_family) {
case AF_INET:
SA4(res->ai_addr)->sin_port = service;
if (conf.c_syncsrcaddr != NULL) {
laddrstr = conf.c_syncsrcaddr;
} else {
laddrstr = "0.0.0.0";
}
break;
#ifdef AF_INET6
case AF_INET6:
SA6(res->ai_addr)->sin6_port = service;
laddrstr = "::";
break;
#endif
default:
mg_log(LOG_ERR, "cannot sync, unknown address family");
close(s);
s = -1;
continue;
}
laddrlen = sizeof(laddr);
if (ipfromstring(laddrstr, SA(&laddr), &laddrlen,
res->ai_family) == 1 &&
bind(s, SA(&laddr), laddrlen) == 0 &&
connect(s, res->ai_addr, res->ai_addrlen) == 0)
break;
close(s);
s = -1;
}
freeaddrinfo(res0);
if (s < 0) {
mg_log(LOG_ERR,
"cannot sync with peer %s: %s (%d entries queued)",
peer->p_name, strerror(errno), peer->p_qlen);
return -1;
}
#else
raddrlen = sizeof(raddr);
if (ipfromstring(peer->p_name, SA(&raddr), &raddrlen,
AF_UNSPEC) != 1) {
mg_log(LOG_ERR, "cannot sync, invalid address");
return -1;
}
if (local_addr(SA(&raddr), raddrlen)) {
peer->p_flags |= P_LOCAL;
return -1;
}
switch (SA(&raddr)->sa_family) {
case AF_INET:
SA4(&raddr)->sin_port = service;
if (conf.c_syncsrcaddr != NULL) {
laddrstr = conf.c_syncsrcaddr;
} else {
laddrstr = "0.0.0.0";
}
break;
#ifdef AF_INET6
case AF_INET6:
SA6(&raddr)->sin6_port = service;
laddrstr = "::";
break;
#endif
default:
mg_log(LOG_ERR, "cannot sync, unknown address family");
return -1;
}
if ((pe = getprotobyname("tcp")) == NULL)
proto = 6;
else
proto = pe->p_proto;
if ((s = socket(SA(&raddr)->sa_family, SOCK_STREAM, proto)) == -1) {
mg_log(LOG_ERR, "cannot sync with peer %s, "
"socket failed: %s (%d entries queued)",
peer->p_name, strerror(errno), peer->p_qlen);
return -1;
}
laddrlen = sizeof(laddr);
if (ipfromstring(laddrstr, SA(&laddr), &laddrlen,
SA(&raddr)->sa_family) != 1) {
mg_log(LOG_ERR, "cannot sync, invalid address");
close(s);
return -1;
}
if (bind(s, SA(&laddr), laddrlen) != 0) {
mg_log(LOG_ERR, "cannot sync with peer %s, "
"bind failed: %s (%d entries queued)",
peer->p_name, strerror(errno), peer->p_qlen);
close(s);
return -1;
}
if (connect(s, SA(&raddr), raddrlen) != 0) {
mg_log(LOG_ERR, "cannot sync with peer %s, "
"connect failed: %s (%d entries queued)",
peer->p_name, strerror(errno), peer->p_qlen);
close(s);
return -1;
}
#endif
param = O_NONBLOCK;
if (fcntl(s, F_SETFL, param) != 0) {
mg_log(LOG_ERR, "cannot set non blocking I/O with %s: %s",
peer->p_name, strerror(errno));
}
errno = 0;
if ((stream = Fdopen(s, "w+")) == NULL) {
mg_log(LOG_ERR, "cannot sync with peer %s, "
"fdopen failed: %s (%d entries queued)",
peer->p_name,
(errno == 0) ? "out of stdio streams" : strerror(errno),
peer->p_qlen);
close(s);
return -1;
}
#ifdef USE_FD_POOL
s = fileno(stream); /* the socket descriptor could have been replaced by Fdopen() ! */
#endif
if (setvbuf(stream, NULL, _IOLBF, 0) != 0)
mg_log(LOG_ERR, "cannot set line buffering with peer %s: %s",
peer->p_name, strerror(errno));
sync_waitdata(s, peer->p_socket_timeout);
if (fgets(line, LINELEN, stream) == NULL) {
mg_log(LOG_ERR, "Lost connection with peer %s: "
"%s (%d entries queued)",
peer->p_name, strerror(errno), peer->p_qlen);
goto bad;
}
/*
* On some systems, opening a stream on a socket introduce
* weird behavior: the in and out buffers get mixed up.
* By calling fflush() after each read operation, we fix that
*/
fflush(stream);
if ((replystr = strtok_r(line, sep, &cookie)) == NULL) {
mg_log(LOG_ERR, "Unexpected reply \"%s\" from peer %s "
"closing connection (%d entries queued)",
line, peer->p_name, peer->p_qlen);
goto bad;
}
replycode = atoi(replystr);
if (replycode != 200) {
mg_log(LOG_ERR, "Unexpected reply \"%s\" from peer %s "
"closing connection (%d entries queued)",
line, peer->p_name, peer->p_qlen);
goto bad;
}
if ((peer->p_vers = select_protocol(peer, s, stream)) == 0)
goto bad;
mg_log(LOG_INFO, "Connection to %s established, protocol version %d",
peer->p_name, peer->p_vers);
peer->p_stream = stream;
peer->p_socket = s;
return 0;
bad:
Fclose(stream);
peer->p_stream = NULL;
return -1;
}
void
sync_master_stop(void) {
if (sync_master4.sock != 1) {
close(sync_master4.sock);
sync_master4.sock = -1;
}
if (sync_master6.sock != 1) {
close(sync_master6.sock);
sync_master6.sock = -1;
}
}
void
sync_master_restart(void) {
pthread_t tid;
int empty;
int error;
int sync_master6_runs = SMS_NOTRUNNING;
int sync_master4_runs = SMS_NOTRUNNING;
PEER_RDLOCK;
empty = LIST_EMPTY(&peer_head);
PEER_UNLOCK;
pthread_mutex_lock(&sync_master_lock);
if (empty || (sync_master4.runs && sync_master6.runs))
goto last;
if (conf.c_syncaddr != NULL) {
if (strchr(conf.c_syncaddr, ':')) {
if (sync_master6.runs != SMS_RUNNING) {
sync_listen(conf.c_syncaddr, conf.c_syncport,
&sync_master6);
sync_master6_runs = sync_master6.runs;
}
sync_master4.runs = SMS_DISABLED;
} else {
if (sync_master4.runs != SMS_RUNNING) {
sync_listen(conf.c_syncaddr, conf.c_syncport,
&sync_master4);
sync_master4_runs = sync_master4.runs;
}
sync_master6.runs = SMS_DISABLED;
}
} else {
#ifdef AF_INET6
if (sync_master6.runs != SMS_RUNNING) {
sync_listen("::", conf.c_syncport, &sync_master6);
sync_master6_runs = sync_master6.runs;
}
#endif
if (sync_master4.runs != SMS_RUNNING) {
sync_listen("0.0.0.0", conf.c_syncport, &sync_master4);
sync_master4_runs = sync_master4.runs;
}
}
if (sync_master4.runs != SMS_RUNNING &&
sync_master6.runs != SMS_RUNNING) {
mg_log(LOG_ERR, "cannot start MX sync, socket failed: %s",
strerror(errno));
exit(EX_OSERR);
}
if (sync_master6_runs == SMS_RUNNING) {
if ((error = pthread_create(&tid, NULL, sync_master,
(void *)&sync_master6)) != SMS_NOTRUNNING) {
mg_log(LOG_ERR,
"Cannot run MX sync thread for IPv6: %s",
strerror(error));
exit(EX_OSERR);
}
if ((error = pthread_detach(tid)) != 0) {
mg_log(LOG_ERR,
"pthread_detach failed for IPv6 MX sync: %s",
strerror(error));
exit(EX_OSERR);
}
}
if (sync_master4_runs == SMS_RUNNING) {
if ((error = pthread_create(&tid, NULL, sync_master,
(void *)&sync_master4)) != 0) {
mg_log(LOG_ERR,
"Cannot run MX sync thread for IPv4: %s",
strerror(error));
exit(EX_OSERR);
}
if ((error = pthread_detach(tid)) != 0) {
mg_log(LOG_ERR,
"pthread_detach failed for IPv4 MX sync: %s",
strerror(error));
exit(EX_OSERR);
}
}
last:
pthread_mutex_unlock(&sync_master_lock);
}
void *
sync_master(arg)
void *arg;
{
struct sync_master_sock *sms = arg;
conf_retain();
for (;;) {
sockaddr_t raddr;
socklen_t raddrlen;
int fd;
FILE *stream;
pthread_t tid;
struct peer *peer;
char peerstr[IPADDRSTRLEN];
int error;
int sock;
pthread_mutex_lock(&sync_master_lock);
sock = sms->sock;
pthread_mutex_unlock(&sync_master_lock);
/* TODO: accept connections in nonblocking mode
* in order to watch conf change */
bzero((void *)&raddr, sizeof(raddr));
raddrlen = sizeof(raddr);
if ((fd = accept(sock, SA(&raddr), &raddrlen)) == -1) {
mg_log(LOG_ERR, "incoming connection "
"failed: %s", strerror(errno));
if (is_fatal(errno))
exit(EX_OSERR);
}
unmappedaddr(SA(&raddr), &raddrlen);
conf_release();
conf_retain();
iptostring(SA(&raddr), raddrlen, peerstr, sizeof(peerstr));
mg_log(LOG_INFO, "Incoming MX sync connection from %s",
peerstr);
errno = 0;
if ((stream = Fdopen(fd, "w+")) == NULL) {
mg_log(LOG_ERR,
"incoming connection from %s failed, "
"fdopen fail: %s", peerstr,
(errno == 0) ? "out of stdio streams"
: strerror(errno));
close(fd);
exit(EX_OSERR);
}
if (setvbuf(stream, NULL, _IOLBF, 0) != 0)
mg_log(LOG_ERR, "cannot set line buffering: %s",
strerror(errno));
/*
* Check that the orginator IP is one of our peers
*/
PEER_RDLOCK;
if (LIST_EMPTY(&peer_head)) {
fprintf(stream, "105 No more peers, shutting down!\n");
PEER_UNLOCK;
Fclose(stream);
pthread_mutex_lock(&sync_master_lock);
close(sms->sock);
sms->sock = -1;
sms->runs = SMS_NOTRUNNING;
pthread_mutex_unlock(&sync_master_lock);
conf_release();
return NULL;
}
LIST_FOREACH(peer, &peer_head, p_list) {
#ifdef HAVE_GETADDRINFO
struct addrinfo hints, *res0, *res;
int err;
int match = 0;
bzero(&hints, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
err = getaddrinfo(peer->p_name, "0", &hints, &res0);
if (err != 0) {
mg_log(LOG_ERR, "cannot resolve %s: %s",
peer->p_name, gai_strerror(err));
continue;
}
for (res = res0; res; res = res->ai_next) {
if (ip_equal(SA(&raddr), res->ai_addr)) {
match = 1;
break;
}
}
freeaddrinfo(res0);
if (match)
break;
#else
sockaddr_t addr;
socklen_t addrlen;
addrlen = sizeof(addr);
if (ipfromstring(peer->p_name, SA(&addr), &addrlen,
AF_UNSPEC) != 1) {
mg_log(LOG_ERR, "cannot resolve %s",
peer->p_name);
continue;
}
if (ip_equal(SA(&raddr), SA(&addr)))
break;
#endif
}
PEER_UNLOCK;
if (peer == NULL) {
mg_log(LOG_INFO, "Remote host %s is not a peer MX",
peerstr);
fprintf(stream,
"106 You have no permission to talk, go away!\n");
Fclose(stream);
continue;
}
if ((error = pthread_create(&tid, NULL,
(void *(*)(void *))sync_server, (void *)stream)) != 0) {
mg_log(LOG_ERR, "incoming connection from %s failed, "
"pthread_create failed: %s",
peerstr, strerror(error));
Fclose(stream);
continue;
}
if ((error = pthread_detach(tid)) != 0) {
mg_log(LOG_ERR, "incoming connection from %s failed, "
"pthread_detach failed: %s",
peerstr, strerror(error));
exit(EX_OSERR);
}
}
/* NOTREACHED */
mg_log(LOG_ERR, "sync_master quitted unexpectedly");
return NULL;
}
/* sync_master_lock must be locked */
static void
sync_listen(addr, port, sms)
char *addr, *port;
struct sync_master_sock *sms;
{
struct protoent *pe;
struct servent *se;
int proto;
sockaddr_t laddr;
socklen_t laddrlen;
int service;
int optval;
int s;
sms->runs = SMS_RUNNING;
laddrlen = sizeof(laddr);
if (ipfromstring(addr, SA(&laddr), &laddrlen, AF_UNSPEC) != 1) {
sms->runs = SMS_DISABLED;
return;
}
if ((pe = getprotobyname("tcp")) == NULL)
proto = 6;
else
proto = pe->p_proto;
if (port != NULL)
service = htons(atoi(port));
else {
if ((se = getservbyname(MXGLSYNC_NAME, "tcp")) == NULL)
service = htons(atoi(MXGLSYNC_PORT));
else
service = se->s_port;
}
switch (SA(&laddr)->sa_family) {
case AF_INET:
SA4(&laddr)->sin_port = service;
break;
#ifdef AF_INET6
case AF_INET6:
SA6(&laddr)->sin6_port = service;
break;
#endif
default:
sms->runs = SMS_DISABLED;
return;
}
if ((s = socket(SA(&laddr)->sa_family, SOCK_STREAM, proto)) == -1) {
sms->runs = SMS_DISABLED;
return;
}
optval = 1;
if ((setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
&optval, sizeof(optval))) != 0) {
mg_log(LOG_ERR, "cannot set SO_REUSEADDR: %s",
strerror(errno));
}
optval = 1;
if ((setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
&optval, sizeof(optval))) != 0) {
mg_log(LOG_ERR, "cannot set SO_KEEPALIVE: %s",
strerror(errno));
}
#ifdef IPV6_V6ONLY
if (SA(&laddr)->sa_family == AF_INET6) {
optval = 1;
if ((setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
&optval, sizeof(optval))) != 0) {
mg_log(LOG_ERR, "cannot set IPV6_V6ONLY: %s",
strerror(errno));
}
}
#endif
if (bind(s, SA(&laddr), laddrlen) != 0) {
mg_log(LOG_ERR, "cannot start MX sync, bind failed: %s",
strerror(errno));
sms->runs = SMS_DISABLED;
close(s);
return;
}
if (listen(s, MXGLSYNC_BACKLOG) != 0) {
mg_log(LOG_ERR, "cannot start MX sync, listen failed: %s",
strerror(errno));
sms->runs = SMS_DISABLED;
close(s);
return;
}
sms->sock = s;
return;
}
void
sync_server(arg)
void *arg;
{