/[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 262 by astrand, Mon Nov 18 15:37:20 2002 UTC revision 309 by jsorg71, Tue Feb 4 05:32:13 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 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 */
24    #include <errno.h>              /* save licence uses it. */
25  #include <pwd.h>                /* getpwuid */  #include <pwd.h>                /* getpwuid */
 #include <limits.h>             /* PATH_MAX */  
26  #include <termios.h>            /* tcgetattr tcsetattr */  #include <termios.h>            /* tcgetattr tcsetattr */
27  #include <sys/stat.h>           /* stat */  #include <sys/stat.h>           /* stat */
28  #include <sys/time.h>           /* gettimeofday */  #include <sys/time.h>           /* gettimeofday */
# 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  BOOL bitmap_compression = True;  BOOL bitmap_compression = True;
54  BOOL sendmotion = True;  BOOL sendmotion = True;
55  BOOL orders = True;  BOOL orders = True;
# Line 56  BOOL desktop_save = True; Line 58  BOOL desktop_save = True;
58  BOOL fullscreen = False;  BOOL fullscreen = False;
59  BOOL grab_keyboard = True;  BOOL grab_keyboard = True;
60  BOOL hide_decorations = False;  BOOL hide_decorations = False;
61    extern BOOL owncolmap;
62    
63  /* Display usage information */  /* Display usage information */
64  static void  static void
65  usage(char *program)  usage(char *program)
66  {  {
67          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
68          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
69          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
70    
71          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
# Line 78  usage(char *program) Line 81  usage(char *program)
81          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
82          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
83          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
84            fprintf(stderr, "   -C: use private colour map\n");
85          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
86          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
87          fprintf(stderr, "   -D: hide window manager decorations\n");          fprintf(stderr, "   -D: hide window manager decorations\n");
88            fprintf(stderr, "   -a: server bpp\n");
89  }  }
90    
91  static BOOL  static BOOL
# Line 134  main(int argc, char *argv[]) Line 139  main(int argc, char *argv[])
139          uint32 flags;          uint32 flags;
140          char *p;          char *p;
141          int c;          int c;
142            int username_option = 0;
143    
144          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
145          prompt_password = False;          prompt_password = False;
146          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
147          strcpy(keymapname, "en-us");          strcpy(keymapname, "en-us");
148    
149          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemKT:Dh?")) != -1)          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:a:fbemCKT:Dh?")) != -1)
150          {          {
151                  switch (c)                  switch (c)
152                  {                  {
153                          case 'u':                          case 'u':
154                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(username, optarg, sizeof(username));
155                                    username_option = 1;
156                                  break;                                  break;
157    
158                          case 'd':                          case 'd':
# Line 185  main(int argc, char *argv[]) Line 192  main(int argc, char *argv[])
192                                  break;                                  break;
193    
194                          case 'g':                          case 'g':
195                                    if (!strcmp(optarg, "workarea"))
196                                    {
197                                            width = height = 0;
198                                            break;
199                                    }
200    
201                                  width = strtol(optarg, &p, 10);                                  width = strtol(optarg, &p, 10);
202                                  if (*p == 'x')                                  if (*p == 'x')
203                                          height = strtol(p + 1, NULL, 10);                                          height = strtol(p + 1, NULL, 10);
# Line 212  main(int argc, char *argv[]) Line 225  main(int argc, char *argv[])
225                                  sendmotion = False;                                  sendmotion = False;
226                                  break;                                  break;
227    
228                            case 'C':
229                                    owncolmap = True;
230                                    break;
231    
232                          case 'K':                          case 'K':
233                                  grab_keyboard = False;                                  grab_keyboard = False;
234                                  break;                                  break;
# Line 224  main(int argc, char *argv[]) Line 241  main(int argc, char *argv[])
241                                  hide_decorations = True;                                  hide_decorations = True;
242                                  break;                                  break;
243    
244                            case 'a':
245                                    server_bpp = strtol(optarg, NULL, 10);
246                                    if (server_bpp != 8 && server_bpp != 16)
247                                    {
248                                            error("invalid server bpp\n");
249                                            return 1;
250                                    }
251                                    break;
252    
253                          case 'h':                          case 'h':
254                          case '?':                          case '?':
255                          default:                          default:
# Line 246  main(int argc, char *argv[]) Line 272  main(int argc, char *argv[])
272                  *p = 0;                  *p = 0;
273          }          }
274    
275          if (username[0] == 0)          if (!username_option)
276          {          {
277                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
278                  if ((pw == NULL) || (pw->pw_name == NULL))                  if ((pw == NULL) || (pw->pw_name == NULL))
# Line 432  error(char *format, ...) Line 458  error(char *format, ...)
458          va_end(ap);          va_end(ap);
459  }  }
460    
461    /* report a warning */
462    void
463    warning(char *format, ...)
464    {
465            va_list ap;
466    
467            fprintf(stderr, "WARNING: ");
468    
469            va_start(ap, format);
470            vfprintf(stderr, format, ap);
471            va_end(ap);
472    }
473    
474  /* report an unimplemented protocol feature */  /* report an unimplemented protocol feature */
475  void  void
476  unimpl(char *format, ...)  unimpl(char *format, ...)
# Line 479  hexdump(unsigned char *p, unsigned int l Line 518  hexdump(unsigned char *p, unsigned int l
518  int  int
519  load_licence(unsigned char **data)  load_licence(unsigned char **data)
520  {  {
521          char path[PATH_MAX];          char *path;
522          char *home;          char *home;
523          struct stat st;          struct stat st;
524          int fd;          int fd;
# Line 488  load_licence(unsigned char **data) Line 527  load_licence(unsigned char **data)
527          if (home == NULL)          if (home == NULL)
528                  return -1;                  return -1;
529    
530          STRNCPY(path, home, sizeof(path));          path = xmalloc(strlen(home) + strlen(hostname) + 20);
531          strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
532    
533          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
534          if (fd == -1)          if (fd == -1)
# Line 505  load_licence(unsigned char **data) Line 544  load_licence(unsigned char **data)
544  void  void
545  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
546  {  {
547          char path[PATH_MAX];          char *fpath;            /* file path for licence */
548            char *fname, *fnamewrk; /* file name for licence .inkl path. */
549          char *home;          char *home;
550          int fd;          uint32 y;
551            struct flock fnfl;
552            int fnfd, fnwrkfd, i, wlen;
553            struct stream s, *s_ptr;
554            uint32 len;
555    
556            /* Construct a stream, so that we can use macros to extract the
557             * licence.
558             */
559            s_ptr = &s;
560            s_ptr->p = data;
561            /* Skip first two bytes */
562            in_uint16(s_ptr, len);
563    
564            /* Skip three strings */
565            for (i = 0; i < 3; i++)
566            {
567                    in_uint32(s_ptr, len);
568                    s_ptr->p += len;
569                    /* Make sure that we won't be past the end of data after
570                     * reading the next length value
571                     */
572                    if ((s_ptr->p) + 4 > data + length)
573                    {
574                            printf("Error in parsing licence key.\n");
575                            printf("Strings %d end value %x > supplied length (%x)\n",
576                                   i, s_ptr->p, data + length);
577                            return;
578                    }
579            }
580            in_uint32(s_ptr, len);
581            if (s_ptr->p + len > data + length)
582            {
583                    printf("Error in parsing licence key.\n");
584                    printf("End of licence %x > supplied length (%x)\n", s_ptr->p + len, data + length);
585                    return;
586            }
587    
588          home = getenv("HOME");          home = getenv("HOME");
589          if (home == NULL)          if (home == NULL)
590                  return;                  return;
591    
592          STRNCPY(path, home, sizeof(path));          /* set and create the directory -- if it doesn't exist. */
593          strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1);          fpath = xmalloc(strlen(home) + 11);
594          mkdir(path, 0700);          STRNCPY(fpath, home, strlen(home) + 1);
595    
596          strncat(path, "/licence", sizeof(path) - strlen(path) - 1);          sprintf(fpath, "%s/.rdesktop", fpath);
597            if (mkdir(fpath, 0700) == -1 && errno != EEXIST)
598            {
599                    perror("mkdir");
600                    exit(1);
601            }
602    
603          fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);          /* set the real licence filename, and put a write lock on it. */
604          if (fd == -1)          fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);
605            sprintf(fname, "%s/licence.%s", fpath, hostname);
606            fnfd = open(fname, O_RDONLY);
607            if (fnfd != -1)
608            {
609                    fnfl.l_type = F_WRLCK;
610                    fnfl.l_whence = SEEK_SET;
611                    fnfl.l_start = 0;
612                    fnfl.l_len = 1;
613                    fcntl(fnfd, F_SETLK, &fnfl);
614            }
615    
616            /* create a temporary licence file */
617            fnamewrk = xmalloc(strlen(fname) + 12);
618            for (y = 0;; y++)
619            {
620                    sprintf(fnamewrk, "%s.%lu", fname, y);
621                    fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600);
622                    if (fnwrkfd == -1)
623                    {
624                            if (errno == EINTR || errno == EEXIST)
625                                    continue;
626                            perror("create");
627                            exit(1);
628                    }
629                    break;
630            }
631            /* write to the licence file */
632            for (y = 0; y < len;)
633          {          {
634                  perror("open");                  do
635                  return;                  {
636                            wlen = write(fnwrkfd, s_ptr->p + y, len - y);
637                    }
638                    while (wlen == -1 && errno == EINTR);
639                    if (wlen < 1)
640                    {
641                            perror("write");
642                            unlink(fnamewrk);
643                            exit(1);
644                    }
645                    y += wlen;
646            }
647    
648            /* close the file and rename it to fname */
649            if (close(fnwrkfd) == -1)
650            {
651                    perror("close");
652                    unlink(fnamewrk);
653                    exit(1);
654            }
655            if (rename(fnamewrk, fname) == -1)
656            {
657                    perror("rename");
658                    unlink(fnamewrk);
659                    exit(1);
660            }
661            /* close the file lock on fname */
662            if (fnfd != -1)
663            {
664                    fnfl.l_type = F_UNLCK;
665                    fnfl.l_whence = SEEK_SET;
666                    fnfl.l_start = 0;
667                    fnfl.l_len = 1;
668                    fcntl(fnfd, F_SETLK, &fnfl);
669                    close(fnfd);
670          }          }
671    
         write(fd, data, length);  
         close(fd);  
672  }  }
673  #endif  #endif

Legend:
Removed from v.262  
changed lines
  Added in v.309

  ViewVC Help
Powered by ViewVC 1.1.26