/[rdesktop]/jpeg/rdesktop/trunk/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 /jpeg/rdesktop/trunk/rdesktop.c

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

revision 223 by matthewc, Fri Oct 11 05:13:19 2002 UTC revision 329 by astrand, Tue Feb 18 13:07:29 2003 UTC
# Line 1  Line 1 
1  /*  /*
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 22  Line 22 
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 */
24  #include <pwd.h>                /* getpwuid */  #include <pwd.h>                /* getpwuid */
 #include <limits.h>             /* PATH_MAX */  
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 45  char username[16]; Line 45  char username[16];
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;  int width = 800;                /* If width or height are reset to zero, the geometry will
49                                       be fetched from _NET_WORKAREA */
50  int height = 600;  int height = 600;
51  int tcp_port_rdp = TCP_PORT_RDP;  int tcp_port_rdp = TCP_PORT_RDP;
52    int server_bpp = 8;
53    int win_button_size = 0;        /* If zero, disable single app mode */
54  BOOL bitmap_compression = True;  BOOL bitmap_compression = True;
55  BOOL sendmotion = True;  BOOL sendmotion = True;
56  BOOL orders = True;  BOOL orders = True;
# Line 55  BOOL encryption = True; Line 58  BOOL encryption = True;
58  BOOL desktop_save = True;  BOOL desktop_save = True;
59  BOOL fullscreen = False;  BOOL fullscreen = False;
60  BOOL grab_keyboard = True;  BOOL grab_keyboard = True;
61    BOOL hide_decorations = False;
62    extern BOOL owncolmap;
63    
64  /* Display usage information */  /* Display usage information */
65  static void  static void
66  usage(char *program)  usage(char *program)
67  {  {
68          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
69          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
70          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
71    
72          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
73          fprintf(stderr, "   -u: user name\n");          fprintf(stderr, "   -u: user name\n");
74          fprintf(stderr, "   -d: domain\n");          fprintf(stderr, "   -d: domain\n");
75          fprintf(stderr, "   -s: shell\n");          fprintf(stderr, "   -s: shell\n");
76            fprintf(stderr, "   -S: caption button size (single application mode)\n");
77          fprintf(stderr, "   -c: working directory\n");          fprintf(stderr, "   -c: working directory\n");
78          fprintf(stderr, "   -p: password (- to prompt)\n");          fprintf(stderr, "   -p: password (- to prompt)\n");
79          fprintf(stderr, "   -n: client hostname\n");          fprintf(stderr, "   -n: client hostname\n");
# Line 77  usage(char *program) Line 83  usage(char *program)
83          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
84          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
85          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
86            fprintf(stderr, "   -C: use private colour map\n");
87          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
88          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
89            fprintf(stderr, "   -D: hide window manager decorations\n");
90            fprintf(stderr, "   -a: server bpp\n");
91  }  }
92    
93  static BOOL  static BOOL
# Line 125  main(int argc, char *argv[]) Line 134  main(int argc, char *argv[])
134          char fullhostname[64];          char fullhostname[64];
135          char domain[16];          char domain[16];
136          char password[16];          char password[16];
137          char shell[32];          char shell[128];
138          char directory[32];          char directory[32];
139          BOOL prompt_password;          BOOL prompt_password;
140          struct passwd *pw;          struct passwd *pw;
141          uint32 flags;          uint32 flags;
142          char *p;          char *p;
143          int c;          int c;
144            int username_option = 0;
145    
146          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
147          prompt_password = False;          prompt_password = False;
148          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
149          strcpy(keymapname, "us");          strcpy(keymapname, "en-us");
150    
151          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemKT:h?")) != -1)          while ((c = getopt(argc, argv, "u:d:s:S:c:p:n:k:g:a:fbemCKT:Dh?")) != -1)
152          {          {
153                  switch (c)                  switch (c)
154                  {                  {
155                          case 'u':                          case 'u':
156                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(username, optarg, sizeof(username));
157                                    username_option = 1;
158                                  break;                                  break;
159    
160                          case 'd':                          case 'd':
# Line 154  main(int argc, char *argv[]) Line 165  main(int argc, char *argv[])
165                                  STRNCPY(shell, optarg, sizeof(shell));                                  STRNCPY(shell, optarg, sizeof(shell));
166                                  break;                                  break;
167    
168                            case 'S':
169                                    if (!strcmp(optarg, "standard"))
170                                    {
171                                            win_button_size = 18;
172                                            break;
173                                    }
174    
175                                    win_button_size = strtol(optarg, &p, 10);
176    
177                                    if (*p)
178                                    {
179                                            error("invalid button size\n");
180                                            return 1;
181                                    }
182    
183                                    break;
184    
185                          case 'c':                          case 'c':
186                                  STRNCPY(directory, optarg, sizeof(directory));                                  STRNCPY(directory, optarg, sizeof(directory));
187                                  break;                                  break;
# Line 183  main(int argc, char *argv[]) Line 211  main(int argc, char *argv[])
211                                  break;                                  break;
212    
213                          case 'g':                          case 'g':
214                                    if (!strcmp(optarg, "workarea"))
215                                    {
216                                            width = height = 0;
217                                            break;
218                                    }
219    
220                                  width = strtol(optarg, &p, 10);                                  width = strtol(optarg, &p, 10);
221                                  if (*p == 'x')                                  if (*p == 'x')
222                                          height = strtol(p + 1, NULL, 10);                                          height = strtol(p + 1, NULL, 10);
# Line 210  main(int argc, char *argv[]) Line 244  main(int argc, char *argv[])
244                                  sendmotion = False;                                  sendmotion = False;
245                                  break;                                  break;
246    
247                            case 'C':
248                                    owncolmap = True;
249                                    break;
250    
251                          case 'K':                          case 'K':
252                                  grab_keyboard = False;                                  grab_keyboard = False;
253                                  break;                                  break;
# Line 218  main(int argc, char *argv[]) Line 256  main(int argc, char *argv[])
256                                  STRNCPY(title, optarg, sizeof(title));                                  STRNCPY(title, optarg, sizeof(title));
257                                  break;                                  break;
258    
259                            case 'D':
260                                    hide_decorations = True;
261                                    break;
262    
263                            case 'a':
264                                    server_bpp = strtol(optarg, NULL, 10);
265                                    if (server_bpp != 8 && server_bpp != 16 && server_bpp != 15
266                                        && server_bpp != 24)
267                                    {
268                                            error("invalid server bpp\n");
269                                            return 1;
270                                    }
271                                    break;
272    
273                          case 'h':                          case 'h':
274                          case '?':                          case '?':
275                          default:                          default:
# Line 240  main(int argc, char *argv[]) Line 292  main(int argc, char *argv[])
292                  *p = 0;                  *p = 0;
293          }          }
294    
295          if (username[0] == 0)          if (!username_option)
296          {          {
297                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
298                  if ((pw == NULL) || (pw->pw_name == NULL))                  if ((pw == NULL) || (pw->pw_name == NULL))
# Line 312  generate_random_egd(uint8 * buf) Line 364  generate_random_egd(uint8 * buf)
364    
365          addr.sun_family = AF_UNIX;          addr.sun_family = AF_UNIX;
366          memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));          memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
367          if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)          if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
368                  goto err;                  goto err;
369    
370          /* PRNGD and EGD use a simple communications protocol */          /* PRNGD and EGD use a simple communications protocol */
371          buf[0] = 1;  /* Non-blocking (similar to /dev/urandom) */          buf[0] = 1;             /* Non-blocking (similar to /dev/urandom) */
372          buf[1] = 32; /* Number of requested random bytes */          buf[1] = 32;            /* Number of requested random bytes */
373          if (write(fd, buf, 2) != 2)          if (write(fd, buf, 2) != 2)
374                  goto err;                  goto err;
375    
376          if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */          if ((read(fd, buf, 1) != 1) || (buf[0] == 0))   /* Available? */
377                  goto err;                  goto err;
378    
379          if (read(fd, buf, 32) != 32)          if (read(fd, buf, 32) != 32)
# Line 329  generate_random_egd(uint8 * buf) Line 381  generate_random_egd(uint8 * buf)
381    
382          ret = True;          ret = True;
383    
384  err:        err:
385          close(fd);          close(fd);
386          return ret;          return ret;
387  }  }
# Line 362  generate_random(uint8 * random) Line 414  generate_random(uint8 * random)
414  #endif  #endif
415    
416          /* Otherwise use whatever entropy we can gather - ideas welcome. */          /* Otherwise use whatever entropy we can gather - ideas welcome. */
417          r = (uint32 *)random;          r = (uint32 *) random;
418          r[0] = (getpid()) | (getppid() << 16);          r[0] = (getpid()) | (getppid() << 16);
419          r[1] = (getuid()) | (getgid() << 16);          r[1] = (getuid()) | (getgid() << 16);
420          r[2] = times(&tmsbuf);  /* system uptime (clocks) */          r[2] = times(&tmsbuf);  /* system uptime (clocks) */
# Line 374  generate_random(uint8 * random) Line 426  generate_random(uint8 * random)
426    
427          /* Hash both halves with MD5 to obscure possible patterns */          /* Hash both halves with MD5 to obscure possible patterns */
428          MD5_Init(&md5);          MD5_Init(&md5);
429          MD5_Update(&md5, random, 16);          MD5_Update(&md5, random, 16);
430          MD5_Final(random, &md5);          MD5_Final(random, &md5);
431          MD5_Update(&md5, random+16, 16);          MD5_Update(&md5, random + 16, 16);
432          MD5_Final(random+16, &md5);          MD5_Final(random + 16, &md5);
433  }  }
434    
435  /* malloc; exit if out of memory */  /* malloc; exit if out of memory */
# Line 426  error(char *format, ...) Line 478  error(char *format, ...)
478          va_end(ap);          va_end(ap);
479  }  }
480    
481    /* report a warning */
482    void
483    warning(char *format, ...)
484    {
485            va_list ap;
486    
487            fprintf(stderr, "WARNING: ");
488    
489            va_start(ap, format);
490            vfprintf(stderr, format, ap);
491            va_end(ap);
492    }
493    
494  /* report an unimplemented protocol feature */  /* report an unimplemented protocol feature */
495  void  void
496  unimpl(char *format, ...)  unimpl(char *format, ...)
# Line 469  hexdump(unsigned char *p, unsigned int l Line 534  hexdump(unsigned char *p, unsigned int l
534          }          }
535  }  }
536    
537  #ifdef SAVE_LICENCE  
538  int  int
539  load_licence(unsigned char **data)  load_licence(unsigned char **data)
540  {  {
541          char path[PATH_MAX];          char *path;
542          char *home;          char *home;
543          struct stat st;          struct stat st;
544          int fd;          int fd;
# Line 482  load_licence(unsigned char **data) Line 547  load_licence(unsigned char **data)
547          if (home == NULL)          if (home == NULL)
548                  return -1;                  return -1;
549    
550          STRNCPY(path, home, sizeof(path));          path = xmalloc(strlen(home) + strlen(hostname) + 20);
551          strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
552    
553          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
554          if (fd == -1)          if (fd == -1)
# Line 499  load_licence(unsigned char **data) Line 564  load_licence(unsigned char **data)
564  void  void
565  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
566  {  {
567          char path[PATH_MAX];          char *fpath;            /* file path for licence */
568            char *fname, *fnamewrk; /* file name for licence .inkl path. */
569          char *home;          char *home;
570          int fd;          uint32 y;
571            struct flock fnfl;
572            int fnfd, fnwrkfd, i, wlen;
573            struct stream s, *s_ptr;
574            uint32 len;
575    
576            /* Construct a stream, so that we can use macros to extract the
577             * licence.
578             */
579            s_ptr = &s;
580            s_ptr->p = data;
581            /* Skip first two bytes */
582            in_uint16(s_ptr, len);
583    
584            /* Skip three strings */
585            for (i = 0; i < 3; i++)
586            {
587                    in_uint32(s_ptr, len);
588                    s_ptr->p += len;
589                    /* Make sure that we won't be past the end of data after
590                     * reading the next length value
591                     */
592                    if ((s_ptr->p) + 4 > data + length)
593                    {
594                            printf("Error in parsing licence key.\n");
595                            printf("Strings %d end value %x > supplied length (%x)\n", i,
596                                   (unsigned int) s_ptr->p, (unsigned int) data + length);
597                            return;
598                    }
599            }
600            in_uint32(s_ptr, len);
601            if (s_ptr->p + len > data + length)
602            {
603                    printf("Error in parsing licence key.\n");
604                    printf("End of licence %x > supplied length (%x)\n",
605                           (unsigned int) s_ptr->p + len, (unsigned int) data + length);
606                    return;
607            }
608    
609          home = getenv("HOME");          home = getenv("HOME");
610          if (home == NULL)          if (home == NULL)
611                  return;                  return;
612    
613          STRNCPY(path, home, sizeof(path));          /* set and create the directory -- if it doesn't exist. */
614          strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1);          fpath = xmalloc(strlen(home) + 11);
615          mkdir(path, 0700);          STRNCPY(fpath, home, strlen(home) + 1);
616    
617          strncat(path, "/licence", sizeof(path) - strlen(path) - 1);          sprintf(fpath, "%s/.rdesktop", fpath);
618            if (mkdir(fpath, 0700) == -1 && errno != EEXIST)
619            {
620                    perror("mkdir");
621                    exit(1);
622            }
623    
624          fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);          /* set the real licence filename, and put a write lock on it. */
625          if (fd == -1)          fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);
626            sprintf(fname, "%s/licence.%s", fpath, hostname);
627            fnfd = open(fname, O_RDONLY);
628            if (fnfd != -1)
629            {
630                    fnfl.l_type = F_WRLCK;
631                    fnfl.l_whence = SEEK_SET;
632                    fnfl.l_start = 0;
633                    fnfl.l_len = 1;
634                    fcntl(fnfd, F_SETLK, &fnfl);
635            }
636    
637            /* create a temporary licence file */
638            fnamewrk = xmalloc(strlen(fname) + 12);
639            for (y = 0;; y++)
640            {
641                    sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int) y);
642                    fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600);
643                    if (fnwrkfd == -1)
644                    {
645                            if (errno == EINTR || errno == EEXIST)
646                                    continue;
647                            perror("create");
648                            exit(1);
649                    }
650                    break;
651            }
652            /* write to the licence file */
653            for (y = 0; y < len;)
654          {          {
655                  perror("open");                  do
656                  return;                  {
657                            wlen = write(fnwrkfd, s_ptr->p + y, len - y);
658                    }
659                    while (wlen == -1 && errno == EINTR);
660                    if (wlen < 1)
661                    {
662                            perror("write");
663                            unlink(fnamewrk);
664                            exit(1);
665                    }
666                    y += wlen;
667            }
668    
669            /* close the file and rename it to fname */
670            if (close(fnwrkfd) == -1)
671            {
672                    perror("close");
673                    unlink(fnamewrk);
674                    exit(1);
675            }
676            if (rename(fnamewrk, fname) == -1)
677            {
678                    perror("rename");
679                    unlink(fnamewrk);
680                    exit(1);
681            }
682            /* close the file lock on fname */
683            if (fnfd != -1)
684            {
685                    fnfl.l_type = F_UNLCK;
686                    fnfl.l_whence = SEEK_SET;
687                    fnfl.l_start = 0;
688                    fnfl.l_len = 1;
689                    fcntl(fnfd, F_SETLK, &fnfl);
690                    close(fnfd);
691          }          }
692    
         write(fd, data, length);  
         close(fd);  
693  }  }
 #endif  

Legend:
Removed from v.223  
changed lines
  Added in v.329

  ViewVC Help
Powered by ViewVC 1.1.26