File: wtmp-helper.c

package info (click to toggle)
accountsservice 0.6.55-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,752 kB
  • sloc: ansic: 8,011; xml: 2,098; sh: 229; perl: 57; python: 54; makefile: 39; sed: 16
file content (222 lines) | stat: -rw-r--r-- 8,167 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
/*
 * Copyright (C) 2013 Red Hat, Inc.
 * Copyright (C) 2014 Canonical Limited
 *
 * 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 licence, 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Ondrej Holy <oholy@redhat.com>
 *          Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#include "wtmp-helper.h"
#include "user.h"

#ifdef HAVE_UTMPX_H

#include <utmpx.h>

typedef struct {
        guint64 frequency;
        gint64 time;
        GList *previous_logins;
} UserAccounting;

typedef struct {
        gchar  *id;
        gint64  login_time;
        gint64  logout_time;
} UserPreviousLogin;

static void
user_previous_login_free (UserPreviousLogin *previous_login)
{
        g_free (previous_login->id);
        g_free (previous_login);
}

static gboolean
wtmp_helper_start (void)
{
#if defined(HAVE_SETUTXDB)
                if (setutxdb (UTXDB_LOG, NULL) != 0) {
                        return FALSE;
                }
#elif defined(PATH_WTMP)
                if (utmpxname (PATH_WTMP) != 0) {
                        return FALSE;
                }

                setutxent ();
#else
#error You have utmpx.h, but no known way to use it for wtmp entries
#endif

                return TRUE;
}

void
wtmp_helper_update_login_frequencies (GHashTable *users)
{
        GHashTable *login_hash, *logout_hash;
        struct utmpx *wtmp_entry;
        GHashTableIter iter;
        gpointer key, value;
        User *user;
        GVariantBuilder *builder, *builder2;
        GList *l;

        if (!wtmp_helper_start ()) {
                return;
        }

        login_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
        logout_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

        while ((wtmp_entry = getutxent ())) {
                UserAccounting    *accounting;
                UserPreviousLogin *previous_login;
                gboolean shutdown_or_reboot = FALSE;

                if (g_str_equal (wtmp_entry->ut_line, "~")) {
                        if (g_str_equal (wtmp_entry->ut_user, "shutdown") ||
                            g_str_equal (wtmp_entry->ut_user, "reboot")) {
                                shutdown_or_reboot = TRUE;
                        }
                }

                if (wtmp_entry->ut_type == BOOT_TIME || shutdown_or_reboot) {
                        /* Set shutdown, reboot, or boot time for missing logout records */
                        g_hash_table_iter_init (&iter, logout_hash);
                        while (g_hash_table_iter_next (&iter, &key, &value)) {
                                previous_login = (UserPreviousLogin *) value;

                                if (previous_login->logout_time == 0) {
                                        previous_login->logout_time = wtmp_entry->ut_tv.tv_sec;
                                }
                        }
                        g_hash_table_remove_all (logout_hash);
                } else if (wtmp_entry->ut_type == DEAD_PROCESS) {
                        /* Save corresponding logout time */
                        if (g_hash_table_lookup_extended (logout_hash, wtmp_entry->ut_line, &key, &value)) {
                                previous_login = (UserPreviousLogin *) value;
                                previous_login->logout_time = wtmp_entry->ut_tv.tv_sec;

                                g_hash_table_remove (logout_hash, previous_login->id);
                        }
                }

                if (wtmp_entry->ut_type != USER_PROCESS) {
                        continue;
                }

                if (wtmp_entry->ut_user[0] == 0) {
                        continue;
                }

                if (!g_hash_table_lookup_extended (login_hash,
                                                   wtmp_entry->ut_user,
                                                   &key, &value)) {
                        accounting = g_new (UserAccounting, 1);
                        accounting->frequency = 0;
                        accounting->previous_logins = NULL;

                        g_hash_table_insert (login_hash, g_strdup (wtmp_entry->ut_user), accounting);
                } else {
                        accounting = value;
                }

                accounting->frequency++;
                accounting->time = wtmp_entry->ut_tv.tv_sec;

                /* Add zero logout time to change it later on logout record */
                previous_login = g_new (UserPreviousLogin, 1);
                previous_login->id = g_strdup (wtmp_entry->ut_line);
                previous_login->login_time = wtmp_entry->ut_tv.tv_sec;
                previous_login->logout_time = 0;
                accounting->previous_logins = g_list_prepend (accounting->previous_logins, previous_login);

                g_hash_table_insert (logout_hash, g_strdup (wtmp_entry->ut_line), previous_login);
        }

        /* Last iteration */
        endutxent ();

        g_hash_table_iter_init (&iter, login_hash);
        while (g_hash_table_iter_next (&iter, &key, &value)) {
                UserAccounting    *accounting = (UserAccounting *) value;
                UserPreviousLogin *previous_login;
                gboolean           changed = FALSE;
                guint64            old_login_frequency;
                guint64            old_login_time;

                user = g_hash_table_lookup (users, key);
                if (user == NULL) {
                        g_list_free_full (accounting->previous_logins, (GDestroyNotify) user_previous_login_free);
                        continue;
                }

                g_object_get (user,
                              "login-frequency", &old_login_frequency,
                              "login-time", &old_login_time,
                              NULL);

                if (old_login_frequency != accounting->frequency) {
                        g_object_set (user, "login-frequency", accounting->frequency, NULL);
                        changed = TRUE;
                }

                if (old_login_time != accounting->time) {
                        g_object_set (user, "login-time", accounting->time, NULL);
                        changed = TRUE;
                }

                builder = g_variant_builder_new (G_VARIANT_TYPE ("a(xxa{sv})"));
                for (l = g_list_last (accounting->previous_logins); l != NULL; l = l->prev) {
                        previous_login = l->data;

                        builder2 = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
                        g_variant_builder_add (builder2, "{sv}", "type", g_variant_new_string (previous_login->id));
                        g_variant_builder_add (builder, "(xxa{sv})", previous_login->login_time, previous_login->logout_time, builder2);
                        g_variant_builder_unref (builder2);
                }
                g_object_set (user, "login-history", g_variant_new ("a(xxa{sv})", builder), NULL);
                g_variant_builder_unref (builder);
                g_list_free_full (accounting->previous_logins, (GDestroyNotify) user_previous_login_free);

                if (changed)
                        user_changed (user);
        }

        g_hash_table_unref (login_hash);
        g_hash_table_unref (logout_hash);
}

const gchar *
wtmp_helper_get_path_for_monitor (void)
{
        return PATH_WTMP;
}

#else /* HAVE_UTMPX_H */

const gchar *
wtmp_helper_get_path_for_monitor (void)
{
        return NULL;
}

#endif /* HAVE_UTMPX_H */