~ubuntu-branches/ubuntu/saucy/sssd/saucy

« back to all changes in this revision

Viewing changes to server/providers/ipa/ipa_common.c

  • Committer: Stéphane Graber
  • Date: 2011-06-15 16:23:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: stgraber@ubuntu.com-20110615162314-rbhoppnpaxfqo5q7
Merge 1.5.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    SSSD
3
 
 
4
 
    IPA Provider Common Functions
5
 
 
6
 
    Authors:
7
 
        Simo Sorce <ssorce@redhat.com>
8
 
 
9
 
    Copyright (C) 2009 Red Hat
10
 
 
11
 
    This program is free software; you can redistribute it and/or modify
12
 
    it under the terms of the GNU General Public License as published by
13
 
    the Free Software Foundation; either version 3 of the License, or
14
 
    (at your option) any later version.
15
 
 
16
 
    This program is distributed in the hope that it will be useful,
17
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 
    GNU General Public License for more details.
20
 
 
21
 
    You should have received a copy of the GNU General Public License
22
 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 
*/
24
 
 
25
 
#include <netdb.h>
26
 
#include <ctype.h>
27
 
#include "providers/ipa/ipa_common.h"
28
 
 
29
 
struct dp_option ipa_basic_opts[] = {
30
 
    { "ipa_domain", DP_OPT_STRING, NULL_STRING, NULL_STRING },
31
 
    { "ipa_server", DP_OPT_STRING, NULL_STRING, NULL_STRING },
32
 
    { "ipa_hostname", DP_OPT_STRING, NULL_STRING, NULL_STRING },
33
 
};
34
 
 
35
 
struct dp_option ipa_def_ldap_opts[] = {
36
 
    { "ldap_uri", DP_OPT_STRING, NULL_STRING, NULL_STRING },
37
 
    { "ldap_search_base", DP_OPT_STRING, NULL_STRING, NULL_STRING },
38
 
    { "ldap_default_bind_dn", DP_OPT_STRING, NULL_STRING, NULL_STRING },
39
 
    { "ldap_default_authtok_type", DP_OPT_STRING, NULL_STRING, NULL_STRING},
40
 
    { "ldap_default_authtok", DP_OPT_BLOB, NULL_BLOB, NULL_BLOB },
41
 
    { "ldap_search_timeout", DP_OPT_NUMBER, { .number = 60 }, NULL_NUMBER },
42
 
    { "ldap_network_timeout", DP_OPT_NUMBER, { .number = 6 }, NULL_NUMBER },
43
 
    { "ldap_opt_timeout", DP_OPT_NUMBER, { .number = 6 }, NULL_NUMBER },
44
 
    { "ldap_tls_reqcert", DP_OPT_STRING, { "hard" }, NULL_STRING },
45
 
    { "ldap_user_search_base", DP_OPT_STRING, NULL_STRING, NULL_STRING },
46
 
    { "ldap_user_search_scope", DP_OPT_STRING, { "sub" }, NULL_STRING },
47
 
    { "ldap_user_search_filter", DP_OPT_STRING, NULL_STRING, NULL_STRING },
48
 
    { "ldap_group_search_base", DP_OPT_STRING, NULL_STRING, NULL_STRING },
49
 
    { "ldap_group_search_scope", DP_OPT_STRING, { "sub" }, NULL_STRING },
50
 
    { "ldap_group_search_filter", DP_OPT_STRING, NULL_STRING, NULL_STRING },
51
 
    { "ldap_schema", DP_OPT_STRING, { "ipa_v1" }, NULL_STRING },
52
 
    { "ldap_offline_timeout", DP_OPT_NUMBER, { .number = 60 }, NULL_NUMBER },
53
 
    { "ldap_force_upper_case_realm", DP_OPT_BOOL, BOOL_TRUE, BOOL_TRUE },
54
 
    { "ldap_enumeration_refresh_timeout", DP_OPT_NUMBER, { .number = 300 }, NULL_NUMBER },
55
 
    { "ldap_purge_cache_timeout", DP_OPT_NUMBER, { .number = 3600 }, NULL_NUMBER },
56
 
    { "entry_cache_timeout", DP_OPT_NUMBER, { .number = 1800 }, NULL_NUMBER },
57
 
    { "ldap_tls_cacert", DP_OPT_STRING, NULL_STRING, NULL_STRING },
58
 
    { "ldap_tls_cacertdir", DP_OPT_STRING, NULL_STRING, NULL_STRING },
59
 
    { "ldap_id_use_start_tls", DP_OPT_BOOL, BOOL_FALSE, BOOL_FALSE },
60
 
    { "ldap_sasl_mech", DP_OPT_STRING, { "GSSAPI" } , NULL_STRING },
61
 
    { "ldap_sasl_authid", DP_OPT_STRING, NULL_STRING, NULL_STRING },
62
 
    { "ldap_krb5_keytab", DP_OPT_STRING, NULL_STRING, NULL_STRING },
63
 
    { "ldap_krb5_init_creds", DP_OPT_BOOL, BOOL_TRUE, BOOL_TRUE },
64
 
    /* use the same parm name as the krb5 module so we set it only once */
65
 
    { "krb5_realm", DP_OPT_STRING, NULL_STRING, NULL_STRING }
66
 
};
67
 
 
68
 
struct sdap_attr_map ipa_attr_map[] = {
69
 
    { "ldap_entry_usn", "entryUSN", SYSDB_USN, NULL },
70
 
    { "ldap_rootdse_last_usn", "lastUSN", SYSDB_HIGH_USN, NULL }
71
 
};
72
 
 
73
 
struct sdap_attr_map ipa_user_map[] = {
74
 
    { "ldap_user_object_class", "posixAccount", SYSDB_USER_CLASS, NULL },
75
 
    { "ldap_user_name", "uid", SYSDB_NAME, NULL },
76
 
    { "ldap_user_pwd", "userPassword", SYSDB_PWD, NULL },
77
 
    { "ldap_user_uid_number", "uidNumber", SYSDB_UIDNUM, NULL },
78
 
    { "ldap_user_gid_number", "gidNumber", SYSDB_GIDNUM, NULL },
79
 
    { "ldap_user_gecos", "gecos", SYSDB_GECOS, NULL },
80
 
    { "ldap_user_home_directory", "homeDirectory", SYSDB_HOMEDIR, NULL },
81
 
    { "ldap_user_shell", "loginShell", SYSDB_SHELL, NULL },
82
 
    { "ldap_user_principal", "krbPrincipalName", SYSDB_UPN, NULL },
83
 
    { "ldap_user_fullname", "cn", SYSDB_FULLNAME, NULL },
84
 
    { "ldap_user_member_of", "memberOf", SYSDB_MEMBEROF, NULL },
85
 
    { "ldap_user_uuid", "nsUniqueId", SYSDB_UUID, NULL },
86
 
    { "ldap_user_modify_timestamp", "modifyTimestamp", SYSDB_ORIG_MODSTAMP, NULL },
87
 
    { "ldap_user_shadow_last_change", "shadowLastChange", SYSDB_SHADOWPW_LASTCHANGE, NULL },
88
 
    { "ldap_user_shadow_min", "shadowMin", SYSDB_SHADOWPW_MIN, NULL },
89
 
    { "ldap_user_shadow_max", "shadowMax", SYSDB_SHADOWPW_MAX, NULL },
90
 
    { "ldap_user_shadow_warning", "shadowWarning", SYSDB_SHADOWPW_WARNING, NULL },
91
 
    { "ldap_user_shadow_inactive", "shadowInactive", SYSDB_SHADOWPW_INACTIVE, NULL },
92
 
    { "ldap_user_shadow_expire", "shadowExpire", SYSDB_SHADOWPW_EXPIRE, NULL },
93
 
    { "ldap_user_shadow_flag", "shadowFlag", SYSDB_SHADOWPW_FLAG, NULL },
94
 
    { "ldap_user_krb_last_pwd_change", "krbLastPwdChange", SYSDB_KRBPW_LASTCHANGE, NULL },
95
 
    { "ldap_user_krb_password_expiration", "krbPasswordExpiration", SYSDB_KRBPW_EXPIRATION, NULL },
96
 
    { "ldap_pwd_attribute", "pwdAttribute", SYSDB_PWD_ATTRIBUTE, NULL }
97
 
};
98
 
 
99
 
struct sdap_attr_map ipa_group_map[] = {
100
 
    { "ldap_group_object_class", "posixGroup", SYSDB_GROUP_CLASS, NULL },
101
 
    { "ldap_group_name", "cn", SYSDB_NAME, NULL },
102
 
    { "ldap_group_pwd", "userPassword", SYSDB_PWD, NULL },
103
 
    { "ldap_group_gid_number", "gidNumber", SYSDB_GIDNUM, NULL },
104
 
    { "ldap_group_member", "member", SYSDB_MEMBER, NULL },
105
 
    { "ldap_group_uuid", "nsUniqueId", SYSDB_UUID, NULL },
106
 
    { "ldap_group_modify_timestamp", "modifyTimestamp", SYSDB_ORIG_MODSTAMP, NULL }
107
 
};
108
 
 
109
 
struct dp_option ipa_def_krb5_opts[] = {
110
 
    { "krb5_kdcip", DP_OPT_STRING, NULL_STRING, NULL_STRING },
111
 
    { "krb5_realm", DP_OPT_STRING, NULL_STRING, NULL_STRING },
112
 
    { "krb5_ccachedir", DP_OPT_STRING, { "/tmp" }, NULL_STRING },
113
 
    { "krb5_ccname_template", DP_OPT_STRING, { "FILE:%d/krb5cc_%U_XXXXXX" }, NULL_STRING},
114
 
    { "krb5_changepw_principal", DP_OPT_STRING, { "kadmin/changepw" }, NULL_STRING },
115
 
    { "krb5_auth_timeout", DP_OPT_NUMBER, { .number = 15 }, NULL_NUMBER },
116
 
    { "krb5_keytab", DP_OPT_STRING, { "/etc/krb5.keytab" }, NULL_STRING },
117
 
    { "krb5_validate", DP_OPT_BOOL, BOOL_TRUE, BOOL_TRUE }
118
 
};
119
 
 
120
 
int domain_to_basedn(TALLOC_CTX *memctx, const char *domain, char **basedn)
121
 
{
122
 
    const char *s;
123
 
    char *dn;
124
 
    char *p;
125
 
    int l;
126
 
 
127
 
    s = domain;
128
 
    dn = talloc_strdup(memctx, "dc=");
129
 
 
130
 
    while ((p = strchr(s, '.'))) {
131
 
        l = p - s;
132
 
        dn = talloc_asprintf_append_buffer(dn, "%.*s,dc=", l, s);
133
 
        if (!dn) {
134
 
            return ENOMEM;
135
 
        }
136
 
        s = p + 1;
137
 
    }
138
 
    dn = talloc_strdup_append_buffer(dn, s);
139
 
    if (!dn) {
140
 
        return ENOMEM;
141
 
    }
142
 
 
143
 
    *basedn = dn;
144
 
    return EOK;
145
 
}
146
 
 
147
 
int ipa_get_options(TALLOC_CTX *memctx,
148
 
                    struct confdb_ctx *cdb,
149
 
                    const char *conf_path,
150
 
                    struct sss_domain_info *dom,
151
 
                    struct ipa_options **_opts)
152
 
{
153
 
    struct ipa_options *opts;
154
 
    char *domain;
155
 
    char *server;
156
 
    char *ipa_hostname;
157
 
    int ret;
158
 
    char hostname[HOST_NAME_MAX + 1];
159
 
 
160
 
    opts = talloc_zero(memctx, struct ipa_options);
161
 
    if (!opts) return ENOMEM;
162
 
 
163
 
    ret = dp_get_options(opts, cdb, conf_path,
164
 
                         ipa_basic_opts,
165
 
                         IPA_OPTS_BASIC,
166
 
                         &opts->basic);
167
 
    if (ret != EOK) {
168
 
        goto done;
169
 
    }
170
 
 
171
 
    domain = dp_opt_get_string(opts->basic, IPA_DOMAIN);
172
 
    if (!domain) {
173
 
        ret = dp_opt_set_string(opts->basic, IPA_DOMAIN, dom->name);
174
 
        if (ret != EOK) {
175
 
            goto done;
176
 
        }
177
 
    }
178
 
 
179
 
    /* FIXME: Make non-fatal once we have discovery */
180
 
    server = dp_opt_get_string(opts->basic, IPA_SERVER);
181
 
    if (!server) {
182
 
        DEBUG(0, ("Can't find ipa server, missing option!\n"));
183
 
        ret = EINVAL;
184
 
        goto done;
185
 
    }
186
 
 
187
 
    ipa_hostname = dp_opt_get_string(opts->basic, IPA_HOSTNAME);
188
 
    if (ipa_hostname == NULL) {
189
 
        ret = gethostname(hostname, HOST_NAME_MAX);
190
 
        if (ret != EOK) {
191
 
            DEBUG(1, ("gethostname failed [%d][%s].\n", errno,
192
 
                      strerror(errno)));
193
 
            ret = errno;
194
 
            goto done;
195
 
        }
196
 
        hostname[HOST_NAME_MAX] = '\0';
197
 
        DEBUG(9, ("Setting ipa_hostname to [%s].\n", hostname));
198
 
        ret = dp_opt_set_string(opts->basic, IPA_HOSTNAME, hostname);
199
 
        if (ret != EOK) {
200
 
            goto done;
201
 
        }
202
 
    }
203
 
 
204
 
 
205
 
    ret = EOK;
206
 
    *_opts = opts;
207
 
 
208
 
done:
209
 
    if (ret != EOK) {
210
 
        talloc_zfree(opts);
211
 
    }
212
 
    return ret;
213
 
}
214
 
 
215
 
/* the following define is used to keep track of * the options in the ldap
216
 
 * module, so that if they change and ipa is not updated correspondingly
217
 
 * this will trigger a runtime abort error */
218
 
#define IPA_OPTS_BASIC_TEST 30
219
 
 
220
 
int ipa_get_id_options(struct ipa_options *ipa_opts,
221
 
                       struct confdb_ctx *cdb,
222
 
                       const char *conf_path,
223
 
                       struct sdap_options **_opts)
224
 
{
225
 
    TALLOC_CTX *tmpctx;
226
 
    char *hostname;
227
 
    char *basedn;
228
 
    char *realm;
229
 
    char *value;
230
 
    int ret;
231
 
    int i;
232
 
 
233
 
    /* self check test, this should never fail, unless someone forgot
234
 
     * to properly update the code after new ldap options have been added */
235
 
    if (SDAP_OPTS_BASIC != IPA_OPTS_BASIC_TEST) {
236
 
        DEBUG(0, ("Option numbers do not match (%d != %d)\n",
237
 
                   SDAP_OPTS_BASIC, IPA_OPTS_BASIC_TEST));
238
 
        abort();
239
 
    }
240
 
 
241
 
    tmpctx = talloc_new(ipa_opts);
242
 
    if (!tmpctx) {
243
 
        return ENOMEM;
244
 
    }
245
 
 
246
 
    ipa_opts->id = talloc_zero(ipa_opts, struct sdap_options);
247
 
    if (!ipa_opts->id) {
248
 
        ret = ENOMEM;
249
 
        goto done;
250
 
    }
251
 
 
252
 
    /* get sdap options */
253
 
    ret = dp_get_options(ipa_opts->id, cdb, conf_path,
254
 
                         ipa_def_ldap_opts,
255
 
                         SDAP_OPTS_BASIC,
256
 
                         &ipa_opts->id->basic);
257
 
    if (ret != EOK) {
258
 
        goto done;
259
 
    }
260
 
 
261
 
    if (NULL == dp_opt_get_string(ipa_opts->id->basic, SDAP_SEARCH_BASE)) {
262
 
        ret = domain_to_basedn(tmpctx,
263
 
                               dp_opt_get_string(ipa_opts->basic, IPA_DOMAIN),
264
 
                               &basedn);
265
 
        if (ret != EOK) {
266
 
            goto done;
267
 
        }
268
 
 
269
 
        /* FIXME: get values by querying IPA */
270
 
        /* set search base */
271
 
        value = talloc_asprintf(tmpctx, "cn=accounts,%s", basedn);
272
 
        if (!value) {
273
 
            ret = ENOMEM;
274
 
            goto done;
275
 
        }
276
 
        ret = dp_opt_set_string(ipa_opts->id->basic,
277
 
                                SDAP_SEARCH_BASE, value);
278
 
        if (ret != EOK) {
279
 
            goto done;
280
 
        }
281
 
        DEBUG(6, ("Option %s set to %s\n",
282
 
                  ipa_opts->id->basic[SDAP_SEARCH_BASE].opt_name,
283
 
                  dp_opt_get_string(ipa_opts->id->basic, SDAP_SEARCH_BASE)));
284
 
    }
285
 
 
286
 
    /* set the ldap_sasl_authid if the ipa_hostname override was specified */
287
 
    if (NULL == dp_opt_get_string(ipa_opts->id->basic, SDAP_SASL_AUTHID)) {
288
 
        hostname = dp_opt_get_string(ipa_opts->basic, IPA_HOSTNAME);
289
 
        if (hostname) {
290
 
            value = talloc_asprintf(tmpctx, "host/%s", hostname);
291
 
            if (!value) {
292
 
                ret = ENOMEM;
293
 
                goto done;
294
 
            }
295
 
            ret = dp_opt_set_string(ipa_opts->id->basic,
296
 
                                    SDAP_SASL_AUTHID, value);
297
 
            if (ret != EOK) {
298
 
                goto done;
299
 
            }
300
 
        }
301
 
        DEBUG(6, ("Option %s set to %s\n",
302
 
                  ipa_opts->id->basic[SDAP_SASL_AUTHID].opt_name,
303
 
                  dp_opt_get_string(ipa_opts->id->basic, SDAP_SASL_AUTHID)));
304
 
    }
305
 
 
306
 
    /* set krb realm */
307
 
    if (NULL == dp_opt_get_string(ipa_opts->id->basic, SDAP_KRB5_REALM)) {
308
 
        realm = dp_opt_get_string(ipa_opts->basic, IPA_DOMAIN);
309
 
        for (i = 0; realm[i]; i++) {
310
 
            realm[i] = toupper(realm[i]);
311
 
        }
312
 
        ret = dp_opt_set_string(ipa_opts->id->basic,
313
 
                                SDAP_KRB5_REALM, realm);
314
 
        if (ret != EOK) {
315
 
            goto done;
316
 
        }
317
 
        DEBUG(6, ("Option %s set to %s\n",
318
 
                  ipa_opts->id->basic[SDAP_KRB5_REALM].opt_name,
319
 
                  dp_opt_get_string(ipa_opts->id->basic, SDAP_KRB5_REALM)));
320
 
    }
321
 
 
322
 
    /* fix schema to IPAv1 for now */
323
 
    ipa_opts->id->schema_type = SDAP_SCHEMA_IPA_V1;
324
 
 
325
 
    /* set user/group search bases if they are not specified */
326
 
    if (NULL == dp_opt_get_string(ipa_opts->id->basic,
327
 
                                  SDAP_USER_SEARCH_BASE)) {
328
 
        ret = dp_opt_set_string(ipa_opts->id->basic, SDAP_USER_SEARCH_BASE,
329
 
                                dp_opt_get_string(ipa_opts->id->basic,
330
 
                                                  SDAP_SEARCH_BASE));
331
 
        if (ret != EOK) {
332
 
            goto done;
333
 
        }
334
 
        DEBUG(6, ("Option %s set to %s\n",
335
 
                  ipa_opts->id->basic[SDAP_USER_SEARCH_BASE].opt_name,
336
 
                  dp_opt_get_string(ipa_opts->id->basic,
337
 
                                    SDAP_USER_SEARCH_BASE)));
338
 
    }
339
 
 
340
 
    if (NULL == dp_opt_get_string(ipa_opts->id->basic,
341
 
                                  SDAP_GROUP_SEARCH_BASE)) {
342
 
        ret = dp_opt_set_string(ipa_opts->id->basic, SDAP_GROUP_SEARCH_BASE,
343
 
                                dp_opt_get_string(ipa_opts->id->basic,
344
 
                                                  SDAP_SEARCH_BASE));
345
 
        if (ret != EOK) {
346
 
            goto done;
347
 
        }
348
 
        DEBUG(6, ("Option %s set to %s\n",
349
 
                  ipa_opts->id->basic[SDAP_GROUP_SEARCH_BASE].opt_name,
350
 
                  dp_opt_get_string(ipa_opts->id->basic,
351
 
                                    SDAP_GROUP_SEARCH_BASE)));
352
 
    }
353
 
 
354
 
    ret = sdap_get_map(ipa_opts->id, cdb, conf_path,
355
 
                       ipa_attr_map,
356
 
                       SDAP_AT_GENERAL,
357
 
                       &ipa_opts->id->gen_map);
358
 
    if (ret != EOK) {
359
 
        goto done;
360
 
    }
361
 
 
362
 
    ret = sdap_get_map(ipa_opts->id,
363
 
                       cdb, conf_path,
364
 
                       ipa_user_map,
365
 
                       SDAP_OPTS_USER,
366
 
                       &ipa_opts->id->user_map);
367
 
    if (ret != EOK) {
368
 
        goto done;
369
 
    }
370
 
 
371
 
    ret = sdap_get_map(ipa_opts->id,
372
 
                       cdb, conf_path,
373
 
                       ipa_group_map,
374
 
                       SDAP_OPTS_GROUP,
375
 
                       &ipa_opts->id->group_map);
376
 
    if (ret != EOK) {
377
 
        goto done;
378
 
    }
379
 
 
380
 
    ret = EOK;
381
 
    *_opts = ipa_opts->id;
382
 
 
383
 
done:
384
 
    talloc_zfree(tmpctx);
385
 
    if (ret != EOK) {
386
 
        talloc_zfree(ipa_opts->id);
387
 
    }
388
 
    return ret;
389
 
}
390
 
 
391
 
/* the following define is used to keep track of * the options in the krb5
392
 
 * module, so that if they change and ipa is not updated correspondingly
393
 
 * this will trigger a runtime abort error */
394
 
#define IPA_KRB5_OPTS_TEST 8
395
 
 
396
 
int ipa_get_auth_options(struct ipa_options *ipa_opts,
397
 
                         struct confdb_ctx *cdb,
398
 
                         const char *conf_path,
399
 
                         struct dp_option **_opts)
400
 
{
401
 
    char *value;
402
 
    int ret;
403
 
    int i;
404
 
 
405
 
    /* self check test, this should never fail, unless someone forgot
406
 
     * to properly update the code after new ldap options have been added */
407
 
    if (KRB5_OPTS != IPA_KRB5_OPTS_TEST) {
408
 
        DEBUG(0, ("Option numbers do not match (%d != %d)\n",
409
 
                   KRB5_OPTS, IPA_KRB5_OPTS_TEST));
410
 
        abort();
411
 
    }
412
 
 
413
 
    ipa_opts->auth = talloc_zero(ipa_opts, struct dp_option);
414
 
    if (ipa_opts->auth == NULL) {
415
 
        ret = ENOMEM;
416
 
        goto done;
417
 
    }
418
 
 
419
 
    /* get krb5 options */
420
 
    ret = dp_get_options(ipa_opts, cdb, conf_path,
421
 
                         ipa_def_krb5_opts,
422
 
                         KRB5_OPTS, &ipa_opts->auth);
423
 
    if (ret != EOK) {
424
 
        goto done;
425
 
    }
426
 
 
427
 
    /* set krb realm */
428
 
    if (NULL == dp_opt_get_string(ipa_opts->auth, KRB5_REALM)) {
429
 
        value = dp_opt_get_string(ipa_opts->basic, IPA_DOMAIN);
430
 
        if (!value) {
431
 
            ret = ENOMEM;
432
 
            goto done;
433
 
        }
434
 
        for (i = 0; value[i]; i++) {
435
 
            value[i] = toupper(value[i]);
436
 
        }
437
 
        ret = dp_opt_set_string(ipa_opts->auth, KRB5_REALM, value);
438
 
        if (ret != EOK) {
439
 
            goto done;
440
 
        }
441
 
        DEBUG(6, ("Option %s set to %s\n",
442
 
                  ipa_opts->auth[KRB5_REALM].opt_name,
443
 
                  dp_opt_get_string(ipa_opts->auth, KRB5_REALM)));
444
 
    }
445
 
 
446
 
    *_opts = ipa_opts->auth;
447
 
    ret = EOK;
448
 
 
449
 
done:
450
 
    if (ret != EOK) {
451
 
        talloc_zfree(ipa_opts->auth);
452
 
    }
453
 
    return ret;
454
 
}
455
 
 
456
 
static void ipa_resolve_callback(void *private_data, struct fo_server *server)
457
 
{
458
 
    struct ipa_service *service;
459
 
    struct hostent *srvaddr;
460
 
    char *address;
461
 
    char *new_uri;
462
 
    int ret;
463
 
 
464
 
    service = talloc_get_type(private_data, struct ipa_service);
465
 
    if (!service) {
466
 
        DEBUG(1, ("FATAL: Bad private_data\n"));
467
 
        return;
468
 
    }
469
 
 
470
 
    srvaddr = fo_get_server_hostent(server);
471
 
    if (!srvaddr) {
472
 
        DEBUG(1, ("FATAL: No hostent available for server (%s)\n",
473
 
                  fo_get_server_name(server)));
474
 
        return;
475
 
    }
476
 
 
477
 
    address = talloc_asprintf(service, srvaddr->h_name);
478
 
    if (!address) {
479
 
        DEBUG(1, ("Failed to copy address ...\n"));
480
 
        return;
481
 
    }
482
 
 
483
 
    new_uri = talloc_asprintf(service, "ldap://%s", address);
484
 
    if (!new_uri) {
485
 
        DEBUG(2, ("Failed to copy URI ...\n"));
486
 
        talloc_free(address);
487
 
        return;
488
 
    }
489
 
 
490
 
    /* free old one and replace with new one */
491
 
    talloc_zfree(service->sdap->uri);
492
 
    service->sdap->uri = new_uri;
493
 
    talloc_zfree(service->krb5_service->address);
494
 
    service->krb5_service->address = address;
495
 
 
496
 
    ret = write_kdcinfo_file(service->krb5_service->realm, address);
497
 
    if (ret != EOK) {
498
 
        DEBUG(2, ("write_kdcinfo_file failed, authentication might fail.\n"));
499
 
    }
500
 
 
501
 
}
502
 
 
503
 
int ipa_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
504
 
                     const char *servers, const char *domain,
505
 
                     struct ipa_service **_service)
506
 
{
507
 
    TALLOC_CTX *tmp_ctx;
508
 
    struct ipa_service *service;
509
 
    char **list = NULL;
510
 
    char *realm;
511
 
    int ret;
512
 
    int i;
513
 
 
514
 
    tmp_ctx = talloc_new(memctx);
515
 
    if (!tmp_ctx) {
516
 
        return ENOMEM;
517
 
    }
518
 
 
519
 
    service = talloc_zero(tmp_ctx, struct ipa_service);
520
 
    if (!service) {
521
 
        ret = ENOMEM;
522
 
        goto done;
523
 
    }
524
 
    service->sdap = talloc_zero(service, struct sdap_service);
525
 
    if (!service->sdap) {
526
 
        ret = ENOMEM;
527
 
        goto done;
528
 
    }
529
 
    service->krb5_service = talloc_zero(service, struct krb5_service);
530
 
    if (!service->krb5_service) {
531
 
        ret = ENOMEM;
532
 
        goto done;
533
 
    }
534
 
 
535
 
    ret = be_fo_add_service(ctx, "IPA");
536
 
    if (ret != EOK) {
537
 
        DEBUG(1, ("Failed to create failover service!\n"));
538
 
        goto done;
539
 
    }
540
 
 
541
 
    service->sdap->name = talloc_strdup(service, "IPA");
542
 
    if (!service->sdap->name) {
543
 
        ret = ENOMEM;
544
 
        goto done;
545
 
    }
546
 
 
547
 
    service->krb5_service->name = talloc_strdup(service, "IPA");
548
 
    if (!service->krb5_service->name) {
549
 
        ret = ENOMEM;
550
 
        goto done;
551
 
    }
552
 
 
553
 
    realm = talloc_strdup(service, domain);
554
 
    if (!realm) {
555
 
        ret = ENOMEM;
556
 
        goto done;
557
 
    }
558
 
    for (i = 0; realm[i]; i++) {
559
 
        realm[i] = toupper(realm[i]);
560
 
    }
561
 
    service->krb5_service->realm = realm;
562
 
 
563
 
    /* split server parm into a list */
564
 
    ret = split_on_separator(tmp_ctx, servers, ',', true, &list, NULL);
565
 
    if (ret != EOK) {
566
 
        DEBUG(1, ("Failed to parse server list!\n"));
567
 
        goto done;
568
 
    }
569
 
 
570
 
    /* now for each one add a new server to the failover service */
571
 
    for (i = 0; list[i]; i++) {
572
 
 
573
 
        talloc_steal(service, list[i]);
574
 
 
575
 
        ret = be_fo_add_server(ctx, "IPA", list[i], 0, NULL);
576
 
        if (ret && ret != EEXIST) {
577
 
            DEBUG(0, ("Failed to add server\n"));
578
 
            goto done;
579
 
        }
580
 
 
581
 
        DEBUG(6, ("Added Server %s\n", list[i]));
582
 
    }
583
 
 
584
 
    ret = be_fo_service_add_callback(memctx, ctx, "IPA",
585
 
                                     ipa_resolve_callback, service);
586
 
    if (ret != EOK) {
587
 
        DEBUG(1, ("Failed to add failover callback!\n"));
588
 
        goto done;
589
 
    }
590
 
 
591
 
    ret = EOK;
592
 
 
593
 
done:
594
 
    if (ret == EOK) {
595
 
        *_service = talloc_steal(memctx, service);
596
 
    }
597
 
    talloc_zfree(tmp_ctx);
598
 
    return ret;
599
 
}
600