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
|
/*
Authors:
Pavel B??ezina <pbrezina@redhat.com>
Copyright (C) 2013 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <strings.h>
#include <talloc.h>
#include <tevent.h>
#include "util/util.h"
#include "resolv/async_resolv.h"
#include "providers/fail_over_srv.h"
struct fo_discover_srv_state {
char *dns_domain;
struct fo_server_info *servers;
size_t num_servers;
};
static void fo_discover_srv_done(struct tevent_req *subreq);
struct tevent_req *fo_discover_srv_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct resolv_ctx *resolv_ctx,
const char *service,
const char *protocol,
const char **discovery_domains)
{
struct fo_discover_srv_state *state = NULL;
struct tevent_req *req = NULL;
struct tevent_req *subreq = NULL;
errno_t ret;
req = tevent_req_create(mem_ctx, &state, struct fo_discover_srv_state);
if (req == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, ("tevent_req_create() failed\n"));
return NULL;
}
subreq = resolv_discover_srv_send(state, ev, resolv_ctx, service,
protocol, discovery_domains);
if (subreq == NULL) {
ret = ENOMEM;
goto immediately;
}
tevent_req_set_callback(subreq, fo_discover_srv_done, req);
return req;
immediately:
tevent_req_error(req, ret);
tevent_req_post(req, ev);
return req;
}
static void fo_discover_srv_done(struct tevent_req *subreq)
{
struct fo_discover_srv_state *state = NULL;
struct tevent_req *req = NULL;
struct ares_srv_reply *reply_list = NULL;
struct ares_srv_reply *record = NULL;
int i;
errno_t ret;
req = tevent_req_callback_data(subreq, struct tevent_req);
state = tevent_req_data(req, struct fo_discover_srv_state);
ret = resolv_discover_srv_recv(state, subreq,
&reply_list, &state->dns_domain);
talloc_zfree(subreq);
if (ret == ENOENT) {
ret = ERR_SRV_NOT_FOUND;
goto done;
} else if (ret == EIO) {
ret = ERR_SRV_LOOKUP_ERROR;
goto done;
} else if (ret != EOK) {
goto done;
}
DEBUG(SSSDBG_TRACE_FUNC, ("Got answer. Processing...\n"));
/* sort and store the answer */
ret = resolv_sort_srv_reply(state, &reply_list);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, ("Could not sort the answers from DNS "
"[%d]: %s\n", ret, strerror(ret)));
goto done;
}
state->num_servers = 0;
for (record = reply_list; record != NULL; record = record->next) {
state->num_servers++;
}
DEBUG(SSSDBG_TRACE_FUNC, ("Got %lu servers\n", state->num_servers));
state->servers = talloc_array(state, struct fo_server_info,
state->num_servers);
if (state->servers == NULL) {
ret = ENOMEM;
goto done;
}
for (record = reply_list, i = 0;
record != NULL;
record = record->next, i++) {
state->servers[i].host = talloc_steal(state->servers, record->host);
state->servers[i].port = record->port;
}
talloc_zfree(reply_list);
ret = EOK;
done:
if (ret != EOK) {
tevent_req_error(req, ret);
return;
}
tevent_req_done(req);
}
errno_t fo_discover_srv_recv(TALLOC_CTX *mem_ctx,
struct tevent_req *req,
char **_dns_domain,
struct fo_server_info **_servers,
size_t *_num_servers)
{
struct fo_discover_srv_state *state = NULL;
state = tevent_req_data(req, struct fo_discover_srv_state);
TEVENT_REQ_RETURN_ON_ERROR(req);
if (_dns_domain != NULL) {
*_dns_domain = talloc_steal(mem_ctx, state->dns_domain);
}
if (_servers != NULL) {
*_servers = talloc_steal(mem_ctx, state->servers);
}
if (_num_servers != NULL) {
*_num_servers = state->num_servers;
}
return EOK;
}
struct fo_discover_servers_state {
struct tevent_context *ev;
struct resolv_ctx *resolv_ctx;
const char *service;
const char *protocol;
const char *primary_domain;
const char *backup_domain;
char *dns_domain;
struct fo_server_info *primary_servers;
size_t num_primary_servers;
struct fo_server_info *backup_servers;
size_t num_backup_servers;
};
static void fo_discover_servers_primary_done(struct tevent_req *subreq);
static void fo_discover_servers_backup_done(struct tevent_req *subreq);
struct tevent_req *fo_discover_servers_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct resolv_ctx *resolv_ctx,
const char *service,
const char *protocol,
const char *primary_domain,
const char *backup_domain)
{
struct fo_discover_servers_state *state = NULL;
struct tevent_req *req = NULL;
struct tevent_req *subreq = NULL;
const char **domains = NULL;
errno_t ret;
req = tevent_req_create(mem_ctx, &state,
struct fo_discover_servers_state);
if (req == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, ("tevent_req_create() failed\n"));
return NULL;
}
if (primary_domain == NULL) {
if (backup_domain == NULL) {
state->primary_servers = NULL;
state->num_primary_servers = 0;
state->backup_servers = NULL;
state->num_backup_servers = 0;
state->dns_domain = NULL;
ret = EOK;
goto immediately;
} else {
primary_domain = backup_domain;
backup_domain = NULL;
}
}
state->ev = ev;
state->resolv_ctx = resolv_ctx;
state->service = talloc_strdup(state, service);
if (state->service == NULL) {
ret = ENOMEM;
goto immediately;
}
state->protocol = talloc_strdup(state, protocol);
if (state->protocol == NULL) {
ret = ENOMEM;
goto immediately;
}
state->primary_domain = talloc_strdup(state, primary_domain);
if (state->primary_domain == NULL) {
ret = ENOMEM;
goto immediately;
}
state->backup_domain = talloc_strdup(state, backup_domain);
if (state->backup_domain == NULL && backup_domain != NULL) {
ret = ENOMEM;
goto immediately;
}
DEBUG(SSSDBG_TRACE_FUNC, ("Looking up primary servers\n"));
domains = talloc_zero_array(state, const char *, 3);
if (domains == NULL) {
ret = ENOMEM;
goto immediately;
}
domains[0] = state->primary_domain;
domains[1] = state->backup_domain;
subreq = fo_discover_srv_send(state, ev, resolv_ctx,
state->service, state->protocol, domains);
if (subreq == NULL) {
ret = ENOMEM;
goto immediately;
}
tevent_req_set_callback(subreq, fo_discover_servers_primary_done, req);
return req;
immediately:
tevent_req_error(req, ret);
tevent_req_post(req, ev);
return req;
}
static void fo_discover_servers_primary_done(struct tevent_req *subreq)
{
struct fo_discover_servers_state *state = NULL;
struct tevent_req *req = NULL;
const char **domains = NULL;
errno_t ret;
req = tevent_req_callback_data(subreq, struct tevent_req);
state = tevent_req_data(req, struct fo_discover_servers_state);
ret = fo_discover_srv_recv(state, subreq,
&state->dns_domain,
&state->primary_servers,
&state->num_primary_servers);
talloc_zfree(subreq);
if (ret != EOK) {
goto done;
}
if (strcasecmp(state->dns_domain, state->backup_domain) == 0) {
/* primary domain was unreachable, we will use servers from backup
* domain as primary */
state->backup_servers = NULL;
state->num_backup_servers = 0;
ret = EOK;
goto done;
}
DEBUG(SSSDBG_TRACE_FUNC, ("Looking up backup servers\n"));
domains = talloc_zero_array(state, const char *, 2);
if (domains == NULL) {
ret = ENOMEM;
goto done;
}
domains[0] = state->backup_domain;
subreq = fo_discover_srv_send(state, state->ev, state->resolv_ctx,
state->service, state->protocol, domains);
if (subreq == NULL) {
ret = ENOMEM;
goto done;
}
tevent_req_set_callback(subreq, fo_discover_servers_backup_done, req);
ret = EAGAIN;
done:
if (ret == EOK) {
tevent_req_done(req);
} else if (ret != EAGAIN) {
tevent_req_error(req, ret);
}
return;
}
static void fo_discover_servers_backup_done(struct tevent_req *subreq)
{
struct fo_discover_servers_state *state = NULL;
struct tevent_req *req = NULL;
errno_t ret;
req = tevent_req_callback_data(subreq, struct tevent_req);
state = tevent_req_data(req, struct fo_discover_servers_state);
ret = fo_discover_srv_recv(state, subreq, NULL,
&state->backup_servers,
&state->num_backup_servers);
talloc_zfree(subreq);
if (ret == ERR_SRV_NOT_FOUND || ret == ERR_SRV_LOOKUP_ERROR) {
/* we have successfully fetched primary servers, so we will
* finish the request normally */
DEBUG(SSSDBG_MINOR_FAILURE, ("Unable to retrieve backup servers "
"[%d]: %s\n", ret, sss_strerror(ret)));
ret = EOK;
}
if (ret != EOK) {
tevent_req_error(req, ret);
return;
}
tevent_req_done(req);
}
errno_t fo_discover_servers_recv(TALLOC_CTX *mem_ctx,
struct tevent_req *req,
char **_dns_domain,
struct fo_server_info **_primary_servers,
size_t *_num_primary_servers,
struct fo_server_info **_backup_servers,
size_t *_num_backup_servers)
{
struct fo_discover_servers_state *state = NULL;
state = tevent_req_data(req, struct fo_discover_servers_state);
TEVENT_REQ_RETURN_ON_ERROR(req);
if (_primary_servers) {
*_primary_servers = talloc_steal(mem_ctx, state->primary_servers);
}
if (_num_primary_servers) {
*_num_primary_servers = state->num_primary_servers;
}
if (_backup_servers) {
*_backup_servers = talloc_steal(mem_ctx, state->backup_servers);
}
if (_num_backup_servers) {
*_num_backup_servers = state->num_backup_servers;
}
if (_dns_domain) {
*_dns_domain = talloc_steal(mem_ctx, state->dns_domain);
}
return EOK;
}
struct fo_resolve_srv_dns_ctx {
struct resolv_ctx *resolv_ctx;
enum restrict_family family_order;
enum host_database *host_dbs;
char *hostname;
char *sssd_domain;
char *detected_domain;
};
struct fo_resolve_srv_dns_state {
struct tevent_context *ev;
struct fo_resolve_srv_dns_ctx *ctx;
const char *service;
const char *protocol;
const char *discovery_domain;
char *dns_domain;
struct fo_server_info *servers;
size_t num_servers;
};
static void fo_resolve_srv_dns_domain_done(struct tevent_req *subreq);
static errno_t fo_resolve_srv_dns_discover(struct tevent_req *req);
static void fo_resolve_srv_dns_done(struct tevent_req *subreq);
struct fo_resolve_srv_dns_ctx *
fo_resolve_srv_dns_ctx_init(TALLOC_CTX *mem_ctx,
struct resolv_ctx *resolv_ctx,
enum restrict_family family_order,
enum host_database *host_dbs,
const char *hostname,
const char *sssd_domain)
{
struct fo_resolve_srv_dns_ctx *ctx = NULL;
ctx = talloc_zero(mem_ctx, struct fo_resolve_srv_dns_ctx);
if (ctx == NULL) {
return NULL;
}
ctx->resolv_ctx = resolv_ctx;
ctx->family_order = family_order;
ctx->host_dbs = host_dbs;
ctx->hostname = talloc_strdup(ctx, hostname);
if (ctx->hostname == NULL) {
goto fail;
}
ctx->sssd_domain = talloc_strdup(ctx, sssd_domain);
if (ctx->sssd_domain == NULL) {
goto fail;
}
return ctx;
fail:
talloc_free(ctx);
return NULL;
}
struct tevent_req *fo_resolve_srv_dns_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
const char *service,
const char *protocol,
const char *discovery_domain,
void *pvt)
{
struct fo_resolve_srv_dns_state *state = NULL;
struct fo_resolve_srv_dns_ctx *ctx = NULL;
struct tevent_req *req = NULL;
struct tevent_req *subreq = NULL;
errno_t ret;
req = tevent_req_create(mem_ctx, &state, struct fo_resolve_srv_dns_state);
if (req == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, ("tevent_req_create() failed\n"));
return NULL;
}
ctx = talloc_get_type(pvt, struct fo_resolve_srv_dns_ctx);
if (ctx == NULL) {
ret = EINVAL;
goto immediately;
}
state->ev = ev;
state->ctx = ctx;
state->service = service;
state->protocol = protocol;
if (discovery_domain == NULL) {
state->discovery_domain = NULL;
} else {
state->discovery_domain = discovery_domain;
}
if (discovery_domain == NULL && ctx->detected_domain == NULL) {
/* we will try to detect proper discovery domain */
subreq = resolv_get_domain_send(state, state->ev, ctx->resolv_ctx,
ctx->hostname, ctx->host_dbs,
ctx->family_order);
if (subreq == NULL) {
ret = ENOMEM;
goto immediately;
}
tevent_req_set_callback(subreq, fo_resolve_srv_dns_domain_done, req);
} else {
/* we will use either provided or previously detected
* discovery domain */
ret = fo_resolve_srv_dns_discover(req);
if (ret != EAGAIN) {
goto immediately;
}
}
return req;
immediately:
if (ret == EOK) {
tevent_req_done(req);
} else {
tevent_req_error(req, ret);
}
tevent_req_post(req, ev);
return req;
}
static void fo_resolve_srv_dns_domain_done(struct tevent_req *subreq)
{
struct fo_resolve_srv_dns_state *state = NULL;
struct tevent_req *req = NULL;
errno_t ret;
req = tevent_req_callback_data(subreq, struct tevent_req);
state = tevent_req_data(req, struct fo_resolve_srv_dns_state);
ret = resolv_get_domain_recv(state->ctx, subreq,
&state->ctx->detected_domain);
talloc_zfree(subreq);
if (ret != EOK) {
goto done;
}
ret = fo_resolve_srv_dns_discover(req);
done:
if (ret == EOK) {
tevent_req_done(req);
} else if (ret != EAGAIN) {
tevent_req_error(req, ret);
}
return;
}
static errno_t fo_resolve_srv_dns_discover(struct tevent_req *req)
{
struct fo_resolve_srv_dns_state *state = NULL;
struct fo_resolve_srv_dns_ctx *ctx = NULL;
struct tevent_req *subreq = NULL;
const char **domains = NULL;
errno_t ret;
state = tevent_req_data(req, struct fo_resolve_srv_dns_state);
ctx = state->ctx;
domains = talloc_zero_array(state, const char *, 3);
if (domains == NULL) {
ret = ENOMEM;
goto done;
}
if (state->discovery_domain == NULL) {
/* we will use detected domain with SSSD domain as fallback */
domains[0] = talloc_strdup(domains, ctx->detected_domain);
if (domains[0] == NULL) {
ret = ENOMEM;
goto done;
}
if (strcasecmp(ctx->detected_domain, ctx->sssd_domain) != 0) {
domains[1] = talloc_strdup(domains, ctx->sssd_domain);
if (domains[1] == NULL) {
ret = ENOMEM;
goto done;
}
}
} else {
/* We will use only discovery domain that was provided via plugin
* interface. We don't have to dup here because it is already on
* state. */
domains[0] = state->discovery_domain;
}
subreq = fo_discover_srv_send(state, state->ev, ctx->resolv_ctx,
state->service, state->protocol, domains);
if (subreq == NULL) {
ret = ENOMEM;
goto done;
}
tevent_req_set_callback(subreq, fo_resolve_srv_dns_done, req);
ret = EAGAIN;
done:
if (ret != EAGAIN) {
talloc_free(domains);
}
return ret;
}
static void fo_resolve_srv_dns_done(struct tevent_req *subreq)
{
struct fo_resolve_srv_dns_state *state = NULL;
struct tevent_req *req = NULL;
errno_t ret;
req = tevent_req_callback_data(subreq, struct tevent_req);
state = tevent_req_data(req, struct fo_resolve_srv_dns_state);
ret = fo_discover_srv_recv(state, subreq, &state->dns_domain,
&state->servers, &state->num_servers);
talloc_zfree(subreq);
if (ret != EOK) {
goto done;
}
done:
if (ret != EOK) {
tevent_req_error(req, ret);
return;
}
tevent_req_done(req);
}
errno_t fo_resolve_srv_dns_recv(TALLOC_CTX *mem_ctx,
struct tevent_req *req,
char **_dns_domain,
struct fo_server_info **_primary_servers,
size_t *_num_primary_servers,
struct fo_server_info **_backup_servers,
size_t *_num_backup_servers)
{
struct fo_resolve_srv_dns_state *state = NULL;
state = tevent_req_data(req, struct fo_resolve_srv_dns_state);
TEVENT_REQ_RETURN_ON_ERROR(req);
if (_primary_servers) {
*_primary_servers = talloc_steal(mem_ctx, state->servers);
}
if (_num_primary_servers) {
*_num_primary_servers = state->num_servers;
}
/* backup servers are not supported by simple srv lookup */
if (_backup_servers) {
*_backup_servers = NULL;
}
if (_num_backup_servers) {
*_num_backup_servers = 0;
}
if (_dns_domain) {
*_dns_domain = talloc_steal(mem_ctx, state->dns_domain);
}
return EOK;
}
|