/[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 29 by matty, Fri Sep 14 03:38:39 2001 UTC revision 214 by matthewc, Sun Oct 6 13:57:39 2002 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-2000     Copyright (C) Matthew Chapman 1999-2002
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
8     the Free Software Foundation; either version 2 of the License, or     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.     (at your option) any later version.
10      
11     This program is distributed in the hope that it will be useful,     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.     GNU General Public License for more details.
15      
16     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */  */
20    
21  #include <stdlib.h>             /* malloc realloc free */  #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 <pwd.h>                /* getpwuid */  #include <pwd.h>                /* getpwuid */
25    #include <limits.h>             /* PATH_MAX */
26    #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 */
29  #include <sys/times.h>          /* times */  #include <sys/times.h>          /* times */
30  #include "rdesktop.h"  #include "rdesktop.h"
31    
32    char title[32] = "";
33  char username[16];  char username[16];
34  char hostname[16];  char hostname[16];
35    char keymapname[16];
36    int keylayout = 0x409;          /* Defaults to US keyboard layout */
37  int width = 800;  int width = 800;
38  int height = 600;  int height = 600;
39  int keylayout = 0x409;  int tcp_port_rdp = TCP_PORT_RDP;
40  BOOL bitmap_compression = True;  BOOL bitmap_compression = True;
41  BOOL sendmotion = True;  BOOL sendmotion = True;
42  BOOL orders = True;  BOOL orders = True;
43  BOOL licence = True;  BOOL encryption = True;
 BOOL use_encryption = True;  
44  BOOL desktop_save = True;  BOOL desktop_save = True;
45  BOOL fullscreen = False;  BOOL fullscreen = False;
46    BOOL grab_keyboard = True;
47    
48  /* Display usage information */  /* Display usage information */
49  static void  static void
50  usage(char *program)  usage(char *program)
51  {  {
52          STATUS("Usage: %s [options] server\n", program);          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
53          STATUS("   -u: user name\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");
54          STATUS("   -d: domain\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
55          STATUS("   -s: shell\n");  
56          STATUS("   -c: working directory\n");          fprintf(stderr, "Usage: %s [options] server\n", program);
57          STATUS("   -p: password (autologon)\n");          fprintf(stderr, "   -u: user name\n");
58          STATUS("   -n: client hostname\n");          fprintf(stderr, "   -d: domain\n");
59          STATUS("   -w: desktop width\n");          fprintf(stderr, "   -s: shell\n");
60          STATUS("   -h: desktop height\n");          fprintf(stderr, "   -c: working directory\n");
61          STATUS("   -k: keyboard layout (hex)\n");          fprintf(stderr, "   -p: password (- to prompt)\n");
62          STATUS("   -b: force bitmap updates\n");          fprintf(stderr, "   -n: client hostname\n");
63          STATUS("   -m: do not send motion events\n");          fprintf(stderr, "   -k: keyboard layout on terminal server (us,sv,gr etc.)\n");
64          STATUS("   -l: do not request licence\n\n");          fprintf(stderr, "   -g: desktop geometry (WxH)\n");
65            fprintf(stderr, "   -f: full-screen mode\n");
66            fprintf(stderr, "   -b: force bitmap updates\n");
67            fprintf(stderr, "   -e: disable encryption (French TS)\n");
68            fprintf(stderr, "   -m: do not send motion events\n");
69            fprintf(stderr, "   -t: rdp tcp port\n");
70            fprintf(stderr, "   -K: keep window manager key bindings\n");
71            fprintf(stderr, "   -w: window title\n");
72    }
73    
74    static BOOL
75    read_password(char *password, int size)
76    {
77            struct termios tios;
78            BOOL ret = False;
79            int istty = 0;
80            char *p;
81    
82            if (tcgetattr(STDIN_FILENO, &tios) == 0)
83            {
84                    fprintf(stderr, "Password: ");
85                    tios.c_lflag &= ~ECHO;
86                    tcsetattr(STDIN_FILENO, TCSANOW, &tios);
87                    istty = 1;
88            }
89    
90            if (fgets(password, size, stdin) != NULL)
91            {
92                    ret = True;
93    
94                    /* strip final newline */
95                    p = strchr(password, '\n');
96                    if (p != NULL)
97                            *p = 0;
98            }
99    
100            if (istty)
101            {
102                    tios.c_lflag |= ECHO;
103                    tcsetattr(STDIN_FILENO, TCSANOW, &tios);
104                    fprintf(stderr, "\n");
105            }
106    
107            return ret;
108  }  }
109    
110  /* Client program */  /* Client program */
111  int  int
112  main(int argc, char *argv[])  main(int argc, char *argv[])
113  {  {
114          struct passwd *pw;          char fullhostname[64];
         char *server;  
         uint32 flags;  
115          char domain[16];          char domain[16];
116          char password[16];          char password[16];
117          char shell[32];          char shell[32];
118          char directory[32];          char directory[32];
119          char title[32];          BOOL prompt_password;
120            struct passwd *pw;
121            char *server, *p;
122            uint32 flags;
123          int c;          int c;
124    
         STATUS("rdesktop: A Remote Desktop Protocol client.\n");  
         STATUS("Version " VERSION  
                ". Copyright (C) 1999-2000 Matt Chapman.\n");  
         STATUS("See http://www.rdesktop.org/ for more information.\n\n");  
   
125          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
126            prompt_password = False;
127          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
128            strcpy(keymapname, "us");
129    
130          while ((c = getopt(argc, argv, "u:d:s:c:p:n:g:k:mbleKFVh?")) != -1)          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:t:fbemKw:h?")) != -1)
131          {          {
132                  switch (c)                  switch (c)
133                  {                  {
134                          case 'u':                          case 'u':
135                                  strncpy(username, optarg, sizeof(username));                                  STRNCPY(username, optarg, sizeof(username));
136                                  break;                                  break;
137    
138                          case 'd':                          case 'd':
139                                  strncpy(domain, optarg, sizeof(domain));                                  STRNCPY(domain, optarg, sizeof(domain));
                                 break;  
   
                         case 'p':  
                                 flags |= RDP_LOGON_AUTO;  
                                 strncpy(password, optarg, sizeof(password));  
140                                  break;                                  break;
141    
142                          case 's':                          case 's':
143                                  strncpy(shell, optarg, sizeof(shell));                                  STRNCPY(shell, optarg, sizeof(shell));
144                                  break;                                  break;
145    
146                          case 'c':                          case 'c':
147                                  strncpy(directory, optarg, sizeof(directory));                                  STRNCPY(directory, optarg, sizeof(directory));
148                                  break;                                  break;
149    
150                          case 'n':                          case 'p':
151                                  strncpy(hostname, optarg, sizeof(hostname));                                  if ((optarg[0] == '-') && (optarg[1] == 0))
                                 break;  
                         case 'g':  
152                                  {                                  {
153                                          char *tgem = 0;                                          prompt_password = True;
154                                          width = strtol(optarg, NULL, 10);                                          break;
                                         tgem = strchr(optarg, 'x');  
                                         if ((tgem == 0) || (strlen(tgem) < 2))  
                                         {  
                                                 ERROR  
                                                         ("-g: invalid parameter. Syntax example: -g 1024x768\n");  
                                                 exit(1);  
                                         }  
                                         height = strtol(tgem + 1, NULL, 10);  
155                                  }                                  }
156    
157                                    STRNCPY(password, optarg, sizeof(password));
158                                    flags |= RDP_LOGON_AUTO;
159    
160                                    /* try to overwrite argument so it won't appear in ps */
161                                    p = optarg;
162                                    while (*p)
163                                            *(p++) = 'X';
164                                    break;
165    
166                            case 'n':
167                                    STRNCPY(hostname, optarg, sizeof(hostname));
168                                  break;                                  break;
169    
170                          case 'k':                          case 'k':
171                                  keylayout = strtol(optarg, NULL, 16);                                  STRNCPY(keymapname, optarg, sizeof(keymapname));
                                 /* keylayout = find_keyb_code(optarg); */  
                                 if (keylayout == 0)  
                                         return 0;  
172                                  break;                                  break;
173    
174                          case 'm':                          case 'g':
175                                  sendmotion = False;                                  width = strtol(optarg, &p, 10);
176                                    if (*p == 'x')
177                                            height = strtol(p + 1, NULL, 10);
178    
179                                    if ((width == 0) || (height == 0))
180                                    {
181                                            error("invalid geometry\n");
182                                            return 1;
183                                    }
184                                    break;
185    
186                            case 'f':
187                                    fullscreen = True;
188                                  break;                                  break;
189    
190                          case 'b':                          case 'b':
191                                  orders = False;                                  orders = False;
192                                  break;                                  break;
193    
194                          case 'l':                          case 'e':
195                                  licence = False;                                  encryption = False;
196                                  break;                                  break;
197    
198                          case 'e':                          case 'm':
199                                  use_encryption = False;                                  sendmotion = False;
200                                  break;                                  break;
201    
202                          case 'F':                          case 't':
203                                  fullscreen = True;                                  tcp_port_rdp = strtol(optarg, NULL, 10);
204                                    break;
205    
206                            case 'K':
207                                    grab_keyboard = False;
208                                    break;
209    
210                            case 'w':
211                                    strncpy(title, optarg, sizeof(title));
212                                  break;                                  break;
213    
214                          case 'h':                          case 'h':
# Line 172  main(int argc, char *argv[]) Line 232  main(int argc, char *argv[])
232                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
233                  if ((pw == NULL) || (pw->pw_name == NULL))                  if ((pw == NULL) || (pw->pw_name == NULL))
234                  {                  {
235                          STATUS("Could not determine user name.\n");                          error("could not determine username, use -u\n");
236                          return 1;                          return 1;
237                  }                  }
238    
239                  strncpy(username, pw->pw_name, sizeof(username));                  STRNCPY(username, pw->pw_name, sizeof(username));
240          }          }
241    
242          if (hostname[0] == 0)          if (hostname[0] == 0)
243          {          {
244                  if (gethostname(hostname, sizeof(hostname)) == -1)                  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
245                  {                  {
246                          STATUS("Could not determine host name.\n");                          error("could not determine local hostname, use -n\n");
247                          return 1;                          return 1;
248                  }                  }
249    
250                    p = strchr(fullhostname, '.');
251                    if (p != NULL)
252                            *p = 0;
253    
254                    STRNCPY(hostname, fullhostname, sizeof(hostname));
255          }          }
256    
257          strcpy(title, "rdesktop - ");          if (prompt_password && read_password(password, sizeof(password)))
258          strncat(title, server, sizeof(title));                  flags |= RDP_LOGON_AUTO;
259    
260          if (ui_create_window(title))          if (title[0] == 0)
261          {          {
262                  if (!rdp_connect(server, flags, domain, password, shell,                  strcpy(title, "rdesktop - ");
263                                   directory))                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
264                          return 1;          }
265    
266            if (!ui_init())
267                    return 1;
268    
269            if (!rdp_connect(server, flags, domain, password, shell, directory))
270                    return 1;
271    
272            DEBUG(("Connection successful.\n"));
273            memset(password, 0, sizeof(password));
274    
275                  STATUS("Connection successful.\n");          if (ui_create_window())
276            {
277                  rdp_main_loop();                  rdp_main_loop();
278                  ui_destroy_window();                  ui_destroy_window();
279          }          }
280    
281            DEBUG(("Disconnecting...\n"));
282          rdp_disconnect();          rdp_disconnect();
283            ui_deinit();
284          return 0;          return 0;
285  }  }
286    
287  /* Generate a 32-byte random for the secure transport code. */  /* Generate a 32-byte random for the secure transport code. */
288  void  void
289  generate_random(uint8 *random)  generate_random(uint8 * random)
290  {  {
291          struct stat st;          struct stat st;
292          struct tms tmsbuf;          struct tms tmsbuf;
# Line 216  generate_random(uint8 *random) Line 294  generate_random(uint8 *random)
294          int fd;          int fd;
295    
296          /* If we have a kernel random device, use it. */          /* If we have a kernel random device, use it. */
297          if ((fd = open("/dev/urandom", O_RDONLY)) != -1)          if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
298                || ((fd = open("/dev/random", O_RDONLY)) != -1))
299          {          {
300                  read(fd, random, 32);                  read(fd, random, 32);
301                  close(fd);                  close(fd);
# Line 241  xmalloc(int size) Line 320  xmalloc(int size)
320          void *mem = malloc(size);          void *mem = malloc(size);
321          if (mem == NULL)          if (mem == NULL)
322          {          {
323                  ERROR("xmalloc %d\n", size);                  error("xmalloc %d\n", size);
324                  exit(1);                  exit(1);
325          }          }
326          return mem;          return mem;
# Line 254  xrealloc(void *oldmem, int size) Line 333  xrealloc(void *oldmem, int size)
333          void *mem = realloc(oldmem, size);          void *mem = realloc(oldmem, size);
334          if (mem == NULL)          if (mem == NULL)
335          {          {
336                  ERROR("xrealloc %d\n", size);                  error("xrealloc %d\n", size);
337                  exit(1);                  exit(1);
338          }          }
339          return mem;          return mem;
# Line 267  xfree(void *mem) Line 346  xfree(void *mem)
346          free(mem);          free(mem);
347  }  }
348    
349  /* Produce a hex dump */  /* report an error */
350    void
351    error(char *format, ...)
352    {
353            va_list ap;
354    
355            fprintf(stderr, "ERROR: ");
356    
357            va_start(ap, format);
358            vfprintf(stderr, format, ap);
359            va_end(ap);
360    }
361    
362    /* report an unimplemented protocol feature */
363    void
364    unimpl(char *format, ...)
365    {
366            va_list ap;
367    
368            fprintf(stderr, "NOT IMPLEMENTED: ");
369    
370            va_start(ap, format);
371            vfprintf(stderr, format, ap);
372            va_end(ap);
373    }
374    
375    /* produce a hex dump */
376  void  void
377  hexdump(unsigned char *p, unsigned int len)  hexdump(unsigned char *p, unsigned int len)
378  {  {
# Line 277  hexdump(unsigned char *p, unsigned int l Line 382  hexdump(unsigned char *p, unsigned int l
382    
383          while (offset < len)          while (offset < len)
384          {          {
385                  STATUS("%04x ", offset);                  printf("%04x ", offset);
386                  thisline = len - offset;                  thisline = len - offset;
387                  if (thisline > 16)                  if (thisline > 16)
388                          thisline = 16;                          thisline = 16;
389    
390                  for (i = 0; i < thisline; i++)                  for (i = 0; i < thisline; i++)
391                          STATUS("%02x ", line[i]) for (; i < 16; i++)                          printf("%02x ", line[i]);
392                                  STATUS("   ");  
393                    for (; i < 16; i++)
394                            printf("   ");
395    
396                  for (i = 0; i < thisline; i++)                  for (i = 0; i < thisline; i++)
397                          STATUS("%c",                          printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
                                (line[i] >= 0x20  
                                 && line[i] < 0x7f) ? line[i] : '.');  
398    
399                  STATUS("\n");                  printf("\n");
400                  offset += thisline;                  offset += thisline;
401                  line += thisline;                  line += thisline;
402          }          }
403  }  }
404    
405    #ifdef SAVE_LICENCE
406    int
407    load_licence(unsigned char **data)
408    {
409            char path[PATH_MAX];
410            char *home;
411            struct stat st;
412            int fd;
413    
414            home = getenv("HOME");
415            if (home == NULL)
416                    return -1;
417    
418            STRNCPY(path, home, sizeof(path));
419            strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1);
420    
421            fd = open(path, O_RDONLY);
422            if (fd == -1)
423                    return -1;
424    
425            if (fstat(fd, &st))
426                    return -1;
427    
428            *data = xmalloc(st.st_size);
429            return read(fd, *data, st.st_size);
430    }
431    
432    void
433    save_licence(unsigned char *data, int length)
434    {
435            char path[PATH_MAX];
436            char *home;
437            int fd;
438    
439            home = getenv("HOME");
440            if (home == NULL)
441                    return;
442    
443            STRNCPY(path, home, sizeof(path));
444            strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1);
445            mkdir(path, 0700);
446    
447            strncat(path, "/licence", sizeof(path) - strlen(path) - 1);
448    
449            fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
450            if (fd == -1)
451            {
452                    perror("open");
453                    return;
454            }
455    
456            write(fd, data, length);
457            close(fd);
458    }
459    #endif

Legend:
Removed from v.29  
changed lines
  Added in v.214

  ViewVC Help
Powered by ViewVC 1.1.26