/[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 64 - (show annotations)
Thu Jul 18 16:38:31 2002 UTC (21 years, 10 months ago) by astrand
File MIME type: text/plain
File size: 8226 byte(s)
Fixed indentation with indent

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

  ViewVC Help
Powered by ViewVC 1.1.26