/[rdesktop]/sourceforge.net/branches/seamlessrdp-branch/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/branches/seamlessrdp-branch/rdesktop/rdesktop.c

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

revision 333 by astrand, Thu Feb 20 12:14:13 2003 UTC revision 630 by n-ki, Fri Mar 5 06:39:00 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-2003     Copyright (C) Matthew Chapman 1999-2003
# Line 26  Line 26 
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>  #include <errno.h>
31  #include "rdesktop.h"  #include "rdesktop.h"
32    
# 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  int server_bpp = 8;  int g_server_bpp = 8;
58  int win_button_size = 0;        /* If zero, disable single app mode */  int g_win_button_size = 0;      /* If zero, disable single app mode */
59  BOOL bitmap_compression = True;  BOOL g_bitmap_compression = True;
60  BOOL sendmotion = True;  BOOL g_sendmotion = True;
61  BOOL orders = True;  BOOL g_orders = True;
62  BOOL encryption = True;  BOOL g_encryption = True;
63  BOOL desktop_save = True;  BOOL packet_encryption = True;
64  BOOL fullscreen = False;  BOOL g_desktop_save = True;
65  BOOL grab_keyboard = True;  BOOL g_fullscreen = False;
66  BOOL hide_decorations = False;  BOOL g_grab_keyboard = True;
67  extern BOOL owncolmap;  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    extern BOOL g_ownbackstore;
73    
74    #ifdef WITH_RDPSND
75    BOOL g_rdpsnd = False;
76    #endif
77    
78    extern RDPDR_DEVICE g_rdpdr_device[];
79    extern uint32 g_num_devices;
80    
81  #ifdef RDP2VNC  #ifdef RDP2VNC
82  extern int rfb_port;  extern int rfb_port;
# Line 79  usage(char *program) Line 96  usage(char *program)
96          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
97  #ifdef RDP2VNC  #ifdef RDP2VNC
98          fprintf(stderr, "   -V: vnc port\n");          fprintf(stderr, "   -V: vnc port\n");
99          fprintf(stderr, "   -E: defer time (ms)\n");          fprintf(stderr, "   -Q: defer time (ms)\n");
100  #endif  #endif
101          fprintf(stderr, "   -u: user name\n");          fprintf(stderr, "   -u: user name\n");
102          fprintf(stderr, "   -d: domain\n");          fprintf(stderr, "   -d: domain\n");
103          fprintf(stderr, "   -s: shell\n");          fprintf(stderr, "   -s: shell\n");
         fprintf(stderr, "   -S: caption button size (single application mode)\n");  
104          fprintf(stderr, "   -c: working directory\n");          fprintf(stderr, "   -c: working directory\n");
105          fprintf(stderr, "   -p: password (- to prompt)\n");          fprintf(stderr, "   -p: password (- to prompt)\n");
106          fprintf(stderr, "   -n: client hostname\n");          fprintf(stderr, "   -n: client hostname\n");
107          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");
108          fprintf(stderr, "   -g: desktop geometry (WxH)\n");          fprintf(stderr, "   -g: desktop geometry (WxH)\n");
109          fprintf(stderr, "   -f: full-screen mode\n");          fprintf(stderr, "   -f: full-screen mode\n");
110          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
111            fprintf(stderr, "   -B: use BackingStore of X-server (if available)\n");
112          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
113            fprintf(stderr, "   -E: disable encryption from client to server\n");
114          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
115          fprintf(stderr, "   -C: use private colour map\n");          fprintf(stderr, "   -C: use private colour map\n");
116            fprintf(stderr, "   -D: hide window manager decorations\n");
117          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
118            fprintf(stderr, "   -S: caption button size (single application mode)\n");
119          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
120          fprintf(stderr, "   -D: hide window manager decorations\n");          fprintf(stderr, "   -N: enable numlock syncronization\n");
121          fprintf(stderr, "   -a: server bpp\n");          fprintf(stderr, "   -a: connection colour depth\n");
122            fprintf(stderr, "   -r: enable specified device redirection (this flag can be repeated)\n");
123            fprintf(stderr,
124                    "         '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");
125            fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");
126            fprintf(stderr,
127                    "         '-r disk:A=/mnt/floppy': enable redirection of /mnt/floppy to A:\n");
128            fprintf(stderr, "             or   A=/mnt/floppy,D=/mnt/cdrom'\n");
129            fprintf(stderr,
130                    "         '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n");
131            fprintf(stderr, "             or      LPT1=/dev/lp0,LPT2=/dev/lp1\n");
132            fprintf(stderr, "         '-r printer:mydeskjet': enable printer redirection\n");
133            fprintf(stderr,
134                    "             or      mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");
135            fprintf(stderr, "         '-r sound:[local|off|remote]': enable sound redirection\n");
136            fprintf(stderr, "                     remote would leave sound on server\n");
137            fprintf(stderr, "   -0: attach to console\n");
138            fprintf(stderr, "   -4: use RDP version 4\n");
139            fprintf(stderr, "   -5: use RDP version 5 (default)\n");
140  }  }
141    
142  static BOOL  static BOOL
# Line 137  read_password(char *password, int size) Line 175  read_password(char *password, int size)
175          return ret;          return ret;
176  }  }
177    
178    static void
179    parse_server_and_port(char *server)
180    {
181            char *p;
182    #ifdef IPv6
183            int addr_colons;
184    #endif
185    
186    #ifdef IPv6
187            p = server;
188            addr_colons = 0;
189            while (*p)
190                    if (*p++ == ':')
191                            addr_colons++;
192            if (addr_colons >= 2)
193            {
194                    /* numeric IPv6 style address format - [1:2:3::4]:port */
195                    p = strchr(server, ']');
196                    if (*server == '[' && p != NULL)
197                    {
198                            if (*(p + 1) == ':' && *(p + 2) != '\0')
199                                    tcp_port_rdp = strtol(p + 2, NULL, 10);
200                            /* remove the port number and brackets from the address */
201                            *p = '\0';
202                            strncpy(server, server + 1, strlen(server));
203                    }
204            }
205            else
206            {
207                    /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
208                    p = strchr(server, ':');
209                    if (p != NULL)
210                    {
211                            tcp_port_rdp = strtol(p + 1, NULL, 10);
212                            *p = 0;
213                    }
214            }
215    #else /* no IPv6 support */
216            p = strchr(server, ':');
217            if (p != NULL)
218            {
219                    tcp_port_rdp = strtol(p + 1, NULL, 10);
220                    *p = 0;
221            }
222    #endif /* IPv6 */
223    
224    }
225    
226  /* Client program */  /* Client program */
227  int  int
228  main(int argc, char *argv[])  main(int argc, char *argv[])
# Line 144  main(int argc, char *argv[]) Line 230  main(int argc, char *argv[])
230          char server[64];          char server[64];
231          char fullhostname[64];          char fullhostname[64];
232          char domain[16];          char domain[16];
233          char password[16];          char password[64];
234          char shell[128];          char shell[128];
235          char directory[32];          char directory[32];
236          BOOL prompt_password;          BOOL prompt_password, rdp_retval = False;
237          struct passwd *pw;          struct passwd *pw;
238          uint32 flags;          uint32 flags;
239          char *p;          char *p;
240          int c;          int c;
241    
242          int username_option = 0;          int username_option = 0;
243    
244          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
# Line 159  main(int argc, char *argv[]) Line 246  main(int argc, char *argv[])
246          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
247          strcpy(keymapname, "en-us");          strcpy(keymapname, "en-us");
248    
249            g_num_devices = 0;
250    
251  #ifdef RDP2VNC  #ifdef RDP2VNC
252  #define VNCOPT "V:E:"  #define VNCOPT "V:Q:"
253  #else  #else
254  #define VNCOPT  #define VNCOPT
255  #endif  #endif
256    
257          while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbemCKT:Dh?")) != -1)          while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbBeEmCDKS:T:Na:r:045h?")) != -1)
258          {          {
259                  switch (c)                  switch (c)
260                  {                  {
# Line 176  main(int argc, char *argv[]) Line 265  main(int argc, char *argv[])
265                                          rfb_port += 5900;                                          rfb_port += 5900;
266                                  break;                                  break;
267    
268                          case 'E':                          case 'Q':
269                                  defer_time = strtol(optarg, NULL, 10);                                  defer_time = strtol(optarg, NULL, 10);
270                                  if (defer_time < 0)                                  if (defer_time < 0)
271                                          defer_time = 0;                                          defer_time = 0;
# Line 184  main(int argc, char *argv[]) Line 273  main(int argc, char *argv[])
273  #endif  #endif
274    
275                          case 'u':                          case 'u':
276                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(g_username, optarg, sizeof(g_username));
277                                  username_option = 1;                                  username_option = 1;
278                                  break;                                  break;
279    
# Line 196  main(int argc, char *argv[]) Line 285  main(int argc, char *argv[])
285                                  STRNCPY(shell, optarg, sizeof(shell));                                  STRNCPY(shell, optarg, sizeof(shell));
286                                  break;                                  break;
287    
                         case 'S':  
                                 if (!strcmp(optarg, "standard"))  
                                 {  
                                         win_button_size = 18;  
                                         break;  
                                 }  
   
                                 win_button_size = strtol(optarg, &p, 10);  
   
                                 if (*p)  
                                 {  
                                         error("invalid button size\n");  
                                         return 1;  
                                 }  
   
                                 break;  
   
288                          case 'c':                          case 'c':
289                                  STRNCPY(directory, optarg, sizeof(directory));                                  STRNCPY(directory, optarg, sizeof(directory));
290                                  break;                                  break;
# Line 242  main(int argc, char *argv[]) Line 314  main(int argc, char *argv[])
314                                  break;                                  break;
315    
316                          case 'g':                          case 'g':
317                                    g_fullscreen = False;
318                                  if (!strcmp(optarg, "workarea"))                                  if (!strcmp(optarg, "workarea"))
319                                  {                                  {
320                                          width = height = 0;                                          g_width = g_height = 0;
321                                          break;                                          break;
322                                  }                                  }
323    
324                                  width = strtol(optarg, &p, 10);                                  g_width = strtol(optarg, &p, 10);
325                                    if (g_width <= 0)
326                                    {
327                                            error("invalid geometry\n");
328                                            return 1;
329                                    }
330    
331                                  if (*p == 'x')                                  if (*p == 'x')
332                                          height = strtol(p + 1, NULL, 10);                                          g_height = strtol(p + 1, NULL, 10);
333    
334                                  if ((width == 0) || (height == 0))                                  if (g_height <= 0)
335                                  {                                  {
336                                          error("invalid geometry\n");                                          error("invalid geometry\n");
337                                          return 1;                                          return 1;
338                                  }                                  }
339    
340                                    if (*p == '%')
341                                            g_width = -g_width;
342    
343                                  break;                                  break;
344    
345                          case 'f':                          case 'f':
346                                  fullscreen = True;                                  g_fullscreen = True;
347                                  break;                                  break;
348    
349                          case 'b':                          case 'b':
350                                  orders = False;                                  g_orders = False;
351                                  break;                                  break;
352    
353                          case 'e':                          case 'B':
354                                  encryption = False;                                  g_ownbackstore = False;
355                                  break;                                  break;
356    
357                            case 'e':
358                                    g_encryption = False;
359                                    break;
360                            case 'E':
361                                    packet_encryption = False;
362                                    break;
363                          case 'm':                          case 'm':
364                                  sendmotion = False;                                  g_sendmotion = False;
365                                  break;                                  break;
366    
367                          case 'C':                          case 'C':
368                                  owncolmap = True;                                  g_owncolmap = True;
369                                    break;
370    
371                            case 'D':
372                                    g_hide_decorations = True;
373                                  break;                                  break;
374    
375                          case 'K':                          case 'K':
376                                  grab_keyboard = False;                                  g_grab_keyboard = False;
377                                    break;
378    
379                            case 'S':
380                                    if (!strcmp(optarg, "standard"))
381                                    {
382                                            g_win_button_size = 18;
383                                            break;
384                                    }
385    
386                                    g_win_button_size = strtol(optarg, &p, 10);
387    
388                                    if (*p)
389                                    {
390                                            error("invalid button size\n");
391                                            return 1;
392                                    }
393    
394                                  break;                                  break;
395    
396                          case 'T':                          case 'T':
397                                  STRNCPY(title, optarg, sizeof(title));                                  STRNCPY(g_title, optarg, sizeof(g_title));
398                                  break;                                  break;
399    
400                          case 'D':                          case 'N':
401                                  hide_decorations = True;                                  g_numlock_sync = True;
402                                  break;                                  break;
403    
404                          case 'a':                          case 'a':
405                                  server_bpp = strtol(optarg, NULL, 10);                                  g_server_bpp = strtol(optarg, NULL, 10);
406                                  if (server_bpp != 8 && server_bpp != 16 && server_bpp != 15                                  if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
407                                      && server_bpp != 24)                                      && g_server_bpp != 24)
408                                  {                                  {
409                                          error("invalid server bpp\n");                                          error("invalid server bpp\n");
410                                          return 1;                                          return 1;
411                                  }                                  }
412                                  break;                                  break;
413    
414                            case 'r':
415    
416                                    if (strncmp("sound", optarg, 5) == 0)
417                                    {
418                                            optarg += 5;
419    
420                                            if (*optarg == ':')
421                                            {
422                                                    *optarg++;
423                                                    while ((p = next_arg(optarg, ',')))
424                                                    {
425                                                            if (strncmp("remote", optarg, 6) == 0)
426                                                                    flags |= RDP_LOGON_LEAVE_AUDIO;
427    
428                                                            if (strncmp("local", optarg, 5) == 0)
429    #ifdef WITH_RDPSND
430                                                                    g_rdpsnd = True;
431    #else
432                                                                    warning("Not compiled with sound support");
433    #endif
434    
435                                                            if (strncmp("off", optarg, 3) == 0)
436                                                                    g_rdpsnd = False;
437    
438                                                            optarg = p;
439                                                    }
440                                            }
441                                            else
442                                            {
443    #ifdef WITH_RDPSND
444                                                    g_rdpsnd = True;
445    #else
446                                                    warning("Not compiled with sound support");
447    #endif
448                                            }
449                                    }
450                                    else if (strncmp("disk", optarg, 4) == 0)
451                                    {
452                                            /* -r disk:h:=/mnt/floppy */
453                                            disk_enum_devices(&g_num_devices, optarg + 4);
454                                    }
455                                    else if (strncmp("comport", optarg, 7) == 0)
456                                    {
457                                            serial_enum_devices(&g_num_devices, optarg + 7);
458                                    }
459                                    else if (strncmp("lptport", optarg, 7) == 0)
460                                    {
461                                            parallel_enum_devices(&g_num_devices, optarg + 7);
462                                    }
463                                    else if (strncmp("printer", optarg, 7) == 0)
464                                    {
465                                            printer_enum_devices(&g_num_devices, optarg + 7);
466                                    }
467                                    else
468                                    {
469                                            warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n");
470                                    }
471                                    break;
472    
473                            case '0':
474                                    g_console_session = True;
475                                    break;
476    
477                            case '4':
478                                    g_use_rdp5 = False;
479                                    break;
480    
481                            case '5':
482                                    g_use_rdp5 = True;
483                                    break;
484    
485                          case 'h':                          case 'h':
486                          case '?':                          case '?':
487                          default:                          default:
# Line 309  main(int argc, char *argv[]) Line 490  main(int argc, char *argv[])
490                  }                  }
491          }          }
492    
493          if (argc - optind < 1)          if (argc - optind != 1)
494          {          {
495                  usage(argv[0]);                  usage(argv[0]);
496                  return 1;                  return 1;
497          }          }
498    
499          STRNCPY(server, argv[optind], sizeof(server));          STRNCPY(server, argv[optind], sizeof(server));
500          p = strchr(server, ':');          parse_server_and_port(server);
         if (p != NULL)  
         {  
                 tcp_port_rdp = strtol(p + 1, NULL, 10);  
                 *p = 0;  
         }  
501    
502          if (!username_option)          if (!username_option)
503          {          {
# Line 332  main(int argc, char *argv[]) Line 508  main(int argc, char *argv[])
508                          return 1;                          return 1;
509                  }                  }
510    
511                  STRNCPY(username, pw->pw_name, sizeof(username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
512          }          }
513    
514          if (hostname[0] == 0)          if (hostname[0] == 0)
# Line 353  main(int argc, char *argv[]) Line 529  main(int argc, char *argv[])
529          if (prompt_password && read_password(password, sizeof(password)))          if (prompt_password && read_password(password, sizeof(password)))
530                  flags |= RDP_LOGON_AUTO;                  flags |= RDP_LOGON_AUTO;
531    
532          if (title[0] == 0)          if (g_title[0] == 0)
533          {          {
534                  strcpy(title, "rdesktop - ");                  strcpy(g_title, "rdesktop - ");
535                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));                  strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
536          }          }
537    
538  #ifdef RDP2VNC  #ifdef RDP2VNC
539          rdp2vnc_connect(server, flags, domain, password, shell, directory);          rdp2vnc_connect(server, flags, domain, password, shell, directory);
540            return 0;
541  #else  #else
542    
543          if (!ui_init())          if (!ui_init())
544                  return 1;                  return 1;
545    
546    #ifdef WITH_RDPSND
547            if (g_rdpsnd)
548                    rdpsnd_init();
549    #endif
550            rdpdr_init();
551    
552          if (!rdp_connect(server, flags, domain, password, shell, directory))          if (!rdp_connect(server, flags, domain, password, shell, directory))
553                  return 1;                  return 1;
554    
555            /* By setting encryption to False here, we have an encrypted login
556               packet but unencrypted transfer of other packets */
557            if (!packet_encryption)
558                    g_encryption = False;
559    
560    
561          DEBUG(("Connection successful.\n"));          DEBUG(("Connection successful.\n"));
562          memset(password, 0, sizeof(password));          memset(password, 0, sizeof(password));
563    
564          if (ui_create_window())          if (ui_create_window())
565          {          {
566                  rdp_main_loop();                  rdp_retval = rdp_main_loop();
567                  ui_destroy_window();                  ui_destroy_window();
568          }          }
569    
# Line 382  main(int argc, char *argv[]) Line 571  main(int argc, char *argv[])
571          rdp_disconnect();          rdp_disconnect();
572          ui_deinit();          ui_deinit();
573    
574            if (True == rdp_retval)
575                    return 0;
576            else
577                    return 2;
578    
579  #endif  #endif
580    
         return 0;  
581  }  }
582    
583  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 547  void Line 740  void
740  hexdump(unsigned char *p, unsigned int len)  hexdump(unsigned char *p, unsigned int len)
741  {  {
742          unsigned char *line = p;          unsigned char *line = p;
743          unsigned int thisline, offset = 0;          int i, thisline, offset = 0;
         int i;  
744    
745          while (offset < len)          while (offset < len)
746          {          {
# Line 572  hexdump(unsigned char *p, unsigned int l Line 764  hexdump(unsigned char *p, unsigned int l
764          }          }
765  }  }
766    
767    /*
768      input: src is the string we look in for needle.
769             Needle may be escaped by a backslash, in
770             that case we ignore that particular needle.
771      return value: returns next src pointer, for
772            succesive executions, like in a while loop
773            if retval is 0, then there are no more args.
774      pitfalls:
775            src is modified. 0x00 chars are inserted to
776            terminate strings.
777            return val, points on the next val chr after ins
778            0x00
779    
780            example usage:
781            while( (pos = next_arg( optarg, ',')) ){
782                    printf("%s\n",optarg);
783                    optarg=pos;
784            }
785    
786    */
787    char *
788    next_arg(char *src, char needle)
789    {
790            char *nextval;
791            char *p;
792            char *mvp = 0;
793    
794            /* EOS */
795            if (*src == (char) 0x00)
796                    return 0;
797    
798            p = src;
799            /*  skip escaped needles */
800            while ((nextval = strchr(p, needle)))
801            {
802                    mvp = nextval - 1;
803                    /* found backslashed needle */
804                    if (*mvp == '\\' && (mvp > src))
805                    {
806                            /* move string one to the left */
807                            while (*(mvp + 1) != (char) 0x00)
808                            {
809                                    *mvp = *(mvp + 1);
810                                    *mvp++;
811                            }
812                            *mvp = (char) 0x00;
813                            p = nextval;
814                    }
815                    else
816                    {
817                            p = nextval + 1;
818                            break;
819                    }
820    
821            }
822    
823            /* more args available */
824            if (nextval)
825            {
826                    *nextval = (char) 0x00;
827                    return ++nextval;
828            }
829    
830            /* no more args after this, jump to EOS */
831            nextval = src + strlen(src);
832            return nextval;
833    }
834    
835    
836    void
837    toupper_str(char *p)
838    {
839            while (*p)
840            {
841                    if ((*p >= 'a') && (*p <= 'z'))
842                            *p = toupper((int) *p);
843                    p++;
844            }
845    }
846    
847    
848    /* not all clibs got ltoa */
849    #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
850    
851    char *
852    l_to_a(long N, int base)
853    {
854            static char ret[LTOA_BUFSIZE];
855    
856            char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf);
857    
858            register int divrem;
859    
860            if (base < 36 || 2 > base)
861                    base = 10;
862    
863            if (N < 0)
864            {
865                    *head++ = '-';
866                    N = -N;
867            }
868    
869            tail = buf + sizeof(buf);
870            *--tail = 0;
871    
872            do
873            {
874                    divrem = N % base;
875                    *--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10;
876                    N /= base;
877            }
878            while (N);
879    
880            strcpy(head, tail);
881            return ret;
882    }
883    
884    
885  int  int
886  load_licence(unsigned char **data)  load_licence(unsigned char **data)
887  {  {
888          char *path;          char *home, *path;
         char *home;  
889          struct stat st;          struct stat st;
890          int fd;          int fd, length;
891    
892          home = getenv("HOME");          home = getenv("HOME");
893          if (home == NULL)          if (home == NULL)
894                  return -1;                  return -1;
895    
896          path = xmalloc(strlen(home) + strlen(hostname) + 20);          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
897          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
898    
899          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
# Line 595  load_licence(unsigned char **data) Line 903  load_licence(unsigned char **data)
903          if (fstat(fd, &st))          if (fstat(fd, &st))
904                  return -1;                  return -1;
905    
906          *data = xmalloc(st.st_size);          *data = (uint8 *) xmalloc(st.st_size);
907          return read(fd, *data, st.st_size);          length = read(fd, *data, st.st_size);
908            close(fd);
909            xfree(path);
910            return length;
911  }  }
912    
913  void  void
914  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
915  {  {
916          char *fpath;            /* file path for licence */          char *home, *path, *tmppath;
917          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,  
                                (unsigned int) s_ptr->p, (unsigned int) 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",  
                        (unsigned int) s_ptr->p + len, (unsigned int) data + length);  
                 return;  
         }  
918    
919          home = getenv("HOME");          home = getenv("HOME");
920          if (home == NULL)          if (home == NULL)
921                  return;                  return;
922    
923          /* 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);  
924    
925          sprintf(fpath, "%s/.rdesktop", fpath);          sprintf(path, "%s/.rdesktop", home);
926          if (mkdir(fpath, 0700) == -1 && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
927          {          {
928                  perror("mkdir");                  perror(path);
929                  exit(1);                  return;
930          }          }
931    
932          /* set the real licence filename, and put a write lock on it. */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
933          fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);  
934          sprintf(fname, "%s/licence.%s", fpath, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
935          fnfd = open(fname, O_RDONLY);          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
936          if (fnfd != -1)          strcpy(tmppath, path);
937          {          strcat(tmppath, ".new");
                 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, (long unsigned int) 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;  
         }  
938    
939          /* close the file and rename it to fname */          fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
940          if (close(fnwrkfd) == -1)          if (fd == -1)
941          {          {
942                  perror("close");                  perror(tmppath);
943                  unlink(fnamewrk);                  return;
                 exit(1);  
944          }          }
945          if (rename(fnamewrk, fname) == -1)  
946            if (write(fd, data, length) != length)
947          {          {
948                  perror("rename");                  perror(tmppath);
949                  unlink(fnamewrk);                  unlink(tmppath);
                 exit(1);  
950          }          }
951          /* close the file lock on fname */          else if (rename(tmppath, path) == -1)
         if (fnfd != -1)  
952          {          {
953                  fnfl.l_type = F_UNLCK;                  perror(path);
954                  fnfl.l_whence = SEEK_SET;                  unlink(tmppath);
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
                 close(fnfd);  
955          }          }
956    
957            close(fd);
958            xfree(tmppath);
959            xfree(path);
960  }  }

Legend:
Removed from v.333  
changed lines
  Added in v.630

  ViewVC Help
Powered by ViewVC 1.1.26