5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <gweb/gweb.h>
36 #define DEFAULT_URL "http://www.connman.net/online/status.html"
40 static GMainLoop *main_loop;
42 static void web_debug(const char *str, void *data)
44 g_print("%s: %s\n", (const char *) data, str);
47 static void sig_term(int sig)
49 g_main_loop_quit(main_loop);
52 static const char *message_type_to_string(int message_type)
54 switch (message_type) {
56 return "Initial redirect message";
58 return "Proxy notification";
60 return "Authentication notification";
62 return "Logoff notification";
64 return "Response to Authentication Poll";
66 return "Response to Abort Login";
72 static const char *response_code_to_string(int response_code)
74 switch (response_code) {
78 return "Login succeeded";
80 return "Login failed";
82 return "RADIUS server error/timeout";
84 return "RADIUS server not enabled";
86 return "Logoff succeeded";
88 return "Login aborted";
90 return "Proxy detection/repeat operation";
92 return "Authentication pending";
94 return "Access gateway internal error";
102 const char *current_element;
106 char *abort_login_url;
108 char *access_procedure;
109 char *access_location;
113 static inline void wispr_msg_init(struct wispr_msg *msg)
115 msg->has_error = FALSE;
116 msg->current_element = NULL;
118 msg->message_type = -1;
119 msg->response_code = -1;
121 g_free(msg->login_url);
122 msg->login_url = NULL;
124 g_free(msg->abort_login_url);
125 msg->abort_login_url = NULL;
127 g_free(msg->logoff_url);
128 msg->logoff_url = NULL;
130 g_free(msg->access_procedure);
131 msg->access_procedure = NULL;
133 g_free(msg->access_location);
134 msg->access_location = NULL;
136 g_free(msg->location_name);
137 msg->location_name = NULL;
140 struct wispr_session {
144 struct wispr_msg msg;
151 static gboolean execute_login(gpointer user_data);
157 WISPR_ELEMENT_ACCESS_PROCEDURE,
158 WISPR_ELEMENT_ACCESS_LOCATION,
159 WISPR_ELEMENT_LOCATION_NAME,
160 WISPR_ELEMENT_LOGIN_URL,
161 WISPR_ELEMENT_ABORT_LOGIN_URL,
162 WISPR_ELEMENT_MESSAGE_TYPE,
163 WISPR_ELEMENT_RESPONSE_CODE,
164 WISPR_ELEMENT_NEXT_URL,
166 WISPR_ELEMENT_REPLY_MESSAGE,
167 WISPR_ELEMENT_LOGIN_RESULTS_URL,
168 WISPR_ELEMENT_LOGOFF_URL,
170 } wispr_element_map[] = {
171 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
172 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
173 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
174 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
175 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
176 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
177 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
178 { "NextURL", WISPR_ELEMENT_NEXT_URL },
179 { "Delay", WISPR_ELEMENT_DELAY },
180 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
181 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
182 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
183 { NULL, WISPR_ELEMENT_NONE },
186 static void start_element_handler(GMarkupParseContext *context,
187 const gchar *element_name,
188 const gchar **attribute_names,
189 const gchar **attribute_values,
190 gpointer user_data, GError **error)
192 struct wispr_msg *msg = user_data;
194 msg->current_element = element_name;
197 static void end_element_handler(GMarkupParseContext *context,
198 const gchar *element_name,
199 gpointer user_data, GError **error)
201 struct wispr_msg *msg = user_data;
203 msg->current_element = NULL;
206 static void text_handler(GMarkupParseContext *context,
207 const gchar *text, gsize text_len,
208 gpointer user_data, GError **error)
210 struct wispr_msg *msg = user_data;
213 if (msg->current_element == NULL)
216 for (i = 0; wispr_element_map[i].str; i++) {
217 if (g_str_equal(wispr_element_map[i].str,
218 msg->current_element) == FALSE)
221 switch (wispr_element_map[i].element) {
222 case WISPR_ELEMENT_NONE:
223 case WISPR_ELEMENT_ACCESS_PROCEDURE:
224 g_free(msg->access_procedure);
225 msg->access_procedure = g_strdup(text);
227 case WISPR_ELEMENT_ACCESS_LOCATION:
228 g_free(msg->access_location);
229 msg->access_location = g_strdup(text);
231 case WISPR_ELEMENT_LOCATION_NAME:
232 g_free(msg->location_name);
233 msg->location_name = g_strdup(text);
235 case WISPR_ELEMENT_LOGIN_URL:
236 g_free(msg->login_url);
237 msg->login_url = g_strdup(text);
239 case WISPR_ELEMENT_ABORT_LOGIN_URL:
240 g_free(msg->abort_login_url);
241 msg->abort_login_url = g_strdup(text);
243 case WISPR_ELEMENT_MESSAGE_TYPE:
244 msg->message_type = atoi(text);
246 case WISPR_ELEMENT_RESPONSE_CODE:
247 msg->response_code = atoi(text);
249 case WISPR_ELEMENT_NEXT_URL:
250 case WISPR_ELEMENT_DELAY:
251 case WISPR_ELEMENT_REPLY_MESSAGE:
252 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
254 case WISPR_ELEMENT_LOGOFF_URL:
255 g_free(msg->logoff_url);
256 msg->logoff_url = g_strdup(text);
262 static void error_handler(GMarkupParseContext *context,
263 GError *error, gpointer user_data)
265 struct wispr_msg *msg = user_data;
267 msg->has_error = TRUE;
270 static const GMarkupParser wispr_parser = {
271 start_element_handler,
278 static void parser_callback(const char *str, gpointer user_data)
280 struct wispr_session *wispr = user_data;
281 GMarkupParseContext *context;
284 //printf("%s\n", str);
286 context = g_markup_parse_context_new(&wispr_parser,
287 G_MARKUP_TREAT_CDATA_AS_TEXT, &wispr->msg, NULL);
289 result = g_markup_parse_context_parse(context, str, strlen(str), NULL);
291 result = g_markup_parse_context_end_parse(context, NULL);
293 g_markup_parse_context_free(context);
296 typedef void (*user_input_cb)(const char *value, gpointer user_data);
298 struct user_input_data {
304 struct termios saved_termios;
307 static void user_callback(struct user_input_data *data)
312 if (data->hidden == TRUE)
313 len = write(data->fd, "\n", 1);
315 tcsetattr(data->fd, TCSADRAIN, &data->saved_termios);
319 value = g_string_free(data->str, FALSE);
322 data->cb(value, data->user_data);
329 static gboolean keyboard_input(GIOChannel *channel, GIOCondition condition,
332 struct user_input_data *data = user_data;
336 len = read(data->fd, buf, 1);
341 if (buf[0] == '\n') {
346 g_string_append_c(data->str, buf[0]);
348 if (data->hidden == TRUE)
349 len = write(data->fd, "*", 1);
354 static gboolean user_input(const char *label, gboolean hidden,
355 user_input_cb func, gpointer user_data)
357 struct user_input_data *data;
358 struct termios new_termios;
363 data = g_try_new0(struct user_input_data, 1);
367 data->str = g_string_sized_new(32);
369 data->user_data = user_data;
370 data->hidden = hidden;
372 data->fd = open("/dev/tty", O_RDWR | O_NOCTTY);
376 if (tcgetattr(data->fd, &data->saved_termios) < 0) {
381 new_termios = data->saved_termios;
382 if (data->hidden == TRUE)
383 new_termios.c_lflag &= ~(ICANON|ECHO);
385 new_termios.c_lflag &= ~ICANON;
386 new_termios.c_cc[VMIN] = 1;
387 new_termios.c_cc[VTIME] = 0;
389 tcsetattr(data->fd, TCSADRAIN, &new_termios);
391 channel = g_io_channel_unix_new(data->fd);
392 g_io_channel_set_encoding(channel, NULL, NULL);
393 g_io_channel_set_buffered(channel, FALSE);
394 watch = g_io_add_watch(channel, G_IO_IN, keyboard_input, data);
395 g_io_channel_unref(channel);
400 len = write(data->fd, label, strlen(label));
401 len = write(data->fd, ": ", 2);
406 g_string_free(data->str, TRUE);
412 static void password_callback(const char *value, gpointer user_data)
414 struct wispr_session *wispr = user_data;
416 g_free(wispr->password);
417 wispr->password = g_strdup(value);
421 execute_login(wispr);
424 static void username_callback(const char *value, gpointer user_data)
426 struct wispr_session *wispr = user_data;
428 g_free(wispr->username);
429 wispr->username = g_strdup(value);
431 if (wispr->password == NULL) {
432 user_input("Password", TRUE, password_callback, wispr);
438 execute_login(wispr);
441 static gboolean wispr_input(const guint8 **data, gsize *length,
444 struct wispr_session *wispr = user_data;
448 buf = g_string_sized_new(100);
450 g_string_append(buf, "button=Login&UserName=");
451 g_string_append_uri_escaped(buf, wispr->username, NULL, FALSE);
452 g_string_append(buf, "&Password=");
453 g_string_append_uri_escaped(buf, wispr->password, NULL, FALSE);
454 g_string_append(buf, "&FNAME=0&OriginatingServer=");
455 g_string_append_uri_escaped(buf, wispr->originurl, NULL, FALSE);
459 g_free(wispr->formdata);
460 wispr->formdata = g_string_free(buf, FALSE);
462 *data = (guint8 *) wispr->formdata;
468 static gboolean wispr_result(GWebResult *result, gpointer user_data)
470 struct wispr_session *wispr = user_data;
476 g_web_result_get_chunk(result, &chunk, &length);
479 //printf("%s\n", (char *) chunk);
480 g_web_parser_feed_data(wispr->parser, chunk, length);
484 g_web_parser_end_data(wispr->parser);
486 status = g_web_result_get_status(result);
488 g_print("status: %03u\n", status);
490 elapsed = g_timer_elapsed(timer, NULL);
492 g_print("elapse: %f seconds\n", elapsed);
494 if (wispr->msg.message_type < 0) {
495 const char *redirect;
500 if (g_web_result_get_header(result, "Location",
504 printf("Redirect URL: %s\n", redirect);
507 wispr->request = g_web_request_get(wispr->web, redirect,
508 wispr_result, wispr);
513 printf("Message type: %s (%d)\n",
514 message_type_to_string(wispr->msg.message_type),
515 wispr->msg.message_type);
516 printf("Response code: %s (%d)\n",
517 response_code_to_string(wispr->msg.response_code),
518 wispr->msg.response_code);
519 if (wispr->msg.access_procedure != NULL)
520 printf("Access procedure: %s\n", wispr->msg.access_procedure);
521 if (wispr->msg.access_location != NULL)
522 printf("Access location: %s\n", wispr->msg.access_location);
523 if (wispr->msg.location_name != NULL)
524 printf("Location name: %s\n", wispr->msg.location_name);
525 if (wispr->msg.login_url != NULL)
526 printf("Login URL: %s\n", wispr->msg.login_url);
527 if (wispr->msg.abort_login_url != NULL)
528 printf("Abort login URL: %s\n", wispr->msg.abort_login_url);
529 if (wispr->msg.logoff_url != NULL)
530 printf("Logoff URL: %s\n", wispr->msg.logoff_url);
533 if (status != 200 && status != 302 && status != 404)
536 if (wispr->msg.message_type == 100) {
537 if (wispr->username == NULL) {
538 user_input("Username", FALSE, username_callback, wispr);
542 if (wispr->password == NULL) {
543 user_input("Password", TRUE, password_callback, wispr);
547 g_idle_add(execute_login, wispr);
549 } else if (wispr->msg.message_type == 120 ||
550 wispr->msg.message_type == 140) {
551 int code = wispr->msg.response_code;
552 printf("Login process: %s\n",
553 code == 50 ? "SUCCESS" : "FAILURE");
557 const char *redirect;
559 if (g_web_result_get_header(result, "Location",
564 printf("Redirect URL: %s\n", redirect);
567 wispr->request = g_web_request_get(wispr->web, redirect,
568 wispr_result, wispr);
574 g_main_loop_quit(main_loop);
579 static gboolean execute_login(gpointer user_data)
581 struct wispr_session *wispr = user_data;
583 wispr->request = g_web_request_post(wispr->web, wispr->msg.login_url,
584 "application/x-www-form-urlencoded",
585 wispr_input, wispr_result, wispr);
587 wispr_msg_init(&wispr->msg);
592 static gboolean option_debug = FALSE;
593 static gchar *option_nameserver = NULL;
594 static gchar *option_username = NULL;
595 static gchar *option_password = NULL;
596 static gchar *option_url = NULL;
598 static GOptionEntry options[] = {
599 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
600 "Enable debug output" },
601 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
602 "Specify nameserver", "ADDRESS" },
603 { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
604 "Specify username", "USERNAME" },
605 { "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
606 "Specify password", "PASSWORD" },
607 { "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
608 "Specify arbitrary request", "URL" },
612 int main(int argc, char *argv[])
614 GOptionContext *context;
615 GError *error = NULL;
617 struct wispr_session wispr;
620 context = g_option_context_new(NULL);
621 g_option_context_add_main_entries(context, options, NULL);
623 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
625 g_printerr("%s\n", error->message);
628 g_printerr("An unknown error occurred\n");
632 g_option_context_free(context);
634 memset(&wispr, 0, sizeof(wispr));
635 wispr_msg_init(&wispr.msg);
637 wispr.web = g_web_new(index);
638 if (wispr.web == NULL) {
639 fprintf(stderr, "Failed to create web service\n");
643 if (option_debug == TRUE)
644 g_web_set_debug(wispr.web, web_debug, "WEB");
646 main_loop = g_main_loop_new(NULL, FALSE);
648 if (option_nameserver != NULL) {
649 g_web_add_nameserver(wispr.web, option_nameserver);
650 g_free(option_nameserver);
653 g_web_set_accept(wispr.web, NULL);
654 g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
655 g_web_set_close_connection(wispr.web, TRUE);
657 if (option_url == NULL)
658 option_url = g_strdup(DEFAULT_URL);
660 wispr.username = option_username;
661 wispr.password = option_password;
662 wispr.originurl = option_url;
664 timer = g_timer_new();
666 wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
667 "WISPAccessGatewayParam>",
668 parser_callback, &wispr);
670 wispr.request = g_web_request_get(wispr.web, option_url,
671 wispr_result, &wispr);
673 if (wispr.request == 0) {
674 fprintf(stderr, "Failed to start request\n");
678 memset(&sa, 0, sizeof(sa));
679 sa.sa_handler = sig_term;
680 sigaction(SIGINT, &sa, NULL);
681 sigaction(SIGTERM, &sa, NULL);
683 g_main_loop_run(main_loop);
685 g_timer_destroy(timer);
687 if (wispr.request > 0)
688 g_web_cancel_request(wispr.web, wispr.request);
690 g_web_parser_unref(wispr.parser);
691 g_web_unref(wispr.web);
693 g_main_loop_unref(main_loop);
695 g_free(wispr.username);
696 g_free(wispr.password);
697 g_free(wispr.originurl);