summaryrefslogtreecommitdiffstats
path: root/src/account/LMI_GroupProvider.c
blob: 20d93b5e79ad736cd96e67506775efb000380556 (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
/*
 * Copyright (C) 2012-2014 Red Hat, Inc.  All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Roman Rakus <rrakus@redhat.com>
 */
#include <shadow.h>
#include <konkret/konkret.h>
#include "LMI_Group.h"

// Disable GLib deprecation warnings - GValueArray is deprecated but we
// need it because libuser uses it
#define GLIB_DISABLE_DEPRECATION_WARNINGS

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

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

#include "lock.h"

// Return values of functions
// Delete group
#define GROUP_NOT_EXIST      4096
#define GROUP_IS_PRIMARY     4097

static const CMPIBroker* _cb = NULL;

static void LMI_GroupInitialize(const CMPIContext *ctx)
{
    lmi_init(provider_name, _cb, ctx, provider_config_defaults);
    if (init_lock_pools() == 0) {
        error("Unable to initialize lock pool.");
        exit (1);
    }

}

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

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

static CMPIStatus LMI_GroupEnumInstances(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char** properties)
{
    LMI_Group lg;
    struct lu_context *luc = NULL;
    struct lu_error *error = NULL;
    GPtrArray *groups = NULL;
    struct lu_ent *lue = NULL;
    size_t i;
    const char *nameSpace = KNameSpace(cop);
    char *instanceid = NULL;

    luc = lu_start(NULL, lu_group, NULL, NULL, lu_prompt_console_quiet, NULL, &error);
    if (!luc)
      {
        KReturn2(_cb, ERR_FAILED,
                 "Unable to initialize libuser: %s\n", lu_strerror(error));
      }

    groups = lu_groups_enumerate_full(luc, "*", &error);
    for (i = 0;  (groups != NULL) && (i < groups->len); i++)
      {
        lue = g_ptr_array_index(groups, i);

        LMI_Group_Init(&lg, _cb, nameSpace);
        LMI_Group_Set_CreationClassName(&lg, LMI_Group_ClassName);
        LMI_Group_Set_Name(&lg, aux_lu_get_str(lue, LU_GROUPNAME));
        LMI_Group_Set_ElementName(&lg, aux_lu_get_str(lue, LU_GROUPNAME));
        LMI_Group_Set_CommonName(&lg, aux_lu_get_str(lue, LU_GROUPNAME));
        asprintf(&instanceid, ORGID":GID:%ld",
          aux_lu_get_long(lue, LU_GIDNUMBER));
        LMI_Group_Set_InstanceID(&lg, instanceid);
        free(instanceid);
        KReturnInstance(cr, lg);
        lu_ent_free(lue);
      } /* for */

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

    lu_end(luc);

    CMReturn(CMPI_RC_OK);
}

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

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

/*
 * Internal function to delete group
 */
static CMPIrc delete_group(
    const char *groupname,
    char *errormsg)
{
    struct lu_context *luc = NULL;
    struct lu_error *error = NULL;
    struct lu_ent *lueg = NULL;
    struct lu_ent *lueu = NULL;
    GValueArray *users = NULL;
    const char *username = NULL;
    long group_gid = 0;
    CMPIrc rc = CMPI_RC_OK;
    int pwdlockres;

    lmi_debug("Getting giant lock for group: %s", groupname);
    get_giant_lock();

    pwdlockres = lckpwdf();
    if (pwdlockres != 0)
        warn("Cannot acquire passwd file lock\n");

    luc = lu_start(NULL, 0, NULL, NULL, lu_prompt_console_quiet, NULL, &error);
    if (!luc) {
        asprintf(&errormsg, "Unable to initialize libuser: %s\n", lu_strerror(error));
        if (pwdlockres == 0)
            ulckpwdf();
        lmi_debug("Releasing giant lock for group: %s", groupname);
        release_giant_lock();
        lmi_debug("Giant lock released for group %s", groupname);

        return CMPI_RC_ERR_FAILED;
    }

    lueg = lu_ent_new();
    if (!lu_group_lookup_name(luc, groupname, lueg, &error)) { /* Group not found */
        asprintf(&errormsg, "Non existing group: %s\n", groupname);
        rc = GROUP_NOT_EXIST;
        goto clean;
    }

    /* check if the group is not primary group of any user */
    group_gid = aux_lu_get_long(lueg, LU_GIDNUMBER);
    users = lu_users_enumerate_by_group(luc, groupname, &error);
    unsigned int j;
    for (j = 0;  (users != NULL) && (j < users->n_values); j++) {
        lueu = lu_ent_new();
        username = g_value_get_string(g_value_array_get_nth(users, j));
        lu_user_lookup_name(luc, username, lueu, &error);
        if (aux_lu_get_long(lueu, LU_GIDNUMBER) == group_gid) {
            asprintf(&errormsg,
                "Cannot delete group %s, it is primary group of user %s\n",
                groupname, username);
            rc = GROUP_IS_PRIMARY;
            goto clean;
        }
        lu_ent_free(lueu);
    }

    if (!lu_group_delete(luc, lueg, &error)) {
        asprintf(&errormsg, "Group %s could not be deleted: %s\n", groupname,
                          lu_strerror(error));
        rc = CMPI_RC_ERR_FAILED;
        goto clean;
    }

clean:
    if (pwdlockres == 0)
        ulckpwdf();
    lmi_debug("Releasing giant lock for group: %s", groupname);
    release_giant_lock();
    lmi_debug("Giant lock released for group %s", groupname);

    if (users)
        g_value_array_free(users);
    if (lueg)
        lu_ent_free(lueg);
    if (lueu)
        lu_ent_free(lueu);
    lu_end(luc);
    return rc;
}

/*
 * DEPRECATED
 */
static CMPIStatus LMI_GroupDeleteInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop)
{
    LMI_Group lg;
    const char *name = NULL;
    CMPIrc rc = CMPI_RC_OK;
    char *errmsg = NULL;

    LMI_Group_InitFromObjectPath(&lg, _cb, cop);
    name = lg.Name.chars;

    rc = delete_group(name, errmsg);
    if (rc != CMPI_RC_OK) {
        CMPIStatus st = {CMPI_RC_ERR_FAILED, NULL};
        CMSetStatusWithChars(_cb, &st, CMPI_RC_ERR_FAILED, errmsg);
        free(errmsg);
        return st;
    }

    CMReturn(rc);
}

static CMPIStatus LMI_GroupExecQuery(
    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_Group,
    LMI_Group,
    _cb,
    LMI_GroupInitialize(ctx))

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

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

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

    char *errmsg = NULL;

    CMPIrc rc = delete_group(
        self->Name.chars,
        errmsg);

    KUint32_Set(&result, rc);

    if (rc > 0 && rc < GROUP_NOT_EXIST) {
        CMSetStatusWithChars(_cb, status, rc, errmsg);
    }
    free(errmsg);

    return result;
}



CMMethodMIStub(
    LMI_Group,
    LMI_Group,
    _cb,
    LMI_GroupInitialize(ctx))

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