File: main.c

package info (click to toggle)
gfcc 0.7.4-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,084 kB
  • ctags: 320
  • sloc: sh: 7,159; ansic: 4,082; makefile: 101
file content (298 lines) | stat: -rw-r--r-- 8,172 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <gtk/gtk.h>
#include <sys/types.h>

#include "gfcc.h"

#include "Pics/open.xpm"
#include "Pics/open-system.xpm"
#include "Pics/save.xpm"
#include "Pics/export.xpm"
#include "Pics/export-grey.xpm"
#include "Pics/masq.xpm"
#include "Pics/apply.xpm"
#include "Pics/newchain.xpm"

#include "Pics/open-grey.xpm"
#include "Pics/save-grey.xpm"
#include "Pics/apply-grey.xpm"

#define NORM_KEY "norm"
#define GREY_KEY "grey"

#define WINDOWWIDTH 700
#define WINDOWHEIGHT 570

GtkWidget *notebook;
static void main_menu(GtkWidget *vbox);
static void toolbar_menu(GtkWidget *vbox);
static void exit_tryhelp(gchar *prgname);
static void about_window();

typedef struct _toolbar {
	char *label;
	char **pixmap;
	char **grey_pixmap;
	char *tooltip;
	void (*callback)();
	gpointer data;
	GtkWidget **store_to;
} ToolBar;

typedef struct _activator {
	GtkWidget **widget;
	int (*activator)();
} Activator;

GtkWidget *save_button;
GtkWidget *load_button;
GtkWidget *export_button;
GtkWidget *new_chain_button;
GtkWidget *load_system_button;
GtkWidget *masq_button;
GtkWidget *apply_button;

const ToolBar toolbardef[] = {
{ "Save", save_xpm, save_grey_xpm, "Save rules to file", save_to_file, NULL, &save_button },
{ "Load", open_xpm, open_grey_xpm, "Load rules from a file", load_from_file, NULL, &load_button },
{ " System ", open_system_xpm, open_grey_xpm, "Load rules from system", load_from_system, NULL, &load_system_button },
{ " Export ", export_xpm, export_grey_xpm, "Export this rule to bash shell script", export_to_file, NULL, &export_button },
{ "", NULL, NULL, NULL, NULL, NULL },
{ " New chain ", newchain, open_grey_xpm, "Create New chain", edit_chain_window, NEW, &new_chain_button },
{ "", NULL, NULL, NULL, NULL, NULL },
{ "Masq", masq_xpm, open_grey_xpm, "Masquerading list", masq_list, NULL, &masq_button },
{ "", NULL, NULL, NULL, NULL, NULL },
{ "Apply", apply_xpm, apply_grey_xpm, "Apply to system", save_fw_data, NULL, &apply_button },
{ NULL, NULL, NULL, NULL, NULL, NULL }
};

static GtkItemFactoryEntry menu_items[] = {
  { "/_File",			NULL,	NULL, 0, "<Branch>" },
  { "/File/Load from _File",	"<control>F",	load_from_file, 0, NULL },
  { "/File/Load from System",	NULL,	load_from_system, 0, NULL },
  { "/File/Save",		"<control>S",	save_to_file, 0, NULL },
  { "/File/Save as",		NULL,	save_as_to_file, 0, NULL },
  { "/File/Export",		NULL,	export_to_file, 0, NULL },
  { "/_File/sep1",		NULL,	NULL, 0, "<Separator>" },
  { "/File/_Quit",		"<control>Q",	program_quit, 0, NULL },
  { "/Sys_tem",			NULL,	NULL, 0, "<Branch>" },
  { "/System/New chain",	NULL,	edit_chain_window, 0, NULL },
  { "/System/Clear system rule",NULL,	dialog_clear_system, 0, NULL },
  { "/System/Apply to system",	NULL,	save_fw_data, 0, NULL },
  { "/System/Masquerading list",NULL,	masq_list, 0, NULL },
  { "/H_ost",			NULL,	NULL, 0, "<Branch>" },
  { "/Host/Edit host&network",	NULL,	call_select_host_window, 0, NULL },
  { "/Host/Reload host&network",NULL,	get_host_name, 0, NULL },
  { "/_Help",			NULL,	NULL, 0, "<LastBranch>" },
  { "/_Help/About",		NULL,	about_window, 0, NULL },
};

const gchar *help[] = {
	"",
	"Usage : gfcc [-sfc] [file]",
	"",
	"",
	" --silent  -s file : silent mode, read data from file and set",
	" --file    -f file : read data from file",
	" --clear   -c      : clear the system rules and chains",
	" --help    -h      : this message",
	"       No arg      : read from system",
	"",
	NULL,
};

GtkWidget *window;

int main (int argc, char *argv[])
{
	GtkWidget *vbox;
	GtkWidget *table;
	gchar *filename = NULL, procipchains[64], full_filename[128];
	struct option opts[] = {
		{ "silent", 1, 0, 's' },
		{ "file", 1, 0, 'f' },
		{ "clear", 1, 0, 'c' },
		{ "help", 0, 0, 'h' },
		{ 0 }
	};
	int ch;

	if(geteuid()) {
		fprintf(stderr, "Run as root !!!.\n");
		exit(1);
	}
	sprintf(procipchains, "/proc/net/%s", IP_FW_PROC_CHAIN_NAMES);
	if(access(procipchains, R_OK) < 0) {
		fprintf(stderr, "Your kernel doesn't support ipchains"
		                " or proc file system\n");
		exit(2);
	}

	while((ch = getopt_long(argc, argv, "s:f:ch", opts, NULL)) != -1) {
		switch(ch) {
		case 's':
			if(filename)
				exit_tryhelp(argv[0]);
			filename = optarg;
			if(!filename)
				filename = GFCC_HOME"/system.rule";
			get_fw_data(filename, 1);
			exit(0);
		case 'f':
			if(filename)
				exit_tryhelp(argv[0]);
			filename = optarg;
			break;
		case 'c':
			if(!clear_system())
				printf("%s\n", ipfwc_strerror(errno));
			exit(0);
		case 'h':
		default :
			exit_tryhelp(argv[0]);
		}
	}
	if(!filename && argc > 1)
		filename = argv[1];

#if defined(__GLIBC__) && __GLIBC__ == 2
	if(getenv("LANG"))
		gtk_set_locale();
#endif
	gtk_rc_add_default_file(GFCC_HOME"/gtkrc");
	gtk_init (&argc, &argv);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_usize( GTK_WIDGET (window), WINDOWWIDTH, WINDOWHEIGHT);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
	gtk_signal_connect(GTK_OBJECT (window), "destroy",
		(GtkSignalFunc) program_quit, NULL);
	gtk_signal_connect(GTK_OBJECT (window), "delete_event",
		(GtkSignalFunc) program_quit, NULL);
	
	vbox = gtk_vbox_new(FALSE, 1);
	gtk_container_add(GTK_CONTAINER(window), vbox);

	main_menu(vbox);
	toolbar_menu(vbox); 
	
	table = gtk_table_new(1,1,FALSE);
	gtk_container_add(GTK_CONTAINER(vbox), table);

	gtk_container_set_border_width(GTK_CONTAINER(table), 10);
	notebook = gtk_notebook_new();
	gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
	gtk_table_attach_defaults(GTK_TABLE(table), notebook, 0,1,0,1);
	gtk_widget_show(notebook);
	
	get_service_name();
	get_proto_name();
	get_host_name();

	if(filename && filename[0] != '/') {
		getcwd(full_filename, sizeof(full_filename));
		strcat(full_filename, "/");
		strcat(full_filename, filename);
		filename = full_filename;
	}
	get_fw_data(filename, 0);
	
	gtk_widget_show(table);
	gtk_widget_show(vbox);
	gtk_widget_show(window);
	
	chdir(GFCC_HOME"/rules");

	gtk_main ();

	return 0;
}

static void main_menu(GtkWidget *vbox)
{
	GtkWidget *menu_bar;
	int nmenu_items = sizeof(menu_items)/sizeof(menu_items[0]);
	GtkItemFactory *item_factory;
	GtkAccelGroup *accel_group;
	
	accel_group = gtk_accel_group_new();
	item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>",
			accel_group);
	gtk_item_factory_create_items(item_factory, nmenu_items,
			menu_items, NULL);
	gtk_accel_group_attach(accel_group, GTK_OBJECT(window));

	menu_bar = gtk_item_factory_get_widget(item_factory, "<main>");

	gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 2);
	gtk_widget_show(menu_bar);
}

static void toolbar_menu(GtkWidget *vbox)
{
	GtkWidget *toolbar;
	int i;
	
	toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,GTK_TOOLBAR_BOTH);
	gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 2);
	gtk_widget_show(toolbar);
	gtk_widget_realize(toolbar);
	
	gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
	for(i=0; toolbardef[i].label; i++) {
		if(!strlen(toolbardef[i].label)) {
			gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
		} else {
			GtkWidget *gtkpix = NULL, *gtkpix_grey = NULL;
			GtkWidget *tb_item;

			if(toolbardef[i].pixmap) {
				gtkpix = new_pixmap(toolbar,
						toolbardef[i].pixmap);
				gtk_widget_show(gtkpix);
				gtkpix_grey = new_pixmap(toolbar,
						toolbardef[i].grey_pixmap);
				gtk_widget_show(gtkpix_grey);
			}
			tb_item = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
					toolbardef[i].label,
					toolbardef[i].tooltip,
					"Private",
					gtkpix,
					toolbardef[i].callback,
					toolbardef[i].data);
			gtk_object_set_data(GTK_OBJECT(tb_item),
					NORM_KEY, gtkpix);
			gtk_object_set_data(GTK_OBJECT(tb_item),
					GREY_KEY, gtkpix_grey);

			*toolbardef[i].store_to = tb_item;
		}
	}
}

static void exit_tryhelp(gchar *prgname)
{
	gint i;
	
	for(i=0; help[i]; i++) {
		fprintf(stderr, "%s\n", help[i]);
	}
	
	exit(1);
}

static void about_window()
{
	gchar buf[128];
	
	sprintf(buf, "%s\n\n"
		     "http://icarus.autostock.co.kr\n"
		     "Koo Kyoseon: icarus@autostock.co.kr", PACKAGE_VERSION);
	dialog_window(buf, NULL);
}