/[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 315 by jsorg71, Sun Feb 9 01:38:07 2003 UTC revision 435 by astrand, Wed Jul 9 09:18:20 2003 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 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 50  int width = 800;               /* If width or height Line 50  int width = 800;               /* If width or height
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;  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;
57  BOOL encryption = True;  BOOL encryption = True;
58    BOOL packet_encryption = True;
59  BOOL desktop_save = True;  BOOL desktop_save = True;
60  BOOL fullscreen = False;  BOOL fullscreen = False;
61  BOOL grab_keyboard = True;  BOOL grab_keyboard = True;
62  BOOL hide_decorations = False;  BOOL hide_decorations = False;
63    BOOL use_rdp5 = False;
64  extern BOOL owncolmap;  extern BOOL owncolmap;
65    
66    #ifdef RDP2VNC
67    extern int rfb_port;
68    extern int defer_time;
69    void
70    rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
71                    char *shell, char *directory);
72    #endif
73  /* Display usage information */  /* Display usage information */
74  static void  static void
75  usage(char *program)  usage(char *program)
# Line 69  usage(char *program) Line 79  usage(char *program)
79          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
80    
81          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
82    #ifdef RDP2VNC
83            fprintf(stderr, "   -V: vnc port\n");
84            fprintf(stderr, "   -E: defer time (ms)\n");
85    #endif
86          fprintf(stderr, "   -u: user name\n");          fprintf(stderr, "   -u: user name\n");
87          fprintf(stderr, "   -d: domain\n");          fprintf(stderr, "   -d: domain\n");
88          fprintf(stderr, "   -s: shell\n");          fprintf(stderr, "   -s: shell\n");
89            fprintf(stderr, "   -S: caption button size (single application mode)\n");
90          fprintf(stderr, "   -c: working directory\n");          fprintf(stderr, "   -c: working directory\n");
91          fprintf(stderr, "   -p: password (- to prompt)\n");          fprintf(stderr, "   -p: password (- to prompt)\n");
92          fprintf(stderr, "   -n: client hostname\n");          fprintf(stderr, "   -n: client hostname\n");
# Line 80  usage(char *program) Line 95  usage(char *program)
95          fprintf(stderr, "   -f: full-screen mode\n");          fprintf(stderr, "   -f: full-screen mode\n");
96          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
97          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
98            fprintf(stderr, "   -E: disable encryption of everything but the logon packet\n");
99          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
100          fprintf(stderr, "   -C: use private colour map\n");          fprintf(stderr, "   -C: use private colour map\n");
101          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
102          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
103          fprintf(stderr, "   -D: hide window manager decorations\n");          fprintf(stderr, "   -D: hide window manager decorations\n");
104          fprintf(stderr, "   -a: server bpp\n");          fprintf(stderr, "   -a: server bpp\n");
105            fprintf(stderr, "   -5: Use RDP5 (EXPERIMENTAL!)\n");
106  }  }
107    
108  static BOOL  static BOOL
# Line 134  main(int argc, char *argv[]) Line 151  main(int argc, char *argv[])
151          char password[16];          char password[16];
152          char shell[128];          char shell[128];
153          char directory[32];          char directory[32];
154          BOOL prompt_password;          BOOL prompt_password, rdp_retval = False;
155          struct passwd *pw;          struct passwd *pw;
156          uint32 flags;          uint32 flags;
157          char *p;          char *p;
# Line 146  main(int argc, char *argv[]) Line 163  main(int argc, char *argv[])
163          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
164          strcpy(keymapname, "en-us");          strcpy(keymapname, "en-us");
165    
166          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:a:fbemCKT:Dh?")) != -1)  #ifdef RDP2VNC
167    #define VNCOPT "V:E:"
168    #else
169    #define VNCOPT
170    #endif
171    
172            while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbeEmCKT:Dh?54")) != -1)
173          {          {
174                  switch (c)                  switch (c)
175                  {                  {
176    #ifdef RDP2VNC
177                            case 'V':
178                                    rfb_port = strtol(optarg, NULL, 10);
179                                    if (rfb_port < 100)
180                                            rfb_port += 5900;
181                                    break;
182    
183                            case 'E':
184                                    defer_time = strtol(optarg, NULL, 10);
185                                    if (defer_time < 0)
186                                            defer_time = 0;
187                                    break;
188    #endif
189    
190                          case 'u':                          case 'u':
191                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(username, optarg, sizeof(username));
192                                  username_option = 1;                                  username_option = 1;
# Line 163  main(int argc, char *argv[]) Line 200  main(int argc, char *argv[])
200                                  STRNCPY(shell, optarg, sizeof(shell));                                  STRNCPY(shell, optarg, sizeof(shell));
201                                  break;                                  break;
202    
203                            case 'S':
204                                    if (!strcmp(optarg, "standard"))
205                                    {
206                                            win_button_size = 18;
207                                            break;
208                                    }
209    
210                                    win_button_size = strtol(optarg, &p, 10);
211    
212                                    if (*p)
213                                    {
214                                            error("invalid button size\n");
215                                            return 1;
216                                    }
217    
218                                    break;
219    
220                          case 'c':                          case 'c':
221                                  STRNCPY(directory, optarg, sizeof(directory));                                  STRNCPY(directory, optarg, sizeof(directory));
222                                  break;                                  break;
# Line 220  main(int argc, char *argv[]) Line 274  main(int argc, char *argv[])
274                          case 'e':                          case 'e':
275                                  encryption = False;                                  encryption = False;
276                                  break;                                  break;
277                            case 'E':
278                                    packet_encryption = False;
279                                    break;
280                          case 'm':                          case 'm':
281                                  sendmotion = False;                                  sendmotion = False;
282                                  break;                                  break;
# Line 243  main(int argc, char *argv[]) Line 299  main(int argc, char *argv[])
299    
300                          case 'a':                          case 'a':
301                                  server_bpp = strtol(optarg, NULL, 10);                                  server_bpp = strtol(optarg, NULL, 10);
302                                  if (server_bpp != 8 && server_bpp != 16 && server_bpp != 15 && server_bpp != 24)                                  if (server_bpp != 8 && server_bpp != 16 && server_bpp != 15
303                                        && server_bpp != 24)
304                                  {                                  {
305                                          error("invalid server bpp\n");                                          error("invalid server bpp\n");
306                                          return 1;                                          return 1;
307                                  }                                  }
308                                  break;                                  break;
309    
310                            case '5':
311                                    use_rdp5 = True;
312                                    break;
313                          case 'h':                          case 'h':
314                          case '?':                          case '?':
315                          default:                          default:
# Line 308  main(int argc, char *argv[]) Line 368  main(int argc, char *argv[])
368                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
369          }          }
370    
371    #ifdef RDP2VNC
372            rdp2vnc_connect(server, flags, domain, password, shell, directory);
373            return 0;
374    #else
375    
376          if (!ui_init())          if (!ui_init())
377                  return 1;                  return 1;
378    
379            /* rdpsnd_init(); */
380            /* rdpdr_init(); */
381    
382          if (!rdp_connect(server, flags, domain, password, shell, directory))          if (!rdp_connect(server, flags, domain, password, shell, directory))
383                  return 1;                  return 1;
384    
385            /* By setting encryption to False here, we have an encrypted login
386               packet but unencrypted transfer of other packets */
387            if (!packet_encryption)
388                    encryption = False;
389    
390    
391          DEBUG(("Connection successful.\n"));          DEBUG(("Connection successful.\n"));
392          memset(password, 0, sizeof(password));          memset(password, 0, sizeof(password));
393    
394          if (ui_create_window())          if (ui_create_window())
395          {          {
396                  rdp_main_loop();                  rdp_retval = rdp_main_loop();
397                  ui_destroy_window();                  ui_destroy_window();
398          }          }
399    
400          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
401          rdp_disconnect();          rdp_disconnect();
402          ui_deinit();          ui_deinit();
403          return 0;  
404            if (True == rdp_retval)
405                    return 0;
406            else
407                    return 2;
408    
409    #endif
410    
411  }  }
412    
413  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 486  unimpl(char *format, ...) Line 567  unimpl(char *format, ...)
567    
568  /* produce a hex dump */  /* produce a hex dump */
569  void  void
570  hexdump(unsigned char *p, unsigned int len)  hexdump(unsigned char *p, int len)
571  {  {
572          unsigned char *line = p;          unsigned char *line = p;
573          unsigned int thisline, offset = 0;          int i, thisline, offset = 0;
         int i;  
574    
575          while (offset < len)          while (offset < len)
576          {          {
# Line 514  hexdump(unsigned char *p, unsigned int l Line 594  hexdump(unsigned char *p, unsigned int l
594          }          }
595  }  }
596    
597  #ifdef SAVE_LICENCE  
598  int  int
599  load_licence(unsigned char **data)  load_licence(unsigned char **data)
600  {  {
601          char *path;          char *home, *path;
         char *home;  
602          struct stat st;          struct stat st;
603          int fd;          int fd, length;
604    
605          home = getenv("HOME");          home = getenv("HOME");
606          if (home == NULL)          if (home == NULL)
607                  return -1;                  return -1;
608    
609          path = xmalloc(strlen(home) + strlen(hostname) + 20);          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
610          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
611    
612          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
# Line 537  load_licence(unsigned char **data) Line 616  load_licence(unsigned char **data)
616          if (fstat(fd, &st))          if (fstat(fd, &st))
617                  return -1;                  return -1;
618    
619          *data = xmalloc(st.st_size);          *data = (uint8 *) xmalloc(st.st_size);
620          return read(fd, *data, st.st_size);          length = read(fd, *data, st.st_size);
621            close(fd);
622            xfree(path);
623            return length;
624  }  }
625    
626  void  void
627  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
628  {  {
629          char *fpath;            /* file path for licence */          char *home, *path, *tmppath;
630          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;  
         }  
631    
632          home = getenv("HOME");          home = getenv("HOME");
633          if (home == NULL)          if (home == NULL)
634                  return;                  return;
635    
636          /* 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);  
637    
638          sprintf(fpath, "%s/.rdesktop", fpath);          sprintf(path, "%s/.rdesktop", home);
639          if (mkdir(fpath, 0700) == -1 && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
640          {          {
641                  perror("mkdir");                  perror(path);
642                  exit(1);                  return;
643          }          }
644    
645          /* set the real licence filename, and put a write lock on it. */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
646          fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);  
647          sprintf(fname, "%s/licence.%s", fpath, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
648          fnfd = open(fname, O_RDONLY);          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
649          if (fnfd != -1)          strcpy(tmppath, path);
650          {          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;  
         }  
651    
652          /* close the file and rename it to fname */          fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
653          if (close(fnwrkfd) == -1)          if (fd == -1)
654          {          {
655                  perror("close");                  perror(tmppath);
656                  unlink(fnamewrk);                  return;
                 exit(1);  
657          }          }
658          if (rename(fnamewrk, fname) == -1)  
659            if (write(fd, data, length) != length)
660          {          {
661                  perror("rename");                  perror(tmppath);
662                  unlink(fnamewrk);                  unlink(tmppath);
                 exit(1);  
663          }          }
664          /* close the file lock on fname */          else if (rename(tmppath, path) == -1)
         if (fnfd != -1)  
665          {          {
666                  fnfl.l_type = F_UNLCK;                  perror(path);
667                  fnfl.l_whence = SEEK_SET;                  unlink(tmppath);
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
                 close(fnfd);  
668          }          }
669    
670            close(fd);
671            xfree(tmppath);
672            xfree(path);
673  }  }
 #endif  

Legend:
Removed from v.315  
changed lines
  Added in v.435

  ViewVC Help
Powered by ViewVC 1.1.26