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
36 #include <gweb/gweb.h>
38 #define DEFAULT_URL "http://www.connman.net/online/status.html"
42 static GMainLoop *main_loop;
44 static void web_debug(const char *str, void *data)
46 g_print("%s: %s\n", (const char *) data, str);
49 static void sig_term(int sig)
51 g_main_loop_quit(main_loop);
54 static const char *message_type_to_string(int message_type)
56 switch (message_type) {
58 return "Initial redirect message";
60 return "Proxy notification";
62 return "Authentication notification";
64 return "Logoff notification";
66 return "Response to Authentication Poll";
68 return "Response to Abort Login";
74 static const char *response_code_to_string(int response_code)
76 switch (response_code) {
80 return "Login succeeded";
82 return "Login failed";
84 return "RADIUS server error/timeout";
86 return "RADIUS server not enabled";
88 return "Logoff succeeded";
90 return "Login aborted";
92 return "Proxy detection/repeat operation";
94 return "Authentication pending";
96 return "Access gateway internal error";
104 const char *current_element;
108 char *abort_login_url;
110 char *access_procedure;
111 char *access_location;
115 static inline void wispr_msg_init(struct wispr_msg *msg)
117 msg->has_error = FALSE;
118 msg->current_element = NULL;
120 msg->message_type = -1;
121 msg->response_code = -1;
123 g_free(msg->login_url);
124 msg->login_url = NULL;
126 g_free(msg->abort_login_url);
127 msg->abort_login_url = NULL;
129 g_free(msg->logoff_url);
130 msg->logoff_url = NULL;
132 g_free(msg->access_procedure);
133 msg->access_procedure = NULL;
135 g_free(msg->access_location);
136 msg->access_location = NULL;
138 g_free(msg->location_name);
139 msg->location_name = NULL;
142 struct wispr_session {
146 struct wispr_msg msg;
153 static gboolean execute_login(gpointer user_data);
159 WISPR_ELEMENT_ACCESS_PROCEDURE,
160 WISPR_ELEMENT_ACCESS_LOCATION,
161 WISPR_ELEMENT_LOCATION_NAME,
162 WISPR_ELEMENT_LOGIN_URL,
163 WISPR_ELEMENT_ABORT_LOGIN_URL,
164 WISPR_ELEMENT_MESSAGE_TYPE,
165 WISPR_ELEMENT_RESPONSE_CODE,
166 WISPR_ELEMENT_NEXT_URL,
168 WISPR_ELEMENT_REPLY_MESSAGE,
169 WISPR_ELEMENT_LOGIN_RESULTS_URL,
170 WISPR_ELEMENT_LOGOFF_URL,
172 } wispr_element_map[] = {
173 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
174 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
175 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
176 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
177 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
178 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
179 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
180 { "NextURL", WISPR_ELEMENT_NEXT_URL },
181 { "Delay", WISPR_ELEMENT_DELAY },
182 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
183 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
184 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
185 { NULL, WISPR_ELEMENT_NONE },
188 static void start_element_handler(GMarkupParseContext *context,
189 const gchar *element_name,
190 const gchar **attribute_names,
191 const gchar **attribute_values,
192 gpointer user_data, GError **error)
194 struct wispr_msg *msg = user_data;
196 msg->current_element = element_name;
199 static void end_element_handler(GMarkupParseContext *context,
200 const gchar *element_name,
201 gpointer user_data, GError **error)
203 struct wispr_msg *msg = user_data;
205 msg->current_element = NULL;
208 static void text_handler(GMarkupParseContext *context,
209 const gchar *text, gsize text_len,
210 gpointer user_data, GError **error)
212 struct wispr_msg *msg = user_data;
215 if (msg->current_element == NULL)
218 for (i = 0; wispr_element_map[i].str; i++) {
219 if (g_str_equal(wispr_element_map[i].str,
220 msg->current_element) == FALSE)
223 switch (wispr_element_map[i].element) {
224 case WISPR_ELEMENT_NONE:
225 case WISPR_ELEMENT_ACCESS_PROCEDURE:
226 g_free(msg->access_procedure);
227 msg->access_procedure = g_strdup(text);
229 case WISPR_ELEMENT_ACCESS_LOCATION:
230 g_free(msg->access_location);
231 msg->access_location = g_strdup(text);
233 case WISPR_ELEMENT_LOCATION_NAME:
234 g_free(msg->location_name);
235 msg->location_name = g_strdup(text);
237 case WISPR_ELEMENT_LOGIN_URL:
238 g_free(msg->login_url);
239 msg->login_url = g_strdup(text);
241 case WISPR_ELEMENT_ABORT_LOGIN_URL:
242 g_free(msg->abort_login_url);
243 msg->abort_login_url = g_strdup(text);
245 case WISPR_ELEMENT_MESSAGE_TYPE:
246 msg->message_type = atoi(text);
248 case WISPR_ELEMENT_RESPONSE_CODE:
249 msg->response_code = atoi(text);
251 case WISPR_ELEMENT_NEXT_URL:
252 case WISPR_ELEMENT_DELAY:
253 case WISPR_ELEMENT_REPLY_MESSAGE:
254 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
256 case WISPR_ELEMENT_LOGOFF_URL:
257 g_free(msg->logoff_url);
258 msg->logoff_url = g_strdup(text);
264 static void error_handler(GMarkupParseContext *context,
265 GError *error, gpointer user_data)
267 struct wispr_msg *msg = user_data;
269 msg->has_error = TRUE;
272 static const GMarkupParser wispr_parser = {
273 start_element_handler,
280 static void parser_callback(const char *str, gpointer user_data)
282 struct wispr_session *wispr = user_data;
283 GMarkupParseContext *context;
286 //printf("%s\n", str);
288 context = g_markup_parse_context_new(&wispr_parser,
289 G_MARKUP_TREAT_CDATA_AS_TEXT, &wispr->msg, NULL);
291 result = g_markup_parse_context_parse(context, str, strlen(str), NULL);
293 result = g_markup_parse_context_end_parse(context, NULL);
295 g_markup_parse_context_free(context);
298 typedef void (*user_input_cb)(const char *value, gpointer user_data);
300 struct user_input_data {
306 struct termios saved_termios;
309 static void user_callback(struct user_input_data *data)
313 if (data->hidden == TRUE) {
316 len = write(data->fd, "\n", 1);
321 tcsetattr(data->fd, TCSADRAIN, &data->saved_termios);
325 value = g_string_free(data->str, FALSE);
328 data->cb(value, data->user_data);
335 static gboolean keyboard_input(GIOChannel *channel, GIOCondition condition,
338 struct user_input_data *data = user_data;
342 len = read(data->fd, buf, 1);
347 if (buf[0] == '\n') {
352 g_string_append_c(data->str, buf[0]);
354 if (data->hidden == TRUE)
355 len = write(data->fd, "*", 1);
360 static gboolean user_input(const char *label, gboolean hidden,
361 user_input_cb func, gpointer user_data)
363 struct user_input_data *data;
364 struct termios new_termios;
369 data = g_try_new0(struct user_input_data, 1);
373 data->str = g_string_sized_new(32);
375 data->user_data = user_data;
376 data->hidden = hidden;
378 data->fd = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC);
382 if (tcgetattr(data->fd, &data->saved_termios) < 0) {
387 new_termios = data->saved_termios;
388 if (data->hidden == TRUE)
389 new_termios.c_lflag &= ~(ICANON|ECHO);
391 new_termios.c_lflag &= ~ICANON;
392 new_termios.c_cc[VMIN] = 1;
393 new_termios.c_cc[VTIME] = 0;
395 tcsetattr(data->fd, TCSADRAIN, &new_termios);
397 channel = g_io_channel_unix_new(data->fd);
398 g_io_channel_set_encoding(channel, NULL, NULL);
399 g_io_channel_set_buffered(channel, FALSE);
400 watch = g_io_add_watch(channel, G_IO_IN, keyboard_input, data);
401 g_io_channel_unref(channel);
406 len = write(data->fd, label, strlen(label));
410 len = write(data->fd, ": ", 2);
417 g_string_free(data->str, TRUE);
423 static void password_callback(const char *value, gpointer user_data)
425 struct wispr_session *wispr = user_data;
427 g_free(wispr->password);
428 wispr->password = g_strdup(value);
432 execute_login(wispr);
435 static void username_callback(const char *value, gpointer user_data)
437 struct wispr_session *wispr = user_data;
439 g_free(wispr->username);
440 wispr->username = g_strdup(value);
442 if (wispr->password == NULL) {
443 user_input("Password", TRUE, password_callback, wispr);
449 execute_login(wispr);
452 static gboolean wispr_input(const guint8 **data, gsize *length,
455 struct wispr_session *wispr = user_data;
459 buf = g_string_sized_new(100);
461 g_string_append(buf, "button=Login&UserName=");
462 g_string_append_uri_escaped(buf, wispr->username, NULL, FALSE);
463 g_string_append(buf, "&Password=");
464 g_string_append_uri_escaped(buf, wispr->password, NULL, FALSE);
465 g_string_append(buf, "&FNAME=0&OriginatingServer=");
466 g_string_append_uri_escaped(buf, wispr->originurl, NULL, FALSE);
470 g_free(wispr->formdata);
471 wispr->formdata = g_string_free(buf, FALSE);
473 *data = (guint8 *) wispr->formdata;
479 static gboolean wispr_route(const char *addr, int ai_family, int if_index,
482 char *family = "unknown";
484 if (ai_family == AF_INET)
486 else if (ai_family == AF_INET6)
489 printf("Route request: %s %s index %d\n", family, addr, if_index);
491 if (ai_family != AF_INET && ai_family != AF_INET6)
497 static gboolean wispr_result(GWebResult *result, gpointer user_data)
499 struct wispr_session *wispr = user_data;
505 g_web_result_get_chunk(result, &chunk, &length);
508 //printf("%s\n", (char *) chunk);
509 g_web_parser_feed_data(wispr->parser, chunk, length);
513 g_web_parser_end_data(wispr->parser);
515 status = g_web_result_get_status(result);
517 g_print("status: %03u\n", status);
519 elapsed = g_timer_elapsed(timer, NULL);
521 g_print("elapse: %f seconds\n", elapsed);
523 if (wispr->msg.message_type < 0) {
524 const char *redirect;
529 if (g_web_result_get_header(result, "Location",
533 printf("Redirect URL: %s\n", redirect);
536 wispr->request = g_web_request_get(wispr->web, redirect,
537 wispr_result, wispr_route, wispr);
542 printf("Message type: %s (%d)\n",
543 message_type_to_string(wispr->msg.message_type),
544 wispr->msg.message_type);
545 printf("Response code: %s (%d)\n",
546 response_code_to_string(wispr->msg.response_code),
547 wispr->msg.response_code);
548 if (wispr->msg.access_procedure != NULL)
549 printf("Access procedure: %s\n", wispr->msg.access_procedure);
550 if (wispr->msg.access_location != NULL)
551 printf("Access location: %s\n", wispr->msg.access_location);
552 if (wispr->msg.location_name != NULL)
553 printf("Location name: %s\n", wispr->msg.location_name);
554 if (wispr->msg.login_url != NULL)
555 printf("Login URL: %s\n", wispr->msg.login_url);
556 if (wispr->msg.abort_login_url != NULL)
557 printf("Abort login URL: %s\n", wispr->msg.abort_login_url);
558 if (wispr->msg.logoff_url != NULL)
559 printf("Logoff URL: %s\n", wispr->msg.logoff_url);
562 if (status != 200 && status != 302 && status != 404)
565 if (wispr->msg.message_type == 100) {
566 if (wispr->username == NULL) {
567 user_input("Username", FALSE, username_callback, wispr);
571 if (wispr->password == NULL) {
572 user_input("Password", TRUE, password_callback, wispr);
576 g_idle_add(execute_login, wispr);
578 } else if (wispr->msg.message_type == 120 ||
579 wispr->msg.message_type == 140) {
580 int code = wispr->msg.response_code;
581 printf("Login process: %s\n",
582 code == 50 ? "SUCCESS" : "FAILURE");
586 const char *redirect;
588 if (g_web_result_get_header(result, "Location",
593 printf("Redirect URL: %s\n", redirect);
596 wispr->request = g_web_request_get(wispr->web, redirect,
597 wispr_result, NULL, wispr);
603 g_main_loop_quit(main_loop);
608 static gboolean execute_login(gpointer user_data)
610 struct wispr_session *wispr = user_data;
612 wispr->request = g_web_request_post(wispr->web, wispr->msg.login_url,
613 "application/x-www-form-urlencoded",
614 wispr_input, wispr_result, wispr);
616 wispr_msg_init(&wispr->msg);
621 static gboolean option_debug = FALSE;
622 static gchar *option_nameserver = NULL;
623 static gchar *option_username = NULL;
624 static gchar *option_password = NULL;
625 static gchar *option_url = NULL;
627 static GOptionEntry options[] = {
628 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
629 "Enable debug output" },
630 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
631 "Specify nameserver", "ADDRESS" },
632 { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
633 "Specify username", "USERNAME" },
634 { "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
635 "Specify password", "PASSWORD" },
636 { "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
637 "Specify arbitrary request", "URL" },
641 int main(int argc, char *argv[])
643 GOptionContext *context;
644 GError *error = NULL;
646 struct wispr_session wispr;
649 context = g_option_context_new(NULL);
650 g_option_context_add_main_entries(context, options, NULL);
652 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
654 g_printerr("%s\n", error->message);
657 g_printerr("An unknown error occurred\n");
661 g_option_context_free(context);
663 memset(&wispr, 0, sizeof(wispr));
664 wispr_msg_init(&wispr.msg);
666 wispr.web = g_web_new(index);
667 if (wispr.web == NULL) {
668 fprintf(stderr, "Failed to create web service\n");
672 if (option_debug == TRUE)
673 g_web_set_debug(wispr.web, web_debug, "WEB");
675 main_loop = g_main_loop_new(NULL, FALSE);
677 if (option_nameserver != NULL) {
678 g_web_add_nameserver(wispr.web, option_nameserver);
679 g_free(option_nameserver);
682 g_web_set_accept(wispr.web, NULL);
683 g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
684 g_web_set_close_connection(wispr.web, TRUE);
686 if (option_url == NULL)
687 option_url = g_strdup(DEFAULT_URL);
689 wispr.username = option_username;
690 wispr.password = option_password;
691 wispr.originurl = option_url;
693 timer = g_timer_new();
695 wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
696 "WISPAccessGatewayParam>",
697 parser_callback, &wispr);
699 wispr.request = g_web_request_get(wispr.web, option_url,
700 wispr_result, wispr_route, &wispr);
702 if (wispr.request == 0) {
703 fprintf(stderr, "Failed to start request\n");
707 memset(&sa, 0, sizeof(sa));
708 sa.sa_handler = sig_term;
709 sigaction(SIGINT, &sa, NULL);
710 sigaction(SIGTERM, &sa, NULL);
712 g_main_loop_run(main_loop);
714 g_timer_destroy(timer);
716 if (wispr.request > 0)
717 g_web_cancel_request(wispr.web, wispr.request);
719 g_web_parser_unref(wispr.parser);
720 g_web_unref(wispr.web);
722 g_main_loop_unref(main_loop);
724 g_free(wispr.username);
725 g_free(wispr.password);
726 g_free(wispr.originurl);