~ubuntu-branches/ubuntu/raring/freeipa/raring-proposed

« back to all changes in this revision

Viewing changes to .pc/fix-string-format.diff/daemons/ipa-slapi-plugins/ipa-enrollment/ipa_enrollment.c

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-22 00:17:10 UTC
  • Revision ID: package-import@ubuntu.com-20120322001710-o0j98p04zsq48nm9
Tags: 2.1.4-0ubuntu1
* Merge from unreleased debian git (LP: #259547, #958599).
* Build only the client package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * This program is free software; you can redistribute it and/or modify
 
3
 * it under the terms of the GNU General Public License as published by
 
4
 * the Free Software Foundation, either version 3 of the License, or
 
5
 * (at your option) any later version.
 
6
 *
 
7
 * This program is distributed in the hope that it will be useful,
 
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 * GNU General Public License for more details.
 
11
 *
 
12
 * You should have received a copy of the GNU General Public License
 
13
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
 *
 
15
 * Additional permission under GPLv3 section 7:
 
16
 *
 
17
 * In the following paragraph, "GPL" means the GNU General Public
 
18
 * License, version 3 or any later version, and "Non-GPL Code" means
 
19
 * code that is governed neither by the GPL nor a license
 
20
 * compatible with the GPL.
 
21
 *
 
22
 * You may link the code of this Program with Non-GPL Code and convey
 
23
 * linked combinations including the two, provided that such Non-GPL
 
24
 * Code only links to the code of this Program through those well
 
25
 * defined interfaces identified in the file named EXCEPTION found in
 
26
 * the source code files (the "Approved Interfaces"). The files of
 
27
 * Non-GPL Code may instantiate templates or use macros or inline
 
28
 * functions from the Approved Interfaces without causing the resulting
 
29
 * work to be covered by the GPL. Only the copyright holders of this
 
30
 * Program may make changes or additions to the list of Approved
 
31
 * Interfaces.
 
32
 *
 
33
 * Copyright (C) 2005 Red Hat, Inc.
 
34
 * All rights reserved.
 
35
 * END COPYRIGHT BLOCK **/
 
36
 
 
37
#ifdef HAVE_CONFIG_H
 
38
#  include <config.h>
 
39
#endif
 
40
 
 
41
/*
 
42
 * Enroll a host into the IPA domain.
 
43
 *
 
44
 */
 
45
 
 
46
#include <stdio.h>
 
47
#include <string.h>
 
48
#include <dirsrv/slapi-plugin.h>
 
49
#include <krb5.h>
 
50
 
 
51
#include "util.h"
 
52
 
 
53
#define IPA_PLUGIN_NAME "ipa-enrollment"
 
54
 
 
55
/* OID of the extended operation handled by this plug-in */
 
56
#define JOIN_OID    "2.16.840.1.113730.3.8.10.3"
 
57
 
 
58
Slapi_PluginDesc pdesc = {
 
59
    IPA_PLUGIN_NAME,
 
60
    "IPA Project",
 
61
    "IPA/2.0",
 
62
    "IPA Enrollment Extended Operation plugin"
 
63
};
 
64
 
 
65
static char *ipaenrollment_oid_list[] = {
 
66
        JOIN_OID,
 
67
        NULL
 
68
};
 
69
 
 
70
static char *ipaenrollment_name_list[] = {
 
71
        "Enrollment Extended Operation",
 
72
        NULL
 
73
};
 
74
 
 
75
static void *ipaenrollment_plugin_id;
 
76
 
 
77
static char *realm;
 
78
static const char *ipa_realm_dn;
 
79
 
 
80
static int
 
81
ipaenrollement_secure(Slapi_PBlock *pb, char **errMesg)
 
82
{
 
83
    int ssf;
 
84
    int rc = LDAP_SUCCESS;
 
85
 
 
86
    LOG_TRACE("=> ipaenrollment_secure\n");
 
87
 
 
88
    /* Allow enrollment on all connections with a Security Strength
 
89
     * Factor (SSF) higher than 1 */
 
90
    if (slapi_pblock_get(pb, SLAPI_OPERATION_SSF, &ssf) != 0) {
 
91
        LOG_TRACE("Could not get SSF from connection\n");
 
92
        *errMesg = "Operation requires a secure connection.\n";
 
93
        rc = LDAP_OPERATIONS_ERROR;
 
94
        goto done;
 
95
    }
 
96
 
 
97
    if (NULL == realm) {
 
98
        *errMesg = "Kerberos realm is not set.\n";
 
99
        LOG_FATAL("%s", errMesg);
 
100
        rc = LDAP_OPERATIONS_ERROR;
 
101
        goto done;
 
102
    }
 
103
 
 
104
    if (ssf <= 1) {
 
105
        *errMesg = "Operation requires a secure connection.\n";
 
106
        rc = LDAP_CONFIDENTIALITY_REQUIRED;
 
107
        goto done;
 
108
    }
 
109
 
 
110
done:
 
111
    LOG_TRACE("<= ipaenrollment_secure\n");
 
112
    return rc;
 
113
 
 
114
}
 
115
 
 
116
/* The extop call passes in the FQDN of the host to enroll.
 
117
 * We take that and set the krbPrincipalName and add the appropriate
 
118
 * objectclasses, then return krbPrincipalName. The caller should take
 
119
 * this and pass it to ipa-getkeytab to generate the keytab.
 
120
 *
 
121
 * The password for the entry is removed by ipa-getkeytab.
 
122
 */
 
123
static int
 
124
ipa_join(Slapi_PBlock *pb)
 
125
{
 
126
    char *bindDN = NULL;
 
127
    char *errMesg = NULL;
 
128
    struct berval *extop_value = NULL;
 
129
    Slapi_PBlock *pbte = NULL;
 
130
    Slapi_PBlock *pbtm = NULL;
 
131
    Slapi_Entry *targetEntry=NULL;
 
132
    Slapi_DN *sdn;
 
133
    Slapi_Backend *be;
 
134
    Slapi_Entry **es = NULL;
 
135
    int rc=0, ret=0, res, i;
 
136
    int is_root=0;
 
137
    char *krbLastPwdChange = NULL;
 
138
    char *fqdn = NULL;
 
139
    Slapi_Mods *smods;
 
140
    char *attrlist[] = {"fqdn", "krbPrincipalKey", "krbLastPwdChange", "krbPrincipalName", NULL };
 
141
    char * filter;
 
142
 
 
143
    int scope = LDAP_SCOPE_SUBTREE;
 
144
    char *principal = NULL;
 
145
    struct berval retbval;
 
146
 
 
147
    if (NULL == realm) {
 
148
        errMesg = "Kerberos realm is not set.\n";
 
149
        LOG_FATAL("%s", errMesg);
 
150
        rc = LDAP_OPERATIONS_ERROR;
 
151
        goto free_and_return;
 
152
    }
 
153
 
 
154
    /* Get Bind DN */
 
155
    slapi_pblock_get(pb, SLAPI_CONN_DN, &bindDN);
 
156
 
 
157
     /* If the connection is bound anonymously we must refuse to process
 
158
      * this operation.
 
159
      */
 
160
    if (bindDN == NULL || *bindDN == '\0') {
 
161
        /* Refuse the operation because they're bound anonymously */
 
162
        errMesg = "Anonymous Binds are not allowed.\n";
 
163
        rc = LDAP_INSUFFICIENT_ACCESS;
 
164
        goto free_and_return;
 
165
    }
 
166
 
 
167
    /* Get the ber value of the extended operation */
 
168
    slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_value);
 
169
 
 
170
    /* We are passed in the FQDN of the host to enroll. Do an internal
 
171
     * search and pull that entry.
 
172
     */
 
173
    filter = slapi_ch_smprintf("(fqdn=%s)", extop_value->bv_val);
 
174
    pbte = slapi_pblock_new();
 
175
    slapi_search_internal_set_pb(pbte,
 
176
            ipa_realm_dn, scope, filter, attrlist, 0,
 
177
            NULL, /* Controls */
 
178
            NULL, /* UniqueID */
 
179
            ipaenrollment_plugin_id,
 
180
            0); /* Flags */
 
181
 
 
182
    /* do search the tree */
 
183
    ret = slapi_search_internal_pb(pbte);
 
184
    slapi_pblock_get(pbte, SLAPI_PLUGIN_INTOP_RESULT, &res);
 
185
    if (ret == -1 || res != LDAP_SUCCESS) {
 
186
        LOG_TRACE("Search for host failed, err (%d)\n", res?res:ret);
 
187
        errMesg = "Host not found.\n";
 
188
        rc = LDAP_NO_SUCH_OBJECT;
 
189
        goto free_and_return;
 
190
    }
 
191
 
 
192
    /* get entries */
 
193
    slapi_pblock_get(pbte, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &es);
 
194
    if (!es) {
 
195
        LOG_TRACE("No entries ?!");
 
196
        errMesg = "Host not found.\n";
 
197
        rc = LDAP_NO_SUCH_OBJECT;
 
198
        goto free_and_return;
 
199
    }
 
200
 
 
201
    /* count entries */
 
202
    for (i = 0; es[i]; i++) /* count */ ;
 
203
 
 
204
    /* if there is none or more than one, freak out */
 
205
    if (i != 1) {
 
206
        LOG_TRACE("Too many entries, or entry no found (%d)", i);
 
207
        errMesg = "Host not found.\n";
 
208
        rc = LDAP_NO_SUCH_OBJECT;
 
209
        goto free_and_return;
 
210
    }
 
211
    targetEntry = es[0];
 
212
 
 
213
    /* Is this host already enrolled? */
 
214
    krbLastPwdChange = slapi_entry_attr_get_charptr(targetEntry, "krbLastPwdChange");
 
215
    if (NULL != krbLastPwdChange) {
 
216
        LOG_TRACE("Host already enrolled");
 
217
        errMesg = "Host already enrolled.\n";
 
218
        rc = LDAP_OPERATIONS_ERROR;
 
219
        goto free_and_return;
 
220
    }
 
221
 
 
222
    /* First thing to do is to ask access control if the bound identity has
 
223
     * rights to modify the userpassword attribute on this entry. If not,
 
224
     * then we fail immediately with insufficient access. This means that
 
225
     * we don't leak any useful information to the client such as current
 
226
     * password wrong, etc.
 
227
     */
 
228
 
 
229
    is_root = slapi_dn_isroot(bindDN);
 
230
    if (slapi_pblock_set(pb, SLAPI_REQUESTOR_ISROOT, &is_root)) {
 
231
        LOG_FATAL("slapi_pblock_set failed!\n");
 
232
        rc = LDAP_OPERATIONS_ERROR;
 
233
        goto free_and_return;
 
234
    }
 
235
 
 
236
    /* In order to perform the access control check,
 
237
     * we need to select a backend (even though
 
238
     * we don't actually need it otherwise).
 
239
     */
 
240
    sdn = slapi_sdn_new_dn_byval(bindDN);
 
241
    be = slapi_be_select(sdn);
 
242
    if (slapi_pblock_set(pb, SLAPI_BACKEND, be)) {
 
243
        LOG_FATAL("slapi_pblock_set failed!\n");
 
244
        rc = LDAP_OPERATIONS_ERROR;
 
245
        goto free_and_return;
 
246
    }
 
247
 
 
248
    /* Access Strategy:
 
249
     * If the user has WRITE-ONLY access, a new keytab is set on the entry.
 
250
     */
 
251
 
 
252
    ret = slapi_access_allowed(pb, targetEntry, "krbPrincipalKey", NULL, SLAPI_ACL_WRITE);
 
253
    if (ret != LDAP_SUCCESS) {
 
254
        errMesg = "Insufficient access rights\n";
 
255
        rc = LDAP_INSUFFICIENT_ACCESS;
 
256
        goto free_and_return;
 
257
    }
 
258
 
 
259
    /* If a principal is already set return the name */
 
260
    principal = slapi_entry_attr_get_charptr(targetEntry, "krbPrincipalName");
 
261
    if (NULL != principal)
 
262
        goto done;
 
263
 
 
264
    /* Add the elements needed for enrollment */
 
265
    smods = slapi_mods_new();
 
266
    fqdn = slapi_entry_attr_get_charptr(targetEntry, "fqdn");
 
267
    principal = slapi_ch_smprintf("host/%s@%s", fqdn, realm);
 
268
    slapi_mods_add_string(smods, LDAP_MOD_ADD, "krbPrincipalName", principal);
 
269
    slapi_mods_add_string(smods, LDAP_MOD_ADD, "objectClass", "krbPrincipalAux");
 
270
 
 
271
    pbtm = slapi_pblock_new();
 
272
    slapi_modify_internal_set_pb (pbtm, slapi_entry_get_dn_const(targetEntry),
 
273
        slapi_mods_get_ldapmods_byref(smods),
 
274
        NULL, /* Controls */
 
275
        NULL, /* UniqueID */
 
276
        ipaenrollment_plugin_id, /* PluginID */
 
277
        0); /* Flags */
 
278
 
 
279
    rc = slapi_modify_internal_pb (pbtm);
 
280
    if (rc) {
 
281
        LOG_TRACE("WARNING: modify error %d on entry '%s'\n",
 
282
                  rc, slapi_entry_get_dn_const(targetEntry));
 
283
    } else {
 
284
        slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
 
285
 
 
286
        if (rc != LDAP_SUCCESS){
 
287
            LOG_TRACE("WARNING: modify error %d on entry '%s'\n",
 
288
                      rc, slapi_entry_get_dn_const(targetEntry));
 
289
        } else {
 
290
            LOG_TRACE("<= apply mods: Successful\n");
 
291
        }
 
292
    }
 
293
 
 
294
done:
 
295
    /* Return the krbprincipalname */
 
296
    retbval.bv_val = principal;
 
297
    retbval.bv_len = strlen(principal);
 
298
 
 
299
    ret = slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, JOIN_OID);
 
300
    if (!ret) ret = slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, &retbval);
 
301
    if (ret) {
 
302
        errMesg = "Could not set return values";
 
303
        LOG("%s\n", errMesg);
 
304
        rc = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
 
305
    }
 
306
 
 
307
    /* Free anything that we allocated above */
 
308
free_and_return:
 
309
 
 
310
    if (pbte) {
 
311
        slapi_free_search_results_internal(pbte);
 
312
        slapi_pblock_destroy(pbte);
 
313
    }
 
314
    if (pbtm) {
 
315
        slapi_pblock_destroy(pbtm);
 
316
    }
 
317
 
 
318
    if (krbLastPwdChange) slapi_ch_free_string(&krbLastPwdChange);
 
319
 
 
320
    LOG(errMesg ? errMesg : "success\n");
 
321
    slapi_send_ldap_result(pb, rc, NULL, errMesg, 0, NULL);
 
322
 
 
323
    free(principal);
 
324
 
 
325
    return SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
 
326
}
 
327
 
 
328
/* Extended operation plug-in */
 
329
static int
 
330
ipaenrollment_extop(Slapi_PBlock *pb)
 
331
{
 
332
    char *oid;
 
333
    char *errMesg = NULL;
 
334
    int rc, ret;
 
335
 
 
336
    LOG_TRACE("=> ipaenrollment_extop\n");
 
337
 
 
338
    rc = ipaenrollement_secure(pb, &errMesg);
 
339
    if (rc) {
 
340
        goto free_and_return;
 
341
    }
 
342
 
 
343
    /* Get the OID and the value included in the request */
 
344
    if (slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &oid ) != 0) {
 
345
        errMesg = "Could not get OID and value from request.\n";
 
346
        rc = LDAP_OPERATIONS_ERROR;
 
347
        LOG(errMesg);
 
348
        goto free_and_return;
 
349
    }
 
350
 
 
351
    if (strcasecmp(oid, JOIN_OID) == 0) {
 
352
        ret = ipa_join(pb);
 
353
        return ret;
 
354
    }
 
355
 
 
356
    errMesg = "Request OID does not match supported OIDs.\n";
 
357
    rc = LDAP_OPERATIONS_ERROR;
 
358
 
 
359
free_and_return:
 
360
    LOG(errMesg);
 
361
    slapi_send_ldap_result(pb, rc, NULL, errMesg, 0, NULL);
 
362
 
 
363
    return SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
 
364
}
 
365
 
 
366
static int
 
367
ipaenrollment_start(Slapi_PBlock *pb)
 
368
{
 
369
    krb5_error_code krberr;
 
370
    krb5_context krbctx;
 
371
    char *config_dn = NULL;
 
372
    char *partition_dn = NULL;
 
373
    Slapi_Entry *config_entry = NULL;
 
374
    int ret = LDAP_SUCCESS;
 
375
    Slapi_DN *sdn;
 
376
    int rc = 0;
 
377
 
 
378
    krberr = krb5_init_context(&krbctx);
 
379
    if (krberr) {
 
380
        LOG_FATAL("krb5_init_context failed\n");
 
381
        /* Yes, we failed, but it is because /etc/krb5.conf doesn't exist
 
382
         * or is misconfigured. Start up in a degraded mode.
 
383
         */
 
384
        goto done;
 
385
    }
 
386
 
 
387
    krberr = krb5_get_default_realm(krbctx, &realm);
 
388
    if (krberr) {
 
389
        realm = NULL;
 
390
        LOG_FATAL("Failed to get default realm?!\n");
 
391
        goto done;
 
392
    }
 
393
 
 
394
    if (slapi_pblock_get(pb, SLAPI_TARGET_DN, &config_dn) != 0) {
 
395
        LOG_FATAL("No config DN?\n");
 
396
        goto done;
 
397
    }
 
398
    sdn = slapi_sdn_new_dn_byref(config_dn);
 
399
    if ((rc = slapi_search_internal_get_entry(sdn, NULL, &config_entry,
 
400
                                ipaenrollment_plugin_id)) != LDAP_SUCCESS ){
 
401
        LOG_TRACE("ipaenrollment_start: No such entry-(%s), err (%d)\n",
 
402
                  config_dn, rc);
 
403
    }
 
404
    slapi_sdn_free(&sdn);
 
405
 
 
406
    partition_dn = slapi_entry_attr_get_charptr(config_entry, "nsslapd-realmtree");
 
407
    if (!partition_dn) {
 
408
        LOG_FATAL("Missing partition configuration entry (nsslapd-realmTree)!\n");
 
409
        ret = LDAP_OPERATIONS_ERROR;
 
410
        goto done;
 
411
    }
 
412
 
 
413
    ipa_realm_dn = slapi_ch_smprintf("cn=computers,cn=accounts,%s", partition_dn);
 
414
    slapi_ch_free_string(&partition_dn);
 
415
    if (!ipa_realm_dn) {
 
416
        LOG_FATAL("Out of memory ?\n");
 
417
        ret = LDAP_OPERATIONS_ERROR;
 
418
        goto done;
 
419
    }
 
420
 
 
421
done:
 
422
    if (krbctx) krb5_free_context(krbctx);
 
423
    if (config_entry) slapi_entry_free(config_entry);
 
424
 
 
425
    return ret;
 
426
}
 
427
 
 
428
int
 
429
ipaenrollment_init(Slapi_PBlock *pb)
 
430
{
 
431
    int ret;
 
432
 
 
433
    /* Get the arguments appended to the plugin extendedop directive
 
434
     * in the plugin entry.  The first argument
 
435
     * (after the standard arguments for the directive) should
 
436
     * contain the OID of the extended op.
 
437
    */
 
438
 
 
439
    ret = slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &ipaenrollment_plugin_id);
 
440
    if ((ret != 0) || (NULL == ipaenrollment_plugin_id)) {
 
441
        LOG("Could not get identity or identity was NULL\n");
 
442
        return -1;
 
443
    }
 
444
 
 
445
    LOG("Registering plug-in for extended op.\n");
 
446
 
 
447
    /* Register the plug-in function as an extended operation
 
448
       plug-in function. */
 
449
    ret = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01);
 
450
    if (!ret) ret = slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, (void *)ipaenrollment_start);
 
451
    if (!ret) ret = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, (void *)&pdesc);
 
452
    if (!ret) ret = slapi_pblock_set(pb, SLAPI_PLUGIN_EXT_OP_OIDLIST, ipaenrollment_oid_list);
 
453
    if (!ret) ret = slapi_pblock_set(pb, SLAPI_PLUGIN_EXT_OP_NAMELIST, ipaenrollment_name_list);
 
454
    if (!ret) slapi_pblock_set(pb, SLAPI_PLUGIN_EXT_OP_FN, (void *)ipaenrollment_extop);
 
455
 
 
456
    if (ret) {
 
457
        LOG("Failed to set plug-in version, function, and OID.\n");
 
458
        return -1;
 
459
    }
 
460
 
 
461
    return 0;
 
462
}