Debian Codegraph @

src/polkitbackend/polkitd.c

raw
1 /*
2 * Copyright (C) 2008-2010 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 * Author: David Zeuthen <[email protected]>
20 */
21
22 #include <signal.h>
23 #include <stdlib.h>
24
25 #include <glib-unix.h>
26
27 #include <pwd.h>
28 #include <grp.h>
29
30 #include <polkit/polkit.h>
31 #include <polkitbackend/polkitbackend.h>
32
33 #ifdef HAVE_LIBSYSTEMD
34 # include <systemd/sd-daemon.h>
35 #endif
36
37 /* ---------------------------------------------------------------------------------------------------- */
38
39 static PolkitBackendAuthority *authority = NULL;
40 static gpointer registration_id = NULL;
41 static GMainLoop *loop = NULL;
42 static gint exit_status = EXIT_FAILURE;
43 static gboolean opt_replace = FALSE;
44 static gboolean opt_no_debug = FALSE;
45 static gchar *opt_log_level = "notice";
46 static GOptionEntry opt_entries[] = {
47 {"replace", 'r', 0, G_OPTION_ARG_NONE, &opt_replace, "Replace existing daemon", NULL},
48 {"no-debug", 'n', 0, G_OPTION_ARG_NONE, &opt_no_debug, "Don't print debug information to stderr and stdout", NULL},
49 {"log-level", 'l', 0, G_OPTION_ARG_STRING, &opt_log_level, "Set a level of logging (syslog style). Defaults to 'notice'.",
50 "[emerg|alert|crit|err|warning|notice|info|debug]"},
51 {NULL }
52 };
53
54 static gboolean
55 load_key_from_config_file (const gchar *filename,
56 const gchar *section,
57 const gchar *key,
58 gchar **ret_value)
59 {
60 gboolean r;
61 gchar *value = NULL;
62 GError *error = NULL;
63 GKeyFile *config_file = NULL;
64
65 g_assert (ret_value != NULL);
66
67 config_file = g_key_file_new ();
68 g_assert (config_file != NULL);
69
70 r = g_key_file_load_from_file (config_file, filename, 0, &error);
71 if (!r)
72 {
73 if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
74 g_printerr ("Error loading config file '%s': %s\n", filename, error->message);
75 goto out;
76 }
77
78 value = g_key_file_get_string (config_file, section, key, &error);
79 if (value == NULL)
80 {
81 r = FALSE;
82 if (!g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
83 g_printerr ("Error reading '%s' from config file '%s': %s\n", key, filename, error->message);
84 goto out;
85 }
86
87 *ret_value = value;
88 value = NULL;
89 r = TRUE;
90
91 out:
92 if (error)
93 g_error_free (error);
94 g_key_file_free (config_file);
95 g_free (value);
96
97 return r;
98 }
99
100 static gboolean
101 parse_expiration_seconds (guint *ret_expiration_seconds)
102 {
103 gboolean r;
104 gchar *value = NULL;
105 gchar *end_ptr = NULL;
106 guint64 expiration_seconds;
107
108 g_assert (ret_expiration_seconds != NULL);
109
110 r = load_key_from_config_file ("/etc/polkit-1/polkitd.conf",
111 "Polkitd",
112 "ExpirationSeconds",
113 &value);
114 if (!r)
115 r = load_key_from_config_file ("/run/polkit-1/polkitd.conf",
116 "Polkitd",
117 "ExpirationSeconds",
118 &value);
119 if (!r)
120 r = load_key_from_config_file ("/usr/share/polkit-1/polkitd.conf",
121 "Polkitd",
122 "ExpirationSeconds",
123 &value);
124 if (!r)
125 goto out;
126
127 expiration_seconds = g_ascii_strtoull (value, &end_ptr, 10);
128 if ((expiration_seconds == G_MAXUINT64 && errno == ERANGE) ||
129 (expiration_seconds == 0 && end_ptr == value) ||
130 (expiration_seconds == 0 && errno == EINVAL) ||
131 expiration_seconds > G_MAXUINT ||
132 *end_ptr != '\0')
133 {
134 g_printerr ("Error parsing ExpirationSeconds value '%s' from config file\n", value);
135 r = FALSE;
136 goto out;
137 }
138
139 *ret_expiration_seconds = (guint) expiration_seconds;
140 r = TRUE;
141
142 out:
143 g_free (value);
144
145 return r;
146 }
147
148 static void
149 on_bus_acquired (GDBusConnection *connection,
150 const gchar *name,
151 gpointer user_data)
152 {
153 GError *error;
154
155 g_print ("Connected to the system bus\n");
156
157 g_assert (registration_id == NULL);
158
159 error = NULL;
160 registration_id = polkit_backend_authority_register (authority,
161 connection,
162 "/org/freedesktop/PolicyKit1/Authority",
163 &error);
164 if (registration_id == NULL)
165 {
166 g_printerr ("Error registering authority: %s\n", error->message);
167 g_error_free (error);
168 g_main_loop_quit (loop); /* exit */
169 }
170 }
171
172 static void
173 on_name_lost (GDBusConnection *connection,
174 const gchar *name,
175 gpointer user_data)
176 {
177 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
178 LOG_LEVEL_WARNING,
179 "Lost the name org.freedesktop.PolicyKit1 - exiting");
180 g_main_loop_quit (loop);
181 }
182
183 static void
184 on_name_acquired (GDBusConnection *connection,
185 const gchar *name,
186 gpointer user_data)
187 {
188 exit_status = EXIT_SUCCESS;
189
190 polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
191 LOG_LEVEL_INFO,
192 "Acquired the name org.freedesktop.PolicyKit1 on the system bus");
193 }
194
195 static gboolean
196 on_sigint_sigterm (gpointer user_data)
197 {
198 g_print ("Handling %s\n", (const char *) user_data);
199 g_main_loop_quit (loop);
200 return TRUE;
201 }
202
203 static gboolean
204 on_sighup (gpointer user_data)
205 {
206 guint expiration_seconds;
207
208 #ifdef HAVE_LIBSYSTEMD
209 gchar reload_message[sizeof("RELOADING=1\nMONOTONIC_USEC=18446744073709551615")];
210 gint64 monotonic_now;
211
212 /* Notify systemd that we are reloading, including a CLOCK_MONOTONIC timestamp in usec
213 * so that the program is compatible with a Type=notify-reload service. */
214
215 monotonic_now = g_get_monotonic_time ();
216 g_snprintf (reload_message, sizeof(reload_message), "RELOADING=1\nMONOTONIC_USEC=%" G_GINT64_FORMAT, monotonic_now);
217
218 sd_notify (0, reload_message);
219 #endif
220
221 g_print ("Handling SIGHUP\n");
222
223 polkit_backend_interactive_authority_reload (POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority));
224
225 if (parse_expiration_seconds (&expiration_seconds))
226 polkit_backend_interactive_authority_set_expiration_seconds (POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority),
227 expiration_seconds);
228
229 #ifdef HAVE_LIBSYSTEMD
230 /* Notify systemd that we have finished reloading. */
231 sd_notify (0, "READY=1\nSTATUS=Processing requests...");
232 #endif
233
234 return TRUE;
235 }
236
237 static gboolean
238 become_user (const gchar *user,
239 GError **error)
240 {
241 gboolean ret = FALSE;
242 struct passwd *pw;
243
244 g_return_val_if_fail (user != NULL, FALSE);
245 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
246
247 pw = getpwnam (user);
248 if (pw == NULL)
249 {
250 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
251 "Error calling getpwnam(): %m");
252 goto out;
253 }
254
255 if ((geteuid () == pw->pw_uid) && (getuid () == pw->pw_uid) &&
256 (getegid () == pw->pw_gid) && (getgid () == pw->pw_gid))
257 {
258 /* already running as user */
259 ret = TRUE;
260 goto out;
261 }
262
263 if (setgroups (0, NULL) != 0)
264 {
265 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
266 "Error clearing groups: %m");
267 goto out;
268 }
269 if (initgroups (pw->pw_name, pw->pw_gid) != 0)
270 {
271 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
272 "Error initializing groups: %m");
273 goto out;
274 }
275
276 (void) setregid (pw->pw_gid, pw->pw_gid);
277 (void) setreuid (pw->pw_uid, pw->pw_uid);
278 if ((geteuid () != pw->pw_uid) || (getuid () != pw->pw_uid) ||
279 (getegid () != pw->pw_gid) || (getgid () != pw->pw_gid))
280 {
281 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
282 "Error becoming real+effective uid %d and gid %d: %m",
283 (int) pw->pw_uid, (int) pw->pw_gid);
284 goto out;
285 }
286
287 if (chdir ("/") != 0)
288 {
289 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
290 "Error changing to root directory %s: %m",
291 pw->pw_dir);
292 goto out;
293 }
294
295
296 ret = TRUE;
297 g_print ("Successfully changed to user %s\n", user);
298
299 out:
300 return ret;
301 }
302
303 int
304 main (int argc,
305 char **argv)
306 {
307 GError *error;
308 GOptionContext *opt_context;
309 guint name_owner_id;
310 guint sigint_id;
311 guint sighup_id;
312 guint expiration_seconds;
313
314 loop = NULL;
315 opt_context = NULL;
316 name_owner_id = 0;
317 sigint_id = 0;
318 sighup_id = 0;
319 registration_id = NULL;
320
321 /* Disable remote file access from GIO. */
322 setenv ("GIO_USE_VFS", "local", 1);
323
324 opt_context = g_option_context_new ("polkit system daemon");
325 g_option_context_add_main_entries (opt_context, opt_entries, NULL);
326 error = NULL;
327 if (!g_option_context_parse (opt_context, &argc, &argv, &error))
328 {
329 g_printerr ("Error parsing options: %s\n", error->message);
330 g_error_free (error);
331 goto out;
332 }
333
334 /* If --no-debug is requested don't clutter stdout/stderr etc.
335 */
336 if (opt_no_debug)
337 {
338 gint dev_null_fd;
339 dev_null_fd = open ("/dev/null", O_RDWR);
340 if (dev_null_fd >= 0)
341 {
342 dup2 (dev_null_fd, STDIN_FILENO);
343 dup2 (dev_null_fd, STDOUT_FILENO);
344 dup2 (dev_null_fd, STDERR_FILENO);
345 close (dev_null_fd);
346 }
347 else
348 {
349 g_warning ("Error opening /dev/null: %m");
350 }
351 }
352
353 error = NULL;
354 if (!become_user (POLKITD_USER, &error))
355 {
356 g_printerr ("Error switching to user %s: %s\n",
357 POLKITD_USER, error->message);
358 g_clear_error (&error);
359 goto out;
360 }
361
362 if (g_getenv ("PATH") == NULL)
363 g_setenv ("PATH", "/usr/bin:/bin:/usr/sbin:/sbin", TRUE);
364
365 polkit_backend_authority_set_log_level (opt_log_level);
366
367 authority = polkit_backend_authority_get ();
368
369 loop = g_main_loop_new (NULL, FALSE);
370
371 sigint_id = g_unix_signal_add (SIGINT,
372 on_sigint_sigterm,
373 "SIGINT");
374
375 sigint_id = g_unix_signal_add (SIGTERM,
376 on_sigint_sigterm,
377 "SIGTERM");
378
379 sighup_id = g_unix_signal_add (SIGHUP,
380 on_sighup,
381 NULL);
382
383 name_owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
384 "org.freedesktop.PolicyKit1",
385 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
386 (opt_replace ? G_BUS_NAME_OWNER_FLAGS_REPLACE : 0),
387 on_bus_acquired,
388 on_name_acquired,
389 on_name_lost,
390 NULL,
391 NULL);
392
393 if (parse_expiration_seconds (&expiration_seconds))
394 polkit_backend_interactive_authority_set_expiration_seconds (POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority),
395 expiration_seconds);
396
397 g_print ("Entering main event loop\n");
398
399 #ifdef HAVE_LIBSYSTEMD
400 sd_notify (0, "READY=1\nSTATUS=Processing requests...");
401 #endif
402
403 g_main_loop_run (loop);
404
405 #ifdef HAVE_LIBSYSTEMD
406 sd_notify (0, "STOPPING=1");
407 #endif
408
409 g_print ("Shutting down\n");
410 out:
411 if (sigint_id > 0)
412 g_source_remove (sigint_id);
413 if (sighup_id > 0)
414 g_source_remove (sighup_id);
415 if (name_owner_id != 0)
416 g_bus_unown_name (name_owner_id);
417 if (registration_id != NULL)
418 polkit_backend_authority_unregister (registration_id);
419 if (authority != NULL)
420 g_object_unref (authority);
421 if (loop != NULL)
422 g_main_loop_unref (loop);
423 if (opt_context != NULL)
424 g_option_context_free (opt_context);
425
426 g_print ("Exiting with code %d\n", exit_status);
427 return exit_status;
428 }