device: renew dhcp leases on awake for software devices
[NetworkManager.git] / src / nm-session-monitor.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write to the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * (C) Copyright 2008 - 2015 Red Hat, Inc.
17  * Author: David Zeuthen <davidz@redhat.com>
18  * Author: Dan Williams <dcbw@redhat.com>
19  * Author: Matthias Clasen
20  * Author: Pavel Šimerda <psimerda@redhat.com>
21  */
22 #include "nm-default.h"
23
24 #include <pwd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 #include "nm-session-monitor.h"
30 #include "NetworkManagerUtils.h"
31
32 #ifdef SESSION_TRACKING_SYSTEMD
33 #include <systemd/sd-login.h>
34 #endif
35
36 /********************************************************************/
37
38 /* <internal>
39  * SECTION:nm-session-monitor
40  * @title: NMSessionMonitor
41  * @short_description: Monitor sessions
42  *
43  * The #NMSessionMonitor class is a utility class to track and monitor sessions.
44  */
45 struct _NMSessionMonitor {
46         GObject parent_instance;
47
48 #ifdef SESSION_TRACKING_SYSTEMD
49         struct {
50                 sd_login_monitor *monitor;
51                 guint watch;
52         } sd;
53 #endif
54
55 #ifdef SESSION_TRACKING_CONSOLEKIT
56         struct {
57                 GFileMonitor *monitor;
58                 GHashTable *cache;
59                 time_t timestamp;
60         } ck;
61 #endif
62 };
63
64 struct _NMSessionMonitorClass {
65         GObjectClass parent_class;
66
67         void (*changed) (NMSessionMonitor *monitor);
68 };
69
70 G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
71
72 enum {
73         CHANGED,
74         LAST_SIGNAL,
75 };
76
77 static guint signals[LAST_SIGNAL] = { 0 };
78
79 /********************************************************************/
80
81 #ifdef SESSION_TRACKING_SYSTEMD
82 static gboolean
83 st_sd_session_exists (NMSessionMonitor *monitor, uid_t uid, gboolean active)
84 {
85         int status;
86
87         if (!monitor->sd.monitor)
88                 return FALSE;
89
90         status = sd_uid_get_sessions (uid, active, NULL);
91
92         if (status < 0)
93                 nm_log_err (LOGD_CORE, "Failed to get systemd sessions for uid %d: %d",
94                             uid, status);
95
96         return status > 0;
97 }
98
99 static gboolean
100 st_sd_changed (GIOChannel *stream, GIOCondition condition, gpointer user_data)
101 {
102         NMSessionMonitor *monitor = user_data;
103
104         g_signal_emit (monitor, signals[CHANGED], 0);
105
106         sd_login_monitor_flush (monitor->sd.monitor);
107
108         return TRUE;
109 }
110
111 static void
112 st_sd_init (NMSessionMonitor *monitor)
113 {
114         int status;
115         GIOChannel *stream;
116
117         if (!g_file_test ("/run/systemd/seats/", G_FILE_TEST_EXISTS))
118                 return;
119
120         if ((status = sd_login_monitor_new (NULL, &monitor->sd.monitor)) < 0) {
121                 nm_log_err (LOGD_CORE, "Failed to create systemd login monitor: %d", status);
122                 return;
123         }
124
125         stream = g_io_channel_unix_new (sd_login_monitor_get_fd (monitor->sd.monitor));
126         monitor->sd.watch = g_io_add_watch (stream, G_IO_IN, st_sd_changed, monitor);
127
128         g_io_channel_unref (stream);
129 }
130
131 static void
132 st_sd_finalize (NMSessionMonitor *monitor)
133 {
134         g_clear_pointer (&monitor->sd.monitor, sd_login_monitor_unref);
135         g_source_remove (monitor->sd.watch);
136 }
137 #endif /* SESSION_TRACKING_SYSTEMD */
138
139 /********************************************************************/
140
141 #ifdef SESSION_TRACKING_CONSOLEKIT
142 typedef struct {
143         gboolean active;
144 } CkSession;
145
146 static gboolean
147 ck_load_cache (GHashTable *cache)
148 {
149         GKeyFile *keyfile = g_key_file_new ();
150         char **groups = NULL;
151         GError *error = NULL;
152         gsize i, len;
153         gboolean finished = FALSE;
154
155         if (!g_key_file_load_from_file (keyfile, CKDB_PATH, G_KEY_FILE_NONE, &error))
156                 goto out;
157
158         if (!(groups = g_key_file_get_groups (keyfile, &len))) {
159                 nm_log_err (LOGD_CORE, "Could not load groups from " CKDB_PATH);
160                 goto out;
161         }
162
163         g_hash_table_remove_all (cache);
164
165         for (i = 0; i < len; i++) {
166                 guint uid = G_MAXUINT;
167                 CkSession session = { .active = FALSE };
168
169                 if (!g_str_has_prefix (groups[i], "CkSession "))
170                         continue;
171
172                 uid = g_key_file_get_integer (keyfile, groups[i], "uid", &error);
173                 if (error)
174                         goto out;
175
176                 session.active = g_key_file_get_boolean (keyfile, groups[i], "is_active", &error);
177                 if (error)
178                         goto out;
179
180                 g_hash_table_insert (cache, GUINT_TO_POINTER (uid), g_memdup (&session, sizeof session));
181         }
182
183         finished = TRUE;
184 out:
185         if (error)
186                 nm_log_err (LOGD_CORE, "ConsoleKit: Failed to load database: %s", error->message);
187         g_clear_error (&error);
188         g_clear_pointer (&groups, g_strfreev);
189         g_clear_pointer (&keyfile, g_key_file_free);
190
191         return finished;
192 }
193
194 static gboolean
195 ck_update_cache (NMSessionMonitor *monitor)
196 {
197         struct stat statbuf;
198
199         if (!monitor->ck.cache)
200                 return FALSE;
201
202         /* Check the database file */
203         if (stat (CKDB_PATH, &statbuf) != 0) {
204                 nm_log_err (LOGD_CORE, "Failed to check ConsoleKit timestamp: %s", strerror (errno));
205                 return FALSE;
206         }
207         if (statbuf.st_mtime == monitor->ck.timestamp)
208                 return TRUE;
209
210         /* Update the cache */
211         if (!ck_load_cache (monitor->ck.cache))
212                 return FALSE;
213
214         monitor->ck.timestamp = statbuf.st_mtime;
215
216         return TRUE;
217 }
218
219 static gboolean
220 ck_session_exists (NMSessionMonitor *monitor, uid_t uid, gboolean active)
221 {
222         CkSession *session;
223
224         if (!ck_update_cache (monitor))
225                 return FALSE;
226
227         session = g_hash_table_lookup (monitor->ck.cache, GUINT_TO_POINTER (uid));
228
229         if (!session)
230                 return FALSE;
231         if (active && !session->active)
232                 return FALSE;
233
234         return TRUE;
235 }
236
237 static void
238 ck_changed (GFileMonitor *    file_monitor,
239             GFile *           file,
240             GFile *           other_file,
241             GFileMonitorEvent event_type,
242             gpointer          user_data)
243 {
244         g_signal_emit (user_data, signals[CHANGED], 0);
245 }
246
247 static void
248 ck_init (NMSessionMonitor *monitor)
249 {
250         GFile *file = g_file_new_for_path (CKDB_PATH);
251         GError *error = NULL;
252
253         if (g_file_query_exists (file, NULL)) {
254                 if ((monitor->ck.monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error))) {
255                         monitor->ck.cache = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
256                         g_signal_connect (monitor->ck.monitor,
257                                                           "changed",
258                                                           G_CALLBACK (ck_changed),
259                                                           monitor);
260                 } else {
261                         nm_log_err (LOGD_CORE, "Error monitoring " CKDB_PATH ": %s", error->message);
262                         g_clear_error (&error);
263                 }
264         }
265
266         g_object_unref (file);
267 }
268
269 static void
270 ck_finalize (NMSessionMonitor *monitor)
271 {
272         g_clear_pointer (&monitor->ck.cache, g_hash_table_unref);
273         g_clear_object (&monitor->ck.monitor);
274 }
275 #endif /* SESSION_TRACKING_CONSOLEKIT */
276
277 /********************************************************************/
278
279 NM_DEFINE_SINGLETON_GETTER (NMSessionMonitor, nm_session_monitor_get, NM_TYPE_SESSION_MONITOR);
280
281 /**
282  * nm_session_monitor_connect:
283  * @self: the session monitor
284  * @callback: The callback.
285  * @user_data: User data for the callback.
286  *
287  * Connect a callback to the session monitor.
288  *
289  * Returns: Handler ID to be used with nm_session_monitor_disconnect().
290  */
291 gulong
292 nm_session_monitor_connect (NMSessionMonitor *self,
293                             NMSessionCallback callback,
294                             gpointer user_data)
295 {
296         g_return_val_if_fail (NM_IS_SESSION_MONITOR (self), 0);
297
298         return g_signal_connect (self,
299                              NM_SESSION_MONITOR_CHANGED,
300                              G_CALLBACK (callback),
301                              user_data);
302 }
303
304 /**
305  * nm_session_monitor_disconnect:
306  * @self: the session monitor
307  * @handler_id: Handler ID returned by nm_session_monitor-connect().
308  *
309  * Disconnect callback from the session handler.
310  */
311 void
312 nm_session_monitor_disconnect (NMSessionMonitor *self,
313                                gulong handler_id)
314 {
315         g_return_if_fail (NM_IS_SESSION_MONITOR (self));
316
317         g_signal_handler_disconnect (self, handler_id);
318 }
319
320 /**
321  * nm_session_monitor_uid_to_user:
322  * @uid: UID.
323  * @out_user: Return location for user name.
324  *
325  * Translates a UID to a user name.
326  */
327 gboolean
328 nm_session_monitor_uid_to_user (uid_t uid, const char **out_user)
329 {
330         struct passwd *pw = getpwuid (uid);
331
332         g_assert (out_user);
333
334         if (!pw)
335                 return FALSE;
336
337         *out_user = pw->pw_name;
338
339         return TRUE;
340 }
341
342 /**
343  * nm_session_monitor_user_to_uid:
344  * @user: User naee.
345  * @out_uid: Return location for UID.
346  *
347  * Translates a user name to a UID.
348  */
349 gboolean
350 nm_session_monitor_user_to_uid (const char *user, uid_t *out_uid)
351 {
352         struct passwd *pw = getpwnam (user);
353
354         g_assert (out_uid);
355
356         if (!pw)
357                 return FALSE;
358
359         *out_uid = pw->pw_uid;
360
361         return TRUE;
362 }
363
364 /**
365  * nm_session_monitor_session_exists:
366  * @self: the session monitor
367  * @uid: A user ID.
368  * @active: Ignore inactive sessions.
369  *
370  * Checks whether the given @uid is logged into an active session. Don't
371  * use this feature for security purposes. It is there just to allow you
372  * to prefer an agent from an active session over an agent from an
373  * inactive one.
374  *
375  * Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
376  * logged into an active session.
377  */
378 gboolean
379 nm_session_monitor_session_exists (NMSessionMonitor *self,
380                                    uid_t uid,
381                                    gboolean active)
382 {
383         g_return_val_if_fail (NM_IS_SESSION_MONITOR (self), FALSE);
384
385 #ifdef SESSION_TRACKING_SYSTEMD
386         if (st_sd_session_exists (self, uid, active))
387                 return TRUE;
388 #endif
389
390 #ifdef SESSION_TRACKING_CONSOLEKIT
391         if (ck_session_exists (self, uid, active))
392                 return TRUE;
393 #endif
394
395         return FALSE;
396 }
397
398 /********************************************************************/
399
400 static void
401 nm_session_monitor_init (NMSessionMonitor *monitor)
402 {
403 #ifdef SESSION_TRACKING_SYSTEMD
404         st_sd_init (monitor);
405 #endif
406
407 #ifdef SESSION_TRACKING_CONSOLEKIT
408         ck_init (monitor);
409 #endif
410 }
411
412 static void
413 nm_session_monitor_finalize (GObject *object)
414 {
415 #ifdef SESSION_TRACKING_SYSTEMD
416         st_sd_finalize (NM_SESSION_MONITOR (object));
417 #endif
418
419 #ifdef SESSION_TRACKING_CONSOLEKIT
420         ck_finalize (NM_SESSION_MONITOR (object));
421 #endif
422
423         if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
424                 G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
425 }
426
427 static void
428 nm_session_monitor_class_init (NMSessionMonitorClass *klass)
429 {
430         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
431
432         gobject_class->finalize = nm_session_monitor_finalize;
433
434         /**
435          * NMSessionMonitor::changed:
436          * @monitor: A #NMSessionMonitor
437          *
438          * Emitted when something changes.
439          */
440         signals[CHANGED] = g_signal_new (NM_SESSION_MONITOR_CHANGED,
441                                                 NM_TYPE_SESSION_MONITOR,
442                                                 G_SIGNAL_RUN_LAST,
443                                                 G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
444                                                 NULL,                   /* accumulator      */
445                                                 NULL,                   /* accumulator data */
446                                                 g_cclosure_marshal_VOID__VOID,
447                                                 G_TYPE_NONE,
448                                                 0);
449 }