nm-vpnc-service.c 26.8 KB
Newer Older
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* nm-vpnc-service - vpnc integration with NetworkManager
 *
 * Dan Williams <dcbw@redhat.com>
 *
 * 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 2 of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * (C) Copyright 2005 Red Hat, Inc.
 */

#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "NetworkManager.h"
#include "nm-vpnc-service.h"
#include "nm-utils.h"

#define VPNC_BINARY_PATH_DEFAULT	"/usr/sbin/vpnc"
#define NM_VPNC_HELPER_PATH		BINDIR"/nm-vpnc-service-vpnc-helper"
#define NM_VPNC_PID_FILE_PATH		LOCALSTATEDIR"/run/vpnc/pid"
#define NM_VPNC_CONFIG_FILE_PATH	LOCALSTATEDIR"/run/vpnc/nm-vpnc-service-vpnc.conf"

typedef struct NmVpncData
{
	GMainLoop *		loop;
	DBusConnection	*	con;
	NMVPNState		state;
	GPid				pid;
	guint			quit_timer;
	guint			helper_timer;
} NmVpncData;


static gboolean nm_vpnc_dbus_handle_stop_vpn (NmVpncData *data);


/*
 * nm_dbus_create_error_message
 *
 * Make a DBus error message
 *
 */
static DBusMessage *nm_dbus_create_error_message (DBusMessage *message, const char *exception_namespace,
										const char *exception, const char *format, ...)
{
	char *exception_text;
	DBusMessage	*reply_message;
	va_list		 args;
	char			 error_text[512];

	va_start (args, format);
	vsnprintf (error_text, 512, format, args);
	va_end (args);

	exception_text = g_strdup_printf ("%s.%s", exception_namespace, exception);
	reply_message = dbus_message_new_error (message, exception_text, error_text);
	g_free (exception_text);

	return (reply_message);
}


/*
 * nm_vpnc_dbus_signal_login_failed
 *
 * Signal the bus that VPN login failed.
 *
 */
static void nm_vpnc_dbus_signal_login_failed (NmVpncData *data)
{
	DBusMessage	*message;
	const char	*error_msg = "The VPN login failed because the user name and password were not accepted.";

	g_return_if_fail (data != NULL);

	if (!(message = dbus_message_new_signal (NM_DBUS_PATH_VPNC, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_SIGNAL_LOGIN_FAILED)))
	{
		nm_warning ("Not enough memory for new dbus message!");
		return;
	}

	dbus_message_append_args (message, DBUS_TYPE_STRING, &error_msg, DBUS_TYPE_INVALID);
	if (!dbus_connection_send (data->con, message, NULL))
		nm_warning ("Could not raise the signal!");

	dbus_message_unref (message);
}


/*
 * nm_vpnc_dbus_signal_state_change
 *
 * Signal the bus that our state changed.
 *
 */
static void nm_vpnc_dbus_signal_state_change (NmVpncData *data, NMVPNState old_state)
{
	DBusMessage	*message;

	g_return_if_fail (data != NULL);

	if (!(message = dbus_message_new_signal (NM_DBUS_PATH_VPNC, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_SIGNAL_STATE_CHANGE)))
	{
		nm_warning ("nm_vpnc_dbus_signal_state_change(): Not enough memory for new dbus message!");
		return;
	}

	dbus_message_append_args (message, DBUS_TYPE_UINT32, &old_state, DBUS_TYPE_UINT32, &(data->state), DBUS_TYPE_INVALID);

	if (!dbus_connection_send (data->con, message, NULL))
		nm_warning ("nm_vpnc_dbus_signal_state_change(): Could not raise the signal!");

	dbus_message_unref (message);
}


/*
 * nm_vpnc_set_state
 *
 * Set our state and make sure to signal the bus.
 *
 */
static void nm_vpnc_set_state (NmVpncData *data, NMVPNState new_state)
{
	NMVPNState	old_state;

	g_return_if_fail (data != NULL);

	old_state = data->state;
	data->state = new_state;
	nm_vpnc_dbus_signal_state_change (data, old_state);
}


/*
 * nm_vpnc_quit_timer_cb
 *
 * Callback to quit nm-vpnc-service after a certain period of time.
 *
 */
static gboolean nm_vpnc_quit_timer_cb (NmVpncData *data)
{
	data->quit_timer = 0;

	g_return_val_if_fail (data != NULL, FALSE);

	g_main_loop_quit (data->loop);

	return FALSE;
}


/*
 * nm_vpnc_schedule_quit_timer
 *
 * If vpnc isn't running, and we haven't been asked to do anything in a while,
 * then we just exit since NetworkManager will re-launch us later.
 *
 */
static void nm_vpnc_schedule_quit_timer (NmVpncData *data, guint interval)
{
	g_return_if_fail (data != NULL);

	if (data->quit_timer == 0)
		data->quit_timer = g_timeout_add (interval, (GSourceFunc) nm_vpnc_quit_timer_cb, data);
}


/*
 * nm_vpnc_cancel_quit_timer
 *
 * Cancel a quit timer that we've scheduled before.
 *
 */
static void nm_vpnc_cancel_quit_timer (NmVpncData *data)
{
	g_return_if_fail (data != NULL);

	if (data->quit_timer > 0)
		g_source_remove (data->quit_timer);
}


/*
 * nm_vpnc_helper_timer_cb
 *
 * If we haven't received the IP4 config info from the helper before the timeout
 * occurs, we kill vpnc.
 *
 */
static gboolean nm_vpnc_helper_timer_cb (NmVpncData *data)
{
	data->helper_timer = 0;

	g_return_val_if_fail (data != NULL, FALSE);

	nm_vpnc_dbus_handle_stop_vpn (data);

	return FALSE;
}


/*
 * nm_vpnc_schedule_helper_timer
 *
 * Once vpnc is running, we wait for the helper to return the IP4 configuration
 * information to us.  If we don't receive that information within 7 seconds,
 * we kill vpnc.
 *
 */
static void nm_vpnc_schedule_helper_timer (NmVpncData *data)
{
	g_return_if_fail (data != NULL);

	if (data->helper_timer == 0)
		data->helper_timer = g_timeout_add (7000, (GSourceFunc) nm_vpnc_helper_timer_cb, data);
}


/*
 * nm_vpnc_cancel_helper_timer
 *
 * Cancel a helper timer that we've scheduled before.
 *
 */
static void nm_vpnc_cancel_helper_timer (NmVpncData *data)
{
	g_return_if_fail (data != NULL);

	if (data->helper_timer > 0)
		g_source_remove (data->helper_timer);
}


/*
 * vpnc_watch_cb
 *
 * Watch our child vpnc process and get notified of events from it.
 *
 */
static void vpnc_watch_cb (GPid pid, gint status, gpointer user_data)
{
	guint	error = -1;

	NmVpncData *data = (NmVpncData *)user_data;

	if (WIFEXITED (status))
	{
		error = WEXITSTATUS (status);
		nm_warning ("vpnc exited with error code %d", error);
	}
	else if (WIFSTOPPED (status))
		nm_warning ("vpnc stopped unexpectedly with signal %d", WSTOPSIG (status));
	else if (WIFSIGNALED (status))
		nm_warning ("vpnc died with signal %d", WTERMSIG (status));
	else
		nm_warning ("vpnc died from an unknown cause");

	/* Reap child if needed. */
	waitpid (data->pid, NULL, WNOHANG);
	data->pid = 0;

	/* Must be after data->state is set since signals use data->state */
	switch (error)
	{
		case 2:	/* Couldn't log in */
			nm_vpnc_dbus_signal_login_failed (data);
			break;

		case 1:	/* Other error (couldn't bind to address, etc) */
			break;

		case 0:	/* Success, vpnc has daemonized */
			{
				GPid	daemon_pid;
				char *contents;

				/* Grab the vpnc daemon's PID from its pidfile */
				if (g_file_get_contents (NM_VPNC_PID_FILE_PATH, &contents, NULL, NULL))
				{
					data->pid = atoi (g_strstrip (contents));
					nm_info ("vpnc daemon's PID is %d\n", data->pid);
					g_free (contents);
				}
			}
			break;

		default:
			break;
	}

	unlink (NM_VPNC_CONFIG_FILE_PATH);

	/* If vpnc did not daemonize (due to errors), we quit after a bit */
	if (data->pid <= 0)
	{
		nm_vpnc_set_state (data, NM_VPN_STATE_STOPPED);
		unlink (NM_VPNC_PID_FILE_PATH);

		nm_vpnc_schedule_quit_timer (data, 10000);
	}

	/* State change from STARTING->STARTED happens when we get successful
	 * ip4 config info from the helper.
	 */
}


/*
 * nm_vpnc_start_vpn_binary
 *
 * Start the vpnc binary with a set of arguments and a config file.
 *
 */
static gboolean nm_vpnc_start_vpnc_binary (NmVpncData *data)
{
	GPid			pid;
	const char *	vpnc_binary;
	GPtrArray *	vpnc_argv;
344
	GError *		error = NULL;
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
	gboolean		success = FALSE;
	GSource *		vpnc_watch;

	g_return_val_if_fail (data != NULL, FALSE);

	data->pid = 0;

	unlink (NM_VPNC_PID_FILE_PATH);

	vpnc_argv = g_ptr_array_new ();
	vpnc_binary = g_getenv ("VPNC_BINARY_PATH") ? g_getenv ("VPNC_BINARY_PATH") : VPNC_BINARY_PATH_DEFAULT;
	g_ptr_array_add (vpnc_argv, (char *) vpnc_binary);
	g_ptr_array_add (vpnc_argv, "--script");
	g_ptr_array_add (vpnc_argv, NM_VPNC_HELPER_PATH);
	g_ptr_array_add (vpnc_argv, "--pid-file");
	g_ptr_array_add (vpnc_argv, NM_VPNC_PID_FILE_PATH);
	g_ptr_array_add (vpnc_argv, NM_VPNC_CONFIG_FILE_PATH);
	g_ptr_array_add (vpnc_argv, NULL);

	if (!g_spawn_async (NULL, (char **) vpnc_argv->pdata, NULL,
				G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error))
	{
		g_ptr_array_free (vpnc_argv, TRUE);
		nm_warning ("vpnc failed to start.  error: '%s'", error->message);
369
		g_error_free(error);
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
		return FALSE;
	}
	g_ptr_array_free (vpnc_argv, TRUE);

	nm_info ("vpnc started with pid %d", pid);

	data->pid = pid;
	vpnc_watch = g_child_watch_source_new (pid);
	g_source_set_callback (vpnc_watch, (GSourceFunc) vpnc_watch_cb, data, NULL);
	g_source_attach (vpnc_watch, NULL);
	g_source_unref (vpnc_watch);

	nm_vpnc_schedule_helper_timer (data);

	return TRUE;
}


/*
 * nm_vpnc_config_file_generate
 *
 * Generate a vpnc config file.
 *
 */
static gboolean nm_vpnc_config_file_generate (const char *user_name, const char *password, const char **data_items, const int num_items)
{
396
397
398
399
400
401
	char *	string;
	int		out_fd;
	int		i, x;
	char *	dirname;
	char *	cmd;
	int		ret;
402
403
404
405
406

	g_return_val_if_fail (user_name != NULL, FALSE);
	g_return_val_if_fail (password != NULL, FALSE);
	g_return_val_if_fail (data_items != NULL, FALSE);

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
	/* Ensure that the config file's directory exists */
	dirname = g_path_get_dirname (NM_VPNC_CONFIG_FILE_PATH);
	if (!dirname || !strlen (dirname))
	{
		nm_warning ("Could not get dirname for vpnc config file path '%s'.", NM_VPNC_CONFIG_FILE_PATH);
		return FALSE;
	}

	cmd = g_strdup_printf ("/bin/mkdir -p -m 700 %s", dirname);
	ret = system (cmd);
	if ((ret == -1) || (WEXITSTATUS(ret) != 0))
	{
		nm_warning ("Could not create dirname for vpnc config file path '%s'.", NM_VPNC_CONFIG_FILE_PATH);
		g_free (cmd);
		g_free (dirname);
		return FALSE;
	}
	g_free (cmd);

426
427
428
429
430
431
432
433
434
	unlink (NM_VPNC_CONFIG_FILE_PATH);
	out_fd = open (NM_VPNC_CONFIG_FILE_PATH, O_WRONLY|O_CREAT|O_TRUNC, 0600);
	if (out_fd < 0)
	{
		nm_warning ("Could not create vpnc config file '%s'.", NM_VPNC_CONFIG_FILE_PATH);
		return FALSE;
	}

	string = g_strdup_printf ("Xauth username %s\n", user_name);
Peter Jones's avatar
Peter Jones committed
435
	x = write (out_fd, string, strlen (string));
436
437
438
	g_free (string);
	
	string = g_strdup_printf ("Xauth password %s\n", password);
Peter Jones's avatar
Peter Jones committed
439
	x = write (out_fd, string, strlen (string));
440
441
442
443
444
	g_free (string);

	for (i = 0; i < num_items; i++)
	{
		char *temp = g_strdup_printf ("%s\n", data_items[i]);
Peter Jones's avatar
Peter Jones committed
445
		x = write (out_fd, temp, strlen (temp));
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
		g_free (temp);
		string++;
	}

	close (out_fd);

	return TRUE;
}


/*
 * nm_vpnc_dbus_handle_start_vpn
 *
 * Parse message arguments and start the VPN connection.
 *
 */
static gboolean nm_vpnc_dbus_handle_start_vpn (DBusMessage *message, DBusMessage **reply, NmVpncData *data)
{
	const char		**data_items = NULL;
	int				  num_items = -1;
	const char		 *name = NULL;
	const char		 *user_name = NULL;
	const char		 *password = NULL;
	DBusError			  error;
	gboolean			  success = FALSE;

	g_return_val_if_fail (message != NULL, FALSE);
	g_return_val_if_fail (data != NULL, FALSE);
	g_return_val_if_fail (reply != NULL, FALSE);

	dbus_error_init (&error);
	if (!dbus_message_get_args (message, &error,
							DBUS_TYPE_STRING, &name,
							DBUS_TYPE_STRING, &user_name,
							DBUS_TYPE_STRING, &password,
							DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &data_items, &num_items,
							DBUS_TYPE_INVALID))
	{
		nm_warning ("Could not process the request because its arguments were invalid.  dbus said: '%s'", error.message);
		dbus_error_free (&error);
		*reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_BAD_ARGUMENTS,
					"Could not process the request because its arguments were invalid.");
		goto out;
	}

	/* Now we can finally try to activate the VPN */
	nm_vpnc_set_state (data, NM_VPN_STATE_STARTING);
	if (nm_vpnc_config_file_generate (user_name, password, data_items, num_items))
		success = nm_vpnc_start_vpnc_binary (data);
	if (!success)
		nm_vpnc_set_state (data, NM_VPN_STATE_STOPPED);

out:
	return success;
}


/*
 * nm_vpnc_dbus_handle_stop_vpn
 *
 * Stop the running vpnc dameon.
 *
 */
static gboolean nm_vpnc_dbus_handle_stop_vpn (NmVpncData *data)
{
	g_return_val_if_fail (data != NULL, FALSE);

	if (data->pid > 0)
	{
		nm_vpnc_set_state (data, NM_VPN_STATE_STOPPING);

		kill (data->pid, SIGTERM);
		nm_info ("Terminated vpnc daemon with PID %d.", data->pid);
		data->pid = 0;

		nm_vpnc_set_state (data, NM_VPN_STATE_STOPPED);
		nm_vpnc_schedule_quit_timer (data, 10000);
	}

	return TRUE;
}


/*
 * nm_vpnc_dbus_start_vpn
 *
 * Begin a VPN connection.
 *
 */
static DBusMessage *nm_vpnc_dbus_start_vpn (DBusConnection *con, DBusMessage *message, NmVpncData *data)
{
	DBusMessage		*reply = NULL;
	gboolean			 success = FALSE;

	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (con != NULL, NULL);
	g_return_val_if_fail (message != NULL, NULL);

	switch (data->state)
	{
		case NM_VPN_STATE_STARTING:
			reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_STARTING_IN_PROGRESS,
						"Could not process the request because the VPN connection is already being started.");
			break;

		case NM_VPN_STATE_STARTED:
			reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_ALREADY_STARTED,
						"Could not process the request because a VPN connection was already active.");
			break;

		case NM_VPN_STATE_STOPPING:
			reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_STOPPING_IN_PROGRESS,
						"Could not process the request because the VPN connection is being stopped.");
			break;

		case NM_VPN_STATE_STOPPED:
			nm_vpnc_cancel_quit_timer (data);
			nm_vpnc_dbus_handle_start_vpn (message, &reply, data);
			if (!reply)
				reply = dbus_message_new_method_return (message);
			break;

		default:
			g_assert_not_reached();
			break;
	}

out:
	return reply;
}


/*
 * nm_vpnc_dbus_stop_vpn
 *
 * Terminate a VPN connection.
 *
 */
static DBusMessage *nm_vpnc_dbus_stop_vpn (DBusConnection *con, DBusMessage *message, NmVpncData *data)
{
	DBusMessage		*reply = NULL;

	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (con != NULL, NULL);
	g_return_val_if_fail (message != NULL, NULL);

	switch (data->state)
	{
		case NM_VPN_STATE_STOPPING:
			reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_STOPPING_IN_PROGRESS,
						"Could not process the request because the VPN connection is already being stopped.");
			break;

		case NM_VPN_STATE_STOPPED:
			reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_ALREADY_STOPPED,
						"Could not process the request because no VPN connection was active.");
			break;

		case NM_VPN_STATE_STARTING:
		case NM_VPN_STATE_STARTED:
			nm_vpnc_dbus_handle_stop_vpn (data);
			reply = dbus_message_new_method_return (message);
			break;

		default:
			g_assert_not_reached();
			break;
	}

out:
	return reply;
}


/*
 * nm_vpnc_dbus_get_state
 *
 * Return some state information to NetworkManager.
 *
 */
static DBusMessage *nm_vpnc_dbus_get_state (DBusConnection *con, DBusMessage *message, NmVpncData *data)
{
	DBusMessage		*reply = NULL;

	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (con != NULL, NULL);
	g_return_val_if_fail (message != NULL, NULL);

	if ((reply = dbus_message_new_method_return (message)))
		dbus_message_append_args (reply, DBUS_TYPE_UINT32, &(data->state), DBUS_TYPE_INVALID);

	return reply;
}


/*
 * nm_vpnc_dbus_process_helper_config_error
 *
 * Signal the bus that the helper could not get all the configuration information
 * it needed.
 *
 */
static void nm_vpnc_dbus_process_helper_config_error (DBusConnection *con, DBusMessage *message, NmVpncData *data)
{
	char *error_item;

	g_return_if_fail (data != NULL);
	g_return_if_fail (con != NULL);
	g_return_if_fail (message != NULL);

	/* Only accept the config info if we're in STARTING state */
	if (data->state != NM_VPN_STATE_STARTING)
		return;

	if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &error_item, DBUS_TYPE_INVALID))
	{
		nm_warning ("vpnc helper did not receive adequate configuration information from vpnc.  It is missing '%s'.", error_item);
		/* Signal NetworkManager */
	}

	nm_vpnc_cancel_helper_timer (data);
	nm_vpnc_dbus_handle_stop_vpn (data);
}


/*
 *  Prints config returned from vpnc-helper
 */
static void print_vpn_config (guint32 ip4_vpn_gateway,
						const char *tundev,
						guint32 ip4_internal_address,
						gint32 ip4_internal_netmask,
						guint32 *ip4_internal_dns,
						guint32 ip4_internal_dns_len,
						guint32 *ip4_internal_nbns,
						guint32 ip4_internal_nbns_len,
						const char *cisco_def_domain,
						const char *cisco_banner)
{
	struct in_addr	temp_addr;
	guint32 		i;

	temp_addr.s_addr = ip4_vpn_gateway;
	nm_info ("VPN Gateway: %s", inet_ntoa (temp_addr));
	nm_info ("Tunnel Device: %s", tundev);
	temp_addr.s_addr = ip4_internal_address;
	nm_info ("Internal IP4 Address: %s", inet_ntoa (temp_addr));
	temp_addr.s_addr = ip4_internal_netmask;
	nm_info ("Internal IP4 Netmask: %s", inet_ntoa (temp_addr));

	for (i = 0; i < ip4_internal_dns_len; i++)
	{
		if (ip4_internal_dns[i] != 0)
		{
			temp_addr.s_addr = ip4_internal_dns[i];
			nm_info ("Internal IP4 DNS: %s", inet_ntoa (temp_addr));
		}
	}

	for (i = 0; i < ip4_internal_nbns_len; i++)
	{
		if (ip4_internal_nbns[i] != 0)
		{
			temp_addr.s_addr = ip4_internal_nbns[i];
			nm_info ("Internal IP4 NBNS: %s", inet_ntoa (temp_addr));
		}
	}

	nm_info ("Cisco Default Domain: '%s'", cisco_def_domain);
	nm_info ("Cisco Banner:");
	nm_info ("-----------------------------------------");
	nm_info ("%s", cisco_banner);
	nm_info ("-----------------------------------------");
}

/*
 * nm_vpnc_dbus_process_helper_ip4_config
 *
 * Signal the bus 
 *
 */
static void nm_vpnc_dbus_process_helper_ip4_config (DBusConnection *con, DBusMessage *message, NmVpncData *data)
{
	guint32		ip4_vpn_gateway;
	char *		tundev;
	guint32		ip4_internal_address;
	guint32		ip4_internal_netmask;
	guint32 *		ip4_internal_dns;
	guint32		ip4_internal_dns_len;
	guint32 *		ip4_internal_nbns;
	guint32		ip4_internal_nbns_len;
	char *		cisco_def_domain;
	char *		cisco_banner;
	gboolean		success = FALSE;

	g_return_if_fail (data != NULL);
	g_return_if_fail (con != NULL);
	g_return_if_fail (message != NULL);

	/* Only accept the config info if we're in STARTING state */
	if (data->state != NM_VPN_STATE_STARTING)
		return;

	nm_vpnc_cancel_helper_timer (data);

	if (dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ip4_vpn_gateway,
									 DBUS_TYPE_STRING, &tundev,
									 DBUS_TYPE_UINT32, &ip4_internal_address,
									 DBUS_TYPE_UINT32, &ip4_internal_netmask,
									 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &ip4_internal_dns, &ip4_internal_dns_len,
									 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &ip4_internal_nbns, &ip4_internal_nbns_len,
									 DBUS_TYPE_STRING, &cisco_def_domain,
									 DBUS_TYPE_STRING, &cisco_banner, DBUS_TYPE_INVALID))
	{
		DBusMessage	*signal;

#if 0
		print_vpn_config (ip4_vpn_gateway, tundev, ip4_internal_address, ip4_internal_netmask,
						ip4_internal_dns, ip4_internal_dns_len, ip4_internal_nbns, ip4_internal_nbns_len,
						cisco_def_domain, cisco_banner);
#endif

		if (!(signal = dbus_message_new_signal (NM_DBUS_PATH_VPNC, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_SIGNAL_IP4_CONFIG)))
		{
			nm_warning ("Not enough memory for new dbus message!");
			goto out;
		}

		dbus_message_append_args (signal, DBUS_TYPE_UINT32, &ip4_vpn_gateway,
									DBUS_TYPE_STRING, &tundev,
									DBUS_TYPE_UINT32, &ip4_internal_address,
									DBUS_TYPE_UINT32, &ip4_internal_netmask,
									DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &ip4_internal_dns, ip4_internal_dns_len,
									DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &ip4_internal_nbns, ip4_internal_nbns_len,
									DBUS_TYPE_STRING, &cisco_def_domain,
									DBUS_TYPE_STRING, &cisco_banner, DBUS_TYPE_INVALID);
		if (!dbus_connection_send (data->con, signal, NULL))
		{
			nm_warning ("Could not raise the "NM_DBUS_VPN_SIGNAL_IP4_CONFIG" signal!");
			goto out;
		}

		dbus_message_unref (signal);
		success = TRUE;
	}

out:
	if (!success)
	{
		nm_warning ("Received invalid IP4 Config information from helper, terminating vpnc.");
		nm_vpnc_dbus_handle_stop_vpn (data);
	}
}


/*
 * nm_vpnc_dbus_message_handler
 *
 * Handle requests for our services.
 *
 */
static DBusHandlerResult nm_vpnc_dbus_message_handler (DBusConnection *con, DBusMessage *message, void *user_data)
{
	NmVpncData		*data = (NmVpncData *)user_data;
	const char		*method;
	const char		*path;
	DBusMessage		*reply = NULL;
	gboolean			 handled = TRUE;

	g_return_val_if_fail (data != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
	g_return_val_if_fail (con != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
	g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);

	method = dbus_message_get_member (message);
	path = dbus_message_get_path (message);

	/* nm_info ("nm_vpnc_dbus_message_handler() got method '%s' for path '%s'.", method, path); */

	/* If we aren't ready to accept dbus messages, don't */
	if ((data->state == NM_VPN_STATE_INIT) || (data->state == NM_VPN_STATE_SHUTDOWN))
	{
		nm_warning ("Received dbus messages but couldn't handle them due to INIT or SHUTDOWN states.");
		reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPNC, NM_DBUS_VPN_WRONG_STATE,
						"Could not process the request due to current state of STATE_INIT or STATE_SHUTDOWN.");
		goto reply;
	}

	if (strcmp ("startConnection", method) == 0)
		reply = nm_vpnc_dbus_start_vpn (con, message, data);
	else if (strcmp ("stopConnection", method) == 0)
		reply = nm_vpnc_dbus_stop_vpn (con, message, data);
	else if (strcmp ("getState", method) == 0)
		reply = nm_vpnc_dbus_get_state (con, message, data);
	else if (strcmp ("signalConfigError", method) == 0)
		nm_vpnc_dbus_process_helper_config_error (con, message, data);
	else if (strcmp ("signalIP4Config", method) == 0)
		nm_vpnc_dbus_process_helper_ip4_config (con, message, data);
	else
		handled = FALSE;

reply:
	if (reply)
	{
		dbus_connection_send (con, reply, NULL);
		dbus_message_unref (reply);
	}

	return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}


/*
 * nm_vpnc_dbus_filter
 *
 * Handle signals from the bus, like NetworkManager network state
 * signals.
 *
 */
static DBusHandlerResult nm_vpnc_dbus_filter (DBusConnection *con, DBusMessage *message, void *user_data)
{
	NmVpncData	*data = (NmVpncData *)user_data;
	gboolean		handled = FALSE;
	DBusError		error;

	g_return_val_if_fail (data != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
	g_return_val_if_fail (con != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
	g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);

	if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
	{
		char 	*service;
		char		*old_owner;
		char		*new_owner;

		dbus_error_init (&error);
		if (    dbus_message_get_args (message, &error,
									DBUS_TYPE_STRING, &service,
									DBUS_TYPE_STRING, &old_owner,
									DBUS_TYPE_STRING, &new_owner,
									DBUS_TYPE_INVALID))
		{
			gboolean old_owner_good = (old_owner && (strlen (old_owner) > 0));
			gboolean new_owner_good = (new_owner && (strlen (new_owner) > 0));

			if ((!old_owner_good && new_owner_good) && (strcmp (service, NM_DBUS_SERVICE) == 0))	/* Equivalent to old ServiceCreated signal */
			{
			}
			else if ((old_owner_good && !new_owner_good) && (strcmp (service, NM_DBUS_SERVICE) == 0))	/* Equivalent to old ServiceDeleted signal */
			{
				/* If NM goes away, we don't stick around */
				nm_vpnc_dbus_handle_stop_vpn (data);
			}
		}
	}
	else if (dbus_message_is_signal (message, NM_DBUS_INTERFACE, "DeviceNoLongerActive"))
	{
		/* If the active device goes down our VPN is certainly not going to work. */
		nm_vpnc_dbus_handle_stop_vpn (data);
	}

	return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}


/*
 * nm_vpnc_dbus_init
 *
 * Grab our connection to the system bus, return NULL if anything goes wrong.
 *
 */
DBusConnection *nm_vpnc_dbus_init (NmVpncData *data)
{
	DBusConnection			*connection = NULL;
	DBusError				 error;
	DBusObjectPathVTable	 vtable = { NULL, &nm_vpnc_dbus_message_handler, NULL, NULL, NULL, NULL };

	g_return_val_if_fail (data != NULL, NULL);

	dbus_error_init (&error);
	if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error)))
	{
		nm_warning ("Error connecting to system bus: '%s'", error.message);
		goto out;
	}

	dbus_connection_setup_with_g_main (connection, NULL);

	dbus_error_init (&error);
	dbus_bus_request_name (connection, NM_DBUS_SERVICE_VPNC, 0, &error);
	if (dbus_error_is_set (&error))
	{
		nm_warning ("Could not acquire the dbus service.  dbus_bus_request_name() says: '%s'", error.message);
		goto out;
	}

	if (!dbus_connection_register_object_path (connection, NM_DBUS_PATH_VPNC, &vtable, data))
	{
		nm_warning ("Could not register a dbus handler for nm-vpnc-service.  Not enough memory?");
		return NULL;
	}

	if (!dbus_connection_add_filter (connection, nm_vpnc_dbus_filter, data, NULL))
		return NULL;

	dbus_error_init (&error);
	dbus_bus_add_match (connection,
				"type='signal',"
				"interface='" NM_DBUS_INTERFACE "',"
				"sender='" NM_DBUS_SERVICE "',"
				"path='" NM_DBUS_PATH "'",
				&error);
	if (dbus_error_is_set (&error))
		goto out;

	dbus_bus_add_match (connection,
				"type='signal',"
				"interface='" DBUS_INTERFACE_DBUS "',"
				"sender='" DBUS_SERVICE_DBUS "'",
				&error);
	if (dbus_error_is_set (&error))
		goto out;

out:
	if (dbus_error_is_set (&error))
	{
		dbus_error_free (&error);
		connection = NULL;
	}
	return connection;
}

NmVpncData *vpn_data = NULL;

static void sigterm_handler (int signum)
{
	nm_info ("nm-vpnc-service caught SIGINT/SIGTERM");

	g_main_loop_quit (vpn_data->loop);
}


/*
 * main
 *
 */
int main( int argc, char *argv[] )
{
	struct sigaction	action;
	sigset_t			block_mask;

	g_type_init ();
	if (!g_thread_supported ())
		g_thread_init (NULL);

	if (!(vpn_data = g_malloc0 (sizeof (NmVpncData))))
	{
		nm_warning ("Not enough memory to initialize.");
		exit (1);
	}
	vpn_data->state = NM_VPN_STATE_INIT;

	vpn_data->loop = g_main_loop_new (NULL, FALSE);

	system ("/sbin/modprobe tun");

	if (!(vpn_data->con = nm_vpnc_dbus_init (vpn_data)))
		exit (1);

	action.sa_handler = sigterm_handler;
	sigemptyset (&block_mask);
	action.sa_mask = block_mask;
	action.sa_flags = 0;
	sigaction (SIGINT, &action, NULL);
	sigaction (SIGTERM, &action, NULL);

	nm_vpnc_set_state (vpn_data, NM_VPN_STATE_STOPPED);
	g_main_loop_run (vpn_data->loop);

	nm_vpnc_dbus_handle_stop_vpn (vpn_data);

	g_main_loop_unref (vpn_data->loop);
	g_free (vpn_data);

	exit (0);
}