2003-03-27 14:18:13 +01:00
|
|
|
/* -*- c-basic-offset: 8 -*-
|
2000-08-15 12:23:24 +02:00
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Entrypoint and utility functions
|
2003-01-30 12:59:27 +01:00
|
|
|
Copyright (C) Matthew Chapman 1999-2003
|
2002-08-24 22:04:56 +02:00
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
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
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2002-08-24 22:04:56 +02:00
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2002-08-24 22:04:56 +02:00
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
#include <stdarg.h> /* va_list va_start va_end */
|
2001-01-06 04:12:10 +01:00
|
|
|
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
|
|
|
|
#include <fcntl.h> /* open */
|
|
|
|
#include <pwd.h> /* getpwuid */
|
2002-10-04 16:28:14 +02:00
|
|
|
#include <termios.h> /* tcgetattr tcsetattr */
|
2001-01-06 04:12:10 +01:00
|
|
|
#include <sys/stat.h> /* stat */
|
|
|
|
#include <sys/time.h> /* gettimeofday */
|
|
|
|
#include <sys/times.h> /* times */
|
2003-02-11 12:31:04 +01:00
|
|
|
#include <errno.h>
|
2000-08-15 12:23:24 +02:00
|
|
|
#include "rdesktop.h"
|
|
|
|
|
2002-10-10 09:25:31 +02:00
|
|
|
#ifdef EGD_SOCKET
|
|
|
|
#include <sys/socket.h> /* socket connect */
|
|
|
|
#include <sys/un.h> /* sockaddr_un */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
#include <openssl/md5.h>
|
|
|
|
#else
|
|
|
|
#include "crypto/md5.h"
|
|
|
|
#endif
|
|
|
|
|
2003-10-06 11:37:24 +02:00
|
|
|
char g_title[64] = "";
|
|
|
|
char g_username[64];
|
2000-08-15 12:23:24 +02:00
|
|
|
char hostname[16];
|
2002-04-04 14:04:33 +02:00
|
|
|
char keymapname[16];
|
2002-07-18 20:28:12 +02:00
|
|
|
int keylayout = 0x409; /* Defaults to US keyboard layout */
|
2003-08-22 01:23:15 +02:00
|
|
|
int g_width = 800; /* If width or height are reset to zero, the geometry will
|
2002-11-18 19:12:49 +01:00
|
|
|
be fetched from _NET_WORKAREA */
|
2003-08-22 01:23:15 +02:00
|
|
|
int g_height = 600;
|
2002-07-14 02:34:21 +02:00
|
|
|
int tcp_port_rdp = TCP_PORT_RDP;
|
2003-08-01 19:01:58 +02:00
|
|
|
int g_server_bpp = 8;
|
2003-08-28 00:51:33 +02:00
|
|
|
int g_win_button_size = 0; /* If zero, disable single app mode */
|
2003-07-28 23:41:12 +02:00
|
|
|
BOOL g_bitmap_compression = True;
|
2003-08-22 01:23:15 +02:00
|
|
|
BOOL g_sendmotion = True;
|
2003-07-28 23:41:12 +02:00
|
|
|
BOOL g_orders = True;
|
|
|
|
BOOL g_encryption = True;
|
2003-06-19 13:51:18 +02:00
|
|
|
BOOL packet_encryption = True;
|
2003-08-01 19:01:58 +02:00
|
|
|
BOOL g_desktop_save = True;
|
2003-08-22 01:23:15 +02:00
|
|
|
BOOL g_fullscreen = False;
|
2003-08-28 00:51:33 +02:00
|
|
|
BOOL g_grab_keyboard = True;
|
|
|
|
BOOL g_hide_decorations = False;
|
2003-09-02 11:32:13 +02:00
|
|
|
BOOL g_use_rdp5 = True;
|
2003-08-28 00:51:33 +02:00
|
|
|
extern BOOL g_owncolmap;
|
2000-08-15 12:23:24 +02:00
|
|
|
|
2003-02-20 13:14:13 +01:00
|
|
|
#ifdef RDP2VNC
|
|
|
|
extern int rfb_port;
|
|
|
|
extern int defer_time;
|
|
|
|
void
|
|
|
|
rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
|
|
|
|
char *shell, char *directory);
|
|
|
|
#endif
|
2000-08-15 12:23:24 +02:00
|
|
|
/* Display usage information */
|
2001-01-06 04:47:04 +01:00
|
|
|
static void
|
|
|
|
usage(char *program)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2002-09-14 13:54:11 +02:00
|
|
|
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
|
2003-01-30 12:59:27 +01:00
|
|
|
fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
|
2002-09-14 13:54:11 +02:00
|
|
|
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
|
|
|
|
|
2002-10-11 06:00:32 +02:00
|
|
|
fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
|
2003-02-20 13:14:13 +01:00
|
|
|
#ifdef RDP2VNC
|
|
|
|
fprintf(stderr, " -V: vnc port\n");
|
2003-09-22 14:33:21 +02:00
|
|
|
fprintf(stderr, " -Q: defer time (ms)\n");
|
2003-02-20 13:14:13 +01:00
|
|
|
#endif
|
2002-09-01 14:03:33 +02:00
|
|
|
fprintf(stderr, " -u: user name\n");
|
|
|
|
fprintf(stderr, " -d: domain\n");
|
|
|
|
fprintf(stderr, " -s: shell\n");
|
2003-02-18 14:07:29 +01:00
|
|
|
fprintf(stderr, " -S: caption button size (single application mode)\n");
|
2002-09-01 14:03:33 +02:00
|
|
|
fprintf(stderr, " -c: working directory\n");
|
2002-10-04 16:28:14 +02:00
|
|
|
fprintf(stderr, " -p: password (- to prompt)\n");
|
2002-09-01 14:03:33 +02:00
|
|
|
fprintf(stderr, " -n: client hostname\n");
|
2002-10-11 07:13:19 +02:00
|
|
|
fprintf(stderr, " -k: keyboard layout on terminal server (us,sv,gr,etc.)\n");
|
2002-09-01 14:03:33 +02:00
|
|
|
fprintf(stderr, " -g: desktop geometry (WxH)\n");
|
|
|
|
fprintf(stderr, " -f: full-screen mode\n");
|
|
|
|
fprintf(stderr, " -b: force bitmap updates\n");
|
|
|
|
fprintf(stderr, " -e: disable encryption (French TS)\n");
|
2003-09-22 14:33:21 +02:00
|
|
|
fprintf(stderr, " -E: disable encryption from client to server\n");
|
2002-09-01 14:03:33 +02:00
|
|
|
fprintf(stderr, " -m: do not send motion events\n");
|
2002-11-26 11:09:14 +01:00
|
|
|
fprintf(stderr, " -C: use private colour map\n");
|
2002-09-01 14:03:33 +02:00
|
|
|
fprintf(stderr, " -K: keep window manager key bindings\n");
|
2002-10-11 07:13:19 +02:00
|
|
|
fprintf(stderr, " -T: window title\n");
|
2002-11-18 16:37:20 +01:00
|
|
|
fprintf(stderr, " -D: hide window manager decorations\n");
|
2003-02-04 06:32:13 +01:00
|
|
|
fprintf(stderr, " -a: server bpp\n");
|
2003-09-02 11:32:13 +02:00
|
|
|
fprintf(stderr, " -4: Use RDP version 4\n");
|
|
|
|
fprintf(stderr, " -5: Use RDP version 5 (default)\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
2002-10-04 16:28:14 +02:00
|
|
|
static BOOL
|
|
|
|
read_password(char *password, int size)
|
|
|
|
{
|
|
|
|
struct termios tios;
|
|
|
|
BOOL ret = False;
|
|
|
|
int istty = 0;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (tcgetattr(STDIN_FILENO, &tios) == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Password: ");
|
|
|
|
tios.c_lflag &= ~ECHO;
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &tios);
|
|
|
|
istty = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fgets(password, size, stdin) != NULL)
|
|
|
|
{
|
|
|
|
ret = True;
|
|
|
|
|
|
|
|
/* strip final newline */
|
|
|
|
p = strchr(password, '\n');
|
|
|
|
if (p != NULL)
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (istty)
|
|
|
|
{
|
|
|
|
tios.c_lflag |= ECHO;
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &tios);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-08-17 09:31:36 +02:00
|
|
|
static void
|
|
|
|
parse_server_and_port(char *server)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
#ifdef IPv6
|
|
|
|
int addr_colons;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef IPv6
|
|
|
|
p = server;
|
|
|
|
addr_colons = 0;
|
|
|
|
while (*p)
|
|
|
|
if (*p++ == ':')
|
|
|
|
addr_colons++;
|
|
|
|
if (addr_colons >= 2)
|
|
|
|
{
|
|
|
|
/* numeric IPv6 style address format - [1:2:3::4]:port */
|
|
|
|
p = strchr(server, ']');
|
|
|
|
if (*server == '[' && p != NULL)
|
|
|
|
{
|
|
|
|
if (*(p + 1) == ':' && *(p + 2) != '\0')
|
|
|
|
tcp_port_rdp = strtol(p + 2, NULL, 10);
|
|
|
|
/* remove the port number and brackets from the address */
|
|
|
|
*p = '\0';
|
|
|
|
strncpy(server, server + 1, strlen(server));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
|
|
|
|
p = strchr(server, ':');
|
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
tcp_port_rdp = strtol(p + 1, NULL, 10);
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* no IPv6 support */
|
|
|
|
p = strchr(server, ':');
|
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
tcp_port_rdp = strtol(p + 1, NULL, 10);
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
#endif /* IPv6 */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
/* Client program */
|
2001-01-06 04:47:04 +01:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2002-10-11 06:00:32 +02:00
|
|
|
char server[64];
|
2001-09-14 15:51:38 +02:00
|
|
|
char fullhostname[64];
|
2000-10-16 10:44:48 +02:00
|
|
|
char domain[16];
|
2003-10-06 11:37:24 +02:00
|
|
|
char password[64];
|
2002-10-23 16:55:52 +02:00
|
|
|
char shell[128];
|
2000-10-16 10:44:48 +02:00
|
|
|
char directory[32];
|
2003-06-19 09:29:53 +02:00
|
|
|
BOOL prompt_password, rdp_retval = False;
|
2001-09-14 15:51:38 +02:00
|
|
|
struct passwd *pw;
|
|
|
|
uint32 flags;
|
2002-10-11 06:00:32 +02:00
|
|
|
char *p;
|
2000-08-15 12:23:24 +02:00
|
|
|
int c;
|
2003-01-16 14:48:02 +01:00
|
|
|
int username_option = 0;
|
2000-08-15 12:23:24 +02:00
|
|
|
|
2000-10-16 10:44:48 +02:00
|
|
|
flags = RDP_LOGON_NORMAL;
|
2002-10-04 16:28:14 +02:00
|
|
|
prompt_password = False;
|
2000-10-16 10:44:48 +02:00
|
|
|
domain[0] = password[0] = shell[0] = directory[0] = 0;
|
2002-10-24 10:21:33 +02:00
|
|
|
strcpy(keymapname, "en-us");
|
2000-10-16 10:44:48 +02:00
|
|
|
|
2003-02-20 13:14:13 +01:00
|
|
|
#ifdef RDP2VNC
|
2003-09-22 14:33:21 +02:00
|
|
|
#define VNCOPT "V:Q:"
|
2003-02-20 13:14:13 +01:00
|
|
|
#else
|
|
|
|
#define VNCOPT
|
|
|
|
#endif
|
|
|
|
|
2003-06-19 13:51:18 +02:00
|
|
|
while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbeEmCKT:Dh?54")) != -1)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2003-02-20 13:14:13 +01:00
|
|
|
#ifdef RDP2VNC
|
|
|
|
case 'V':
|
|
|
|
rfb_port = strtol(optarg, NULL, 10);
|
|
|
|
if (rfb_port < 100)
|
|
|
|
rfb_port += 5900;
|
|
|
|
break;
|
|
|
|
|
2003-09-22 14:33:21 +02:00
|
|
|
case 'Q':
|
2003-02-20 13:14:13 +01:00
|
|
|
defer_time = strtol(optarg, NULL, 10);
|
|
|
|
if (defer_time < 0)
|
|
|
|
defer_time = 0;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
case 'u':
|
2003-07-28 23:41:12 +02:00
|
|
|
STRNCPY(g_username, optarg, sizeof(g_username));
|
2003-01-16 14:48:02 +01:00
|
|
|
username_option = 1;
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
2000-10-16 10:44:48 +02:00
|
|
|
case 'd':
|
2001-09-14 15:51:38 +02:00
|
|
|
STRNCPY(domain, optarg, sizeof(domain));
|
2000-10-16 10:44:48 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
2001-09-14 15:51:38 +02:00
|
|
|
STRNCPY(shell, optarg, sizeof(shell));
|
2000-10-16 10:44:48 +02:00
|
|
|
break;
|
|
|
|
|
2003-02-18 14:03:51 +01:00
|
|
|
case 'S':
|
|
|
|
if (!strcmp(optarg, "standard"))
|
|
|
|
{
|
2003-08-28 00:51:33 +02:00
|
|
|
g_win_button_size = 18;
|
2003-02-18 14:03:51 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-08-28 00:51:33 +02:00
|
|
|
g_win_button_size = strtol(optarg, &p, 10);
|
2003-02-18 14:03:51 +01:00
|
|
|
|
|
|
|
if (*p)
|
|
|
|
{
|
|
|
|
error("invalid button size\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2000-10-16 10:44:48 +02:00
|
|
|
case 'c':
|
2001-09-14 15:51:38 +02:00
|
|
|
STRNCPY(directory, optarg, sizeof(directory));
|
2000-10-16 10:44:48 +02:00
|
|
|
break;
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
case 'p':
|
2002-10-04 16:28:14 +02:00
|
|
|
if ((optarg[0] == '-') && (optarg[1] == 0))
|
|
|
|
{
|
|
|
|
prompt_password = True;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
STRNCPY(password, optarg, sizeof(password));
|
|
|
|
flags |= RDP_LOGON_AUTO;
|
2002-10-04 16:28:14 +02:00
|
|
|
|
|
|
|
/* try to overwrite argument so it won't appear in ps */
|
2002-09-17 11:20:13 +02:00
|
|
|
p = optarg;
|
|
|
|
while (*p)
|
|
|
|
*(p++) = 'X';
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
2001-09-14 15:51:38 +02:00
|
|
|
|
|
|
|
case 'n':
|
|
|
|
STRNCPY(hostname, optarg, sizeof(hostname));
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'k':
|
2002-07-30 09:18:48 +02:00
|
|
|
STRNCPY(keymapname, optarg, sizeof(keymapname));
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
case 'g':
|
2002-11-18 19:12:49 +01:00
|
|
|
if (!strcmp(optarg, "workarea"))
|
|
|
|
{
|
2003-08-22 01:23:15 +02:00
|
|
|
g_width = g_height = 0;
|
2002-11-18 19:12:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-08-22 01:23:15 +02:00
|
|
|
g_width = strtol(optarg, &p, 10);
|
2001-09-14 15:51:38 +02:00
|
|
|
if (*p == 'x')
|
2003-08-22 01:23:15 +02:00
|
|
|
g_height = strtol(p + 1, NULL, 10);
|
2001-09-14 15:51:38 +02:00
|
|
|
|
2003-08-22 01:23:15 +02:00
|
|
|
if ((g_width == 0) || (g_height == 0))
|
2001-09-14 15:51:38 +02:00
|
|
|
{
|
|
|
|
error("invalid geometry\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2003-08-22 01:23:15 +02:00
|
|
|
g_fullscreen = True;
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
2003-07-28 23:41:12 +02:00
|
|
|
g_orders = False;
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
case 'e':
|
2003-07-28 23:41:12 +02:00
|
|
|
g_encryption = False;
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
2003-07-09 11:18:20 +02:00
|
|
|
case 'E':
|
2003-06-19 13:51:18 +02:00
|
|
|
packet_encryption = False;
|
|
|
|
break;
|
2001-09-14 15:51:38 +02:00
|
|
|
case 'm':
|
2003-08-22 01:23:15 +02:00
|
|
|
g_sendmotion = False;
|
2001-06-20 15:54:48 +02:00
|
|
|
break;
|
2001-09-14 05:38:39 +02:00
|
|
|
|
2002-11-26 11:09:14 +01:00
|
|
|
case 'C':
|
2003-08-28 00:51:33 +02:00
|
|
|
g_owncolmap = True;
|
2002-11-26 11:09:14 +01:00
|
|
|
break;
|
|
|
|
|
2002-07-29 22:16:22 +02:00
|
|
|
case 'K':
|
2003-08-28 00:51:33 +02:00
|
|
|
g_grab_keyboard = False;
|
2002-07-29 22:16:22 +02:00
|
|
|
break;
|
|
|
|
|
2002-10-11 07:13:19 +02:00
|
|
|
case 'T':
|
2003-08-28 00:51:33 +02:00
|
|
|
STRNCPY(g_title, optarg, sizeof(g_title));
|
2002-09-01 13:27:58 +02:00
|
|
|
break;
|
|
|
|
|
2002-11-18 16:37:20 +01:00
|
|
|
case 'D':
|
2003-08-28 00:51:33 +02:00
|
|
|
g_hide_decorations = True;
|
2002-11-18 16:37:20 +01:00
|
|
|
break;
|
|
|
|
|
2003-02-04 06:32:13 +01:00
|
|
|
case 'a':
|
2003-08-01 19:01:58 +02:00
|
|
|
g_server_bpp = strtol(optarg, NULL, 10);
|
|
|
|
if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
|
|
|
|
&& g_server_bpp != 24)
|
2003-02-04 06:32:13 +01:00
|
|
|
{
|
|
|
|
error("invalid server bpp\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-09-02 11:32:13 +02:00
|
|
|
case '4':
|
|
|
|
g_use_rdp5 = False;
|
|
|
|
break;
|
|
|
|
|
2003-03-27 14:18:13 +01:00
|
|
|
case '5':
|
2003-08-01 19:01:58 +02:00
|
|
|
g_use_rdp5 = True;
|
2003-03-27 14:18:13 +01:00
|
|
|
break;
|
2003-09-02 11:32:13 +02:00
|
|
|
|
2001-06-20 15:54:48 +02:00
|
|
|
case 'h':
|
2000-08-15 12:23:24 +02:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind < 1)
|
|
|
|
{
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-10-11 06:00:32 +02:00
|
|
|
STRNCPY(server, argv[optind], sizeof(server));
|
2003-08-17 09:31:36 +02:00
|
|
|
parse_server_and_port(server);
|
2000-08-15 12:23:24 +02:00
|
|
|
|
2003-01-16 14:48:02 +01:00
|
|
|
if (!username_option)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
pw = getpwuid(getuid());
|
|
|
|
if ((pw == NULL) || (pw->pw_name == NULL))
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("could not determine username, use -u\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-07-28 23:41:12 +02:00
|
|
|
STRNCPY(g_username, pw->pw_name, sizeof(g_username));
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hostname[0] == 0)
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("could not determine local hostname, use -n\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2001-09-14 15:51:38 +02:00
|
|
|
|
|
|
|
p = strchr(fullhostname, '.');
|
|
|
|
if (p != NULL)
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
STRNCPY(hostname, fullhostname, sizeof(hostname));
|
|
|
|
}
|
|
|
|
|
2002-10-04 16:28:14 +02:00
|
|
|
if (prompt_password && read_password(password, sizeof(password)))
|
|
|
|
flags |= RDP_LOGON_AUTO;
|
2001-09-14 15:51:38 +02:00
|
|
|
|
2003-08-28 00:51:33 +02:00
|
|
|
if (g_title[0] == 0)
|
2002-09-01 13:27:58 +02:00
|
|
|
{
|
2003-08-28 00:51:33 +02:00
|
|
|
strcpy(g_title, "rdesktop - ");
|
|
|
|
strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
|
2002-09-01 13:27:58 +02:00
|
|
|
}
|
2000-08-15 14:01:01 +02:00
|
|
|
|
2003-02-20 13:14:13 +01:00
|
|
|
#ifdef RDP2VNC
|
|
|
|
rdp2vnc_connect(server, flags, domain, password, shell, directory);
|
2003-06-19 09:29:53 +02:00
|
|
|
return 0;
|
2003-02-20 13:14:13 +01:00
|
|
|
#else
|
|
|
|
|
2002-07-30 09:18:48 +02:00
|
|
|
if (!ui_init())
|
|
|
|
return 1;
|
2002-07-18 20:28:12 +02:00
|
|
|
|
2003-09-30 11:11:08 +02:00
|
|
|
#ifdef WITH_RDPSND
|
|
|
|
rdpsnd_init();
|
|
|
|
#endif
|
2003-07-01 11:31:25 +02:00
|
|
|
/* rdpdr_init(); */
|
2003-06-06 13:11:20 +02:00
|
|
|
|
2002-05-28 13:48:55 +02:00
|
|
|
if (!rdp_connect(server, flags, domain, password, shell, directory))
|
|
|
|
return 1;
|
|
|
|
|
2003-06-19 13:51:18 +02:00
|
|
|
/* By setting encryption to False here, we have an encrypted login
|
|
|
|
packet but unencrypted transfer of other packets */
|
2003-07-09 11:18:20 +02:00
|
|
|
if (!packet_encryption)
|
2003-07-28 23:41:12 +02:00
|
|
|
g_encryption = False;
|
2003-06-19 13:51:18 +02:00
|
|
|
|
|
|
|
|
2002-09-14 13:54:11 +02:00
|
|
|
DEBUG(("Connection successful.\n"));
|
2002-10-04 16:28:14 +02:00
|
|
|
memset(password, 0, sizeof(password));
|
2002-05-28 13:48:55 +02:00
|
|
|
|
2002-08-24 22:04:56 +02:00
|
|
|
if (ui_create_window())
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2003-06-19 09:29:53 +02:00
|
|
|
rdp_retval = rdp_main_loop();
|
2000-08-15 12:23:24 +02:00
|
|
|
ui_destroy_window();
|
|
|
|
}
|
|
|
|
|
2002-09-14 13:54:11 +02:00
|
|
|
DEBUG(("Disconnecting...\n"));
|
2002-05-28 13:48:55 +02:00
|
|
|
rdp_disconnect();
|
2002-09-24 08:09:09 +02:00
|
|
|
ui_deinit();
|
2003-02-20 13:14:13 +01:00
|
|
|
|
2003-07-09 11:18:20 +02:00
|
|
|
if (True == rdp_retval)
|
2003-06-19 09:29:53 +02:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 2;
|
|
|
|
|
2003-02-20 13:14:13 +01:00
|
|
|
#endif
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
2002-10-10 09:25:31 +02:00
|
|
|
#ifdef EGD_SOCKET
|
|
|
|
/* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
|
|
|
|
static BOOL
|
|
|
|
generate_random_egd(uint8 * buf)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
BOOL ret = False;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1)
|
|
|
|
return False;
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
|
2002-11-18 16:30:12 +01:00
|
|
|
if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
|
2002-10-10 09:25:31 +02:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* PRNGD and EGD use a simple communications protocol */
|
2002-11-18 16:30:12 +01:00
|
|
|
buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
|
|
|
|
buf[1] = 32; /* Number of requested random bytes */
|
2002-10-10 09:25:31 +02:00
|
|
|
if (write(fd, buf, 2) != 2)
|
|
|
|
goto err;
|
|
|
|
|
2002-11-18 16:30:12 +01:00
|
|
|
if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
|
2002-10-10 09:25:31 +02:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (read(fd, buf, 32) != 32)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = True;
|
|
|
|
|
2002-11-18 16:30:12 +01:00
|
|
|
err:
|
2002-10-10 09:25:31 +02:00
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
/* Generate a 32-byte random for the secure transport code. */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
2002-07-18 18:38:31 +02:00
|
|
|
generate_random(uint8 * random)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
2000-10-17 08:12:31 +02:00
|
|
|
struct tms tmsbuf;
|
2002-10-10 09:25:31 +02:00
|
|
|
MD5_CTX md5;
|
|
|
|
uint32 *r;
|
|
|
|
int fd, n;
|
2000-08-15 12:23:24 +02:00
|
|
|
|
2002-10-10 09:25:31 +02:00
|
|
|
/* If we have a kernel random device, try that first */
|
2001-09-14 15:51:38 +02:00
|
|
|
if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
|
|
|
|
|| ((fd = open("/dev/random", O_RDONLY)) != -1))
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2002-10-10 09:25:31 +02:00
|
|
|
n = read(fd, random, 32);
|
2000-08-15 12:23:24 +02:00
|
|
|
close(fd);
|
2002-10-10 09:25:31 +02:00
|
|
|
if (n == 32)
|
|
|
|
return;
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
2002-10-10 09:25:31 +02:00
|
|
|
#ifdef EGD_SOCKET
|
|
|
|
/* As a second preference use an EGD */
|
|
|
|
if (generate_random_egd(random))
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
/* Otherwise use whatever entropy we can gather - ideas welcome. */
|
2002-11-18 16:30:12 +01:00
|
|
|
r = (uint32 *) random;
|
2000-08-15 12:23:24 +02:00
|
|
|
r[0] = (getpid()) | (getppid() << 16);
|
|
|
|
r[1] = (getuid()) | (getgid() << 16);
|
2001-01-06 04:12:10 +01:00
|
|
|
r[2] = times(&tmsbuf); /* system uptime (clocks) */
|
|
|
|
gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
|
2000-08-15 12:23:24 +02:00
|
|
|
stat("/tmp", &st);
|
|
|
|
r[5] = st.st_atime;
|
|
|
|
r[6] = st.st_mtime;
|
|
|
|
r[7] = st.st_ctime;
|
2002-10-10 09:25:31 +02:00
|
|
|
|
|
|
|
/* Hash both halves with MD5 to obscure possible patterns */
|
|
|
|
MD5_Init(&md5);
|
2002-11-18 16:30:12 +01:00
|
|
|
MD5_Update(&md5, random, 16);
|
2002-10-10 09:25:31 +02:00
|
|
|
MD5_Final(random, &md5);
|
2002-11-18 16:30:12 +01:00
|
|
|
MD5_Update(&md5, random + 16, 16);
|
|
|
|
MD5_Final(random + 16, &md5);
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* malloc; exit if out of memory */
|
2001-01-06 04:47:04 +01:00
|
|
|
void *
|
|
|
|
xmalloc(int size)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
void *mem = malloc(size);
|
|
|
|
if (mem == NULL)
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("xmalloc %d\n", size);
|
2000-08-15 12:23:24 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* realloc; exit if out of memory */
|
2001-01-06 04:47:04 +01:00
|
|
|
void *
|
|
|
|
xrealloc(void *oldmem, int size)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
void *mem = realloc(oldmem, size);
|
|
|
|
if (mem == NULL)
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("xrealloc %d\n", size);
|
2000-08-15 12:23:24 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
|
|
|
xfree(void *mem)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
free(mem);
|
|
|
|
}
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
/* report an error */
|
|
|
|
void
|
|
|
|
error(char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fprintf(stderr, "ERROR: ");
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2003-01-28 13:27:28 +01:00
|
|
|
/* report a warning */
|
|
|
|
void
|
|
|
|
warning(char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fprintf(stderr, "WARNING: ");
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
/* report an unimplemented protocol feature */
|
|
|
|
void
|
|
|
|
unimpl(char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fprintf(stderr, "NOT IMPLEMENTED: ");
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* produce a hex dump */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
2003-05-19 23:36:33 +02:00
|
|
|
hexdump(unsigned char *p, int len)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
unsigned char *line = p;
|
2003-05-19 23:36:33 +02:00
|
|
|
int i, thisline, offset = 0;
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
while (offset < len)
|
|
|
|
{
|
2002-09-17 09:27:49 +02:00
|
|
|
printf("%04x ", offset);
|
2000-08-15 12:23:24 +02:00
|
|
|
thisline = len - offset;
|
|
|
|
if (thisline > 16)
|
|
|
|
thisline = 16;
|
|
|
|
|
|
|
|
for (i = 0; i < thisline; i++)
|
2002-09-17 09:27:49 +02:00
|
|
|
printf("%02x ", line[i]);
|
2001-09-14 15:51:38 +02:00
|
|
|
|
|
|
|
for (; i < 16; i++)
|
2002-09-17 09:27:49 +02:00
|
|
|
printf(" ");
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
for (i = 0; i < thisline; i++)
|
2002-09-17 09:27:49 +02:00
|
|
|
printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
|
2000-08-15 12:23:24 +02:00
|
|
|
|
2002-09-17 09:27:49 +02:00
|
|
|
printf("\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
offset += thisline;
|
|
|
|
line += thisline;
|
|
|
|
}
|
|
|
|
}
|
2003-02-11 12:31:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
load_licence(unsigned char **data)
|
|
|
|
{
|
2003-04-16 15:04:15 +02:00
|
|
|
char *home, *path;
|
2003-02-11 12:31:04 +01:00
|
|
|
struct stat st;
|
2003-04-16 15:04:15 +02:00
|
|
|
int fd, length;
|
2003-02-11 12:31:04 +01:00
|
|
|
|
|
|
|
home = getenv("HOME");
|
|
|
|
if (home == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2003-06-06 13:11:20 +02:00
|
|
|
path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
|
2003-02-11 12:31:04 +01:00
|
|
|
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (fstat(fd, &st))
|
|
|
|
return -1;
|
|
|
|
|
2003-06-06 13:11:20 +02:00
|
|
|
*data = (uint8 *) xmalloc(st.st_size);
|
2003-04-16 15:04:15 +02:00
|
|
|
length = read(fd, *data, st.st_size);
|
|
|
|
close(fd);
|
|
|
|
xfree(path);
|
|
|
|
return length;
|
2003-02-11 12:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
save_licence(unsigned char *data, int length)
|
|
|
|
{
|
2003-04-16 15:04:15 +02:00
|
|
|
char *home, *path, *tmppath;
|
|
|
|
int fd;
|
2003-02-11 12:31:04 +01:00
|
|
|
|
|
|
|
home = getenv("HOME");
|
|
|
|
if (home == NULL)
|
|
|
|
return;
|
|
|
|
|
2003-06-06 13:11:20 +02:00
|
|
|
path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
|
2003-02-11 12:31:04 +01:00
|
|
|
|
2003-04-16 15:04:15 +02:00
|
|
|
sprintf(path, "%s/.rdesktop", home);
|
|
|
|
if ((mkdir(path, 0700) == -1) && errno != EEXIST)
|
2003-02-11 12:31:04 +01:00
|
|
|
{
|
2003-04-16 15:04:15 +02:00
|
|
|
perror(path);
|
|
|
|
return;
|
2003-02-11 12:31:04 +01:00
|
|
|
}
|
|
|
|
|
2003-04-16 15:04:15 +02:00
|
|
|
/* write licence to licence.hostname.new, then atomically rename to licence.hostname */
|
2003-02-11 12:31:04 +01:00
|
|
|
|
2003-04-16 15:04:15 +02:00
|
|
|
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
|
2003-06-06 13:11:20 +02:00
|
|
|
tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
|
2003-04-16 15:04:15 +02:00
|
|
|
strcpy(tmppath, path);
|
|
|
|
strcat(tmppath, ".new");
|
2003-02-11 12:31:04 +01:00
|
|
|
|
2003-06-06 13:11:20 +02:00
|
|
|
fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
2003-04-16 15:04:15 +02:00
|
|
|
if (fd == -1)
|
2003-02-11 12:31:04 +01:00
|
|
|
{
|
2003-04-16 15:04:15 +02:00
|
|
|
perror(tmppath);
|
|
|
|
return;
|
2003-02-11 12:31:04 +01:00
|
|
|
}
|
2003-04-16 15:04:15 +02:00
|
|
|
|
|
|
|
if (write(fd, data, length) != length)
|
2003-02-11 12:31:04 +01:00
|
|
|
{
|
2003-04-16 15:04:15 +02:00
|
|
|
perror(tmppath);
|
|
|
|
unlink(tmppath);
|
2003-02-11 12:31:04 +01:00
|
|
|
}
|
2003-04-16 15:04:15 +02:00
|
|
|
else if (rename(tmppath, path) == -1)
|
2003-02-11 12:31:04 +01:00
|
|
|
{
|
2003-04-16 15:04:15 +02:00
|
|
|
perror(path);
|
|
|
|
unlink(tmppath);
|
2003-02-11 12:31:04 +01:00
|
|
|
}
|
|
|
|
|
2003-04-16 15:04:15 +02:00
|
|
|
close(fd);
|
|
|
|
xfree(tmppath);
|
|
|
|
xfree(path);
|
2003-02-11 12:31:04 +01:00
|
|
|
}
|