/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 66 - (hide annotations)
Thu Jul 18 18:28:12 2002 UTC (21 years, 10 months ago) by astrand
File MIME type: text/plain
File size: 8326 byte(s)
Merged new keysym-based keyboard handling (from alt. CVS repos)

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

  ViewVC Help
Powered by ViewVC 1.1.26