/[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 482 - (hide annotations)
Thu Oct 9 04:21:19 2003 UTC (20 years, 7 months ago) by matthewc
File MIME type: text/plain
File size: 15153 byte(s)
Add option to attach to console on Windows 2003.
(thanks to Chris Knight <chris@e-easy.com.au>)

1 forsberg 350 /* -*- c-basic-offset: 8 -*-
2 matty 10 rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions
4 matthewc 304 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 astrand 325 #include <errno.h>
30 matty 10 #include "rdesktop.h"
31    
32 matthewc 220 #ifdef EGD_SOCKET
33     #include <sys/socket.h> /* socket connect */
34     #include <sys/un.h> /* sockaddr_un */
35     #endif
36    
37     #ifdef WITH_OPENSSL
38     #include <openssl/md5.h>
39     #else
40     #include "crypto/md5.h"
41     #endif
42    
43 astrand 479 char g_title[64] = "";
44     char g_username[64];
45 matty 10 char hostname[16];
46 matthewc 38 char keymapname[16];
47 astrand 66 int keylayout = 0x409; /* Defaults to US keyboard layout */
48 jsorg71 447 int g_width = 800; /* If width or height are reset to zero, the geometry will
49 astrand 263 be fetched from _NET_WORKAREA */
50 jsorg71 447 int g_height = 600;
51 jsorg71 58 int tcp_port_rdp = TCP_PORT_RDP;
52 jsorg71 438 int g_server_bpp = 8;
53 jsorg71 450 int g_win_button_size = 0; /* If zero, disable single app mode */
54 jsorg71 437 BOOL g_bitmap_compression = True;
55 jsorg71 447 BOOL g_sendmotion = True;
56 jsorg71 437 BOOL g_orders = True;
57     BOOL g_encryption = True;
58 forsberg 427 BOOL packet_encryption = True;
59 jsorg71 438 BOOL g_desktop_save = True;
60 jsorg71 447 BOOL g_fullscreen = False;
61 jsorg71 450 BOOL g_grab_keyboard = True;
62     BOOL g_hide_decorations = False;
63 astrand 458 BOOL g_use_rdp5 = True;
64 matthewc 482 BOOL g_console_session = False;
65 jsorg71 450 extern BOOL g_owncolmap;
66 matty 10
67 astrand 333 #ifdef RDP2VNC
68     extern int rfb_port;
69     extern int defer_time;
70     void
71     rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
72     char *shell, char *directory);
73     #endif
74 matty 10 /* Display usage information */
75 matty 25 static void
76     usage(char *program)
77 matty 10 {
78 matthewc 122 fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
79 matthewc 304 fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
80 matthewc 122 fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
81    
82 matthewc 222 fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
83 astrand 333 #ifdef RDP2VNC
84     fprintf(stderr, " -V: vnc port\n");
85 forsberg 471 fprintf(stderr, " -Q: defer time (ms)\n");
86 astrand 333 #endif
87 astrand 111 fprintf(stderr, " -u: user name\n");
88     fprintf(stderr, " -d: domain\n");
89     fprintf(stderr, " -s: shell\n");
90 astrand 329 fprintf(stderr, " -S: caption button size (single application mode)\n");
91 astrand 111 fprintf(stderr, " -c: working directory\n");
92 matthewc 211 fprintf(stderr, " -p: password (- to prompt)\n");
93 astrand 111 fprintf(stderr, " -n: client hostname\n");
94 matthewc 223 fprintf(stderr, " -k: keyboard layout on terminal server (us,sv,gr,etc.)\n");
95 astrand 111 fprintf(stderr, " -g: desktop geometry (WxH)\n");
96     fprintf(stderr, " -f: full-screen mode\n");
97     fprintf(stderr, " -b: force bitmap updates\n");
98     fprintf(stderr, " -e: disable encryption (French TS)\n");
99 forsberg 471 fprintf(stderr, " -E: disable encryption from client to server\n");
100 astrand 111 fprintf(stderr, " -m: do not send motion events\n");
101 n-ki 279 fprintf(stderr, " -C: use private colour map\n");
102 astrand 111 fprintf(stderr, " -K: keep window manager key bindings\n");
103 matthewc 223 fprintf(stderr, " -T: window title\n");
104 astrand 262 fprintf(stderr, " -D: hide window manager decorations\n");
105 jsorg71 309 fprintf(stderr, " -a: server bpp\n");
106 matthewc 482 fprintf(stderr, " -0: attach to console\n");
107     fprintf(stderr, " -4: use RDP version 4\n");
108     fprintf(stderr, " -5: use RDP version 5 (default)\n");
109 matty 10 }
110    
111 matthewc 211 static BOOL
112     read_password(char *password, int size)
113     {
114     struct termios tios;
115     BOOL ret = False;
116     int istty = 0;
117     char *p;
118    
119     if (tcgetattr(STDIN_FILENO, &tios) == 0)
120     {
121     fprintf(stderr, "Password: ");
122     tios.c_lflag &= ~ECHO;
123     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
124     istty = 1;
125     }
126    
127     if (fgets(password, size, stdin) != NULL)
128     {
129     ret = True;
130    
131     /* strip final newline */
132     p = strchr(password, '\n');
133     if (p != NULL)
134     *p = 0;
135     }
136    
137     if (istty)
138     {
139     tios.c_lflag |= ECHO;
140     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
141     fprintf(stderr, "\n");
142     }
143    
144     return ret;
145     }
146    
147 astrand 444 static void
148     parse_server_and_port(char *server)
149     {
150     char *p;
151     #ifdef IPv6
152     int addr_colons;
153     #endif
154    
155     #ifdef IPv6
156     p = server;
157     addr_colons = 0;
158     while (*p)
159     if (*p++ == ':')
160     addr_colons++;
161     if (addr_colons >= 2)
162     {
163     /* numeric IPv6 style address format - [1:2:3::4]:port */
164     p = strchr(server, ']');
165     if (*server == '[' && p != NULL)
166     {
167     if (*(p + 1) == ':' && *(p + 2) != '\0')
168     tcp_port_rdp = strtol(p + 2, NULL, 10);
169     /* remove the port number and brackets from the address */
170     *p = '\0';
171     strncpy(server, server + 1, strlen(server));
172     }
173     }
174     else
175     {
176     /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
177     p = strchr(server, ':');
178     if (p != NULL)
179     {
180     tcp_port_rdp = strtol(p + 1, NULL, 10);
181     *p = 0;
182     }
183     }
184     #else /* no IPv6 support */
185     p = strchr(server, ':');
186     if (p != NULL)
187     {
188     tcp_port_rdp = strtol(p + 1, NULL, 10);
189     *p = 0;
190     }
191     #endif /* IPv6 */
192    
193     }
194    
195 matty 10 /* Client program */
196 matty 25 int
197     main(int argc, char *argv[])
198 matty 10 {
199 matthewc 222 char server[64];
200 matty 30 char fullhostname[64];
201 matty 21 char domain[16];
202 astrand 479 char password[64];
203 astrand 238 char shell[128];
204 matty 21 char directory[32];
205 forsberg 424 BOOL prompt_password, rdp_retval = False;
206 matty 30 struct passwd *pw;
207     uint32 flags;
208 matthewc 222 char *p;
209 matty 10 int c;
210 astrand 289 int username_option = 0;
211 matty 10
212 matty 21 flags = RDP_LOGON_NORMAL;
213 matthewc 211 prompt_password = False;
214 matty 21 domain[0] = password[0] = shell[0] = directory[0] = 0;
215 matthewc 240 strcpy(keymapname, "en-us");
216 matty 21
217 astrand 333 #ifdef RDP2VNC
218 forsberg 471 #define VNCOPT "V:Q:"
219 astrand 333 #else
220     #define VNCOPT
221     #endif
222    
223 matthewc 482 while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbeEmCKT:D045h?")) != -1)
224 matty 10 {
225     switch (c)
226     {
227 astrand 333 #ifdef RDP2VNC
228     case 'V':
229     rfb_port = strtol(optarg, NULL, 10);
230     if (rfb_port < 100)
231     rfb_port += 5900;
232     break;
233    
234 forsberg 471 case 'Q':
235 astrand 333 defer_time = strtol(optarg, NULL, 10);
236     if (defer_time < 0)
237     defer_time = 0;
238     break;
239     #endif
240    
241 matty 10 case 'u':
242 jsorg71 437 STRNCPY(g_username, optarg, sizeof(g_username));
243 astrand 289 username_option = 1;
244 matty 10 break;
245    
246 matty 21 case 'd':
247 matty 30 STRNCPY(domain, optarg, sizeof(domain));
248 matty 21 break;
249    
250     case 's':
251 matty 30 STRNCPY(shell, optarg, sizeof(shell));
252 matty 21 break;
253    
254 astrand 328 case 'S':
255     if (!strcmp(optarg, "standard"))
256     {
257 jsorg71 450 g_win_button_size = 18;
258 astrand 328 break;
259     }
260    
261 jsorg71 450 g_win_button_size = strtol(optarg, &p, 10);
262 astrand 328
263     if (*p)
264     {
265     error("invalid button size\n");
266     return 1;
267     }
268    
269     break;
270    
271 matty 21 case 'c':
272 matty 30 STRNCPY(directory, optarg, sizeof(directory));
273 matty 21 break;
274    
275 matty 30 case 'p':
276 matthewc 211 if ((optarg[0] == '-') && (optarg[1] == 0))
277     {
278     prompt_password = True;
279     break;
280     }
281    
282 matty 30 STRNCPY(password, optarg, sizeof(password));
283     flags |= RDP_LOGON_AUTO;
284 matthewc 211
285     /* try to overwrite argument so it won't appear in ps */
286 n-ki 171 p = optarg;
287     while (*p)
288     *(p++) = 'X';
289 matty 30 break;
290    
291 matty 10 case 'n':
292 matty 30 STRNCPY(hostname, optarg, sizeof(hostname));
293 matty 10 break;
294    
295     case 'k':
296 astrand 82 STRNCPY(keymapname, optarg, sizeof(keymapname));
297 matty 10 break;
298    
299 matty 30 case 'g':
300 astrand 263 if (!strcmp(optarg, "workarea"))
301     {
302 jsorg71 447 g_width = g_height = 0;
303 astrand 263 break;
304     }
305    
306 jsorg71 447 g_width = strtol(optarg, &p, 10);
307 matty 30 if (*p == 'x')
308 jsorg71 447 g_height = strtol(p + 1, NULL, 10);
309 matty 30
310 jsorg71 447 if ((g_width == 0) || (g_height == 0))
311 matty 30 {
312     error("invalid geometry\n");
313     return 1;
314     }
315 matty 10 break;
316    
317 matty 30 case 'f':
318 jsorg71 447 g_fullscreen = True;
319 matty 30 break;
320    
321 matty 10 case 'b':
322 jsorg71 437 g_orders = False;
323 matty 10 break;
324    
325 matty 30 case 'e':
326 jsorg71 437 g_encryption = False;
327 matty 10 break;
328 astrand 435 case 'E':
329 forsberg 427 packet_encryption = False;
330     break;
331 matty 30 case 'm':
332 jsorg71 447 g_sendmotion = False;
333 matty 28 break;
334 matty 29
335 n-ki 279 case 'C':
336 jsorg71 450 g_owncolmap = True;
337 n-ki 279 break;
338    
339 astrand 76 case 'K':
340 jsorg71 450 g_grab_keyboard = False;
341 astrand 76 break;
342    
343 matthewc 223 case 'T':
344 jsorg71 450 STRNCPY(g_title, optarg, sizeof(g_title));
345 astrand 107 break;
346    
347 astrand 262 case 'D':
348 jsorg71 450 g_hide_decorations = True;
349 astrand 262 break;
350    
351 jsorg71 309 case 'a':
352 jsorg71 438 g_server_bpp = strtol(optarg, NULL, 10);
353     if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
354     && g_server_bpp != 24)
355 jsorg71 309 {
356     error("invalid server bpp\n");
357     return 1;
358     }
359     break;
360    
361 matthewc 482 case '0':
362     g_console_session = True;
363     break;
364    
365 astrand 458 case '4':
366     g_use_rdp5 = False;
367     break;
368    
369 forsberg 350 case '5':
370 jsorg71 438 g_use_rdp5 = True;
371 forsberg 350 break;
372 astrand 458
373 matty 28 case 'h':
374 matty 10 case '?':
375     default:
376     usage(argv[0]);
377     return 1;
378     }
379     }
380    
381     if (argc - optind < 1)
382     {
383     usage(argv[0]);
384     return 1;
385     }
386    
387 matthewc 222 STRNCPY(server, argv[optind], sizeof(server));
388 astrand 444 parse_server_and_port(server);
389 matty 10
390 astrand 289 if (!username_option)
391 matty 10 {
392     pw = getpwuid(getuid());
393     if ((pw == NULL) || (pw->pw_name == NULL))
394     {
395 matty 30 error("could not determine username, use -u\n");
396 matty 10 return 1;
397     }
398    
399 jsorg71 437 STRNCPY(g_username, pw->pw_name, sizeof(g_username));
400 matty 10 }
401    
402     if (hostname[0] == 0)
403     {
404 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
405 matty 10 {
406 matty 30 error("could not determine local hostname, use -n\n");
407 matty 10 return 1;
408     }
409 matty 30
410     p = strchr(fullhostname, '.');
411     if (p != NULL)
412     *p = 0;
413    
414     STRNCPY(hostname, fullhostname, sizeof(hostname));
415 matty 10 }
416    
417 matthewc 211 if (prompt_password && read_password(password, sizeof(password)))
418     flags |= RDP_LOGON_AUTO;
419 matty 30
420 jsorg71 450 if (g_title[0] == 0)
421 astrand 107 {
422 jsorg71 450 strcpy(g_title, "rdesktop - ");
423     strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
424 astrand 107 }
425 matty 12
426 astrand 333 #ifdef RDP2VNC
427     rdp2vnc_connect(server, flags, domain, password, shell, directory);
428 forsberg 424 return 0;
429 astrand 333 #else
430    
431 astrand 82 if (!ui_init())
432     return 1;
433 astrand 66
434 matthewc 474 #ifdef WITH_RDPSND
435     rdpsnd_init();
436     #endif
437 matthewc 432 /* rdpdr_init(); */
438 forsberg 416
439 matthewc 53 if (!rdp_connect(server, flags, domain, password, shell, directory))
440     return 1;
441    
442 forsberg 427 /* By setting encryption to False here, we have an encrypted login
443     packet but unencrypted transfer of other packets */
444 astrand 435 if (!packet_encryption)
445 jsorg71 437 g_encryption = False;
446 forsberg 427
447    
448 matthewc 122 DEBUG(("Connection successful.\n"));
449 matthewc 211 memset(password, 0, sizeof(password));
450 matthewc 53
451 jsorg71 100 if (ui_create_window())
452 matty 10 {
453 forsberg 424 rdp_retval = rdp_main_loop();
454 matty 10 ui_destroy_window();
455     }
456    
457 matthewc 122 DEBUG(("Disconnecting...\n"));
458 matthewc 53 rdp_disconnect();
459 matthewc 188 ui_deinit();
460 astrand 333
461 astrand 435 if (True == rdp_retval)
462 forsberg 424 return 0;
463     else
464     return 2;
465    
466 astrand 333 #endif
467    
468 matty 10 }
469    
470 matthewc 220 #ifdef EGD_SOCKET
471     /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
472     static BOOL
473     generate_random_egd(uint8 * buf)
474     {
475     struct sockaddr_un addr;
476     BOOL ret = False;
477     int fd;
478    
479     fd = socket(AF_UNIX, SOCK_STREAM, 0);
480     if (fd == -1)
481     return False;
482    
483     addr.sun_family = AF_UNIX;
484     memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
485 astrand 259 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
486 matthewc 220 goto err;
487    
488     /* PRNGD and EGD use a simple communications protocol */
489 astrand 259 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
490     buf[1] = 32; /* Number of requested random bytes */
491 matthewc 220 if (write(fd, buf, 2) != 2)
492     goto err;
493    
494 astrand 259 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
495 matthewc 220 goto err;
496    
497     if (read(fd, buf, 32) != 32)
498     goto err;
499    
500     ret = True;
501    
502 astrand 259 err:
503 matthewc 220 close(fd);
504     return ret;
505     }
506     #endif
507    
508 matty 10 /* Generate a 32-byte random for the secure transport code. */
509 matty 25 void
510 astrand 64 generate_random(uint8 * random)
511 matty 10 {
512     struct stat st;
513 matty 22 struct tms tmsbuf;
514 matthewc 220 MD5_CTX md5;
515     uint32 *r;
516     int fd, n;
517 matty 10
518 matthewc 220 /* If we have a kernel random device, try that first */
519 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
520     || ((fd = open("/dev/random", O_RDONLY)) != -1))
521 matty 10 {
522 matthewc 220 n = read(fd, random, 32);
523 matty 10 close(fd);
524 matthewc 220 if (n == 32)
525     return;
526 matty 10 }
527    
528 matthewc 220 #ifdef EGD_SOCKET
529     /* As a second preference use an EGD */
530     if (generate_random_egd(random))
531     return;
532     #endif
533    
534 matty 10 /* Otherwise use whatever entropy we can gather - ideas welcome. */
535 astrand 259 r = (uint32 *) random;
536 matty 10 r[0] = (getpid()) | (getppid() << 16);
537     r[1] = (getuid()) | (getgid() << 16);
538 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
539     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
540 matty 10 stat("/tmp", &st);
541     r[5] = st.st_atime;
542     r[6] = st.st_mtime;
543     r[7] = st.st_ctime;
544 matthewc 220
545     /* Hash both halves with MD5 to obscure possible patterns */
546     MD5_Init(&md5);
547 astrand 259 MD5_Update(&md5, random, 16);
548 matthewc 220 MD5_Final(random, &md5);
549 astrand 259 MD5_Update(&md5, random + 16, 16);
550     MD5_Final(random + 16, &md5);
551 matty 10 }
552    
553     /* malloc; exit if out of memory */
554 matty 25 void *
555     xmalloc(int size)
556 matty 10 {
557     void *mem = malloc(size);
558     if (mem == NULL)
559     {
560 matty 30 error("xmalloc %d\n", size);
561 matty 10 exit(1);
562     }
563     return mem;
564     }
565    
566     /* realloc; exit if out of memory */
567 matty 25 void *
568     xrealloc(void *oldmem, int size)
569 matty 10 {
570     void *mem = realloc(oldmem, size);
571     if (mem == NULL)
572     {
573 matty 30 error("xrealloc %d\n", size);
574 matty 10 exit(1);
575     }
576     return mem;
577     }
578    
579     /* free */
580 matty 25 void
581     xfree(void *mem)
582 matty 10 {
583     free(mem);
584     }
585    
586 matty 30 /* report an error */
587 matty 25 void
588 matty 30 error(char *format, ...)
589     {
590     va_list ap;
591    
592     fprintf(stderr, "ERROR: ");
593    
594     va_start(ap, format);
595     vfprintf(stderr, format, ap);
596     va_end(ap);
597     }
598    
599 matthewc 297 /* report a warning */
600     void
601     warning(char *format, ...)
602     {
603     va_list ap;
604    
605     fprintf(stderr, "WARNING: ");
606    
607     va_start(ap, format);
608     vfprintf(stderr, format, ap);
609     va_end(ap);
610     }
611    
612 matty 30 /* report an unimplemented protocol feature */
613     void
614     unimpl(char *format, ...)
615     {
616     va_list ap;
617    
618     fprintf(stderr, "NOT IMPLEMENTED: ");
619    
620     va_start(ap, format);
621     vfprintf(stderr, format, ap);
622     va_end(ap);
623     }
624    
625     /* produce a hex dump */
626     void
627 jsorg71 376 hexdump(unsigned char *p, int len)
628 matty 10 {
629     unsigned char *line = p;
630 jsorg71 376 int i, thisline, offset = 0;
631 matty 10
632     while (offset < len)
633     {
634 matthewc 169 printf("%04x ", offset);
635 matty 10 thisline = len - offset;
636     if (thisline > 16)
637     thisline = 16;
638    
639     for (i = 0; i < thisline; i++)
640 matthewc 169 printf("%02x ", line[i]);
641 matty 10
642 matty 30 for (; i < 16; i++)
643 matthewc 169 printf(" ");
644 matty 30
645 matty 10 for (i = 0; i < thisline; i++)
646 matthewc 169 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
647 matty 10
648 matthewc 169 printf("\n");
649 matty 10 offset += thisline;
650     line += thisline;
651     }
652     }
653 astrand 325
654    
655     int
656     load_licence(unsigned char **data)
657     {
658 matthewc 367 char *home, *path;
659 astrand 325 struct stat st;
660 matthewc 367 int fd, length;
661 astrand 325
662     home = getenv("HOME");
663     if (home == NULL)
664     return -1;
665    
666 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
667 astrand 325 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
668    
669     fd = open(path, O_RDONLY);
670     if (fd == -1)
671     return -1;
672    
673     if (fstat(fd, &st))
674     return -1;
675    
676 forsberg 416 *data = (uint8 *) xmalloc(st.st_size);
677 matthewc 367 length = read(fd, *data, st.st_size);
678     close(fd);
679     xfree(path);
680     return length;
681 astrand 325 }
682    
683     void
684     save_licence(unsigned char *data, int length)
685     {
686 matthewc 367 char *home, *path, *tmppath;
687     int fd;
688 astrand 325
689     home = getenv("HOME");
690     if (home == NULL)
691     return;
692    
693 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
694 astrand 325
695 matthewc 367 sprintf(path, "%s/.rdesktop", home);
696     if ((mkdir(path, 0700) == -1) && errno != EEXIST)
697 astrand 325 {
698 matthewc 367 perror(path);
699     return;
700 astrand 325 }
701    
702 matthewc 367 /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
703 astrand 325
704 matthewc 367 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
705 forsberg 416 tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
706 matthewc 367 strcpy(tmppath, path);
707     strcat(tmppath, ".new");
708    
709 forsberg 416 fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
710 matthewc 367 if (fd == -1)
711 astrand 325 {
712 matthewc 367 perror(tmppath);
713     return;
714 astrand 325 }
715    
716 matthewc 367 if (write(fd, data, length) != length)
717 astrand 325 {
718 matthewc 367 perror(tmppath);
719     unlink(tmppath);
720 astrand 325 }
721 matthewc 367 else if (rename(tmppath, path) == -1)
722 astrand 325 {
723 matthewc 367 perror(path);
724     unlink(tmppath);
725 astrand 325 }
726    
727 matthewc 367 close(fd);
728     xfree(tmppath);
729     xfree(path);
730 astrand 325 }

  ViewVC Help
Powered by ViewVC 1.1.26