/[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 240 by matthewc, Thu Oct 24 08:21:33 2002 UTC revision 416 by forsberg, Fri Jun 6 11:11: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-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    BOOL use_rdp5 = False;
63    extern BOOL owncolmap;
64    
65    #ifdef RDP2VNC
66    extern int rfb_port;
67    extern int defer_time;
68    void
69    rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
70                    char *shell, char *directory);
71    #endif
72  /* Display usage information */  /* Display usage information */
73  static void  static void
74  usage(char *program)  usage(char *program)
75  {  {
76          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
77          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
78          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
79    
80          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
81    #ifdef RDP2VNC
82            fprintf(stderr, "   -V: vnc port\n");
83            fprintf(stderr, "   -E: defer time (ms)\n");
84    #endif
85          fprintf(stderr, "   -u: user name\n");          fprintf(stderr, "   -u: user name\n");
86          fprintf(stderr, "   -d: domain\n");          fprintf(stderr, "   -d: domain\n");
87          fprintf(stderr, "   -s: shell\n");          fprintf(stderr, "   -s: shell\n");
88            fprintf(stderr, "   -S: caption button size (single application mode)\n");
89          fprintf(stderr, "   -c: working directory\n");          fprintf(stderr, "   -c: working directory\n");
90          fprintf(stderr, "   -p: password (- to prompt)\n");          fprintf(stderr, "   -p: password (- to prompt)\n");
91          fprintf(stderr, "   -n: client hostname\n");          fprintf(stderr, "   -n: client hostname\n");
# Line 77  usage(char *program) Line 95  usage(char *program)
95          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
96          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
97          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
98            fprintf(stderr, "   -C: use private colour map\n");
99          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
100          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
101            fprintf(stderr, "   -D: hide window manager decorations\n");
102            fprintf(stderr, "   -a: server bpp\n");
103            fprintf(stderr, "   -5: Use RDP5 (EXPERIMENTAL!)\n");
104  }  }
105    
106  static BOOL  static BOOL
# Line 132  main(int argc, char *argv[]) Line 154  main(int argc, char *argv[])
154          uint32 flags;          uint32 flags;
155          char *p;          char *p;
156          int c;          int c;
157            int username_option = 0;
158    
159          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
160          prompt_password = False;          prompt_password = False;
161          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
162          strcpy(keymapname, "en-us");          strcpy(keymapname, "en-us");
163    
164          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemKT:h?")) != -1)  #ifdef RDP2VNC
165    #define VNCOPT "V:E:"
166    #else
167    #define VNCOPT
168    #endif
169    
170            while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbemCKT:Dh?54")) != -1)
171          {          {
172                  switch (c)                  switch (c)
173                  {                  {
174    #ifdef RDP2VNC
175                            case 'V':
176                                    rfb_port = strtol(optarg, NULL, 10);
177                                    if (rfb_port < 100)
178                                            rfb_port += 5900;
179                                    break;
180    
181                            case 'E':
182                                    defer_time = strtol(optarg, NULL, 10);
183                                    if (defer_time < 0)
184                                            defer_time = 0;
185                                    break;
186    #endif
187    
188                          case 'u':                          case 'u':
189                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(username, optarg, sizeof(username));
190                                    username_option = 1;
191                                  break;                                  break;
192    
193                          case 'd':                          case 'd':
# Line 154  main(int argc, char *argv[]) Line 198  main(int argc, char *argv[])
198                                  STRNCPY(shell, optarg, sizeof(shell));                                  STRNCPY(shell, optarg, sizeof(shell));
199                                  break;                                  break;
200    
201                            case 'S':
202                                    if (!strcmp(optarg, "standard"))
203                                    {
204                                            win_button_size = 18;
205                                            break;
206                                    }
207    
208                                    win_button_size = strtol(optarg, &p, 10);
209    
210                                    if (*p)
211                                    {
212                                            error("invalid button size\n");
213                                            return 1;
214                                    }
215    
216                                    break;
217    
218                          case 'c':                          case 'c':
219                                  STRNCPY(directory, optarg, sizeof(directory));                                  STRNCPY(directory, optarg, sizeof(directory));
220                                  break;                                  break;
# Line 183  main(int argc, char *argv[]) Line 244  main(int argc, char *argv[])
244                                  break;                                  break;
245    
246                          case 'g':                          case 'g':
247                                    if (!strcmp(optarg, "workarea"))
248                                    {
249                                            width = height = 0;
250                                            break;
251                                    }
252    
253                                  width = strtol(optarg, &p, 10);                                  width = strtol(optarg, &p, 10);
254                                  if (*p == 'x')                                  if (*p == 'x')
255                                          height = strtol(p + 1, NULL, 10);                                          height = strtol(p + 1, NULL, 10);
# Line 210  main(int argc, char *argv[]) Line 277  main(int argc, char *argv[])
277                                  sendmotion = False;                                  sendmotion = False;
278                                  break;                                  break;
279    
280                            case 'C':
281                                    owncolmap = True;
282                                    break;
283    
284                          case 'K':                          case 'K':
285                                  grab_keyboard = False;                                  grab_keyboard = False;
286                                  break;                                  break;
# Line 218  main(int argc, char *argv[]) Line 289  main(int argc, char *argv[])
289                                  STRNCPY(title, optarg, sizeof(title));                                  STRNCPY(title, optarg, sizeof(title));
290                                  break;                                  break;
291    
292                            case 'D':
293                                    hide_decorations = True;
294                                    break;
295    
296                            case 'a':
297                                    server_bpp = strtol(optarg, NULL, 10);
298                                    if (server_bpp != 8 && server_bpp != 16 && server_bpp != 15
299                                        && server_bpp != 24)
300                                    {
301                                            error("invalid server bpp\n");
302                                            return 1;
303                                    }
304                                    break;
305    
306                            case '5':
307                                    use_rdp5 = True;
308                                    break;
309                          case 'h':                          case 'h':
310                          case '?':                          case '?':
311                          default:                          default:
# Line 240  main(int argc, char *argv[]) Line 328  main(int argc, char *argv[])
328                  *p = 0;                  *p = 0;
329          }          }
330    
331          if (username[0] == 0)          if (!username_option)
332          {          {
333                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
334                  if ((pw == NULL) || (pw->pw_name == NULL))                  if ((pw == NULL) || (pw->pw_name == NULL))
# Line 276  main(int argc, char *argv[]) Line 364  main(int argc, char *argv[])
364                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
365          }          }
366    
367    #ifdef RDP2VNC
368            rdp2vnc_connect(server, flags, domain, password, shell, directory);
369    #else
370    
371          if (!ui_init())          if (!ui_init())
372                  return 1;                  return 1;
373    
374            ipc_init();             // Must be run after ui_init, we need X to be setup.
375    
376            if (use_rdp5)
377                    cliprdr_init(); // FIXME: Should perhaps be integrated into the channel management code?
378    
379          if (!rdp_connect(server, flags, domain, password, shell, directory))          if (!rdp_connect(server, flags, domain, password, shell, directory))
380                  return 1;                  return 1;
381    
# Line 294  main(int argc, char *argv[]) Line 391  main(int argc, char *argv[])
391          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
392          rdp_disconnect();          rdp_disconnect();
393          ui_deinit();          ui_deinit();
394    
395    #endif
396    
397          return 0;          return 0;
398  }  }
399    
# Line 312  generate_random_egd(uint8 * buf) Line 412  generate_random_egd(uint8 * buf)
412    
413          addr.sun_family = AF_UNIX;          addr.sun_family = AF_UNIX;
414          memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));          memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
415          if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)          if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
416                  goto err;                  goto err;
417    
418          /* PRNGD and EGD use a simple communications protocol */          /* PRNGD and EGD use a simple communications protocol */
419          buf[0] = 1;  /* Non-blocking (similar to /dev/urandom) */          buf[0] = 1;             /* Non-blocking (similar to /dev/urandom) */
420          buf[1] = 32; /* Number of requested random bytes */          buf[1] = 32;            /* Number of requested random bytes */
421          if (write(fd, buf, 2) != 2)          if (write(fd, buf, 2) != 2)
422                  goto err;                  goto err;
423    
424          if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */          if ((read(fd, buf, 1) != 1) || (buf[0] == 0))   /* Available? */
425                  goto err;                  goto err;
426    
427          if (read(fd, buf, 32) != 32)          if (read(fd, buf, 32) != 32)
# Line 329  generate_random_egd(uint8 * buf) Line 429  generate_random_egd(uint8 * buf)
429    
430          ret = True;          ret = True;
431    
432  err:        err:
433          close(fd);          close(fd);
434          return ret;          return ret;
435  }  }
# Line 362  generate_random(uint8 * random) Line 462  generate_random(uint8 * random)
462  #endif  #endif
463    
464          /* Otherwise use whatever entropy we can gather - ideas welcome. */          /* Otherwise use whatever entropy we can gather - ideas welcome. */
465          r = (uint32 *)random;          r = (uint32 *) random;
466          r[0] = (getpid()) | (getppid() << 16);          r[0] = (getpid()) | (getppid() << 16);
467          r[1] = (getuid()) | (getgid() << 16);          r[1] = (getuid()) | (getgid() << 16);
468          r[2] = times(&tmsbuf);  /* system uptime (clocks) */          r[2] = times(&tmsbuf);  /* system uptime (clocks) */
# Line 374  generate_random(uint8 * random) Line 474  generate_random(uint8 * random)
474    
475          /* Hash both halves with MD5 to obscure possible patterns */          /* Hash both halves with MD5 to obscure possible patterns */
476          MD5_Init(&md5);          MD5_Init(&md5);
477          MD5_Update(&md5, random, 16);          MD5_Update(&md5, random, 16);
478          MD5_Final(random, &md5);          MD5_Final(random, &md5);
479          MD5_Update(&md5, random+16, 16);          MD5_Update(&md5, random + 16, 16);
480          MD5_Final(random+16, &md5);          MD5_Final(random + 16, &md5);
481  }  }
482    
483  /* malloc; exit if out of memory */  /* malloc; exit if out of memory */
# Line 426  error(char *format, ...) Line 526  error(char *format, ...)
526          va_end(ap);          va_end(ap);
527  }  }
528    
529    /* report a warning */
530    void
531    warning(char *format, ...)
532    {
533            va_list ap;
534    
535            fprintf(stderr, "WARNING: ");
536    
537            va_start(ap, format);
538            vfprintf(stderr, format, ap);
539            va_end(ap);
540    }
541    
542  /* report an unimplemented protocol feature */  /* report an unimplemented protocol feature */
543  void  void
544  unimpl(char *format, ...)  unimpl(char *format, ...)
# Line 441  unimpl(char *format, ...) Line 554  unimpl(char *format, ...)
554    
555  /* produce a hex dump */  /* produce a hex dump */
556  void  void
557  hexdump(unsigned char *p, unsigned int len)  hexdump(unsigned char *p, int len)
558  {  {
559          unsigned char *line = p;          unsigned char *line = p;
560          unsigned int thisline, offset = 0;          int i, thisline, offset = 0;
         int i;  
561    
562          while (offset < len)          while (offset < len)
563          {          {
# Line 469  hexdump(unsigned char *p, unsigned int l Line 581  hexdump(unsigned char *p, unsigned int l
581          }          }
582  }  }
583    
584  #ifdef SAVE_LICENCE  
585  int  int
586  load_licence(unsigned char **data)  load_licence(unsigned char **data)
587  {  {
588          char path[PATH_MAX];          char *home, *path;
         char *home;  
589          struct stat st;          struct stat st;
590          int fd;          int fd, length;
591    
592          home = getenv("HOME");          home = getenv("HOME");
593          if (home == NULL)          if (home == NULL)
594                  return -1;                  return -1;
595    
596          STRNCPY(path, home, sizeof(path));          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
597          strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
598    
599          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
600          if (fd == -1)          if (fd == -1)
# Line 492  load_licence(unsigned char **data) Line 603  load_licence(unsigned char **data)
603          if (fstat(fd, &st))          if (fstat(fd, &st))
604                  return -1;                  return -1;
605    
606          *data = xmalloc(st.st_size);          *data = (uint8 *) xmalloc(st.st_size);
607          return read(fd, *data, st.st_size);          length = read(fd, *data, st.st_size);
608            close(fd);
609            xfree(path);
610            return length;
611  }  }
612    
613  void  void
614  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
615  {  {
616          char path[PATH_MAX];          char *home, *path, *tmppath;
         char *home;  
617          int fd;          int fd;
618    
619          home = getenv("HOME");          home = getenv("HOME");
620          if (home == NULL)          if (home == NULL)
621                  return;                  return;
622    
623          STRNCPY(path, home, sizeof(path));          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
         strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1);  
         mkdir(path, 0700);  
624    
625          strncat(path, "/licence", sizeof(path) - strlen(path) - 1);          sprintf(path, "%s/.rdesktop", home);
626            if ((mkdir(path, 0700) == -1) && errno != EEXIST)
627            {
628                    perror(path);
629                    return;
630            }
631    
632          fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
633    
634            sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
635            tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
636            strcpy(tmppath, path);
637            strcat(tmppath, ".new");
638    
639            fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
640          if (fd == -1)          if (fd == -1)
641          {          {
642                  perror("open");                  perror(tmppath);
643                  return;                  return;
644          }          }
645    
646          write(fd, data, length);          if (write(fd, data, length) != length)
647            {
648                    perror(tmppath);
649                    unlink(tmppath);
650            }
651            else if (rename(tmppath, path) == -1)
652            {
653                    perror(path);
654                    unlink(tmppath);
655            }
656    
657          close(fd);          close(fd);
658            xfree(tmppath);
659            xfree(path);
660  }  }
 #endif  

Legend:
Removed from v.240  
changed lines
  Added in v.416

  ViewVC Help
Powered by ViewVC 1.1.26