/[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 96 - (hide annotations)
Fri Aug 9 09:11:01 2002 UTC (21 years, 9 months ago) by astrand
File MIME type: text/plain
File size: 8774 byte(s)
Added -P parameter: Get password from external program (likeSSH_ASKPASS)

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

  ViewVC Help
Powered by ViewVC 1.1.26