/[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 571 - (hide annotations)
Wed Jan 21 18:17:20 2004 UTC (20 years, 3 months ago) by n-ki
File MIME type: text/plain
File size: 19044 byte(s)
next_arg modified to support escaping of needles with backslash. ( you may need two dep. on your shell. )

1 forsberg 350 /* -*- c-basic-offset: 8 -*-
2 matty 10 rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions
4 n-ki 569 Copyright (C) Matthew Chapman 1999-2003
5 jsorg71 100
6 matty 10 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 jsorg71 100
11 matty 10 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 jsorg71 100
16 matty 10 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 30 #include <stdarg.h> /* va_list va_start va_end */
22 matty 24 #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
23     #include <fcntl.h> /* open */
24     #include <pwd.h> /* getpwuid */
25 matthewc 211 #include <termios.h> /* tcgetattr tcsetattr */
26 matty 24 #include <sys/stat.h> /* stat */
27     #include <sys/time.h> /* gettimeofday */
28     #include <sys/times.h> /* times */
29 stargo 570 #include <ctype.h> /* toupper */
30 astrand 325 #include <errno.h>
31 matty 10 #include "rdesktop.h"
32    
33 matthewc 220 #ifdef EGD_SOCKET
34     #include <sys/socket.h> /* socket connect */
35     #include <sys/un.h> /* sockaddr_un */
36     #endif
37    
38     #ifdef WITH_OPENSSL
39     #include <openssl/md5.h>
40     #else
41     #include "crypto/md5.h"
42     #endif
43    
44 astrand 479 char g_title[64] = "";
45     char g_username[64];
46 matty 10 char hostname[16];
47 matthewc 38 char keymapname[16];
48 astrand 66 int keylayout = 0x409; /* Defaults to US keyboard layout */
49 astrand 500
50     int g_width = 800; /* width is special: If 0, the
51     geometry will be fetched from
52     _NET_WORKAREA. If negative,
53     absolute value specifies the
54     percent of the whole screen. */
55 jsorg71 447 int g_height = 600;
56 jsorg71 58 int tcp_port_rdp = TCP_PORT_RDP;
57 jsorg71 438 int g_server_bpp = 8;
58 jsorg71 450 int g_win_button_size = 0; /* If zero, disable single app mode */
59 jsorg71 437 BOOL g_bitmap_compression = True;
60 jsorg71 447 BOOL g_sendmotion = True;
61 jsorg71 437 BOOL g_orders = True;
62     BOOL g_encryption = True;
63 forsberg 427 BOOL packet_encryption = True;
64 jsorg71 438 BOOL g_desktop_save = True;
65 jsorg71 447 BOOL g_fullscreen = False;
66 jsorg71 450 BOOL g_grab_keyboard = True;
67     BOOL g_hide_decorations = False;
68 astrand 458 BOOL g_use_rdp5 = True;
69 matthewc 482 BOOL g_console_session = False;
70 astrand 552 BOOL g_numlock_sync = False;
71 jsorg71 450 extern BOOL g_owncolmap;
72 matty 10
73 stargo 501 #ifdef WITH_RDPSND
74 matthewc 520 BOOL g_rdpsnd = False;
75 stargo 501 #endif
76    
77 n-ki 569 extern RDPDR_DEVICE g_rdpdr_device[];
78     extern uint32 g_num_devices;
79    
80 astrand 333 #ifdef RDP2VNC
81     extern int rfb_port;
82     extern int defer_time;
83     void
84     rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
85     char *shell, char *directory);
86     #endif
87 matty 10 /* Display usage information */
88 matty 25 static void
89     usage(char *program)
90 matty 10 {
91 matthewc 122 fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
92 matthewc 304 fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
93 matthewc 122 fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
94    
95 matthewc 222 fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
96 astrand 333 #ifdef RDP2VNC
97     fprintf(stderr, " -V: vnc port\n");
98 forsberg 471 fprintf(stderr, " -Q: defer time (ms)\n");
99 astrand 333 #endif
100 astrand 111 fprintf(stderr, " -u: user name\n");
101     fprintf(stderr, " -d: domain\n");
102     fprintf(stderr, " -s: shell\n");
103     fprintf(stderr, " -c: working directory\n");
104 matthewc 211 fprintf(stderr, " -p: password (- to prompt)\n");
105 astrand 111 fprintf(stderr, " -n: client hostname\n");
106 matthewc 520 fprintf(stderr, " -k: keyboard layout on server (en-us, de, sv, etc.)\n");
107 astrand 111 fprintf(stderr, " -g: desktop geometry (WxH)\n");
108     fprintf(stderr, " -f: full-screen mode\n");
109     fprintf(stderr, " -b: force bitmap updates\n");
110     fprintf(stderr, " -e: disable encryption (French TS)\n");
111 forsberg 471 fprintf(stderr, " -E: disable encryption from client to server\n");
112 astrand 111 fprintf(stderr, " -m: do not send motion events\n");
113 n-ki 279 fprintf(stderr, " -C: use private colour map\n");
114 matthewc 520 fprintf(stderr, " -D: hide window manager decorations\n");
115 astrand 111 fprintf(stderr, " -K: keep window manager key bindings\n");
116 matthewc 520 fprintf(stderr, " -S: caption button size (single application mode)\n");
117 matthewc 223 fprintf(stderr, " -T: window title\n");
118 n-ki 569 fprintf(stderr, " -N: enable numlock syncronization\n");
119 matthewc 520 fprintf(stderr, " -a: connection colour depth\n");
120 n-ki 569 fprintf(stderr, " -r: enable specified device redirection (this flag can be repeated)\n");
121     fprintf(stderr, " '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");
122     fprintf(stderr, " or :COM1=/dev/ttyS0:9600,0|1|2,0|2,5|6|7|8:dtr \n");
123     fprintf(stderr, " '-r disk:A=/mnt/floppy': enable redirection of /mnt/floppy to A:\n");
124     fprintf(stderr, " or A=/mnt/floppy,D=/mnt/cdrom'\n");
125     fprintf(stderr, " '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n");
126     fprintf(stderr, " or LPT1=/dev/lp0,LPT2=/dev/lp1\n");
127     fprintf(stderr, " '-r printer:mydeskjet': enable printer redirection\n");
128     fprintf(stderr, " or mydeskjet:\"HP Laserjet IIIP\" to enter server driver as well\n");
129     fprintf(stderr, " '-r sound': enable sound redirection\n");
130 matthewc 482 fprintf(stderr, " -0: attach to console\n");
131     fprintf(stderr, " -4: use RDP version 4\n");
132     fprintf(stderr, " -5: use RDP version 5 (default)\n");
133 matty 10 }
134    
135 matthewc 211 static BOOL
136     read_password(char *password, int size)
137     {
138     struct termios tios;
139     BOOL ret = False;
140     int istty = 0;
141     char *p;
142    
143     if (tcgetattr(STDIN_FILENO, &tios) == 0)
144     {
145     fprintf(stderr, "Password: ");
146     tios.c_lflag &= ~ECHO;
147     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
148     istty = 1;
149     }
150    
151     if (fgets(password, size, stdin) != NULL)
152     {
153     ret = True;
154    
155     /* strip final newline */
156     p = strchr(password, '\n');
157     if (p != NULL)
158     *p = 0;
159     }
160    
161     if (istty)
162     {
163     tios.c_lflag |= ECHO;
164     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
165     fprintf(stderr, "\n");
166     }
167    
168     return ret;
169     }
170    
171 astrand 444 static void
172     parse_server_and_port(char *server)
173     {
174     char *p;
175     #ifdef IPv6
176     int addr_colons;
177     #endif
178    
179     #ifdef IPv6
180     p = server;
181     addr_colons = 0;
182     while (*p)
183     if (*p++ == ':')
184     addr_colons++;
185     if (addr_colons >= 2)
186     {
187     /* numeric IPv6 style address format - [1:2:3::4]:port */
188     p = strchr(server, ']');
189     if (*server == '[' && p != NULL)
190     {
191     if (*(p + 1) == ':' && *(p + 2) != '\0')
192     tcp_port_rdp = strtol(p + 2, NULL, 10);
193     /* remove the port number and brackets from the address */
194     *p = '\0';
195     strncpy(server, server + 1, strlen(server));
196     }
197     }
198     else
199     {
200     /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
201     p = strchr(server, ':');
202     if (p != NULL)
203     {
204     tcp_port_rdp = strtol(p + 1, NULL, 10);
205     *p = 0;
206     }
207     }
208     #else /* no IPv6 support */
209     p = strchr(server, ':');
210     if (p != NULL)
211     {
212     tcp_port_rdp = strtol(p + 1, NULL, 10);
213     *p = 0;
214     }
215     #endif /* IPv6 */
216    
217     }
218    
219 matty 10 /* Client program */
220 matty 25 int
221     main(int argc, char *argv[])
222 matty 10 {
223 matthewc 222 char server[64];
224 matty 30 char fullhostname[64];
225 matty 21 char domain[16];
226 astrand 479 char password[64];
227 astrand 238 char shell[128];
228 matty 21 char directory[32];
229 forsberg 424 BOOL prompt_password, rdp_retval = False;
230 matty 30 struct passwd *pw;
231     uint32 flags;
232 matthewc 222 char *p;
233 matty 10 int c;
234 n-ki 569
235 astrand 289 int username_option = 0;
236 matty 10
237 matty 21 flags = RDP_LOGON_NORMAL;
238 matthewc 211 prompt_password = False;
239 matty 21 domain[0] = password[0] = shell[0] = directory[0] = 0;
240 matthewc 240 strcpy(keymapname, "en-us");
241 matty 21
242 n-ki 569 g_num_devices = 0;
243    
244 astrand 333 #ifdef RDP2VNC
245 forsberg 471 #define VNCOPT "V:Q:"
246 astrand 333 #else
247     #define VNCOPT
248     #endif
249    
250 astrand 552 while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbeEmCDKS:T:Na:r:045h?")) != -1)
251 matty 10 {
252     switch (c)
253     {
254 astrand 333 #ifdef RDP2VNC
255     case 'V':
256     rfb_port = strtol(optarg, NULL, 10);
257     if (rfb_port < 100)
258     rfb_port += 5900;
259     break;
260    
261 forsberg 471 case 'Q':
262 astrand 333 defer_time = strtol(optarg, NULL, 10);
263     if (defer_time < 0)
264     defer_time = 0;
265     break;
266     #endif
267    
268 matty 10 case 'u':
269 jsorg71 437 STRNCPY(g_username, optarg, sizeof(g_username));
270 astrand 289 username_option = 1;
271 matty 10 break;
272    
273 matty 21 case 'd':
274 matty 30 STRNCPY(domain, optarg, sizeof(domain));
275 matty 21 break;
276    
277     case 's':
278 matty 30 STRNCPY(shell, optarg, sizeof(shell));
279 matty 21 break;
280    
281     case 'c':
282 matty 30 STRNCPY(directory, optarg, sizeof(directory));
283 matty 21 break;
284    
285 matty 30 case 'p':
286 matthewc 211 if ((optarg[0] == '-') && (optarg[1] == 0))
287     {
288     prompt_password = True;
289     break;
290     }
291    
292 matty 30 STRNCPY(password, optarg, sizeof(password));
293     flags |= RDP_LOGON_AUTO;
294 matthewc 211
295     /* try to overwrite argument so it won't appear in ps */
296 n-ki 171 p = optarg;
297     while (*p)
298     *(p++) = 'X';
299 matty 30 break;
300    
301 matty 10 case 'n':
302 matty 30 STRNCPY(hostname, optarg, sizeof(hostname));
303 matty 10 break;
304    
305     case 'k':
306 astrand 82 STRNCPY(keymapname, optarg, sizeof(keymapname));
307 matty 10 break;
308    
309 matty 30 case 'g':
310 astrand 547 g_fullscreen = False;
311 astrand 263 if (!strcmp(optarg, "workarea"))
312     {
313 jsorg71 447 g_width = g_height = 0;
314 astrand 263 break;
315     }
316    
317 jsorg71 447 g_width = strtol(optarg, &p, 10);
318 astrand 500 if (g_width <= 0)
319     {
320     error("invalid geometry\n");
321     return 1;
322     }
323    
324 matty 30 if (*p == 'x')
325 jsorg71 447 g_height = strtol(p + 1, NULL, 10);
326 matty 30
327 astrand 500 if (g_height <= 0)
328 matty 30 {
329     error("invalid geometry\n");
330     return 1;
331     }
332 astrand 500
333     if (*p == '%')
334     g_width = -g_width;
335    
336 matty 10 break;
337    
338 matty 30 case 'f':
339 jsorg71 447 g_fullscreen = True;
340 matty 30 break;
341    
342 matty 10 case 'b':
343 jsorg71 437 g_orders = False;
344 matty 10 break;
345    
346 matty 30 case 'e':
347 jsorg71 437 g_encryption = False;
348 matty 10 break;
349 astrand 435 case 'E':
350 forsberg 427 packet_encryption = False;
351     break;
352 matty 30 case 'm':
353 jsorg71 447 g_sendmotion = False;
354 matty 28 break;
355 matty 29
356 n-ki 279 case 'C':
357 jsorg71 450 g_owncolmap = True;
358 n-ki 279 break;
359    
360 matthewc 520 case 'D':
361     g_hide_decorations = True;
362     break;
363    
364 astrand 76 case 'K':
365 jsorg71 450 g_grab_keyboard = False;
366 astrand 76 break;
367    
368 matthewc 520 case 'S':
369     if (!strcmp(optarg, "standard"))
370     {
371     g_win_button_size = 18;
372     break;
373     }
374    
375     g_win_button_size = strtol(optarg, &p, 10);
376    
377     if (*p)
378     {
379     error("invalid button size\n");
380     return 1;
381     }
382    
383     break;
384    
385 matthewc 223 case 'T':
386 jsorg71 450 STRNCPY(g_title, optarg, sizeof(g_title));
387 astrand 107 break;
388    
389 astrand 552 case 'N':
390     g_numlock_sync = True;
391     break;
392    
393 jsorg71 309 case 'a':
394 jsorg71 438 g_server_bpp = strtol(optarg, NULL, 10);
395     if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
396     && g_server_bpp != 24)
397 jsorg71 309 {
398     error("invalid server bpp\n");
399     return 1;
400     }
401     break;
402    
403 matthewc 520 case 'r':
404 n-ki 569
405     if (strncmp("sound", optarg, 5) == 0)
406     {
407 stargo 501 #ifdef WITH_RDPSND
408 matthewc 520 g_rdpsnd = True;
409     #else
410     warning("Not compiled with sound support");
411     #endif
412 n-ki 569 }
413     else if (strncmp("disk", optarg, 4) == 0)
414     {
415     /* -r disk:h:=/mnt/floppy */
416     disk_enum_devices(&g_num_devices, optarg + 4);
417     }
418     else if (strncmp("comport", optarg, 7) == 0)
419     {
420     serial_enum_devices(&g_num_devices, optarg + 7);
421     }
422     else if (strncmp("lptport", optarg, 7) == 0)
423     {
424     parallel_enum_devices(&g_num_devices, optarg + 7);
425     }
426     else if (strncmp("printer", optarg, 7) == 0)
427     {
428     printer_enum_devices(&g_num_devices, optarg + 7);
429     }
430     else
431     {
432     warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n");
433     }
434 stargo 501 break;
435 matthewc 520
436 matthewc 482 case '0':
437     g_console_session = True;
438     break;
439    
440 astrand 458 case '4':
441     g_use_rdp5 = False;
442     break;
443    
444 forsberg 350 case '5':
445 jsorg71 438 g_use_rdp5 = True;
446 forsberg 350 break;
447 astrand 458
448 matty 28 case 'h':
449 matty 10 case '?':
450     default:
451     usage(argv[0]);
452     return 1;
453     }
454     }
455    
456     if (argc - optind < 1)
457     {
458     usage(argv[0]);
459     return 1;
460     }
461    
462 matthewc 222 STRNCPY(server, argv[optind], sizeof(server));
463 astrand 444 parse_server_and_port(server);
464 matty 10
465 astrand 289 if (!username_option)
466 matty 10 {
467     pw = getpwuid(getuid());
468     if ((pw == NULL) || (pw->pw_name == NULL))
469     {
470 matty 30 error("could not determine username, use -u\n");
471 matty 10 return 1;
472     }
473    
474 jsorg71 437 STRNCPY(g_username, pw->pw_name, sizeof(g_username));
475 matty 10 }
476    
477     if (hostname[0] == 0)
478     {
479 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
480 matty 10 {
481 matty 30 error("could not determine local hostname, use -n\n");
482 matty 10 return 1;
483     }
484 matty 30
485     p = strchr(fullhostname, '.');
486     if (p != NULL)
487     *p = 0;
488    
489     STRNCPY(hostname, fullhostname, sizeof(hostname));
490 matty 10 }
491    
492 matthewc 211 if (prompt_password && read_password(password, sizeof(password)))
493     flags |= RDP_LOGON_AUTO;
494 matty 30
495 jsorg71 450 if (g_title[0] == 0)
496 astrand 107 {
497 jsorg71 450 strcpy(g_title, "rdesktop - ");
498     strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
499 astrand 107 }
500 matty 12
501 astrand 333 #ifdef RDP2VNC
502     rdp2vnc_connect(server, flags, domain, password, shell, directory);
503 forsberg 424 return 0;
504 astrand 333 #else
505    
506 astrand 82 if (!ui_init())
507     return 1;
508 astrand 66
509 matthewc 474 #ifdef WITH_RDPSND
510 stargo 501 if (g_rdpsnd)
511     rdpsnd_init();
512 matthewc 474 #endif
513 n-ki 569 rdpdr_init();
514 forsberg 416
515 matthewc 53 if (!rdp_connect(server, flags, domain, password, shell, directory))
516     return 1;
517    
518 forsberg 427 /* By setting encryption to False here, we have an encrypted login
519     packet but unencrypted transfer of other packets */
520 astrand 435 if (!packet_encryption)
521 jsorg71 437 g_encryption = False;
522 forsberg 427
523    
524 matthewc 122 DEBUG(("Connection successful.\n"));
525 matthewc 211 memset(password, 0, sizeof(password));
526 matthewc 53
527 jsorg71 100 if (ui_create_window())
528 matty 10 {
529 forsberg 424 rdp_retval = rdp_main_loop();
530 matty 10 ui_destroy_window();
531     }
532    
533 matthewc 122 DEBUG(("Disconnecting...\n"));
534 matthewc 53 rdp_disconnect();
535 matthewc 188 ui_deinit();
536 astrand 333
537 astrand 435 if (True == rdp_retval)
538 forsberg 424 return 0;
539     else
540     return 2;
541    
542 astrand 333 #endif
543    
544 matty 10 }
545    
546 matthewc 220 #ifdef EGD_SOCKET
547     /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
548     static BOOL
549     generate_random_egd(uint8 * buf)
550     {
551     struct sockaddr_un addr;
552     BOOL ret = False;
553     int fd;
554    
555     fd = socket(AF_UNIX, SOCK_STREAM, 0);
556     if (fd == -1)
557     return False;
558    
559     addr.sun_family = AF_UNIX;
560     memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
561 astrand 259 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
562 matthewc 220 goto err;
563    
564     /* PRNGD and EGD use a simple communications protocol */
565 astrand 259 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
566     buf[1] = 32; /* Number of requested random bytes */
567 matthewc 220 if (write(fd, buf, 2) != 2)
568     goto err;
569    
570 astrand 259 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
571 matthewc 220 goto err;
572    
573     if (read(fd, buf, 32) != 32)
574     goto err;
575    
576     ret = True;
577    
578 astrand 259 err:
579 matthewc 220 close(fd);
580     return ret;
581     }
582     #endif
583    
584 matty 10 /* Generate a 32-byte random for the secure transport code. */
585 matty 25 void
586 astrand 64 generate_random(uint8 * random)
587 matty 10 {
588     struct stat st;
589 matty 22 struct tms tmsbuf;
590 matthewc 220 MD5_CTX md5;
591     uint32 *r;
592     int fd, n;
593 matty 10
594 matthewc 220 /* If we have a kernel random device, try that first */
595 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
596     || ((fd = open("/dev/random", O_RDONLY)) != -1))
597 matty 10 {
598 matthewc 220 n = read(fd, random, 32);
599 matty 10 close(fd);
600 matthewc 220 if (n == 32)
601     return;
602 matty 10 }
603    
604 matthewc 220 #ifdef EGD_SOCKET
605     /* As a second preference use an EGD */
606     if (generate_random_egd(random))
607     return;
608     #endif
609    
610 matty 10 /* Otherwise use whatever entropy we can gather - ideas welcome. */
611 astrand 259 r = (uint32 *) random;
612 matty 10 r[0] = (getpid()) | (getppid() << 16);
613     r[1] = (getuid()) | (getgid() << 16);
614 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
615     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
616 matty 10 stat("/tmp", &st);
617     r[5] = st.st_atime;
618     r[6] = st.st_mtime;
619     r[7] = st.st_ctime;
620 matthewc 220
621     /* Hash both halves with MD5 to obscure possible patterns */
622     MD5_Init(&md5);
623 astrand 259 MD5_Update(&md5, random, 16);
624 matthewc 220 MD5_Final(random, &md5);
625 astrand 259 MD5_Update(&md5, random + 16, 16);
626     MD5_Final(random + 16, &md5);
627 matty 10 }
628    
629     /* malloc; exit if out of memory */
630 matty 25 void *
631     xmalloc(int size)
632 matty 10 {
633     void *mem = malloc(size);
634     if (mem == NULL)
635     {
636 matty 30 error("xmalloc %d\n", size);
637 matty 10 exit(1);
638     }
639     return mem;
640     }
641    
642     /* realloc; exit if out of memory */
643 matty 25 void *
644     xrealloc(void *oldmem, int size)
645 matty 10 {
646     void *mem = realloc(oldmem, size);
647     if (mem == NULL)
648     {
649 matty 30 error("xrealloc %d\n", size);
650 matty 10 exit(1);
651     }
652     return mem;
653     }
654    
655     /* free */
656 matty 25 void
657     xfree(void *mem)
658 matty 10 {
659     free(mem);
660     }
661    
662 matty 30 /* report an error */
663 matty 25 void
664 matty 30 error(char *format, ...)
665     {
666     va_list ap;
667    
668     fprintf(stderr, "ERROR: ");
669    
670     va_start(ap, format);
671     vfprintf(stderr, format, ap);
672     va_end(ap);
673     }
674    
675 matthewc 297 /* report a warning */
676     void
677     warning(char *format, ...)
678     {
679     va_list ap;
680    
681     fprintf(stderr, "WARNING: ");
682    
683     va_start(ap, format);
684     vfprintf(stderr, format, ap);
685     va_end(ap);
686     }
687    
688 matty 30 /* report an unimplemented protocol feature */
689     void
690     unimpl(char *format, ...)
691     {
692     va_list ap;
693    
694     fprintf(stderr, "NOT IMPLEMENTED: ");
695    
696     va_start(ap, format);
697     vfprintf(stderr, format, ap);
698     va_end(ap);
699     }
700    
701     /* produce a hex dump */
702     void
703 n-ki 569 hexdump(unsigned char *p, unsigned int len)
704 matty 10 {
705     unsigned char *line = p;
706 jsorg71 376 int i, thisline, offset = 0;
707 matty 10
708     while (offset < len)
709     {
710 matthewc 169 printf("%04x ", offset);
711 matty 10 thisline = len - offset;
712     if (thisline > 16)
713     thisline = 16;
714    
715     for (i = 0; i < thisline; i++)
716 matthewc 169 printf("%02x ", line[i]);
717 matty 10
718 matty 30 for (; i < 16; i++)
719 matthewc 169 printf(" ");
720 matty 30
721 matty 10 for (i = 0; i < thisline; i++)
722 matthewc 169 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
723 matty 10
724 matthewc 169 printf("\n");
725 matty 10 offset += thisline;
726     line += thisline;
727     }
728     }
729 astrand 325
730 n-ki 569 /*
731     input: src is the string we look in for needle
732     return value: returns next src pointer, for
733     succesive executions, like in a while loop
734     if retval is 0, then there are no more args.
735     pitfalls:
736     src is modified. 0x00 chars are inserted to
737     terminate strings.
738     return val, points on the next val chr after ins
739     0x00
740 astrand 325
741 n-ki 569 example usage:
742     while( (pos = next_arg( optarg, ',')) ){
743     printf("%s\n",optarg);
744     optarg=pos;
745     }
746    
747     */
748     char *
749     next_arg(char *src, char needle)
750     {
751     char *nextval;
752 n-ki 571 char *p;
753 n-ki 569
754     // EOS
755     if (*src == (char) 0x00)
756     return 0;
757    
758 n-ki 571 p = src;
759     // skip escaped needles.
760     while( (nextval = strchr(p, needle) ) )
761     {
762     if( *(nextval-1) != '\\' )
763     break;
764     p = nextval +1;
765     }
766    
767     // more args available
768 n-ki 569 if (nextval)
769     {
770     *nextval = (char) 0x00;
771     return ++nextval;
772     }
773    
774     // no more args after this, jump to EOS
775     nextval = src + strlen(src);
776     return nextval;
777     }
778    
779    
780 stargo 570 void
781     toupper_str(char* p)
782 n-ki 569 {
783     while( *p ){
784     if( (*p >= 'a') && (*p <= 'z') )
785 stargo 570 *p = toupper((int) *p);
786 n-ki 569 p++;
787     }
788     }
789    
790    
791     /* not all clibs got ltoa */
792     #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
793    
794     char *
795     ltoa(long N, int base)
796     {
797     static char ret[LTOA_BUFSIZE];
798    
799     register int i = 2;
800     long uarg;
801     char *tail, *head = ret, buf[LTOA_BUFSIZE];
802    
803     if (36 < base || 2 > base)
804     base = 10;
805    
806     tail = &buf[LTOA_BUFSIZE - 1];
807     *tail-- = '\0';
808    
809     if (10 == base && N < 0L)
810     {
811     *head++ = '-';
812     uarg = -N;
813     }
814     else
815     uarg = N;
816    
817     if (uarg)
818     {
819     for (i = 1; uarg; ++i)
820     {
821     register ldiv_t r;
822    
823     r = ldiv(uarg, base);
824     *tail-- = (char) (r.rem + ((9L < r.rem) ? ('A' - 10L) : '0'));
825     uarg = r.quot;
826     }
827     }
828     else
829     *tail-- = '0';
830    
831     memcpy(head, ++tail, i);
832     return ret;
833     }
834    
835    
836 astrand 325 int
837     load_licence(unsigned char **data)
838     {
839 matthewc 367 char *home, *path;
840 astrand 325 struct stat st;
841 matthewc 367 int fd, length;
842 astrand 325
843     home = getenv("HOME");
844     if (home == NULL)
845     return -1;
846    
847 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
848 astrand 325 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
849    
850     fd = open(path, O_RDONLY);
851     if (fd == -1)
852     return -1;
853    
854     if (fstat(fd, &st))
855     return -1;
856    
857 forsberg 416 *data = (uint8 *) xmalloc(st.st_size);
858 matthewc 367 length = read(fd, *data, st.st_size);
859     close(fd);
860     xfree(path);
861     return length;
862 astrand 325 }
863    
864     void
865     save_licence(unsigned char *data, int length)
866     {
867 matthewc 367 char *home, *path, *tmppath;
868     int fd;
869 astrand 325
870     home = getenv("HOME");
871     if (home == NULL)
872     return;
873    
874 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
875 astrand 325
876 matthewc 367 sprintf(path, "%s/.rdesktop", home);
877     if ((mkdir(path, 0700) == -1) && errno != EEXIST)
878 astrand 325 {
879 matthewc 367 perror(path);
880     return;
881 astrand 325 }
882    
883 matthewc 367 /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
884 astrand 325
885 matthewc 367 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
886 forsberg 416 tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
887 matthewc 367 strcpy(tmppath, path);
888     strcat(tmppath, ".new");
889    
890 forsberg 416 fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
891 matthewc 367 if (fd == -1)
892 astrand 325 {
893 matthewc 367 perror(tmppath);
894     return;
895 astrand 325 }
896    
897 matthewc 367 if (write(fd, data, length) != length)
898 astrand 325 {
899 matthewc 367 perror(tmppath);
900     unlink(tmppath);
901 astrand 325 }
902 matthewc 367 else if (rename(tmppath, path) == -1)
903 astrand 325 {
904 matthewc 367 perror(path);
905     unlink(tmppath);
906 astrand 325 }
907    
908 matthewc 367 close(fd);
909     xfree(tmppath);
910     xfree(path);
911 astrand 325 }

  ViewVC Help
Powered by ViewVC 1.1.26