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);
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;
440 buf = g_string_sized_new(100);
442 g_string_append(buf, "button=Login&UserName=");
443 g_string_append_uri_escaped(buf, wispr->username, NULL, FALSE);
444 g_string_append(buf, "&Password=");
445 g_string_append_uri_escaped(buf, wispr->password, NULL, FALSE);
446 g_string_append(buf, "&FNAME=0&OriginatingServer=");
447 g_string_append_uri_escaped(buf, wispr->originurl, NULL, FALSE);
451 g_free(wispr->formdata);
452 wispr->formdata = g_string_free(buf, FALSE);
454 *data = (guint8 *) wispr->formdata;
460 static gboolean wispr_result(GWebResult *result, gpointer user_data)
462 struct wispr_session *wispr = user_data;
468 g_web_result_get_chunk(result, &chunk, &length);
471 //printf("%s\n", (char *) chunk);
472 g_web_parser_feed_data(wispr->parser, chunk, length);
476 g_web_parser_end_data(wispr->parser);
478 status = g_web_result_get_status(result);
480 g_print("status: %03u\n", status);
482 elapsed = g_timer_elapsed(timer, NULL);
484 g_print("elapse: %f seconds\n", elapsed);
486 if (wispr->msg.message_type < 0)
489 printf("Message type: %s (%d)\n",
490 message_type_to_string(wispr->msg.message_type),
491 wispr->msg.message_type);
492 printf("Response code: %s (%d)\n",
493 response_code_to_string(wispr->msg.response_code),
494 wispr->msg.response_code);
495 if (wispr->msg.access_procedure != NULL)
496 printf("Access procedure: %s\n", wispr->msg.access_procedure);
497 if (wispr->msg.access_location != NULL)
498 printf("Access location: %s\n", wispr->msg.access_location);
499 if (wispr->msg.location_name != NULL)
500 printf("Location name: %s\n", wispr->msg.location_name);
501 if (wispr->msg.login_url != NULL)
502 printf("Login URL: %s\n", wispr->msg.login_url);
503 if (wispr->msg.logoff_url != NULL)
504 printf("Logoff URL: %s\n", wispr->msg.logoff_url);
507 if (status == 302 && wispr->msg.message_type == 100) {
508 if (wispr->username == NULL) {
509 user_input("Username", FALSE, username_callback, wispr);
513 if (wispr->password == NULL) {
514 user_input("Password", TRUE, password_callback, wispr);
518 execute_login(wispr);
520 } else if (status == 200 && wispr->msg.message_type == 120) {
521 int code = wispr->msg.response_code;
522 printf("Login process: %s\n",
523 code == 50 ? "SUCCESS" : "FAILURE");
527 g_main_loop_quit(main_loop);
532 static void execute_login(struct wispr_session *wispr)
534 wispr->request = g_web_request_post(wispr->web, wispr->msg.login_url,
535 "application/x-www-form-urlencoded",
536 wispr_input, wispr_result, wispr);
538 wispr_msg_init(&wispr->msg);
541 static gboolean option_debug = FALSE;
542 static gchar *option_nameserver = NULL;
543 static gchar *option_username = NULL;
544 static gchar *option_password = NULL;
545 static gchar *option_url = NULL;
547 static GOptionEntry options[] = {
548 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
549 "Enable debug output" },
550 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
551 "Specify nameserver", "ADDRESS" },
552 { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
553 "Specify username", "USERNAME" },
554 { "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
555 "Specify password", "PASSWORD" },
556 { "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
557 "Specify arbitrary request", "URL" },
561 int main(int argc, char *argv[])
563 GOptionContext *context;
564 GError *error = NULL;
566 struct wispr_session wispr;
569 context = g_option_context_new(NULL);
570 g_option_context_add_main_entries(context, options, NULL);
572 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
574 g_printerr("%s\n", error->message);
577 g_printerr("An unknown error occurred\n");
581 g_option_context_free(context);
583 memset(&wispr, 0, sizeof(wispr));
584 wispr_msg_init(&wispr.msg);
586 wispr.web = g_web_new(index);
587 if (wispr.web == NULL) {
588 fprintf(stderr, "Failed to create web service\n");
592 if (option_debug == TRUE)
593 g_web_set_debug(wispr.web, web_debug, "WEB");
595 main_loop = g_main_loop_new(NULL, FALSE);
597 if (option_nameserver != NULL) {
598 g_web_add_nameserver(wispr.web, option_nameserver);
599 g_free(option_nameserver);
602 g_web_set_accept(wispr.web, NULL);
603 g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
604 g_web_set_close_connection(wispr.web, TRUE);
606 if (option_url == NULL)
607 option_url = g_strdup(DEFAULT_URL);
609 wispr.username = option_username;
610 wispr.password = option_password;
611 wispr.originurl = option_url;
613 timer = g_timer_new();
615 wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
616 "WISPAccessGatewayParam>",
617 parser_callback, &wispr);
619 wispr.request = g_web_request_get(wispr.web, option_url,
620 wispr_result, &wispr);
622 if (wispr.request == 0) {
623 fprintf(stderr, "Failed to start request\n");
627 memset(&sa, 0, sizeof(sa));
628 sa.sa_handler = sig_term;
629 sigaction(SIGINT, &sa, NULL);
630 sigaction(SIGTERM, &sa, NULL);
632 g_main_loop_run(main_loop);
634 g_timer_destroy(timer);
636 if (wispr.request > 0)
637 g_web_cancel_request(wispr.web, wispr.request);
639 g_web_parser_unref(wispr.parser);
640 g_web_unref(wispr.web);
642 g_main_loop_unref(main_loop);
644 g_free(wispr.username);
645 g_free(wispr.password);
646 g_free(wispr.originurl);