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

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

  ViewVC Help
Powered by ViewVC 1.1.26