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

Legend:
Removed from v.297  
changed lines
  Added in v.569

  ViewVC Help
Powered by ViewVC 1.1.26