/[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 329 by astrand, Tue Feb 18 13:07:29 2003 UTC revision 482 by matthewc, Thu Oct 9 04:21:19 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 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  int g_width = 800;              /* If width or height are reset to zero, the geometry will
49                                     be fetched from _NET_WORKAREA */                                     be fetched from _NET_WORKAREA */
50  int height = 600;  int g_height = 600;
51  int tcp_port_rdp = TCP_PORT_RDP;  int tcp_port_rdp = TCP_PORT_RDP;
52  int server_bpp = 8;  int g_server_bpp = 8;
53  int win_button_size = 0;        /* If zero, disable single app mode */  int g_win_button_size = 0;      /* If zero, disable single app mode */
54  BOOL bitmap_compression = True;  BOOL g_bitmap_compression = True;
55  BOOL sendmotion = True;  BOOL g_sendmotion = True;
56  BOOL orders = True;  BOOL g_orders = True;
57  BOOL encryption = True;  BOOL g_encryption = True;
58  BOOL desktop_save = True;  BOOL packet_encryption = True;
59  BOOL fullscreen = False;  BOOL g_desktop_save = True;
60  BOOL grab_keyboard = True;  BOOL g_fullscreen = False;
61  BOOL hide_decorations = False;  BOOL g_grab_keyboard = True;
62  extern BOOL owncolmap;  BOOL g_hide_decorations = False;
63    BOOL g_use_rdp5 = True;
64    BOOL g_console_session = False;
65    extern BOOL g_owncolmap;
66    
67    #ifdef RDP2VNC
68    extern int rfb_port;
69    extern int defer_time;
70    void
71    rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
72                    char *shell, char *directory);
73    #endif
74  /* Display usage information */  /* Display usage information */
75  static void  static void
76  usage(char *program)  usage(char *program)
# Line 70  usage(char *program) Line 80  usage(char *program)
80          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
81    
82          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
83    #ifdef RDP2VNC
84            fprintf(stderr, "   -V: vnc port\n");
85            fprintf(stderr, "   -Q: defer time (ms)\n");
86    #endif
87          fprintf(stderr, "   -u: user name\n");          fprintf(stderr, "   -u: user name\n");
88          fprintf(stderr, "   -d: domain\n");          fprintf(stderr, "   -d: domain\n");
89          fprintf(stderr, "   -s: shell\n");          fprintf(stderr, "   -s: shell\n");
# Line 82  usage(char *program) Line 96  usage(char *program)
96          fprintf(stderr, "   -f: full-screen mode\n");          fprintf(stderr, "   -f: full-screen mode\n");
97          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
98          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
99            fprintf(stderr, "   -E: disable encryption from client to server\n");
100          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
101          fprintf(stderr, "   -C: use private colour map\n");          fprintf(stderr, "   -C: use private colour map\n");
102          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
103          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
104          fprintf(stderr, "   -D: hide window manager decorations\n");          fprintf(stderr, "   -D: hide window manager decorations\n");
105          fprintf(stderr, "   -a: server bpp\n");          fprintf(stderr, "   -a: server bpp\n");
106            fprintf(stderr, "   -0: attach to console\n");
107            fprintf(stderr, "   -4: use RDP version 4\n");
108            fprintf(stderr, "   -5: use RDP version 5 (default)\n");
109  }  }
110    
111  static BOOL  static BOOL
# Line 126  read_password(char *password, int size) Line 144  read_password(char *password, int size)
144          return ret;          return ret;
145  }  }
146    
147    static void
148    parse_server_and_port(char *server)
149    {
150            char *p;
151    #ifdef IPv6
152            int addr_colons;
153    #endif
154    
155    #ifdef IPv6
156            p = server;
157            addr_colons = 0;
158            while (*p)
159                    if (*p++ == ':')
160                            addr_colons++;
161            if (addr_colons >= 2)
162            {
163                    /* numeric IPv6 style address format - [1:2:3::4]:port */
164                    p = strchr(server, ']');
165                    if (*server == '[' && p != NULL)
166                    {
167                            if (*(p + 1) == ':' && *(p + 2) != '\0')
168                                    tcp_port_rdp = strtol(p + 2, NULL, 10);
169                            /* remove the port number and brackets from the address */
170                            *p = '\0';
171                            strncpy(server, server + 1, strlen(server));
172                    }
173            }
174            else
175            {
176                    /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
177                    p = strchr(server, ':');
178                    if (p != NULL)
179                    {
180                            tcp_port_rdp = strtol(p + 1, NULL, 10);
181                            *p = 0;
182                    }
183            }
184    #else /* no IPv6 support */
185            p = strchr(server, ':');
186            if (p != NULL)
187            {
188                    tcp_port_rdp = strtol(p + 1, NULL, 10);
189                    *p = 0;
190            }
191    #endif /* IPv6 */
192    
193    }
194    
195  /* Client program */  /* Client program */
196  int  int
197  main(int argc, char *argv[])  main(int argc, char *argv[])
# Line 133  main(int argc, char *argv[]) Line 199  main(int argc, char *argv[])
199          char server[64];          char server[64];
200          char fullhostname[64];          char fullhostname[64];
201          char domain[16];          char domain[16];
202          char password[16];          char password[64];
203          char shell[128];          char shell[128];
204          char directory[32];          char directory[32];
205          BOOL prompt_password;          BOOL prompt_password, rdp_retval = False;
206          struct passwd *pw;          struct passwd *pw;
207          uint32 flags;          uint32 flags;
208          char *p;          char *p;
# Line 148  main(int argc, char *argv[]) Line 214  main(int argc, char *argv[])
214          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
215          strcpy(keymapname, "en-us");          strcpy(keymapname, "en-us");
216    
217          while ((c = getopt(argc, argv, "u:d:s:S:c:p:n:k:g:a:fbemCKT:Dh?")) != -1)  #ifdef RDP2VNC
218    #define VNCOPT "V:Q:"
219    #else
220    #define VNCOPT
221    #endif
222    
223            while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbeEmCKT:D045h?")) != -1)
224          {          {
225                  switch (c)                  switch (c)
226                  {                  {
227    #ifdef RDP2VNC
228                            case 'V':
229                                    rfb_port = strtol(optarg, NULL, 10);
230                                    if (rfb_port < 100)
231                                            rfb_port += 5900;
232                                    break;
233    
234                            case 'Q':
235                                    defer_time = strtol(optarg, NULL, 10);
236                                    if (defer_time < 0)
237                                            defer_time = 0;
238                                    break;
239    #endif
240    
241                          case 'u':                          case 'u':
242                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(g_username, optarg, sizeof(g_username));
243                                  username_option = 1;                                  username_option = 1;
244                                  break;                                  break;
245    
# Line 168  main(int argc, char *argv[]) Line 254  main(int argc, char *argv[])
254                          case 'S':                          case 'S':
255                                  if (!strcmp(optarg, "standard"))                                  if (!strcmp(optarg, "standard"))
256                                  {                                  {
257                                          win_button_size = 18;                                          g_win_button_size = 18;
258                                          break;                                          break;
259                                  }                                  }
260    
261                                  win_button_size = strtol(optarg, &p, 10);                                  g_win_button_size = strtol(optarg, &p, 10);
262    
263                                  if (*p)                                  if (*p)
264                                  {                                  {
# Line 213  main(int argc, char *argv[]) Line 299  main(int argc, char *argv[])
299                          case 'g':                          case 'g':
300                                  if (!strcmp(optarg, "workarea"))                                  if (!strcmp(optarg, "workarea"))
301                                  {                                  {
302                                          width = height = 0;                                          g_width = g_height = 0;
303                                          break;                                          break;
304                                  }                                  }
305    
306                                  width = strtol(optarg, &p, 10);                                  g_width = strtol(optarg, &p, 10);
307                                  if (*p == 'x')                                  if (*p == 'x')
308                                          height = strtol(p + 1, NULL, 10);                                          g_height = strtol(p + 1, NULL, 10);
309    
310                                  if ((width == 0) || (height == 0))                                  if ((g_width == 0) || (g_height == 0))
311                                  {                                  {
312                                          error("invalid geometry\n");                                          error("invalid geometry\n");
313                                          return 1;                                          return 1;
# Line 229  main(int argc, char *argv[]) Line 315  main(int argc, char *argv[])
315                                  break;                                  break;
316    
317                          case 'f':                          case 'f':
318                                  fullscreen = True;                                  g_fullscreen = True;
319                                  break;                                  break;
320    
321                          case 'b':                          case 'b':
322                                  orders = False;                                  g_orders = False;
323                                  break;                                  break;
324    
325                          case 'e':                          case 'e':
326                                  encryption = False;                                  g_encryption = False;
327                                    break;
328                            case 'E':
329                                    packet_encryption = False;
330                                  break;                                  break;
   
331                          case 'm':                          case 'm':
332                                  sendmotion = False;                                  g_sendmotion = False;
333                                  break;                                  break;
334    
335                          case 'C':                          case 'C':
336                                  owncolmap = True;                                  g_owncolmap = True;
337                                  break;                                  break;
338    
339                          case 'K':                          case 'K':
340                                  grab_keyboard = False;                                  g_grab_keyboard = False;
341                                  break;                                  break;
342    
343                          case 'T':                          case 'T':
344                                  STRNCPY(title, optarg, sizeof(title));                                  STRNCPY(g_title, optarg, sizeof(g_title));
345                                  break;                                  break;
346    
347                          case 'D':                          case 'D':
348                                  hide_decorations = True;                                  g_hide_decorations = True;
349                                  break;                                  break;
350    
351                          case 'a':                          case 'a':
352                                  server_bpp = strtol(optarg, NULL, 10);                                  g_server_bpp = strtol(optarg, NULL, 10);
353                                  if (server_bpp != 8 && server_bpp != 16 && server_bpp != 15                                  if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
354                                      && server_bpp != 24)                                      && g_server_bpp != 24)
355                                  {                                  {
356                                          error("invalid server bpp\n");                                          error("invalid server bpp\n");
357                                          return 1;                                          return 1;
358                                  }                                  }
359                                  break;                                  break;
360    
361                            case '0':
362                                    g_console_session = True;
363                                    break;
364    
365                            case '4':
366                                    g_use_rdp5 = False;
367                                    break;
368    
369                            case '5':
370                                    g_use_rdp5 = True;
371                                    break;
372    
373                          case 'h':                          case 'h':
374                          case '?':                          case '?':
375                          default:                          default:
# Line 285  main(int argc, char *argv[]) Line 385  main(int argc, char *argv[])
385          }          }
386    
387          STRNCPY(server, argv[optind], sizeof(server));          STRNCPY(server, argv[optind], sizeof(server));
388          p = strchr(server, ':');          parse_server_and_port(server);
         if (p != NULL)  
         {  
                 tcp_port_rdp = strtol(p + 1, NULL, 10);  
                 *p = 0;  
         }  
389    
390          if (!username_option)          if (!username_option)
391          {          {
# Line 301  main(int argc, char *argv[]) Line 396  main(int argc, char *argv[])
396                          return 1;                          return 1;
397                  }                  }
398    
399                  STRNCPY(username, pw->pw_name, sizeof(username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
400          }          }
401    
402          if (hostname[0] == 0)          if (hostname[0] == 0)
# Line 322  main(int argc, char *argv[]) Line 417  main(int argc, char *argv[])
417          if (prompt_password && read_password(password, sizeof(password)))          if (prompt_password && read_password(password, sizeof(password)))
418                  flags |= RDP_LOGON_AUTO;                  flags |= RDP_LOGON_AUTO;
419    
420          if (title[0] == 0)          if (g_title[0] == 0)
421          {          {
422                  strcpy(title, "rdesktop - ");                  strcpy(g_title, "rdesktop - ");
423                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));                  strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
424          }          }
425    
426    #ifdef RDP2VNC
427            rdp2vnc_connect(server, flags, domain, password, shell, directory);
428            return 0;
429    #else
430    
431          if (!ui_init())          if (!ui_init())
432                  return 1;                  return 1;
433    
434    #ifdef WITH_RDPSND
435            rdpsnd_init();
436    #endif
437            /* rdpdr_init(); */
438    
439          if (!rdp_connect(server, flags, domain, password, shell, directory))          if (!rdp_connect(server, flags, domain, password, shell, directory))
440                  return 1;                  return 1;
441    
442            /* By setting encryption to False here, we have an encrypted login
443               packet but unencrypted transfer of other packets */
444            if (!packet_encryption)
445                    g_encryption = False;
446    
447    
448          DEBUG(("Connection successful.\n"));          DEBUG(("Connection successful.\n"));
449          memset(password, 0, sizeof(password));          memset(password, 0, sizeof(password));
450    
451          if (ui_create_window())          if (ui_create_window())
452          {          {
453                  rdp_main_loop();                  rdp_retval = rdp_main_loop();
454                  ui_destroy_window();                  ui_destroy_window();
455          }          }
456    
457          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
458          rdp_disconnect();          rdp_disconnect();
459          ui_deinit();          ui_deinit();
460          return 0;  
461            if (True == rdp_retval)
462                    return 0;
463            else
464                    return 2;
465    
466    #endif
467    
468  }  }
469    
470  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 506  unimpl(char *format, ...) Line 624  unimpl(char *format, ...)
624    
625  /* produce a hex dump */  /* produce a hex dump */
626  void  void
627  hexdump(unsigned char *p, unsigned int len)  hexdump(unsigned char *p, int len)
628  {  {
629          unsigned char *line = p;          unsigned char *line = p;
630          unsigned int thisline, offset = 0;          int i, thisline, offset = 0;
         int i;  
631    
632          while (offset < len)          while (offset < len)
633          {          {
# Line 538  hexdump(unsigned char *p, unsigned int l Line 655  hexdump(unsigned char *p, unsigned int l
655  int  int
656  load_licence(unsigned char **data)  load_licence(unsigned char **data)
657  {  {
658          char *path;          char *home, *path;
         char *home;  
659          struct stat st;          struct stat st;
660          int fd;          int fd, length;
661    
662          home = getenv("HOME");          home = getenv("HOME");
663          if (home == NULL)          if (home == NULL)
664                  return -1;                  return -1;
665    
666          path = xmalloc(strlen(home) + strlen(hostname) + 20);          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
667          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
668    
669          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
# Line 557  load_licence(unsigned char **data) Line 673  load_licence(unsigned char **data)
673          if (fstat(fd, &st))          if (fstat(fd, &st))
674                  return -1;                  return -1;
675    
676          *data = xmalloc(st.st_size);          *data = (uint8 *) xmalloc(st.st_size);
677          return read(fd, *data, st.st_size);          length = read(fd, *data, st.st_size);
678            close(fd);
679            xfree(path);
680            return length;
681  }  }
682    
683  void  void
684  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
685  {  {
686          char *fpath;            /* file path for licence */          char *home, *path, *tmppath;
687          char *fname, *fnamewrk; /* file name for licence .inkl path. */          int fd;
         char *home;  
         uint32 y;  
         struct flock fnfl;  
         int fnfd, fnwrkfd, i, wlen;  
         struct stream s, *s_ptr;  
         uint32 len;  
   
         /* Construct a stream, so that we can use macros to extract the  
          * licence.  
          */  
         s_ptr = &s;  
         s_ptr->p = data;  
         /* Skip first two bytes */  
         in_uint16(s_ptr, len);  
   
         /* Skip three strings */  
         for (i = 0; i < 3; i++)  
         {  
                 in_uint32(s_ptr, len);  
                 s_ptr->p += len;  
                 /* Make sure that we won't be past the end of data after  
                  * reading the next length value  
                  */  
                 if ((s_ptr->p) + 4 > data + length)  
                 {  
                         printf("Error in parsing licence key.\n");  
                         printf("Strings %d end value %x > supplied length (%x)\n", i,  
                                (unsigned int) s_ptr->p, (unsigned int) data + length);  
                         return;  
                 }  
         }  
         in_uint32(s_ptr, len);  
         if (s_ptr->p + len > data + length)  
         {  
                 printf("Error in parsing licence key.\n");  
                 printf("End of licence %x > supplied length (%x)\n",  
                        (unsigned int) s_ptr->p + len, (unsigned int) data + length);  
                 return;  
         }  
688    
689          home = getenv("HOME");          home = getenv("HOME");
690          if (home == NULL)          if (home == NULL)
691                  return;                  return;
692    
693          /* 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);  
694    
695          sprintf(fpath, "%s/.rdesktop", fpath);          sprintf(path, "%s/.rdesktop", home);
696          if (mkdir(fpath, 0700) == -1 && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
697          {          {
698                  perror("mkdir");                  perror(path);
699                  exit(1);                  return;
700          }          }
701    
702          /* set the real licence filename, and put a write lock on it. */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
         fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);  
         sprintf(fname, "%s/licence.%s", fpath, hostname);  
         fnfd = open(fname, O_RDONLY);  
         if (fnfd != -1)  
         {  
                 fnfl.l_type = F_WRLCK;  
                 fnfl.l_whence = SEEK_SET;  
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
         }  
   
         /* create a temporary licence file */  
         fnamewrk = xmalloc(strlen(fname) + 12);  
         for (y = 0;; y++)  
         {  
                 sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int) y);  
                 fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600);  
                 if (fnwrkfd == -1)  
                 {  
                         if (errno == EINTR || errno == EEXIST)  
                                 continue;  
                         perror("create");  
                         exit(1);  
                 }  
                 break;  
         }  
         /* write to the licence file */  
         for (y = 0; y < len;)  
         {  
                 do  
                 {  
                         wlen = write(fnwrkfd, s_ptr->p + y, len - y);  
                 }  
                 while (wlen == -1 && errno == EINTR);  
                 if (wlen < 1)  
                 {  
                         perror("write");  
                         unlink(fnamewrk);  
                         exit(1);  
                 }  
                 y += wlen;  
         }  
703    
704          /* close the file and rename it to fname */          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
705          if (close(fnwrkfd) == -1)          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
706            strcpy(tmppath, path);
707            strcat(tmppath, ".new");
708    
709            fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
710            if (fd == -1)
711          {          {
712                  perror("close");                  perror(tmppath);
713                  unlink(fnamewrk);                  return;
                 exit(1);  
714          }          }
715          if (rename(fnamewrk, fname) == -1)  
716            if (write(fd, data, length) != length)
717          {          {
718                  perror("rename");                  perror(tmppath);
719                  unlink(fnamewrk);                  unlink(tmppath);
                 exit(1);  
720          }          }
721          /* close the file lock on fname */          else if (rename(tmppath, path) == -1)
         if (fnfd != -1)  
722          {          {
723                  fnfl.l_type = F_UNLCK;                  perror(path);
724                  fnfl.l_whence = SEEK_SET;                  unlink(tmppath);
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
                 close(fnfd);  
725          }          }
726    
727            close(fd);
728            xfree(tmppath);
729            xfree(path);
730  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26