From 990c8197f467b12992ce77bfb275d93dd45924f9 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 29 Nov 2011 11:38:36 +0100 Subject: vnc-client: allow passing in username+password from the command line --- vnc-client.c | 29 +++++++++++++++++++++-------- vnc.c | 58 ++++++++++++++++++++++++++++++++++++---------------------- vnc.h | 2 +- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/vnc-client.c b/vnc-client.c index d6db233..a4c833e 100644 --- a/vnc-client.c +++ b/vnc-client.c @@ -24,11 +24,13 @@ static void usage(FILE *fp) "\n" "usage: %s [options] display\n" "options:\n" - " -h Print this text.\n" - " -d Enable debug output.\n" - " -p Show mouse pointer.\n" - " -f Start in fullscreen mode.\n" - " -c Close on disconnect.\n" + " -h Print this text.\n" + " -d Enable debug output.\n" + " -p Show mouse pointer.\n" + " -f Start in fullscreen mode.\n" + " -c Close on disconnect.\n" + " -u Set username.\n" + " -s Set password.\n" "\n" "-- \n" "(c) 2006 Gerd Hoffmann \n", @@ -39,7 +41,7 @@ int main(int argc, char *argv[]) { unsigned long vnc_flags = VNC_FLAG_STANDALONE; - char hostname[65]; + char hostname[65], *username = NULL, *password = NULL; int port; int debug = 0; int c; @@ -51,7 +53,7 @@ main(int argc, char *argv[]) ++progname; gtk_init(&argc, &argv); - while ((c = getopt(argc, argv, "hdpfc")) != -1) { + while ((c = getopt(argc, argv, "hdpfcu:s:")) != -1) { switch (c) { case 'd': debug++; @@ -65,6 +67,12 @@ main(int argc, char *argv[]) case 'c': vnc_flags |= VNC_FLAG_DISCONNECT_CLOSE; break; + case 'u': + username = optarg; + break; + case 's': + password = optarg; + break; case 'h': usage(stdout); exit(0); @@ -108,8 +116,13 @@ main(int argc, char *argv[]) printf("Connecting to host '%s' port %d\n", hostname, port); if (NULL == vnc_open(strlen(hostname) ? hostname : NULL, - port, vnc_flags, debug)) + port, vnc_flags, debug, username, password)) exit(1); + if (password) { + while (*password) { + *(password++) = '*'; + } + } gtk_main(); fprintf(stderr,"bye...\n"); diff --git a/vnc.c b/vnc.c index 0e84451..b8f1e86 100644 --- a/vnc.c +++ b/vnc.c @@ -39,6 +39,8 @@ struct vnc_window { char display[100]; char hostname[80]; char tcpport[16]; + char username[32]; + char password[32]; /* state */ int input_grabbed; @@ -88,7 +90,7 @@ static void vnc_window_texts(struct vnc_window *vnc) if (vnc->input_grabbed) snprintf(st, sizeof(st), "Press Ctrl-Alt to release input grab."); - + gtk_window_set_title(GTK_WINDOW(vnc->win), ti); gtk_label_set_text(GTK_LABEL(vnc->line), st); gtk_label_set_text(GTK_LABEL(vnc->res), si); @@ -120,7 +122,7 @@ static int user_getstring(GtkWidget *window, char *title, char *message, GtkWidget *dialog, *label, *entry; const char *txt; int retval; - + /* Create the widgets */ dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(window), @@ -131,7 +133,7 @@ static int user_getstring(GtkWidget *window, char *title, char *message, GTK_RESPONSE_REJECT, NULL); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); - + label = gtk_label_new(message); gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); @@ -235,25 +237,31 @@ static void vnc_credential(GtkWidget *vncdisplay, struct vnc_window *vnc = data; char *val, msg[127], str[128]; int i, rc; - + for (i = 0 ; i < credList->n_values ; i++) { GValue *cred = g_value_array_get_nth(credList, i); switch (g_value_get_enum(cred)) { case VNC_DISPLAY_CREDENTIAL_USERNAME: - snprintf(msg, sizeof(msg), "Username for %s ?", vnc->display); - rc = user_getstring(vnc->win, "Authentication", msg, - str, sizeof(str), 0); - if (0 != rc) - return; - val = str; + if (strlen(vnc->username) == 0) { + snprintf(msg, sizeof(msg), "Username for %s ?", vnc->display); + rc = user_getstring(vnc->win, "Authentication", msg, + str, sizeof(str), 0); + if (0 != rc) + return; + snprintf(vnc->username, sizeof(vnc->username), "%s", str); + } + val = vnc->username; break; case VNC_DISPLAY_CREDENTIAL_PASSWORD: - snprintf(msg, sizeof(msg), "Password for %s ?", vnc->display); - rc = user_getstring(vnc->win, "Authentication", msg, - str, sizeof(str), 1); - if (0 != rc) - return; - val = str; + if (strlen(vnc->password) == 0) { + snprintf(msg, sizeof(msg), "Password for %s ?", vnc->display); + rc = user_getstring(vnc->win, "Authentication", msg, + str, sizeof(str), 1); + if (0 != rc) + return; + snprintf(vnc->password, sizeof(vnc->password), "%s", str); + } + val = vnc->password; break; case VNC_DISPLAY_CREDENTIAL_CLIENTNAME: val = "vnc"; @@ -373,7 +381,7 @@ static void menu_cb_connect(GtkToggleAction *action, gpointer user_data) str, sizeof(str), 0); if (0 != rc) return; - + if (2 == sscanf(str, "%64[^:]:%d", hostname, &displayno)) { port = displayno + 5900; goto connect; @@ -432,7 +440,7 @@ static void menu_cb_about(GtkMenuItem *item, void *user_data) static void menu_cb_quit(GtkMenuItem *item, void *user_data) { struct vnc_window *vnc = user_data; - + gtk_widget_destroy(vnc->win); } @@ -514,7 +522,7 @@ static char ui_xml[] = /* public API functions */ GtkWidget *vnc_open(char *hostname, int tcpport, unsigned long flags, - int debug_level) + int debug_level, const char *username, const char *password) { GtkWidget *vbox, *hbox, *frame, *item; GtkWidget *ebox, *align; @@ -532,6 +540,12 @@ GtkWidget *vnc_open(char *hostname, int tcpport, unsigned long flags, vnc->grab_keyboard = (flags & VNC_FLAG_GRAB_KEYBOARD); vnc->disconn_close = (flags & VNC_FLAG_DISCONNECT_CLOSE); vnc->debug = debug_level; + if (username) { + snprintf(vnc->username, sizeof(vnc->username), "%s", username); + } + if (password) { + snprintf(vnc->password, sizeof(vnc->password), "%s", password); + } /* gtk toplevel */ vnc->win = gtk_window_new(GTK_WINDOW_TOPLEVEL); @@ -596,7 +610,7 @@ GtkWidget *vnc_open(char *hostname, int tcpport, unsigned long flags, item = gtk_ui_manager_get_widget(vnc->ui, "/ConfMenu/GrabKeyboard"); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), vnc->grab_keyboard); vnc_display_set_keyboard_grab(VNC_DISPLAY(vnc->vnc), vnc->grab_keyboard); - + /* labels for the status line */ vnc->line = gtk_label_new("status line"); vnc->res = gtk_label_new("vnc screen resolution"); @@ -632,7 +646,7 @@ GtkWidget *vnc_open(char *hostname, int tcpport, unsigned long flags, vnc_window_texts(vnc); if (flags & VNC_FLAG_FULLSCREEN) gtk_window_fullscreen(GTK_WINDOW(vnc->win)); - + /* connect */ if (hostname) vnc_connect_to(vnc, hostname, tcpport); @@ -647,7 +661,7 @@ GtkWidget *vnc_open(char *hostname, int tcpport, unsigned long flags, #else /* HAVE_GTK_VNC */ GtkWidget *vnc_open(char *hostname, int tcpport, unsigned long flags, - int debug_level) + int debug_level, const char *username, const char *password) { fprintf(stderr, "compiled without VNC support, sorry\n"); return NULL; diff --git a/vnc.h b/vnc.h index cf14027..90b0b31 100644 --- a/vnc.h +++ b/vnc.h @@ -8,4 +8,4 @@ #define VNC_FLAG_GRAB_KEYBOARD (1 << 6) GtkWidget *vnc_open(char *hostname, int tcpport, unsigned long flags, - int debug_level); + int debug_level, const char *username, const char *password); -- cgit