/[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 17 by matty, Thu Sep 28 15:54:11 2000 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 <sys/stat.h>   /* stat */  #include <limits.h>             /* PATH_MAX */
26  #include <sys/time.h>   /* gettimeofday */  #include <termios.h>            /* tcgetattr tcsetattr */
27  #include <sys/times.h>  /* times */  #include <sys/stat.h>           /* stat */
28    #include <sys/time.h>           /* gettimeofday */
29    #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 motion = False;  BOOL bitmap_compression = True;
41    BOOL sendmotion = True;
42  BOOL orders = True;  BOOL orders = True;
43  BOOL licence = True;  BOOL encryption = True;
44    BOOL desktop_save = True;
45    BOOL fullscreen = False;
46    BOOL grab_keyboard = True;
47    
48  /* Display usage information */  /* Display usage information */
49  static void usage(char *program)  static void
50    usage(char *program)
51    {
52            fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
53            fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");
54            fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
55    
56            fprintf(stderr, "Usage: %s [options] server\n", program);
57            fprintf(stderr, "   -u: user name\n");
58            fprintf(stderr, "   -d: domain\n");
59            fprintf(stderr, "   -s: shell\n");
60            fprintf(stderr, "   -c: working directory\n");
61            fprintf(stderr, "   -p: password (- to prompt)\n");
62            fprintf(stderr, "   -n: client hostname\n");
63            fprintf(stderr, "   -k: keyboard layout on terminal server (us,sv,gr etc.)\n");
64            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          STATUS("Usage: %s [options] server\n", program);          struct termios tios;
78          STATUS("   -u: user name\n");          BOOL ret = False;
79          STATUS("   -n: client hostname\n");          int istty = 0;
80          STATUS("   -w: desktop width\n");          char *p;
81          STATUS("   -h: desktop height\n");  
82          STATUS("   -k: keyboard layout (hex)\n");          if (tcgetattr(STDIN_FILENO, &tios) == 0)
83          STATUS("   -m: send motion events\n");          {
84          STATUS("   -b: force bitmap updates\n");                  fprintf(stderr, "Password: ");
85          STATUS("   -l: do not request licence\n\n");                  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 main(int argc, char *argv[])  int
112    main(int argc, char *argv[])
113  {  {
114            char fullhostname[64];
115            char domain[16];
116            char password[16];
117            char shell[32];
118            char directory[32];
119            BOOL prompt_password;
120          struct passwd *pw;          struct passwd *pw;
121          char *server;          char *server, *p;
122          char title[32];          uint32 flags;
123          int c;          int c;
124    
125          STATUS("rdesktop: A Remote Desktop Protocol client.\n");          flags = RDP_LOGON_NORMAL;
126          STATUS("Version "VERSION". Copyright (C) 1999-2000 Matt Chapman.\n");          prompt_password = False;
127          STATUS("See http://www.rdesktop.org/ for more information.\n\n");          domain[0] = password[0] = shell[0] = directory[0] = 0;
128            strcpy(keymapname, "us");
129    
130          while ((c = getopt(argc, argv, "u:n:w:h:k:mbl?")) != -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 'n':                          case 'd':
139                                  strncpy(hostname, optarg, sizeof(hostname));                                  STRNCPY(domain, optarg, sizeof(domain));
140                                  break;                                  break;
141    
142                          case 'w':                          case 's':
143                                  width = strtol(optarg, NULL, 10);                                  STRNCPY(shell, optarg, sizeof(shell));
144                                  break;                                  break;
145    
146                          case 'h':                          case 'c':
147                                  height = strtol(optarg, NULL, 10);                                  STRNCPY(directory, optarg, sizeof(directory));
148                                    break;
149    
150                            case 'p':
151                                    if ((optarg[0] == '-') && (optarg[1] == 0))
152                                    {
153                                            prompt_password = True;
154                                            break;
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));
172                                  break;                                  break;
173    
174                          case 'm':                          case 'g':
175                                  motion = True;                                  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 'm':
199                                    sendmotion = False;
200                                    break;
201    
202                            case 't':
203                                    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;
213    
214                            case 'h':
215                          case '?':                          case '?':
216                          default:                          default:
217                                  usage(argv[0]);                                  usage(argv[0]);
# Line 118  int main(int argc, char *argv[]) Line 232  int 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            if (prompt_password && read_password(password, sizeof(password)))
258                    flags |= RDP_LOGON_AUTO;
259    
260            if (title[0] == 0)
261            {
262                    strcpy(title, "rdesktop - ");
263                    strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
264          }          }
265    
266          if (!rdp_connect(server))          if (!ui_init())
267                  return 1;                  return 1;
268    
269          STATUS("Connection successful.\n");          if (!rdp_connect(server, flags, domain, password, shell, directory))
270                    return 1;
271    
272          strcpy(title, "rdesktop - ");          DEBUG(("Connection successful.\n"));
273          strncat(title, server, sizeof(title));          memset(password, 0, sizeof(password));
274    
275          if (ui_create_window(title))          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 generate_random(uint8 *random)  void
289    generate_random(uint8 * random)
290  {  {
291          struct stat st;          struct stat st;
292          uint32 *r = (uint32 *)random;          struct tms tmsbuf;
293            uint32 *r = (uint32 *) 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 170  void generate_random(uint8 *random) Line 305  void generate_random(uint8 *random)
305          /* Otherwise use whatever entropy we can gather - ideas welcome. */          /* Otherwise use whatever entropy we can gather - ideas welcome. */
306          r[0] = (getpid()) | (getppid() << 16);          r[0] = (getpid()) | (getppid() << 16);
307          r[1] = (getuid()) | (getgid() << 16);          r[1] = (getuid()) | (getgid() << 16);
308          r[2] = times(NULL); /* system uptime (clocks) */          r[2] = times(&tmsbuf);  /* system uptime (clocks) */
309          gettimeofday((struct timeval *)&r[3], NULL); /* sec and usec */          gettimeofday((struct timeval *) &r[3], NULL);   /* sec and usec */
310          stat("/tmp", &st);          stat("/tmp", &st);
311          r[5] = st.st_atime;          r[5] = st.st_atime;
312          r[6] = st.st_mtime;          r[6] = st.st_mtime;
# Line 179  void generate_random(uint8 *random) Line 314  void generate_random(uint8 *random)
314  }  }
315    
316  /* malloc; exit if out of memory */  /* malloc; exit if out of memory */
317  void *xmalloc(int size)  void *
318    xmalloc(int size)
319  {  {
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;
327  }  }
328    
329  /* realloc; exit if out of memory */  /* realloc; exit if out of memory */
330  void *xrealloc(void *oldmem, int size)  void *
331    xrealloc(void *oldmem, int size)
332  {  {
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;
340  }  }
341    
342  /* free */  /* free */
343  void xfree(void *mem)  void
344    xfree(void *mem)
345  {  {
346          free(mem);          free(mem);
347  }  }
348    
349  /* Produce a hex dump */  /* report an error */
350  void hexdump(unsigned char *p, unsigned int len)  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
377    hexdump(unsigned char *p, unsigned int len)
378  {  {
379          unsigned char *line = p;          unsigned char *line = p;
380          unsigned int thisline, offset = 0;          unsigned int thisline, offset = 0;
# Line 217  void hexdump(unsigned char *p, unsigned Line 382  void hexdump(unsigned char *p, unsigned
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])                          printf("%02x ", line[i]);
392    
393                  for (; i < 16; i++)                  for (; i < 16; i++)
394                          STATUS("   ");                          printf("   ");
395    
396                  for (i = 0; i < thisline; i++)                  for (i = 0; i < thisline; i++)
397                          STATUS("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');                          printf("%c", (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.17  
changed lines
  Added in v.214

  ViewVC Help
Powered by ViewVC 1.1.26