File: config.c

package info (click to toggle)
radcli 1.2.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,768 kB
  • sloc: ansic: 6,089; sh: 767; makefile: 190; perl: 110
file content (1168 lines) | stat: -rw-r--r-- 29,932 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
/*
 * Copyright (C) 1995,1996,1997 Lars Fenneberg
 *
 * Copyright 1992 Livingston Enterprises, Inc.
 *
 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
 * and Merit Network, Inc. All Rights Reserved
 *
 * See the file COPYRIGHT for the respective terms and conditions.
 * If the file is missing contact me at lf@elemental.net
 * and I'll send you a copy.
 *
 */

/**
 * @defgroup radcli-api Main API
 * @brief Main API Functions
 *
 * @{
 */

#include <config.h>
#include <includes.h>
#include <radcli/radcli.h>
#include <options.h>
#include "util.h"
#include "tls.h"

#ifndef TRUE
#define TRUE  1
#define FALSE 0
#endif

static int rc_conf_int_2(rc_handle const *rh, char const *optname, int complain);

/** Find an option in the option list
 *
 * @param rh a handle to parsed configuration.
 * @param optname the name of the option.
 * @param type the option type.
 * @return pointer to option on success, NULL otherwise.
 */
static OPTION *find_option(rc_handle const *rh, char const *optname, unsigned int type)
{
	int 	i;

	/* there're so few options that a binary search seems not necessary */
	for (i = 0; i < NUM_OPTIONS; i++) {
		if (!strcmp(rh->config_options[i].name, optname) &&
		    (rh->config_options[i].type & type))
		{
		    	return &rh->config_options[i];
		}
	}

	return NULL;
}

/** Set a specific option doing type conversions
 *
 * @param filename the name of the config file (for logging purposes).
 * @param line the line number in the file.
 * @param option option to set.
 * @param p Value.
 * @return 0 on success, -1 on failure.
 */
static int set_option_str(char const *filename, int line, OPTION *option, char const *p)
{
	if (p) {
		option->val = (void *) strdup(p);
		if (option->val == NULL) {
			rc_log(LOG_CRIT, "read_config: out of memory");
			return -1;
		}
	} else {
		option->val = NULL;
	}

	return 0;
}

static int set_option_int(char const *filename, int line, OPTION *option, char const *p)
{
	int *iptr;

	if (p == NULL) {
		rc_log(LOG_ERR, "%s: line %d: bogus option value", filename, line);
		return -1;
	}

	if ((iptr = malloc(sizeof(*iptr))) == NULL) {
		rc_log(LOG_CRIT, "read_config: out of memory");
		return -1;
	}

	*iptr = atoi(p);
	option->val = (void *) iptr;

	return 0;
}

static int set_option_srv(char const *filename, int line, OPTION *option, char const *p)
{
	SERVER *serv;
	char *p_pointer;
	char *p_dupe;
	char *p_save;
	char *q;
	char *s;
	struct servent *svp;

	p_dupe = strdup(p);

	if (p_dupe == NULL) {
		rc_log(LOG_ERR, "%s: line %d: Invalid option or memory failure", filename, line);
		return -1;
	}

	serv = (SERVER *) option->val;
	if (serv == NULL) {
		serv = calloc(1, sizeof(*serv));
		if (serv == NULL) {
			rc_log(LOG_CRIT, "read_config: out of memory");
			free(p_dupe);
			return -1;
		}
		serv->max = 0;
	}

	p_pointer = strtok_r(p_dupe, ", \t", &p_save);

        while(p_pointer != NULL) {
                if (serv->max > SERVER_MAX) {
                        DEBUG(LOG_ERR, "cannot set more than %d servers", SERVER_MAX);
                        return -1;
                }

		DEBUG(LOG_ERR, "processing server: %s", p_pointer);
                /* check to see for '[IPv6]:port' syntax */
                if ((q = strchr(p_pointer,'[')) != NULL) {
                        *q = '\0';
                        q++;
                        p_pointer = q;

                        q = strchr(p_pointer, ']');
                        if (q == NULL) {
                                free(p_dupe);
                                rc_log(LOG_CRIT, "read_config: IPv6 parse error");
                                return -1;
                        }
                        *q = '\0';
                        q++;

                        if (q[0] == ':') {
                                q++;
                        }

                        /* Check to see if we have '[IPv6]:port:secret' syntax */
                        if((s=strchr(q, ':')) != NULL) {
                                *s = '\0';
                                s++;
                                serv->secret[serv->max] = strdup(s);
                                if (serv->secret[serv->max] == NULL) {
                                        rc_log(LOG_CRIT, "read_config: out of memory");
                                        if (option->val == NULL) {
                                                free(p_dupe);
                                                free(serv);
                                        }
                                        return -1;
                                }
                        }

                } else /* Check to see if we have 'servername:port' syntax */
                        if ((q = strchr(p_pointer,':')) != NULL) {
                                *q = '\0';
                                q++;

                                /* Check to see if we have 'servername:port:secret' syntax */
                                if((s = strchr(q,':')) != NULL) {
                                        *s = '\0';
                                        s++;
                                        serv->secret[serv->max] = strdup(s);
                                        if (serv->secret[serv->max] == NULL) {
                                                rc_log(LOG_CRIT, "read_config: out of memory");
                                                if (option->val == NULL) {
                                                        free(p_dupe);
                                                        free(serv);
                                                }
                                                return -1;
                                        }
                                }
                        }

                if(q && strlen(q) > 0) {
                        serv->port[serv->max] = atoi(q);
                } else {
                        if (!strcmp(option->name,"authserver"))
                                if ((svp = getservbyname ("radius", "udp")) == NULL)
                                        serv->port[serv->max] = PW_AUTH_UDP_PORT;
                                else
                                        serv->port[serv->max] = ntohs ((unsigned int) svp->s_port);
                        else if (!strcmp(option->name, "acctserver"))
                                if ((svp = getservbyname ("radacct", "udp")) == NULL)
                                        serv->port[serv->max] = PW_ACCT_UDP_PORT;
                                else
                                        serv->port[serv->max] = ntohs ((unsigned int) svp->s_port);
                        else {
                                rc_log(LOG_ERR, "%s: line %d: no default port for %s", filename, line, option->name);
                                if (option->val == NULL) {
                                        free(p_dupe);
                                        free(serv);
                                }
                                return -1;
                        }
                }

                serv->name[serv->max] = strdup(p_pointer);
                if (serv->name[serv->max] == NULL) {
                        rc_log(LOG_CRIT, "read_config: out of memory");
                        if (option->val == NULL) {
                                free(p_dupe);
                                free(serv);
                        }
                        return -1;
                }

                serv->max++;
                p_pointer = strtok_r(NULL, ", \t", &p_save);
        }

        free(p_dupe);
	if (option->val == NULL)
		option->val = (void *)serv;

	return 0;
}

static int set_option_auo(char const *filename, int line, OPTION *option, char const *p)
{
	int *iptr;
	char *p_dupe = NULL;
	char *p_pointer = NULL;
	char *p_save = NULL;

	p_dupe = strdup(p);

	if (p_dupe == NULL) {
		rc_log(LOG_WARNING, "%s: line %d: bogus option value", filename, line);
		return -1;
	}

	if ((iptr = malloc(sizeof(*iptr))) == NULL) {
			rc_log(LOG_CRIT, "read_config: out of memory");
			free(p_dupe);
			return -1;
	}

	*iptr = 0;
	p_pointer = strtok_r(p_dupe, ", \t", &p_save);

	if (!strncmp(p_pointer, "local", 5))
			*iptr = AUTH_LOCAL_FST;
	else if (!strncmp(p_pointer, "radius", 6))
			*iptr = AUTH_RADIUS_FST;
	else {
		rc_log(LOG_ERR,"%s: auth_order: unknown keyword: %s", filename, p);
		free(iptr);
		free(p_dupe);
		return -1;
	}

	p_pointer = strtok_r(NULL, ", \t", &p_save);

	if (p_pointer && (*p_pointer != '\0')) {
		if ((*iptr & AUTH_RADIUS_FST) && !strcmp(p_pointer, "local"))
			*iptr = (*iptr) | AUTH_LOCAL_SND;
		else if ((*iptr & AUTH_LOCAL_FST) && !strcmp(p_pointer, "radius"))
			*iptr = (*iptr) | AUTH_RADIUS_SND;
		else {
			rc_log(LOG_ERR,"%s: auth_order: unknown or unexpected keyword: %s", filename, p);
			free(iptr);
			free(p_dupe);
			return -1;
		}
	}

	option->val = (void *) iptr;

	free(p_dupe);
	return 0;
}

/** Allow a config option to be added to rc_handle from inside a program.
 *
 * That allows programs to setup a handle without loading a configuration
 * file.
 *
 * @param rh a handle to parsed configuration.
 * @param option_name the name of the option.
 * @param option_val the value to be added.
 * @param source typically should be __FILE__ or __func__ for logging purposes.
 * @param line __LINE__ for logging purposes.
 * @return 0 on success, -1 on failure.
 */
int rc_add_config(rc_handle *rh, char const *option_name, char const *option_val, char const *source, int line)
{
	OPTION *option;

	if ((option = find_option(rh, option_name, OT_ANY)) == NULL)
	{
		rc_log(LOG_ERR, "ERROR: unrecognized option: %s", option_name);
		return -1;
	}

	if (option->status != ST_UNDEF)
	{
		rc_log(LOG_ERR, "ERROR: duplicate option: %s", option_name);
		return -1;
	}

	switch (option->type) {
		case OT_STR:
			if (set_option_str(source, line, option, option_val) < 0) {
				return -1;
			}
			break;
		case OT_INT:
			if (set_option_int(source, line, option, option_val) < 0) {
				return -1;
			}
			break;
		case OT_SRV:
			if (set_option_srv(source, line, option, option_val) < 0) {
				return -1;
			}
			break;
		case OT_AUO:
			if (set_option_auo(source, line, option, option_val) < 0) {
				return -1;
			}
			break;
		default:
			rc_log(LOG_CRIT, "rc_add_config: impossible case branch!");
			abort();
	}

	return 0;
}

/** Initialise a configuration structure
 *
 * Initialize the configuration structure from an external program.  For use when not
 * running a standalone client that reads from a config file.
 *
 * The provided handled must have been allocated using rc_new().
 *
 * @param rh a handle to parsed configuration.
 * @return rc_handle on success, NULL on failure.
 */
rc_handle *rc_config_init(rc_handle *rh)
{
	SERVER *authservers = NULL;
	SERVER *acctservers;
	OPTION *acct;
	OPTION *auth;

        rh->config_options = malloc(sizeof(config_options_default));
        if (rh->config_options == NULL)
	{
                rc_log(LOG_CRIT, "rc_config_init: out of memory");
		rc_destroy(rh);
                return NULL;
        }
        memcpy(rh->config_options, &config_options_default, sizeof(config_options_default));

	auth = find_option(rh, "authserver", OT_ANY);
	if (auth) {
		authservers = calloc(1, sizeof(SERVER));
		if(authservers == NULL) {
	                rc_log(LOG_CRIT, "rc_config_init: error initializing server structs");
			rc_destroy(rh);
	                return NULL;
		}
		auth->val = authservers;
	}

	acct = find_option(rh, "acctserver", OT_ANY);
	if (acct) {
		acctservers = calloc(1, sizeof(SERVER));
		if(acctservers == NULL) {
	                rc_log(LOG_CRIT, "rc_config_init: error initializing server structs");
			rc_destroy(rh);
			if(authservers) free(authservers);
	                return NULL;
		}
		acct->val = acctservers;
	}

	return rh;
}

static ssize_t plain_sendto(void *ptr, int sockfd,
			    const void *buf, size_t len, int flags,
			    const struct sockaddr *dest_addr, socklen_t addrlen)
{
	return sendto(sockfd, buf, len, flags, dest_addr, addrlen);
}

static ssize_t plain_tcp_sendto(void *ptr, int sockfd,
			    const void *buf, size_t len, int flags,
			    const struct sockaddr *dest_addr, socklen_t addrlen)
{
	if((connect(sockfd, dest_addr, addrlen)) != 0){
		rc_log(LOG_ERR, "%s: Connect Call Failed : %s", __FUNCTION__, strerror(errno));
		return -1;
	}
	return sendto(sockfd, buf, len, flags, dest_addr, addrlen);
}

static ssize_t plain_recvfrom(void *ptr, int sockfd,
			      void *buf, size_t len, int flags,
			      struct sockaddr *src_addr, socklen_t * addrlen)
{
	return recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
}

static void plain_close_fd(int fd)
{
	close(fd);
}

static int plain_get_fd(void *ptr, struct sockaddr *our_sockaddr)
{
	int sockfd;

	sockfd = socket(our_sockaddr->sa_family, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		return -1;
	}

	if (our_sockaddr->sa_family == AF_INET)
		((struct sockaddr_in *)our_sockaddr)->sin_port = 0;
	else
		((struct sockaddr_in6 *)our_sockaddr)->sin6_port = 0;

	if (bind(sockfd, SA(our_sockaddr), SA_LEN(our_sockaddr)) < 0) {
		close(sockfd);
		return -1;
	}
	return sockfd;
}

static int plain_tcp_get_fd(void *ptr, struct sockaddr *our_sockaddr)
{
	int sockfd;

	sockfd = socket(our_sockaddr->sa_family, SOCK_STREAM, 0);
	if (sockfd < 0) {
		return -1;
	}

	if (our_sockaddr->sa_family == AF_INET)
		((struct sockaddr_in *)our_sockaddr)->sin_port = 0;
	else
		((struct sockaddr_in6 *)our_sockaddr)->sin6_port = 0;

	if (bind(sockfd, SA(our_sockaddr), SA_LEN(our_sockaddr)) < 0) {
		close(sockfd);
		return -1;
	}
	return sockfd;
}

const static rc_sockets_override default_socket_funcs = {
	.get_fd = plain_get_fd,
	.close_fd = plain_close_fd,
	.sendto = plain_sendto,
	.recvfrom = plain_recvfrom
};

const static rc_sockets_override default_tcp_socket_funcs = {
	.get_fd = plain_tcp_get_fd,
	.close_fd = plain_close_fd,
	.sendto = plain_tcp_sendto,
	.recvfrom = plain_recvfrom
};

static int set_addr(struct sockaddr_storage *ss, const char *ip)
{
	memset(ss, 0, sizeof(*ss));
	if (inet_pton(AF_INET, ip, &((struct sockaddr_in *)ss)->sin_addr) == 1) {
		ss->ss_family = AF_INET;
	} else if (inet_pton(AF_INET6, ip, &((struct sockaddr_in6 *)ss)->sin6_addr) == 1) {
		ss->ss_family = AF_INET6;
	} else {
		rc_log(LOG_CRIT, "invalid IP address for nas-ip: %s", ip);
		return -1;
	}
	return 0;
}

/** Applies and initializes any parameters from the radcli configuration
 *
 * When no configuration file is provided and the configuration
 * is provided via rc_add_config(), radcli requires the call of this function
 * in order to initialize items for the connection.
 *
 * @param rh a handle to parsed configuration.
 * @return 0 on success, -1 when failure.
 */
int rc_apply_config(rc_handle *rh)
{
	const char *txt;
	int ret;

	memset(&rh->own_bind_addr, 0, sizeof(rh->own_bind_addr));
	rh->own_bind_addr_set = 0;
	rc_own_bind_addr(rh, &rh->own_bind_addr);
	rh->own_bind_addr_set = 1;

	txt = rc_conf_str(rh, "nas-ip");
	if (txt != NULL) {
		if (set_addr(&rh->nas_addr, txt) < 0)
			return -1;
		rh->nas_addr_set = 1;
	}

	txt = rc_conf_str(rh, "serv-type");
	if (txt == NULL)
		txt = rc_conf_str(rh, "serv-auth-type");

	if (txt == NULL)
		txt = "udp";

	if (strcasecmp(txt, "udp") == 0) {
		memset(&rh->so, 0, sizeof(rh->so));
		rh->so_type = RC_SOCKET_UDP;
		memcpy(&rh->so, &default_socket_funcs, sizeof(rh->so));
		ret = 0;
	} else if (strcasecmp(txt, "tcp") == 0) {
		memset(&rh->so, 0, sizeof(rh->so));
		rh->so_type = RC_SOCKET_TCP;
		memcpy(&rh->so, &default_tcp_socket_funcs, sizeof(rh->so));
		ret = 0;
#ifdef HAVE_GNUTLS
	} else if (strcasecmp(txt, "dtls") == 0) {
		ret = rc_init_tls(rh, SEC_FLAG_DTLS);
	} else if (strcasecmp(txt, "tls") == 0) {
		ret = rc_init_tls(rh, 0);
#endif
	} else {
		rc_log(LOG_CRIT, "unknown server type: %s", txt);
		return -1;
	}

	if (ret < 0) {
		rc_log(LOG_CRIT, "error initializing %s", txt);
		return -1;
	}

	return 0;

}

/** Read the global config file
 *
 * This function will load the provided configuration file, and
 * any other files such as the dictionary. This is the most common
 * mode of use of this library. The configuration format is compatible
 * with the radiusclient-ng and freeradius-client formats.
 *
 * Note: To preserve compatibility with libraries of the same API
 * which don't load the dictionary care is taken not to reload the
 * same filename twice even if instructed to.
 *
 * @param filename a name of a file.
 * @return new rc_handle on success, NULL when failure.
 */
rc_handle *rc_read_config(char const *filename)
{
	FILE *configfd;
	char buffer[512], *p;
	OPTION *option;
	int line;
	size_t pos;
	rc_handle *rh;


	rh = rc_new();
	if (rh == NULL)
		return NULL;

        rh->config_options = malloc(sizeof(config_options_default));
        if (rh->config_options == NULL) {
                rc_log(LOG_CRIT, "rc_read_config: out of memory");
		rc_destroy(rh);
                return NULL;
        }
        memcpy(rh->config_options, &config_options_default, sizeof(config_options_default));

	if ((configfd = fopen(filename,"r")) == NULL)
	{
		rc_log(LOG_ERR,"rc_read_config: can't open %s: %s", filename, strerror(errno));
		rc_destroy(rh);
		return NULL;
	}

	line = 0;
	while ((fgets(buffer, sizeof(buffer), configfd) != NULL))
	{
		line++;
		p = buffer;

		if ((*p == '\n') || (*p == '#') || (*p == '\0'))
			continue;

		p[strlen(p)-1] = '\0';


		if ((pos = strcspn(p, "\t ")) == 0) {
			rc_log(LOG_ERR, "%s: line %d: bogus format: %s", filename, line, p);
			fclose(configfd);
			rc_destroy(rh);
			return NULL;
		}

		p[pos] = '\0';

		if ((option = find_option(rh, p, OT_ANY)) == NULL) {
			rc_log(LOG_ERR, "%s: line %d: unrecognized keyword: %s", filename, line, p);
			fclose(configfd);
			rc_destroy(rh);
			return NULL;
		}

		if (option->status != ST_UNDEF) {
			rc_log(LOG_ERR, "%s: line %d: duplicate option line: %s", filename, line, p);
			fclose(configfd);
			rc_destroy(rh);
			return NULL;
		}

		p += pos+1;
		while (isspace(*p))
			p++;
		pos = strlen(p) - 1;
		while(pos != 0 && isspace(p[pos]))
			pos--;
		p[pos + 1] = '\0';

		switch (option->type) {
			case OT_STR:
				if (set_option_str(filename, line, option, p) < 0) {
					fclose(configfd);
					rc_destroy(rh);
				 	return NULL;
				}
				break;
			case OT_INT:
				if (set_option_int(filename, line, option, p) < 0) {
					fclose(configfd);
					rc_destroy(rh);
				 	return NULL;
				}
				break;
			case OT_SRV:
				if (set_option_srv(filename, line, option, p) < 0) {
					fclose(configfd);
					rc_destroy(rh);
				 	return NULL;
				}
				break;
			case OT_AUO:
				if (set_option_auo(filename, line, option, p) < 0) {
					fclose(configfd);
					rc_destroy(rh);
				 	return NULL;
				}
				break;
			default:
				rc_log(LOG_CRIT, "rc_read_config: impossible case branch!");
				abort();
		}
	}
	fclose(configfd);

	if (rc_test_config(rh, filename) == -1) {
		rc_destroy(rh);
		return NULL;
	}

        {
                int clientdebug = rc_conf_int_2(rh, "clientdebug", FALSE);
                if(clientdebug > 0) {
                        radcli_debug = clientdebug;
                }
        }

	p = rc_conf_str(rh, "dictionary");
	if (p != NULL) {
		if (rc_read_dictionary(rh, p) != 0) {
			rc_log(LOG_CRIT, "could not load dictionary");
			rc_destroy(rh);
			return NULL;
		}
	} else {
		rc_log(LOG_INFO, "rc_read_config: no dictionary was specified");
	}

	return rh;
}

/** Get the value of a config option
 *
 * @param rh a handle to parsed configuration.
 * @param optname the name of an option.
 * @return config option value.
 */
char *rc_conf_str(rc_handle const *rh, char const *optname)
{
	OPTION *option;

	option = find_option(rh, optname, OT_STR);

	if (option != NULL) {
		return (char *)option->val;
	} else {
		rc_log(LOG_CRIT, "rc_conf_str: unkown config option requested: %s", optname);
		return NULL;
	}
}

/*- Get the value of a config option
 *
 * @param rh a handle to parsed configuration.
 * @param optname the name of an option.
 * @return config option value.
 */
static int rc_conf_int_2(rc_handle const *rh, char const *optname, int complain)
{
	OPTION *option;

	option = find_option(rh, optname, OT_INT|OT_AUO);

	if (option != NULL) {
		if (option->val) {
			return *((int *)option->val);
		} else if(complain) {
			rc_log(LOG_ERR, "rc_conf_int: config option %s was not set", optname);
		}
                return 0;
	} else {
		rc_log(LOG_CRIT, "rc_conf_int: unkown config option requested: %s", optname);
		return 0;
	}
}

int rc_conf_int(rc_handle const *rh, char const *optname)
{
        return rc_conf_int_2(rh, optname, TRUE);
}

/** Get the value of a config option
 *
 * @param rh a handle to parsed configuration.
 * @param optname the name of an option.
 * @return config option value.
 */
SERVER *rc_conf_srv(rc_handle const *rh, char const *optname)
{
	OPTION *option;

	option = find_option(rh, optname, OT_SRV);

	if (option != NULL) {
		return (SERVER *)option->val;
	} else {
		rc_log(LOG_CRIT, "rc_conf_srv: unkown config option requested: %s", optname);
		return NULL;
	}
}

/** Tests the configuration the user supplied
 *
 * @param rh a handle to parsed configuration.
 * @param filename a name of a configuration file.
 * @return 0 on success, -1 when failure.
 */
int rc_test_config(rc_handle *rh, char const *filename)
{
	SERVER *srv;

	srv = rc_conf_srv(rh, "authserver");
	if (!srv || !srv->max)
	{
		rc_log(LOG_ERR,"%s: no authserver specified", filename);
		return -1;
	}

	srv = rc_conf_srv(rh, "acctserver");
	if (!srv || !srv->max)
	{
		/* it is allowed not to have acct servers */
		if (rh->so_type != RC_SOCKET_TLS && rh->so_type != RC_SOCKET_DTLS)
			rc_log(LOG_DEBUG,"%s: no acctserver specified", filename);
	}
	if (!rc_conf_str(rh, "dictionary"))
	{
		rc_log(LOG_ERR,"%s: no dictionary specified", filename);
		return -1;
	}

	if (rc_conf_int(rh, "radius_timeout") <= 0)
	{
		rc_log(LOG_ERR,"%s: radius_timeout <= 0 is illegal", filename);
		return -1;
	}
	if (rc_conf_int(rh, "radius_retries") <= 0)
	{
		rc_log(LOG_ERR,"%s: radius_retries <= 0 is illegal", filename);
		return -1;
	}

	if (rc_apply_config(rh) == -1) {
		return -1;
	}

	return 0;
}

/** See if info matches hostname
 *
 * @param addr a struct addrinfo
 * @param hostname the name of the host.
 * @return 0 on success, -1 when failure.
 */
static int find_match (const struct addrinfo* addr, const struct addrinfo *hostname)
{
	const struct addrinfo *ptr, *ptr2;
	unsigned len1, len2;

	ptr = addr;
	while(ptr) {
		ptr2 = hostname;
		while(ptr2) {
			len1 = SA_GET_INLEN(ptr->ai_addr);
			len2 = SA_GET_INLEN(ptr2->ai_addr);

			if (len1 > 0 &&
			    len1 == len2 &&
			    memcmp(SA_GET_INADDR(ptr->ai_addr), SA_GET_INADDR(ptr2->ai_addr), len1) == 0) {
				return 0;
			}
			ptr2 = ptr2->ai_next;
 		}
		ptr = ptr->ai_next;
 	}
 	return -1;
}

/** Checks if provided address is local address
 *
 * @param addr an %AF_INET or %AF_INET6 address
 * @return 0 if local, 1 if not local, -1 on failure.
 */
static int rc_ipaddr_local(const struct sockaddr *addr)
{
	int temp_sock, res, serrno;
	struct sockaddr_storage tmpaddr;

	memcpy(&tmpaddr, addr, SA_LEN(addr));

	temp_sock = socket(addr->sa_family, SOCK_DGRAM, 0);
	if (temp_sock == -1)
		return -1;

	if (addr->sa_family == AF_INET) {
		((struct sockaddr_in*)&tmpaddr)->sin_port = 0;
	} else {
		((struct sockaddr_in6*)&tmpaddr)->sin6_port = 0;
	}
	res = bind(temp_sock, SA(&tmpaddr), SS_LEN(&tmpaddr));
	serrno = errno;
	close(temp_sock);
	if (res == 0)
		return 0;
	if (serrno == EADDRNOTAVAIL)
		return 1;
	return -1;
}

/** Checks if provided name refers to ourselves
 *
 * @param info an addrinfo of the host to check
 * @return 0 if yes, 1 if no and -1 on failure.
 */
static int rc_is_myname(const struct addrinfo *info)
{
	const struct addrinfo *p;
	int	res;

	p = info;
	while(p != NULL) {
		res = rc_ipaddr_local(p->ai_addr);
		if (res == 0 || res == -1) {
 			return res;
		}
		p = p->ai_next;
 	}
 	return 1;
}

/** Locate a server in the rh config or if not found, check for a servers file
 *
 * @param rh a handle to parsed configuration.
 * @param server_name the name of the server.
 * @param info: will hold a pointer to addrinfo
 * @param secret will hold the server's secret (of %MAX_SECRET_LENGTH).
 * @param type %AUTH or %ACCT

 * @return 0 on success, -1 on failure.
 */
int rc_find_server_addr (rc_handle const *rh, char const *server_name,
                         struct addrinfo** info, char *secret, rc_type type)
{
	int             result = 0;
	FILE           *clientfd;
	char           *h;
	char           *s;
	char            buffer[128];
	char            hostnm[AUTH_ID_LEN + 1];
	char	       *buffer_save;
	char	       *hostnm_save;
	SERVER	       *servers;
	struct addrinfo *tmpinfo = NULL;
	const char      *fservers;
	char const      *optname;

	/* Lookup the IP address of the radius server */
	if ((*info = rc_getaddrinfo (server_name, type==AUTH?PW_AI_AUTH:PW_AI_ACCT)) == NULL)
		return -1;

	switch (type)
	{
	case AUTH: optname = "authserver"; break;
	case ACCT: optname = "acctserver"; break;
	default:   optname = NULL;
	}

	if ( (optname != NULL) &&
	     ((servers = rc_conf_srv(rh, optname)) != NULL) )
	{
		/* Check to see if the server secret is defined in the rh config */
		unsigned  servernum;
		size_t    server_name_len = strlen(server_name);
		for (servernum = 0; servernum < servers->max; servernum++)
		{
			if( (strncmp(server_name, servers->name[servernum], server_name_len) == 0) &&
				(servers->secret[servernum] != NULL) )
			{
				memset(secret, '\0', MAX_SECRET_LENGTH);
				strlcpy(secret, servers->secret[servernum], MAX_SECRET_LENGTH);
				return 0;
			}
		}
	}

	/* We didn't find it in the rh_config or the servername is too long so look for a
	 * servers file to define the secret(s)
	 */

	fservers = rc_conf_str(rh, "servers");
	if (fservers != NULL) {
		if ((clientfd = fopen (fservers, "r")) == NULL)
		{
			rc_log(LOG_ERR, "rc_find_server: couldn't open file: %s: %s", strerror(errno), rc_conf_str(rh, "servers"));
			goto fail;
		}

		while (fgets (buffer, sizeof (buffer), clientfd) != NULL)
		{
			if (*buffer == '#')
				continue;

			if ((h = strtok_r(buffer, " \t\n", &buffer_save)) == NULL) /* first hostname */
				continue;

			strlcpy (hostnm, h, AUTH_ID_LEN);

			if ((s = strtok_r (NULL, " \t\n", &buffer_save)) == NULL) /* and secret field */
				continue;

			strlcpy (secret, s, MAX_SECRET_LENGTH);

			if (!strchr (hostnm, '/')) /* If single name form */
			{
				tmpinfo = rc_getaddrinfo(hostnm, 0);
				if (tmpinfo)
				{
					result = find_match (*info, tmpinfo);
					if (result == 0)
					{
						result++;
						break;
					}

					freeaddrinfo(tmpinfo);
					tmpinfo = NULL;
				}
			}
			else /* <name1>/<name2> "paired" form */
			{
				strtok_r(hostnm, "/", &hostnm_save);
				tmpinfo = rc_getaddrinfo(hostnm, 0);
				if (tmpinfo)
 				{
					if (rc_is_myname(tmpinfo) == 0)
					{	     /* If we're the 1st name, target is 2nd */
						if (find_match (*info, tmpinfo) == 0)
						{
							result++;
							break;
						}
					}
					else	/* If we were 2nd name, target is 1st name */
 					{
						if (find_match (*info, tmpinfo) == 0)
						{
							result++;
							break;
						}
 					}
					freeaddrinfo(tmpinfo);
					tmpinfo = NULL;
 				}
			}
		}
		fclose (clientfd);
	}
	if (result == 0)
	{
		memset (buffer, '\0', sizeof (buffer));
		memset (secret, '\0', MAX_SECRET_LENGTH);
		rc_log(LOG_ERR, "rc_find_server: couldn't find RADIUS server %s in %s",
			 server_name, rc_conf_str(rh, "servers"));
		goto fail;
	}

	result = 0;
	goto cleanup;

 fail:
 	freeaddrinfo(*info);
 	result = -1;

 cleanup:
 	if (tmpinfo)
 		freeaddrinfo(tmpinfo);

	return result;
}

/**
 * rc_config_free:
 * @param rh a handle to parsed configuration
 *
 * Free allocated config values. For legacy compatibility
 * reasons this will not release any dictionary entries.
 * To release all memory from the handle use rc_destroy()
 * instead.
 *
 */
void rc_config_free(rc_handle *rh)
{
	int i;
	SERVER *serv;

	if (rh->config_options == NULL)
		return;

	for (i = 0; i < NUM_OPTIONS; i++) {
		if (rh->config_options[i].val == NULL)
			continue;
		if (rh->config_options[i].type == OT_SRV) {
		        serv = (SERVER *)rh->config_options[i].val;
			free(serv->name[0]);
			if(serv->secret[0]) free(serv->secret[0]);
			free(serv);
		} else {
			free(rh->config_options[i].val);
		}
	}
	free(rh->config_options);
	free(rh->first_dict_read);
	rh->config_options = NULL;
	rh->first_dict_read = NULL;
}

static int _initialized = 0;

/** Initialises new Radius Client handle
 *
 * @return a new rc_handle (free with rc_destroy).
 */
rc_handle *rc_new(void)
{
	rc_handle *rh;

	if (_initialized == 0) {
#if defined(HAVE_GNUTLS) && GNUTLS_VERSION_NUMBER < 0x030300
		int ret;
		ret = gnutls_global_init();
		if (ret < 0) {
			rc_log(LOG_ERR,
			       "%s: error initializing gnutls: %s",
			       __func__, gnutls_strerror(ret));
			return NULL;
		}
#endif
		srandom((unsigned int)(time(NULL)+getpid()));
	}
	_initialized++;

	rh = calloc(1, sizeof(*rh));
	if (rh == NULL) {
                rc_log(LOG_CRIT, "rc_new: out of memory");
                return NULL;
        }
	return rh;
}

/** Destroys Radius Client handle reclaiming all memory
 *
 * @param rh The Radius client handle to free.
 */
void rc_destroy(rc_handle *rh)
{
	rc_dict_free(rh);
	rc_config_free(rh);
	free(rh);

#if defined(HAVE_GNUTLS) && GNUTLS_VERSION_NUMBER < 0x030300
	_initialized--;
	if (_initialized == 0) {
		gnutls_global_deinit();
	}
#endif
}

/** Returns the type of the socket used
 *
 * That indicates the type of connection used with the radius
 * server, and can be UDP, TLS or DTLS.
 *
 * @return the type of the socket
 */
rc_socket_type rc_get_socket_type(rc_handle *rh)
{
	return rh->so_type;
}

/** @} */
 /*
 * Local Variables:
 * c-basic-offset:8
 * c-style: whitesmith
 * End:
 */