/[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 21 by matty, Mon Oct 16 08:44:48 2000 UTC revision 58 by jsorg71, Sun Jul 14 00:34:21 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-2001
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 18  Line 18 
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 <stdlib.h>             /* malloc realloc free */
22  #include <unistd.h>     /* read close getuid getgid getpid getppid gethostname */  #include <stdarg.h>             /* va_list va_start va_end */
23  #include <fcntl.h>      /* open */  #include <unistd.h>             /* read close getuid getgid getpid getppid gethostname */
24  #include <pwd.h>        /* getpwuid */  #include <fcntl.h>              /* open */
25  #include <sys/stat.h>   /* stat */  #include <pwd.h>                /* getpwuid */
26  #include <sys/time.h>   /* gettimeofday */  #include <limits.h>             /* PATH_MAX */
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 username[16];  char username[16];
33  char hostname[16];  char hostname[16];
34  int width = 800;  char keymapname[16];
35  int height = 600;  int keylayout;
36  int keylayout = 0x409;  int width;
37  BOOL motion = True;  int height;
38    int tcp_port_rdp = TCP_PORT_RDP;
39    BOOL bitmap_compression = True;
40    BOOL sendmotion = True;
41  BOOL orders = True;  BOOL orders = True;
42  BOOL licence = True;  BOOL licence = True;
43    BOOL encryption = True;
44    BOOL desktop_save = True;
45    BOOL fullscreen = False;
46    
47  /* Display usage information */  /* Display usage information */
48  static void usage(char *program)  static void
49    usage(char *program)
50  {  {
51          STATUS("Usage: %s [options] server\n", program);          printf("Usage: %s [options] server\n", program);
52          STATUS("   -u: user name\n");          printf("   -u: user name\n");
53          STATUS("   -d: domain\n");          printf("   -d: domain\n");
54          STATUS("   -s: shell\n");          printf("   -s: shell\n");
55          STATUS("   -c: working directory\n");          printf("   -c: working directory\n");
56          STATUS("   -p: password (autologon)\n");          printf("   -p: password (autologon)\n");
57          STATUS("   -n: client hostname\n");          printf("   -n: client hostname\n");
58          STATUS("   -w: desktop width\n");          printf("   -k: keyboard layout\n");
59          STATUS("   -h: desktop height\n");          printf("   -g: desktop geometry (WxH)\n");
60          STATUS("   -k: keyboard layout (hex)\n");          printf("   -f: full-screen mode\n");
61          STATUS("   -b: force bitmap updates\n");          printf("   -b: force bitmap updates\n");
62          STATUS("   -m: do not send motion events\n");          printf("   -e: disable encryption (French TS)\n");
63          STATUS("   -l: do not request licence\n\n");          printf("   -m: do not send motion events\n");
64            printf("   -l: do not request licence\n");
65            printf("   -t: rdp tcp port\n\n");
66  }  }
67    
68  /* Client program */  /* Client program */
69  int main(int argc, char *argv[])  int
70    main(int argc, char *argv[])
71  {  {
72          struct passwd *pw;          char fullhostname[64];
         char *server;  
         uint32 flags;  
73          char domain[16];          char domain[16];
74          char password[16];          char password[16];
75          char shell[32];          char shell[32];
76          char directory[32];          char directory[32];
77          char title[32];          char title[32];
78            struct passwd *pw;
79            char *server, *p;
80            uint32 flags;
81          int c;          int c;
82    
83          STATUS("rdesktop: A Remote Desktop Protocol client.\n");          printf("rdesktop: A Remote Desktop Protocol client.\n");
84          STATUS("Version "VERSION". Copyright (C) 1999-2000 Matt Chapman.\n");          printf("Version " VERSION ". Copyright (C) 1999-2001 Matt Chapman.\n");
85          STATUS("See http://www.rdesktop.org/ for more information.\n\n");          printf("See http://www.rdesktop.org/ for more information.\n\n");
86    
87          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
88          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
89            strcpy(keymapname, "us");
90    
91          while ((c = getopt(argc, argv, "u:d:s:c:p:n:w:h:k:bml?")) != -1)          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:t:fbemlh?")) != -1)
92          {          {
93                  switch (c)                  switch (c)
94                  {                  {
95                          case 'u':                          case 'u':
96                                  strncpy(username, optarg, sizeof(username));                                  STRNCPY(username, optarg, sizeof(username));
97                                  break;                                  break;
98    
99                          case 'd':                          case 'd':
100                                  strncpy(domain, optarg, sizeof(domain));                                  STRNCPY(domain, optarg, sizeof(domain));
                                 break;  
   
                         case 'p':  
                                 flags |= RDP_LOGON_AUTO;  
                                 strncpy(password, optarg, sizeof(password));  
101                                  break;                                  break;
102    
103                          case 's':                          case 's':
104                                  strncpy(shell, optarg, sizeof(shell));                                  STRNCPY(shell, optarg, sizeof(shell));
105                                  break;                                  break;
106    
107                          case 'c':                          case 'c':
108                                  strncpy(directory, optarg, sizeof(directory));                                  STRNCPY(directory, optarg, sizeof(directory));
109                                  break;                                  break;
110    
111                          case 'n':                          case 'p':
112                                  strncpy(hostname, optarg, sizeof(hostname));                                  STRNCPY(password, optarg, sizeof(password));
113                                    flags |= RDP_LOGON_AUTO;
114                                  break;                                  break;
115    
116                          case 'w':                          case 'n':
117                                  width = strtol(optarg, NULL, 10);                                  STRNCPY(hostname, optarg, sizeof(hostname));
118                                  break;                                  break;
119    
120                          case 'h':                          case 'k':
121                                  height = strtol(optarg, NULL, 10);                                  STRNCPY(keymapname, optarg, sizeof(keymapname));
122                                  break;                                  break;
123    
124                          case 'k':                          case 'g':
125                                  keylayout = strtol(optarg, NULL, 16);                                  width = strtol(optarg, &p, 10);
126                                    if (*p == 'x')
127                                            height = strtol(p+1, NULL, 10);
128    
129                                    if ((width == 0) || (height == 0))
130                                    {
131                                            error("invalid geometry\n");
132                                            return 1;
133                                    }
134                                  break;                                  break;
135    
136                          case 'm':                          case 'f':
137                                  motion = False;                                  fullscreen = True;
138                                  break;                                  break;
139    
140                          case 'b':                          case 'b':
141                                  orders = False;                                  orders = False;
142                                  break;                                  break;
143    
144                            case 'e':
145                                    encryption = False;
146                                    break;
147    
148                            case 'm':
149                                    sendmotion = False;
150                                    break;
151    
152                          case 'l':                          case 'l':
153                                  licence = False;                                  licence = False;
154                                  break;                                  break;
155    
156                            case 't':
157                                    tcp_port_rdp = strtol(optarg, NULL, 10);
158                                    break;
159    
160                            case 'h':
161                          case '?':                          case '?':
162                          default:                          default:
163                                  usage(argv[0]);                                  usage(argv[0]);
# Line 147  int main(int argc, char *argv[]) Line 178  int main(int argc, char *argv[])
178                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
179                  if ((pw == NULL) || (pw->pw_name == NULL))                  if ((pw == NULL) || (pw->pw_name == NULL))
180                  {                  {
181                          STATUS("Could not determine user name.\n");                          error("could not determine username, use -u\n");
182                          return 1;                          return 1;
183                  }                  }
184    
185                  strncpy(username, pw->pw_name, sizeof(username));                  STRNCPY(username, pw->pw_name, sizeof(username));
186          }          }
187    
188          if (hostname[0] == 0)          if (hostname[0] == 0)
189          {          {
190                  if (gethostname(hostname, sizeof(hostname)) == -1)                  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
191                  {                  {
192                          STATUS("Could not determine host name.\n");                          error("could not determine local hostname, use -n\n");
193                          return 1;                          return 1;
194                  }                  }
195    
196                    p = strchr(fullhostname, '.');
197                    if (p != NULL)
198                            *p = 0;
199    
200                    STRNCPY(hostname, fullhostname, sizeof(hostname));
201          }          }
202    
203          if (!rdp_connect(server, flags, domain, password, shell, directory))          if (!strcmp(password, "-"))
204                  return 1;          {
205                    p = getpass("Password: ");
206                    if (p == NULL)
207                    {
208                            error("failed to read password\n");
209                            return 0;
210                    }
211                    STRNCPY(password, p, sizeof(password));
212            }
213    
214          STATUS("Connection successful.\n");          if ((width == 0) || (height == 0))
215            {
216                    width = 800;
217                    height = 600;
218            }
219    
220          strcpy(title, "rdesktop - ");          strcpy(title, "rdesktop - ");
221          strncat(title, server, sizeof(title));          strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
222    
223            if (!rdp_connect(server, flags, domain, password, shell, directory))
224                    return 1;
225    
226            printf("Connection successful.\n");
227    
228          if (ui_create_window(title))          if (ui_create_window(title))
229          {          {
# Line 177  int main(int argc, char *argv[]) Line 231  int main(int argc, char *argv[])
231                  ui_destroy_window();                  ui_destroy_window();
232          }          }
233    
234            printf("Disconnecting...\n");
235          rdp_disconnect();          rdp_disconnect();
236          return 0;          return 0;
237  }  }
238    
239  /* Generate a 32-byte random for the secure transport code. */  /* Generate a 32-byte random for the secure transport code. */
240  void generate_random(uint8 *random)  void
241    generate_random(uint8 *random)
242  {  {
243          struct stat st;          struct stat st;
244          uint32 *r = (uint32 *)random;          struct tms tmsbuf;
245            uint32 *r = (uint32 *) random;
246          int fd;          int fd;
247    
248          /* If we have a kernel random device, use it. */          /* If we have a kernel random device, use it. */
249          if ((fd = open("/dev/urandom", O_RDONLY)) != -1)          if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
250                || ((fd = open("/dev/random", O_RDONLY)) != -1))
251          {          {
252                  read(fd, random, 32);                  read(fd, random, 32);
253                  close(fd);                  close(fd);
# Line 199  void generate_random(uint8 *random) Line 257  void generate_random(uint8 *random)
257          /* Otherwise use whatever entropy we can gather - ideas welcome. */          /* Otherwise use whatever entropy we can gather - ideas welcome. */
258          r[0] = (getpid()) | (getppid() << 16);          r[0] = (getpid()) | (getppid() << 16);
259          r[1] = (getuid()) | (getgid() << 16);          r[1] = (getuid()) | (getgid() << 16);
260          r[2] = times(NULL); /* system uptime (clocks) */          r[2] = times(&tmsbuf);  /* system uptime (clocks) */
261          gettimeofday((struct timeval *)&r[3], NULL); /* sec and usec */          gettimeofday((struct timeval *) &r[3], NULL);   /* sec and usec */
262          stat("/tmp", &st);          stat("/tmp", &st);
263          r[5] = st.st_atime;          r[5] = st.st_atime;
264          r[6] = st.st_mtime;          r[6] = st.st_mtime;
# Line 208  void generate_random(uint8 *random) Line 266  void generate_random(uint8 *random)
266  }  }
267    
268  /* malloc; exit if out of memory */  /* malloc; exit if out of memory */
269  void *xmalloc(int size)  void *
270    xmalloc(int size)
271  {  {
272          void *mem = malloc(size);          void *mem = malloc(size);
273          if (mem == NULL)          if (mem == NULL)
274          {          {
275                  ERROR("xmalloc %d\n", size);                  error("xmalloc %d\n", size);
276                  exit(1);                  exit(1);
277          }          }
278          return mem;          return mem;
279  }  }
280    
281  /* realloc; exit if out of memory */  /* realloc; exit if out of memory */
282  void *xrealloc(void *oldmem, int size)  void *
283    xrealloc(void *oldmem, int size)
284  {  {
285          void *mem = realloc(oldmem, size);          void *mem = realloc(oldmem, size);
286          if (mem == NULL)          if (mem == NULL)
287          {          {
288                  ERROR("xrealloc %d\n", size);                  error("xrealloc %d\n", size);
289                  exit(1);                  exit(1);
290          }          }
291          return mem;          return mem;
292  }  }
293    
294  /* free */  /* free */
295  void xfree(void *mem)  void
296    xfree(void *mem)
297  {  {
298          free(mem);          free(mem);
299  }  }
300    
301  /* Produce a hex dump */  /* report an error */
302  void hexdump(unsigned char *p, unsigned int len)  void
303    error(char *format, ...)
304    {
305            va_list ap;
306    
307            fprintf(stderr, "ERROR: ");
308    
309            va_start(ap, format);
310            vfprintf(stderr, format, ap);
311            va_end(ap);
312    }
313    
314    /* report an unimplemented protocol feature */
315    void
316    unimpl(char *format, ...)
317    {
318            va_list ap;
319    
320            fprintf(stderr, "NOT IMPLEMENTED: ");
321    
322            va_start(ap, format);
323            vfprintf(stderr, format, ap);
324            va_end(ap);
325    }
326    
327    /* produce a hex dump */
328    void
329    hexdump(unsigned char *p, unsigned int len)
330  {  {
331          unsigned char *line = p;          unsigned char *line = p;
332          unsigned int thisline, offset = 0;          unsigned int thisline, offset = 0;
# Line 246  void hexdump(unsigned char *p, unsigned Line 334  void hexdump(unsigned char *p, unsigned
334    
335          while (offset < len)          while (offset < len)
336          {          {
337                  STATUS("%04x ", offset);                  printf("%04x ", offset);
338                  thisline = len - offset;                  thisline = len - offset;
339                  if (thisline > 16)                  if (thisline > 16)
340                          thisline = 16;                          thisline = 16;
341    
342                  for (i = 0; i < thisline; i++)                  for (i = 0; i < thisline; i++)
343                          STATUS("%02x ", line[i])                          printf("%02x ", line[i]);
344    
345                  for (; i < 16; i++)                  for (; i < 16; i++)
346                          STATUS("   ");                                  printf("   ");
347    
348                  for (i = 0; i < thisline; i++)                  for (i = 0; i < thisline; i++)
349                          STATUS("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');                          printf("%c",
350                                   (line[i] >= 0x20
351                                    && line[i] < 0x7f) ? line[i] : '.');
352    
353                  STATUS("\n");                  printf("\n");
354                  offset += thisline;                  offset += thisline;
355                  line += thisline;                  line += thisline;
356          }          }
357  }  }
358    
359    int
360    load_licence(unsigned char **data)
361    {
362            char path[PATH_MAX];
363            char *home;
364            struct stat st;
365            int fd;
366    
367            home = getenv("HOME");
368            if (home == NULL)
369                    return -1;
370    
371            STRNCPY(path, home, sizeof(path));
372            strncat(path, "/.rdesktop/licence", sizeof(path)-strlen(path)-1);
373    
374            fd = open(path, O_RDONLY);
375            if (fd == -1)
376                    return -1;
377    
378            if (fstat(fd, &st))
379                    return -1;
380    
381            *data = xmalloc(st.st_size);
382            return read(fd, *data, st.st_size);
383    }
384    
385    void
386    save_licence(unsigned char *data, int length)
387    {
388            char path[PATH_MAX];
389            char *home;
390            int fd;
391    
392            home = getenv("HOME");
393            if (home == NULL)
394                    return;
395    
396            STRNCPY(path, home, sizeof(path));
397            strncat(path, "/.rdesktop", sizeof(path)-strlen(path)-1);
398            mkdir(path, 0700);
399    
400            strncat(path, "/licence", sizeof(path)-strlen(path)-1);
401    
402            fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0600);
403            if (fd == -1)
404            {
405                    perror("open");
406                    return;
407            }
408    
409            write(fd, data, length);
410            close(fd);
411    }
412    

Legend:
Removed from v.21  
changed lines
  Added in v.58

  ViewVC Help
Powered by ViewVC 1.1.26