-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsrv_conf.c
619 lines (464 loc) · 12.7 KB
/
srv_conf.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
/*
* srv_conf.c
*
* Routines to process the krb525 configuration files and check on the
* legality of requests.
*
* $Id: srv_conf.c,v 1.2 2012/05/16 13:25:45 kouril Exp $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if defined(HAVE_REGCOMP) && defined(HAVE_REGEX_H)
#include <regex.h>
#elif defined(HAVE_COMPILE) && defined(HAVE_REGEXPR_H)
#include <regexpr.h>
#else
#define NO_REGEX_SUPPORT
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "srv_conf.h"
#include "parse_conf.h"
#include "server.h"
#include "capability.h"
char srv_conf_error[255] = "No error";
#define BUFFER_SIZE 256
static pconf_entry *find_string_in_list(pconf_entry *, char *);
static pconf_entry *find_string_in_regex_list(pconf_entry *, char *);
static pconf_entry *find_princ_in_regex_list(krb5_context, pconf_entry *, char *);
static char *find_string_in_regex_values(pconf_entry *, char *);
static char *find_host_in_regex_values(pconf_entry *, struct sockaddr_in *);
static char *find_princ_in_regex_values(krb5_context, pconf_entry *, char *);
static int regex_compare(char *, char *);
/*
* Our local storage
*/
static pconf_entry *conf = NULL;
/*
* Initialize our configuration. Return -1 on error, 0 otherwise.
*/
int
init_conf(char *conf_file)
{
if ((conf = parse_conf(conf_file, NULL)) == NULL) {
strcpy(srv_conf_error, pconf_error);
return -1;
}
return 0;
}
/*
* Free any storage we had
*/
void
free_conf()
{
free_pconf_enteries(conf);
conf = NULL;
}
/*
* Check request against configuration. Returns 0 if good, -1 otherwise
*/
int
check_conf(krb525_request * request)
{
pconf_entry *entry;
pconf_entry *client_conf;
pconf_entry *list;
int retval = -1;
/*
* Check to be sure we're initialized
*/
if (conf == NULL) {
sprintf(srv_conf_error, "Configuration not initialized");
return -1;
}
/*
* First check the list of allowed clients
*/
entry = find_string_in_list(conf, "allowed_clients");
if (entry == NULL) {
sprintf(srv_conf_error, "No clients allowed!");
goto done;
}
if (find_princ_in_regex_values(request->krb5_context, entry, request->sender_name) == NULL) {
sprintf(srv_conf_error, "Client not allowed");
goto done;
}
/*
* If this is a special client (it has it's own entry) then we check
* things against that.
*/
client_conf = find_princ_in_regex_list(request->krb5_context, conf, request->sender_name);
if (client_conf != NULL) {
/*
* Client has it's own entry of the form:
*
* client = {
* capability = <name>, ... ;
* allowed_hosts = <host1>, <host2>, <host3>, ...;
* target_clients = <client1>, <client2>, <client3>, ... ;
* target_servers = <server1>, <server2>, <client4>, ... ;
* server_mappings = {
* source_server = target_server ;
* }
* client_mappings = {
* source_client = target_client ;
* }
* }
*/
/* Find and check capabilities, if required */
entry = find_string_in_list(client_conf->list, "capability");
if (entry) {
if (check_capabilities(request, entry->values)) {
sprintf(srv_conf_error, "capability check failed: %s", cap_error);
goto done;
}
}
/* Find the allowed hosts list and check this host */
list = find_string_in_list(client_conf->list, "allowed_hosts");
if (list == NULL) {
sprintf(srv_conf_error, "No hosts allowed for client");
goto done;
}
if (find_host_in_regex_values(list, &(request->addr)) == NULL) {
sprintf(srv_conf_error, "Host not allowed");
goto done;
}
/*
* If the client is changing, then check the target
*/
if (strcmp(request->cname, request->target_cname)) {
entry = find_string_in_list(client_conf->list, "target_clients");
if (entry != NULL) {
if (find_princ_in_regex_values(request->krb5_context, entry,
request->target_cname) == NULL) {
sprintf(srv_conf_error, "Target client not allowed");
goto done;
}
} else {
list = find_string_in_list(client_conf->list, "client_mappings");
if (list == NULL) {
sprintf(srv_conf_error, "No client mappings allowed");
goto done;
}
entry = find_princ_in_regex_list(request->krb5_context, list->list, request->cname);
if (entry == NULL) {
sprintf(srv_conf_error, "No mappings for client");
goto done;
}
if (find_princ_in_regex_values(request->krb5_context, entry,
request->target_cname) == NULL) {
sprintf(srv_conf_error, "Target client not a legal mapping");
goto done;
}
}
}
/*
* If the server is changing, then check the target
*/
if (strcmp(request->sname, request->target_sname)) {
entry = find_string_in_list(client_conf->list, "target_servers");
if (entry != NULL) {
if (find_princ_in_regex_values(request->krb5_context, entry,
request->target_sname) == NULL) {
/* target_servers does not contain our target */
sprintf(srv_conf_error, "Target server not allowed");
goto done;
}
} else {
/* No target_servers, try server_mappings */
list = find_string_in_list(client_conf->list, "server_mappings");
if (list == NULL) {
sprintf(srv_conf_error, "No server mappings allowed");
goto done;
}
entry = find_princ_in_regex_list(request->krb5_context, list->list, request->sname);
if (entry == NULL) {
sprintf(srv_conf_error, "No mappings for server");
goto done;
}
if (find_princ_in_regex_values(request->krb5_context, entry,
request->target_sname) == NULL) {
sprintf(srv_conf_error, "Target server not a legal mapping");
goto done;
}
}
}
/* Checks out OK */
} else {
/* No special entry for client, check for defaults */
/* Find the allowed hosts list and check this host */
list = find_string_in_list(conf, "allowed_hosts");
if (list == NULL) {
sprintf(srv_conf_error, "No default hosts allowed");
goto done;
}
if (find_host_in_regex_values(list, &(request->addr)) == NULL) {
sprintf(srv_conf_error, "Host not allowed in default list");
goto done;
}
/*
* If the client is changing, check the mapping
*/
if (strcmp(request->cname, request->target_cname)) {
list = find_string_in_list(conf, "client_mappings");
if (list == NULL) {
sprintf(srv_conf_error, "No client mappings allowed");
goto done;
}
entry = find_princ_in_regex_list(request->krb5_context, list->list, request->cname);
if (entry == NULL) {
sprintf(srv_conf_error, "No mappings for client");
goto done;
}
if (find_princ_in_regex_values(request->krb5_context, entry, request->target_cname) == NULL) {
sprintf(srv_conf_error, "Target client not a legal mapping");
goto done;
}
}
/*
* If the server is changing, check the mapping
*/
if (strcmp(request->sname, request->target_sname)) {
list = find_string_in_list(conf, "server_mappings");
if (list == NULL) {
sprintf(srv_conf_error, "No server mappings allowed");
goto done;
}
entry = find_princ_in_regex_list(request->krb5_context, list->list, request->sname);
if (entry == NULL) {
sprintf(srv_conf_error, "No mappings for server");
goto done;
}
if (find_princ_in_regex_values(request->krb5_context, entry, request->target_sname) == NULL) {
sprintf(srv_conf_error, "Target server not a legal mapping");
goto done;
}
}
/* Checks out OK */
}
/*
* If we've gotten here, then we passed all the tests
*/
retval = 0;
done:
return retval;
}
/*
* Find string in list and return entry
*/
static pconf_entry *
find_string_in_list(pconf_entry * entry, char *string)
{
while (entry) {
char **str = entry->strings;
while (*str) {
if (strcmp(string, *str) == 0)
return entry;
str++;
}
entry = entry->next;
}
return NULL;
}
/*
* Find string in list of regexs and return entry
*/
static pconf_entry *
find_string_in_regex_list(pconf_entry * entry, char *string)
{
while (entry) {
char **str = entry->strings;
while (*str) {
if (regex_compare(*str, string))
return entry;
str++;
}
entry = entry->next;
}
return NULL;
}
/*
* Find a principal in a regex list. First try the name as given,
* then if it's in the default realm, try without the realm.
*/
static pconf_entry *
find_princ_in_regex_list(krb5_context kcontext, pconf_entry * entry, char *pname)
{
pconf_entry *found_entry = NULL;
char *local_realm;
char *princ_realm;
if ((found_entry = find_string_in_regex_list(entry, pname)))
return found_entry;
if (krb5_get_default_realm(kcontext, &local_realm))
return NULL; /* No good way to get an error out of here. */
princ_realm = strchr(pname, '@');
if (princ_realm && (strcmp(princ_realm + 1, local_realm) == 0)) {
*princ_realm = '\0';
found_entry = find_string_in_regex_list(entry, pname);
*princ_realm = '@';
}
krb5_xfree(local_realm);
return found_entry;
}
/*
* Seach through the list of values in entry (which are assumed to be
* regexs). If string is found to match one, return a pointer to the one
* it matches, otherwise return NULL.
*/
static char *
find_string_in_regex_values(pconf_entry * entry, char *string)
{
char **value = entry->values;
while (*value) {
if (regex_compare(*value, string))
return *value;
value++;
}
return NULL;
}
/*
* Given a host address, look for it in the values list (which is assumed
* to be made up of regexs) both by it dot address (e.g. 10.11.12.13) and
* by full hostname.
*/
static char *
find_host_in_regex_values(pconf_entry * entry, struct sockaddr_in *sockaddr)
{
char **value = entry->values;
char *dot_addr;
char *hostname = NULL;
struct hostent *hinfo;
dot_addr = inet_ntoa(sockaddr->sin_addr);
hinfo = gethostbyaddr((char *)&(sockaddr->sin_addr.s_addr),
sizeof(sockaddr->sin_addr.s_addr), sockaddr->sin_family);
if (hinfo)
hostname = hinfo->h_name;
while (*value) {
if (dot_addr && regex_compare(*value, dot_addr))
return *value;
if (hostname && regex_compare(*value, hostname))
return *value;
value++;
}
return NULL;
}
/*
* Find a principal in a regex values list. First try the name as given,
* then if it's in the default realm, try without the realm.
*/
static char *
find_princ_in_regex_values(krb5_context kcontext, pconf_entry * entry, char *pname)
{
char *found_string = NULL;
char *local_realm;
char *princ_realm;
if ((found_string = find_string_in_regex_values(entry, pname)))
return found_string;
if (krb5_get_default_realm(kcontext, &local_realm))
return NULL; /* No good way to get an error out of here. */
princ_realm = strchr(pname, '@');
if (princ_realm && (strcmp(princ_realm + 1, local_realm) == 0)) {
*princ_realm = '\0';
found_string = find_string_in_regex_values(entry, pname);
*princ_realm = '@';
}
krb5_xfree(local_realm);
return found_string;
}
/*
* Compare a string with a regular expression, returning 1 if they match,
* 0 if they don't and -1 on error.
*/
static int
regex_compare(char *regex, char *string)
{
#ifndef NO_REGEX_SUPPORT
char *buf;
char *bufp;
int result;
/*
* First we convert the regular expression from the human-readable
* form (e.g. *.domain.com) to the machine-readable form
* (e.g. ^.*\.domain\.com$).
*
* Make a buffer large enough to hold the largest possible converted
* regex from the string plus our extra characters (one at the
* begining, one at the end, plus a NULL).
*/
buf = (char *)malloc(2 * strlen(regex) + 3);
if (!buf) {
sprintf(srv_conf_error, "malloc() failed");
return -1;
}
bufp = buf;
*bufp++ = '^';
while (*regex) {
switch (*regex) {
case '*':
/* '*' turns into '.*' */
*bufp++ = '.';
*bufp++ = '*';
break;
case '?':
/* '?' turns into '.' */
*bufp++ = '.';
break;
/* '.' needs to be escaped to '\.' */
case '.':
*bufp++ = '\\';
*bufp++ = '.';
break;
default:
*bufp++ = *regex;
}
regex++;
}
*bufp++ = '$';
*bufp++ = '\0';
#ifdef HAVE_REGCOMP
{
regex_t preg;
if (regcomp(&preg, buf, REG_EXTENDED)) {
sprintf(srv_conf_error, "Error parsing string \"%s\"", regex);
result = -1;
} else {
result = (regexec(&preg, string, 0, NULL, 0) == 0);
regfree(&preg);
}
}
#elif HAVE_COMPILE
{
char *expbuf;
expbuf = compile(buf, NULL, NULL);
if (!expbuf) {
sprintf(srv_conf_error, "Error parsing string \"%s\"", regex);
result = -1;
} else {
result = step(string, expbuf);
free(expbuf);
}
}
#else
/*
* If we've gotten here then there is an error in the configuration
* process or this file's #ifdefs
*/
error - No regular expression support found.
#endif
if (buf)
free(buf);
return result;
#else /* NOREGEX_SUPPORT */
/* No regular expression support */
return (strcmp(regex, string) == 0);
#endif /* NO_REGEX_SUPPORT */
}