File: shared-data-manager.c

package info (click to toggle)
lightdm 1.18.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,028 kB
  • ctags: 3,701
  • sloc: ansic: 26,653; sh: 4,811; makefile: 1,266; cpp: 1,162; python: 258; xml: 19
file content (268 lines) | stat: -rw-r--r-- 8,432 bytes parent folder | download
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
/*
 * Copyright (C) 2014 Canonical, Ltd
 * Author: Michael Terry <michael.terry@canonical.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 3 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

#include <config.h>
#include <gio/gio.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>

#include "configuration.h"
#include "shared-data-manager.h"
#include "user-list.h"

#define NUM_ENUMERATION_FILES 100

struct SharedDataManagerPrivate
{
    gchar *greeter_user;
    guint32 greeter_gid;
    GHashTable *starting_dirs;
};

struct OwnerInfo
{
    SharedDataManager *manager;
    guint32 uid;
};

G_DEFINE_TYPE (SharedDataManager, shared_data_manager, G_TYPE_OBJECT);

static SharedDataManager *singleton = NULL;

SharedDataManager *
shared_data_manager_get_instance (void)
{
    if (!singleton)
        singleton = g_object_new (SHARED_DATA_MANAGER_TYPE, NULL);
    return singleton;
}

void
shared_data_manager_cleanup (void)
{
    g_clear_object (&singleton);
}

static void
delete_unused_user (gpointer key, gpointer value, gpointer user_data)
{
    const gchar *user = (const gchar *)key;
    GError *error = NULL;

    /* For this operation, we just need a fire and forget rm -rf.  Since
       recursively deleting in GIO is a huge pain in the butt, we'll just drop
       to shell for this. */

    gchar *path = g_build_filename (USERS_DIR, user, NULL);
    gchar *quoted_path = g_shell_quote (path);
    gchar *cmd = g_strdup_printf ("/bin/rm -rf %s", quoted_path);

    g_spawn_command_line_async (cmd, &error);
    if (error)
        g_warning ("Could not delete unused user data directory %s: %s", path, error->message);
    g_clear_error (&error);

    g_free (cmd);
    g_free (quoted_path);
    g_free (path);
}

gchar *
shared_data_manager_ensure_user_dir (SharedDataManager *manager, const gchar *user)
{
    struct passwd *entry;
    gchar *path;
    GFile *file;
    gboolean result;
    GFileInfo *info;
    GError *error = NULL;

    entry = getpwnam (user);
    if (!entry)
        return NULL;

    path = g_build_filename (USERS_DIR, user, NULL);
    file = g_file_new_for_path (path);

    g_debug ("Creating shared data directory %s", path);

    result = g_file_make_directory_with_parents (file, NULL, &error);
    if (error)
    {
        if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
            result = TRUE;
        else
            g_warning ("Could not create user data directory %s: %s", path, error->message);
    }
    g_clear_error (&error);
    if (!result)
    {
        g_object_unref (file);
        g_free (path);
        return NULL;
    }

    /* Even if the directory already exists, we want to re-affirm the owners
       because the greeter gid is configuration based and may change between
       runs. */
    info = g_file_info_new ();
    g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, entry->pw_uid);
    g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, manager->priv->greeter_gid);
    g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, 0770);
    result = g_file_set_attributes_from_info (file, info, G_FILE_QUERY_INFO_NONE, NULL, &error);
    g_object_unref (info);
    g_object_unref (file);
    if (error)
        g_warning ("Could not chown user data directory %s: %s", path, error->message);
    if (!result)
    {
        g_free (path);
        return NULL;
    }

    return path;
}

static void
next_user_dirs_cb (GObject *object, GAsyncResult *res, gpointer user_data)
{
    GFileEnumerator *enumerator = G_FILE_ENUMERATOR (object);
    SharedDataManager *manager = SHARED_DATA_MANAGER (user_data);
    GList *link;
    GError *error = NULL;

    GList *files = g_file_enumerator_next_files_finish (enumerator, res, &error);
    if (error)
    {
        g_warning ("Could not enumerate user data directory %s: %s", USERS_DIR, error->message);
        g_clear_error (&error);
        g_object_unref (manager);
        return;
    }

    for (link = files; link; link = link->next)
    {
        GFileInfo *info = link->data;
        g_hash_table_insert (manager->priv->starting_dirs, g_strdup (g_file_info_get_name (info)), NULL);
    }

    if (files != NULL)
    {
        g_list_free_full (files, g_object_unref);
        g_file_enumerator_next_files_async (enumerator, NUM_ENUMERATION_FILES, G_PRIORITY_DEFAULT, NULL, next_user_dirs_cb, manager);
    }
    else
    {
        // We've finally assembled all the initial directories.  Now let's
        // iterate the current users and as we go, remove the users from the
        // starting_dirs hash and thus see which users are obsolete.
        GList *users = common_user_list_get_users (common_user_list_get_instance ());
        for (link = users; link; link = link->next)
        {
            CommonUser *user = link->data;
            g_hash_table_remove (manager->priv->starting_dirs, common_user_get_name (user));
        }
        g_hash_table_foreach (manager->priv->starting_dirs, delete_unused_user, manager);
        g_hash_table_destroy (manager->priv->starting_dirs);
        manager->priv->starting_dirs = NULL;

        g_object_unref (manager);
    }
}

static void
list_user_dirs_cb (GObject *object, GAsyncResult *res, gpointer user_data)
{
    GFile *file = G_FILE (object);
    SharedDataManager *manager = SHARED_DATA_MANAGER (user_data);
    GFileEnumerator *enumerator;
    GError *error = NULL;

    enumerator = g_file_enumerate_children_finish (file, res, &error);
    if (error)
        g_warning ("Could not enumerate user data directory %s: %s", USERS_DIR, error->message);
    g_clear_error (&error);
    if (!enumerator)
    {
        g_object_unref (manager);
        return;
    }

    manager->priv->starting_dirs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
    g_file_enumerator_next_files_async (enumerator, NUM_ENUMERATION_FILES,
                                        G_PRIORITY_DEFAULT, NULL,
                                        next_user_dirs_cb, manager);
}

static void
user_removed_cb (CommonUserList *list, CommonUser *user, SharedDataManager *manager)
{
    delete_unused_user ((gpointer) common_user_get_name (user), NULL, manager);
}

void
shared_data_manager_start (SharedDataManager *manager)
{
    GFile *file;

    /* Grab list of all current directories, so we know if any exist that we no longer need. */
    file = g_file_new_for_path (USERS_DIR);
    g_file_enumerate_children_async (file, G_FILE_ATTRIBUTE_STANDARD_NAME,
                                     G_FILE_QUERY_INFO_NONE,
                                     G_PRIORITY_DEFAULT, NULL,
                                     list_user_dirs_cb, g_object_ref (manager));
    g_object_unref (file);

    /* And listen for user removals. */
    g_signal_connect (common_user_list_get_instance (), USER_LIST_SIGNAL_USER_REMOVED, G_CALLBACK (user_removed_cb), manager);
}

static void
shared_data_manager_init (SharedDataManager *manager)
{
    struct passwd *greeter_entry;

    manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager, SHARED_DATA_MANAGER_TYPE, SharedDataManagerPrivate);

    /* Grab current greeter-user gid */
    manager->priv->greeter_user = config_get_string (config_get_instance (), "LightDM", "greeter-user");
    greeter_entry = getpwnam (manager->priv->greeter_user);
    if (greeter_entry)
        manager->priv->greeter_gid = greeter_entry->pw_gid;
}

static void
shared_data_manager_finalize (GObject *object)
{
    SharedDataManager *self = SHARED_DATA_MANAGER (object);
  
    /* Should also cancel outstanding GIO operations, but whatever, let them do their thing. */

    g_signal_handlers_disconnect_by_data (common_user_list_get_instance (), self);

    if (self->priv->starting_dirs)
        g_hash_table_destroy (self->priv->starting_dirs);

    g_free (self->priv->greeter_user);

    G_OBJECT_CLASS (shared_data_manager_parent_class)->finalize (object);
}

static void
shared_data_manager_class_init (SharedDataManagerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->finalize = shared_data_manager_finalize;

    g_type_class_add_private (klass, sizeof (SharedDataManagerPrivate));
}