/[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

Diff of /sourceforge.net/trunk/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 289 by astrand, Thu Jan 16 13:48:02 2003 UTC revision 582 by n-ki, Fri Jan 23 14:37:51 2004 UTC
# Line 1  Line 1 
1  /*  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions     Entrypoint and utility functions
4     Copyright (C) Matthew Chapman 1999-2002     Copyright (C) Matthew Chapman 1999-2003
5    
6     This program is free software; you can redistribute it and/or modify     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     it under the terms of the GNU General Public License as published by
# Line 21  Line 21 
21  #include <stdarg.h>             /* va_list va_start va_end */  #include <stdarg.h>             /* va_list va_start va_end */
22  #include <unistd.h>             /* read close getuid getgid getpid getppid gethostname */  #include <unistd.h>             /* read close getuid getgid getpid getppid gethostname */
23  #include <fcntl.h>              /* open */  #include <fcntl.h>              /* open */
 #include <errno.h>              /* save licence uses it. */  
24  #include <pwd.h>                /* getpwuid */  #include <pwd.h>                /* getpwuid */
25  #include <termios.h>            /* tcgetattr tcsetattr */  #include <termios.h>            /* tcgetattr tcsetattr */
26  #include <sys/stat.h>           /* stat */  #include <sys/stat.h>           /* stat */
27  #include <sys/time.h>           /* gettimeofday */  #include <sys/time.h>           /* gettimeofday */
28  #include <sys/times.h>          /* times */  #include <sys/times.h>          /* times */
29    #include <ctype.h>              /* toupper */
30    #include <errno.h>
31  #include "rdesktop.h"  #include "rdesktop.h"
32    
33  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 40  Line 41 
41  #include "crypto/md5.h"  #include "crypto/md5.h"
42  #endif  #endif
43    
44  char title[32] = "";  char g_title[64] = "";
45  char username[16];  char g_username[64];
46  char hostname[16];  char hostname[16];
47  char keymapname[16];  char keymapname[16];
48  int keylayout = 0x409;          /* Defaults to US keyboard layout */  int keylayout = 0x409;          /* Defaults to US keyboard layout */
49  int width = 800;                /* If width or height are reset to zero, the geometry will  
50                                     be fetched from _NET_WORKAREA */  int g_width = 800;              /* width is special: If 0, the
51  int height = 600;                                     geometry will be fetched from
52                                       _NET_WORKAREA. If negative,
53                                       absolute value specifies the
54                                       percent of the whole screen. */
55    int g_height = 600;
56  int tcp_port_rdp = TCP_PORT_RDP;  int tcp_port_rdp = TCP_PORT_RDP;
57  BOOL bitmap_compression = True;  int g_server_bpp = 8;
58  BOOL sendmotion = True;  int g_win_button_size = 0;      /* If zero, disable single app mode */
59  BOOL orders = True;  BOOL g_bitmap_compression = True;
60  BOOL encryption = True;  BOOL g_sendmotion = True;
61  BOOL desktop_save = True;  BOOL g_orders = True;
62  BOOL fullscreen = False;  BOOL g_encryption = True;
63  BOOL grab_keyboard = True;  BOOL packet_encryption = True;
64  BOOL hide_decorations = False;  BOOL g_desktop_save = True;
65  extern BOOL owncolmap;  BOOL g_fullscreen = False;
66    BOOL g_grab_keyboard = True;
67    BOOL g_hide_decorations = False;
68    BOOL g_use_rdp5 = True;
69    BOOL g_console_session = False;
70    BOOL g_numlock_sync = False;
71    extern BOOL g_owncolmap;
72    
73    #ifdef WITH_RDPSND
74    BOOL g_rdpsnd = False;
75    #endif
76    
77    extern RDPDR_DEVICE g_rdpdr_device[];
78    extern uint32 g_num_devices;
79    
80    #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  /* Display usage information */  /* Display usage information */
88  static void  static void
89  usage(char *program)  usage(char *program)
90  {  {
91          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
92          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
93          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
94    
95          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
96    #ifdef RDP2VNC
97            fprintf(stderr, "   -V: vnc port\n");
98            fprintf(stderr, "   -Q: defer time (ms)\n");
99    #endif
100          fprintf(stderr, "   -u: user name\n");          fprintf(stderr, "   -u: user name\n");
101          fprintf(stderr, "   -d: domain\n");          fprintf(stderr, "   -d: domain\n");
102          fprintf(stderr, "   -s: shell\n");          fprintf(stderr, "   -s: shell\n");
103          fprintf(stderr, "   -c: working directory\n");          fprintf(stderr, "   -c: working directory\n");
104          fprintf(stderr, "   -p: password (- to prompt)\n");          fprintf(stderr, "   -p: password (- to prompt)\n");
105          fprintf(stderr, "   -n: client hostname\n");          fprintf(stderr, "   -n: client hostname\n");
106          fprintf(stderr, "   -k: keyboard layout on terminal server (us,sv,gr,etc.)\n");          fprintf(stderr, "   -k: keyboard layout on server (en-us, de, sv, etc.)\n");
107          fprintf(stderr, "   -g: desktop geometry (WxH)\n");          fprintf(stderr, "   -g: desktop geometry (WxH)\n");
108          fprintf(stderr, "   -f: full-screen mode\n");          fprintf(stderr, "   -f: full-screen mode\n");
109          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
110          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
111            fprintf(stderr, "   -E: disable encryption from client to server\n");
112          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
113          fprintf(stderr, "   -C: use private colour map\n");          fprintf(stderr, "   -C: use private colour map\n");
114            fprintf(stderr, "   -D: hide window manager decorations\n");
115          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
116            fprintf(stderr, "   -S: caption button size (single application mode)\n");
117          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
118          fprintf(stderr, "   -D: hide window manager decorations\n");          fprintf(stderr, "   -N: enable numlock syncronization\n");
119            fprintf(stderr, "   -a: connection colour depth\n");
120            fprintf(stderr, "   -r: enable specified device redirection (this flag can be repeated)\n");
121            fprintf(stderr,
122                    "         '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");
123            fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");
124            fprintf(stderr,
125                    "         '-r disk:A=/mnt/floppy': enable redirection of /mnt/floppy to A:\n");
126            fprintf(stderr, "             or   A=/mnt/floppy,D=/mnt/cdrom'\n");
127            fprintf(stderr,
128                    "         '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n");
129            fprintf(stderr, "             or      LPT1=/dev/lp0,LPT2=/dev/lp1\n");
130            fprintf(stderr, "         '-r printer:mydeskjet': enable printer redirection\n");
131            fprintf(stderr,
132                    "             or       mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");
133            fprintf(stderr, "         '-r sound': enable sound redirection\n");
134            fprintf(stderr, "   -0: attach to console\n");
135            fprintf(stderr, "   -4: use RDP version 4\n");
136            fprintf(stderr, "   -5: use RDP version 5 (default)\n");
137  }  }
138    
139  static BOOL  static BOOL
# Line 122  read_password(char *password, int size) Line 172  read_password(char *password, int size)
172          return ret;          return ret;
173  }  }
174    
175    static void
176    parse_server_and_port(char *server)
177    {
178            char *p;
179    #ifdef IPv6
180            int addr_colons;
181    #endif
182    
183    #ifdef IPv6
184            p = server;
185            addr_colons = 0;
186            while (*p)
187                    if (*p++ == ':')
188                            addr_colons++;
189            if (addr_colons >= 2)
190            {
191                    /* numeric IPv6 style address format - [1:2:3::4]:port */
192                    p = strchr(server, ']');
193                    if (*server == '[' && p != NULL)
194                    {
195                            if (*(p + 1) == ':' && *(p + 2) != '\0')
196                                    tcp_port_rdp = strtol(p + 2, NULL, 10);
197                            /* remove the port number and brackets from the address */
198                            *p = '\0';
199                            strncpy(server, server + 1, strlen(server));
200                    }
201            }
202            else
203            {
204                    /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
205                    p = strchr(server, ':');
206                    if (p != NULL)
207                    {
208                            tcp_port_rdp = strtol(p + 1, NULL, 10);
209                            *p = 0;
210                    }
211            }
212    #else /* no IPv6 support */
213            p = strchr(server, ':');
214            if (p != NULL)
215            {
216                    tcp_port_rdp = strtol(p + 1, NULL, 10);
217                    *p = 0;
218            }
219    #endif /* IPv6 */
220    
221    }
222    
223  /* Client program */  /* Client program */
224  int  int
225  main(int argc, char *argv[])  main(int argc, char *argv[])
# Line 129  main(int argc, char *argv[]) Line 227  main(int argc, char *argv[])
227          char server[64];          char server[64];
228          char fullhostname[64];          char fullhostname[64];
229          char domain[16];          char domain[16];
230          char password[16];          char password[64];
231          char shell[128];          char shell[128];
232          char directory[32];          char directory[32];
233          BOOL prompt_password;          BOOL prompt_password, rdp_retval = False;
234          struct passwd *pw;          struct passwd *pw;
235          uint32 flags;          uint32 flags;
236          char *p;          char *p;
237          int c;          int c;
238    
239          int username_option = 0;          int username_option = 0;
240    
241          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
# Line 144  main(int argc, char *argv[]) Line 243  main(int argc, char *argv[])
243          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
244          strcpy(keymapname, "en-us");          strcpy(keymapname, "en-us");
245    
246          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemCKT:Dh?")) != -1)          g_num_devices = 0;
247    
248    #ifdef RDP2VNC
249    #define VNCOPT "V:Q:"
250    #else
251    #define VNCOPT
252    #endif
253    
254            while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbeEmCDKS:T:Na:r:045h?")) != -1)
255          {          {
256                  switch (c)                  switch (c)
257                  {                  {
258    #ifdef RDP2VNC
259                            case 'V':
260                                    rfb_port = strtol(optarg, NULL, 10);
261                                    if (rfb_port < 100)
262                                            rfb_port += 5900;
263                                    break;
264    
265                            case 'Q':
266                                    defer_time = strtol(optarg, NULL, 10);
267                                    if (defer_time < 0)
268                                            defer_time = 0;
269                                    break;
270    #endif
271    
272                          case 'u':                          case 'u':
273                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(g_username, optarg, sizeof(g_username));
274                                  username_option = 1;                                  username_option = 1;
275                                  break;                                  break;
276    
# Line 190  main(int argc, char *argv[]) Line 311  main(int argc, char *argv[])
311                                  break;                                  break;
312    
313                          case 'g':                          case 'g':
314                                    g_fullscreen = False;
315                                  if (!strcmp(optarg, "workarea"))                                  if (!strcmp(optarg, "workarea"))
316                                  {                                  {
317                                          width = height = 0;                                          g_width = g_height = 0;
318                                          break;                                          break;
319                                  }                                  }
320    
321                                  width = strtol(optarg, &p, 10);                                  g_width = strtol(optarg, &p, 10);
322                                    if (g_width <= 0)
323                                    {
324                                            error("invalid geometry\n");
325                                            return 1;
326                                    }
327    
328                                  if (*p == 'x')                                  if (*p == 'x')
329                                          height = strtol(p + 1, NULL, 10);                                          g_height = strtol(p + 1, NULL, 10);
330    
331                                  if ((width == 0) || (height == 0))                                  if (g_height <= 0)
332                                  {                                  {
333                                          error("invalid geometry\n");                                          error("invalid geometry\n");
334                                          return 1;                                          return 1;
335                                  }                                  }
336    
337                                    if (*p == '%')
338                                            g_width = -g_width;
339    
340                                  break;                                  break;
341    
342                          case 'f':                          case 'f':
343                                  fullscreen = True;                                  g_fullscreen = True;
344                                  break;                                  break;
345    
346                          case 'b':                          case 'b':
347                                  orders = False;                                  g_orders = False;
348                                  break;                                  break;
349    
350                          case 'e':                          case 'e':
351                                  encryption = False;                                  g_encryption = False;
352                                    break;
353                            case 'E':
354                                    packet_encryption = False;
355                                  break;                                  break;
   
356                          case 'm':                          case 'm':
357                                  sendmotion = False;                                  g_sendmotion = False;
358                                  break;                                  break;
359    
360                          case 'C':                          case 'C':
361                                  owncolmap = True;                                  g_owncolmap = True;
362                                    break;
363    
364                            case 'D':
365                                    g_hide_decorations = True;
366                                  break;                                  break;
367    
368                          case 'K':                          case 'K':
369                                  grab_keyboard = False;                                  g_grab_keyboard = False;
370                                    break;
371    
372                            case 'S':
373                                    if (!strcmp(optarg, "standard"))
374                                    {
375                                            g_win_button_size = 18;
376                                            break;
377                                    }
378    
379                                    g_win_button_size = strtol(optarg, &p, 10);
380    
381                                    if (*p)
382                                    {
383                                            error("invalid button size\n");
384                                            return 1;
385                                    }
386    
387                                  break;                                  break;
388    
389                          case 'T':                          case 'T':
390                                  STRNCPY(title, optarg, sizeof(title));                                  STRNCPY(g_title, optarg, sizeof(g_title));
391                                  break;                                  break;
392    
393                          case 'D':                          case 'N':
394                                  hide_decorations = True;                                  g_numlock_sync = True;
395                                    break;
396    
397                            case 'a':
398                                    g_server_bpp = strtol(optarg, NULL, 10);
399                                    if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
400                                        && g_server_bpp != 24)
401                                    {
402                                            error("invalid server bpp\n");
403                                            return 1;
404                                    }
405                                    break;
406    
407                            case 'r':
408    
409                                    if (strncmp("sound", optarg, 5) == 0)
410                                    {
411    #ifdef WITH_RDPSND
412                                            g_rdpsnd = True;
413    #else
414                                            warning("Not compiled with sound support");
415    #endif
416                                    }
417                                    else if (strncmp("disk", optarg, 4) == 0)
418                                    {
419                                            /* -r disk:h:=/mnt/floppy */
420                                            disk_enum_devices(&g_num_devices, optarg + 4);
421                                    }
422                                    else if (strncmp("comport", optarg, 7) == 0)
423                                    {
424                                            serial_enum_devices(&g_num_devices, optarg + 7);
425                                    }
426                                    else if (strncmp("lptport", optarg, 7) == 0)
427                                    {
428                                            parallel_enum_devices(&g_num_devices, optarg + 7);
429                                    }
430                                    else if (strncmp("printer", optarg, 7) == 0)
431                                    {
432                                            printer_enum_devices(&g_num_devices, optarg + 7);
433                                    }
434                                    else
435                                    {
436                                            warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n");
437                                    }
438                                    break;
439    
440                            case '0':
441                                    g_console_session = True;
442                                    break;
443    
444                            case '4':
445                                    g_use_rdp5 = False;
446                                    break;
447    
448                            case '5':
449                                    g_use_rdp5 = True;
450                                  break;                                  break;
451    
452                          case 'h':                          case 'h':
# Line 254  main(int argc, char *argv[]) Line 464  main(int argc, char *argv[])
464          }          }
465    
466          STRNCPY(server, argv[optind], sizeof(server));          STRNCPY(server, argv[optind], sizeof(server));
467          p = strchr(server, ':');          parse_server_and_port(server);
         if (p != NULL)  
         {  
                 tcp_port_rdp = strtol(p + 1, NULL, 10);  
                 *p = 0;  
         }  
468    
469          if (!username_option)          if (!username_option)
470          {          {
# Line 270  main(int argc, char *argv[]) Line 475  main(int argc, char *argv[])
475                          return 1;                          return 1;
476                  }                  }
477    
478                  STRNCPY(username, pw->pw_name, sizeof(username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
479          }          }
480    
481          if (hostname[0] == 0)          if (hostname[0] == 0)
# Line 291  main(int argc, char *argv[]) Line 496  main(int argc, char *argv[])
496          if (prompt_password && read_password(password, sizeof(password)))          if (prompt_password && read_password(password, sizeof(password)))
497                  flags |= RDP_LOGON_AUTO;                  flags |= RDP_LOGON_AUTO;
498    
499          if (title[0] == 0)          if (g_title[0] == 0)
500          {          {
501                  strcpy(title, "rdesktop - ");                  strcpy(g_title, "rdesktop - ");
502                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));                  strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
503          }          }
504    
505    #ifdef RDP2VNC
506            rdp2vnc_connect(server, flags, domain, password, shell, directory);
507            return 0;
508    #else
509    
510          if (!ui_init())          if (!ui_init())
511                  return 1;                  return 1;
512    
513    #ifdef WITH_RDPSND
514            if (g_rdpsnd)
515                    rdpsnd_init();
516    #endif
517            rdpdr_init();
518    
519          if (!rdp_connect(server, flags, domain, password, shell, directory))          if (!rdp_connect(server, flags, domain, password, shell, directory))
520                  return 1;                  return 1;
521    
522            /* By setting encryption to False here, we have an encrypted login
523               packet but unencrypted transfer of other packets */
524            if (!packet_encryption)
525                    g_encryption = False;
526    
527    
528          DEBUG(("Connection successful.\n"));          DEBUG(("Connection successful.\n"));
529          memset(password, 0, sizeof(password));          memset(password, 0, sizeof(password));
530    
531          if (ui_create_window())          if (ui_create_window())
532          {          {
533                  rdp_main_loop();                  rdp_retval = rdp_main_loop();
534                  ui_destroy_window();                  ui_destroy_window();
535          }          }
536    
537          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
538          rdp_disconnect();          rdp_disconnect();
539          ui_deinit();          ui_deinit();
540          return 0;  
541            if (True == rdp_retval)
542                    return 0;
543            else
544                    return 2;
545    
546    #endif
547    
548  }  }
549    
550  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 447  error(char *format, ...) Line 676  error(char *format, ...)
676          va_end(ap);          va_end(ap);
677  }  }
678    
679    /* report a warning */
680    void
681    warning(char *format, ...)
682    {
683            va_list ap;
684    
685            fprintf(stderr, "WARNING: ");
686    
687            va_start(ap, format);
688            vfprintf(stderr, format, ap);
689            va_end(ap);
690    }
691    
692  /* report an unimplemented protocol feature */  /* report an unimplemented protocol feature */
693  void  void
694  unimpl(char *format, ...)  unimpl(char *format, ...)
# Line 465  void Line 707  void
707  hexdump(unsigned char *p, unsigned int len)  hexdump(unsigned char *p, unsigned int len)
708  {  {
709          unsigned char *line = p;          unsigned char *line = p;
710          unsigned int thisline, offset = 0;          int i, thisline, offset = 0;
         int i;  
711    
712          while (offset < len)          while (offset < len)
713          {          {
# Line 490  hexdump(unsigned char *p, unsigned int l Line 731  hexdump(unsigned char *p, unsigned int l
731          }          }
732  }  }
733    
734  #ifdef SAVE_LICENCE  /*
735      input: src is the string we look in for needle
736      return value: returns next src pointer, for
737            succesive executions, like in a while loop
738            if retval is 0, then there are no more args.
739      pitfalls:
740            src is modified. 0x00 chars are inserted to
741            terminate strings.
742            return val, points on the next val chr after ins
743            0x00
744    
745            example usage:
746            while( (pos = next_arg( optarg, ',')) ){
747                    printf("%s\n",optarg);
748                    optarg=pos;
749            }
750    
751    */
752    char *
753    next_arg(char *src, char needle)
754    {
755            char *nextval;
756            char *p;
757            char *mvp = 0;
758    
759            /* EOS */
760            if (*src == (char) 0x00)
761                    return 0;
762    
763            p = src;
764            /*  skip escaped needles */
765            while ((nextval = strchr(p, needle)))
766            {
767                    mvp = nextval - 1;
768                    /* found backslashed needle */
769                    if (*mvp == '\\' && (mvp > src))
770                    {
771                            /* move string one to the left */
772                            while (*(mvp + 1) != (char) 0x00)
773                            {
774                                    *mvp = *(mvp + 1);
775                                    *mvp++;
776                            }
777                            *mvp = (char) 0x00;
778                            p = nextval;
779                    }
780                    else
781                    {
782                            p = nextval + 1;
783                            break;
784                    }
785    
786            }
787    
788            /* more args available */
789            if (nextval)
790            {
791                    *nextval = (char) 0x00;
792                    return ++nextval;
793            }
794    
795            /* no more args after this, jump to EOS */
796            nextval = src + strlen(src);
797            return nextval;
798    }
799    
800    
801    void
802    toupper_str(char *p)
803    {
804            while (*p)
805            {
806                    if ((*p >= 'a') && (*p <= 'z'))
807                            *p = toupper((int) *p);
808                    p++;
809            }
810    }
811    
812    
813    /* not all clibs got ltoa */
814    #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
815    
816    char *
817    ltoa(long N, int base)
818    {
819            static char ret[LTOA_BUFSIZE];
820    
821            register int i = 2;
822            long uarg;
823            char *tail, *head = ret, buf[LTOA_BUFSIZE];
824    
825            if (36 < base || 2 > base)
826                    base = 10;
827    
828            tail = &buf[LTOA_BUFSIZE - 1];
829            *tail-- = '\0';
830    
831            if (10 == base && N < 0L)
832            {
833                    *head++ = '-';
834                    uarg = -N;
835            }
836            else
837                    uarg = N;
838    
839            if (uarg)
840            {
841                    for (i = 1; uarg; ++i)
842                    {
843                            register ldiv_t r;
844    
845                            r = ldiv(uarg, base);
846                            *tail-- = (char) (r.rem + ((9L < r.rem) ? ('A' - 10L) : '0'));
847                            uarg = r.quot;
848                    }
849            }
850            else
851                    *tail-- = '0';
852    
853            memcpy(head, ++tail, i);
854            return ret;
855    }
856    
857    
858  int  int
859  load_licence(unsigned char **data)  load_licence(unsigned char **data)
860  {  {
861          char *path;          char *home, *path;
         char *home;  
862          struct stat st;          struct stat st;
863          int fd;          int fd, length;
864    
865          home = getenv("HOME");          home = getenv("HOME");
866          if (home == NULL)          if (home == NULL)
867                  return -1;                  return -1;
868    
869          path = xmalloc(strlen(home) + strlen(hostname) + 20);          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
870          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
871    
872          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
# Line 513  load_licence(unsigned char **data) Line 876  load_licence(unsigned char **data)
876          if (fstat(fd, &st))          if (fstat(fd, &st))
877                  return -1;                  return -1;
878    
879          *data = xmalloc(st.st_size);          *data = (uint8 *) xmalloc(st.st_size);
880          return read(fd, *data, st.st_size);          length = read(fd, *data, st.st_size);
881            close(fd);
882            xfree(path);
883            return length;
884  }  }
885    
886  void  void
887  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
888  {  {
889          char *fpath;            /* file path for licence */          char *home, *path, *tmppath;
890          char *fname, *fnamewrk; /* file name for licence .inkl path. */          int fd;
         char *home;  
         uint32 y;  
         struct flock fnfl;  
         int fnfd, fnwrkfd, i, wlen;  
         struct stream s, *s_ptr;  
         uint32 len;  
   
         /* Construct a stream, so that we can use macros to extract the  
          * licence.  
          */  
         s_ptr = &s;  
         s_ptr->p = data;  
         /* Skip first two bytes */  
         in_uint16(s_ptr, len);  
   
         /* Skip three strings */  
         for (i = 0; i < 3; i++)  
         {  
                 in_uint32(s_ptr, len);  
                 s_ptr->p += len;  
                 /* Make sure that we won't be past the end of data after  
                  * reading the next length value  
                  */  
                 if ((s_ptr->p) + 4 > data + length)  
                 {  
                         printf("Error in parsing licence key.\n");  
                         printf("Strings %d end value %x > supplied length (%x)\n",  
                                i, s_ptr->p, data + length);  
                         return;  
                 }  
         }  
         in_uint32(s_ptr, len);  
         if (s_ptr->p + len > data + length)  
         {  
                 printf("Error in parsing licence key.\n");  
                 printf("End of licence %x > supplied length (%x)\n", s_ptr->p + len, data + length);  
                 return;  
         }  
891    
892          home = getenv("HOME");          home = getenv("HOME");
893          if (home == NULL)          if (home == NULL)
894                  return;                  return;
895    
896          /* set and create the directory -- if it doesn't exist. */          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
         fpath = xmalloc(strlen(home) + 11);  
         STRNCPY(fpath, home, strlen(home) + 1);  
897    
898          sprintf(fpath, "%s/.rdesktop", fpath);          sprintf(path, "%s/.rdesktop", home);
899          if (mkdir(fpath, 0700) == -1 && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
900          {          {
901                  perror("mkdir");                  perror(path);
902                  exit(1);                  return;
903          }          }
904    
905          /* set the real licence filename, and put a write lock on it. */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
         fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);  
         sprintf(fname, "%s/licence.%s", fpath, hostname);  
         fnfd = open(fname, O_RDONLY);  
         if (fnfd != -1)  
         {  
                 fnfl.l_type = F_WRLCK;  
                 fnfl.l_whence = SEEK_SET;  
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
         }  
   
         /* create a temporary licence file */  
         fnamewrk = xmalloc(strlen(fname) + 12);  
         for (y = 0;; y++)  
         {  
                 sprintf(fnamewrk, "%s.%lu", fname, y);  
                 fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600);  
                 if (fnwrkfd == -1)  
                 {  
                         if (errno == EINTR || errno == EEXIST)  
                                 continue;  
                         perror("create");  
                         exit(1);  
                 }  
                 break;  
         }  
         /* write to the licence file */  
         for (y = 0; y < len;)  
         {  
                 do  
                 {  
                         wlen = write(fnwrkfd, s_ptr->p + y, len - y);  
                 }  
                 while (wlen == -1 && errno == EINTR);  
                 if (wlen < 1)  
                 {  
                         perror("write");  
                         unlink(fnamewrk);  
                         exit(1);  
                 }  
                 y += wlen;  
         }  
906    
907          /* close the file and rename it to fname */          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
908          if (close(fnwrkfd) == -1)          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
909            strcpy(tmppath, path);
910            strcat(tmppath, ".new");
911    
912            fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
913            if (fd == -1)
914          {          {
915                  perror("close");                  perror(tmppath);
916                  unlink(fnamewrk);                  return;
                 exit(1);  
917          }          }
918          if (rename(fnamewrk, fname) == -1)  
919            if (write(fd, data, length) != length)
920          {          {
921                  perror("rename");                  perror(tmppath);
922                  unlink(fnamewrk);                  unlink(tmppath);
                 exit(1);  
923          }          }
924          /* close the file lock on fname */          else if (rename(tmppath, path) == -1)
         if (fnfd != -1)  
925          {          {
926                  fnfl.l_type = F_UNLCK;                  perror(path);
927                  fnfl.l_whence = SEEK_SET;                  unlink(tmppath);
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
                 close(fnfd);  
928          }          }
929    
930            close(fd);
931            xfree(tmppath);
932            xfree(path);
933  }  }
 #endif  

Legend:
Removed from v.289  
changed lines
  Added in v.582

  ViewVC Help
Powered by ViewVC 1.1.26