2000-08-15 12:23:24 +02:00
|
|
|
/*
|
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Entrypoint and utility functions
|
2001-09-14 15:51:38 +02:00
|
|
|
Copyright (C) Matthew Chapman 1999-2001
|
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.
|
|
|
|
|
|
|
|
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 */
|
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 */
|
|
|
|
#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];
|
2002-04-04 14:04:33 +02:00
|
|
|
char keymapname[16];
|
|
|
|
int keylayout;
|
2001-09-14 15:51:38 +02:00
|
|
|
int width;
|
|
|
|
int height;
|
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-09-14 15:51:38 +02:00
|
|
|
BOOL encryption = True;
|
2001-06-20 15:54:48 +02:00
|
|
|
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
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
printf("Usage: %s [options] server\n", program);
|
|
|
|
printf(" -u: user name\n");
|
|
|
|
printf(" -d: domain\n");
|
|
|
|
printf(" -s: shell\n");
|
|
|
|
printf(" -c: working directory\n");
|
|
|
|
printf(" -p: password (autologon)\n");
|
|
|
|
printf(" -n: client hostname\n");
|
2002-04-04 14:04:33 +02:00
|
|
|
printf(" -k: keyboard layout\n");
|
2001-09-14 15:51:38 +02:00
|
|
|
printf(" -g: desktop geometry (WxH)\n");
|
|
|
|
printf(" -f: full-screen mode\n");
|
|
|
|
printf(" -b: force bitmap updates\n");
|
|
|
|
printf(" -e: disable encryption (French TS)\n");
|
|
|
|
printf(" -m: do not send motion events\n");
|
|
|
|
printf(" -l: do not request licence\n\n");
|
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
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
char fullhostname[64];
|
2000-10-16 10:44:48 +02:00
|
|
|
char domain[16];
|
|
|
|
char password[16];
|
|
|
|
char shell[32];
|
|
|
|
char directory[32];
|
2000-08-15 12:23:24 +02:00
|
|
|
char title[32];
|
2001-09-14 15:51:38 +02:00
|
|
|
struct passwd *pw;
|
|
|
|
char *server, *p;
|
|
|
|
uint32 flags;
|
2000-08-15 12:23:24 +02:00
|
|
|
int c;
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
printf("rdesktop: A Remote Desktop Protocol client.\n");
|
|
|
|
printf("Version " VERSION ". Copyright (C) 1999-2001 Matt Chapman.\n");
|
|
|
|
printf("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;
|
2002-04-04 14:04:33 +02:00
|
|
|
strcpy(keymapname, "us");
|
2000-10-16 10:44:48 +02:00
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemlh?")) != -1)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'u':
|
2001-09-14 15:51:38 +02:00
|
|
|
STRNCPY(username, optarg, sizeof(username));
|
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;
|
|
|
|
|
|
|
|
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':
|
|
|
|
STRNCPY(password, optarg, sizeof(password));
|
|
|
|
flags |= RDP_LOGON_AUTO;
|
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-04-04 14:04:33 +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':
|
|
|
|
width = strtol(optarg, &p, 10);
|
|
|
|
if (*p == 'x')
|
|
|
|
height = strtol(p+1, NULL, 10);
|
|
|
|
|
|
|
|
if ((width == 0) || (height == 0))
|
|
|
|
{
|
|
|
|
error("invalid geometry\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
fullscreen = True;
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
orders = False;
|
|
|
|
break;
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
case 'e':
|
|
|
|
encryption = False;
|
2000-08-15 12:23:24 +02:00
|
|
|
break;
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
case 'm':
|
|
|
|
sendmotion = False;
|
2001-06-20 15:54:48 +02:00
|
|
|
break;
|
2001-09-14 05:38:39 +02:00
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
case 'l':
|
|
|
|
licence = False;
|
2001-06-20 15:54:48 +02:00
|
|
|
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))
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
STRNCPY(username, pw->pw_name, sizeof(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));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(password, "-"))
|
|
|
|
{
|
|
|
|
p = getpass("Password: ");
|
|
|
|
if (p == NULL)
|
|
|
|
{
|
|
|
|
error("failed to read password\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
STRNCPY(password, p, sizeof(password));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((width == 0) || (height == 0))
|
|
|
|
{
|
|
|
|
width = 800;
|
|
|
|
height = 600;
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
2000-08-15 14:01:01 +02:00
|
|
|
strcpy(title, "rdesktop - ");
|
2001-09-14 15:51:38 +02:00
|
|
|
strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
|
2000-08-15 14:01:01 +02:00
|
|
|
|
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;
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
printf("Connection successful.\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
rdp_main_loop();
|
2001-09-15 14:34:34 +02:00
|
|
|
printf("Disconnecting...\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
ui_destroy_window();
|
2002-04-04 14:04:33 +02:00
|
|
|
rdp_disconnect();
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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. */
|
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
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
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)
|
|
|
|
{
|
2001-09-14 15:51:38 +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++)
|
2001-09-14 15:51:38 +02:00
|
|
|
printf("%02x ", line[i]);
|
|
|
|
|
|
|
|
for (; i < 16; i++)
|
|
|
|
printf(" ");
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
for (i = 0; i < thisline; i++)
|
2001-09-14 15:51:38 +02:00
|
|
|
printf("%c",
|
2001-01-06 04:12:10 +01:00
|
|
|
(line[i] >= 0x20
|
|
|
|
&& line[i] < 0x7f) ? line[i] : '.');
|
2000-08-15 12:23:24 +02:00
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
printf("\n");
|
2000-08-15 12:23:24 +02:00
|
|
|
offset += thisline;
|
|
|
|
line += thisline;
|
|
|
|
}
|
|
|
|
}
|