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)
311 if (data->hidden == TRUE) {
314 len = write(data->fd, "\n", 1);
319 tcsetattr(data->fd, TCSADRAIN, &data->saved_termios);
323 value = g_string_free(data->str, FALSE);
326 data->cb(value, data->user_data);
333 static gboolean keyboard_input(GIOChannel *channel, GIOCondition condition,
336 struct user_input_data *data = user_data;
340 len = read(data->fd, buf, 1);
345 if (buf[0] == '\n') {
350 g_string_append_c(data->str, buf[0]);
352 if (data->hidden == TRUE)
353 len = write(data->fd, "*", 1);
358 static gboolean user_input(const char *label, gboolean hidden,
359 user_input_cb func, gpointer user_data)
361 struct user_input_data *data;
362 struct termios new_termios;
367 data = g_try_new0(struct user_input_data, 1);
371 data->str = g_string_sized_new(32);
373 data->user_data = user_data;
374 data->hidden = hidden;
376 data->fd = open("/dev/tty", O_RDWR | O_NOCTTY);
380 if (tcgetattr(data->fd, &data->saved_termios) < 0) {
385 new_termios = data->saved_termios;
386 if (data->hidden == TRUE)
387 new_termios.c_lflag &= ~(ICANON|ECHO);
389 new_termios.c_lflag &= ~ICANON;
390 new_termios.c_cc[VMIN] = 1;
391 new_termios.c_cc[VTIME] = 0;
393 tcsetattr(data->fd, TCSADRAIN, &new_termios);
395 channel = g_io_channel_unix_new(data->fd);
396 g_io_channel_set_encoding(channel, NULL, NULL);
397 g_io_channel_set_buffered(channel, FALSE);
398 watch = g_io_add_watch(channel, G_IO_IN, keyboard_input, data);
399 g_io_channel_unref(channel);
404 len = write(data->fd, label, strlen(label));
408 len = write(data->fd, ": ", 2);
415 g_string_free(data->str, TRUE);
421 static void password_callback(const char *value, gpointer user_data)
423 struct wispr_session *wispr = user_data;
425 g_free(wispr->password);
426 wispr->password = g_strdup(value);
430 execute_login(wispr);
433 static void username_callback(const char *value, gpointer user_data)
435 struct wispr_session *wispr = user_data;
437 g_free(wispr->username);
438 wispr->username = g_strdup(value);
440 if (wispr->password == NULL) {
441 user_input("Password", TRUE, password_callback, wispr);
447 execute_login(wispr);
450 static gboolean wispr_input(const guint8 **data, gsize *length,
453 struct wispr_session *wispr = user_data;
457 buf = g_string_sized_new(100);
459 g_string_append(buf, "button=Login&UserName=");
460 g_string_append_uri_escaped(buf, wispr->username, NULL, FALSE);
461 g_string_append(buf, "&Password=");
462 g_string_append_uri_escaped(buf, wispr->password, NULL, FALSE);
463 g_string_append(buf, "&FNAME=0&OriginatingServer=");
464 g_string_append_uri_escaped(buf, wispr->originurl, NULL, FALSE);
468 g_free(wispr->formdata);
469 wispr->formdata = g_string_free(buf, FALSE);
471 *data = (guint8 *) wispr->formdata;
477 static gboolean wispr_result(GWebResult *result, gpointer user_data)
479 struct wispr_session *wispr = user_data;
485 g_web_result_get_chunk(result, &chunk, &length);
488 //printf("%s\n", (char *) chunk);
489 g_web_parser_feed_data(wispr->parser, chunk, length);
493 g_web_parser_end_data(wispr->parser);
495 status = g_web_result_get_status(result);
497 g_print("status: %03u\n", status);
499 elapsed = g_timer_elapsed(timer, NULL);
501 g_print("elapse: %f seconds\n", elapsed);
503 if (wispr->msg.message_type < 0) {
504 const char *redirect;
509 if (g_web_result_get_header(result, "Location",
513 printf("Redirect URL: %s\n", redirect);
516 wispr->request = g_web_request_get(wispr->web, redirect,
517 wispr_result, wispr);
522 printf("Message type: %s (%d)\n",
523 message_type_to_string(wispr->msg.message_type),
524 wispr->msg.message_type);
525 printf("Response code: %s (%d)\n",
526 response_code_to_string(wispr->msg.response_code),
527 wispr->msg.response_code);
528 if (wispr->msg.access_procedure != NULL)
529 printf("Access procedure: %s\n", wispr->msg.access_procedure);
530 if (wispr->msg.access_location != NULL)
531 printf("Access location: %s\n", wispr->msg.access_location);
532 if (wispr->msg.location_name != NULL)
533 printf("Location name: %s\n", wispr->msg.location_name);
534 if (wispr->msg.login_url != NULL)
535 printf("Login URL: %s\n", wispr->msg.login_url);
536 if (wispr->msg.abort_login_url != NULL)
537 printf("Abort login URL: %s\n", wispr->msg.abort_login_url);
538 if (wispr->msg.logoff_url != NULL)
539 printf("Logoff URL: %s\n", wispr->msg.logoff_url);
542 if (status != 200 && status != 302 && status != 404)
545 if (wispr->msg.message_type == 100) {
546 if (wispr->username == NULL) {
547 user_input("Username", FALSE, username_callback, wispr);
551 if (wispr->password == NULL) {
552 user_input("Password", TRUE, password_callback, wispr);
556 g_idle_add(execute_login, wispr);
558 } else if (wispr->msg.message_type == 120 ||
559 wispr->msg.message_type == 140) {
560 int code = wispr->msg.response_code;
561 printf("Login process: %s\n",
562 code == 50 ? "SUCCESS" : "FAILURE");
566 const char *redirect;
568 if (g_web_result_get_header(result, "Location",
573 printf("Redirect URL: %s\n", redirect);
576 wispr->request = g_web_request_get(wispr->web, redirect,
577 wispr_result, wispr);
583 g_main_loop_quit(main_loop);
588 static gboolean execute_login(gpointer user_data)
590 struct wispr_session *wispr = user_data;
592 wispr->request = g_web_request_post(wispr->web, wispr->msg.login_url,
593 "application/x-www-form-urlencoded",
594 wispr_input, wispr_result, wispr);
596 wispr_msg_init(&wispr->msg);
601 static gboolean option_debug = FALSE;
602 static gchar *option_nameserver = NULL;
603 static gchar *option_username = NULL;
604 static gchar *option_password = NULL;
605 static gchar *option_url = NULL;
607 static GOptionEntry options[] = {
608 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
609 "Enable debug output" },
610 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
611 "Specify nameserver", "ADDRESS" },
612 { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
613 "Specify username", "USERNAME" },
614 { "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
615 "Specify password", "PASSWORD" },
616 { "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
617 "Specify arbitrary request", "URL" },
621 int main(int argc, char *argv[])
623 GOptionContext *context;
624 GError *error = NULL;
626 struct wispr_session wispr;
629 context = g_option_context_new(NULL);
630 g_option_context_add_main_entries(context, options, NULL);
632 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
634 g_printerr("%s\n", error->message);
637 g_printerr("An unknown error occurred\n");
641 g_option_context_free(context);
643 memset(&wispr, 0, sizeof(wispr));
644 wispr_msg_init(&wispr.msg);
646 wispr.web = g_web_new(index);
647 if (wispr.web == NULL) {
648 fprintf(stderr, "Failed to create web service\n");
652 if (option_debug == TRUE)
653 g_web_set_debug(wispr.web, web_debug, "WEB");
655 main_loop = g_main_loop_new(NULL, FALSE);
657 if (option_nameserver != NULL) {
658 g_web_add_nameserver(wispr.web, option_nameserver);
659 g_free(option_nameserver);
662 g_web_set_accept(wispr.web, NULL);
663 g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
664 g_web_set_close_connection(wispr.web, TRUE);
666 if (option_url == NULL)
667 option_url = g_strdup(DEFAULT_URL);
669 wispr.username = option_username;
670 wispr.password = option_password;
671 wispr.originurl = option_url;
673 timer = g_timer_new();
675 wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
676 "WISPAccessGatewayParam>",
677 parser_callback, &wispr);
679 wispr.request = g_web_request_get(wispr.web, option_url,
680 wispr_result, &wispr);
682 if (wispr.request == 0) {
683 fprintf(stderr, "Failed to start request\n");
687 memset(&sa, 0, sizeof(sa));
688 sa.sa_handler = sig_term;
689 sigaction(SIGINT, &sa, NULL);
690 sigaction(SIGTERM, &sa, NULL);
692 g_main_loop_run(main_loop);
694 g_timer_destroy(timer);
696 if (wispr.request > 0)
697 g_web_cancel_request(wispr.web, wispr.request);
699 g_web_parser_unref(wispr.parser);
700 g_web_unref(wispr.web);
702 g_main_loop_unref(main_loop);
704 g_free(wispr.username);
705 g_free(wispr.password);
706 g_free(wispr.originurl);