forked from mlichvar/chrony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
3344 lines (2819 loc) · 91.4 KB
/
client.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
/*
chronyd/chronyc - Programs for keeping computer clocks accurate.
**********************************************************************
* Copyright (C) Richard P. Curnow 1997-2003
* Copyright (C) Lonnie Abelbeck 2016, 2018
* Copyright (C) Miroslav Lichvar 2009-2018
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
**********************************************************************
=======================================================================
Command line client for configuring the daemon and obtaining status
from it whilst running.
*/
#include "config.h"
#include "sysincl.h"
#include "array.h"
#include "candm.h"
#include "cmac.h"
#include "logging.h"
#include "memory.h"
#include "nameserv.h"
#include "getdate.h"
#include "cmdparse.h"
#include "pktlength.h"
#include "socket.h"
#include "util.h"
#ifdef FEAT_READLINE
#ifdef USE_EDITLINE
#include <editline/readline.h>
#else
#include <readline/readline.h>
#include <readline/history.h>
#endif
#endif
/* ================================================== */
struct Address {
SCK_AddressType type;
union {
IPSockAddr ip;
char *path;
} addr;
};
static ARR_Instance server_addresses;
static int sock_fd = -1;
static int quit = 0;
static int on_terminal = 0;
static int no_dns = 0;
static int source_names = 0;
static int csv_mode = 0;
/* ================================================== */
/* Log a message. This is a minimalistic replacement of the logging.c
implementation to avoid linking with it and other modules. */
LOG_Severity log_min_severity = LOGS_INFO;
void LOG_Message(LOG_Severity severity,
#if DEBUG > 0
int line_number, const char *filename, const char *function_name,
#endif
const char *format, ...)
{
va_list ap;
if (severity < log_min_severity)
return;
va_start(ap, format);
vfprintf(stderr, format, ap);
putc('\n', stderr);
va_end(ap);
}
/* ================================================== */
/* Read a single line of commands from standard input */
#ifdef FEAT_READLINE
static char **command_name_completion(const char *text, int start, int end);
#endif
static char *
read_line(void)
{
static char line[2048];
static const char *prompt = "chronyc> ";
if (on_terminal) {
#ifdef FEAT_READLINE
char *cmd;
rl_attempted_completion_function = command_name_completion;
rl_basic_word_break_characters = " \t\n\r";
/* save line only if not empty */
cmd = readline(prompt);
if( cmd == NULL ) return( NULL );
/* user pressed return */
if( *cmd != '\0' ) {
strncpy(line, cmd, sizeof(line) - 1);
line[sizeof(line) - 1] = '\0';
add_history(cmd);
/* free the buffer allocated by readline */
Free(cmd);
} else {
/* simulate the user has entered an empty line */
*line = '\0';
}
return( line );
#else
printf("%s", prompt);
fflush(stdout);
#endif
}
if (fgets(line, sizeof(line), stdin)) {
return line;
} else {
return NULL;
}
}
/* ================================================== */
static ARR_Instance
get_addresses(const char *hostnames, int port)
{
struct Address *addr;
ARR_Instance addrs;
char *hostname, *s1, *s2;
IPAddr ip_addrs[DNS_MAX_ADDRESSES];
int i;
addrs = ARR_CreateInstance(sizeof (*addr));
s1 = Strdup(hostnames);
/* Parse the comma-separated list of hostnames */
for (hostname = s1; hostname && *hostname; hostname = s2) {
s2 = strchr(hostname, ',');
if (s2)
*s2++ = '\0';
/* hostname starting with / is considered a path of Unix domain socket */
if (hostname[0] == '/') {
addr = ARR_GetNewElement(addrs);
addr->type = SCK_ADDR_UNIX;
addr->addr.path = Strdup(hostname);
} else {
if (DNS_Name2IPAddress(hostname, ip_addrs, DNS_MAX_ADDRESSES) != DNS_Success) {
DEBUG_LOG("Could not get IP address for %s", hostname);
continue;
}
for (i = 0; i < DNS_MAX_ADDRESSES && ip_addrs[i].family != IPADDR_UNSPEC; i++) {
addr = ARR_GetNewElement(addrs);
addr->type = SCK_ADDR_IP;
addr->addr.ip.ip_addr = ip_addrs[i];
addr->addr.ip.port = port;
DEBUG_LOG("Resolved %s to %s", hostname, UTI_IPToString(&ip_addrs[i]));
}
}
}
Free(s1);
return addrs;
}
/* ================================================== */
static void
free_addresses(ARR_Instance addresses)
{
struct Address *addr;
unsigned int i;
for (i = 0; i < ARR_GetSize(addresses); i++) {
addr = ARR_GetElement(addresses, i);
if (addr->type == SCK_ADDR_UNIX)
Free(addr->addr.path);
}
ARR_DestroyInstance(addresses);
}
/* ================================================== */
/* Initialise the socket used to talk to the daemon */
static int
open_socket(struct Address *addr)
{
char *dir, *local_addr;
size_t local_addr_len;
switch (addr->type) {
case SCK_ADDR_IP:
sock_fd = SCK_OpenUdpSocket(&addr->addr.ip, NULL, 0);
break;
case SCK_ADDR_UNIX:
/* Construct path of our socket. Use the same directory as the server
socket and include our process ID to allow multiple chronyc instances
running at the same time. */
dir = UTI_PathToDir(addr->addr.path);
local_addr_len = strlen(dir) + 50;
local_addr = Malloc(local_addr_len);
snprintf(local_addr, local_addr_len, "%s/chronyc.%d.sock", dir, (int)getpid());
sock_fd = SCK_OpenUnixDatagramSocket(addr->addr.path, local_addr,
SCK_FLAG_ALL_PERMISSIONS);
Free(dir);
Free(local_addr);
break;
default:
assert(0);
}
if (sock_fd < 0)
return 0;
return 1;
}
/* ================================================== */
static void
close_io(void)
{
if (sock_fd < 0)
return;
SCK_RemoveSocket(sock_fd);
SCK_CloseSocket(sock_fd);
sock_fd = -1;
}
/* ================================================== */
static int
open_io(void)
{
static unsigned int address_index = 0;
struct Address *addr;
/* If a socket is already opened, close it and try the next address */
if (sock_fd >= 0) {
close_io();
address_index++;
}
/* Find an address for which a socket can be opened and connected */
for (; address_index < ARR_GetSize(server_addresses); address_index++) {
addr = ARR_GetElement(server_addresses, address_index);
if (open_socket(addr))
return 1;
close_io();
}
return 0;
}
/* ================================================== */
static void
bits_to_mask(int bits, int family, IPAddr *mask)
{
int i;
mask->family = family;
switch (family) {
case IPADDR_INET4:
if (bits > 32 || bits < 0)
bits = 32;
if (bits > 0) {
mask->addr.in4 = -1;
mask->addr.in4 <<= 32 - bits;
} else {
mask->addr.in4 = 0;
}
break;
case IPADDR_INET6:
if (bits > 128 || bits < 0)
bits = 128;
for (i = 0; i < bits / 8; i++)
mask->addr.in6[i] = 0xff;
if (i < 16)
mask->addr.in6[i++] = (0xff << (8 - bits % 8)) & 0xff;
for (; i < 16; i++)
mask->addr.in6[i] = 0x0;
break;
default:
assert(0);
}
}
/* ================================================== */
static int
read_mask_address(char *line, IPAddr *mask, IPAddr *address)
{
unsigned int bits;
char *p, *q;
p = line;
if (!*p) {
mask->family = address->family = IPADDR_UNSPEC;
return 1;
} else {
q = strchr(p, '/');
if (q) {
*q++ = 0;
if (UTI_StringToIP(p, mask)) {
p = q;
if (UTI_StringToIP(p, address)) {
if (address->family == mask->family)
return 1;
} else if (sscanf(p, "%u", &bits) == 1) {
*address = *mask;
bits_to_mask(bits, address->family, mask);
return 1;
}
}
} else {
if (DNS_Name2IPAddress(p, address, 1) == DNS_Success) {
bits_to_mask(-1, address->family, mask);
return 1;
} else {
LOG(LOGS_ERR, "Could not get address for hostname");
return 0;
}
}
}
LOG(LOGS_ERR, "Invalid syntax for mask/address");
return 0;
}
/* ================================================== */
static int
process_cmd_offline(CMD_Request *msg, char *line)
{
IPAddr mask, address;
int ok;
if (read_mask_address(line, &mask, &address)) {
UTI_IPHostToNetwork(&mask, &msg->data.offline.mask);
UTI_IPHostToNetwork(&address, &msg->data.offline.address);
msg->command = htons(REQ_OFFLINE);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static int
process_cmd_online(CMD_Request *msg, char *line)
{
IPAddr mask, address;
int ok;
if (read_mask_address(line, &mask, &address)) {
UTI_IPHostToNetwork(&mask, &msg->data.online.mask);
UTI_IPHostToNetwork(&address, &msg->data.online.address);
msg->command = htons(REQ_ONLINE);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static void
process_cmd_onoffline(CMD_Request *msg, char *line)
{
msg->command = htons(REQ_ONOFFLINE);
}
/* ================================================== */
static int
read_address_integer(char *line, IPAddr *address, int *value)
{
char *hostname;
int ok = 0;
hostname = line;
line = CPS_SplitWord(line);
if (sscanf(line, "%d", value) != 1) {
LOG(LOGS_ERR, "Invalid syntax for address value");
ok = 0;
} else {
if (DNS_Name2IPAddress(hostname, address, 1) != DNS_Success) {
LOG(LOGS_ERR, "Could not get address for hostname");
ok = 0;
} else {
ok = 1;
}
}
return ok;
}
/* ================================================== */
static int
read_address_double(char *line, IPAddr *address, double *value)
{
char *hostname;
int ok = 0;
hostname = line;
line = CPS_SplitWord(line);
if (sscanf(line, "%lf", value) != 1) {
LOG(LOGS_ERR, "Invalid syntax for address value");
ok = 0;
} else {
if (DNS_Name2IPAddress(hostname, address, 1) != DNS_Success) {
LOG(LOGS_ERR, "Could not get address for hostname");
ok = 0;
} else {
ok = 1;
}
}
return ok;
}
/* ================================================== */
static int
process_cmd_minpoll(CMD_Request *msg, char *line)
{
IPAddr address;
int minpoll;
int ok;
if (read_address_integer(line, &address, &minpoll)) {
UTI_IPHostToNetwork(&address, &msg->data.modify_minpoll.address);
msg->data.modify_minpoll.new_minpoll = htonl(minpoll);
msg->command = htons(REQ_MODIFY_MINPOLL);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static int
process_cmd_maxpoll(CMD_Request *msg, char *line)
{
IPAddr address;
int maxpoll;
int ok;
if (read_address_integer(line, &address, &maxpoll)) {
UTI_IPHostToNetwork(&address, &msg->data.modify_maxpoll.address);
msg->data.modify_maxpoll.new_maxpoll = htonl(maxpoll);
msg->command = htons(REQ_MODIFY_MAXPOLL);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static int
process_cmd_maxdelay(CMD_Request *msg, char *line)
{
IPAddr address;
double max_delay;
int ok;
if (read_address_double(line, &address, &max_delay)) {
UTI_IPHostToNetwork(&address, &msg->data.modify_maxdelay.address);
msg->data.modify_maxdelay.new_max_delay = UTI_FloatHostToNetwork(max_delay);
msg->command = htons(REQ_MODIFY_MAXDELAY);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static int
process_cmd_maxdelaydevratio(CMD_Request *msg, char *line)
{
IPAddr address;
double max_delay_dev_ratio;
int ok;
if (read_address_double(line, &address, &max_delay_dev_ratio)) {
UTI_IPHostToNetwork(&address, &msg->data.modify_maxdelaydevratio.address);
msg->data.modify_maxdelayratio.new_max_delay_ratio = UTI_FloatHostToNetwork(max_delay_dev_ratio);
msg->command = htons(REQ_MODIFY_MAXDELAYDEVRATIO);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static int
process_cmd_maxdelayratio(CMD_Request *msg, char *line)
{
IPAddr address;
double max_delay_ratio;
int ok;
if (read_address_double(line, &address, &max_delay_ratio)) {
UTI_IPHostToNetwork(&address, &msg->data.modify_maxdelayratio.address);
msg->data.modify_maxdelayratio.new_max_delay_ratio = UTI_FloatHostToNetwork(max_delay_ratio);
msg->command = htons(REQ_MODIFY_MAXDELAYRATIO);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static int
process_cmd_minstratum(CMD_Request *msg, char *line)
{
IPAddr address;
int min_stratum;
int ok;
if (read_address_integer(line, &address, &min_stratum)) {
UTI_IPHostToNetwork(&address, &msg->data.modify_minstratum.address);
msg->data.modify_minstratum.new_min_stratum = htonl(min_stratum);
msg->command = htons(REQ_MODIFY_MINSTRATUM);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static int
process_cmd_polltarget(CMD_Request *msg, char *line)
{
IPAddr address;
int poll_target;
int ok;
if (read_address_integer(line, &address, &poll_target)) {
UTI_IPHostToNetwork(&address, &msg->data.modify_polltarget.address);
msg->data.modify_polltarget.new_poll_target = htonl(poll_target);
msg->command = htons(REQ_MODIFY_POLLTARGET);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static int
process_cmd_maxupdateskew(CMD_Request *msg, char *line)
{
int ok;
double new_max_update_skew;
if (sscanf(line, "%lf", &new_max_update_skew) == 1) {
msg->data.modify_maxupdateskew.new_max_update_skew = UTI_FloatHostToNetwork(new_max_update_skew);
msg->command = htons(REQ_MODIFY_MAXUPDATESKEW);
ok = 1;
} else {
ok = 0;
}
return ok;
}
/* ================================================== */
static void
process_cmd_dump(CMD_Request *msg, char *line)
{
msg->command = htons(REQ_DUMP);
msg->data.dump.pad = htonl(0);
}
/* ================================================== */
static void
process_cmd_writertc(CMD_Request *msg, char *line)
{
msg->command = htons(REQ_WRITERTC);
}
/* ================================================== */
static void
process_cmd_trimrtc(CMD_Request *msg, char *line)
{
msg->command = htons(REQ_TRIMRTC);
}
/* ================================================== */
static void
process_cmd_cyclelogs(CMD_Request *msg, char *line)
{
msg->command = htons(REQ_CYCLELOGS);
}
/* ================================================== */
static int
process_cmd_burst(CMD_Request *msg, char *line)
{
int n_good_samples, n_total_samples;
char *s1, *s2;
IPAddr address, mask;
s1 = line;
s2 = CPS_SplitWord(s1);
CPS_SplitWord(s2);
if (sscanf(s1, "%d/%d", &n_good_samples, &n_total_samples) != 2) {
LOG(LOGS_ERR, "Invalid syntax for burst command");
return 0;
}
mask.family = address.family = IPADDR_UNSPEC;
if (*s2 && !read_mask_address(s2, &mask, &address)) {
return 0;
}
msg->command = htons(REQ_BURST);
msg->data.burst.n_good_samples = ntohl(n_good_samples);
msg->data.burst.n_total_samples = ntohl(n_total_samples);
UTI_IPHostToNetwork(&mask, &msg->data.burst.mask);
UTI_IPHostToNetwork(&address, &msg->data.burst.address);
return 1;
}
/* ================================================== */
static int
process_cmd_local(CMD_Request *msg, char *line)
{
int on_off, stratum = 0, orphan = 0;
double distance = 0.0;
if (!strcmp(line, "off")) {
on_off = 0;
} else if (CPS_ParseLocal(line, &stratum, &orphan, &distance)) {
on_off = 1;
} else {
LOG(LOGS_ERR, "Invalid syntax for local command");
return 0;
}
msg->command = htons(REQ_LOCAL2);
msg->data.local.on_off = htonl(on_off);
msg->data.local.stratum = htonl(stratum);
msg->data.local.distance = UTI_FloatHostToNetwork(distance);
msg->data.local.orphan = htonl(orphan);
return 1;
}
/* ================================================== */
static int
process_cmd_manual(CMD_Request *msg, const char *line)
{
const char *p;
p = line;
if (!strcmp(p, "off")) {
msg->data.manual.option = htonl(0);
} else if (!strcmp(p, "on")) {
msg->data.manual.option = htonl(1);
} else if (!strcmp(p, "reset")) {
msg->data.manual.option = htonl(2);
} else {
LOG(LOGS_ERR, "Invalid syntax for manual command");
return 0;
}
msg->command = htons(REQ_MANUAL);
return 1;
}
/* ================================================== */
static int
parse_allow_deny(CMD_Request *msg, char *line)
{
unsigned long a, b, c, d;
int n, specified_subnet_bits;
IPAddr ip;
char *p;
p = line;
if (!*p) {
/* blank line - applies to all addresses */
ip.family = IPADDR_UNSPEC;
UTI_IPHostToNetwork(&ip, &msg->data.allow_deny.ip);
msg->data.allow_deny.subnet_bits = htonl(0);
} else {
char *slashpos;
slashpos = strchr(p, '/');
if (slashpos) *slashpos = 0;
n = 0;
if (!UTI_StringToIP(p, &ip) &&
(n = sscanf(p, "%lu.%lu.%lu.%lu", &a, &b, &c, &d)) <= 0) {
/* Try to parse as the name of a machine */
if (slashpos || DNS_Name2IPAddress(p, &ip, 1) != DNS_Success) {
LOG(LOGS_ERR, "Could not read address");
return 0;
} else {
UTI_IPHostToNetwork(&ip, &msg->data.allow_deny.ip);
if (ip.family == IPADDR_INET6)
msg->data.allow_deny.subnet_bits = htonl(128);
else
msg->data.allow_deny.subnet_bits = htonl(32);
}
} else {
if (n == 0) {
if (ip.family == IPADDR_INET6)
msg->data.allow_deny.subnet_bits = htonl(128);
else
msg->data.allow_deny.subnet_bits = htonl(32);
} else {
ip.family = IPADDR_INET4;
a &= 0xff;
b &= 0xff;
c &= 0xff;
d &= 0xff;
switch (n) {
case 1:
ip.addr.in4 = htonl((a<<24));
msg->data.allow_deny.subnet_bits = htonl(8);
break;
case 2:
ip.addr.in4 = htonl((a<<24) | (b<<16));
msg->data.allow_deny.subnet_bits = htonl(16);
break;
case 3:
ip.addr.in4 = htonl((a<<24) | (b<<16) | (c<<8));
msg->data.allow_deny.subnet_bits = htonl(24);
break;
case 4:
ip.addr.in4 = htonl((a<<24) | (b<<16) | (c<<8) | d);
msg->data.allow_deny.subnet_bits = htonl(32);
break;
default:
assert(0);
}
}
UTI_IPHostToNetwork(&ip, &msg->data.allow_deny.ip);
if (slashpos) {
n = sscanf(slashpos+1, "%d", &specified_subnet_bits);
if (n == 1) {
msg->data.allow_deny.subnet_bits = htonl(specified_subnet_bits);
} else {
LOG(LOGS_WARN, "Warning: badly formatted subnet size, using %d",
(int)ntohl(msg->data.allow_deny.subnet_bits));
}
}
}
}
return 1;
}
/* ================================================== */
static int
process_cmd_allow(CMD_Request *msg, char *line)
{
int status;
msg->command = htons(REQ_ALLOW);
status = parse_allow_deny(msg, line);
return status;
}
/* ================================================== */
static int
process_cmd_allowall(CMD_Request *msg, char *line)
{
int status;
msg->command = htons(REQ_ALLOWALL);
status = parse_allow_deny(msg, line);
return status;
}
/* ================================================== */
static int
process_cmd_deny(CMD_Request *msg, char *line)
{
int status;
msg->command = htons(REQ_DENY);
status = parse_allow_deny(msg, line);
return status;
}
/* ================================================== */
static int
process_cmd_denyall(CMD_Request *msg, char *line)
{
int status;
msg->command = htons(REQ_DENYALL);
status = parse_allow_deny(msg, line);
return status;
}
/* ================================================== */
static int
process_cmd_cmdallow(CMD_Request *msg, char *line)
{
int status;
msg->command = htons(REQ_CMDALLOW);
status = parse_allow_deny(msg, line);
return status;
}
/* ================================================== */
static int
process_cmd_cmdallowall(CMD_Request *msg, char *line)
{
int status;
msg->command = htons(REQ_CMDALLOWALL);
status = parse_allow_deny(msg, line);
return status;
}
/* ================================================== */
static int
process_cmd_cmddeny(CMD_Request *msg, char *line)
{
int status;
msg->command = htons(REQ_CMDDENY);
status = parse_allow_deny(msg, line);
return status;
}
/* ================================================== */
static int
process_cmd_cmddenyall(CMD_Request *msg, char *line)
{
int status;
msg->command = htons(REQ_CMDDENYALL);
status = parse_allow_deny(msg, line);
return status;
}
/* ================================================== */
static int
accheck_getaddr(char *line, IPAddr *addr)
{
unsigned long a, b, c, d;
IPAddr ip;
char *p;
p = line;
if (!*p) {
return 0;
} else {
if (sscanf(p, "%lu.%lu.%lu.%lu", &a, &b, &c, &d) == 4) {
addr->family = IPADDR_INET4;
addr->addr.in4 = (a<<24) | (b<<16) | (c<<8) | d;
return 1;
} else {
if (DNS_Name2IPAddress(p, &ip, 1) != DNS_Success) {
return 0;
} else {
*addr = ip;
return 1;
}
}
}
}
/* ================================================== */
static int
process_cmd_accheck(CMD_Request *msg, char *line)
{
IPAddr ip;
msg->command = htons(REQ_ACCHECK);
if (accheck_getaddr(line, &ip)) {
UTI_IPHostToNetwork(&ip, &msg->data.ac_check.ip);
return 1;
} else {
LOG(LOGS_ERR, "Could not read address");
return 0;
}
}
/* ================================================== */
static int
process_cmd_cmdaccheck(CMD_Request *msg, char *line)
{
IPAddr ip;
msg->command = htons(REQ_CMDACCHECK);
if (accheck_getaddr(line, &ip)) {
UTI_IPHostToNetwork(&ip, &msg->data.ac_check.ip);
return 1;
} else {
LOG(LOGS_ERR, "Could not read address");
return 0;
}