summaryrefslogtreecommitdiffstats
path: root/src/providers/ipa/ipa_init.c
blob: 5b7c8e1348f561901782c872078a0e7391d4ff75 (plain)
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
/*
    SSSD

    IPA Provider Initialization functions

    Authors:
        Simo Sorce <ssorce@redhat.com>

    Copyright (C) 2009 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 <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "util/child_common.h"
#include "providers/ipa/ipa_common.h"
#include "providers/krb5/krb5_auth.h"
#include "providers/krb5/krb5_init_shared.h"
#include "providers/ipa/ipa_id.h"
#include "providers/ipa/ipa_auth.h"
#include "providers/ipa/ipa_access.h"
#include "providers/ipa/ipa_hostid.h"
#include "providers/ipa/ipa_dyndns.h"
#include "providers/ipa/ipa_selinux.h"
#include "providers/ldap/sdap_access.h"
#include "providers/ldap/sdap_idmap.h"
#include "providers/ipa/ipa_subdomains.h"
#include "providers/ipa/ipa_srv.h"
#include "providers/be_dyndns.h"
#include "providers/ipa/ipa_session.h"

#define DNS_SRV_MISCONFIGURATION "SRV discovery is enabled on the IPA " \
    "server while using custom dns_discovery_domain. DNS discovery of " \
    "trusted AD domain will likely fail. It is recommended not to use " \
    "SRV discovery or the dns_discovery_domain option for the IPA "     \
    "domain while running on the server itself\n"

#define PREAUTH_INDICATOR_ERROR "Failed to create preauth indicator file, " \
    "special password prompting might not be available.\n"

struct ipa_init_ctx {
    struct ipa_options *options;
    struct ipa_id_ctx *id_ctx;
    struct ipa_auth_ctx *auth_ctx;
};


struct krb5_ctx *ipa_init_get_krb5_auth_ctx(void *data)
{
    struct ipa_init_ctx *ipa_init_ctx;

    ipa_init_ctx = talloc_get_type(data, struct ipa_init_ctx);
    if (ipa_init_ctx == NULL || ipa_init_ctx->auth_ctx == NULL) {
        return NULL;
    }

    return ipa_init_ctx->auth_ctx->krb5_auth_ctx;
}

static bool srv_in_server_list(const char *servers)
{
    TALLOC_CTX *tmp_ctx;
    char **list = NULL;
    int ret = 0;
    bool has_srv = false;

    if (servers == NULL) return true;

    tmp_ctx = talloc_new(NULL);
    if (!tmp_ctx) {
        return false;
    }

    /* split server parm into a list */
    ret = split_on_separator(tmp_ctx, servers, ',', true, true, &list, NULL);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to parse server list!\n");
        goto done;
    }

    for (int i = 0; list[i]; i++) {
        has_srv = be_fo_is_srv_identifier(list[i]);
        if (has_srv == true) {
            break;
        }
    }

done:
    talloc_free(tmp_ctx);
    return has_srv;
}

static errno_t ipa_init_options(TALLOC_CTX *mem_ctx,
                                struct be_ctx *be_ctx,
                                struct ipa_options **_ipa_options)
{
    struct ipa_options *ipa_options;
    const char *ipa_servers;
    const char *ipa_backup_servers;
    errno_t ret;

    ret = ipa_get_options(mem_ctx, be_ctx->cdb, be_ctx->conf_path,
                          be_ctx->domain, &ipa_options);
    if (ret != EOK) {
        return ret;
    }

    ipa_servers = dp_opt_get_string(ipa_options->basic, IPA_SERVER);
    ipa_backup_servers = dp_opt_get_string(ipa_options->basic, IPA_BACKUP_SERVER);

    ret = ipa_service_init(ipa_options, be_ctx, ipa_servers,
                           ipa_backup_servers, ipa_options,
                           &ipa_options->service);
    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Failed to init IPA service [%d]: %s\n",
              ret, sss_strerror(ret));
        talloc_free(ipa_options);
        return ret;
    }

    *_ipa_options = ipa_options;
    return EOK;
}

static errno_t ipa_init_id_ctx(TALLOC_CTX *mem_ctx,
                               struct be_ctx *be_ctx,
                               struct ipa_options *ipa_options,
                               struct ipa_id_ctx **_ipa_id_ctx)
{
    struct ipa_id_ctx *ipa_id_ctx = NULL;
    struct sdap_id_ctx *sdap_id_ctx = NULL;
    errno_t ret;

    ipa_id_ctx = talloc_zero(mem_ctx, struct ipa_id_ctx);
    if (ipa_id_ctx == NULL) {
        ret = ENOMEM;
        goto done;
    }

    sdap_id_ctx = sdap_id_ctx_new(mem_ctx, be_ctx, ipa_options->service->sdap);
    if (sdap_id_ctx == NULL) {
        ret = ENOMEM;
        goto done;
    }

    ipa_id_ctx->ipa_options = ipa_options;
    ipa_id_ctx->sdap_id_ctx = sdap_id_ctx;
    ipa_options->id_ctx = ipa_id_ctx;

    ret = ipa_get_id_options(ipa_options, be_ctx->cdb, be_ctx->conf_path,
                             &sdap_id_ctx->opts);
    if (ret != EOK) {
        goto done;
    }

    *_ipa_id_ctx = ipa_id_ctx;

    ret = EOK;

done:
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init id context [%d]: %s\n",
              ret, sss_strerror(ret));

        talloc_free(ipa_id_ctx);
        talloc_free(sdap_id_ctx);
    }

    return ret;
}


static errno_t ipa_init_dyndns(struct be_ctx *be_ctx,
                               struct ipa_options *ipa_options)
{
    bool enabled;
    errno_t ret;

    ret = ipa_get_dyndns_options(be_ctx, ipa_options);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get dyndns options [%d]: %s\n",
              ret, sss_strerror(ret));
        return ret;
    }

    enabled = dp_opt_get_bool(ipa_options->dyndns_ctx->opts,
                              DP_OPT_DYNDNS_UPDATE);
    if (!enabled) {
        DEBUG(SSSDBG_CONF_SETTINGS, "Dynamic DNS updates are off.\n");
        return EOK;
    }

    /* Perform automatic DNS updates when the IP address changes.
     * Register a callback for successful LDAP reconnections.
     * This is the easiest way to identify that we have gone online.
     */

    DEBUG(SSSDBG_CONF_SETTINGS,
          "Dynamic DNS updates are on. Checking for nsupdate...\n");

    ret = be_nsupdate_check();
    if (ret != EOK) {
        DEBUG(SSSDBG_CONF_SETTINGS, "nsupdate is not availabe, "
              "dynamic DNS updates will not work\n");
        return EOK;
    }

    DEBUG(SSSDBG_CONF_SETTINGS, "nsupdate is available\n");

    ret = ipa_dyndns_init(be_ctx, ipa_options);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              "Failure setting up automatic DNS update\n");
        /* We will continue without DNS updating */
    }

    return EOK;
}

static errno_t ipa_init_server_mode(struct be_ctx *be_ctx,
                                    struct ipa_options *ipa_options,
                                    struct ipa_id_ctx *ipa_id_ctx)
{
    const char *ipa_servers;
    const char *dnsdomain;
    const char *hostname;
    bool sites_enabled;
    errno_t ret;

    ipa_id_ctx->view_name = talloc_strdup(ipa_id_ctx, SYSDB_DEFAULT_VIEW_NAME);
    if (ipa_id_ctx->view_name == NULL) {
        DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup() failed.\n");
        return ENOMEM;
    }

    ret = sysdb_update_view_name(be_ctx->domain->sysdb, ipa_id_ctx->view_name);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot add/update view name to sysdb.\n");
        return ret;
    }

    hostname = dp_opt_get_string(ipa_options->basic, IPA_HOSTNAME);
    ipa_servers = dp_opt_get_string(ipa_options->basic, IPA_SERVER);
    sites_enabled = dp_opt_get_bool(ipa_options->basic, IPA_ENABLE_DNS_SITES);
    dnsdomain = dp_opt_get_string(be_ctx->be_res->opts, DP_RES_OPT_DNS_DOMAIN);

    if (srv_in_server_list(ipa_servers) || sites_enabled) {
        DEBUG(SSSDBG_IMPORTANT_INFO, "SSSD configuration uses either DNS "
              "SRV resolution or IPA site discovery to locate IPA servers. "
              "On IPA server itself, it is recommended that SSSD is "
              "configured to only connect to the IPA server it's running at. ");

        /* If SRV discovery is enabled on the server and
         * dns_discovery_domain is set explicitly, then
         * the current failover code would use the dns_discovery
         * domain to try to find AD servers and fail.
         */
        if (dnsdomain != NULL) {
            sss_log(SSS_LOG_ERR, DNS_SRV_MISCONFIGURATION);
            DEBUG(SSSDBG_CRIT_FAILURE, DNS_SRV_MISCONFIGURATION);
        }

        ret = be_fo_set_dns_srv_lookup_plugin(be_ctx, hostname);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to set SRV lookup plugin "
                  "[%d]: %s\n", ret, sss_strerror(ret));
            return ret;
        }

        return EOK;
    } else {
        /* In server mode we need to ignore the dns_discovery_domain if set
         * and only discover servers based on AD domains. */
        ret = dp_opt_set_string(be_ctx->be_res->opts, DP_RES_OPT_DNS_DOMAIN,
                                NULL);
        if (ret != EOK) {
            DEBUG(SSSDBG_MINOR_FAILURE, "Could not reset the "
                  "dns_discovery_domain, trusted AD domains discovery "
                  "might fail. Please remove dns_discovery_domain "
                  "from the config file and restart the SSSD\n");
        } else {
            DEBUG(SSSDBG_CONF_SETTINGS, "The value of dns_discovery_domain "
                  "will be ignored in ipa_server_mode\n");
        }
    }

    return EOK;
}

static errno_t ipa_init_client_mode(struct be_ctx *be_ctx,
                                    struct ipa_options *ipa_options,
                                    struct ipa_id_ctx *ipa_id_ctx)
{
    struct ipa_srv_plugin_ctx *srv_ctx;
    const char *ipa_domain;
    const char *hostname;
    bool sites_enabled;
    errno_t ret;

    ret = sysdb_get_view_name(ipa_id_ctx, be_ctx->domain->sysdb,
                              &ipa_id_ctx->view_name);
    if (ret == ENOENT) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot find view name in the cache. "
              "Will do online lookup later.\n");
    } else if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "sysdb_get_view_name() failed [%d]: %s\n",
              ret, sss_strerror(ret));
        return ret;
    }

    hostname = dp_opt_get_string(ipa_options->basic, IPA_HOSTNAME);
    sites_enabled = dp_opt_get_bool(ipa_options->basic, IPA_ENABLE_DNS_SITES);

    if (sites_enabled) {
        /* use IPA plugin */
        ipa_domain = dp_opt_get_string(ipa_options->basic, IPA_DOMAIN);
        srv_ctx = ipa_srv_plugin_ctx_init(be_ctx, be_ctx->be_res->resolv,
                                          hostname, ipa_domain);
        if (srv_ctx == NULL) {
            DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory?\n");
            return ENOMEM;
        }

        be_fo_set_srv_lookup_plugin(be_ctx, ipa_srv_plugin_send,
                                    ipa_srv_plugin_recv, srv_ctx, "IPA");
    } else {
        /* fall back to standard plugin on clients. */
        ret = be_fo_set_dns_srv_lookup_plugin(be_ctx, hostname);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to set SRV lookup plugin "
                  "[%d]: %s\n", ret, strerror(ret));
            return ret;
        }
    }

    return EOK;
}

static errno_t ipa_init_ipa_auth_ctx(TALLOC_CTX *mem_ctx,
                                     struct ipa_options *ipa_options,
                                     struct ipa_id_ctx *ipa_id_ctx,
                                     struct ipa_auth_ctx **_ipa_auth_ctx)
{
    struct ipa_auth_ctx *ipa_auth_ctx;
    errno_t ret;

    ipa_auth_ctx = talloc_zero(mem_ctx, struct ipa_auth_ctx);
    if (ipa_auth_ctx == NULL) {
        return ENOMEM;
    }

    ipa_auth_ctx->sdap_id_ctx = ipa_id_ctx->sdap_id_ctx;

    ret = dp_copy_options(ipa_auth_ctx, ipa_options->basic,
                          IPA_OPTS_BASIC, &ipa_auth_ctx->ipa_options);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "dp_copy_options failed.\n");
        talloc_free(ipa_auth_ctx);
        return ret;
    }

    *_ipa_auth_ctx = ipa_auth_ctx;

    return EOK;
}

static errno_t ipa_init_krb5_auth_ctx(TALLOC_CTX *mem_ctx,
                                      struct be_ctx *be_ctx,
                                      struct ipa_options *ipa_options,
                                      struct krb5_ctx **_krb5_auth_ctx)
{
    struct krb5_ctx *krb5_auth_ctx;
    bool server_mode;
    errno_t ret;

    krb5_auth_ctx = talloc_zero(mem_ctx, struct krb5_ctx);
    if (krb5_auth_ctx == NULL) {
        return ENOMEM;
    }

    krb5_auth_ctx->service = ipa_options->service->krb5_service;

    server_mode = dp_opt_get_bool(ipa_options->basic, IPA_SERVER_MODE);
    krb5_auth_ctx->config_type = server_mode ? K5C_IPA_SERVER : K5C_IPA_CLIENT;

    ret = ipa_get_auth_options(ipa_options, be_ctx->cdb, be_ctx->conf_path,
                               &krb5_auth_ctx->opts);
    if (ret != EOK) {
        talloc_free(krb5_auth_ctx);
        return ret;
    }

    *_krb5_auth_ctx = krb5_auth_ctx;
    return EOK;
}

static errno_t ipa_init_sdap_auth_ctx(TALLOC_CTX *mem_ctx,
                                      struct be_ctx *be_ctx,
                                      struct ipa_options *ipa_options,
                                      struct sdap_auth_ctx **_sdap_auth_ctx)
{
    struct sdap_auth_ctx *sdap_auth_ctx;

    sdap_auth_ctx = talloc_zero(mem_ctx, struct sdap_auth_ctx);
    if (sdap_auth_ctx == NULL) {
        return ENOMEM;
    }

    sdap_auth_ctx->be =  be_ctx;
    sdap_auth_ctx->service = ipa_options->service->sdap;

    if (ipa_options->id == NULL) {
        talloc_free(sdap_auth_ctx);
        return EINVAL;
    }

    sdap_auth_ctx->opts = ipa_options->id;

    *_sdap_auth_ctx = sdap_auth_ctx;

    return EOK;
}

static void cleanup_ipa_preauth_indicator(void)
{
    int ret;

    ret = unlink(PAM_PREAUTH_INDICATOR);
    if (ret != EOK) {
        ret = errno;
        DEBUG(SSSDBG_OP_FAILURE,
              "Failed to remove preauth indicator file [%s] %d [%s].\n",
              PAM_PREAUTH_INDICATOR, ret, sss_strerror(ret));
    }
}

static errno_t create_ipa_preauth_indicator(void)
{
    TALLOC_CTX *tmp_ctx;
    errno_t ret;
    int fd;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n");
        return ENOMEM;
    }

    fd = open(PAM_PREAUTH_INDICATOR, O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW,
              0644);
    if (fd < 0) {
        if (errno != EEXIST) {
            DEBUG(SSSDBG_OP_FAILURE,
                  "Failed to create preauth indicator file [%s].\n",
                  PAM_PREAUTH_INDICATOR);
            ret = EOK;
            goto done;
        }

        DEBUG(SSSDBG_CRIT_FAILURE,
              "Preauth indicator file [%s] already exists. "
              "Maybe it is left after an unplanned exit. Continuing.\n",
              PAM_PREAUTH_INDICATOR);
    } else {
        close(fd);
    }

    ret = atexit(cleanup_ipa_preauth_indicator);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "atexit failed. Continuing.\n");
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);

    return ret;
}

static struct sdap_ext_member_ctx *
ipa_create_ext_members_ctx(TALLOC_CTX *mem_ctx,
                           struct ipa_id_ctx *id_ctx)
{
    struct sdap_ext_member_ctx *ext_ctx = NULL;

    ext_ctx = talloc_zero(mem_ctx, struct sdap_ext_member_ctx);
    if (ext_ctx == NULL) {
        return NULL;
    }

    ext_ctx->pvt = id_ctx;
    ext_ctx->ext_member_resolve_send = ipa_ext_group_member_send;
    ext_ctx->ext_member_resolve_recv = ipa_ext_group_member_recv;

    return ext_ctx;
}

static errno_t ipa_init_auth_ctx(TALLOC_CTX *mem_ctx,
                                 struct be_ctx *be_ctx,
                                 struct ipa_options *ipa_options,
                                 struct ipa_id_ctx *id_ctx,
                                 struct ipa_auth_ctx **_auth_ctx)
{
    struct sdap_auth_ctx *sdap_auth_ctx;
    struct ipa_auth_ctx *ipa_auth_ctx;
    struct krb5_ctx *krb5_auth_ctx;
    errno_t ret;

    ret = ipa_init_ipa_auth_ctx(mem_ctx, ipa_options, id_ctx, &ipa_auth_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init IPA auth context\n");
        return ret;
    }

    ipa_options->auth_ctx = ipa_auth_ctx;

    ret = ipa_init_krb5_auth_ctx(ipa_auth_ctx, be_ctx, ipa_options,
                                 &krb5_auth_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init KRB5 auth context\n");
        goto done;
    }
    ipa_options->auth_ctx->krb5_auth_ctx = krb5_auth_ctx;

    ret = ipa_init_sdap_auth_ctx(ipa_auth_ctx, be_ctx, ipa_options,
                                 &sdap_auth_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init SDAP auth context\n");
        goto done;
    }
    ipa_options->auth_ctx->sdap_auth_ctx = sdap_auth_ctx;

    ret = setup_tls_config(sdap_auth_ctx->opts->basic);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "setup_tls_config failed [%d]: %s\n",
              ret, sss_strerror(ret));
        goto done;
    }

    /* Initialize features needed by the krb5_child */
    ret = krb5_child_init(krb5_auth_ctx, be_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Could not initialize krb5_child "
              "settings [%d]: %s\n", ret, sss_strerror(ret));
        goto done;
    }

    ret = create_ipa_preauth_indicator();
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, PREAUTH_INDICATOR_ERROR);
        sss_log(SSSDBG_CRIT_FAILURE, PREAUTH_INDICATOR_ERROR);
    }

    *_auth_ctx = ipa_auth_ctx;
    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(ipa_auth_ctx);
    }

    return ret;
}

static bool ipa_check_fqdn(const char *str)
{
    return strchr(str, '.');
}

static errno_t ipa_init_misc(struct be_ctx *be_ctx,
                             struct ipa_options *ipa_options,
                             struct ipa_id_ctx *ipa_id_ctx,
                             struct sdap_id_ctx *sdap_id_ctx)
{
    errno_t ret;

    if (!ipa_check_fqdn(dp_opt_get_string(ipa_options->basic,
                        IPA_HOSTNAME))) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "ipa_hostname is not Fully Qualified Domain Name.\n");
    }

    ret = ipa_init_dyndns(be_ctx, ipa_options);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init dyndns [%d]: %s\n",
              ret, sss_strerror(ret));
        return ret;
    }

    ret = setup_tls_config(sdap_id_ctx->opts->basic);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get TLS options [%d]: %s\n",
              ret, sss_strerror(ret));
        return ret;
    }

    ret = ipa_idmap_init(sdap_id_ctx, sdap_id_ctx,
                         &sdap_id_ctx->opts->idmap_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE,
              "Could not initialize ID mapping. In case ID mapping properties "
              "changed on the server, please remove the SSSD database\n");
        return ret;
    }

    ret = ldap_id_setup_tasks(sdap_id_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup background tasks "
              "[%d]: %s\n", ret, sss_strerror(ret));
        return ret;
    }

    ret = sdap_setup_child();
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup sdap child [%d]: %s\n",
              ret, sss_strerror(ret));
        return ret;
    }

    if (dp_opt_get_bool(ipa_options->basic, IPA_SERVER_MODE)) {
        ret = ipa_init_server_mode(be_ctx, ipa_options, ipa_id_ctx);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init server mode "
                  "[%d]: %s\n", ret, sss_strerror(ret));
            return ret;
        }
    } else {
        ret = ipa_init_client_mode(be_ctx, ipa_options, ipa_id_ctx);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init client mode "
                  "[%d]: %s\n", ret, sss_strerror(ret));
            return ret;
        }
    }

    ret = sdap_refresh_init(be_ctx->refresh_ctx, sdap_id_ctx);
    if (ret != EOK && ret != EEXIST) {
        DEBUG(SSSDBG_MINOR_FAILURE, "Periodical refresh "
              "will not work [%d]: %s\n", ret, sss_strerror(ret));
    }

    ipa_id_ctx->sdap_id_ctx->opts->ext_ctx = ipa_create_ext_members_ctx(
                                ipa_id_ctx->sdap_id_ctx->opts, ipa_id_ctx);
    if (ipa_id_ctx->sdap_id_ctx->opts->ext_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to set the extrernal group ctx\n");
        return ENOMEM;
    }

    ret = sdap_init_certmap(sdap_id_ctx, sdap_id_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Failed to initialized certificate mapping.\n");
        return ret;
    }

    return EOK;
}

errno_t sssm_ipa_init(TALLOC_CTX *mem_ctx,
                      struct be_ctx *be_ctx,
                      struct data_provider *provider,
                      const char *module_name,
                      void **_module_data)
{
    struct ipa_init_ctx *init_ctx;
    errno_t ret;

    init_ctx = talloc_zero(mem_ctx, struct ipa_init_ctx);
    if (init_ctx == NULL) {
        return ENOMEM;
    }

    /* Always initialize options since it is needed everywhere. */
    ret = ipa_init_options(init_ctx, be_ctx, &init_ctx->options);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init IPA options "
              "[%d]: %s\n", ret, sss_strerror(ret));
        goto done;
    }

    /* Always initialize id_ctx since it is needed everywhere. */
    ret = ipa_init_id_ctx(init_ctx, be_ctx, init_ctx->options,
                          &init_ctx->id_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init IPA ID context "
              "[%d]: %s\n", ret, sss_strerror(ret));
        goto done;
    }

    /* Setup miscellaneous things. */
    ret = ipa_init_misc(be_ctx, init_ctx->options, init_ctx->id_ctx,
                        init_ctx->id_ctx->sdap_id_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init IPA module "
              "[%d]: %s\n", ret, sss_strerror(ret));
        goto done;
    }

    /* Initialize auth_ctx only if one of the target is enabled. */
    if (dp_target_enabled(provider, module_name, DPT_AUTH, DPT_CHPASS)) {
        ret = ipa_init_auth_ctx(init_ctx, be_ctx, init_ctx->options,
                                init_ctx->id_ctx, &init_ctx->auth_ctx);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to init IPA auth context "
                  "[%d]: %s\n", ret, sss_strerror(ret));
            goto done;
        }
    }

    *_module_data = init_ctx;

    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(init_ctx);
    }

    return ret;
}

errno_t sssm_ipa_id_init(TALLOC_CTX *mem_ctx,
                         struct be_ctx *be_ctx,
                         void *module_data,
                         struct dp_method *dp_methods)
{
    struct ipa_init_ctx *init_ctx;
    struct ipa_id_ctx *id_ctx;

    init_ctx = talloc_get_type(module_data, struct ipa_init_ctx);
    id_ctx = init_ctx->id_ctx;

    dp_set_method(dp_methods, DPM_ACCOUNT_HANDLER,
                  ipa_account_info_handler_send, ipa_account_info_handler_recv, id_ctx,
                  struct ipa_id_ctx, struct dp_id_data, struct dp_reply_std);

    dp_set_method(dp_methods, DPM_CHECK_ONLINE,
                  sdap_online_check_handler_send, sdap_online_check_handler_recv, id_ctx->sdap_id_ctx,
                  struct sdap_id_ctx, void, struct dp_reply_std);

    return EOK;
}

errno_t sssm_ipa_auth_init(TALLOC_CTX *mem_ctx,
                           struct be_ctx *be_ctx,
                           void *module_data,
                           struct dp_method *dp_methods)
{
    struct ipa_init_ctx *init_ctx;
    struct ipa_auth_ctx *auth_ctx;

    init_ctx = talloc_get_type(module_data, struct ipa_init_ctx);
    auth_ctx = init_ctx->auth_ctx;

    dp_set_method(dp_methods, DPM_AUTH_HANDLER,
                  ipa_pam_auth_handler_send, ipa_pam_auth_handler_recv, auth_ctx,
                  struct ipa_auth_ctx, struct pam_data, struct pam_data *);

    return EOK;
}

errno_t sssm_ipa_chpass_init(TALLOC_CTX *mem_ctx,
                             struct be_ctx *be_ctx,
                             void *module_data,
                             struct dp_method *dp_methods)
{
    return sssm_ipa_auth_init(mem_ctx, be_ctx, module_data, dp_methods);
}

errno_t sssm_ipa_access_init(TALLOC_CTX *mem_ctx,
                             struct be_ctx *be_ctx,
                             void *module_data,
                             struct dp_method *dp_methods)
{
    struct ipa_access_ctx *access_ctx;
    struct ipa_init_ctx *init_ctx;
    struct ipa_id_ctx *id_ctx;
    errno_t ret;

    init_ctx = talloc_get_type(module_data, struct ipa_init_ctx);
    id_ctx = init_ctx->id_ctx;

    access_ctx = talloc_zero(mem_ctx, struct ipa_access_ctx);
    if (access_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed.\n");
        return ENOMEM;
    }

    access_ctx->sdap_ctx = id_ctx->sdap_id_ctx;
    access_ctx->host_map = id_ctx->ipa_options->host_map;
    access_ctx->hostgroup_map = id_ctx->ipa_options->hostgroup_map;
    access_ctx->host_search_bases = id_ctx->ipa_options->host_search_bases;
    access_ctx->hbac_search_bases = id_ctx->ipa_options->hbac_search_bases;

    ret = dp_copy_options(access_ctx, id_ctx->ipa_options->basic,
                          IPA_OPTS_BASIC, &access_ctx->ipa_options);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "dp_copy_options() failed.\n");
        goto done;
    }

    /* Set up an sdap_access_ctx for checking expired/locked accounts. */
    access_ctx->sdap_access_ctx = talloc_zero(access_ctx, struct sdap_access_ctx);
    if (access_ctx->sdap_access_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed\n");
        ret = ENOMEM;
        goto done;
    }

    access_ctx->sdap_access_ctx->id_ctx = access_ctx->sdap_ctx;
    access_ctx->sdap_access_ctx->access_rule[0] = LDAP_ACCESS_EXPIRE;
    access_ctx->sdap_access_ctx->access_rule[1] = LDAP_ACCESS_EMPTY;

    dp_set_method(dp_methods, DPM_ACCESS_HANDLER,
                  ipa_pam_access_handler_send, ipa_pam_access_handler_recv, access_ctx,
                  struct ipa_access_ctx, struct pam_data, struct pam_data *);

    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(access_ctx);
    }

    return ret;
}

errno_t sssm_ipa_selinux_init(TALLOC_CTX *mem_ctx,
                              struct be_ctx *be_ctx,
                              void *module_data,
                              struct dp_method *dp_methods)
{
#if defined HAVE_SELINUX && defined HAVE_SELINUX_LOGIN_DIR
    struct ipa_selinux_ctx *selinux_ctx;
    struct ipa_init_ctx *init_ctx;
    struct ipa_options *opts;

    init_ctx = talloc_get_type(module_data, struct ipa_init_ctx);
    opts = init_ctx->options;

    selinux_ctx = talloc_zero(mem_ctx, struct ipa_selinux_ctx);
    if (selinux_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed.\n");
        return ENOMEM;
    }

    selinux_ctx->id_ctx = init_ctx->id_ctx;
    selinux_ctx->hbac_search_bases = opts->hbac_search_bases;
    selinux_ctx->host_search_bases = opts->host_search_bases;
    selinux_ctx->selinux_search_bases = opts->selinux_search_bases;

    dp_set_method(dp_methods, DPM_SELINUX_HANDLER,
                  ipa_selinux_handler_send, ipa_selinux_handler_recv, selinux_ctx,
                  struct ipa_selinux_ctx, struct pam_data, struct pam_data *);

    return EOK;
#else
    DEBUG(SSSDBG_MINOR_FAILURE, "SELinux init handler called but SSSD is "
                                "built without SSH support, ignoring\n");
    return EOK;
#endif
}

errno_t sssm_ipa_hostid_init(TALLOC_CTX *mem_ctx,
                             struct be_ctx *be_ctx,
                             void *module_data,
                             struct dp_method *dp_methods)
{
#ifdef BUILD_SSH
    struct ipa_hostid_ctx *hostid_ctx;
    struct ipa_init_ctx *init_ctx;

    init_ctx = talloc_get_type(module_data, struct ipa_init_ctx);

    hostid_ctx = talloc_zero(mem_ctx, struct ipa_hostid_ctx);
    if (hostid_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n");
        return ENOMEM;
    }

    hostid_ctx->sdap_id_ctx = init_ctx->id_ctx->sdap_id_ctx;
    hostid_ctx->host_search_bases = init_ctx->options->host_search_bases;
    hostid_ctx->ipa_opts = init_ctx->options;

    dp_set_method(dp_methods, DPM_HOSTID_HANDLER,
                  ipa_hostid_handler_send, ipa_hostid_handler_recv, hostid_ctx,
                  struct ipa_hostid_ctx, struct dp_hostid_data, struct dp_reply_std);

    return EOK;
#else
    DEBUG(SSSDBG_MINOR_FAILURE, "HostID init handler called but SSSD is "
                                "built without SSH support, ignoring\n");
    return EOK;
#endif
}

errno_t sssm_ipa_autofs_init(TALLOC_CTX *mem_ctx,
                             struct be_ctx *be_ctx,
                             void *module_data,
                             struct dp_method *dp_methods)
{
#ifdef BUILD_AUTOFS
    struct ipa_init_ctx *init_ctx;

    DEBUG(SSSDBG_TRACE_INTERNAL, "Initializing IPA autofs handler\n");
    init_ctx = talloc_get_type(module_data, struct ipa_init_ctx);

    return ipa_autofs_init(mem_ctx, be_ctx, init_ctx->id_ctx, dp_methods);
#else
    DEBUG(SSSDBG_MINOR_FAILURE, "Autofs init handler called but SSSD is "
                                "built without autofs support, ignoring\n");
    return EOK;
#endif
}

errno_t sssm_ipa_subdomains_init(TALLOC_CTX *mem_ctx,
                                 struct be_ctx *be_ctx,
                                 void *module_data,
                                 struct dp_method *dp_methods)
{
    struct ipa_init_ctx *init_ctx;

    DEBUG(SSSDBG_TRACE_INTERNAL, "Initializing IPA subdomains handler\n");
    init_ctx = talloc_get_type(module_data, struct ipa_init_ctx);

    return ipa_subdomains_init(mem_ctx, be_ctx, init_ctx->id_ctx, dp_methods);
}

errno_t sssm_ipa_sudo_init(TALLOC_CTX *mem_ctx,
                           struct be_ctx *be_ctx,
                           void *module_data,
                           struct dp_method *dp_methods)
{
#ifdef BUILD_SUDO
    struct ipa_init_ctx *init_ctx;

    DEBUG(SSSDBG_TRACE_INTERNAL, "Initializing IPA sudo handler\n");
    init_ctx = talloc_get_type(module_data, struct ipa_init_ctx);

    return ipa_sudo_init(mem_ctx, be_ctx, init_ctx->id_ctx, dp_methods);
#else
    DEBUG(SSSDBG_MINOR_FAILURE, "Sudo init handler called but SSSD is "
                                "built without sudo support, ignoring\n");
    return EOK;
#endif
}

errno_t sssm_ipa_session_init(TALLOC_CTX *mem_ctx,
                              struct be_ctx *be_ctx,
                              void *module_data,
                              struct dp_method *dp_methods)
{
    struct ipa_session_ctx *session_ctx;
    struct ipa_init_ctx *init_ctx;
    struct ipa_id_ctx *id_ctx;
    errno_t ret;

    init_ctx = talloc_get_type(module_data, struct ipa_init_ctx);
    id_ctx = init_ctx->id_ctx;

    session_ctx = talloc_zero(mem_ctx, struct ipa_session_ctx);
    if (session_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed.\n");

        return ENOMEM;
    }

    session_ctx->sdap_ctx = id_ctx->sdap_id_ctx;
    session_ctx->host_map = id_ctx->ipa_options->host_map;
    session_ctx->hostgroup_map = id_ctx->ipa_options->hostgroup_map;
    session_ctx->host_search_bases = id_ctx->ipa_options->host_search_bases;
    session_ctx->deskprofile_search_bases = id_ctx->ipa_options->deskprofile_search_bases;

    ret = dp_copy_options(session_ctx, id_ctx->ipa_options->basic,
                          IPA_OPTS_BASIC, &session_ctx->ipa_options);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "dp_copy_options() failed.\n");

        goto done;
    }

    dp_set_method(dp_methods, DPM_SESSION_HANDLER,
                  ipa_pam_session_handler_send, ipa_pam_session_handler_recv, session_ctx,
                  struct ipa_session_ctx, struct pam_data, struct pam_data *);

    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(session_ctx);
    }

    return ret;
}