/
realms.c
2713 lines (2255 loc) · 65.7 KB
/
realms.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
/*
* realms.c Realm handling code
*
* Version: $Id$
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2007 The FreeRADIUS server project
* Copyright 2007 Alan DeKok <aland@deployingradius.com>
*/
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/realms.h>
#include <freeradius-devel/rad_assert.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
static rbtree_t *realms_byname = NULL;
#ifdef WITH_TCP
bool home_servers_udp = false;
#endif
#ifdef HAVE_REGEX
typedef struct realm_regex realm_regex_t;
/** Regular expression associated with a realm
*
*/
struct realm_regex {
REALM *realm; //!< The realm this regex matches.
regex_t *preg; //!< The pre-compiled regular expression.
realm_regex_t *next; //!< The next realm in the list of regular expressions.
};
static realm_regex_t *realms_regex = NULL;
#endif /* HAVE_REGEX */
struct realm_config {
CONF_SECTION *cs;
uint32_t dead_time;
uint32_t retry_count;
uint32_t retry_delay;
bool dynamic;
bool fallback;
bool wake_all_if_all_dead;
};
static const FR_NAME_NUMBER home_server_types[] = {
{ "auth", HOME_TYPE_AUTH },
{ "acct", HOME_TYPE_ACCT },
{ "auth+acct", HOME_TYPE_AUTH_ACCT },
{ "coa", HOME_TYPE_COA },
{ NULL, 0 }
};
static const FR_NAME_NUMBER home_ping_check[] = {
{ "none", HOME_PING_CHECK_NONE },
{ "status-server", HOME_PING_CHECK_STATUS_SERVER },
{ "request", HOME_PING_CHECK_REQUEST },
{ NULL, 0 }
};
static realm_config_t *realm_config = NULL;
#ifdef WITH_PROXY
static rbtree_t *home_servers_byaddr = NULL;
static rbtree_t *home_servers_byname = NULL;
#ifdef WITH_STATS
static int home_server_max_number = 0;
static rbtree_t *home_servers_bynumber = NULL;
#endif
static rbtree_t *home_pools_byname = NULL;
/*
* Map the proxy server configuration parameters to variables.
*/
static const CONF_PARSER proxy_config[] = {
{ FR_CONF_OFFSET("retry_delay", PW_TYPE_INTEGER, realm_config_t, retry_delay), .dflt = STRINGIFY(RETRY_DELAY) },
{ FR_CONF_OFFSET("retry_count", PW_TYPE_INTEGER, realm_config_t, retry_count), .dflt = STRINGIFY(RETRY_COUNT) },
{ FR_CONF_OFFSET("default_fallback", PW_TYPE_BOOLEAN, realm_config_t, fallback), .dflt = "no" },
{ FR_CONF_OFFSET("dynamic", PW_TYPE_BOOLEAN, realm_config_t, dynamic) },
{ FR_CONF_OFFSET("dead_time", PW_TYPE_INTEGER, realm_config_t, dead_time), .dflt = STRINGIFY(DEAD_TIME) },
{ FR_CONF_OFFSET("wake_all_if_all_dead", PW_TYPE_BOOLEAN, realm_config_t, wake_all_if_all_dead), .dflt = "no" },
CONF_PARSER_TERMINATOR
};
#endif
static int realm_name_cmp(void const *one, void const *two)
{
REALM const *a = one;
REALM const *b = two;
return strcasecmp(a->name, b->name);
}
#ifdef WITH_PROXY
static void home_server_free(void *data)
{
home_server_t *home = talloc_get_type_abort(data, home_server_t);
talloc_free(home);
}
static int home_server_name_cmp(void const *one, void const *two)
{
home_server_t const *a = one;
home_server_t const *b = two;
if (a->type < b->type) return -1;
if (a->type > b->type) return +1;
return strcasecmp(a->name, b->name);
}
static int home_server_addr_cmp(void const *one, void const *two)
{
int rcode;
home_server_t const *a = one;
home_server_t const *b = two;
if (a->server && !b->server) return -1;
if (!a->server && b->server) return +1;
if (a->server && b->server) {
rcode = a->type - b->type;
if (rcode != 0) return rcode;
return strcmp(a->server, b->server);
}
if (a->port < b->port) return -1;
if (a->port > b->port) return +1;
#ifdef WITH_TCP
if (a->proto < b->proto) return -1;
if (a->proto > b->proto) return +1;
#endif
rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
if (rcode != 0) return rcode;
return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
}
#ifdef WITH_STATS
static int home_server_number_cmp(void const *one, void const *two)
{
home_server_t const *a = one;
home_server_t const *b = two;
return (a->number - b->number);
}
#endif
static int home_pool_name_cmp(void const *one, void const *two)
{
home_pool_t const *a = one;
home_pool_t const *b = two;
if (a->server_type < b->server_type) return -1;
if (a->server_type > b->server_type) return +1;
return strcasecmp(a->name, b->name);
}
static size_t CC_HINT(nonnull) xlat_cs(CONF_SECTION *cs, char const *fmt, char *out, size_t outlen)
{
char const *value = NULL;
/*
* Instance name
*/
if (strcmp(fmt, "instance") == 0) {
value = cf_section_name2(cs);
if (!value) {
*out = '\0';
return 0;
}
} else {
CONF_PAIR *cp;
cp = cf_pair_find(cs, fmt);
if (!cp || !(value = cf_pair_value(cp))) {
*out = '\0';
return 0;
}
}
strlcpy(out, value, outlen);
return strlen(out);
}
/** xlat for %{home_server:foo}
*
*/
static ssize_t xlat_home_server(char **out, size_t outlen,
UNUSED void const *mod_inst, UNUSED void const *xlat_inst,
REQUEST *request, char const *fmt) CC_HINT(nonnull);
static ssize_t xlat_home_server(char **out, size_t outlen,
UNUSED void const *mod_inst, UNUSED void const *xlat_inst,
REQUEST *request, char const *fmt)
{
if (!request->home_server) {
RWDEBUG("No home_server associated with this request");
return 0;
}
return xlat_cs(request->home_server->cs, fmt, *out, outlen);
}
/** xlat for %{home_server_pool:foo}
*
*/
static ssize_t xlat_server_pool(char **out, size_t outlen,
UNUSED void const *mod_inst, UNUSED void const *xlat_inst,
REQUEST *request, char const *fmt) CC_HINT(nonnull);
static ssize_t xlat_server_pool(char **out, size_t outlen,
UNUSED void const *mod_inst, UNUSED void const *xlat_inst,
REQUEST *request, char const *fmt)
{
if (!request->home_pool) {
RWDEBUG("No home_pool associated with this request");
return 0;
}
return xlat_cs(request->home_pool->cs, fmt, *out, outlen);
}
#endif
void realms_free(void)
{
#ifdef WITH_PROXY
# ifdef WITH_STATS
rbtree_free(home_servers_bynumber);
home_servers_bynumber = NULL;
# endif
rbtree_free(home_servers_byname);
home_servers_byname = NULL;
rbtree_free(home_servers_byaddr);
home_servers_byaddr = NULL;
rbtree_free(home_pools_byname);
home_pools_byname = NULL;
#endif
rbtree_free(realms_byname);
realms_byname = NULL;
realm_pool_free(NULL);
talloc_free(realm_config);
realm_config = NULL;
}
#ifdef WITH_PROXY
static CONF_PARSER limit_config[] = {
{ FR_CONF_OFFSET("max_connections", PW_TYPE_INTEGER, home_server_t, limit.max_connections), .dflt = "16" },
{ FR_CONF_OFFSET("max_requests", PW_TYPE_INTEGER, home_server_t, limit.max_requests), .dflt = "0" },
{ FR_CONF_OFFSET("lifetime", PW_TYPE_INTEGER, home_server_t, limit.lifetime), .dflt = "0" },
{ FR_CONF_OFFSET("idle_timeout", PW_TYPE_INTEGER, home_server_t, limit.idle_timeout), .dflt = "0" },
CONF_PARSER_TERMINATOR
};
#ifdef WITH_COA
static CONF_PARSER home_server_coa[] = {
{ FR_CONF_OFFSET("irt", PW_TYPE_INTEGER, home_server_t, coa_irt), .dflt = STRINGIFY(2) },
{ FR_CONF_OFFSET("mrt", PW_TYPE_INTEGER, home_server_t, coa_mrt), .dflt = STRINGIFY(16) },
{ FR_CONF_OFFSET("mrc", PW_TYPE_INTEGER, home_server_t, coa_mrc), .dflt = STRINGIFY(5) },
{ FR_CONF_OFFSET("mrd", PW_TYPE_INTEGER, home_server_t, coa_mrd), .dflt = STRINGIFY(30) },
CONF_PARSER_TERMINATOR
};
#endif
static CONF_PARSER home_server_config[] = {
{ FR_CONF_OFFSET("ipaddr", PW_TYPE_COMBO_IP_ADDR, home_server_t, ipaddr) },
{ FR_CONF_OFFSET("ipv4addr", PW_TYPE_IPV4_ADDR, home_server_t, ipaddr) },
{ FR_CONF_OFFSET("ipv6addr", PW_TYPE_IPV6_ADDR, home_server_t, ipaddr) },
{ FR_CONF_OFFSET("virtual_server", PW_TYPE_STRING | PW_TYPE_NOT_EMPTY, home_server_t, server) },
{ FR_CONF_OFFSET("port", PW_TYPE_SHORT, home_server_t, port), .dflt = "0" },
{ FR_CONF_OFFSET("type", PW_TYPE_STRING, home_server_t, type_str) },
#ifdef WITH_TCP
{ FR_CONF_OFFSET("proto", PW_TYPE_STRING | PW_TYPE_NOT_EMPTY, home_server_t, proto_str) },
#endif
{ FR_CONF_OFFSET("secret", PW_TYPE_STRING | PW_TYPE_SECRET, home_server_t, secret) },
{ FR_CONF_OFFSET("src_ipaddr", PW_TYPE_STRING, home_server_t, src_ipaddr_str) },
{ FR_CONF_OFFSET("response_window", PW_TYPE_TIMEVAL, home_server_t, response_window), .dflt = "30" },
{ FR_CONF_OFFSET("response_timeouts", PW_TYPE_INTEGER, home_server_t, max_response_timeouts), .dflt = "1" },
{ FR_CONF_OFFSET("max_outstanding", PW_TYPE_INTEGER, home_server_t, max_outstanding), .dflt = "65536" },
{ FR_CONF_OFFSET("zombie_period", PW_TYPE_INTEGER, home_server_t, zombie_period), .dflt = "40" },
{ FR_CONF_OFFSET("status_check", PW_TYPE_STRING, home_server_t, ping_check_str), .dflt = "none" },
{ FR_CONF_OFFSET("ping_check", PW_TYPE_STRING, home_server_t, ping_check_str) },
{ FR_CONF_OFFSET("ping_interval", PW_TYPE_INTEGER, home_server_t, ping_interval), .dflt = "30" },
{ FR_CONF_OFFSET("check_interval", PW_TYPE_INTEGER, home_server_t, ping_interval) },
{ FR_CONF_OFFSET("check_timeout", PW_TYPE_INTEGER, home_server_t, ping_timeout), .dflt = "4" },
{ FR_CONF_OFFSET("status_check_timeout", PW_TYPE_INTEGER, home_server_t, ping_timeout) },
{ FR_CONF_OFFSET("num_answers_to_alive", PW_TYPE_INTEGER, home_server_t, num_pings_to_alive), .dflt = "3" },
{ FR_CONF_OFFSET("revive_interval", PW_TYPE_INTEGER, home_server_t, revive_interval), .dflt = "300" },
{ FR_CONF_OFFSET("username", PW_TYPE_STRING | PW_TYPE_NOT_EMPTY, home_server_t, ping_user_name) },
{ FR_CONF_OFFSET("password", PW_TYPE_STRING | PW_TYPE_NOT_EMPTY, home_server_t, ping_user_password) },
#ifdef WITH_STATS
{ FR_CONF_OFFSET("historic_average_window", PW_TYPE_INTEGER, home_server_t, ema.window) },
#endif
{ FR_CONF_POINTER("limit", PW_TYPE_SUBSECTION, NULL), .dflt = (void const *) limit_config },
#ifdef WITH_COA
{ FR_CONF_POINTER("coa", PW_TYPE_SUBSECTION, NULL), .dflt = (void const *) home_server_coa },
#endif
CONF_PARSER_TERMINATOR
};
static void null_free(UNUSED void *data)
{
}
/*
* Ensure that all of the parameters in the home server are OK.
*/
void realm_home_server_sanitize(home_server_t *home, CONF_SECTION *cs)
{
CONF_SECTION *parent = NULL;
FR_INTEGER_BOUND_CHECK("max_outstanding", home->max_outstanding, >=, 8);
FR_INTEGER_BOUND_CHECK("max_outstanding", home->max_outstanding, <=, 65536*16);
FR_INTEGER_BOUND_CHECK("ping_interval", home->ping_interval, >=, 6);
FR_INTEGER_BOUND_CHECK("ping_interval", home->ping_interval, <=, 120);
FR_TIMEVAL_BOUND_CHECK("response_window", &home->response_window, >=, 0, 1000);
FR_TIMEVAL_BOUND_CHECK("response_window", &home->response_window, <=, 60, 0);
FR_TIMEVAL_BOUND_CHECK("response_window", &home->response_window, <=,
main_config.max_request_time, 0);
FR_INTEGER_BOUND_CHECK("response_timeouts", home->max_response_timeouts, >=, 1);
FR_INTEGER_BOUND_CHECK("response_timeouts", home->max_response_timeouts, <=, 1000);
/*
* Track the minimum response window, so that we can
* correctly set the timers in process.c
*/
if (timercmp(&main_config.init_delay, &home->response_window, >)) {
main_config.init_delay = home->response_window;
}
FR_INTEGER_BOUND_CHECK("zombie_period", home->zombie_period, >=, 1);
FR_INTEGER_BOUND_CHECK("zombie_period", home->zombie_period, <=, 120);
FR_INTEGER_BOUND_CHECK("zombie_period", home->zombie_period, >=, (uint32_t) home->response_window.tv_sec);
FR_INTEGER_BOUND_CHECK("num_pings_to_alive", home->num_pings_to_alive, >=, 3);
FR_INTEGER_BOUND_CHECK("num_pings_to_alive", home->num_pings_to_alive, <=, 10);
FR_INTEGER_BOUND_CHECK("check_timeout", home->ping_timeout, >=, 1);
FR_INTEGER_BOUND_CHECK("check_timeout", home->ping_timeout, <=, 10);
FR_INTEGER_BOUND_CHECK("revive_interval", home->revive_interval, >=, 60);
FR_INTEGER_BOUND_CHECK("revive_interval", home->revive_interval, <=, 3600);
#ifdef WITH_COA
FR_INTEGER_BOUND_CHECK("coa_irt", home->coa_irt, >=, 1);
FR_INTEGER_BOUND_CHECK("coa_irt", home->coa_irt, <=, 5);
FR_INTEGER_BOUND_CHECK("coa_mrc", home->coa_mrc, <=, 20);
FR_INTEGER_BOUND_CHECK("coa_mrt", home->coa_mrt, <=, 30);
FR_INTEGER_BOUND_CHECK("coa_mrd", home->coa_mrd, >=, 5);
FR_INTEGER_BOUND_CHECK("coa_mrd", home->coa_mrd, <=, 60);
#endif
FR_INTEGER_BOUND_CHECK("max_connections", home->limit.max_connections, <=, 1024);
#ifdef WITH_TCP
/*
* UDP sockets can't be connection limited.
*/
if (home->proto != IPPROTO_TCP) home->limit.max_connections = 0;
#endif
if ((home->limit.idle_timeout > 0) && (home->limit.idle_timeout < 5))
home->limit.idle_timeout = 5;
if ((home->limit.lifetime > 0) && (home->limit.lifetime < 5))
home->limit.lifetime = 5;
if ((home->limit.lifetime > 0) && (home->limit.idle_timeout > home->limit.lifetime))
home->limit.idle_timeout = 0;
/*
* Make sure that this is set.
*/
if (home->src_ipaddr.af == AF_UNSPEC) {
home->src_ipaddr.af = home->ipaddr.af;
}
parent = cf_item_parent(cf_section_to_item(cs));
if (parent && strcmp(cf_section_name1(parent), "server") == 0) {
home->parent_server = cf_section_name2(parent);
}
}
/** Insert a new home server into the various internal lookup trees
*
* @param home server to add.
* @param cs That defined the home server.
* @return
* - true on success.
* - false on failure.
*/
static bool home_server_insert(home_server_t *home, CONF_SECTION *cs)
{
if (home->name && !rbtree_insert(home_servers_byname, home)) {
cf_log_err_cs(cs, "Internal error %d adding home server %s", __LINE__, home->log_name);
return false;
}
if (!home->server && !rbtree_insert(home_servers_byaddr, home)) {
rbtree_deletebydata(home_servers_byname, home);
cf_log_err_cs(cs, "Internal error %d adding home server %s", __LINE__, home->log_name);
return false;
}
#ifdef WITH_STATS
home->number = home_server_max_number++;
if (!rbtree_insert(home_servers_bynumber, home)) {
rbtree_deletebydata(home_servers_byname, home);
if (home->ipaddr.af != AF_UNSPEC) {
rbtree_deletebydata(home_servers_byname, home);
}
cf_log_err_cs(cs, "Internal error %d adding home server %s", __LINE__, home->log_name);
return false;
}
#endif
return true;
}
/** Add an already allocate home_server_t to the various trees
*
* @param home server to add.
* @return
* - true on success.
* - false on error.
*/
bool realm_home_server_add(home_server_t *home)
{
/*
* The structs aren't mutex protected. Refuse to destroy
* the server.
*/
if (event_loop_started && !realm_config->dynamic) {
ERROR("Failed to add dynamic home server, \"dynamic = yes\" must be set in proxy.conf");
return false;
}
if (home->name && (rbtree_finddata(home_servers_byname, home) != NULL)) {
cf_log_err_cs(home->cs, "Duplicate home server name %s", home->name);
return false;
}
if (!home->server && (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
char buffer[INET6_ADDRSTRLEN];
inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr, buffer, sizeof(buffer));
cf_log_err_cs(home->cs, "Duplicate home server address%s%s%s: %s:%s%s/%i",
home->name ? " (already in use by " : "",
home->name ? home->name : "",
home->name ? ")" : "",
buffer,
fr_int2str(fr_net_ip_proto_table, home->proto, "<INVALID>"),
#ifdef WITH_TLS
home->tls ? "+tls" : "",
#else
"",
#endif
home->port);
return false;
}
if (!home_server_insert(home, home->cs)) return false;
/*
* Dual home servers cause us to auto-create an
* accounting server for UDP sockets, and leave
* everything alone for TLS sockets.
*/
if (home->dual
#ifdef WITH_TLS
&& !home->tls
#endif
) {
home_server_t *home2 = talloc(talloc_parent(home), home_server_t);
memcpy(home2, home, sizeof(*home2));
home2->type = HOME_TYPE_ACCT;
home2->dual = true;
home2->port++;
home2->ping_user_password = NULL;
home2->cs = home->cs;
home2->parent_server = home->parent_server;
if (!home_server_insert(home2, home->cs)) {
talloc_free(home2);
return false;
}
}
/*
* Mark it as already processed
*/
cf_data_add(home->cs, "home_server", (void *)null_free, null_free);
return true;
}
/** Alloc a new home server defined by a #CONF_SECTION
*
* @param ctx to allocate home_server_t in.
* @param rc Realm config, may be NULL in which case the global realm_config will be used.
* @param cs Configuration section containing home server parameters.
* @return
* - New #home_server_t alloced in the context of the realm_config.
* - NULL on error.
*/
home_server_t *home_server_afrom_cs(TALLOC_CTX *ctx, realm_config_t *rc, CONF_SECTION *cs)
{
home_server_t *home;
CONF_SECTION *tls;
if (!rc) rc = realm_config; /* Use the global config */
home = talloc_zero(ctx, home_server_t);
home->name = cf_section_name2(cs);
home->log_name = talloc_typed_strdup(home, home->name);
home->cs = cs;
home->state = HOME_STATE_UNKNOWN;
home->proto = IPPROTO_UDP;
/*
* Parse the configuration into the home server
* struct.
*/
if (cf_section_parse(cs, home, home_server_config) < 0) goto error;
/*
* It has an IP address, it must be a remote server.
*/
if (cf_pair_find(cs, "ipaddr") || cf_pair_find(cs, "ipv4addr") || cf_pair_find(cs, "ipv6addr")) {
if (fr_is_inaddr_any(&home->ipaddr) == 1) {
cf_log_err_cs(cs, "Wildcard '*' addresses are not permitted for home servers");
goto error;
}
if (!home->log_name) {
char buffer[INET6_ADDRSTRLEN + 3];
fr_ntop(buffer, sizeof(buffer), &home->ipaddr);
home->log_name = talloc_asprintf(home, "%s:%i", buffer, home->port);
}
/*
* If it has a 'virtual_Server' config item, it's
* a loopback into a virtual server.
*/
} else if (cf_pair_find(cs, "virtual_server") != NULL) {
home->ipaddr.af = AF_UNSPEC; /* mark ipaddr as unused */
if (!home->server) {
cf_log_err_cs(cs, "Invalid value for virtual_server");
goto error;
}
/*
* Try and find a 'server' section off the root of
* the config with a name that matches the
* virtual_server.
*/
if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
cf_log_err_cs(cs, "No such server %s", home->server);
goto error;
}
home->secret = "";
home->log_name = talloc_typed_strdup(home, home->server);
/*
* Otherwise it's an invalid config section and we
* raise an error.
*/
} else {
cf_log_err_cs(cs, "No ipaddr, ipv4addr, ipv6addr, or virtual_server defined "
"for home server");
error:
talloc_free(home);
return false;
}
{
home_type_t type = HOME_TYPE_AUTH_ACCT;
if (home->type_str) type = fr_str2int(home_server_types, home->type_str, HOME_TYPE_INVALID);
home->type = type;
switch (type) {
case HOME_TYPE_AUTH_ACCT:
home->dual = true;
break;
case HOME_TYPE_AUTH:
case HOME_TYPE_ACCT:
break;
#ifdef WITH_COA
case HOME_TYPE_COA:
if (home->server != NULL) {
cf_log_err_cs(cs, "Home servers of type \"coa\" cannot point to a virtual server");
goto error;
}
break;
#endif
case HOME_TYPE_INVALID:
cf_log_err_cs(cs, "Invalid type \"%s\" for home server %s", home->type_str, home->log_name);
goto error;
}
}
{
home_ping_check_t type = HOME_PING_CHECK_NONE;
if (home->ping_check_str) type = fr_str2int(home_ping_check, home->ping_check_str,
HOME_PING_CHECK_INVALID);
switch (type) {
case HOME_PING_CHECK_STATUS_SERVER:
case HOME_PING_CHECK_NONE:
break;
case HOME_PING_CHECK_REQUEST:
if (!home->ping_user_name) {
cf_log_err_cs(cs, "You must supply a 'username' to enable status_check=request");
goto error;
}
if ((home->type == HOME_TYPE_AUTH) && !home->ping_user_password) {
cf_log_err_cs(cs, "You must supply a 'password' to enable status_check=request");
goto error;
}
break;
case HOME_PING_CHECK_INVALID:
cf_log_err_cs(cs, "Invalid status_check \"%s\" for home server %s",
home->ping_check_str, home->log_name);
goto error;
}
home->ping_check = type;
}
{
int proto = IPPROTO_UDP;
if (home->proto_str) proto = fr_str2int(fr_net_ip_proto_table, home->proto_str, -1);
switch (proto) {
case IPPROTO_UDP:
home_servers_udp = true;
break;
case IPPROTO_TCP:
#ifndef WITH_TCP
cf_log_err_cs(cs, "Server not built with support for RADIUS over TCP");
goto error;
#endif
if (home->ping_check != HOME_PING_CHECK_NONE) {
cf_log_err_cs(cs, "Only 'status_check = none' is allowed for home "
"servers with 'proto = tcp'");
goto error;
}
break;
default:
cf_log_err_cs(cs, "Unknown proto \"%s\"", home->proto_str);
goto error;
}
home->proto = proto;
}
if (!home->server && rbtree_finddata(home_servers_byaddr, home)) {
cf_log_err_cs(cs, "Duplicate home server");
goto error;
}
/*
* Check the TLS configuration.
*/
tls = cf_section_sub_find(cs, "tls");
#ifndef WITH_TLS
if (tls) {
cf_log_err_cs(cs, "TLS transport is not available in this executable");
goto error;
}
#endif
/*
* If were doing RADSEC (tls+tcp) the secret should default
* to radsec, else a secret must be set.
*/
if (!home->secret) {
#ifdef WITH_TLS
if (tls && (home->proto == IPPROTO_TCP)) {
home->secret = "radsec";
} else
#endif
{
cf_log_err_cs(cs, "No shared secret defined for home server %s", home->log_name);
goto error;
}
}
/*
* Virtual servers have some TLS restrictions.
*/
if (home->server) {
if (tls) {
cf_log_err_cs(cs, "Virtual home_servers cannot have a \"tls\" subsection");
goto error;
}
} else {
/*
* If the home is not a virtual server, guess the port
* and look up the source ip address.
*/
rad_assert(home->ipaddr.af != AF_UNSPEC);
#ifdef WITH_TLS
if (tls && (home->proto != IPPROTO_TCP)) {
cf_log_err_cs(cs, "TLS transport is not available for UDP sockets");
goto error;
}
#endif
/*
* Set the default port if necessary.
*/
if (home->port == 0) {
char buffer[INET6_ADDRSTRLEN + 3];
/*
* For RADSEC we use the special RADIUS over TCP/TLS port
* for both accounting and authentication, but for some
* bizarre reason for RADIUS over plain TCP we use separate
* ports 1812 and 1813.
*/
#ifdef WITH_TLS
if (tls) {
home->port = PW_RADIUS_TLS_PORT;
} else
#endif
switch (home->type) {
default:
rad_assert(0);
/* FALL-THROUGH */
case HOME_TYPE_AUTH:
home->port = PW_AUTH_UDP_PORT;
break;
case HOME_TYPE_ACCT:
home->port = PW_ACCT_UDP_PORT;
break;
case HOME_TYPE_COA:
home->port = PW_COA_UDP_PORT;
break;
}
/*
* Now that we have a real port, use that.
*/
rad_const_free(home->log_name);
fr_ntop(buffer, sizeof(buffer), &home->ipaddr);
home->log_name = talloc_asprintf(home, "%s:%i", buffer, home->port);
}
/*
* If we have a src_ipaddr_str resolve it to
* the same address family as the destination
* IP.
*/
if (home->src_ipaddr_str) {
if (ip_hton(&home->src_ipaddr, home->ipaddr.af, home->src_ipaddr_str, false) < 0) {
cf_log_err_cs(cs, "Failed parsing src_ipaddr");
goto error;
}
/*
* Source isn't specified, set it to the
* correct address family, but leave it as
* zeroes.
*/
} else {
home->src_ipaddr.af = home->ipaddr.af;
}
#ifdef WITH_TLS
/*
* Parse the SSL client configuration.
*/
if (tls) {
home->tls = tls_client_conf_parse(tls);
if (!home->tls) {
goto error;
}
}
#endif
} /* end of parse home server */
realm_home_server_sanitize(home, cs);
return home;
}
/** Fixup a client configuration section to specify a home server
*
* This is used to create the equivalent CoA home server entry for a client,
* so that the server can originate CoA messages.
*
* The server section automatically inherits the following fields from the client:
* - ipaddr/ipv4addr/ipv6addr
* - secret
* - src_ipaddr
*
* @note new CONF_SECTION will be allocated in the context of the client, but the client
* C#ONF_SECTION will not be modified.
*
* @param client CONF_SECTION to inherit values from.
* @return
* - A new server #CONF_SECTION.
* - A pointer to the existing #CONF_SECTION in the client.
*/
CONF_SECTION *home_server_cs_afrom_client(CONF_SECTION *client)
{
CONF_SECTION *server, *cs;
CONF_PAIR *cp;
/*
* Alloc a plain home server for both cases
*
* There's no way these can be referenced by a pool,
* and they may conflict with home servers in proxy.conf
* so it's easier to not set a name.
*/
/*
*
* Duplicate the server section, so we don't mangle
* the client CONF_SECTION we were passed.
*/
cs = cf_section_sub_find(client, "coa_server");
if (cs) {
server = cf_section_dup(client, cs, "home_server", NULL, true);
} else {
server = cf_section_alloc(client, "home_server", NULL);
}
if (!cs || (!cf_pair_find(cs, "ipaddr") && !cf_pair_find(cs, "ipv4addr") && !cf_pair_find(cs, "ipv6addr"))) {
cp = cf_pair_find(client, "ipaddr");
if (!cp) cp = cf_pair_find(client, "ipv4addr");
if (!cp) cp = cf_pair_find(client, "ipv6addr");
cf_pair_add(server, cf_pair_dup(server, cp));
}
if (!cs || !cf_pair_find(cs, "secret")) {
cp = cf_pair_find(client, "secret");
if (cp) cf_pair_add(server, cp);
}
if (!cs || !cf_pair_find(cs, "src_ipaddr")) {
cp = cf_pair_find(client, "src_ipaddr");
if (cp) cf_pair_add(server, cf_pair_dup(server, cp));
}
if (!cs || !(cp = cf_pair_find(cs, "type"))) {
cp = cf_pair_alloc(server, "type", "coa", T_OP_EQ, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
if (cp) cf_pair_add(server, cf_pair_dup(server, cp));
} else if (strcmp(cf_pair_value(cp), "coa") != 0) {
talloc_free(server);
cf_log_err_cs(server, "server.type must be \"coa\"");
return NULL;
}
return server;
}
static home_pool_t *server_pool_alloc(realm_config_t *rc, char const *name, home_pool_type_t type,
home_type_t server_type, int num_home_servers)
{
home_pool_t *pool;
/*
* FIXME: Move to two separate structs, which allows
* for re-alloc.
*/
pool = talloc_size(rc, sizeof(*pool) + (sizeof(pool->servers[0]) * num_home_servers));
if (!pool) return NULL; /* just for pairanoia */
memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) * num_home_servers));
pool->name = name;
pool->type = type;
pool->server_type = server_type;
pool->num_home_servers = num_home_servers;
return pool;
}
/*
* Ensure any home_server clauses in a home_server_pool section reference
* defined home servers, which should already have been created, regardless
* of where they appear in the configuration.
*/
static int pool_check_home_server(UNUSED realm_config_t *rc, CONF_PAIR *cp,
char const *name, home_type_t server_type,
home_server_t **phome)
{
home_server_t myhome, *home;
if (!name) {
cf_log_err_cp(cp,
"No value given for home_server");
return 0;
}
myhome.name = name;
myhome.type = server_type;
home = rbtree_finddata(home_servers_byname, &myhome);
if (home) {
*phome = home;
return 1;
}
switch (server_type) {
case HOME_TYPE_AUTH:
case HOME_TYPE_ACCT:
myhome.type = HOME_TYPE_AUTH_ACCT;
home = rbtree_finddata(home_servers_byname, &myhome);
if (home) {
*phome = home;
return 1;
}
break;
default: