/[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 39 - (hide annotations)
Fri Apr 5 07:57:43 2002 UTC (22 years, 1 month ago) by matthewc
File MIME type: text/plain
File size: 8066 byte(s)
Code to save and load licence (not quite working yet - server still seems
to reissue the licence).
Small clean-up in xkeymap.

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

  ViewVC Help
Powered by ViewVC 1.1.26