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 void execute_login(struct wispr_session *wispr);
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);
226 case WISPR_ELEMENT_ACCESS_LOCATION:
227 g_free(msg->access_location);
228 msg->access_location = g_strdup(text);
229 case WISPR_ELEMENT_LOCATION_NAME:
230 g_free(msg->location_name);
231 msg->location_name = g_strdup(text);
233 case WISPR_ELEMENT_LOGIN_URL:
234 g_free(msg->login_url);
235 msg->login_url = g_strdup(text);
237 case WISPR_ELEMENT_ABORT_LOGIN_URL:
238 g_free(msg->abort_login_url);
239 msg->abort_login_url = g_strdup(text);
241 case WISPR_ELEMENT_MESSAGE_TYPE:
242 msg->message_type = atoi(text);
244 case WISPR_ELEMENT_RESPONSE_CODE:
245 msg->response_code = atoi(text);
247 case WISPR_ELEMENT_NEXT_URL:
248 case WISPR_ELEMENT_DELAY:
249 case WISPR_ELEMENT_REPLY_MESSAGE:
250 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
252 case WISPR_ELEMENT_LOGOFF_URL:
253 g_free(msg->logoff_url);
254 msg->logoff_url = g_strdup(text);
260 static void error_handler(GMarkupParseContext *context,
261 GError *error, gpointer user_data)
263 struct wispr_msg *msg = user_data;
265 msg->has_error = TRUE;
268 static const GMarkupParser wispr_parser = {
269 start_element_handler,
276 static void parser_callback(const char *str, gpointer user_data)
278 struct wispr_session *wispr = user_data;
279 GMarkupParseContext *context;
282 //printf("%s\n", str);
284 context = g_markup_parse_context_new(&wispr_parser,
285 G_MARKUP_TREAT_CDATA_AS_TEXT, &wispr->msg, NULL);
287 result = g_markup_parse_context_parse(context, str, strlen(str), NULL);
289 result = g_markup_parse_context_end_parse(context, NULL);
291 g_markup_parse_context_free(context);
294 typedef void (*user_input_cb)(const char *value, gpointer user_data);
296 struct user_input_data {
302 struct termios saved_termios;
305 static void user_callback(struct user_input_data *data)
310 if (data->hidden == TRUE)
311 len = write(data->fd, "\n", 1);
313 tcsetattr(data->fd, TCSADRAIN, &data->saved_termios);
317 value = g_string_free(data->str, FALSE);
320 data->cb(value, data->user_data);
327 static gboolean keyboard_input(GIOChannel *channel, GIOCondition condition,
330 struct user_input_data *data = user_data;
334 len = read(data->fd, buf, 1);
339 if (buf[0] == '\n') {
344 g_string_append_c(data->str, buf[0]);
346 if (data->hidden == TRUE)
347 len = write(data->fd, "*", 1);
352 static gboolean user_input(const char *label, gboolean hidden,
353 user_input_cb func, gpointer user_data)
355 struct user_input_data *data;
356 struct termios new_termios;
361 data = g_try_new0(struct user_input_data, 1);
365 data->str = g_string_sized_new(32);
367 data->user_data = user_data;
368 data->hidden = hidden;
370 data->fd = open("/dev/tty", O_RDWR | O_NOCTTY);
374 if (tcgetattr(data->fd, &data->saved_termios) < 0) {
379 new_termios = data->saved_termios;
380 if (data->hidden == TRUE)
381 new_termios.c_lflag &= ~(ICANON|ECHO);
383 new_termios.c_lflag &= ~ICANON;
384 new_termios.c_cc[VMIN] = 1;
385 new_termios.c_cc[VTIME] = 0;
387 tcsetattr(data->fd, TCSADRAIN, &new_termios);
389 channel = g_io_channel_unix_new(data->fd);
390 g_io_channel_set_encoding(channel, NULL, NULL);
391 g_io_channel_set_buffered(channel, FALSE);
392 watch = g_io_add_watch(channel, G_IO_IN, keyboard_input, data);
393 g_io_channel_unref(channel);
398 len = write(data->fd, label, strlen(label));
399 len = write(data->fd, ": ", 2);
404 g_string_free(data->str, TRUE);
410 static void password_callback(const char *value, gpointer user_data)
412 struct wispr_session *wispr = user_data;
414 g_free(wispr->password);
415 wispr->password = g_strdup(value);
419 execute_login(wispr);
422 static void username_callback(const char *value, gpointer user_data)
424 struct wispr_session *wispr = user_data;
426 g_free(wispr->username);
427 wispr->username = g_strdup(value);
429 if (wispr->password == NULL) {
430 user_input("Password", TRUE, password_callback, wispr);
436 execute_login(wispr);
439 static gboolean wispr_input(const guint8 **data, gsize *length,
442 struct wispr_session *wispr = user_data;
446 buf = g_string_sized_new(100);
448 g_string_append(buf, "button=Login&UserName=");
449 g_string_append_uri_escaped(buf, wispr->username, NULL, FALSE);
450 g_string_append(buf, "&Password=");
451 g_string_append_uri_escaped(buf, wispr->password, NULL, FALSE);
452 g_string_append(buf, "&FNAME=0&OriginatingServer=");
453 g_string_append_uri_escaped(buf, wispr->originurl, NULL, FALSE);
457 g_free(wispr->formdata);
458 wispr->formdata = g_string_free(buf, FALSE);
460 *data = (guint8 *) wispr->formdata;
466 static gboolean wispr_result(GWebResult *result, gpointer user_data)
468 struct wispr_session *wispr = user_data;
474 g_web_result_get_chunk(result, &chunk, &length);
477 //printf("%s\n", (char *) chunk);
478 g_web_parser_feed_data(wispr->parser, chunk, length);
482 g_web_parser_end_data(wispr->parser);
484 status = g_web_result_get_status(result);
486 g_print("status: %03u\n", status);
488 elapsed = g_timer_elapsed(timer, NULL);
490 g_print("elapse: %f seconds\n", elapsed);
492 if (wispr->msg.message_type < 0)
495 printf("Message type: %s (%d)\n",
496 message_type_to_string(wispr->msg.message_type),
497 wispr->msg.message_type);
498 printf("Response code: %s (%d)\n",
499 response_code_to_string(wispr->msg.response_code),
500 wispr->msg.response_code);
501 if (wispr->msg.access_procedure != NULL)
502 printf("Access procedure: %s\n", wispr->msg.access_procedure);
503 if (wispr->msg.access_location != NULL)
504 printf("Access location: %s\n", wispr->msg.access_location);
505 if (wispr->msg.location_name != NULL)
506 printf("Location name: %s\n", wispr->msg.location_name);
507 if (wispr->msg.login_url != NULL)
508 printf("Login URL: %s\n", wispr->msg.login_url);
509 if (wispr->msg.abort_login_url != NULL)
510 printf("Abort login URL: %s\n", wispr->msg.abort_login_url);
511 if (wispr->msg.logoff_url != NULL)
512 printf("Logoff URL: %s\n", wispr->msg.logoff_url);
515 if (status == 302 && wispr->msg.message_type == 100) {
516 if (wispr->username == NULL) {
517 user_input("Username", FALSE, username_callback, wispr);
521 if (wispr->password == NULL) {
522 user_input("Password", TRUE, password_callback, wispr);
526 execute_login(wispr);
528 } else if (status == 200 && wispr->msg.message_type == 120) {
529 int code = wispr->msg.response_code;
530 printf("Login process: %s\n",
531 code == 50 ? "SUCCESS" : "FAILURE");
535 g_main_loop_quit(main_loop);
540 static void execute_login(struct wispr_session *wispr)
542 wispr->request = g_web_request_post(wispr->web, wispr->msg.login_url,
543 "application/x-www-form-urlencoded",
544 wispr_input, wispr_result, wispr);
546 wispr_msg_init(&wispr->msg);
549 static gboolean option_debug = FALSE;
550 static gchar *option_nameserver = NULL;
551 static gchar *option_username = NULL;
552 static gchar *option_password = NULL;
553 static gchar *option_url = NULL;
555 static GOptionEntry options[] = {
556 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
557 "Enable debug output" },
558 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
559 "Specify nameserver", "ADDRESS" },
560 { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
561 "Specify username", "USERNAME" },
562 { "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
563 "Specify password", "PASSWORD" },
564 { "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
565 "Specify arbitrary request", "URL" },
569 int main(int argc, char *argv[])
571 GOptionContext *context;
572 GError *error = NULL;
574 struct wispr_session wispr;
577 context = g_option_context_new(NULL);
578 g_option_context_add_main_entries(context, options, NULL);
580 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
582 g_printerr("%s\n", error->message);
585 g_printerr("An unknown error occurred\n");
589 g_option_context_free(context);
591 memset(&wispr, 0, sizeof(wispr));
592 wispr_msg_init(&wispr.msg);
594 wispr.web = g_web_new(index);
595 if (wispr.web == NULL) {
596 fprintf(stderr, "Failed to create web service\n");
600 if (option_debug == TRUE)
601 g_web_set_debug(wispr.web, web_debug, "WEB");
603 main_loop = g_main_loop_new(NULL, FALSE);
605 if (option_nameserver != NULL) {
606 g_web_add_nameserver(wispr.web, option_nameserver);
607 g_free(option_nameserver);
610 g_web_set_accept(wispr.web, NULL);
611 g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
612 g_web_set_close_connection(wispr.web, TRUE);
614 if (option_url == NULL)
615 option_url = g_strdup(DEFAULT_URL);
617 wispr.username = option_username;
618 wispr.password = option_password;
619 wispr.originurl = option_url;
621 timer = g_timer_new();
623 wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
624 "WISPAccessGatewayParam>",
625 parser_callback, &wispr);
627 wispr.request = g_web_request_get(wispr.web, option_url,
628 wispr_result, &wispr);
630 if (wispr.request == 0) {
631 fprintf(stderr, "Failed to start request\n");
635 memset(&sa, 0, sizeof(sa));
636 sa.sa_handler = sig_term;
637 sigaction(SIGINT, &sa, NULL);
638 sigaction(SIGTERM, &sa, NULL);
640 g_main_loop_run(main_loop);
642 g_timer_destroy(timer);
644 if (wispr.request > 0)
645 g_web_cancel_request(wispr.web, wispr.request);
647 g_web_parser_unref(wispr.parser);
648 g_web_unref(wispr.web);
650 g_main_loop_unref(main_loop);
652 g_free(wispr.username);
653 g_free(wispr.password);
654 g_free(wispr.originurl);