2000-08-15 12:23:24 +02:00
|
|
|
/*
|
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Entrypoint and utility functions
|
|
|
|
Copyright (C) Matthew Chapman 1999-2000
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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-01-06 04:12:10 +01:00
|
|
|
#include <stdlib.h> /* malloc realloc free */
|
|
|
|
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
|
|
|
|
#include <fcntl.h> /* open */
|
|
|
|
#include <pwd.h> /* getpwuid */
|
|
|
|
#include <sys/stat.h> /* stat */
|
|
|
|
#include <sys/time.h> /* gettimeofday */
|
|
|
|
#include <sys/times.h> /* times */
|
2000-08-15 12:23:24 +02:00
|
|
|
#include "rdesktop.h"
|
|
|
|
|
|
|
|
char username[16];
|
|
|
|
char hostname[16];
|
|
|
|
int width = 800;
|
|
|
|
int height = 600;
|
|
|
|
int keylayout = 0x409;
|
2001-06-20 15:54:48 +02:00
|
|
|
BOOL bitmap_compression = True;
|
2001-09-14 05:38:39 +02:00
|
|
|
BOOL sendmotion = True;
|
2000-08-15 12:23:24 +02:00
|
|
|
BOOL orders = True;
|
|
|
|
BOOL licence = True;
|
2001-06-20 15:54:48 +02:00
|
|
|
BOOL use_encryption = True;
|
|
|
|
BOOL desktop_save = True;
|
|
|
|
BOOL fullscreen = False;
|
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
|
|
|
{
|
|
|
|
STATUS("Usage: %s [options] server\n", program);
|
|
|
|
STATUS(" -u: user name\n");
|
2000-10-16 10:44:48 +02:00
|
|
|
STATUS(" -d: domain\n");
|
|
|
|
STATUS(" -s: shell\n");
|
|
|
|
STATUS(" -c: working directory\n");
|
|
|
|
STATUS(" -p: password (autologon)\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
STATUS(" -n: client hostname\n");
|
|
|
|
STATUS(" -w: desktop width\n");
|
|
|
|
STATUS(" -h: desktop height\n");
|
|
|
|
STATUS(" -k: keyboard layout (hex)\n");
|
|
|
|
STATUS(" -b: force bitmap updates\n");
|
2000-10-16 10:44:48 +02:00
|
|
|
STATUS(" -m: do not send motion events\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
STATUS(" -l: do not request licence\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Client program */
|
2001-01-06 04:47:04 +01:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
char *server;
|
2000-10-16 10:44:48 +02:00
|
|
|
uint32 flags;
|
|
|
|
char domain[16];
|
|
|
|
char password[16];
|
|
|
|
char shell[32];
|
|
|
|
char directory[32];
|
2000-08-15 12:23:24 +02:00
|
|
|
char title[32];
|
|
|
|
int c;
|
|
|
|
|
|
|
|
STATUS("rdesktop: A Remote Desktop Protocol client.\n");
|
2001-01-06 04:12:10 +01:00
|
|
|
STATUS("Version " VERSION
|
|
|
|
". Copyright (C) 1999-2000 Matt Chapman.\n");
|
2000-09-28 17:54:11 +02:00
|
|
|
STATUS("See http://www.rdesktop.org/ for more information.\n\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
|
2000-10-16 10:44:48 +02:00
|
|
|
flags = RDP_LOGON_NORMAL;
|
|
|
|
domain[0] = password[0] = shell[0] = directory[0] = 0;
|
|
|
|
|
2001-09-14 05:38:39 +02:00
|
|
|
while ((c = getopt(argc, argv, "u:d:s:c:p:n:g:k:mbleKFVh?")) != -1)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'u':
|
|
|
|
strncpy(username, optarg, sizeof(username));
|
|
|
|
break;
|
|
|
|
|
2000-10-16 10:44:48 +02:00
|
|
|
case 'd':
|
|
|
|
strncpy(domain, optarg, sizeof(domain));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
flags |= RDP_LOGON_AUTO;
|
|
|
|
strncpy(password, optarg, sizeof(password));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
strncpy(shell, optarg, sizeof(shell));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
strncpy(directory, optarg, sizeof(directory));
|
|
|
|
break;
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
case 'n':
|
|
|
|
strncpy(hostname, optarg, sizeof(hostname));
|
|
|
|
break;
|
2001-06-20 15:54:48 +02:00
|
|
|
case 'g':
|
|
|
|
{
|
|
|
|
char *tgem = 0;
|
|
|
|
width = strtol(optarg, NULL, 10);
|
|
|
|
tgem = strchr(optarg, 'x');
|
|
|
|
if ((tgem == 0) || (strlen(tgem) < 2))
|
|
|
|
{
|
|
|
|
ERROR
|
|
|
|
("-g: invalid parameter. Syntax example: -g 1024x768\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
height = strtol(tgem + 1, NULL, 10);
|
|
|
|
}
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'k':
|
|
|
|
keylayout = strtol(optarg, NULL, 16);
|
2001-06-20 15:54:48 +02:00
|
|
|
/* keylayout = find_keyb_code(optarg); */
|
|
|
|
if (keylayout == 0)
|
|
|
|
return 0;
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
2001-09-14 05:38:39 +02:00
|
|
|
sendmotion = False;
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
orders = False;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
licence = False;
|
|
|
|
break;
|
|
|
|
|
2001-06-20 15:54:48 +02:00
|
|
|
case 'e':
|
|
|
|
use_encryption = False;
|
|
|
|
break;
|
2001-09-14 05:38:39 +02:00
|
|
|
|
2001-06-20 15:54:48 +02:00
|
|
|
case 'F':
|
|
|
|
fullscreen = True;
|
|
|
|
break;
|
2001-09-14 05:38:39 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
server = argv[optind];
|
|
|
|
|
|
|
|
if (username[0] == 0)
|
|
|
|
{
|
|
|
|
pw = getpwuid(getuid());
|
|
|
|
if ((pw == NULL) || (pw->pw_name == NULL))
|
|
|
|
{
|
|
|
|
STATUS("Could not determine user name.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(username, pw->pw_name, sizeof(username));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hostname[0] == 0)
|
|
|
|
{
|
|
|
|
if (gethostname(hostname, sizeof(hostname)) == -1)
|
|
|
|
{
|
|
|
|
STATUS("Could not determine host name.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-15 14:01:01 +02:00
|
|
|
strcpy(title, "rdesktop - ");
|
|
|
|
strncat(title, server, sizeof(title));
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
if (ui_create_window(title))
|
|
|
|
{
|
2001-06-20 15:54:48 +02:00
|
|
|
if (!rdp_connect(server, flags, domain, password, shell,
|
|
|
|
directory))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
STATUS("Connection successful.\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
rdp_main_loop();
|
|
|
|
ui_destroy_window();
|
|
|
|
}
|
|
|
|
|
|
|
|
rdp_disconnect();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate a 32-byte random for the secure transport code. */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
|
|
|
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;
|
2001-01-06 04:12:10 +01:00
|
|
|
uint32 *r = (uint32 *) random;
|
2000-08-15 12:23:24 +02:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* If we have a kernel random device, use it. */
|
|
|
|
if ((fd = open("/dev/urandom", O_RDONLY)) != -1)
|
|
|
|
{
|
|
|
|
read(fd, random, 32);
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise use whatever entropy we can gather - ideas welcome. */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
ERROR("xmalloc %d\n", size);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ERROR("xrealloc %d\n", size);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Produce a hex dump */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
|
|
|
hexdump(unsigned char *p, unsigned int len)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
unsigned char *line = p;
|
|
|
|
unsigned int thisline, offset = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (offset < len)
|
|
|
|
{
|
|
|
|
STATUS("%04x ", offset);
|
|
|
|
thisline = len - offset;
|
|
|
|
if (thisline > 16)
|
|
|
|
thisline = 16;
|
|
|
|
|
|
|
|
for (i = 0; i < thisline; i++)
|
2001-01-06 04:12:10 +01:00
|
|
|
STATUS("%02x ", line[i]) for (; i < 16; i++)
|
|
|
|
STATUS(" ");
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
for (i = 0; i < thisline; i++)
|
2001-01-06 04:12:10 +01:00
|
|
|
STATUS("%c",
|
|
|
|
(line[i] >= 0x20
|
|
|
|
&& line[i] < 0x7f) ? line[i] : '.');
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
STATUS("\n");
|
|
|
|
offset += thisline;
|
|
|
|
line += thisline;
|
|
|
|
}
|
|
|
|
}
|