summaryrefslogtreecommitdiffstats
path: root/service_passwords.c
blob: fdd4bbddc75b7a3188c4fdea2553693f0025d916 (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
#include <stdio.h>
#include <string.h>
#include <syslog.h>

#include <slapi-plugin.h>

/* This is missing in slapi-plugin.h: */
extern int slapi_pw_find(struct berval **vals, struct berval *v);

static Slapi_ComponentId *plugin_id = NULL;

/* Get an entry specified by a DN and with the specified attributes. */
static int get_entry(const char *dn, char **attrs, Slapi_Entry **entry)
{
    Slapi_Entry **entries = NULL;
    Slapi_PBlock *pb = NULL;
    int rc = 0;

    if (entry) {
        *entry = NULL;
    }

    pb = slapi_pblock_new();
    slapi_search_internal_set_pb(
        pb,
        dn,
        LDAP_SCOPE_BASE,
        "(objectclass=*)",
        attrs,
        0 /* attrsonly */,
        NULL /* controls */,
        NULL /* uniqueid */,
        plugin_id,
        0 /* actions */);

    slapi_search_internal_pb(pb);
    slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);

    if (LDAP_SUCCESS == rc) {
        slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);

        if (NULL != entries && NULL != entries[0]) {
            if (entry) {
                Slapi_Entry *tmp = NULL;
                tmp = entries[0];
                *entry = slapi_entry_dup(tmp);
            }
        } else {
            /* No entry there */
            rc = LDAP_NO_SUCH_OBJECT;
        }
    }


    slapi_free_search_results_internal(pb);
    slapi_pblock_destroy(pb);
    pb = NULL;

    return rc;
}

/* Check if the parent entry contains a uid attribute. */
static int dn_contains_uid(const char *dn)
{
    char *attrs[] = { "uid", NULL };
    Slapi_Entry *entry = NULL;
    Slapi_Attr *attr = NULL;
    Slapi_ValueSet *uid_values = NULL;

    int rc = 0;

    rc |= get_entry(dn, attrs, &entry);
    if (rc != 0 || entry == NULL) {
        /* parent_dn not found */
        rc = 1;
        goto fail1;
    }

    rc |= slapi_entry_attr_find(entry, "uid", &attr);
    if (rc != 0 || attr == NULL) {
        /* no uid attribute */
        rc = 1;
        goto fail1;
    }

    rc |= slapi_attr_get_valueset(attr, &uid_values);
    if (rc != 0 || slapi_valueset_count(uid_values) == 0) {
        /* empty uid value */
        rc = 1;
        goto fail1;
    }

fail1:
    slapi_entry_free(entry);

    return rc;
}

static char* is_service_dn(const char *dn)
{
    char *service = NULL;

    Slapi_Entry *entry = NULL;
    char *attrs[] = { "cn", NULL };
    Slapi_Attr *attr = NULL;
    struct berval **cn = NULL;

    int rc = 0;

    /* TODO: check parent dn */

    rc |= get_entry(dn, attrs, &entry);
    if (rc != 0 || entry == NULL) {
        /* dn not found */
        service = NULL;
        goto fail1;
    }

    rc |= slapi_entry_attr_find(entry, "cn", &attr);
    if (rc != 0 || attr == NULL) {
        /* no cn attribute */
        service = NULL;
        goto fail1;
    }

    rc |= slapi_attr_get_values(attr, &cn);
    if (rc != 0 || cn == NULL) {
        /* no value in cn attribute */
        service = NULL;
        goto fail1;
    }

    if (*cn == NULL) {
        service = NULL;
        goto fail1;
    }

    service = slapi_ch_strdup((*cn)->bv_val);

fail1:
    slapi_entry_free(entry);

    return service;
}

static int auth(char *dn, struct berval *credentials)
{
    char *attrs[] = { "userPassword", NULL };
    Slapi_Entry *entry = NULL;
    Slapi_Attr *attr = NULL;
    struct berval **userPasswords = NULL;

    int rc = 0;

    /* get entry */
    rc |= get_entry(dn, attrs, &entry);
    if (rc != 0 || entry == NULL) {
        rc = -1;
        goto fail1;
    }

    /* get userpassword attr */
    rc |= slapi_entry_attr_find(entry, "userPassword", &attr);
    if (rc != 0 || attr == NULL) {
        rc = 1;
        goto fail1;
    }

    /* get attribute values */
    rc |= slapi_attr_get_values(attr, &userPasswords);
    if (rc != 0 || userPasswords == NULL) {
        rc = 1;
        goto fail1;
    }

    /* check password */
    rc |= slapi_pw_find(userPasswords, credentials);

fail1:
    slapi_entry_free(entry);
    return rc;
}

static int auth_with_password_fallback(char *dn, struct berval *credentials)
{
    char *parent_dn;

    int rc = 0;
    char fn[] = "auth_with_password_fallback in service_passwords plug-in";

    slapi_log_error(
        SLAPI_LOG_PLUGIN, fn,
        "Try password fallback with dn: %s.\n", dn);

    rc = auth(dn, credentials);
    if (rc == 0) {
        /* auth success */
        return 0;
    }
    else if (rc == 1) {
        /* auth fail, but entry exists (no fallback) */
        return 1;
    }

    /* fallback to parent dn */
    parent_dn = slapi_dn_parent(dn);
    rc = auth(parent_dn, credentials);
    slapi_ch_free_string(&parent_dn);

    if (rc == 0) {
        /* auth success */
        return 0;
    }

    return 1;
}

static int pre_bind(Slapi_PBlock *pb)
{
    char *dn;
    int method;
    struct berval *credentials;
    int is_replication;
    int is_internal;

    char *parent_dn;

    int rc = 0;
    char fn[] = "pre_bind in service_passwords plug-in";

    /* Obtain the bind information from the parameter block. */
    rc |= slapi_pblock_get(pb, SLAPI_BIND_TARGET, &dn);
    rc |= slapi_pblock_get(pb, SLAPI_BIND_METHOD, &method);
    rc |= slapi_pblock_get(pb, SLAPI_BIND_CREDENTIALS, &credentials);
    rc |= slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replication);
    rc |= slapi_pblock_get(pb, SLAPI_IS_INTERNAL_OPERATION, &is_internal);

    if (rc != 0) {
        slapi_log_error(
            SLAPI_LOG_PLUGIN, fn,
            "Could not get parameters for bind operation (error %d).\n",
            rc);

        /* Cancel bind */
        slapi_send_ldap_result(
            pb, LDAP_OPERATIONS_ERROR, NULL, NULL, 0, NULL);
        return LDAP_OPERATIONS_ERROR;
    }

    if (is_replication || is_internal) {
        return 0;
    }

    if (method != LDAP_AUTH_SIMPLE) {
        slapi_log_error(
            SLAPI_LOG_PLUGIN, fn,
            "The service_passwords plug-in does not handle this bind "
                "(method %d).\n",
            method);

        return 0;
    }

    parent_dn = slapi_dn_parent(dn);
    rc |= dn_contains_uid(parent_dn);
    slapi_ch_free_string(&parent_dn);

    if (rc != 0) {
        /* parent_dn is not an user, so we ignore this bind request. */
        return 0;
    }

    if (auth_with_password_fallback(dn, credentials) == 0) {
        /* auth success: set connection info */
        rc |= slapi_pblock_set(pb, SLAPI_CONN_DN, slapi_ch_strdup(dn));

        if (rc != 0) {
            slapi_log_error(
                SLAPI_LOG_PLUGIN, fn,
                "Failed to set connection info.\n");

            rc = LDAP_OPERATIONS_ERROR;
            slapi_send_ldap_result(pb, rc, NULL, NULL, 0, NULL);
            return rc;
        }

        /* auth done */
        slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
        return 1;
    }

    /* auth fail */
    rc = LDAP_INVALID_CREDENTIALS;
    slapi_send_ldap_result(pb, rc, NULL, NULL, 0, NULL);
    return rc;
}

static int pre_entry(Slapi_PBlock *pb)
{
    char *bind_dn;
    char *auth_method;
    Slapi_Entry *entry;
    Slapi_Operation *op;
    int is_replication;
    int is_internal;

    char *service;
    char *parent_dn = NULL;
    char rdn[255];
    const char *result_dn = NULL;
    char *new_dn = NULL;
    Slapi_Entry *new_entry;

    int rc = 0;
    char fn[] = "pre_entry in service_passwords plug-in";

    rc |= slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
    rc |= slapi_pblock_get(pb, SLAPI_CONN_AUTHMETHOD, &auth_method);
    rc |= slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY, &entry);
    rc |= slapi_pblock_get(pb, SLAPI_OPERATION, &op);
    rc |= slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replication);
    rc |= slapi_pblock_get(pb, SLAPI_IS_INTERNAL_OPERATION, &is_internal);

    if (rc != 0) {
        slapi_log_error(
            SLAPI_LOG_PLUGIN, fn,
            "Could not get parameters (error %d).\n",
            rc);

        slapi_send_ldap_result(
            pb, LDAP_OPERATIONS_ERROR, NULL, NULL, 0, NULL);
        return LDAP_OPERATIONS_ERROR;
    }

    if (is_replication || is_internal) {
        return 0;
    }

    if (strcmp(auth_method, SLAPD_AUTH_NONE) == 0) {
        /* only handle authenticated searches */
        return 0;
    }

    if (slapi_op_get_type(op) != SLAPI_OPERATION_SEARCH) {
        return 0;
    }

    service = is_service_dn(bind_dn);
    if (service == NULL) {
        goto fail1;
    }

    result_dn = slapi_entry_get_dn(entry);

    /* ignore service_password entries */
    parent_dn = slapi_dn_parent(result_dn);
    if (dn_contains_uid(parent_dn) == 0) {
        rc = -1;
        goto fail1;
    }
    slapi_ch_free_string(&parent_dn);

    /* modify the dn of the returned entry */
    if (dn_contains_uid(result_dn) == 0) {
        /* TODO: style */
        snprintf(rdn, 254, "cn=%s", service);
        new_dn = slapi_dn_plus_rdn(result_dn, rdn);
        new_entry = slapi_entry_dup(entry);
        slapi_entry_set_dn(new_entry, new_dn);
        slapi_ch_free_string(&new_dn);
        slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, new_entry);
    }

fail1:
    slapi_ch_free_string(&service);

    return rc;
}

Slapi_PluginDesc bindpdesc = {
    .spd_id = "service_passwords",
    .spd_vendor = "spline",
    .spd_version = "1.0",
    .spd_description = "preoperation plugin "
        "to authenticate a bind against different passwords"
};

int service_passwords_init(Slapi_PBlock *pb)
{
    int rc = 0;

    rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_CURRENT_VERSION);
    rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, (void *) &bindpdesc);
    rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_BIND_FN, (void *) pre_bind);
    rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ENTRY_FN, (void *) pre_entry);
    rc |= slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_id);
    return rc;
}