/
main.c
1522 lines (1305 loc) · 43.7 KB
/
main.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
/*
clsync - file tree sync utility based on fanotify and inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include <pwd.h> // For getpwnam()
#include <grp.h> // For getgrnam()
#include "output.h"
#include "sync.h"
#include "malloc.h"
#include "cluster.h"
#include "fileutils.h"
#include "socket.h"
//#include "revision.h"
static const struct option long_options[] =
{
{"watch-dir", required_argument, NULL, WATCHDIR},
{"sync-handler", required_argument, NULL, SYNCHANDLER},
{"rules-file", required_argument, NULL, RULESFILE},
{"destination-dir", required_argument, NULL, DESTDIR},
{"mode", required_argument, NULL, MODE},
{"socket", required_argument, NULL, SOCKETPATH},
{"socket-auth", required_argument, NULL, SOCKETAUTH},
{"socket-mod", required_argument, NULL, SOCKETMOD},
{"socket-own", required_argument, NULL, SOCKETOWN},
{"status-file", required_argument, NULL, STATUSFILE},
{"background", optional_argument, NULL, BACKGROUND},
{"config-file", required_argument, NULL, CONFIGFILE},
{"config-block", required_argument, NULL, CONFIGBLOCK},
{"pid-file", required_argument, NULL, PIDFILE},
{"uid", required_argument, NULL, UID},
{"gid", required_argument, NULL, GID},
#ifdef HAVE_CAPABILITIES
{"preserve-file-access",optional_argument, NULL, CAP_PRESERVE_FILEACCESS},
#endif
{"pthread", optional_argument, NULL, PTHREAD},
{"retries", optional_argument, NULL, RETRIES},
{"syslog", optional_argument, NULL, SYSLOG},
{"one-file-system", optional_argument, NULL, ONEFILESYSTEM},
{"exclude-mount-points",optional_argument, NULL, EXCLUDEMOUNTPOINTS},
#ifdef CLUSTER_SUPPORT
{"cluster-iface", required_argument, NULL, CLUSTERIFACE}, // Not implemented, yet
{"cluster-ip", required_argument, NULL, CLUSTERMCASTIPADDR}, // Not implemented, yet
{"cluster-port", required_argument, NULL, CLUSTERMCASTIPPORT}, // Not implemented, yet
{"cluster-timeout", required_argument, NULL, CLUSTERTIMEOUT}, // Not implemented, yet
{"cluster-node-name", required_argument, NULL, CLUSTERNODENAME}, // Not implemented, yet
{"cluster-hash-dl-min", required_argument, NULL, CLUSTERHDLMIN},
{"cluster-hash-dl-max", required_argument, NULL, CLUSTERHDLMAX},
{"cluster-scan-dl-max", required_argument, NULL, CLUSTERSDLMAX},
#endif
{"max-iterations", required_argument, NULL, MAXITERATIONS},
{"standby-file", required_argument, NULL, STANDBYFILE},
{"timeout-sync", required_argument, NULL, SYNCTIMEOUT},
{"delay-sync", required_argument, NULL, SYNCDELAY},
{"delay-collect", required_argument, NULL, DELAY},
{"delay-collect-bigfile",required_argument, NULL, BFILEDELAY},
{"threshold-bigfile", required_argument, NULL, BFILETHRESHOLD},
{"lists-dir", required_argument, NULL, OUTLISTSDIR},
{"have-recursive-sync", optional_argument, NULL, HAVERECURSIVESYNC},
{"synclist-simplify", optional_argument, NULL, SYNCLISTSIMPLIFY},
{"auto-add-rules-w", optional_argument, NULL, AUTORULESW},
{"rsync-inclimit", required_argument, NULL, RSYNCINCLIMIT},
{"rsync-prefer-include",optional_argument, NULL, RSYNCPREFERINCLUDE},
{"ignore-exitcode", required_argument, NULL, IGNOREEXITCODE},
{"dont-unlink-lists", optional_argument, NULL, DONTUNLINK},
{"full-initialsync", optional_argument, NULL, INITFULL},
{"only-initialsync", optional_argument, NULL, ONLYINITSYNC},
{"skip-initialsync", optional_argument, NULL, SKIPINITSYNC},
{"exit-on-no-events", optional_argument, NULL, EXITONNOEVENTS},
{"exit-hook", required_argument, NULL, EXITHOOK},
{"verbose", optional_argument, NULL, VERBOSE},
{"debug", optional_argument, NULL, DEBUG},
{"quiet", optional_argument, NULL, QUIET},
#ifdef FANOTIFY_SUPPORT
{"fanotify", optional_argument, NULL, FANOTIFY},
{"inotify", optional_argument, NULL, INOTIFY},
#endif
{"label", required_argument, NULL, LABEL},
{"help", optional_argument, NULL, HELP},
{"version", optional_argument, NULL, SHOW_VERSION},
{NULL, 0, NULL, 0}
};
static char *const socketauth[] = {
[SOCKAUTH_UNSET] = "",
[SOCKAUTH_NULL] = "null",
// [SOCKAUTH_PAM] = "pam",
NULL
};
static char *const modes[] = {
[MODE_UNSET] = "",
[MODE_SIMPLE] = "simple",
[MODE_SHELL] = "shell",
[MODE_RSYNCSHELL] = "rsyncshell",
[MODE_RSYNCDIRECT] = "rsyncdirect",
[MODE_RSYNCSO] = "rsyncso",
[MODE_SO] = "so",
NULL
};
static char *const status_descr[] = {
[STATE_EXIT] = "exiting",
[STATE_STARTING] = "starting",
[STATE_RUNNING] = "running",
[STATE_REHASH] = "rehashing",
[STATE_TERM] = "terminating",
[STATE_PTHREAD_GC] = "pthread gc",
[STATE_INITSYNC] = "initsync",
NULL
};
int syntax() {
printf("possible options:\n");
int i=0;
while(long_options[i].name != NULL) {
if(!(long_options[i].val & OPTION_CONFIGONLY))
printf("\t--%-24s%c%c%s\n", long_options[i].name,
long_options[i].val & OPTION_LONGOPTONLY ? ' ' : '-',
long_options[i].val & OPTION_LONGOPTONLY ? ' ' : long_options[i].val,
(long_options[i].has_arg == required_argument ? " argument" : ""));
i++;
}
exit(EINVAL);
}
int version() {
printf(PROGRAM" v%i.%i"REVISION"\n\t"AUTHOR"\n", VERSION_MAJ, VERSION_MIN);
exit(0);
}
int clsyncapi_getapiversion() {
return CLSYNC_API_VERSION;
}
int parse_parameter(options_t *options_p, uint16_t param_id, char *arg, paramsource_t paramsource) {
#ifdef _DEBUG
fprintf(stderr, "Force-Debug: parse_parameter(): %i: %i = \"%s\"\n", paramsource, param_id, arg);
#endif
switch(paramsource) {
case PS_ARGUMENT:
if(param_id & OPTION_CONFIGONLY) {
syntax();
return 0;
}
options_p->flags_set[param_id] = 1;
break;
case PS_CONFIG:
if(options_p->flags_set[param_id])
return 0;
break;
default:
printf_e("Warning: Unknown parameter #%i source (value \"%s\").\n", param_id, arg!=NULL ? arg : "");
break;
}
switch(param_id) {
case '?':
case HELP:
syntax();
break;
case CONFIGFILE:
options_p->config_path = arg;
break;
case CONFIGBLOCK:
options_p->config_block = arg;
break;
case GID:
options_p->gid = (unsigned int)atol(arg);
options_p->flags[param_id]++;
break;
case UID:
options_p->uid = (unsigned int)atol(arg);
options_p->flags[param_id]++;
break;
case PIDFILE:
options_p->pidfile = arg;
break;
case RETRIES:
options_p->retries = (unsigned int)atol(arg);
break;
#ifdef CLUSTER_SUPPORT
case CLUSTERIFACE:
options_p->cluster_iface = arg;
break;
case CLUSTERMCASTIPADDR:
options_p->cluster_mcastipaddr = arg;
break;
case CLUSTERMCASTIPPORT:
options_p->cluster_mcastipport = (uint16_t)atoi(arg);
break;
case CLUSTERTIMEOUT:
options_p->cluster_timeout = (unsigned int)atol(arg);
break;
case CLUSTERNODENAME:
options_p->cluster_nodename = arg;
break;
case CLUSTERHDLMIN:
options_p->cluster_hash_dl_min = (uint16_t)atoi(arg);
break;
case CLUSTERHDLMAX:
options_p->cluster_hash_dl_max = (uint16_t)atoi(arg);
break;
case CLUSTERSDLMAX:
options_p->cluster_scan_dl_max = (uint16_t)atoi(arg);
break;
#endif
case OUTLISTSDIR:
options_p->listoutdir = arg;
break;
case LABEL:
options_p->label = arg;
break;
case STANDBYFILE:
if(strlen(arg)) {
options_p->standbyfile = arg;
options_p->flags[STANDBYFILE] = 1;
} else {
options_p->standbyfile = NULL;
options_p->flags[STANDBYFILE] = 0;
}
break;
case SYNCDELAY:
options_p->syncdelay = (unsigned int)atol(arg);
break;
case DELAY:
options_p->_queues[QUEUE_NORMAL].collectdelay = (unsigned int)atol(arg);
break;
case BFILEDELAY:
options_p->_queues[QUEUE_BIGFILE].collectdelay = (unsigned int)atol(arg);
break;
case BFILETHRESHOLD:
options_p->bfilethreshold = (unsigned long)atol(arg);
break;
#ifdef FANOTIFY_SUPPORT
case FANOTIFY:
options_p->notifyengine = NE_FANOTIFY;
break;
#endif
case INOTIFY:
options_p->notifyengine = NE_INOTIFY;
break;
case RSYNCINCLIMIT:
options_p->rsyncinclimit = (unsigned int)atol(arg);
break;
case SYNCTIMEOUT:
options_p->synctimeout = (unsigned int)atol(arg);
break;
case EXITHOOK:
if(strlen(arg)) {
options_p->exithookfile = arg;
options_p->flags[EXITHOOK] = 1;
} else {
options_p->exithookfile = NULL;
options_p->flags[EXITHOOK] = 0;
}
break;
case IGNOREEXITCODE: {
char *ptr = arg, *start = arg;
unsigned char exitcode;
do {
switch(*ptr) {
case 0:
case ',':
// *ptr=0;
exitcode = (unsigned char)atoi(start);
if(exitcode == 0) {
// flushing the setting
int i = 0;
while(i < 256)
options_p->isignoredexitcode[i++] = 0;
#ifdef _DEBUG
fprintf(stderr, "Force-Debug: parse_parameter(): Reset ignored exitcodes.\n");
#endif
} else {
options_p->isignoredexitcode[exitcode] = 1;
#ifdef _DEBUG
fprintf(stderr, "Force-Debug: parse_parameter(): Adding ignored exitcode %u.\n", exitcode);
#endif
}
start = ptr+1;
break;
}
} while(*(ptr++));
break;
}
case SHOW_VERSION:
version();
break;
case WATCHDIR:
options_p->watchdir = arg;
break;
case SYNCHANDLER:
options_p->handlerfpath = arg;
break;
case RULESFILE:
options_p->rulfpath = arg;
break;
case DESTDIR:
options_p->destdir = arg;
break;
case SOCKETPATH:
options_p->socketpath = arg;
break;
case SOCKETAUTH: {
char *value;
options_p->flags[SOCKETAUTH] = getsubopt(&arg, modes, &value);
if(options_p->flags[SOCKETAUTH] == -1) {
printf_e("Error: Wrong socket auth mech entered: \"%s\"\n", arg);
return EINVAL;
}
}
case SOCKETMOD:
if(!sscanf(arg, "%o", &options_p->socketmod)) {
printf_e("Error: Non octal value passed to --socket-mod: \"%s\"\n", arg);
return EINVAL;
}
options_p->flags[param_id]++;
break;
case SOCKETOWN: {
char *colon = strchr(arg, ':');
uid_t uid;
gid_t gid;
if(colon == NULL) {
struct passwd *pwent = getpwnam(arg);
if(pwent == NULL) {
printf_e("Error: Cannot find username \"%s\" (case #0): %s (errno: %i)\n",
arg, strerror(errno), errno);
return EINVAL;
}
uid = pwent->pw_uid;
gid = pwent->pw_gid;
} else {
char user[USER_LEN+2], group[GROUP_LEN+2];
memcpy(user, arg, MIN(USER_LEN, colon-arg));
user[colon-arg] = 0;
strncpy(group, &colon[1], GROUP_LEN);
errno=0;
struct passwd *pwent = getpwnam(user);
if(pwent == NULL) {
printf_e("Error: Cannot find username \"%s\" (case #1): %s (errno: %i)\n",
user, strerror(errno), errno);
return EINVAL;
}
errno=0;
struct group *grent = getgrnam(group);
if(grent == NULL) {
printf_e("Error: Cannot find group \"%s\": %s (errno: %i)\n",
group, strerror(errno), errno);
return EINVAL;
}
uid = pwent->pw_uid;
gid = grent->gr_gid;
}
options_p->socketuid = uid;
options_p->socketgid = gid;
options_p->flags[param_id]++;
printf_dd("Debug2: socket: uid == %u; gid == %u\n", uid, gid);
break;
}
case STATUSFILE:
options_p->statusfile = arg;
break;
case MODE: {
char *value;
options_p->flags[MODE] = getsubopt(&arg, modes, &value);
if(options_p->flags[MODE] == -1) {
fprintf(stderr, "Error: Wrong mode name entered: \"%s\"\n", arg);
return EINVAL;
}
break;
}
default:
if(arg == NULL)
options_p->flags[param_id]++;
else
options_p->flags[param_id] = atoi(arg);
#ifdef _DEBUG
fprintf(stderr, "Force-Debug: flag %i is set to %i\n", param_id&0xff, options_p->flags[param_id]);
#endif
break;
}
return 0;
}
int arguments_parse(int argc, char *argv[], struct options *options_p) {
int c;
int option_index = 0;
// Generating "optstring" (man 3 getopt_long) with using information from struct array "long_options"
char *optstring = alloca((('z'-'a'+1)*3 + '9'-'0'+1)*3 + 1);
char *optstring_ptr = optstring;
const struct option *lo_ptr = long_options;
while(lo_ptr->name != NULL) {
if(!(lo_ptr->val & (OPTION_CONFIGONLY|OPTION_LONGOPTONLY))) {
*(optstring_ptr++) = lo_ptr->val & 0xff;
if(lo_ptr->has_arg == required_argument)
*(optstring_ptr++) = ':';
if(lo_ptr->has_arg == optional_argument) {
*(optstring_ptr++) = ':';
*(optstring_ptr++) = ':';
}
}
lo_ptr++;
}
*optstring_ptr = 0;
#ifdef _DEBUG
fprintf(stderr, "Force-Debug: %s\n", optstring);
#endif
// Parsing arguments
while(1) {
c = getopt_long(argc, argv, optstring, long_options, &option_index);
if (c == -1) break;
int ret = parse_parameter(options_p, c, optarg, PS_ARGUMENT);
if(ret) return ret;
}
if(optind+1 < argc)
syntax();
/*
if(optind+1 >= argc)
syntax();
options_p->handlerfpath = argv[optind+1];
if(optind+2 < argc) {
options_p->rulfpath = argv[optind+2];
if(!strcmp(options_p->rulfpath, ""))
options_p->rulfpath = NULL;
}
if(optind+3 < argc) {
options_p->destdir = argv[optind+3];
options_p->destdirlen = strlen(options_p->destdir);
}
options_p->watchdir = argv[optind];
options_p->watchdirlen = strlen(options_p->watchdir);*/
/*
if(optind+0 < argc) {
options_p->watchdir = argv[optind];
options_p->watchdirlen = strlen(options_p->watchdir);
} else {
options_p->watchdir = NULL;
options_p->watchdirlen = 0;
}
if(optind+1 < argc) {
options_p->handlerfpath = argv[optind+1];
} else {
options_p->handlerfpath = NULL;
}
if(optind+2 < argc) {
options_p->rulfpath = argv[optind+2];
if(!strcmp(options_p->rulfpath, ""))
options_p->rulfpath = NULL;
} else {
options_p->rulfpath = NULL;
}
if(optind+3 < argc) {
options_p->destdir = argv[optind+3];
options_p->destdirlen = strlen(options_p->destdir);
} else {
options_p->destdir = NULL;
options_p->destdirlen = 0;
}
*/
return 0;
}
char *configs_parse_str[1<<10] = {0};
void gkf_parse(options_t *options_p, GKeyFile *gkf) {
const struct option *lo_ptr = long_options;
while(lo_ptr->name != NULL) {
gchar *value = g_key_file_get_value(gkf, options_p->config_block, lo_ptr->name, NULL);
if(value != NULL) {
unsigned char val_char = lo_ptr->val&0xff;
if(configs_parse_str[val_char])
free(configs_parse_str[val_char]);
configs_parse_str[val_char] = value;
int ret = parse_parameter(options_p, lo_ptr->val, value, PS_CONFIG);
if(ret) exit(ret);
}
lo_ptr++;
}
return;
}
int configs_parse(options_t *options_p) {
GKeyFile *gkf;
gkf = g_key_file_new();
if(options_p->config_path) {
printf_d("Debug: configs_parse(): Trying config-file \"%s\"\n", options_p->config_path);
if(!g_key_file_load_from_file(gkf, options_p->config_path, G_KEY_FILE_NONE, NULL)) {
printf_e("Error: configs_parse(): Cannot open/parse file \"%s\"\n", options_p->config_path);
g_key_file_free(gkf);
return -1;
} else
gkf_parse(options_p, gkf);
} else {
char *config_paths[] = CONFIG_PATHS;
char **config_path_p = config_paths, *config_path_real = xmalloc(PATH_MAX);
size_t config_path_real_size=PATH_MAX;
char *homedir = getenv("HOME");
size_t homedir_len = strlen(homedir);
while(*config_path_p != NULL) {
size_t config_path_len = strlen(*config_path_p);
if(config_path_len+homedir_len+3 > config_path_real_size) {
config_path_real_size = config_path_len+homedir_len+3;
config_path_real = xmalloc(config_path_real_size);
}
if(*config_path_p[0] != '/') {
memcpy(config_path_real, homedir, homedir_len);
config_path_real[homedir_len] = '/';
memcpy(&config_path_real[homedir_len+1], *config_path_p, config_path_len+1);
} else
memcpy(config_path_real, *config_path_p, config_path_len+1);
printf_d("Debug: configs_parse(): Trying config-file \"%s\"\n", config_path_real);
if(!g_key_file_load_from_file(gkf, config_path_real, G_KEY_FILE_NONE, NULL)) {
printf_d("Debug: configs_parse(): Cannot open/parse file \"%s\"\n", config_path_real);
config_path_p++;
continue;
}
gkf_parse(options_p, gkf);
break;
}
free(config_path_real);
}
g_key_file_free(gkf);
return 0;
}
int configs_cleanup() {
int i=0;
while(i < (1<<10)) {
if(configs_parse_str[i] != NULL) {
free(configs_parse_str[i]);
configs_parse_str[i] = NULL;
}
i++;
}
return 0;
}
int rule_complete(rule_t *rule_p, const char *expr) {
printf_ddd("Debug3: rule_complete(): <%s>.\n", expr);
#ifdef VERYPARANOID
if(rule_p->mask == RA_NONE) {
printf_e("Error: rule_complete(): Received a rule with rule_p->mask == 0x00. Exit.\n");
return EINVAL;
}
#endif
char buf[BUFSIZ];
int ret = 0;
if(rule_p->num >= MAXRULES) {
printf_e("Error: Too many rules (%i >= %i).\n", rule_p->num, MAXRULES);
return ENOMEM;
}
if((ret = regcomp(&rule_p->expr, expr, REG_EXTENDED | REG_NOSUB))) {
regerror(ret, &rule_p->expr, buf, BUFSIZ);
printf_e("Error: Invalid regexp pattern <%s>: %s (regex-errno: %i).\n", expr, buf, ret);
return ret;
}
return ret;
}
int parse_rules_fromfile(options_t *options_p) {
int ret = 0;
char *rulfpath = options_p->rulfpath;
rule_t *rules = options_p->rules;
char *line_buf=NULL;
FILE *f = fopen(rulfpath, "r");
if(f == NULL) {
rules->mask = RA_NONE; // Terminator. End of rules' chain.
rules->perm = DEFAULT_RULES_PERM;
printf_e("Error: Cannot open \"%s\" for reading: %s (errno: %i).\n", rulfpath, strerror(errno), errno);
return errno;
}
GHashTable *autowrules_ht = g_hash_table_new_full(g_str_hash, g_str_equal, free, 0);
int i=0;
size_t linelen, size=0;
while((linelen = getline(&line_buf, &size, f)) != -1) {
if(linelen>1) {
uint8_t sign = 0;
char *line = line_buf;
rule_t *rule;
rule = &rules[i];
#ifdef VERYPARANOID
memset(rule, 0, sizeof(*rule));
#endif
rule->num = i++;
line[--linelen] = 0;
// Parsing the first character of the line
switch(*line) {
case '+':
sign = RS_PERMIT;
break;
case '-':
sign = RS_REJECT;
break;
case '#': // Comment?
i--; // Canceling new rule
continue;
default:
printf_e("Error: Wrong rule action <%c>.\n", *line);
return EINVAL;
}
line++;
linelen--;
// Parsing the second character of the line
*line |= 0x20; // lower-casing
// Default rule->mask and rule->perm
// rule->mask - sets bitmask of operations that are affected by the rule
// rule->perm - sets bitmask of permit/reject for every operation. Effect have only bits specified by the rule->mask.
rule->mask = RA_ALL;
switch(sign) {
case RS_REJECT:
rule->perm = RA_NONE;
break;
case RS_PERMIT:
rule->perm = RA_ALL;
break;
}
switch(*line) {
case '*':
rule->objtype = 0; // "0" - means "of any type"
break;
#ifdef DETAILED_FTYPE
case 's':
rule->objtype = S_IFSOCK;
break;
case 'l':
rule->objtype = S_IFLNK;
break;
case 'b':
rule->objtype = S_IFBLK;
break;
case 'c':
rule->objtype = S_IFCHR;
break;
case 'p':
rule->objtype = S_IFIFO;
break;
#endif
case 'f':
rule->objtype = S_IFREG;
break;
case 'd':
rule->objtype = S_IFDIR;
break;
case 'w': // accept or reject walking to directory
if(
(options_p->flags[MODE] == MODE_RSYNCDIRECT) ||
(options_p->flags[MODE] == MODE_RSYNCSHELL) ||
(options_p->flags[MODE] == MODE_RSYNCSO)
) {
printf_e("parse_rules_fromfile(): Warning: Used \"w\" rule in \"--rsync\" case."
" This may cause unexpected problems.\n");
}
rule->objtype = S_IFDIR;
rule->mask = RA_WALK;
break;
default:
printf_e("parse_rules_fromfile(): Warning: Cannot parse the rule <%s>\n", &line[-1]);
i--; // Canceling new rule
continue;
}
line++;
linelen--;
// Parsing the rest part of the line
printf_d("Debug: parse_rules_fromfile(): Rule #%i <%c> <%c> pattern <%s> (length: %i).\n", rule->num, line[-2], line[-1], line, linelen);
if((ret=rule_complete(rule, line)))
goto l_parse_rules_fromfile_end;
// Post-processing:
line--;
linelen++;
if(*line != 'w') {
// processing --auto-add-rules-w
if(options_p->flags[AUTORULESW] && (sign == RS_PERMIT)) {
// Preparing to add appropriate w-rules
char skip = 0;
char *expr = alloca(linelen+2);
memcpy(expr, line, linelen+1);
size_t exprlen = linelen;
// Making expr to be starting with '^'
if(line[1] == '^') {
expr++;
exprlen--;
} else
*expr = '^';
char *end;
if(*line == 'd' || *line == '*') {
// "d" rule already doing what we need, so we can skip the last level
end = &expr[exprlen];
if(end[-1] != '$')
*(end++) = '$';
*end = 0;
// printf_ddd("Debug3: parse_rules_fromfile(): Don't adding w-rule for \"%s\" due to [*d]-rule for \"%s\"\n",
// expr, &line[1]);
g_hash_table_insert(autowrules_ht, strdup(expr), GINT_TO_POINTER(1));
}
if(!skip) {
do {
// Decreasing directory level and make the '$' ending
end = strrchr(expr, '/');
if(end != NULL) {
if(end[-1] != '$')
*(end++) = '$';
*end = 0;
exprlen = (size_t)(end - expr);
} else {
expr[1] = '$';
expr[2] = 0;
exprlen = 2;
}
// Checking if it not already set
if(!g_hash_table_lookup(autowrules_ht, expr)) {
// Switching to next rule:
rule = &rules[i];
rule->num = i++;
// Adding the rule
rule->objtype = S_IFDIR;
rule->mask = RA_WALK;
rule->perm = RA_WALK;
printf_d("Debug: parse_rules_fromfile(): Rule #%i <+> <w> pattern <%s> (length: %i) [auto].\n",
rule->num, expr, exprlen);
if((ret=rule_complete(rule, expr)))
goto l_parse_rules_fromfile_end;
g_hash_table_insert(autowrules_ht, strdup(expr), GINT_TO_POINTER(1));
}
} while(end != NULL);
}
}
}
}
}
l_parse_rules_fromfile_end:
if(size)
free(line_buf);
fclose(f);
printf_ddd("Debug3: parse_rules_fromfile(): Adding tail-rule #%u (effective #%u).\n", -1, i);
rules[i].mask = RA_NONE; // Terminator. End of rules' chain.
rules[i].perm = DEFAULT_RULES_PERM;
g_hash_table_destroy(autowrules_ht);
#ifdef _DEBUG
printf_ddd("Debug3: parse_rules_fromfile(): Total (p == %p):\n", rules);
i=0;
do {
printf_ddd("\t%i\t%i\t%p/%p\n", i, rules[i].objtype, (void *)(long)rules[i].perm, (void *)(long)rules[i].mask);
i++;
} while(rules[i].mask != RA_NONE);
#endif
return ret;
}
int becomedaemon() {
int pid;
signal(SIGPIPE, SIG_IGN);
switch((pid = fork())) {
case -1:
printf_e("Error: Cannot fork(): %s (errno: %i).\n", strerror(errno), errno);
return(errno);
case 0:
setsid();
break;
default:
printf_d("Debug: fork()-ed, pid is %i.\n", pid);
exit(0);
}
return 0;
}
int main_cleanup(options_t *options_p) {
int i=0;
while((i < MAXRULES) && (options_p->rules[i].mask != RA_NONE))
regfree(&options_p->rules[i++].expr);
printf_ddd("Debug3: main_cleanup(): %i %i %i %i\n", options_p->watchdirsize, options_p->watchdirwslashsize, options_p->destdirsize, options_p->destdirwslashsize);
return 0;
}
int main_rehash(options_t *options_p) {
printf_ddd("Debug3: main_rehash()\n");
int ret=0;
main_cleanup(options_p);
if(options_p->rulfpath != NULL) {
ret = parse_rules_fromfile(options_p);
if(ret)
printf_e("Error: main_rehash(): Got error from parse_rules_fromfile(): %s (errno: %i).\n", strerror(ret), ret);
} else {
options_p->rules[0].perm = DEFAULT_RULES_PERM;
options_p->rules[0].mask = RA_NONE; // Terminator. End of rules.
}
return ret;
}
int main_status_update(options_t *options_p, state_t state) {
static state_t state_old = STATE_UNKNOWN;
if(state == state_old) {
printf_ddd("Debug3: main_status_update: State unchanged: %u == %u\n", state, state_old);
return 0;
}
#ifdef VERYPARANOID
if(status_descr[state] == NULL) {
printf_e("Error: main_status_update(): status_descr[%u] == NULL.\n", state);
return EINVAL;
}
#endif
setenv("CLSYNC_STATUS", status_descr[state], 1);
if(options_p->statusfile == NULL)
return 0;
FILE *f = fopen(options_p->statusfile, "w");
if(f == NULL) {
printf_e("Error: main_status_update(): Cannot open file \"%s\" for writing: %s (errno: %u).\n",
options_p->statusfile, strerror(errno), errno);
return errno;
}
printf_ddd("Debug3: main_status_update(): Setting status to %i: %s.\n", state, status_descr[state]);
state_old=state;
int ret = 0;
if(fprintf(f, "%s\n", status_descr[state]) <= 0) { // TODO: check output length
printf_e("Error: main_status_update(): Cannot write to file \"%s\": %s (errno: %u).\n",
options_p->statusfile, strerror(errno), errno);
ret = errno;
}
if(fclose(f)) {
printf_e("Error: main_status_update(): Cannot close file \"%s\": %s (errno: %u).\n",
options_p->statusfile, strerror(errno), errno);
ret = errno;
}
return ret;
}
int main(int argc, char *argv[]) {
struct options options;
#ifdef CLUSTER_SUPPORT
struct utsname utsname;
#endif
memset(&options, 0, sizeof(options));
int ret = 0, nret;
options.notifyengine = DEFAULT_NOTIFYENGINE;
options.syncdelay = DEFAULT_SYNCDELAY;
options._queues[QUEUE_NORMAL].collectdelay = DEFAULT_COLLECTDELAY;
options._queues[QUEUE_BIGFILE].collectdelay= DEFAULT_BFILECOLLECTDELAY;
options._queues[QUEUE_INSTANT].collectdelay= COLLECTDELAY_INSTANT;
options.bfilethreshold = DEFAULT_BFILETHRESHOLD;
options.label = DEFAULT_LABEL;
options.rsyncinclimit = DEFAULT_RSYNCINCLUDELINESLIMIT;
options.synctimeout = DEFAULT_SYNCTIMEOUT;
#ifdef CLUSTER_SUPPORT
options.cluster_hash_dl_min = DEFAULT_CLUSTERHDLMIN;
options.cluster_hash_dl_max = DEFAULT_CLUSTERHDLMAX;
options.cluster_scan_dl_max = DEFAULT_CLUSTERSDLMAX;
#endif
options.config_block = DEFAULT_CONFIG_BLOCK;
options.retries = DEFAULT_RETRIES;
nret = arguments_parse(argc, argv, &options);
if(nret) ret = nret;
out_init(options.flags);
nret = configs_parse(&options);
if(nret) ret = nret;
out_init(options.flags);
main_status_update(&options, STATE_STARTING);
if(options.socketpath != NULL) {
#ifndef ENABLE_SOCKET
printf_e("Error: clsync is compiled without control socket support, option \"--socket\" cannot be used.\n");
ret = EINVAL;
#endif
if(options.flags[SOCKETAUTH] == SOCKAUTH_UNSET)
options.flags[SOCKETAUTH] = SOCKAUTH_NULL;
}
if((options.flags[SOCKETOWN]) && (options.socketpath == NULL)) {
printf_e("Error: \"--socket-own\" is useless without \"--socket\"");
ret = EINVAL;
}
if((options.flags[SOCKETMOD]) && (options.socketpath == NULL)) {
printf_e("Error: \"--socket-mod\" is useless without \"--socket\"");
ret = EINVAL;
}