Debian Codegraph @

src/programs/pkttyagent.c

raw
1 /*
2 * Copyright (C) 2009-2012 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 <stdio.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <termios.h>
26 #include <glib/gi18n.h>
27 #include <polkit/polkit.h>
28 #define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE
29 #include <polkitagent/polkitagent.h>
30
31
32 static volatile sig_atomic_t tty_flags_saved;
33 static volatile sig_atomic_t tty_flags_changed;
34 struct termios ts;
35 FILE *tty = NULL;
36 struct sigaction savesigterm, savesigint, savesigtstp;
37
38
39 static void tty_handler(int signal)
40 {
41 switch (signal)
42 {
43 case SIGTERM:
44 sigaction (SIGTERM, &savesigterm, NULL);
45 break;
46 case SIGINT:
47 sigaction (SIGINT, &savesigint, NULL);
48 break;
49 case SIGTSTP:
50 sigaction (SIGTSTP, &savesigtstp, NULL);
51 break;
52 }
53
54 if (tty_flags_saved && tty_flags_changed)
55 {
56 tcsetattr (fileno (tty), TCSADRAIN, &ts);
57 }
58
59 kill(getpid(), signal);
60 }
61
62
63 static void tty_attrs_changed(PolkitAgentListener *listener G_GNUC_UNUSED,
64 gboolean changed,
65 gpointer user_data G_GNUC_UNUSED)
66 {
67 tty_flags_changed = changed;
68 }
69
70
71 int
72 main (int argc, char *argv[])
73 {
74 gboolean opt_show_version = FALSE;
75 gboolean opt_fallback = FALSE;
76 gchar *opt_process = NULL;
77 gchar *opt_system_bus_name = NULL;
78 gint opt_notify_fd = -1;
79 GOptionEntry options[] =
80 {
81 {
82 "fallback", 0, 0, G_OPTION_ARG_NONE, &opt_fallback,
83 N_("Don't replace existing agent if any"), NULL
84 },
85 {
86 "notify-fd", 0, 0, G_OPTION_ARG_INT, &opt_notify_fd,
87 N_("Close FD when the agent is registered"), N_("FD")
88 },
89 {
90 "process", 'p', 0, G_OPTION_ARG_STRING, &opt_process,
91 N_("Register the agent for the specified process"),
92 N_("PID[,START_TIME]")
93 },
94 {
95 "system-bus-name", 's', 0, G_OPTION_ARG_STRING, &opt_system_bus_name,
96 N_("Register the agent for the owner of BUS_NAME"), N_("BUS_NAME")
97 },
98 {
99 "version", 0, 0, G_OPTION_ARG_NONE, &opt_show_version,
100 N_("Show version"), NULL
101 },
102 { NULL, 0, 0, 0, NULL, NULL, NULL }
103 };
104 GOptionContext *context;
105 gchar *s;
106 PolkitAuthority *authority = NULL;
107 PolkitSubject *subject = NULL;
108 gpointer local_agent_handle = NULL;
109 PolkitAgentListener *listener = NULL;
110 GVariant *listener_options = NULL;
111 GError *error;
112 GMainLoop *loop = NULL;
113 guint ret = 126;
114 GVariantBuilder builder;
115 struct sigaction sa;
116 const char *tty_name = NULL;
117
118 /* Disable remote file access from GIO. */
119 setenv ("GIO_USE_VFS", "local", 1);
120
121 error = NULL;
122 context = g_option_context_new ("");
123 s = g_strdup_printf (_("Report bugs to: %s\n"
124 "%s home page: <%s>"), PACKAGE_BUGREPORT,
125 PACKAGE_NAME, PACKAGE_URL);
126 g_option_context_set_description (context, s);
127 g_free (s);
128 g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
129 if (!g_option_context_parse (context, &argc, &argv, &error))
130 {
131 g_printerr ("%s: %s\n", g_get_prgname (), error->message);
132 g_error_free (error);
133 goto out;
134 }
135 if (argc > 1)
136 {
137 g_printerr (_("%s: Unexpected argument `%s'\n"), g_get_prgname (),
138 argv[1]);
139 goto out;
140 }
141
142 if (opt_show_version)
143 {
144 g_print ("pkttyagent version %s\n", PACKAGE_VERSION);
145 ret = 0;
146 goto out;
147 }
148
149 if (opt_process != NULL && opt_system_bus_name != NULL)
150 {
151 g_printerr (_("%s: Options --process and --system-bus-name are mutually exclusive\n"),
152 g_get_prgname());
153 goto out;
154 }
155 if (opt_process != NULL)
156 {
157 gint pid;
158 guint64 pid_start_time;
159
160 if (sscanf (opt_process, "%i,%" G_GUINT64_FORMAT, &pid, &pid_start_time)
161 == 2)
162 {
163 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
164 subject = polkit_unix_process_new_full (pid, pid_start_time);
165 G_GNUC_END_IGNORE_DEPRECATIONS
166 }
167 else if (sscanf (opt_process, "%i", &pid) == 1)
168 {
169 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
170 subject = polkit_unix_process_new (pid);
171 G_GNUC_END_IGNORE_DEPRECATIONS
172 }
173 else
174 {
175 g_printerr (_("%s: Invalid process specifier `%s'\n"),
176 g_get_prgname (), opt_process);
177 goto out;
178 }
179 }
180 if (opt_system_bus_name != NULL)
181 subject = polkit_system_bus_name_new (opt_system_bus_name);
182 /* Use parent process, if no subject has been specified */
183 if (subject == NULL)
184 {
185 pid_t pid_of_caller;
186 pid_of_caller = getppid ();
187 if (pid_of_caller == 1)
188 {
189 /* getppid() can return 1 if the parent died (meaning that we are reaped
190 * by /sbin/init); In that case we simpy bail.
191 */
192 g_printerr ("Refusing to render service to dead parents.\n");
193 goto out;
194 }
195
196 subject = polkit_unix_process_new_for_owner (pid_of_caller,
197 0, /* 0 means "look up start-time in /proc" */
198 getuid ());
199 /* really double-check the invariants guaranteed by the PolkitUnixProcess class */
200 g_assert (subject != NULL);
201 g_assert (polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject)) == pid_of_caller);
202 g_assert (polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject)) >= 0);
203 g_assert (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (subject)) > 0);
204 }
205
206 authority = polkit_authority_get_sync (NULL /* GCancellable* */, &error);
207 if (authority == NULL)
208 {
209 g_printerr ("Authorization not available. Check if polkit service is running or see debug message for more information.\n");
210 g_debug ("Error getting authority: %s (%s, %d)\n",
211 error->message, g_quark_to_string (error->domain), error->code);
212 g_error_free (error);
213 ret = 127;
214 goto out;
215 }
216
217 if (opt_fallback)
218 {
219 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
220 g_variant_builder_add (&builder, "{sv}", "fallback", g_variant_new_boolean (TRUE));
221 listener_options = g_variant_builder_end (&builder);
222 }
223
224 error = NULL;
225 /* this will fail if we can't find a controlling terminal */
226 listener = polkit_agent_text_listener_new (NULL, &error);
227 if (listener == NULL)
228 {
229 g_printerr ("Error creating textual authentication agent: %s (%s, %d)\n",
230 error->message, g_quark_to_string (error->domain), error->code);
231 g_error_free (error);
232 ret = 127;
233 goto out;
234 }
235 g_signal_connect(G_OBJECT(listener), "tty_attrs_changed",
236 G_CALLBACK(tty_attrs_changed), NULL);
237
238 local_agent_handle = polkit_agent_listener_register_with_options (listener,
239 POLKIT_AGENT_REGISTER_FLAGS_RUN_IN_THREAD,
240 subject,
241 NULL, /* object_path */
242 listener_options,
243 NULL, /* GCancellable */
244 &error);
245 listener_options = NULL; /* consumed */
246 g_object_unref (listener);
247 if (local_agent_handle == NULL)
248 {
249 g_printerr ("Error registering authentication agent: %s (%s, %d)\n",
250 error->message, g_quark_to_string (error->domain), error->code);
251 g_error_free (error);
252 goto out;
253 }
254
255 if (opt_notify_fd != -1)
256 {
257 if (close (opt_notify_fd) != 0)
258 {
259 g_printerr ("Error closing notify-fd %d: %m\n", opt_notify_fd);
260 goto out;
261 }
262 }
263
264 /* Bash leaves tty echo disabled if SIGINT/SIGTERM comes to polkitagenttextlistener.c::on_request(),
265 but due to threading the handlers cannot take care of the signal there.
266 Though if controlling terminal cannot be found, the world won't stop spinning.
267 */
268 tty_name = ctermid(NULL);
269 if (tty_name != NULL)
270 {
271 tty = fopen(tty_name, "r+");
272 }
273
274 if (tty != NULL && !tcgetattr (fileno (tty), &ts))
275 {
276 tty_flags_saved = TRUE;
277 }
278
279 memset (&sa, 0, sizeof (sa));
280 sa.sa_handler = &tty_handler;
281 /* If tty_handler() resets terminal while pkttyagent is run in background job,
282 the process gets stopped by SIGTTOU. This impacts systemctl, hence it must
283 be blocked for a while and then the process gets killed anyway.
284 */
285 sigemptyset(&sa.sa_mask);
286 sigaddset(&sa.sa_mask, SIGTTOU);
287 sigaction (SIGTERM, &sa, &savesigterm);
288 sigaction (SIGINT, &sa, &savesigint);
289 sigaction (SIGTSTP, &sa, &savesigtstp);
290
291 loop = g_main_loop_new (NULL, FALSE);
292 g_main_loop_run (loop);
293
294 out:
295 if (loop != NULL)
296 g_main_loop_unref (loop);
297
298 if (local_agent_handle != NULL)
299 polkit_agent_listener_unregister (local_agent_handle);
300
301 if (listener_options != NULL)
302 g_variant_unref (listener_options);
303
304 if (subject != NULL)
305 g_object_unref (subject);
306
307 if (authority != NULL)
308 g_object_unref (authority);
309
310 g_free (opt_process);
311 g_free (opt_system_bus_name);
312 g_option_context_free (context);
313
314 return ret;
315 }