/[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 479 - (hide annotations)
Mon Oct 6 09:37:24 2003 UTC (20 years, 7 months ago) by astrand
File MIME type: text/plain
File size: 15017 byte(s)
Increased lengths for title, username and password

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

  ViewVC Help
Powered by ViewVC 1.1.26