summaryrefslogtreecommitdiffstats
path: root/src/account/LMI_AccountProvider.c
blob: 86b5c2b320a06e7346b73466797dd229fb8a32ce (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
/*
 * Copyright (C) 2012  Roman Rakus <rrakus@redhat.com>
 *
 * 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 2
 * 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 <konkret/konkret.h>
#include "LMI_Account.h"

#include <time.h>
#include <stdbool.h>

#include <utmp.h>

#include <glib.h>

#include <libuser/entity.h>
#include <libuser/user.h>

#include <cmpi/cmpidt.h>
#include <cmpi/cmpift.h>
#include <cmpi/cmpimacs.h>


#include "aux_lu.h"
#include "macros.h"
#include "globals.h"

static const CMPIBroker* _cb = NULL;

static void LMI_AccountInitialize()
{
}

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

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

static CMPIStatus LMI_AccountEnumInstances(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char** properties)
{
    LMI_Account la;
    struct lu_context *luc = NULL;
    struct lu_error *error = NULL;
    GPtrArray *accounts = NULL;
    struct lu_ent *lue = NULL;

    size_t i;
    const char *nameSpace = KNameSpace(cop);
    const char *hostname = get_system_name();
    char *uid = NULL;
    long last_change, min_lifetime, max_lifetime, warn, inactive, expire;
    time_t last_login;
    CMPIStatus *rc = NULL;
    char *password = NULL;

    luc = lu_start(NULL, lu_user, NULL, NULL, lu_prompt_console_quiet, NULL, &error);
    if (!luc)
      {
        fprintf(stderr, "Error initializing: %s\n", lu_strerror(error));
        exit(1);
      }

    uid = malloc(256 * sizeof(char));
    accounts = lu_users_enumerate_full(luc, "*", &error);
    for (i = 0;  (accounts != NULL) && (i < accounts->len); i++)
      {
        lue = g_ptr_array_index(accounts, i);

        LMI_Account_Init(&la, _cb, nameSpace);
        LMI_Account_Set_CreationClassName(&la, LMI_Account_ClassName);
        LMI_Account_Set_SystemName(&la, hostname);
        LMI_Account_Set_SystemCreationClassName(&la, get_system_creation_class_name());
        LMI_Account_Set_Name(&la, aux_lu_get_str(lue, LU_USERNAME));

        LMI_Account_Init_OrganizationName(&la, 1); /* XXX */
        LMI_Account_Set_OrganizationName(&la, 0, ""); /* XXX */

        /* Need to convert long int UID to the string */
        snprintf(uid, 255, "%ld", aux_lu_get_long(lue, LU_UIDNUMBER));
        LMI_Account_Set_UserID(&la, uid);

        LMI_Account_Init_Host(&la, 1);
        LMI_Account_Set_Host(&la, 0, hostname);
        LMI_Account_Set_ElementName(&la, aux_lu_get_str(lue, LU_GECOS));
        LMI_Account_Set_HomeDirectory(&la, aux_lu_get_str(lue,
          LU_HOMEDIRECTORY));
        LMI_Account_Set_LoginShell(&la, aux_lu_get_str(lue, LU_LOGINSHELL));

        last_change = aux_lu_get_long(lue, LU_SHADOWLASTCHANGE);
        LMI_Account_Set_PasswordLastChange(&la,
          CMNewDateTimeFromBinary(_cb, DAYSTOMS(last_change), false, rc));

        min_lifetime = aux_lu_get_long(lue, LU_SHADOWMIN);
        max_lifetime = aux_lu_get_long(lue, LU_SHADOWMAX);

        LMI_Account_Set_PasswordPossibleChange(&la,
          CMNewDateTimeFromBinary(_cb, DAYSTOMS(min_lifetime), true, rc));

        if (max_lifetime != 0 && max_lifetime != 99999)
          {
            LMI_Account_Set_PasswordExpiration(&la,
              CMNewDateTimeFromBinary(_cb, DAYSTOMS(max_lifetime), true, rc));
            warn = aux_lu_get_long(lue, LU_SHADOWWARNING);
              LMI_Account_Set_PasswordExpirationWarning(&la,
                CMNewDateTimeFromBinary(_cb, DAYSTOMS(warn), true, rc));
            inactive = aux_lu_get_long(lue, LU_SHADOWINACTIVE);
            if (inactive != -1)
              {
                LMI_Account_Set_PasswordInactivation(&la,
                  CMNewDateTimeFromBinary(_cb, DAYSTOMS(inactive), true, rc));
              }
          }
        else
          {
            LMI_Account_Null_PasswordExpiration(&la);
            LMI_Account_Null_PasswordExpirationWarning(&la);
            LMI_Account_Null_PasswordInactivation(&la);
          }

        expire = aux_lu_get_long(lue, LU_SHADOWEXPIRE);
        if (expire != -1)
          {
            LMI_Account_Set_AccountExpiration(&la,
              CMNewDateTimeFromBinary(_cb, DAYSTOMS(expire), false, rc));
          }
        else
          {
            LMI_Account_Null_AccountExpiration(&la);
          }

        last_login = aux_utmp_latest(aux_lu_get_str(lue, LU_USERNAME));

        if (last_login != -1)
          {
            LMI_Account_Set_LastLogin(&la,
              CMNewDateTimeFromBinary(_cb, STOMS(last_login), false, rc));
          }

        password = aux_lu_get_str(lue, LU_SHADOWPASSWORD);
        LMI_Account_Init_UserPassword(&la, 1);
        LMI_Account_Set_UserPassword(&la, 0, password);
        /* Assume all passwords (encrypted or not) are in ascii encoding */
        LMI_Account_Set_UserPasswordEncoding(&la, 2);

        KReturnInstance(cr, la);
        lu_ent_free(lue);
      } /* for */
    free(uid);

    if (accounts)
      {
        g_ptr_array_free(accounts, TRUE);
      }

    lu_end(luc);

    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_AccountGetInstance(
    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_AccountCreateInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const CMPIInstance* ci)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

/* Need better view on set date time properties */
typedef struct _date_time_prop
{
  long value;
  bool null; /* True if property is set to null - has no value */
  bool interval; /* True if value is interval */
} date_time_prop;

static CMPIStatus LMI_AccountModifyInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const CMPIInstance* ci,
    const char** properties)
{
    /* TODO:
     *
     * Allow to modify the following:
     * Set up account expiration
     * Set up password expiration
     */
    char* value = NULL, *password = NULL;
    CMPIString* vs = NULL;
    CMPIArray* ar = NULL; int arsize;/* used for password */
    CMPIData data;
    char errmsg[256];

    struct lu_context *luc = NULL;
    struct lu_ent *lue = NULL;
    struct lu_error *error = NULL;
    GValue val;

    long last_change;
    date_time_prop expiration, warning, inactive_password, inactive_account;
    date_time_prop possible_change;

    LMI_Account la;

    LMI_Account_InitFromObjectPath(&la, _cb, cop);

    luc = lu_start(NULL, lu_user, NULL, NULL, lu_prompt_console_quiet, NULL, &error);
    if (!luc)
      {
        fprintf(stderr, "Error initializing: %s\n", lu_strerror(error));
        exit(1);
      }

    lue = lu_ent_new();
    if (!lu_user_lookup_name(luc, la.Name.chars, lue, &error))
      {
        snprintf(errmsg, 256, "User %s not found: %s", la.Name.chars,
          lu_strerror(error));
        lu_end(luc);
        lu_ent_free(lue);
        CMReturnWithChars(_cb, CMPI_RC_ERR_NOT_FOUND, errmsg);
      }

    data = ci->ft->getProperty(ci, "UserPassword", NULL);
    ar = data.value.array;
    if (ar && (arsize = ar->ft->getSize(ar, NULL) > 0))
      {
        vs = ar->ft->getElementAt(ar, 0, NULL).value.string;
        value = vs->ft->getCharPtr(vs, NULL);
        password = aux_lu_get_str(lue, LU_SHADOWPASSWORD);
        if (strcmp(password, value) != 0)
          {
            if (!lu_user_setpass(luc, lue, value, TRUE, &error))
              {
                snprintf(errmsg, 256, "Error setting password: %s",
                  lu_strerror(error));
                lu_end(luc);
                lu_ent_free(lue);
                CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg);
              }
          }
      }
    else
      {
        if (!lu_user_removepass(luc, lue, &error))
          {
            snprintf(errmsg, 256, "Error removing password: %s",
              lu_strerror(error));
            lu_end(luc);
            lu_ent_free(lue);
            CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg);
          }
      }

#define PARAMSTR(ATTR, VAR)\
    g_value_set_string(&val, (VAR));\
    lu_ent_clear(lue, (ATTR));\
    lu_ent_add(lue, (ATTR), &val);\

#define PARAMLONG(ATTR, VAR)\
    g_value_set_long(&val, (VAR).value);\
    lu_ent_clear(lue, (ATTR));\
    if (!(VAR).null) lu_ent_add(lue, (ATTR), &val);\

/* This macro will get property named NAME and save it into `value' variable */
#define GETSTRVALUE(NAME)\
    data = ci->ft->getProperty(ci, (NAME), NULL);\
    vs = data.value.string;\
    value = vs->ft->getCharPtr(vs, NULL);\

#define GETDATEVALUE(NAME, VAR)\
    (VAR).null = CMGetProperty(ci, (NAME), NULL).state == CMPI_nullValue;\
    if (!(VAR).null)\
      {\
        (VAR).interval = CMIsInterval(\
          CMGetProperty(ci, (NAME), NULL).value.dateTime, NULL); \
        (VAR).value = MSTODAYS(CMGetBinaryFormat(\
          CMGetProperty(ci, (NAME), NULL).value.dateTime, NULL));\
      }\

    /* First string values */
    memset(&val, 0, sizeof(val));
    g_value_init(&val, G_TYPE_STRING);

    GETSTRVALUE("ElementName");
    PARAMSTR(LU_GECOS, value);

    GETSTRVALUE("HomeDirectory");
    PARAMSTR(LU_HOMEDIRECTORY, value);

    GETSTRVALUE("LoginShell");
    PARAMSTR(LU_LOGINSHELL, value);

    /* Now long values */
    memset(&val, 0, sizeof(val));
    g_value_init(&val, G_TYPE_LONG);

    last_change = aux_lu_get_long(lue, LU_SHADOWLASTCHANGE);

    errmsg[0] = '\0';
    do
      {
        GETDATEVALUE("PasswordExpiration", expiration);
        if (!expiration.null && !expiration.interval)
          {
            if ((expiration.value -= last_change) < 0)
              {
                snprintf(errmsg, 256, "Wrong property setting, "
                  "PasswordExpiration must be later than "
                  "PasswordLastChange\n");
                  break;
              }
          }

        GETDATEVALUE("PasswordExpirationWarning", warning);
        if (!warning.null && !warning.interval)
          {
            if (expiration.null)
              {
                snprintf(errmsg, 256, "Wrong property setting, "
                  "PasswordExpiration must be set if you want to set "
                  "PasswordExpirationWarning as time of date\n");
                  break;
              }
            warning.value = last_change + expiration.value - warning.value;
            if (warning.value < 0)
              {
                snprintf(errmsg, 256, "Wrong property setting, "
                  "PasswordExpirationWarning must be earlier than "
                  "PasswordExpiration\n");
                  break;
              }
          }

        GETDATEVALUE("PasswordInactivation", inactive_password);
        if (!inactive_password.null && !inactive_password.interval)
          {
            if (expiration.null)
              {
                snprintf(errmsg, 256, "Wrong property setting, "
                  "PasswordExpiration must be set if you want to set "
                  "PasswordInactivation as time of date\n");
                  break;
              }
            inactive_password.value -= last_change + expiration.value;
            if (inactive_password.value < 0)
              {
                snprintf(errmsg, 256, "Wrong property setting, "
                  "PasswordInactivation must be later than "
                  "PasswordExpiration\n");
                  break;
              }
          }

        GETDATEVALUE("PasswordPossibleChange", possible_change);
        if (!possible_change.null && !possible_change.interval)
          {
            if ((possible_change.value -= last_change) < 0)
              {
                snprintf(errmsg, 256, "Wrong property setting, "
                  "PasswordPossibleChange must be later than "
                  "PasswordLastChange\n");
                  break;
              }
          }

        GETDATEVALUE("AccountExpiration", inactive_account);
        if (!inactive_account.null && inactive_account.interval)
          {
            snprintf(errmsg, 256, "Wrong property setting, "
              "AccountExpiration must be set as interval\n");
            break;
          }
      } while (0);

    if (errmsg[0])
      { /* There was an error, print it and return */
        lu_end(luc);
        lu_ent_free(lue);
        CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg);
      }

    PARAMLONG(LU_SHADOWMIN, possible_change);
    PARAMLONG(LU_SHADOWMAX, expiration);
    PARAMLONG(LU_SHADOWWARNING, warning);
    PARAMLONG(LU_SHADOWINACTIVE, inactive_password);
    PARAMLONG(LU_SHADOWEXPIRE, inactive_account);

#undef PARAMSTR
#undef PARAMLONG
#undef GETSTRVALUE
#undef GETDATEVALUE
    g_value_unset(&val);

    if (!lu_user_modify(luc, lue, &error))
      {
        snprintf(errmsg, 256, "User modification failed: %s",
          lu_strerror(error));
        lu_end(luc);
        lu_ent_free(lue);
        CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg);
      }

    lu_ent_free(lue);
    lu_end(luc);
    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_AccountDeleteInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop)
{
    LMI_Account acc;
    const char* username = NULL;
    char errmsg[256];
    struct lu_context *luc = NULL;
    struct lu_error *error = NULL;
    struct lu_ent *lue = NULL;

    LMI_Account_InitFromObjectPath(&acc, _cb, cop);
    username = acc.Name.chars;

    luc = lu_start(NULL, 0, NULL, NULL, lu_prompt_console_quiet, NULL, &error);
    if (!luc)
      {
        fprintf(stderr, "Error initializing: %s\n", lu_strerror(error));
        exit(1);
      }

    lue = lu_ent_new();

    if (!lu_user_lookup_name(luc, username, lue, &error))
      {
        snprintf(errmsg, 256, "Non existing user: %s\n", username);
        lu_ent_free(lue);
        lu_end(luc);
        CMReturnWithChars(_cb, CMPI_RC_ERR_NOT_FOUND, errmsg);
      }

    if (!lu_user_delete(luc, lue, &error))
      {
        snprintf(errmsg, 256, "User %s could not be deleted: %s\n", username,
          lu_strerror(error));
        lu_ent_free(lue);
        lu_end(luc);
        CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg);
      }

    lu_ent_free(lue);
    lu_end(luc);

    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_AccountExecQuery(
    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_Account,
    LMI_Account,
    _cb,
    LMI_AccountInitialize())

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

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

CMMethodMIStub(
    LMI_Account,
    LMI_Account,
    _cb,
    LMI_AccountInitialize())

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

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}


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