1    	/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2    	/* NetworkManager -- Network link manager
3    	 *
4    	 * This program is free software; you can redistribute it and/or modify
5    	 * it under the terms of the GNU General Public License as published by
6    	 * the Free Software Foundation; either version 2 of the License, or
7    	 * (at your option) any later version.
8    	 *
9    	 * This program 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
12   	 * GNU General Public License for more details.
13   	 *
14   	 * You should have received a copy of the GNU General Public License along
15   	 * with this program; if not, write to the Free Software Foundation, Inc.,
16   	 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17   	 *
18   	 * Copyright (C) 2008 - 2012 Red Hat, Inc.
19   	 */
20   	
21   	#include <syslog.h>
22   	#include <stdio.h>
23   	#include <unistd.h>
24   	#include <stdlib.h>
25   	#include <string.h>
26   	#include <sys/types.h>
27   	#include <signal.h>
28   	#include <sys/stat.h>
29   	#include <sys/wait.h>
30   	#include <errno.h>
31   	#include <arpa/inet.h>
32   	
33   	#include <glib.h>
34   	#include <dbus/dbus.h>
35   	#include <dbus/dbus-glib-lowlevel.h>
36   	#include <dbus/dbus-glib.h>
37   	
38   	
39   	#include "nm-dispatcher-action.h"
40   	#include "nm-dispatcher-utils.h"
41   	#include "nm-glib-compat.h"
42   	
43   	#define NMD_SCRIPT_DIR NMCONFDIR "/dispatcher.d"
44   	
45   	static GMainLoop *loop = NULL;
46   	static gboolean debug = FALSE;
47   	
48   	typedef struct {
49   		GObject parent;
50   	
51   		/* Private data */
52   		guint quit_id;
53   		gboolean persist;
54   	} Handler;
55   	
56   	typedef struct {
57   	  GObjectClass parent;
58   	} HandlerClass;
59   	
60   	GType handler_get_type (void);
61   	
62   	#define HANDLER_TYPE         (handler_get_type ())
63   	#define HANDLER(object)      (G_TYPE_CHECK_INSTANCE_CAST ((object), HANDLER_TYPE, Handler))
64   	#define HANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), HANDLER_TYPE, HandlerClass))
65   	
66   	G_DEFINE_TYPE(Handler, handler, G_TYPE_OBJECT)
67   	
68   	static void
69   	impl_dispatch (Handler *h,
70   	               const char *action,
71   	               GHashTable *connection_hash,
72   	               GHashTable *connection_props,
73   	               GHashTable *device_props,
74   	               GHashTable *device_ip4_props,
75   	               GHashTable *device_ip6_props,
76   	               GHashTable *device_dhcp4_props,
77   	               GHashTable *device_dhcp6_props,
78   	               const char *vpn_ip_iface,
79   	               GHashTable *vpn_ip4_props,
80   	               GHashTable *vpn_ip6_props,
81   	               DBusGMethodInvocation *context);
82   	
83   	#include "nm-dispatcher-glue.h"
84   	
85   	
86   	static void
87   	handler_init (Handler *h)
88   	{
89   	}
90   	
91   	static void
92   	handler_class_init (HandlerClass *h_class)
93   	{
94   	}
95   	
96   	typedef struct Request Request;
97   	
98   	static void dispatch_one_script (Request *request);
99   	
100  	typedef struct {
101  		Request *request;
102  	
103  		char *script;
104  		GPid pid;
105  		DispatchResult result;
106  		char *error;
107  	} ScriptInfo;
108  	
109  	struct Request {
110  		Handler *handler;
111  	
112  		DBusGMethodInvocation *context;
113  		char *action;
114  		char *iface;
115  		char **envp;
116  		GPtrArray *scripts;  /* list of ScriptInfo */
117  		guint idx;
118  	
119  		guint script_watch_id;
120  		guint script_timeout_id;
121  	};
122  	
123  	static void
124  	script_info_free (gpointer ptr)
125  	{
126  		ScriptInfo *info = ptr;
127  	
128  		g_free (info->script);
129  		g_free (info->error);
130  		g_free (info);
131  	}
132  	
133  	static void
134  	request_free (Request *request)
135  	{
136  		g_free (request->action);
137  		g_free (request->iface);
138  		g_strfreev (request->envp);
139  		if (request->scripts)
140  			g_ptr_array_free (request->scripts, TRUE);
141  	}
142  	
143  	static gboolean
144  	quit_timeout_cb (gpointer user_data)
145  	{
146  		g_main_loop_quit (loop);
147  		return FALSE;
148  	}
149  	
150  	static void
151  	quit_timeout_reschedule (Handler *h)
152  	{
153  		if (h->quit_id)
154  			g_source_remove (h->quit_id);
155  		if (!h->persist)
156  			h->quit_id = g_timeout_add_seconds (10, quit_timeout_cb, NULL);
157  	}
158  	
159  	static gboolean
160  	next_script (gpointer user_data)
161  	{
162  		Request *request = user_data;
163  		GPtrArray *results;
164  		GValueArray *item;
165  		guint i;
166  	
167  		quit_timeout_reschedule (request->handler);
168  	
169  		request->idx++;
170  		if (request->idx < request->scripts->len) {
171  			dispatch_one_script (request);
172  			return FALSE;
173  		}
174  	
175  		/* All done */
176  		results = g_ptr_array_new_full (request->scripts->len, (GDestroyNotify) g_value_array_free);
177  		for (i = 0; i < request->scripts->len; i++) {
178  			ScriptInfo *script = g_ptr_array_index (request->scripts, i);
179  			GValue elt = G_VALUE_INIT;
180  	
181  			item = g_value_array_new (3);
182  	
183  			/* Script path */
184  			g_value_init (&elt, G_TYPE_STRING);
185  			g_value_set_string (&elt, script->script);
186  			g_value_array_append (item, &elt);
187  			g_value_unset (&elt);
188  	
189  			/* Result */
190  			g_value_init (&elt, G_TYPE_UINT);
191  			g_value_set_uint (&elt, script->result);
192  			g_value_array_append (item, &elt);
193  			g_value_unset (&elt);
194  	
195  			/* Error */
196  			g_value_init (&elt, G_TYPE_STRING);
197  			g_value_set_string (&elt, script->error ? script->error : "");
198  			g_value_array_append (item, &elt);
199  			g_value_unset (&elt);
200  	
201  			g_ptr_array_add (results, item);
202  		}
203  	
204  		dbus_g_method_return (request->context, results);
205  	
206  		request_free (request);
207  		g_ptr_array_unref (results);
208  		return FALSE;
209  	}
210  	
211  	static void
212  	script_watch_cb (GPid pid, gint status, gpointer user_data)
213  	{
214  		ScriptInfo *script = user_data;
215  		guint err;
216  	
217  		g_assert (pid == script->pid);
218  	
219  		script->request->script_watch_id = 0;
220  		g_source_remove (script->request->script_timeout_id);
221  		script->request->script_timeout_id = 0;
222  	
223  		if (WIFEXITED (status)) {
224  			err = WEXITSTATUS (status);
225  			if (err == 0)
226  				script->result = DISPATCH_RESULT_SUCCESS;
227  			else {
228  				script->error = g_strdup_printf ("Script '%s' exited with error status %d.",
229  				                                 script->script, err);
230  			}
231  		} else if (WIFSTOPPED (status)) {
232  			script->error = g_strdup_printf ("Script '%s' stopped unexpectedly with signal %d.",
233  			                                 script->script, WSTOPSIG (status));
234  		} else if (WIFSIGNALED (status)) {
235  			script->error = g_strdup_printf ("Script '%s' died with signal %d",
236  			                                 script->script, WTERMSIG (status));
237  		} else {
238  			script->error = g_strdup_printf ("Script '%s' died from an unknown cause",
239  			                                 script->script);
240  		}
241  	
242  		if (script->result != DISPATCH_RESULT_SUCCESS) {
243  			script->result = DISPATCH_RESULT_FAILED;
244  			g_warning ("%s", script->error);
245  		}
246  	
247  		g_spawn_close_pid (script->pid);
248  		next_script (script->request);
249  	}
250  	
251  	static gboolean
252  	script_timeout_cb (gpointer user_data)
253  	{
254  		ScriptInfo *script = user_data;
255  	
256  		g_source_remove (script->request->script_watch_id);
257  		script->request->script_watch_id = 0;
258  		script->request->script_timeout_id = 0;
259  	
260  		g_warning ("Script '%s' took too long; killing it.", script->script);
261  	
262  		if (kill (script->pid, 0) == 0)
263  			kill (script->pid, SIGKILL);
264  		waitpid (script->pid, NULL, 0);
265  	
266  		script->error = g_strdup_printf ("Script '%s' timed out.", script->script);
267  		script->result = DISPATCH_RESULT_TIMEOUT;
268  	
269  		g_spawn_close_pid (script->pid);
270  		g_idle_add (next_script, script->request);
271  		return FALSE;
272  	}
273  	
274  	static inline gboolean
275  	check_permissions (struct stat *s, GError **error)
276  	{
277  		g_return_val_if_fail (s != NULL, FALSE);
278  		g_return_val_if_fail (error != NULL, FALSE);
279  		g_return_val_if_fail (*error == NULL, FALSE);
280  	
281  		/* Only accept regular files */
282  		if (!S_ISREG (s->st_mode)) {
283  			g_set_error (error, 0, 0, "not a regular file.");
284  			return FALSE;
285  		}
286  	
287  		/* Only accept files owned by root */
288  		if (s->st_uid != 0) {
289  			g_set_error (error, 0, 0, "not owned by root.");
290  			return FALSE;
291  		}
292  	
293  		/* Only accept files not writable by group or other, and not SUID */
294  		if (s->st_mode & (S_IWGRP | S_IWOTH | S_ISUID)) {
295  			g_set_error (error, 0, 0, "writable by group or other, or set-UID.");
296  			return FALSE;
297  		}
298  	
299  		/* Only accept files executable by the owner */
300  		if (!(s->st_mode & S_IXUSR)) {
301  			g_set_error (error, 0, 0, "not executable by owner.");
302  			return FALSE;
303  		}
304  	
305  		return TRUE;
306  	}
307  	
308  	static gboolean
309  	check_filename (const char *file_name)
310  	{
311  		char *bad_suffixes[] = { "~", ".rpmsave", ".rpmorig", ".rpmnew", NULL };
312  		char *tmp;
313  		guint i;
314  	
315  		/* File must not be a backup file, package management file, or start with '.' */
316  	
317  		if (file_name[0] == '.')
318  			return FALSE;
319  		for (i = 0; bad_suffixes[i]; i++) {
320  			if (g_str_has_suffix (file_name, bad_suffixes[i]))
321  				return FALSE;
322  		}
323  		tmp = g_strrstr (file_name, ".dpkg-");
324  		if (tmp && (tmp == strrchr (file_name, '.')))
325  			return FALSE;
326  		return TRUE;
327  	}
328  	
329  	static void
330  	child_setup (gpointer user_data G_GNUC_UNUSED)
331  	{
332  		/* We are in the child process at this point */
333  		/* Give child a different process group to ensure signal separation. */
334  		pid_t pid = getpid ();
335  		setpgid (pid, pid);
336  	}
337  	
338  	static void
339  	dispatch_one_script (Request *request)
340  	{
341  		GError *error = NULL;
342  		gchar *argv[4];
343  		ScriptInfo *script = g_ptr_array_index (request->scripts, request->idx);
344  	
345  		argv[0] = script->script;
346  		argv[1] = request->iface ? request->iface : "none";
347  		argv[2] = request->action;
348  		argv[3] = NULL;
349  	
350  		if (debug)
351  			g_message ("Script: %s %s %s", script->script, request->iface ? request->iface : "(none)", request->action);
352  	
353  		if (g_spawn_async ("/", argv, request->envp, G_SPAWN_DO_NOT_REAP_CHILD, child_setup, request, &script->pid, &error)) {
354  			request->script_watch_id = g_child_watch_add (script->pid, (GChildWatchFunc) script_watch_cb, script);
355  			request->script_timeout_id = g_timeout_add_seconds (3, script_timeout_cb, script);
356  		} else {
357  			g_warning ("Failed to execute script '%s': (%d) %s",
358  			           script->script, error->code, error->message);
359  			script->result = DISPATCH_RESULT_EXEC_FAILED;
360  			script->error = g_strdup (error->message);
361  			g_clear_error (&error);
362  	
363  			/* Try the next script */
364  			g_idle_add (next_script, request);
365  		}
366  	}
367  	
368  	static GSList *
369  	find_scripts (void)
370  	{
371  		GDir *dir;
372  		const char *filename;
373  		GSList *sorted = NULL;
374  		GError *error = NULL;
375  	
376  		if (!(dir = g_dir_open (NMD_SCRIPT_DIR, 0, &error))) {
377  			g_warning ("Failed to open dispatcher directory '%s': (%d) %s",
378  			           NMD_SCRIPT_DIR, error->code, error->message);
379  			g_error_free (error);
380  			return NULL;
381  		}
382  	
383  		while ((filename = g_dir_read_name (dir))) {
384  			char *path;
385  			struct stat	st;
386  			int err;
387  	
388  			if (!check_filename (filename))
389  				continue;
390  	
391  			path = g_build_filename (NMD_SCRIPT_DIR, filename, NULL);
392  	
393  			err = stat (path, &st);
394  			if (err)
395  				g_warning ("Failed to stat '%s': %d", path, err);
396  			else if (!check_permissions (&st, &error)) {
397  				g_warning ("Cannot execute '%s': %s", path, error->message);
398  				g_clear_error (&error);
399  			} else {
400  				/* success */
401  				sorted = g_slist_insert_sorted (sorted, path, (GCompareFunc) g_strcmp0);
402  			}
403  		}
404  		g_dir_close (dir);
405  	
406  		return sorted;
407  	}
408  	
409  	static void
410  	impl_dispatch (Handler *h,
411  	               const char *str_action,
412  	               GHashTable *connection_hash,
413  	               GHashTable *connection_props,
414  	               GHashTable *device_props,
415  	               GHashTable *device_ip4_props,
416  	               GHashTable *device_ip6_props,
417  	               GHashTable *device_dhcp4_props,
418  	               GHashTable *device_dhcp6_props,
419  	               const char *vpn_ip_iface,
420  	               GHashTable *vpn_ip4_props,
421  	               GHashTable *vpn_ip6_props,
422  	               DBusGMethodInvocation *context)
423  	{
424  		GSList *sorted_scripts = NULL;
425  		GSList *iter;
426  		Request *request;
427  		char **p;
428  		char *iface = NULL;
429  	
430  		sorted_scripts = find_scripts ();
431  	
432  		if (!sorted_scripts) {
433  			dbus_g_method_return (context, g_ptr_array_new ());
434  			return;
435  		}
436  	
437  		quit_timeout_reschedule (h);
438  	
439  		request = g_malloc0 (sizeof (*request));
440  		request->handler = h;
441  		request->context = context;
442  		request->action = g_strdup (str_action);
443  	
444  		request->envp = nm_dispatcher_utils_construct_envp (str_action,
445  		                                                    connection_hash,
446  		                                                    connection_props,
447  		                                                    device_props,
448  		                                                    device_ip4_props,
449  		                                                    device_ip6_props,
450  		                                                    device_dhcp4_props,
451  		                                                    device_dhcp6_props,
452  		                                                    vpn_ip_iface,
453  		                                                    vpn_ip4_props,
454  		                                                    vpn_ip6_props,
455  		                                                    &iface);
456  	
457  		if (debug) {
458  			g_message ("------------ Action ID %p '%s' Interface %s Environment ------------",
459  			           context, str_action, iface ? iface : "(none)");
460  			for (p = request->envp; *p; p++)
461  				g_message ("  %s", *p);
462  			g_message ("\n");
463  		}
464  	
465  		request->iface = g_strdup (iface);
466  	
467  		request->scripts = g_ptr_array_new_full (5, script_info_free);
468  		for (iter = sorted_scripts; iter; iter = g_slist_next (iter)) {
469  			ScriptInfo *s = g_malloc0 (sizeof (*s));
470  			s->request = request;
471  			s->script = iter->data;
472  			g_ptr_array_add (request->scripts, s);
473  		}
474  		g_slist_free (sorted_scripts);
475  	
476  		/* start dispatching scripts */
477  		dispatch_one_script (request);
478  	}
479  	
480  	static void
481  	destroy_cb (DBusGProxy *proxy, gpointer user_data)
482  	{
483  		g_warning ("Disconnected from the system bus, exiting.");
484  		g_main_loop_quit (loop);
485  	}
486  	
487  	static DBusGConnection *
488  	dbus_init (void)
489  	{
490  		GError *error = NULL;
491  		DBusGConnection *bus;
492  		DBusConnection *connection;
493  		DBusGProxy *proxy;
494  		int result;
495  	
496  		dbus_connection_set_change_sigpipe (TRUE);
497  	
498  		bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
499  		if (!bus) {
500  			g_warning ("Could not get the system bus.  Make sure "
501  			           "the message bus daemon is running!  Message: %s",
502  			           error->message);
503  			g_error_free (error);
504  			return NULL;
505  		}
506  	
507  		/* Clean up nicely if we get kicked off the bus */
508  		connection = dbus_g_connection_get_connection (bus);
509  		dbus_connection_set_exit_on_disconnect (connection, FALSE);
510  	
511  		proxy = dbus_g_proxy_new_for_name (bus,
512  		                                   "org.freedesktop.DBus",
513  		                                   "/org/freedesktop/DBus",
514  		                                   "org.freedesktop.DBus");
515  		if (!proxy) {
516  			g_warning ("Could not create the DBus proxy!");
517  			goto error;
518  		}
519  	
520  		g_signal_connect (proxy, "destroy", G_CALLBACK (destroy_cb), NULL);
521  	
(2) Event example_checked: Example1: "dbus_g_proxy_call(proxy, "RequestName", &error, 64UL, "org.freedesktop.nm_dispatcher", 28UL, 4, 0UL, 28UL, &result, 0UL)" has its value checked in "dbus_g_proxy_call(proxy, "RequestName", &error, 64UL, "org.freedesktop.nm_dispatcher", 28UL, 4, 0UL, 28UL, &result, 0UL)".
Also see events: [check_return][example_checked][example_checked][example_checked][example_checked][unchecked_value]
522  		if (!dbus_g_proxy_call (proxy, "RequestName", &error,
523  		                        G_TYPE_STRING, NM_DISPATCHER_DBUS_SERVICE,
524  		                        G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
525  		                        G_TYPE_INVALID,
526  		                        G_TYPE_UINT, &result,
527  		                        G_TYPE_INVALID)) {
528  			g_warning ("Could not acquire the " NM_DISPATCHER_DBUS_SERVICE " service.\n"
529  			           "  Message: '%s'", error->message);
530  			g_error_free (error);
531  			goto error;
532  		}
533  	
534  		if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
535  			g_warning ("Could not acquire the " NM_DISPATCHER_DBUS_SERVICE " service "
536  			           "as it is already taken.  Result: %d",
537  			           result);
538  			goto error;
539  		}
540  	
541  		return bus;
542  	
543  	error:
544  		if (proxy)
545  			g_object_unref (proxy);
546  		dbus_g_connection_unref (bus);
547  		return NULL;
548  	}
549  	
550  	static void
551  	log_handler (const gchar *log_domain,
552  	             GLogLevelFlags log_level,
553  	             const gchar *message,
554  	             gpointer ignored)
555  	{
556  		int syslog_priority;	
557  	
558  		switch (log_level) {
559  		case G_LOG_LEVEL_ERROR:
560  			syslog_priority = LOG_CRIT;
561  			break;
562  		case G_LOG_LEVEL_CRITICAL:
563  			syslog_priority = LOG_ERR;
564  			break;
565  		case G_LOG_LEVEL_WARNING:
566  			syslog_priority = LOG_WARNING;
567  			break;
568  		case G_LOG_LEVEL_MESSAGE:
569  			syslog_priority = LOG_NOTICE;
570  			break;
571  		case G_LOG_LEVEL_DEBUG:
572  			syslog_priority = LOG_DEBUG;
573  			break;
574  		case G_LOG_LEVEL_INFO:
575  		default:
576  			syslog_priority = LOG_INFO;
577  			break;
578  		}
579  	
580  		syslog (syslog_priority, "%s", message);
581  	}
582  	
583  	
584  	static void
585  	logging_setup (void)
586  	{
587  		openlog (G_LOG_DOMAIN, LOG_CONS, LOG_DAEMON);
588  		g_log_set_handler (G_LOG_DOMAIN, 
589  		                   G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
590  		                   log_handler,
591  		                   NULL);
592  	}
593  	
594  	static void
595  	logging_shutdown (void)
596  	{
597  		closelog ();
598  	}
599  	
600  	static void
601  	signal_handler (int signo)
602  	{
603  		if (signo == SIGINT || signo == SIGTERM) {
604  			g_message ("Caught signal %d, shutting down...", signo);
605  			g_main_loop_quit (loop);
606  		}
607  	}
608  	
609  	static void
610  	setup_signals (void)
611  	{
612  		struct sigaction action;
613  		sigset_t mask;
614  	
615  		sigemptyset (&mask);
616  		action.sa_handler = signal_handler;
617  		action.sa_mask = mask;
618  		action.sa_flags = 0;
619  		sigaction (SIGTERM,  &action, NULL);
620  		sigaction (SIGINT,  &action, NULL);
621  	}
622  	
623  	int
624  	main (int argc, char **argv)
625  	{
626  		GOptionContext *opt_ctx;
627  		GError *error = NULL;
628  		gboolean persist = FALSE;
629  		DBusGConnection *bus;
630  		Handler *handler;
631  	
632  		GOptionEntry entries[] = {
633  			{ "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Output to console rather than syslog", NULL },
634  			{ "persist", 0, 0, G_OPTION_ARG_NONE, &persist, "Don't quit after a short timeout", NULL },
635  			{ NULL }
636  		};
637  	
638  		opt_ctx = g_option_context_new (NULL);
639  		g_option_context_set_summary (opt_ctx, "Executes scripts upon actions by NetworkManager.");
640  		g_option_context_add_main_entries (opt_ctx, entries, NULL);
641  	
642  		if (!g_option_context_parse (opt_ctx, &argc, &argv, &error)) {
643  			g_warning ("%s\n", error->message);
644  			g_error_free (error);
645  			return 1;
646  		}
647  	
648  		g_option_context_free (opt_ctx);
649  	
650  		g_type_init ();
651  		setup_signals ();
652  	
653  		if (!debug)
654  			logging_setup ();
655  	
656  		loop = g_main_loop_new (NULL, FALSE);
657  	
658  		bus = dbus_init ();
659  		if (!bus)
660  			return 1;
661  	
662  		handler = g_object_new (HANDLER_TYPE, NULL);
663  		if (!handler)
664  			return 1;
665  		handler->persist = persist;
666  	
667  		dbus_g_object_type_install_info (HANDLER_TYPE, &dbus_glib_nm_dispatcher_object_info);
668  		dbus_g_connection_register_g_object (bus,
669  		                                     NM_DISPATCHER_DBUS_PATH,
670  		                                     G_OBJECT (handler));
671  	
672  		if (!persist)
673  			handler->quit_id = g_timeout_add_seconds (10, quit_timeout_cb, NULL);
674  	
675  		g_main_loop_run (loop);
676  	
677  		g_object_unref (handler);
678  		dbus_g_connection_unref (bus);
679  	
680  		if (!debug)
681  			logging_shutdown ();
682  	
683  		return 0;
684  	}
685  	
686