/[rdesktop]/sourceforge.net/trunk/rdesktop/rdesktop.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 214 - (show annotations)
Sun Oct 6 13:57:39 2002 UTC (21 years, 7 months ago) by matthewc
File MIME type: text/plain
File size: 9384 byte(s)
Remove -l (Microsoft have long since fixed the bug that made it work).

1 /*
2 rdesktop: A Remote Desktop Protocol client.
3 Entrypoint and utility functions
4 Copyright (C) Matthew Chapman 1999-2002
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <stdarg.h> /* va_list va_start va_end */
22 #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
23 #include <fcntl.h> /* open */
24 #include <pwd.h> /* getpwuid */
25 #include <limits.h> /* PATH_MAX */
26 #include <termios.h> /* tcgetattr tcsetattr */
27 #include <sys/stat.h> /* stat */
28 #include <sys/time.h> /* gettimeofday */
29 #include <sys/times.h> /* times */
30 #include "rdesktop.h"
31
32 char title[32] = "";
33 char username[16];
34 char hostname[16];
35 char keymapname[16];
36 int keylayout = 0x409; /* Defaults to US keyboard layout */
37 int width = 800;
38 int height = 600;
39 int tcp_port_rdp = TCP_PORT_RDP;
40 BOOL bitmap_compression = True;
41 BOOL sendmotion = True;
42 BOOL orders = True;
43 BOOL encryption = True;
44 BOOL desktop_save = True;
45 BOOL fullscreen = False;
46 BOOL grab_keyboard = True;
47
48 /* Display usage information */
49 static void
50 usage(char *program)
51 {
52 fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
53 fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");
54 fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
55
56 fprintf(stderr, "Usage: %s [options] server\n", program);
57 fprintf(stderr, " -u: user name\n");
58 fprintf(stderr, " -d: domain\n");
59 fprintf(stderr, " -s: shell\n");
60 fprintf(stderr, " -c: working directory\n");
61 fprintf(stderr, " -p: password (- to prompt)\n");
62 fprintf(stderr, " -n: client hostname\n");
63 fprintf(stderr, " -k: keyboard layout on terminal server (us,sv,gr etc.)\n");
64 fprintf(stderr, " -g: desktop geometry (WxH)\n");
65 fprintf(stderr, " -f: full-screen mode\n");
66 fprintf(stderr, " -b: force bitmap updates\n");
67 fprintf(stderr, " -e: disable encryption (French TS)\n");
68 fprintf(stderr, " -m: do not send motion events\n");
69 fprintf(stderr, " -t: rdp tcp port\n");
70 fprintf(stderr, " -K: keep window manager key bindings\n");
71 fprintf(stderr, " -w: window title\n");
72 }
73
74 static BOOL
75 read_password(char *password, int size)
76 {
77 struct termios tios;
78 BOOL ret = False;
79 int istty = 0;
80 char *p;
81
82 if (tcgetattr(STDIN_FILENO, &tios) == 0)
83 {
84 fprintf(stderr, "Password: ");
85 tios.c_lflag &= ~ECHO;
86 tcsetattr(STDIN_FILENO, TCSANOW, &tios);
87 istty = 1;
88 }
89
90 if (fgets(password, size, stdin) != NULL)
91 {
92 ret = True;
93
94 /* strip final newline */
95 p = strchr(password, '\n');
96 if (p != NULL)
97 *p = 0;
98 }
99
100 if (istty)
101 {
102 tios.c_lflag |= ECHO;
103 tcsetattr(STDIN_FILENO, TCSANOW, &tios);
104 fprintf(stderr, "\n");
105 }
106
107 return ret;
108 }
109
110 /* Client program */
111 int
112 main(int argc, char *argv[])
113 {
114 char fullhostname[64];
115 char domain[16];
116 char password[16];
117 char shell[32];
118 char directory[32];
119 BOOL prompt_password;
120 struct passwd *pw;
121 char *server, *p;
122 uint32 flags;
123 int c;
124
125 flags = RDP_LOGON_NORMAL;
126 prompt_password = False;
127 domain[0] = password[0] = shell[0] = directory[0] = 0;
128 strcpy(keymapname, "us");
129
130 while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:t:fbemKw:h?")) != -1)
131 {
132 switch (c)
133 {
134 case 'u':
135 STRNCPY(username, optarg, sizeof(username));
136 break;
137
138 case 'd':
139 STRNCPY(domain, optarg, sizeof(domain));
140 break;
141
142 case 's':
143 STRNCPY(shell, optarg, sizeof(shell));
144 break;
145
146 case 'c':
147 STRNCPY(directory, optarg, sizeof(directory));
148 break;
149
150 case 'p':
151 if ((optarg[0] == '-') && (optarg[1] == 0))
152 {
153 prompt_password = True;
154 break;
155 }
156
157 STRNCPY(password, optarg, sizeof(password));
158 flags |= RDP_LOGON_AUTO;
159
160 /* try to overwrite argument so it won't appear in ps */
161 p = optarg;
162 while (*p)
163 *(p++) = 'X';
164 break;
165
166 case 'n':
167 STRNCPY(hostname, optarg, sizeof(hostname));
168 break;
169
170 case 'k':
171 STRNCPY(keymapname, optarg, sizeof(keymapname));
172 break;
173
174 case 'g':
175 width = strtol(optarg, &p, 10);
176 if (*p == 'x')
177 height = strtol(p + 1, NULL, 10);
178
179 if ((width == 0) || (height == 0))
180 {
181 error("invalid geometry\n");
182 return 1;
183 }
184 break;
185
186 case 'f':
187 fullscreen = True;
188 break;
189
190 case 'b':
191 orders = False;
192 break;
193
194 case 'e':
195 encryption = False;
196 break;
197
198 case 'm':
199 sendmotion = False;
200 break;
201
202 case 't':
203 tcp_port_rdp = strtol(optarg, NULL, 10);
204 break;
205
206 case 'K':
207 grab_keyboard = False;
208 break;
209
210 case 'w':
211 strncpy(title, optarg, sizeof(title));
212 break;
213
214 case 'h':
215 case '?':
216 default:
217 usage(argv[0]);
218 return 1;
219 }
220 }
221
222 if (argc - optind < 1)
223 {
224 usage(argv[0]);
225 return 1;
226 }
227
228 server = argv[optind];
229
230 if (username[0] == 0)
231 {
232 pw = getpwuid(getuid());
233 if ((pw == NULL) || (pw->pw_name == NULL))
234 {
235 error("could not determine username, use -u\n");
236 return 1;
237 }
238
239 STRNCPY(username, pw->pw_name, sizeof(username));
240 }
241
242 if (hostname[0] == 0)
243 {
244 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
245 {
246 error("could not determine local hostname, use -n\n");
247 return 1;
248 }
249
250 p = strchr(fullhostname, '.');
251 if (p != NULL)
252 *p = 0;
253
254 STRNCPY(hostname, fullhostname, sizeof(hostname));
255 }
256
257 if (prompt_password && read_password(password, sizeof(password)))
258 flags |= RDP_LOGON_AUTO;
259
260 if (title[0] == 0)
261 {
262 strcpy(title, "rdesktop - ");
263 strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
264 }
265
266 if (!ui_init())
267 return 1;
268
269 if (!rdp_connect(server, flags, domain, password, shell, directory))
270 return 1;
271
272 DEBUG(("Connection successful.\n"));
273 memset(password, 0, sizeof(password));
274
275 if (ui_create_window())
276 {
277 rdp_main_loop();
278 ui_destroy_window();
279 }
280
281 DEBUG(("Disconnecting...\n"));
282 rdp_disconnect();
283 ui_deinit();
284 return 0;
285 }
286
287 /* Generate a 32-byte random for the secure transport code. */
288 void
289 generate_random(uint8 * random)
290 {
291 struct stat st;
292 struct tms tmsbuf;
293 uint32 *r = (uint32 *) random;
294 int fd;
295
296 /* If we have a kernel random device, use it. */
297 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
298 || ((fd = open("/dev/random", O_RDONLY)) != -1))
299 {
300 read(fd, random, 32);
301 close(fd);
302 return;
303 }
304
305 /* Otherwise use whatever entropy we can gather - ideas welcome. */
306 r[0] = (getpid()) | (getppid() << 16);
307 r[1] = (getuid()) | (getgid() << 16);
308 r[2] = times(&tmsbuf); /* system uptime (clocks) */
309 gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
310 stat("/tmp", &st);
311 r[5] = st.st_atime;
312 r[6] = st.st_mtime;
313 r[7] = st.st_ctime;
314 }
315
316 /* malloc; exit if out of memory */
317 void *
318 xmalloc(int size)
319 {
320 void *mem = malloc(size);
321 if (mem == NULL)
322 {
323 error("xmalloc %d\n", size);
324 exit(1);
325 }
326 return mem;
327 }
328
329 /* realloc; exit if out of memory */
330 void *
331 xrealloc(void *oldmem, int size)
332 {
333 void *mem = realloc(oldmem, size);
334 if (mem == NULL)
335 {
336 error("xrealloc %d\n", size);
337 exit(1);
338 }
339 return mem;
340 }
341
342 /* free */
343 void
344 xfree(void *mem)
345 {
346 free(mem);
347 }
348
349 /* report an error */
350 void
351 error(char *format, ...)
352 {
353 va_list ap;
354
355 fprintf(stderr, "ERROR: ");
356
357 va_start(ap, format);
358 vfprintf(stderr, format, ap);
359 va_end(ap);
360 }
361
362 /* report an unimplemented protocol feature */
363 void
364 unimpl(char *format, ...)
365 {
366 va_list ap;
367
368 fprintf(stderr, "NOT IMPLEMENTED: ");
369
370 va_start(ap, format);
371 vfprintf(stderr, format, ap);
372 va_end(ap);
373 }
374
375 /* produce a hex dump */
376 void
377 hexdump(unsigned char *p, unsigned int len)
378 {
379 unsigned char *line = p;
380 unsigned int thisline, offset = 0;
381 int i;
382
383 while (offset < len)
384 {
385 printf("%04x ", offset);
386 thisline = len - offset;
387 if (thisline > 16)
388 thisline = 16;
389
390 for (i = 0; i < thisline; i++)
391 printf("%02x ", line[i]);
392
393 for (; i < 16; i++)
394 printf(" ");
395
396 for (i = 0; i < thisline; i++)
397 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
398
399 printf("\n");
400 offset += thisline;
401 line += thisline;
402 }
403 }
404
405 #ifdef SAVE_LICENCE
406 int
407 load_licence(unsigned char **data)
408 {
409 char path[PATH_MAX];
410 char *home;
411 struct stat st;
412 int fd;
413
414 home = getenv("HOME");
415 if (home == NULL)
416 return -1;
417
418 STRNCPY(path, home, sizeof(path));
419 strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1);
420
421 fd = open(path, O_RDONLY);
422 if (fd == -1)
423 return -1;
424
425 if (fstat(fd, &st))
426 return -1;
427
428 *data = xmalloc(st.st_size);
429 return read(fd, *data, st.st_size);
430 }
431
432 void
433 save_licence(unsigned char *data, int length)
434 {
435 char path[PATH_MAX];
436 char *home;
437 int fd;
438
439 home = getenv("HOME");
440 if (home == NULL)
441 return;
442
443 STRNCPY(path, home, sizeof(path));
444 strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1);
445 mkdir(path, 0700);
446
447 strncat(path, "/licence", sizeof(path) - strlen(path) - 1);
448
449 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
450 if (fd == -1)
451 {
452 perror("open");
453 return;
454 }
455
456 write(fd, data, length);
457 close(fd);
458 }
459 #endif

  ViewVC Help
Powered by ViewVC 1.1.26