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;
107 char *access_procedure;
108 char *access_location;
112 static inline void wispr_msg_init(struct wispr_msg *msg)
114 msg->has_error = FALSE;
115 msg->current_element = NULL;
117 msg->message_type = -1;
118 msg->response_code = -1;
120 g_free(msg->login_url);
121 msg->login_url = NULL;
123 g_free(msg->logoff_url);
124 msg->logoff_url = NULL;
126 g_free(msg->access_procedure);
127 msg->access_procedure = NULL;
129 g_free(msg->access_location);
130 msg->access_location = NULL;
132 g_free(msg->location_name);
133 msg->location_name = NULL;
136 struct wispr_session {
140 struct wispr_msg msg;
147 static void execute_login(struct wispr_session *wispr);
153 WISPR_ELEMENT_ACCESS_PROCEDURE,
154 WISPR_ELEMENT_ACCESS_LOCATION,
155 WISPR_ELEMENT_LOCATION_NAME,
156 WISPR_ELEMENT_LOGIN_URL,
157 WISPR_ELEMENT_ABORT_LOGIN_URL,
158 WISPR_ELEMENT_MESSAGE_TYPE,
159 WISPR_ELEMENT_RESPONSE_CODE,
160 WISPR_ELEMENT_NEXT_URL,
162 WISPR_ELEMENT_REPLY_MESSAGE,
163 WISPR_ELEMENT_LOGIN_RESULTS_URL,
164 WISPR_ELEMENT_LOGOFF_URL,
166 } wispr_element_map[] = {
167 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
168 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
169 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
170 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
171 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
172 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
173 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
174 { "NextURL", WISPR_ELEMENT_NEXT_URL },
175 { "Delay", WISPR_ELEMENT_DELAY },
176 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
177 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
178 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
179 { NULL, WISPR_ELEMENT_NONE },
182 static void start_element_handler(GMarkupParseContext *context,
183 const gchar *element_name,
184 const gchar **attribute_names,
185 const gchar **attribute_values,
186 gpointer user_data, GError **error)
188 struct wispr_msg *msg = user_data;
190 msg->current_element = element_name;
193 static void end_element_handler(GMarkupParseContext *context,
194 const gchar *element_name,
195 gpointer user_data, GError **error)
197 struct wispr_msg *msg = user_data;
199 msg->current_element = NULL;
202 static void text_handler(GMarkupParseContext *context,
203 const gchar *text, gsize text_len,
204 gpointer user_data, GError **error)
206 struct wispr_msg *msg = user_data;
209 if (msg->current_element == NULL)
212 for (i = 0; wispr_element_map[i].str; i++) {
213 if (g_str_equal(wispr_element_map[i].str,
214 msg->current_element) == FALSE)
217 switch (wispr_element_map[i].element) {
218 case WISPR_ELEMENT_NONE:
219 case WISPR_ELEMENT_ACCESS_PROCEDURE:
220 g_free(msg->access_procedure);
221 msg->access_procedure = g_strdup(text);
222 case WISPR_ELEMENT_ACCESS_LOCATION:
223 g_free(msg->access_location);
224 msg->access_location = g_strdup(text);
225 case WISPR_ELEMENT_LOCATION_NAME:
226 g_free(msg->location_name);
227 msg->location_name = g_strdup(text);
229 case WISPR_ELEMENT_LOGIN_URL:
230 g_free(msg->login_url);
231 msg->login_url = g_strdup(text);
233 case WISPR_ELEMENT_ABORT_LOGIN_URL:
235 case WISPR_ELEMENT_MESSAGE_TYPE:
236 msg->message_type = atoi(text);
238 case WISPR_ELEMENT_RESPONSE_CODE:
239 msg->response_code = atoi(text);
241 case WISPR_ELEMENT_NEXT_URL:
242 case WISPR_ELEMENT_DELAY:
243 case WISPR_ELEMENT_REPLY_MESSAGE:
244 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
246 case WISPR_ELEMENT_LOGOFF_URL:
247 g_free(msg->logoff_url);
248 msg->logoff_url = g_strdup(text);
254 static void error_handler(GMarkupParseContext *context,
255 GError *error, gpointer user_data)
257 struct wispr_msg *msg = user_data;
259 msg->has_error = TRUE;
262 static const GMarkupParser wispr_parser = {
263 start_element_handler,
270 static void parser_callback(const char *str, gpointer user_data)
272 struct wispr_session *wispr = user_data;
273 GMarkupParseContext *context;
276 //printf("%s\n", str);
278 context = g_markup_parse_context_new(&wispr_parser,
279 G_MARKUP_TREAT_CDATA_AS_TEXT, &wispr->msg, NULL);
281 result = g_markup_parse_context_parse(context, str, strlen(str), NULL);
283 result = g_markup_parse_context_end_parse(context, NULL);
285 g_markup_parse_context_free(context);
288 typedef void (*user_input_cb)(const char *value, gpointer user_data);
290 struct user_input_data {
296 struct termios saved_termios;
299 static void user_callback(struct user_input_data *data)
304 if (data->hidden == TRUE)
305 len = write(data->fd, "\n", 1);
307 tcsetattr(data->fd, TCSADRAIN, &data->saved_termios);
311 value = g_string_free(data->str, FALSE);
314 data->cb(value, data->user_data);
321 static gboolean keyboard_input(GIOChannel *channel, GIOCondition condition,
324 struct user_input_data *data = user_data;
328 len = read(data->fd, buf, 1);
333 if (buf[0] == '\n') {
338 g_string_append_c(data->str, buf[0]);
340 if (data->hidden == TRUE)
341 len = write(data->fd, "*", 1);
346 static gboolean user_input(const char *label, gboolean hidden,
347 user_input_cb func, gpointer user_data)
349 struct user_input_data *data;
350 struct termios new_termios;
355 data = g_try_new0(struct user_input_data, 1);
359 data->str = g_string_sized_new(32);
361 data->user_data = user_data;
362 data->hidden = hidden;
364 data->fd = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC);
368 if (tcgetattr(data->fd, &data->saved_termios) < 0) {
373 new_termios = data->saved_termios;
374 if (data->hidden == TRUE)
375 new_termios.c_lflag &= ~(ICANON|ECHO);
377 new_termios.c_lflag &= ~ICANON;
378 new_termios.c_cc[VMIN] = 1;
379 new_termios.c_cc[VTIME] = 0;
381 tcsetattr(data->fd, TCSADRAIN, &new_termios);
383 channel = g_io_channel_unix_new(data->fd);
384 g_io_channel_set_encoding(channel, NULL, NULL);
385 g_io_channel_set_buffered(channel, FALSE);
386 watch = g_io_add_watch(channel, G_IO_IN, keyboard_input, data);
387 g_io_channel_unref(channel);
392 len = write(data->fd, label, strlen(label));
393 len = write(data->fd, ": ", 2);
398 g_string_free(data->str, TRUE);
404 static void password_callback(const char *value, gpointer user_data)
406 struct wispr_session *wispr = user_data;
408 g_free(wispr->password);
409 wispr->password = g_strdup(value);
413 execute_login(wispr);
416 static void username_callback(const char *value, gpointer user_data)
418 struct wispr_session *wispr = user_data;
420 g_free(wispr->username);
421 wispr->username = g_strdup(value);
423 if (wispr->password == NULL) {
424 user_input("Password", TRUE, password_callback, wispr);
430 execute_login(wispr);
433 static gboolean wispr_input(const guint8 **data, gsize *length,
436 struct wispr_session *wispr = user_data;
438 g_free(wispr->formdata);
439 wispr->formdata = g_strdup_printf("button=Login&UserName=%s&"
440 "Password=%s&FNAME=0&OriginatingServer=%s",
441 wispr->username, wispr->password, wispr->originurl);
443 *data = (guint8 *) wispr->formdata;
444 *length = strlen(wispr->formdata);
449 static gboolean wispr_result(GWebResult *result, gpointer user_data)
451 struct wispr_session *wispr = user_data;
457 g_web_result_get_chunk(result, &chunk, &length);
460 //printf("%s\n", (char *) chunk);
461 g_web_parser_feed_data(wispr->parser, chunk, length);
465 g_web_parser_end_data(wispr->parser);
467 status = g_web_result_get_status(result);
469 g_print("status: %03u\n", status);
471 elapsed = g_timer_elapsed(timer, NULL);
473 g_print("elapse: %f seconds\n", elapsed);
475 if (wispr->msg.message_type < 0)
478 printf("Message type: %s (%d)\n",
479 message_type_to_string(wispr->msg.message_type),
480 wispr->msg.message_type);
481 printf("Response code: %s (%d)\n",
482 response_code_to_string(wispr->msg.response_code),
483 wispr->msg.response_code);
484 if (wispr->msg.access_procedure != NULL)
485 printf("Access procedure: %s\n", wispr->msg.access_procedure);
486 if (wispr->msg.access_location != NULL)
487 printf("Access location: %s\n", wispr->msg.access_location);
488 if (wispr->msg.location_name != NULL)
489 printf("Location name: %s\n", wispr->msg.location_name);
490 if (wispr->msg.login_url != NULL)
491 printf("Login URL: %s\n", wispr->msg.login_url);
492 if (wispr->msg.logoff_url != NULL)
493 printf("Logoff URL: %s\n", wispr->msg.logoff_url);
496 if (status == 302 && wispr->msg.message_type == 100) {
497 if (wispr->username == NULL) {
498 user_input("Username", FALSE, username_callback, wispr);
502 if (wispr->password == NULL) {
503 user_input("Password", TRUE, password_callback, wispr);
507 execute_login(wispr);
509 } else if (status == 200 && wispr->msg.message_type == 120) {
510 int code = wispr->msg.response_code;
511 printf("Login process: %s\n",
512 code == 50 ? "SUCCESS" : "FAILURE");
516 g_main_loop_quit(main_loop);
521 static void execute_login(struct wispr_session *wispr)
523 wispr->request = g_web_request_post(wispr->web, wispr->msg.login_url,
524 "application/x-www-form-urlencoded",
525 wispr_input, wispr_result, wispr);
527 wispr_msg_init(&wispr->msg);
530 static gboolean option_debug = FALSE;
531 static gchar *option_nameserver = NULL;
532 static gchar *option_username = NULL;
533 static gchar *option_password = NULL;
534 static gchar *option_url = NULL;
536 static GOptionEntry options[] = {
537 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
538 "Enable debug output" },
539 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
540 "Specify nameserver", "ADDRESS" },
541 { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
542 "Specify username", "USERNAME" },
543 { "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
544 "Specify password", "PASSWORD" },
545 { "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
546 "Specify arbitrary request", "URL" },
550 int main(int argc, char *argv[])
552 GOptionContext *context;
553 GError *error = NULL;
555 struct wispr_session wispr;
558 context = g_option_context_new(NULL);
559 g_option_context_add_main_entries(context, options, NULL);
561 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
563 g_printerr("%s\n", error->message);
566 g_printerr("An unknown error occurred\n");
570 g_option_context_free(context);
572 memset(&wispr, 0, sizeof(wispr));
573 wispr_msg_init(&wispr.msg);
575 wispr.web = g_web_new(index);
576 if (wispr.web == NULL) {
577 fprintf(stderr, "Failed to create web service\n");
581 if (option_debug == TRUE)
582 g_web_set_debug(wispr.web, web_debug, "WEB");
584 main_loop = g_main_loop_new(NULL, FALSE);
586 if (option_nameserver != NULL) {
587 g_web_add_nameserver(wispr.web, option_nameserver);
588 g_free(option_nameserver);
591 g_web_set_accept(wispr.web, NULL);
592 g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
593 g_web_set_close_connection(wispr.web, TRUE);
595 if (option_url == NULL)
596 option_url = g_strdup(DEFAULT_URL);
598 wispr.username = option_username;
599 wispr.password = option_password;
600 wispr.originurl = option_url;
602 timer = g_timer_new();
604 wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
605 "WISPAccessGatewayParam>",
606 parser_callback, &wispr);
608 wispr.request = g_web_request_get(wispr.web, option_url,
609 wispr_result, &wispr);
611 if (wispr.request == 0) {
612 fprintf(stderr, "Failed to start request\n");
616 memset(&sa, 0, sizeof(sa));
617 sa.sa_handler = sig_term;
618 sigaction(SIGINT, &sa, NULL);
619 sigaction(SIGTERM, &sa, NULL);
621 g_main_loop_run(main_loop);
623 g_timer_destroy(timer);
625 if (wispr.request > 0)
626 g_web_cancel_request(wispr.web, wispr.request);
628 g_web_parser_unref(wispr.parser);
629 g_web_unref(wispr.web);
631 g_main_loop_unref(main_loop);
633 g_free(wispr.username);
634 g_free(wispr.password);
635 g_free(wispr.originurl);