summaryrefslogtreecommitdiffstats
path: root/src/realmd/LMI_RealmdServiceProvider.c
blob: 8ecd3818e88db484a1ebfb2075b8fb5376e50d45 (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
#include <konkret/konkret.h>
#include "LMI_RealmdService.h"
#include "rdcp_util.h"
#include "globals.h"
#include "rdcp_error.h"
#include "rdcp_dbus.h"

static const CMPIBroker* _cb = NULL;

/**
 * get_joined_domain:
 *
 * @provider_props Realmd service provider properties
 *
 * Determine if the host is joined to a domain and if so return the domain name.
 *
 * Returns: domain name if found, NULL otherwise. Must be freed with g_free.
 */
static gchar *
get_joined_domain(GVariant *provider_props)
{
    CMPIStatus status;
    GError *g_error = NULL;
    GVariant *realm_props = NULL;
    GVariant *kerberos_props = NULL;
    GVariantIter *iter = NULL;
    gchar *realm_obj_path = NULL;
    gchar *configured_interface = NULL;
    gchar *domain_name = NULL;

    CMSetStatus(&status, CMPI_RC_OK);

    if (!g_variant_lookup(provider_props, "Realms", "ao", &iter))
        goto exit;

    while (g_variant_iter_next(iter, "&o", &realm_obj_path)) {
        GET_DBUS_PROPERIES_OR_EXIT(realm_props, realm_obj_path,
                                   REALM_DBUS_REALM_INTERFACE, &status);
        if (g_variant_lookup(realm_props, "Configured", "&s", &configured_interface)) {
            if (strlen(configured_interface)) {
                if (strcmp(configured_interface, REALM_DBUS_KERBEROS_MEMBERSHIP_INTERFACE) == 0) {
                    GET_DBUS_PROPERIES_OR_EXIT(kerberos_props, realm_obj_path,
                                               REALM_DBUS_KERBEROS_INTERFACE, &status);
                    if (g_variant_lookup(kerberos_props, "DomainName", "&s", &domain_name)) {
                        goto exit;
                    }
                    G_VARIANT_FREE(kerberos_props);
                }
            }
        }
        G_VARIANT_FREE(realm_props);
    }

 exit:
    G_VARIANT_ITER_FREE(iter);
    G_VARIANT_FREE(realm_props);
    G_VARIANT_FREE(kerberos_props);

    return domain_name ? g_strdup(domain_name) : NULL;
}



static void LMI_RealmdServiceInitialize()
{
    lmi_init(provider_name, _cb, provider_config_defaults);
}

static CMPIStatus LMI_RealmdServiceCleanup(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    CMPIBoolean term)
{
    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_RealmdServiceEnumInstanceNames(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop)
{
    return KDefaultEnumerateInstanceNames(
        _cb, mi, cc, cr, cop);
}

static CMPIStatus LMI_RealmdServiceEnumInstances(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char** properties)
{
    CMPIStatus status;
    GError *g_error = NULL;
    LMI_RealmdService lmi_realmd_service;
    const char *name_space = KNameSpace(cop);
    const char *host_name = get_system_name();
    GVariant *provider_props = NULL;
    gchar *joined_domain = NULL;

    CMSetStatus(&status, CMPI_RC_OK);

    if (!rdcp_dbus_initialize(&g_error)) {
        return handle_g_error(&g_error, _cb, &status, CMPI_RC_ERR_FAILED, "rdcp_dbus_initialize failed");
    }

    LMI_InitRealmdServiceKeys(LMI_RealmdService, &lmi_realmd_service, name_space, host_name);

    GET_DBUS_PROPERIES_OR_EXIT(provider_props, REALM_DBUS_SERVICE_PATH,
                               REALM_DBUS_PROVIDER_INTERFACE, &status);

    if ((joined_domain = get_joined_domain(provider_props))) {
        LMI_RealmdService_Set_Domain(&lmi_realmd_service, joined_domain);
    }

    KReturnInstance(cr, lmi_realmd_service);

 exit:
    G_VARIANT_FREE(provider_props);
    g_free(joined_domain);

    return status;
}

static CMPIStatus LMI_RealmdServiceGetInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char** properties)
{
    return KDefaultGetInstance(
        _cb, mi, cc, cr, cop, properties);
}

static CMPIStatus LMI_RealmdServiceCreateInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const CMPIInstance* ci)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

static CMPIStatus LMI_RealmdServiceModifyInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const CMPIInstance* ci,
    const char** properties)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

static CMPIStatus LMI_RealmdServiceDeleteInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

static CMPIStatus LMI_RealmdServiceExecQuery(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char* lang,
    const char* query)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

CMInstanceMIStub(
    LMI_RealmdService,
    LMI_RealmdService,
    _cb,
    LMI_RealmdServiceInitialize())

static CMPIStatus LMI_RealmdServiceMethodCleanup(
    CMPIMethodMI* mi,
    const CMPIContext* cc,
    CMPIBoolean term)
{
    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_RealmdServiceInvokeMethod(
    CMPIMethodMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char* meth,
    const CMPIArgs* in,
    CMPIArgs* out)
{
    return LMI_RealmdService_DispatchMethod(
        _cb, mi, cc, cr, cop, meth, in, out);
}

CMMethodMIStub(
    LMI_RealmdService,
    LMI_RealmdService,
    _cb,
    LMI_RealmdServiceInitialize())

KUint32 LMI_RealmdService_RequestStateChange(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_RealmdServiceRef* self,
    const KUint16* RequestedState,
    KRef* Job,
    const KDateTime* TimeoutPeriod,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_RealmdService_StartService(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_RealmdServiceRef* self,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_RealmdService_StopService(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_RealmdServiceRef* self,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_RealmdService_ChangeAffectedElementsAssignedSequence(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_RealmdServiceRef* self,
    const KRefA* ManagedElements,
    const KUint16A* AssignedSequence,
    KRef* Job,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

// FIXME
static gboolean
get_credential_supported_owner(GVariant *supported, const gchar *cred_type, const gchar **cred_owner_return)
{
    GVariantIter iter;
    const gchar *type;
    const gchar *owner;

    g_variant_iter_init (&iter, supported);
    while (g_variant_iter_loop (&iter, "(&s&s)", &type, &owner)) {
        if (g_str_equal (cred_type, type)) {
            *cred_owner_return = owner;
            return TRUE;
        }
    }

    return FALSE;
}

static gboolean
is_credential_supported (GVariant *supported, const gchar *cred_type, const gchar *cred_owner)
{
    GVariantIter iter;
    const gchar *type;
    const gchar *owner;

    g_variant_iter_init(&iter, supported);
    while (g_variant_iter_loop (&iter, "(&s&s)", &type, &owner)) {
        if (g_str_equal(cred_type, type) &&
            g_str_equal(cred_owner, owner)) {
            return TRUE;
        }
    }

    return FALSE;
}

static gboolean
supports_dbus_interface(GVariant *dbus_props, const char *dbus_interface)
{
    gboolean result = FALSE;
    GVariantIter *iter = NULL;
    gchar *value;

    if (g_variant_lookup(dbus_props, "SupportedInterfaces", "as", &iter)) {
        while (g_variant_iter_next(iter, "&s", &value)) {
            if (strcmp(value, dbus_interface) == 0) {
                result = TRUE;
                break;
            }
        }
        G_VARIANT_ITER_FREE(iter);
    }
    return result;
}

KUint32 LMI_RealmdService_Join_Leave_Domain(
    bool join,
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_RealmdServiceRef* self,
    const KString* Domain,
    const KString* User,
    const KString* Password,
    const KStringA* OptionNames,
    const KStringA* OptionValues,
    CMPIStatus* status)
{
    const gchar *method_name = NULL;
    const gchar *supported_credentials_property = NULL;
    GError *g_error = NULL;
    KUint32 result = KUINT32_INIT;
    gint32 relevance = 0;
    gchar **paths = NULL;
    gchar *dbus_path, **pp;
    CMPICount n_paths;
    const gchar *cred_type = NULL;
    const gchar *cred_owner = NULL;
    GVariant *supported_creds = NULL;
    GVariant *realm_props = NULL;
    GVariant *kerberos_membership_props = NULL;
    GVariant *credentials = NULL;
    GVariant *options = NULL;

    KUint32_Set(&result, LMI_REALMD_RESULT_SUCCESS);
    CMSetStatus(status, CMPI_RC_OK);

    /* Assure we can communicate with DBus */
    if (!rdcp_dbus_initialize(&g_error)) {
        handle_g_error(&g_error, _cb, status, CMPI_RC_ERR_FAILED, "rdcp_dbus_initialize failed");
        KUint32_Set(&result, LMI_REALMD_RESULT_FAILED);
        goto exit;
    }

    if (join) {
        method_name = "Join";
        supported_credentials_property = "SupportedJoinCredentials";
    } else {
        method_name = "Leave";
        supported_credentials_property = "SupportedLeaveCredentials";
    }

    /* Call Discover to obtain list of DBus object paths for domain */
    if (!build_g_variant_options_from_KStringA(OptionNames, OptionValues, &options, &g_error)) {
        handle_g_error(&g_error, _cb, status, CMPI_RC_ERR_FAILED,
                       "failed to convert options to gvariant");
        KUint32_Set(&result, LMI_REALMD_RESULT_FAILED);
        goto exit;
    }

    if (!dbus_discover_call(system_bus, Domain->chars, options,
                            &relevance, &paths, &g_error)) {
        handle_g_error(&g_error, _cb, status, CMPI_RC_ERR_FAILED, "dbus_discover_call() failed");
        KUint32_Set(&result, LMI_REALMD_RESULT_FAILED);
        goto exit;
    }

#ifdef RDCP_DEBUG
    print_paths(paths, "%s: target=%s, paths:", __FUNCTION__, Domain->chars);
#endif

    for (pp = paths, dbus_path = *pp++, n_paths = 0; dbus_path; dbus_path = *pp++, n_paths++);

    if (n_paths < 1) {
        SetCMPIStatus(cb, status, CMPI_RC_ERR_FAILED, "Domain (%s) does not exist", Domain->chars);
        KUint32_Set(&result, LMI_REALMD_RESULT_NO_SUCH_DOMAIN);
        goto exit;
    }

    dbus_path = paths[0];

    /* Lookup the realm properties so we can determine the supported DBus interfaces */
    GET_DBUS_PROPERIES_OR_EXIT(realm_props, dbus_path,
                               REALM_DBUS_REALM_INTERFACE, status);
    if (!supports_dbus_interface(realm_props, REALM_DBUS_KERBEROS_MEMBERSHIP_INTERFACE)) {
        SetCMPIStatus(cb, status, CMPI_RC_ERR_FAILED, "Domain (%s) does not support joining or leaving",
                      Domain->chars);
        KUint32_Set(&result, LMI_REALMD_RESULT_DOMAIN_DOES_NOT_SUPPORT_JOINING);
        goto exit;
    }

    GET_DBUS_PROPERIES_OR_EXIT(kerberos_membership_props, dbus_path,
                               REALM_DBUS_KERBEROS_MEMBERSHIP_INTERFACE, status);

    if (!g_variant_lookup(kerberos_membership_props, supported_credentials_property, "@a(ss)",
                          &supported_creds)) {
        SetCMPIStatus(cb, status, CMPI_RC_ERR_FAILED,
                      "Domain (%s) did not supply supported %s credentials",
                      Domain->chars, method_name);
        KUint32_Set(&result, LMI_REALMD_RESULT_FAILED);
        goto exit;
    }

    if (!User->exists || User->null) {
        /* No User */
        if (!Password->exists || Password->null) {
            /* No User, No Password: automatic */
            cred_type = "automatic";
            if (!get_credential_supported_owner(supported_creds, cred_type, &cred_owner)) {
                SetCMPIStatus(cb, status, CMPI_RC_ERR_FAILED,
                              "Domain (%s) does not support automatic %s credentials",
                              Domain->chars, method_name);
                KUint32_Set(&result, LMI_REALMD_RESULT_DOMAIN_DOES_NOT_SUPPORT_PROVIDED_CREDENTIALS);
                goto exit;
            }

            credentials = g_variant_new ("(ssv)", cred_type, cred_owner,
                                         g_variant_new_string (""));

        } else {
            /* No User, Password: one time password using secret */
            cred_type = "secret";
            if (!get_credential_supported_owner(supported_creds, cred_type, &cred_owner)) {
                SetCMPIStatus(cb, status, CMPI_RC_ERR_FAILED,
                              "Domain (%s) does not support secret %s credentials",
                              Domain->chars, method_name);
                KUint32_Set(&result, LMI_REALMD_RESULT_DOMAIN_DOES_NOT_SUPPORT_PROVIDED_CREDENTIALS);
                goto exit;
            }
            credentials = g_variant_new("(ssv)", cred_type, cred_owner,
                                        g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
                                                                   Password->chars,
                                                                   strlen(Password->chars), 1));
        }
    } else {
        /* User */
        if (!Password->exists || Password->null) {
            /* User, No Password: invalid combination */
            SetCMPIStatus(cb, status, CMPI_RC_ERR_INVALID_PARAMETER,
                          "Must provide a password when User is provided");
            KUint32_Set(&result, LMI_REALMD_RESULT_FAILED);
            goto exit;
        } else {
            /* User, Password: password auth */
            cred_type = "password";
            cred_owner = "administrator";
            if (!is_credential_supported(supported_creds, cred_type, cred_owner)) {
                SetCMPIStatus(cb, status, CMPI_RC_ERR_FAILED,
                              "Domain (%s) does not support password with administrator ownership credentials",
                              Domain->chars);
                KUint32_Set(&result, LMI_REALMD_RESULT_DOMAIN_DOES_NOT_SUPPORT_PROVIDED_CREDENTIALS);
                goto exit;
            }
            credentials = g_variant_new("(ssv)", cred_type, cred_owner,
                                        g_variant_new("(ss)", User->chars, Password->chars));

        }
    }

    if (join) {
        if (!dbus_join_call(system_bus, dbus_path, credentials, options, &g_error)) {
            handle_g_error(&g_error, cb, status, CMPI_RC_ERR_FAILED, "dbus_join_call() failed");
            KUint32_Set(&result, LMI_REALMD_RESULT_FAILED);
            goto exit;
        }
    } else {
        if (!dbus_leave_call(system_bus, dbus_path, credentials, options, &g_error)) {
            handle_g_error(&g_error, cb, status, CMPI_RC_ERR_FAILED, "dbus_leave_call() failed");
            KUint32_Set(&result, LMI_REALMD_RESULT_FAILED);
            goto exit;
        }
    }


 exit:

    G_VARIANT_FREE(supported_creds);
    G_VARIANT_FREE(realm_props);
    G_VARIANT_FREE(kerberos_membership_props);
    G_VARIANT_FREE(credentials);
    G_VARIANT_FREE(options);
    g_strfreev(paths);

    return result;
}
KEXTERN KUint32 LMI_RealmdService_JoinDomain(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_RealmdServiceRef* self,
    const KString* Domain,
    const KString* User,
    const KString* Password,
    const KStringA* OptionNames,
    const KStringA* OptionValues,
    CMPIStatus* status)
{
    return LMI_RealmdService_Join_Leave_Domain(true, cb, mi, context, self,
                                               Domain, User, Password,
                                               OptionNames, OptionValues,
                                               status);
}

KEXTERN KUint32 LMI_RealmdService_LeaveDomain(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_RealmdServiceRef* self,
    const KString* Domain,
    const KString* User,
    const KString* Password,
    const KStringA* OptionNames,
    const KStringA* OptionValues,
    CMPIStatus* status)
{
    return LMI_RealmdService_Join_Leave_Domain(false, cb, mi, context, self,
                                               Domain, User, Password,
                                               OptionNames, OptionValues,
                                               status);
}

KONKRET_REGISTRATION(
    "root/cimv2",
    "LMI_RealmdService",
    "LMI_RealmdService",
    "instance method")