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)
497 printf("Message type: %s (%d)\n",
498 message_type_to_string(wispr->msg.message_type),
499 wispr->msg.message_type);
500 printf("Response code: %s (%d)\n",
501 response_code_to_string(wispr->msg.response_code),
502 wispr->msg.response_code);
503 if (wispr->msg.access_procedure != NULL)
504 printf("Access procedure: %s\n", wispr->msg.access_procedure);
505 if (wispr->msg.access_location != NULL)
506 printf("Access location: %s\n", wispr->msg.access_location);
507 if (wispr->msg.location_name != NULL)
508 printf("Location name: %s\n", wispr->msg.location_name);
509 if (wispr->msg.login_url != NULL)
510 printf("Login URL: %s\n", wispr->msg.login_url);
511 if (wispr->msg.abort_login_url != NULL)
512 printf("Abort login URL: %s\n", wispr->msg.abort_login_url);
513 if (wispr->msg.logoff_url != NULL)
514 printf("Logoff URL: %s\n", wispr->msg.logoff_url);
517 if (status == 302 && wispr->msg.message_type == 100) {
518 if (wispr->username == NULL) {
519 user_input("Username", FALSE, username_callback, wispr);
523 if (wispr->password == NULL) {
524 user_input("Password", TRUE, password_callback, wispr);
528 g_idle_add(execute_login, wispr);
530 } else if (status == 200 && wispr->msg.message_type == 120) {
531 int code = wispr->msg.response_code;
532 printf("Login process: %s\n",
533 code == 50 ? "SUCCESS" : "FAILURE");
537 g_main_loop_quit(main_loop);
542 static gboolean execute_login(gpointer user_data)
544 struct wispr_session *wispr = user_data;
546 wispr->request = g_web_request_post(wispr->web, wispr->msg.login_url,
547 "application/x-www-form-urlencoded",
548 wispr_input, wispr_result, wispr);
550 wispr_msg_init(&wispr->msg);
555 static gboolean option_debug = FALSE;
556 static gchar *option_nameserver = NULL;
557 static gchar *option_username = NULL;
558 static gchar *option_password = NULL;
559 static gchar *option_url = NULL;
561 static GOptionEntry options[] = {
562 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
563 "Enable debug output" },
564 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
565 "Specify nameserver", "ADDRESS" },
566 { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
567 "Specify username", "USERNAME" },
568 { "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
569 "Specify password", "PASSWORD" },
570 { "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
571 "Specify arbitrary request", "URL" },
575 int main(int argc, char *argv[])
577 GOptionContext *context;
578 GError *error = NULL;
580 struct wispr_session wispr;
583 context = g_option_context_new(NULL);
584 g_option_context_add_main_entries(context, options, NULL);
586 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
588 g_printerr("%s\n", error->message);
591 g_printerr("An unknown error occurred\n");
595 g_option_context_free(context);
597 memset(&wispr, 0, sizeof(wispr));
598 wispr_msg_init(&wispr.msg);
600 wispr.web = g_web_new(index);
601 if (wispr.web == NULL) {
602 fprintf(stderr, "Failed to create web service\n");
606 if (option_debug == TRUE)
607 g_web_set_debug(wispr.web, web_debug, "WEB");
609 main_loop = g_main_loop_new(NULL, FALSE);
611 if (option_nameserver != NULL) {
612 g_web_add_nameserver(wispr.web, option_nameserver);
613 g_free(option_nameserver);
616 g_web_set_accept(wispr.web, NULL);
617 g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
618 g_web_set_close_connection(wispr.web, TRUE);
620 if (option_url == NULL)
621 option_url = g_strdup(DEFAULT_URL);
623 wispr.username = option_username;
624 wispr.password = option_password;
625 wispr.originurl = option_url;
627 timer = g_timer_new();
629 wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
630 "WISPAccessGatewayParam>",
631 parser_callback, &wispr);
633 wispr.request = g_web_request_get(wispr.web, option_url,
634 wispr_result, &wispr);
636 if (wispr.request == 0) {
637 fprintf(stderr, "Failed to start request\n");
641 memset(&sa, 0, sizeof(sa));
642 sa.sa_handler = sig_term;
643 sigaction(SIGINT, &sa, NULL);
644 sigaction(SIGTERM, &sa, NULL);
646 g_main_loop_run(main_loop);
648 g_timer_destroy(timer);
650 if (wispr.request > 0)
651 g_web_cancel_request(wispr.web, wispr.request);
653 g_web_parser_unref(wispr.parser);
654 g_web_unref(wispr.web);
656 g_main_loop_unref(main_loop);
658 g_free(wispr.username);
659 g_free(wispr.password);
660 g_free(wispr.originurl);