summaryrefslogtreecommitdiff
path: root/src/plugins/default/tlm-account-plugin-default.c
blob: 619340de03d0803205414bf43fc92c7c26765a1a (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
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of tlm (Tiny Login Manager)
 *
 * Copyright (C) 2013 Intel Corporation.
 *
 * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
 *
 * 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 St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <glib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "tzplatform_config.h"

#include "tlm-account-plugin-default.h"
#include "tlm-log.h"

/**
 * SECTION:tlm-account-plugin-default
 * @short_description: an default plugin for user account operation
 *
 * #TlmAccountPluginDefault provides a default implementation of user account
 * operations:
 * - setting up guest account is performed by running 'useradd'
 * - cleaning up guest account is performed by running 'rm -rf' on the account's
 * home directory
 * - check the account validity is done using getpwnam().
 *
 * It is recommended to use a GUM plugin instead: see #TlmAccountPluginGumd.
 *
 */

/**
 * TlmAccountPluginDefault:
 *
 * Opaque data structure
 */
/**
 * TlmAccountPluginDefaultClass:
 * @parent_class: a reference to a parent class
 *
 * The class structure for the #TlmAccountPluginDefault objects,
 */

enum {
    PROP_0,
    PROP_CONFIG
};

struct _TlmAccountPluginDefault
{
    GObject parent;
    GHashTable *config;
};


static gboolean
_setup_guest_account (TlmAccountPlugin *plugin, const gchar *user_name)
{
    int res = -1;
    pid_t pid;

    g_return_val_if_fail (plugin, FALSE);
    g_return_val_if_fail (TLM_IS_ACCOUNT_PLUGIN_DEFAULT(plugin), FALSE);
    g_return_val_if_fail (user_name && user_name[0], FALSE);

    pid = fork();
    if (0 == pid) {    //child process
        gchar **args = NULL;
        const gchar *path = tzplatform_getenv(TZ_SYS_SBIN);

        args = g_new0 (gchar *, 3);
        if (NULL == args)
            return FALSE;

        args[0] = g_strdup_printf("%s/useradd", path);
        args[1] = g_strdup(user_name);
        args[2] = NULL;

        execv(args[0], args);
        //fail execv
        g_strfreev(args);
        exit(EXIT_FAILURE);
    }
    else if (-1 != pid) {  // parent process
        pid_t ret;
        int status;
        while ((ret = waitpid(pid, &status, WNOHANG))== 0) {
            sleep(1);
        }
        if ((ret == pid) && (WIFEXITED(status)))
            res = WEXITSTATUS(status);
        else
            res = 0;
    }

    return ((res != -1) && (res != EXIT_FAILURE));
}

gboolean delete_sub_files(const gchar* dir)
{
    DIR *dirp;
    struct dirent *result;
    struct stat file_stat;
    gchar *file = NULL;
    gboolean ret = FALSE;
    int max_path = 0;

    max_path = pathconf("/", _PC_PATH_MAX);
    if (0 >= max_path)  max_path=1025;

    if ((dirp = opendir(dir)) == NULL)
        return FALSE;

    file = g_malloc(max_path);
    if (file == NULL) {
        closedir(dirp);
        return FALSE;
    }

    errno = 0;
    while ((result = readdir(dirp)) != NULL)
    {
        if ((0 == strcmp(".", result->d_name)) || (0 == strcmp("..", result->d_name)))
            continue;

        snprintf(file, max_path, "%s/%s", dir, result->d_name);
        lstat(file, &file_stat);
        if (S_ISDIR(file_stat.st_mode)) {
            if (!delete_sub_files(file)) {
                break;
            } else {
                if(0 != rmdir(file)) {
                    DBG("Could not delete directory '%s', error : %s", file, strerror(errno));
                    break;
                }
            }
        } else {
            if (0 != unlink(file)) {
                DBG("Could not delete file '%s', error : %s", file, strerror(errno));
                break;
            }
        }
    }
    if (errno == 0)
        ret = TRUE;

    g_free(file);
    closedir(dirp);
    return ret;
}

static gboolean
_cleanup_guest_user (TlmAccountPlugin *plugin,
                     const gchar *user_name,
                     gboolean delete)
{
    struct passwd *pwd_entry = NULL;
    struct passwd buf_pwd;
    gboolean res;
    gchar *buf = NULL, *tmp = NULL;

    gsize size = sysconf(_SC_GETPW_R_SIZE_MAX);
    if (size < sizeof(struct passwd))
        size = 1024;

    (void) delete;

    g_return_val_if_fail (plugin, FALSE);
    g_return_val_if_fail (TLM_IS_ACCOUNT_PLUGIN_DEFAULT(plugin), FALSE);
    g_return_val_if_fail (user_name && user_name[0], FALSE);

    /* clear error */
    errno = 0;

    for (; NULL != (tmp = g_realloc(buf, size)); size*=2)
    {
        buf = tmp;

        if (ERANGE == getpwnam_r(user_name, &buf_pwd, buf, size, &pwd_entry))
            continue;
        break;
    }

    if (!pwd_entry) {
        DBG("Could not get info for user '%s', error : %s", 
            user_name, strerror(errno));

        if (buf)
            g_free(buf);

        return FALSE;
    }

    if (!pwd_entry->pw_dir) {
        DBG("No home folder entry found for user '%s'", user_name);

        if (buf)
            g_free(buf);

        return FALSE;
    }

    res = delete_sub_files(pwd_entry->pw_dir);

    if (buf)
        g_free(buf);

    return res;
}

static gboolean
_is_valid_user (TlmAccountPlugin *plugin, const gchar *user_name)
{
    struct passwd *pwd_entry = NULL;
    struct passwd pwd_buf;
    gchar *buf = NULL, *tmp = NULL;
    gsize size = sysconf(_SC_GETPW_R_SIZE_MAX);
    if (size < sizeof(struct passwd))
        size = 1024;

    g_return_val_if_fail (plugin, FALSE);
    g_return_val_if_fail (TLM_IS_ACCOUNT_PLUGIN_DEFAULT(plugin), FALSE);
    g_return_val_if_fail (user_name && user_name[0], FALSE);

    /* clear error */
    errno = 0;

    for (; NULL != (tmp = g_realloc(buf, size)); size*=2)
    {
        buf = tmp;

        if (ERANGE == getpwnam_r(user_name, &pwd_buf, buf, size, &pwd_entry))
            continue;
        break;
    }

    if (!pwd_entry) {
        gchar strerr_buf[MAX_STRERROR_LEN] = {0,};
        strerror_r(errno, strerr_buf, MAX_STRERROR_LEN);
        DBG("Could not get info for user '%s', error : %s",
            user_name, strerr_buf);
        if (buf)
            g_free(buf);
        return FALSE;
    }

    if (buf)
        g_free(buf);

    return TRUE;
}

static void
_plugin_interface_init (TlmAccountPluginInterface *iface)
{
    iface->setup_guest_user_account = _setup_guest_account;
    iface->cleanup_guest_user = _cleanup_guest_user;
    iface->is_valid_user = _is_valid_user;
}

G_DEFINE_TYPE_WITH_CODE (TlmAccountPluginDefault, tlm_account_plugin_default,
                         G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (TLM_TYPE_ACCOUNT_PLUGIN,
                                                _plugin_interface_init));


static void
_plugin_finalize (GObject *self)
{
    TlmAccountPluginDefault *plugin = TLM_ACCOUNT_PLUGIN_DEFAULT(self);

    if (plugin->config) g_hash_table_unref (plugin->config);

    G_OBJECT_CLASS (tlm_account_plugin_default_parent_class)->finalize(self);
}

static void
_plugin_set_property (GObject      *object,
                      guint         prop_id,
                      const GValue *value,
                      GParamSpec   *pspec)
{
    TlmAccountPluginDefault *self = TLM_ACCOUNT_PLUGIN_DEFAULT (object);

    switch (prop_id) {
        case PROP_CONFIG: {
            gpointer p = g_value_get_boxed (value);
            if (p)
                self->config = g_hash_table_ref ((GHashTable *)p);
            break;
        }
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
            break;
    }
}

static void
_plugin_get_property (GObject *object,
                      guint    prop_id,
                      GValue  *value,
                      GParamSpec *pspec)
{
    TlmAccountPluginDefault *self = TLM_ACCOUNT_PLUGIN_DEFAULT (object);

    switch (prop_id) {
        case PROP_CONFIG:
            g_value_set_boxed (value, self->config);
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
            break;
    }
}

static void
tlm_account_plugin_default_class_init (TlmAccountPluginDefaultClass *kls)
{
    GObjectClass *g_class = G_OBJECT_CLASS (kls);

    g_class->set_property = _plugin_set_property;
    g_class->get_property = _plugin_get_property;
    g_class->finalize = _plugin_finalize;

    g_object_class_override_property (g_class, PROP_CONFIG, "config");
}

static void
tlm_account_plugin_default_init (TlmAccountPluginDefault *self)
{
    tlm_log_init(G_LOG_DOMAIN);
}