/[rdesktop]/sourceforge.net/branches/seamlessrdp-branch/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

Annotation of /sourceforge.net/branches/seamlessrdp-branch/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 82 - (hide annotations)
Tue Jul 30 07:18:48 2002 UTC (21 years, 9 months ago) by astrand
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 8460 byte(s)
Changed max line length to 100

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

  ViewVC Help
Powered by ViewVC 1.1.26