/* * 15-821 Coda * Auth Module * Anoop Jaishankar, Sang Kil Cha, Jaeyoon Chong */ #include "coda-authorization.h" HildonFileSystemModel *model; void cb_login_quit(GtkWidget *w, AuthData * data) { gtk_main_quit (); } void alert(char* message) { GtkWidget * alert; GtkWidget * label; alert = gtk_dialog_new_with_buttons("Alert", NULL, (GTK_DIALOG_DESTROY_WITH_PARENT || GTK_DIALOG_MODAL), GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL ); label = gtk_label_new(message); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(alert)->vbox), label); printf("%s\n", message); gtk_widget_show_all(alert); // Run the dialog gint response = gtk_dialog_run (GTK_DIALOG (alert)); // Handle the response if (response == GTK_RESPONSE_ACCEPT) { // Destroy widget, once its handled gtk_widget_destroy(alert); } } int ctokens_verify(const char* realm) { char *argv[10]; int pipefds[2], devnull; int rc, status, n = 0; // anoop int pipe2[2]; char errMsg[100], realmParam[80]; int i; for(i=0;i<100;i++) errMsg[i] = '\0'; for(i=0;i<80;i++) realmParam[i] = '\0'; sprintf(realmParam, "%c%s", '@', realm); /* set up pipe to communicate with ctokens */ rc = pipe(pipefds); if (rc) return -1; rc = pipe(pipe2); if (rc) return -1; /* fork and execute ctokens */ rc = fork(); if (rc < 0) return -1; if (rc == 0) /* child */ { devnull = open("/dev/null", O_WRONLY); /* set the pipe's output to stdin redirect stdout and * stderr to /dev/null */ dup2(pipefds[0], 0); // comment next 2 dup2(pipe2[1], 1); dup2(pipe2[1], 2); close(pipe2[0]); close(pipe2[1]); /* cleanup open file descriptors */ close(pipefds[0]); close(pipefds[1]); close(devnull); /* set up arguments */ argv[n++] = "ctokens"; argv[n++] = (char*) realmParam; argv[n++] = NULL; execv(CTOKENS_PROGRAM, argv); /* exec failed? nothing to do but die, maybe log a failure */ exit(-1); } /* cleanup */ close(pipefds[0]); close(pipefds[1]); /* and wait for clog to exit (wait not strictly necessary), * but this way we will know if the execution failed */ rc = wait(&status); if (rc > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0) { printf("CTOKENS succeeded\n"); return 0; } read(pipe2[0], errMsg, 100); close(pipe2[0]); close(pipe2[1]); return -1; } int clog_verify(const char *uname, const char* realm, const char* passwd) { char *argv[10]; int pipefds[2], devnull; int rc, status, n = 0; int pipe2[2]; char errMsg[100], fullUserName[80]; int i; for(i=0;i<100;i++) errMsg[i] = '\0'; for(i=0;i<80;i++) fullUserName[i] = '\0'; sprintf(fullUserName, "%s%c%s", uname, '@', realm); /* set up pipe to communicate with clog */ rc = pipe(pipefds); if (rc) return -1; rc = pipe(pipe2); if (rc) return -1; /* fork and execute clog */ rc = fork(); if (rc < 0) return -1; if (rc == 0) /* child */ { devnull = open("/dev/null", O_WRONLY); /* set the pipe's output to stdin redirect stdout and * stderr to /dev/null */ dup2(pipefds[0], 0); dup2(pipe2[1], 1); dup2(pipe2[1], 2); close(pipe2[0]); close(pipe2[1]); /* cleanup open file descriptors */ close(pipefds[0]); close(pipefds[1]); close(devnull); /* set up arguments */ argv[n++] = "clog"; argv[n++] = "-pipe"; argv[n++] = (char*) fullUserName; argv[n++] = NULL; execv(CLOG_PROGRAM, argv); /* exec failed? nothing to do but die, maybe log a failure */ exit(-1); } /* send password to clog */ write(pipefds[1], passwd, strlen(passwd)); write(pipefds[1], "\n", 1); /* cleanup */ close(pipefds[0]); close(pipefds[1]); /* and wait for clog to exit (wait not strictly necessary), * but this way we will know if the execution failed */ rc = wait(&status); if (rc > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0) { //printf("CLOG succeeded\n"); // Folder refresh method. hildon_file_system_model_refresh(model); return 0; } read(pipe2[0], errMsg, 100); close(pipe2[0]); close(pipe2[1]); alert(errMsg); return -1; } int authorize_user(AuthData * data) { char *uname, *realm, *passwd; int success = 0; uname = passwd = NULL; uname = (char*) (gtk_entry_get_text(GTK_ENTRY(data->user_name))); realm = (char*) (gtk_entry_get_text(GTK_ENTRY(data->realm_name))); passwd = (char*) (gtk_entry_get_text(GTK_ENTRY(data->password))); if((uname != NULL) && !strlen(uname)) { alert("Enter Username"); return success; } else if((realm != NULL) && !strlen(realm)) { alert("Enter Realm"); return success; } else if((passwd != NULL) && !strlen(passwd)) { alert("Enter Password"); return success; } if(clog_verify(uname, realm, passwd) == 0) { success = 1; } return success; } void login_dialog_init(AuthData * data ) { /* Initializing auth_hbox */ data->auth_hbox1 = gtk_hbox_new(TRUE, 1); data->auth_hbox2 = gtk_hbox_new(TRUE, 1); data->auth_hbox3 = gtk_hbox_new(TRUE, 1); // Auth dialog data->auth_dialog = gtk_dialog_new_with_buttons("AuthDialog", NULL, (GTK_DIALOG_DESTROY_WITH_PARENT || GTK_DIALOG_MODAL), GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL); // Username data->user_name_label = gtk_label_new("Username: "); data->user_name = gtk_entry_new_with_max_length(40); gtk_box_pack_start(GTK_BOX(data->auth_hbox1), data->user_name_label, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(data->auth_hbox1), data->user_name, FALSE, TRUE, 0); // Realm data->realm_label = gtk_label_new("Realm: "); data->realm_name = gtk_entry_new_with_max_length(40); gtk_box_pack_start(GTK_BOX(data->auth_hbox2), data->realm_label, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(data->auth_hbox2), data->realm_name, FALSE, TRUE, 0); // Password data->password_label = gtk_label_new("Password: "); data->password = gtk_entry_new_with_max_length(20); gtk_entry_set_visibility(GTK_ENTRY(data->password), FALSE); gtk_box_pack_start(GTK_BOX(data->auth_hbox3), data->password_label, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(data->auth_hbox3), data->password, FALSE, TRUE, 0); // Add hboxs to dialog->vbox gtk_container_add(GTK_CONTAINER(GTK_DIALOG(data->auth_dialog)->vbox), data->auth_hbox1); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(data->auth_dialog)->vbox), data->auth_hbox2); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(data->auth_dialog)->vbox), data->auth_hbox3); // Display the dialog gtk_widget_show_all(data->auth_dialog); } void coda_login_dialog(char *uri, HildonFileSystemModel *model_in) { static int trials = 0; int success = 0; AuthData *data = g_new0(AuthData, 1); char *realm = NULL; model = model_in; while((trials < 3) && !success) { // URI of form: /coda/testserver.coda.cs.cmu.edu/ /* delete direcoty from the filename path */ //printf("URI RECVD: %s\n", uri); char *slash; slash = uri; slash += 7; realm = strtok(slash, "/"); realm = strtok(NULL, "/"); //printf("realm2: %s\n", realm); // Check if tokens already exist. // If they do, then don't invoke clog. // If no tokens available, invoke clog. if(ctokens_verify(realm) == 0) { //printf("Tokens exist\n"); return; } // Initialize and display the Dialog login_dialog_init(data); // Run the dialog gint response = gtk_dialog_run (GTK_DIALOG (data->auth_dialog)); // Handle the response if (response == GTK_RESPONSE_ACCEPT) { if(authorize_user(data)) { success = 1; trials = 0; } } else { success = 1; trials = 0; } // Destroy widget, once its handled gtk_widget_destroy(data->auth_dialog); trials++; } trials = 0; } /* void login_create_menu(AuthData *data) { data->main_menu = GTK_MENU(gtk_menu_new()); / * Create Menu items and connect signals * / data->menu_item_login = gtk_menu_item_new_with_label ("Login..."); g_signal_connect (G_OBJECT (data->menu_item_login), "activate", G_CALLBACK (cb_login_dialog), data); data->menu_item_quit = gtk_menu_item_new_with_label ("Quit"); g_signal_connect (G_OBJECT (data->menu_item_quit), "activate", G_CALLBACK (cb_login_quit), NULL); / * Append items to menu * / gtk_menu_append (data->main_menu, data->menu_item_login); gtk_menu_append (data->main_menu, gtk_separator_menu_item_new()); gtk_menu_append (data->main_menu, data->menu_item_quit); hildon_window_set_menu(HILDON_WINDOW(data->window), GTK_MENU(data->main_menu)); // Show all the items gtk_widget_show_all (GTK_WIDGET (data->main_menu)); } void login_init(AuthData *data) { / * Create the hildon program and setup the title * / data->program = HILDON_PROGRAM(hildon_program_get_instance()); g_set_application_name("Auth Module"); / * Create HildonWindow and set it to HildonProgram * / data->window = HILDON_WINDOW(hildon_window_new()); hildon_program_add_window(data->program, data->window); / * Add main vbox to appview * / data->main_vbox = gtk_vbox_new(FALSE, 0); gtk_container_add(GTK_CONTAINER(data->window), data->main_vbox); / * Add label to main vbox * / data->label = gtk_label_new("Test Authorization\nClick on Menu to Login"); gtk_box_pack_start(GTK_BOX(data->main_vbox), data->label, TRUE, TRUE, 0); / * Connect signal to X in the upper corner * / g_signal_connect(G_OBJECT(data->window), "delete_event", G_CALLBACK(cb_login_quit), data); gtk_widget_show_all(GTK_WIDGET(data->window)); / * Create Menu * / login_create_menu(data); } int main(int argc, char* argv[]) { AuthData *data = g_new0(AuthData, 1); gtk_init(&argc, &argv); login_init(data); gtk_main(); g_free(data); return 0; } */