--- sourceforge.net/trunk/rdesktop/rdesktop.c 2004/07/30 08:25:54 739 +++ sourceforge.net/trunk/rdesktop/rdesktop.c 2005/08/31 13:00:57 1010 @@ -1,7 +1,7 @@ /* -*- c-basic-offset: 8 -*- rdesktop: A Remote Desktop Protocol client. Entrypoint and utility functions - Copyright (C) Matthew Chapman 1999-2003 + Copyright (C) Matthew Chapman 1999-2005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -30,22 +30,31 @@ #include #include "rdesktop.h" +#ifdef HAVE_LOCALE_H +#include +#endif +#ifdef HAVE_ICONV +#ifdef HAVE_LANGINFO_H +#include +#endif +#endif + #ifdef EGD_SOCKET +#include #include /* socket connect */ #include /* sockaddr_un */ #endif -#ifdef WITH_OPENSSL #include -#else -#include "crypto/md5.h" -#endif char g_title[64] = ""; char g_username[64]; char g_hostname[16]; -char keymapname[16]; +char g_keymapname[PATH_MAX] = ""; int g_keylayout = 0x409; /* Defaults to US keyboard layout */ +int g_keyboard_type = 0x4; /* Defaults to US keyboard layout */ +int g_keyboard_subtype = 0x0; /* Defaults to US keyboard layout */ +int g_keyboard_functionkeys = 0xc; /* Defaults to US keyboard layout */ int g_width = 800; /* width is special: If 0, the geometry will be fetched from @@ -53,6 +62,12 @@ absolute value specifies the percent of the whole screen. */ int g_height = 600; +int g_xpos = 0; +int g_ypos = 0; +int g_pos = 0; /* 0 position unspecified, + 1 specified, + 2 xpos neg, + 4 ypos neg */ extern int g_tcp_port_rdp; int g_server_bpp = 8; int g_win_button_size = 0; /* If zero, disable single app mode */ @@ -63,7 +78,8 @@ BOOL g_bitmap_cache_precache = True; BOOL g_encryption = True; BOOL packet_encryption = True; -BOOL g_desktop_save = True; +BOOL g_desktop_save = True; /* desktop save order */ +BOOL g_polygon_ellipse_orders = True; /* polygon / ellipse orders */ BOOL g_fullscreen = False; BOOL g_grab_keyboard = True; BOOL g_hide_decorations = False; @@ -75,11 +91,23 @@ uint32 g_embed_wnd; uint32 g_rdp5_performanceflags = RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS; +/* Session Directory redirection */ +BOOL g_redirect = False; +char g_redirect_server[64]; +char g_redirect_domain[16]; +char g_redirect_password[64]; +char g_redirect_username[64]; +char g_redirect_cookie[128]; +uint32 g_redirect_flags = 0; #ifdef WITH_RDPSND BOOL g_rdpsnd = False; #endif +#ifdef HAVE_ICONV +char g_codepage[16] = ""; +#endif + extern RDPDR_DEVICE g_rdpdr_device[]; extern uint32 g_num_devices; extern char *g_rdpdr_clientname; @@ -96,7 +124,7 @@ usage(char *program) { fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n"); - fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n"); + fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2005 Matt Chapman.\n"); fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); fprintf(stderr, "Usage: %s [options] server[:port]\n", program); @@ -114,6 +142,9 @@ fprintf(stderr, " -g: desktop geometry (WxH)\n"); fprintf(stderr, " -f: full-screen mode\n"); fprintf(stderr, " -b: force bitmap updates\n"); +#ifdef HAVE_ICONV + fprintf(stderr, " -L: local codepage\n"); +#endif fprintf(stderr, " -B: use BackingStore of X-server (if available)\n"); fprintf(stderr, " -e: disable encryption (French TS)\n"); fprintf(stderr, " -E: disable encryption from client to server\n"); @@ -126,6 +157,7 @@ fprintf(stderr, " -N: enable numlock syncronization\n"); fprintf(stderr, " -X: embed into another window with a given id.\n"); fprintf(stderr, " -a: connection colour depth\n"); + fprintf(stderr, " -z: enable rdp compression\n"); fprintf(stderr, " -x: RDP5 experience (m[odem 28.8], b[roadband], l[an] or hex nr.)\n"); fprintf(stderr, " -P: use persistent bitmap caching\n"); fprintf(stderr, " -r: enable specified device redirection (this flag can be repeated)\n"); @@ -133,8 +165,8 @@ " '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n"); fprintf(stderr, " or COM1=/dev/ttyS0,COM2=/dev/ttyS1\n"); fprintf(stderr, - " '-r disk:A=/mnt/floppy': enable redirection of /mnt/floppy to A:\n"); - fprintf(stderr, " or A=/mnt/floppy,D=/mnt/cdrom'\n"); + " '-r disk:floppy=/mnt/floppy': enable redirection of /mnt/floppy to 'floppy' share\n"); + fprintf(stderr, " or 'floppy=/mnt/floppy,cdrom=/mnt/cdrom'\n"); fprintf(stderr, " '-r clientname=': Set the client name displayed\n"); fprintf(stderr, " for redirected disks\n"); fprintf(stderr, @@ -150,7 +182,7 @@ fprintf(stderr, " -5: use RDP version 5 (default)\n"); } -void +static void print_disconnect_reason(uint16 reason) { char *text; @@ -250,6 +282,12 @@ fprintf(stderr, "disconnect: %s.\n", text); } +static void +rdesktop_reset_state(void) +{ + rdp_reset_state(); +} + static BOOL read_password(char *password, int size) { @@ -349,13 +387,23 @@ uint32 flags, ext_disc_reason = 0; char *p; int c; - + char *locale = NULL; int username_option = 0; + int run_count = 0; /* Session Directory support */ + BOOL continue_connect = True; /* Session Directory support */ +#ifdef HAVE_LOCALE_H + /* Set locale according to environment */ + locale = setlocale(LC_ALL, ""); + if (locale) + { + locale = xstrdup(locale); + } + +#endif flags = RDP_LOGON_NORMAL; prompt_password = False; domain[0] = password[0] = shell[0] = directory[0] = 0; - strcpy(keymapname, "en-us"); g_embed_wnd = 0; g_num_devices = 0; @@ -367,7 +415,7 @@ #endif while ((c = getopt(argc, argv, - VNCOPT "u:d:s:c:p:n:k:g:fbBeEmCDKS:T:NX:a:x:Pr:045h?")) != -1) + VNCOPT "u:L:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1) { switch (c) { @@ -390,6 +438,14 @@ username_option = 1; break; + case 'L': +#ifdef HAVE_ICONV + STRNCPY(g_codepage, optarg, sizeof(g_codepage)); +#else + error("iconv support not available\n"); +#endif + break; + case 'd': STRNCPY(domain, optarg, sizeof(domain)); break; @@ -423,7 +479,7 @@ break; case 'k': - STRNCPY(keymapname, optarg, sizeof(keymapname)); + STRNCPY(g_keymapname, optarg, sizeof(g_keymapname)); break; case 'g': @@ -442,7 +498,7 @@ } if (*p == 'x') - g_height = strtol(p + 1, NULL, 10); + g_height = strtol(p + 1, &p, 10); if (g_height <= 0) { @@ -451,7 +507,22 @@ } if (*p == '%') + { g_width = -g_width; + p++; + } + + if (*p == '+' || *p == '-') + { + g_pos |= (*p == '-') ? 2 : 1; + g_xpos = strtol(p, &p, 10); + + } + if (*p == '+' || *p == '-') + { + g_pos |= (*p == '-') ? 4 : 1; + g_ypos = strtol(p, NULL, 10); + } break; @@ -515,7 +586,7 @@ break; case 'X': - g_embed_wnd = strtol(optarg, NULL, 10); + g_embed_wnd = strtol(optarg, NULL, 0); break; case 'a': @@ -528,19 +599,23 @@ } break; - case 'x': + case 'z': + DEBUG(("rdp compression enabled\n")); + flags |= (RDP_LOGON_COMPRESSION | RDP_LOGON_COMPRESSION2); + break; - if (strncmp("modem", optarg, 1) == 0) + case 'x': + if (str_startswith(optarg, "m")) /* modem */ { g_rdp5_performanceflags = RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS | RDP5_NO_THEMING; } - else if (strncmp("broadband", optarg, 1) == 0) + else if (str_startswith(optarg, "b")) /* broadband */ { g_rdp5_performanceflags = RDP5_NO_WALLPAPER; } - else if (strncmp("lan", optarg, 1) == 0) + else if (str_startswith(optarg, "l")) /* lan */ { g_rdp5_performanceflags = RDP5_DISABLE_NOTHING; } @@ -556,7 +631,7 @@ case 'r': - if (strncmp("sound", optarg, 5) == 0) + if (str_startswith(optarg, "sound")) { optarg += 5; @@ -565,17 +640,17 @@ *optarg++; while ((p = next_arg(optarg, ','))) { - if (strncmp("remote", optarg, 6) == 0) + if (str_startswith(optarg, "remote")) flags |= RDP_LOGON_LEAVE_AUDIO; - if (strncmp("local", optarg, 5) == 0) + if (str_startswith(optarg, "local")) #ifdef WITH_RDPSND g_rdpsnd = True; #else warning("Not compiled with sound support\n"); #endif - if (strncmp("off", optarg, 3) == 0) + if (str_startswith(optarg, "off")) #ifdef WITH_RDPSND g_rdpsnd = False; #else @@ -594,24 +669,24 @@ #endif } } - else if (strncmp("disk", optarg, 4) == 0) + else if (str_startswith(optarg, "disk")) { /* -r disk:h:=/mnt/floppy */ disk_enum_devices(&g_num_devices, optarg + 4); } - else if (strncmp("comport", optarg, 7) == 0) + else if (str_startswith(optarg, "comport")) { serial_enum_devices(&g_num_devices, optarg + 7); } - else if (strncmp("lptport", optarg, 7) == 0) + else if (str_startswith(optarg, "lptport")) { parallel_enum_devices(&g_num_devices, optarg + 7); } - else if (strncmp("printer", optarg, 7) == 0) + else if (str_startswith(optarg, "printer")) { printer_enum_devices(&g_num_devices, optarg + 7); } - else if (strncmp("clientname", optarg, 7) == 0) + else if (str_startswith(optarg, "clientname")) { g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1); strcpy(g_rdpdr_clientname, optarg + 11); @@ -663,6 +738,20 @@ STRNCPY(g_username, pw->pw_name, sizeof(g_username)); } +#ifdef HAVE_ICONV + if (g_codepage[0] == 0) + { + if (setlocale(LC_CTYPE, "")) + { + STRNCPY(g_codepage, nl_langinfo(CODESET), sizeof(g_codepage)); + } + else + { + STRNCPY(g_codepage, DEFAULT_CODEPAGE, sizeof(g_codepage)); + } + } +#endif + if (g_hostname[0] == 0) { if (gethostname(fullhostname, sizeof(fullhostname)) == -1) @@ -678,6 +767,21 @@ STRNCPY(g_hostname, fullhostname, sizeof(g_hostname)); } + if (g_keymapname[0] == 0) + { + if (locale && xkeymap_from_locale(locale)) + { + fprintf(stderr, "Autoselected keyboard map %s\n", g_keymapname); + } + else + { + STRNCPY(g_keymapname, "en-us", sizeof(g_keymapname)); + } + } + if (locale) + xfree(locale); + + if (prompt_password && read_password(password, sizeof(password))) flags |= RDP_LOGON_AUTO; @@ -701,26 +805,59 @@ #endif rdpdr_init(); - if (!rdp_connect(server, flags, domain, password, shell, directory)) - return 1; + while (run_count < 2 && continue_connect) /* add support for Session Directory; only reconnect once */ + { + if (run_count == 0) + { + if (!rdp_connect(server, flags, domain, password, shell, directory)) + return 1; + } + else if (!rdp_reconnect + (server, flags, domain, password, shell, directory, g_redirect_cookie)) + return 1; - /* By setting encryption to False here, we have an encrypted login - packet but unencrypted transfer of other packets */ - if (!packet_encryption) - g_encryption = False; + /* By setting encryption to False here, we have an encrypted login + packet but unencrypted transfer of other packets */ + if (!packet_encryption) + g_encryption = False; - DEBUG(("Connection successful.\n")); - memset(password, 0, sizeof(password)); + DEBUG(("Connection successful.\n")); + memset(password, 0, sizeof(password)); - if (ui_create_window()) - { - rdp_main_loop(&deactivated, &ext_disc_reason); - ui_destroy_window(); + if (run_count == 0) + if (!ui_create_window()) + continue_connect = False; + + if (continue_connect) + rdp_main_loop(&deactivated, &ext_disc_reason); + + DEBUG(("Disconnecting...\n")); + rdp_disconnect(); + + if ((g_redirect == True) && (run_count == 0)) /* Support for Session Directory */ + { + /* reset state of major globals */ + rdesktop_reset_state(); + + STRNCPY(domain, g_redirect_domain, sizeof(domain)); + STRNCPY(g_username, g_redirect_username, sizeof(g_username)); + STRNCPY(password, g_redirect_password, sizeof(password)); + STRNCPY(server, g_redirect_server, sizeof(server)); + flags |= RDP_LOGON_AUTO; + + g_redirect = False; + } + else + { + continue_connect = False; + ui_destroy_window(); + break; + } + + run_count++; } - DEBUG(("Disconnecting...\n")); - rdp_disconnect(); cache_save_state(); ui_deinit(); @@ -847,11 +984,28 @@ return mem; } +/* strdup */ +char * +xstrdup(const char *s) +{ + char *mem = strdup(s); + if (mem == NULL) + { + perror("strdup"); + exit(1); + } + return mem; +} + /* realloc; exit if out of memory */ void * xrealloc(void *oldmem, int size) { - void *mem = realloc(oldmem, size); + void *mem; + + if (size < 1) + size = 1; + mem = realloc(oldmem, size); if (mem == NULL) { error("xrealloc %d\n", size); @@ -1016,6 +1170,13 @@ } +BOOL +str_startswith(const char *s, const char *prefix) +{ + return (strncmp(s, prefix, strlen(prefix)) == 0); +} + + /* not all clibs got ltoa */ #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)