summaryrefslogtreecommitdiff
path: root/tools/wispr.c
blob: 5e7a72035525c003db7b150973271db206f4a1ee (plain)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
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
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <termios.h>

#include <gweb/gweb.h>

#define DEFAULT_URL  "http://www.connman.net/online/status.html"

static GTimer *timer;

static GMainLoop *main_loop;

static void web_debug(const char *str, void *data)
{
	g_print("%s: %s\n", (const char *) data, str);
}

static void sig_term(int sig)
{
	g_main_loop_quit(main_loop);
}

static const char *message_type_to_string(int message_type)
{
	switch (message_type) {
	case 100:
		return "Initial redirect message";
	case 110:
		return "Proxy notification";
	case 120:
		return "Authentication notification";
	case 130:
		return "Logoff notification";
	case 140:
		return "Response to Authentication Poll";
	case 150:
		return "Response to Abort Login";
	}

	return NULL;
}

static const char *response_code_to_string(int response_code)
{
	switch (response_code) {
	case 0:
		return "No error";
	case 50:
		return "Login succeeded";
	case 100:
		return "Login failed";
	case 102:
		return "RADIUS server error/timeout";
	case 105:
		return "RADIUS server not enabled";
	case 150:
		return "Logoff succeeded";
	case 151:
		return "Login aborted";
	case 200:
		return "Proxy detection/repeat operation";
	case 201:
		return "Authentication pending";
	case 255:
		return "Access gateway internal error";
	}

	return NULL;
}

struct wispr_msg {
	gboolean has_error;
	const char *current_element;
	int message_type;
	int response_code;
	char *login_url;
	char *logoff_url;
	char *access_procedure;
	char *access_location;
	char *location_name;
};

static inline void wispr_msg_init(struct wispr_msg *msg)
{
	msg->has_error = FALSE;
	msg->current_element = NULL;

	msg->message_type = -1;
	msg->response_code = -1;

	g_free(msg->login_url);
	msg->login_url = NULL;

	g_free(msg->logoff_url);
	msg->logoff_url = NULL;

	g_free(msg->access_procedure);
	msg->access_procedure = NULL;

	g_free(msg->access_location);
	msg->access_location = NULL;

	g_free(msg->location_name);
	msg->location_name = NULL;
}

struct wispr_session {
	GWeb *web;
	GWebParser *parser;
	guint request;
	struct wispr_msg msg;
	char *username;
	char *password;
	char *originurl;
	char *formdata;
};

static void execute_login(struct wispr_session *wispr);

static struct {
	const char *str;
	enum {
		WISPR_ELEMENT_NONE,
		WISPR_ELEMENT_ACCESS_PROCEDURE,
		WISPR_ELEMENT_ACCESS_LOCATION,
		WISPR_ELEMENT_LOCATION_NAME,
		WISPR_ELEMENT_LOGIN_URL,
		WISPR_ELEMENT_ABORT_LOGIN_URL,
		WISPR_ELEMENT_MESSAGE_TYPE,
		WISPR_ELEMENT_RESPONSE_CODE,
		WISPR_ELEMENT_NEXT_URL,
		WISPR_ELEMENT_DELAY,
		WISPR_ELEMENT_REPLY_MESSAGE,
		WISPR_ELEMENT_LOGIN_RESULTS_URL,
		WISPR_ELEMENT_LOGOFF_URL,
	} element;
} wispr_element_map[] = {
	{ "AccessProcedure",	WISPR_ELEMENT_ACCESS_PROCEDURE	},
	{ "AccessLocation",	WISPR_ELEMENT_ACCESS_LOCATION	},
	{ "LocationName",	WISPR_ELEMENT_LOCATION_NAME	},
	{ "LoginURL",		WISPR_ELEMENT_LOGIN_URL		},
	{ "AbortLoginURL",	WISPR_ELEMENT_ABORT_LOGIN_URL	},
	{ "MessageType",	WISPR_ELEMENT_MESSAGE_TYPE	},
	{ "ResponseCode",	WISPR_ELEMENT_RESPONSE_CODE	},
	{ "NextURL",		WISPR_ELEMENT_NEXT_URL		},
	{ "Delay",		WISPR_ELEMENT_DELAY		},
	{ "ReplyMessage",	WISPR_ELEMENT_REPLY_MESSAGE	},
	{ "LoginResultsURL",	WISPR_ELEMENT_LOGIN_RESULTS_URL	},
	{ "LogoffURL",		WISPR_ELEMENT_LOGOFF_URL	},
	{ NULL,			WISPR_ELEMENT_NONE		},
};

static void start_element_handler(GMarkupParseContext *context,
					const gchar *element_name,
					const gchar **attribute_names,
					const gchar **attribute_values,
					gpointer user_data, GError **error)
{
	struct wispr_msg *msg = user_data;

	msg->current_element = element_name;
}

static void end_element_handler(GMarkupParseContext *context,
					const gchar *element_name,
					gpointer user_data, GError **error)
{
	struct wispr_msg *msg = user_data;

	msg->current_element = NULL;
}

static void text_handler(GMarkupParseContext *context,
					const gchar *text, gsize text_len,
					gpointer user_data, GError **error)
{
	struct wispr_msg *msg = user_data;
	int i;

	if (msg->current_element == NULL)
		return;

	for (i = 0; wispr_element_map[i].str; i++) {
		if (g_str_equal(wispr_element_map[i].str,
					msg->current_element) == FALSE)
			continue;

		switch (wispr_element_map[i].element) {
		case WISPR_ELEMENT_NONE:
		case WISPR_ELEMENT_ACCESS_PROCEDURE:
			g_free(msg->access_procedure);
			msg->access_procedure = g_strdup(text);
		case WISPR_ELEMENT_ACCESS_LOCATION:
			g_free(msg->access_location);
			msg->access_location = g_strdup(text);
		case WISPR_ELEMENT_LOCATION_NAME:
			g_free(msg->location_name);
			msg->location_name = g_strdup(text);
			break;
		case WISPR_ELEMENT_LOGIN_URL:
			g_free(msg->login_url);
			msg->login_url = g_strdup(text);
			break;
		case WISPR_ELEMENT_ABORT_LOGIN_URL:
			break;
		case WISPR_ELEMENT_MESSAGE_TYPE:
			msg->message_type = atoi(text);
			break;
		case WISPR_ELEMENT_RESPONSE_CODE:
			msg->response_code = atoi(text);
			break;
		case WISPR_ELEMENT_NEXT_URL:
		case WISPR_ELEMENT_DELAY:
		case WISPR_ELEMENT_REPLY_MESSAGE:
		case WISPR_ELEMENT_LOGIN_RESULTS_URL:
			break;
		case WISPR_ELEMENT_LOGOFF_URL:
			g_free(msg->logoff_url);
			msg->logoff_url = g_strdup(text);
			break;
		}
	}
}

static void error_handler(GMarkupParseContext *context,
					GError *error, gpointer user_data)
{
	struct wispr_msg *msg = user_data;

	msg->has_error = TRUE;
}

static const GMarkupParser wispr_parser = {
	start_element_handler,
	end_element_handler,
	text_handler,
	NULL,
	error_handler,
};

static void parser_callback(const char *str, gpointer user_data)
{
	struct wispr_session *wispr = user_data;
	GMarkupParseContext *context;
	gboolean result;

	//printf("%s\n", str);

	context = g_markup_parse_context_new(&wispr_parser,
			G_MARKUP_TREAT_CDATA_AS_TEXT, &wispr->msg, NULL);

	result = g_markup_parse_context_parse(context, str, strlen(str), NULL);
	if (result == TRUE)
		result = g_markup_parse_context_end_parse(context, NULL);

	g_markup_parse_context_free(context);
}

typedef void (*user_input_cb)(const char *value, gpointer user_data);

struct user_input_data {
	GString *str;
	user_input_cb cb;
	gpointer user_data;
	gboolean hidden;
	int fd;
	struct termios saved_termios;
};

static void user_callback(struct user_input_data *data)
{
	char *value;
	int len;

	if (data->hidden == TRUE)
		len = write(data->fd, "\n", 1);

	tcsetattr(data->fd, TCSADRAIN, &data->saved_termios);

	close(data->fd);

	value = g_string_free(data->str, FALSE);

	if (data->cb)
		data->cb(value, data->user_data);

	g_free(value);

	g_free(data);
}

static gboolean keyboard_input(GIOChannel *channel, GIOCondition condition,
							gpointer user_data)
{
	struct user_input_data *data = user_data;
	char buf[1];
	int len;

	len = read(data->fd, buf, 1);

	if (len != 1)
		return TRUE;

	if (buf[0] == '\n') {
		user_callback(data);
		return FALSE;
	}

	g_string_append_c(data->str, buf[0]);

	if (data->hidden == TRUE)
		len = write(data->fd, "*", 1);

	return TRUE;
}

static gboolean user_input(const char *label, gboolean hidden,
				user_input_cb func, gpointer user_data)
{
	struct user_input_data *data;
	struct termios new_termios;
	GIOChannel *channel;
	guint watch;
	int len;

	data = g_try_new0(struct user_input_data, 1);
	if (data == NULL)
		return FALSE;

	data->str = g_string_sized_new(32);
	data->cb = func;
	data->user_data = user_data;
	data->hidden = hidden;

	data->fd = open("/dev/tty", O_RDWR | O_NOCTTY);
	if (data->fd < 0)
		goto error;

	if (tcgetattr(data->fd, &data->saved_termios) < 0) {
		close(data->fd);
		goto error;
	}

	new_termios = data->saved_termios;
	if (data->hidden == TRUE)
		new_termios.c_lflag &= ~(ICANON|ECHO);
	else
		new_termios.c_lflag &= ~ICANON;
	new_termios.c_cc[VMIN] = 1;
	new_termios.c_cc[VTIME] = 0;

	tcsetattr(data->fd, TCSADRAIN, &new_termios);

	channel = g_io_channel_unix_new(data->fd);
	g_io_channel_set_encoding(channel, NULL, NULL);
	g_io_channel_set_buffered(channel, FALSE);
	watch = g_io_add_watch(channel, G_IO_IN, keyboard_input, data);
	g_io_channel_unref(channel);

	if (watch == 0)
		goto error;

	len = write(data->fd, label, strlen(label));
	len = write(data->fd, ": ", 2);

	return TRUE;

error:
	g_string_free(data->str, TRUE);
	g_free(data);

	return FALSE;
}

static void password_callback(const char *value, gpointer user_data)
{
	struct wispr_session *wispr = user_data;

	g_free(wispr->password);
	wispr->password = g_strdup(value);

	printf("\n");

	execute_login(wispr);
}

static void username_callback(const char *value, gpointer user_data)
{
	struct wispr_session *wispr = user_data;

	g_free(wispr->username);
	wispr->username = g_strdup(value);

	if (wispr->password == NULL) {
		user_input("Password", TRUE, password_callback, wispr);
		return;
	}

	printf("\n");

	execute_login(wispr);
}

static gboolean wispr_input(const guint8 **data, gsize *length,
						gpointer user_data)
{
	struct wispr_session *wispr = user_data;

	g_free(wispr->formdata);
	wispr->formdata = g_strdup_printf("button=Login&UserName=%s&"
			"Password=%s&FNAME=0&OriginatingServer=%s",
			wispr->username, wispr->password, wispr->originurl);

	*data = (guint8 *) wispr->formdata;
	*length = strlen(wispr->formdata);

	return FALSE;
}

static gboolean wispr_result(GWebResult *result, gpointer user_data)
{
	struct wispr_session *wispr = user_data;
	const guint8 *chunk;
	gsize length;
	guint16 status;
	gdouble elapsed;

	g_web_result_get_chunk(result, &chunk, &length);

	if (length > 0) {
		//printf("%s\n", (char *) chunk);
		g_web_parser_feed_data(wispr->parser, chunk, length);
		return TRUE;
	}

	g_web_parser_end_data(wispr->parser);

	status = g_web_result_get_status(result);

	g_print("status: %03u\n", status);

	elapsed = g_timer_elapsed(timer, NULL);

	g_print("elapse: %f seconds\n", elapsed);

	if (wispr->msg.message_type < 0)
		goto done;

	printf("Message type: %s (%d)\n",
			message_type_to_string(wispr->msg.message_type),
						wispr->msg.message_type);
	printf("Response code: %s (%d)\n",
			response_code_to_string(wispr->msg.response_code),
						wispr->msg.response_code);
	if (wispr->msg.access_procedure != NULL)
		printf("Access procedure: %s\n", wispr->msg.access_procedure);
	if (wispr->msg.access_location != NULL)
		printf("Access location: %s\n", wispr->msg.access_location);
	if (wispr->msg.location_name != NULL)
		printf("Location name: %s\n", wispr->msg.location_name);
	if (wispr->msg.login_url != NULL)
		printf("Login URL: %s\n", wispr->msg.login_url);
	if (wispr->msg.logoff_url != NULL)
		printf("Logoff URL: %s\n", wispr->msg.logoff_url);
	printf("\n");

	if (status == 302 && wispr->msg.message_type == 100) {
		if (wispr->username == NULL) {
			user_input("Username", FALSE, username_callback, wispr);
			return FALSE;
		}

		if (wispr->password == NULL) {
			user_input("Password", TRUE, password_callback, wispr);
			return FALSE;
		}

		execute_login(wispr);
		return FALSE;
	} else if (status == 200 && wispr->msg.message_type == 120) {
		int code = wispr->msg.response_code;
		printf("Login process: %s\n",
					code == 50 ? "SUCCESS" : "FAILURE");
	}

done:
	g_main_loop_quit(main_loop);

	return FALSE;
}

static void execute_login(struct wispr_session *wispr)
{
	wispr->request = g_web_request_post(wispr->web, wispr->msg.login_url,
					"application/x-www-form-urlencoded",
					wispr_input, wispr_result, wispr);

	wispr_msg_init(&wispr->msg);
}

static gboolean option_debug = FALSE;
static gchar *option_nameserver = NULL;
static gchar *option_username = NULL;
static gchar *option_password = NULL;
static gchar *option_url = NULL;

static GOptionEntry options[] = {
	{ "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
					"Enable debug output" },
	{ "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
					"Specify nameserver", "ADDRESS" },
	{ "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
					"Specify username", "USERNAME" },
	{ "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
					"Specify password", "PASSWORD" },
	{ "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
					"Specify arbitrary request", "URL" },
	{ NULL },
};

int main(int argc, char *argv[])
{
	GOptionContext *context;
	GError *error = NULL;
	struct sigaction sa;
	struct wispr_session wispr;
	int index = 0;

	context = g_option_context_new(NULL);
	g_option_context_add_main_entries(context, options, NULL);

	if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
		if (error != NULL) {
			g_printerr("%s\n", error->message);
			g_error_free(error);
		} else
			g_printerr("An unknown error occurred\n");
		return 1;
	}

	g_option_context_free(context);

	memset(&wispr, 0, sizeof(wispr));
	wispr_msg_init(&wispr.msg);

	wispr.web = g_web_new(index);
	if (wispr.web == NULL) {
		fprintf(stderr, "Failed to create web service\n");
		return 1;
	}

	if (option_debug == TRUE)
		g_web_set_debug(wispr.web, web_debug, "WEB");

	main_loop = g_main_loop_new(NULL, FALSE);

	if (option_nameserver != NULL) {
		g_web_add_nameserver(wispr.web, option_nameserver);
		g_free(option_nameserver);
	}

	g_web_set_accept(wispr.web, NULL);
	g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
	g_web_set_close_connection(wispr.web, TRUE);

	if (option_url == NULL)
		option_url = g_strdup(DEFAULT_URL);

	wispr.username = option_username;
	wispr.password = option_password;
	wispr.originurl = option_url;

	timer = g_timer_new();

	wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
						"WISPAccessGatewayParam>",
						parser_callback, &wispr);

	wispr.request = g_web_request_get(wispr.web, option_url,
							wispr_result, &wispr);

	if (wispr.request == 0) {
		fprintf(stderr, "Failed to start request\n");
		return 1;
	}

	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = sig_term;
	sigaction(SIGINT, &sa, NULL);
	sigaction(SIGTERM, &sa, NULL);

	g_main_loop_run(main_loop);

	g_timer_destroy(timer);

	if (wispr.request > 0)
		g_web_cancel_request(wispr.web, wispr.request);

	g_web_parser_unref(wispr.parser);
	g_web_unref(wispr.web);

	g_main_loop_unref(main_loop);

	g_free(wispr.username);
	g_free(wispr.password);
	g_free(wispr.originurl);

	return 0;
}