/[rdesktop]/sourceforge.net/branches/seamlessrdp-branch/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/branches/seamlessrdp-branch/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 603 by stargo, Sat Feb 7 18:47:06 2004 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-2000     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
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 <termios.h>            /* tcgetattr tcsetattr */
26  #include <sys/time.h>   /* gettimeofday */  #include <sys/stat.h>           /* stat */
27  #include <sys/times.h>  /* times */  #include <sys/time.h>           /* gettimeofday */
28    #include <sys/times.h>          /* times */
29    #include <ctype.h>              /* toupper */
30    #include <errno.h>
31  #include "rdesktop.h"  #include "rdesktop.h"
32    
33  char username[16];  #ifdef EGD_SOCKET
34    #include <sys/socket.h>         /* socket connect */
35    #include <sys/un.h>             /* sockaddr_un */
36    #endif
37    
38    #ifdef WITH_OPENSSL
39    #include <openssl/md5.h>
40    #else
41    #include "crypto/md5.h"
42    #endif
43    
44    char g_title[64] = "";
45    char g_username[64];
46  char hostname[16];  char hostname[16];
47  int width = 800;  char keymapname[16];
48  int height = 600;  int keylayout = 0x409;          /* Defaults to US keyboard layout */
 int keylayout = 0x409;  
 BOOL motion = False;  
 BOOL orders = True;  
 BOOL licence = True;  
49    
50    int g_width = 800;              /* width is special: If 0, the
51                                       geometry will be fetched from
52                                       _NET_WORKAREA. If negative,
53                                       absolute value specifies the
54                                       percent of the whole screen. */
55    int g_height = 600;
56    int tcp_port_rdp = TCP_PORT_RDP;
57    int g_server_bpp = 8;
58    int g_win_button_size = 0;      /* If zero, disable single app mode */
59    BOOL g_bitmap_compression = True;
60    BOOL g_sendmotion = True;
61    BOOL g_orders = True;
62    BOOL g_encryption = True;
63    BOOL packet_encryption = True;
64    BOOL g_desktop_save = True;
65    BOOL g_fullscreen = False;
66    BOOL g_grab_keyboard = True;
67    BOOL g_hide_decorations = False;
68    BOOL g_use_rdp5 = True;
69    BOOL g_console_session = False;
70    BOOL g_numlock_sync = False;
71    extern BOOL g_owncolmap;
72    
73    #ifdef WITH_RDPSND
74    BOOL g_rdpsnd = False;
75    #endif
76    
77    extern RDPDR_DEVICE g_rdpdr_device[];
78    extern uint32 g_num_devices;
79    
80    #ifdef RDP2VNC
81    extern int rfb_port;
82    extern int defer_time;
83    void
84    rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
85                    char *shell, char *directory);
86    #endif
87  /* Display usage information */  /* Display usage information */
88  static void usage(char *program)  static void
89    usage(char *program)
90    {
91            fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
92            fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
93            fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
94    
95            fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
96    #ifdef RDP2VNC
97            fprintf(stderr, "   -V: vnc port\n");
98            fprintf(stderr, "   -Q: defer time (ms)\n");
99    #endif
100            fprintf(stderr, "   -u: user name\n");
101            fprintf(stderr, "   -d: domain\n");
102            fprintf(stderr, "   -s: shell\n");
103            fprintf(stderr, "   -c: working directory\n");
104            fprintf(stderr, "   -p: password (- to prompt)\n");
105            fprintf(stderr, "   -n: client hostname\n");
106            fprintf(stderr, "   -k: keyboard layout on server (en-us, de, sv, etc.)\n");
107            fprintf(stderr, "   -g: desktop geometry (WxH)\n");
108            fprintf(stderr, "   -f: full-screen mode\n");
109            fprintf(stderr, "   -b: force bitmap updates\n");
110            fprintf(stderr, "   -e: disable encryption (French TS)\n");
111            fprintf(stderr, "   -E: disable encryption from client to server\n");
112            fprintf(stderr, "   -m: do not send motion events\n");
113            fprintf(stderr, "   -C: use private colour map\n");
114            fprintf(stderr, "   -D: hide window manager decorations\n");
115            fprintf(stderr, "   -K: keep window manager key bindings\n");
116            fprintf(stderr, "   -S: caption button size (single application mode)\n");
117            fprintf(stderr, "   -T: window title\n");
118            fprintf(stderr, "   -N: enable numlock syncronization\n");
119            fprintf(stderr, "   -a: connection colour depth\n");
120            fprintf(stderr, "   -r: enable specified device redirection (this flag can be repeated)\n");
121            fprintf(stderr, "         '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");
122            fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");
123            fprintf(stderr, "         '-r disk:A=/mnt/floppy': enable redirection of /mnt/floppy to A:\n");
124            fprintf(stderr, "             or   A=/mnt/floppy,D=/mnt/cdrom'\n");
125            fprintf(stderr, "         '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n");
126            fprintf(stderr, "             or      LPT1=/dev/lp0,LPT2=/dev/lp1\n");
127            fprintf(stderr, "         '-r printer:mydeskjet': enable printer redirection\n");
128            fprintf(stderr, "             or       mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");
129            fprintf(stderr, "         '-r sound': enable sound redirection\n");
130            fprintf(stderr, "   -0: attach to console\n");
131            fprintf(stderr, "   -4: use RDP version 4\n");
132            fprintf(stderr, "   -5: use RDP version 5 (default)\n");
133    }
134    
135    static BOOL
136    read_password(char *password, int size)
137    {
138            struct termios tios;
139            BOOL ret = False;
140            int istty = 0;
141            char *p;
142    
143            if (tcgetattr(STDIN_FILENO, &tios) == 0)
144            {
145                    fprintf(stderr, "Password: ");
146                    tios.c_lflag &= ~ECHO;
147                    tcsetattr(STDIN_FILENO, TCSANOW, &tios);
148                    istty = 1;
149            }
150    
151            if (fgets(password, size, stdin) != NULL)
152            {
153                    ret = True;
154    
155                    /* strip final newline */
156                    p = strchr(password, '\n');
157                    if (p != NULL)
158                            *p = 0;
159            }
160    
161            if (istty)
162            {
163                    tios.c_lflag |= ECHO;
164                    tcsetattr(STDIN_FILENO, TCSANOW, &tios);
165                    fprintf(stderr, "\n");
166            }
167    
168            return ret;
169    }
170    
171    static void
172    parse_server_and_port(char *server)
173  {  {
174          STATUS("Usage: %s [options] server\n", program);          char *p;
175          STATUS("   -u: user name\n");  #ifdef IPv6
176          STATUS("   -n: client hostname\n");          int addr_colons;
177          STATUS("   -w: desktop width\n");  #endif
178          STATUS("   -h: desktop height\n");  
179          STATUS("   -k: keyboard layout (hex)\n");  #ifdef IPv6
180          STATUS("   -m: send motion events\n");          p = server;
181          STATUS("   -b: force bitmap updates\n");          addr_colons = 0;
182          STATUS("   -l: do not request licence\n\n");          while (*p)
183                    if (*p++ == ':')
184                            addr_colons++;
185            if (addr_colons >= 2)
186            {
187                    /* numeric IPv6 style address format - [1:2:3::4]:port */
188                    p = strchr(server, ']');
189                    if (*server == '[' && p != NULL)
190                    {
191                            if (*(p + 1) == ':' && *(p + 2) != '\0')
192                                    tcp_port_rdp = strtol(p + 2, NULL, 10);
193                            /* remove the port number and brackets from the address */
194                            *p = '\0';
195                            strncpy(server, server + 1, strlen(server));
196                    }
197            }
198            else
199            {
200                    /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
201                    p = strchr(server, ':');
202                    if (p != NULL)
203                    {
204                            tcp_port_rdp = strtol(p + 1, NULL, 10);
205                            *p = 0;
206                    }
207            }
208    #else /* no IPv6 support */
209            p = strchr(server, ':');
210            if (p != NULL)
211            {
212                    tcp_port_rdp = strtol(p + 1, NULL, 10);
213                    *p = 0;
214            }
215    #endif /* IPv6 */
216    
217  }  }
218    
219  /* Client program */  /* Client program */
220  int main(int argc, char *argv[])  int
221    main(int argc, char *argv[])
222  {  {
223            char server[64];
224            char fullhostname[64];
225            char domain[16];
226            char password[64];
227            char shell[128];
228            char directory[32];
229            BOOL prompt_password, rdp_retval = False;
230          struct passwd *pw;          struct passwd *pw;
231          char *server;          uint32 flags;
232          char title[32];          char *p;
233          int c;          int c;
234    
235          STATUS("rdesktop: A Remote Desktop Protocol client.\n");          int username_option = 0;
         STATUS("Version "VERSION". Copyright (C) 1999-2000 Matt Chapman.\n");  
         STATUS("See http://www.rdesktop.org/ for more information.\n\n");  
236    
237          while ((c = getopt(argc, argv, "u:n:w:h:k:mbl?")) != -1)          flags = RDP_LOGON_NORMAL;
238            prompt_password = False;
239            domain[0] = password[0] = shell[0] = directory[0] = 0;
240            strcpy(keymapname, "en-us");
241    
242            g_num_devices = 0;
243    
244    #ifdef RDP2VNC
245    #define VNCOPT "V:Q:"
246    #else
247    #define VNCOPT
248    #endif
249    
250            while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbeEmCDKS:T:Na:r:045h?")) != -1)
251          {          {
252                  switch (c)                  switch (c)
253                  {                  {
254    #ifdef RDP2VNC
255                            case 'V':
256                                    rfb_port = strtol(optarg, NULL, 10);
257                                    if (rfb_port < 100)
258                                            rfb_port += 5900;
259                                    break;
260    
261                            case 'Q':
262                                    defer_time = strtol(optarg, NULL, 10);
263                                    if (defer_time < 0)
264                                            defer_time = 0;
265                                    break;
266    #endif
267    
268                          case 'u':                          case 'u':
269                                  strncpy(username, optarg, sizeof(username));                                  STRNCPY(g_username, optarg, sizeof(g_username));
270                                    username_option = 1;
271                                  break;                                  break;
272    
273                          case 'n':                          case 'd':
274                                  strncpy(hostname, optarg, sizeof(hostname));                                  STRNCPY(domain, optarg, sizeof(domain));
275                                  break;                                  break;
276    
277                          case 'w':                          case 's':
278                                  width = strtol(optarg, NULL, 10);                                  STRNCPY(shell, optarg, sizeof(shell));
279                                  break;                                  break;
280    
281                          case 'h':                          case 'c':
282                                  height = strtol(optarg, NULL, 10);                                  STRNCPY(directory, optarg, sizeof(directory));
283                                    break;
284    
285                            case 'p':
286                                    if ((optarg[0] == '-') && (optarg[1] == 0))
287                                    {
288                                            prompt_password = True;
289                                            break;
290                                    }
291    
292                                    STRNCPY(password, optarg, sizeof(password));
293                                    flags |= RDP_LOGON_AUTO;
294    
295                                    /* try to overwrite argument so it won't appear in ps */
296                                    p = optarg;
297                                    while (*p)
298                                            *(p++) = 'X';
299                                    break;
300    
301                            case 'n':
302                                    STRNCPY(hostname, optarg, sizeof(hostname));
303                                  break;                                  break;
304    
305                          case 'k':                          case 'k':
306                                  keylayout = strtol(optarg, NULL, 16);                                  STRNCPY(keymapname, optarg, sizeof(keymapname));
307                                  break;                                  break;
308    
309                          case 'm':                          case 'g':
310                                  motion = True;                                  g_fullscreen = False;
311                                    if (!strcmp(optarg, "workarea"))
312                                    {
313                                            g_width = g_height = 0;
314                                            break;
315                                    }
316    
317                                    g_width = strtol(optarg, &p, 10);
318                                    if (g_width <= 0)
319                                    {
320                                            error("invalid geometry\n");
321                                            return 1;
322                                    }
323    
324                                    if (*p == 'x')
325                                            g_height = strtol(p + 1, NULL, 10);
326    
327                                    if (g_height <= 0)
328                                    {
329                                            error("invalid geometry\n");
330                                            return 1;
331                                    }
332    
333                                    if (*p == '%')
334                                            g_width = -g_width;
335    
336                                    break;
337    
338                            case 'f':
339                                    g_fullscreen = True;
340                                  break;                                  break;
341    
342                          case 'b':                          case 'b':
343                                  orders = False;                                  g_orders = False;
344                                    break;
345    
346                            case 'e':
347                                    g_encryption = False;
348                                    break;
349                            case 'E':
350                                    packet_encryption = False;
351                                    break;
352                            case 'm':
353                                    g_sendmotion = False;
354                                    break;
355    
356                            case 'C':
357                                    g_owncolmap = True;
358                                    break;
359    
360                            case 'D':
361                                    g_hide_decorations = True;
362                                    break;
363    
364                            case 'K':
365                                    g_grab_keyboard = False;
366                                    break;
367    
368                            case 'S':
369                                    if (!strcmp(optarg, "standard"))
370                                    {
371                                            g_win_button_size = 18;
372                                            break;
373                                    }
374    
375                                    g_win_button_size = strtol(optarg, &p, 10);
376    
377                                    if (*p)
378                                    {
379                                            error("invalid button size\n");
380                                            return 1;
381                                    }
382    
383                                    break;
384    
385                            case 'T':
386                                    STRNCPY(g_title, optarg, sizeof(g_title));
387                                  break;                                  break;
388    
389                          case 'l':                          case 'N':
390                                  licence = False;                                  g_numlock_sync = True;
391                                  break;                                  break;
392    
393                            case 'a':
394                                    g_server_bpp = strtol(optarg, NULL, 10);
395                                    if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
396                                        && g_server_bpp != 24)
397                                    {
398                                            error("invalid server bpp\n");
399                                            return 1;
400                                    }
401                                    break;
402    
403                            case 'r':
404    
405                                    if (strncmp("sound", optarg, 5) == 0)
406                                    {
407    #ifdef WITH_RDPSND
408                                            g_rdpsnd = True;
409    #else
410                                            warning("Not compiled with sound support");
411    #endif
412                                    }
413                                    else if (strncmp("disk", optarg, 4) == 0)
414                                    {
415                                            /* -r disk:h:=/mnt/floppy */
416                                            disk_enum_devices(&g_num_devices, optarg + 4);
417                                    }
418                                    else if (strncmp("comport", optarg, 7) == 0)
419                                    {
420                                            serial_enum_devices(&g_num_devices, optarg + 7);
421                                    }
422                                    else if (strncmp("lptport", optarg, 7) == 0)
423                                    {
424                                            parallel_enum_devices(&g_num_devices, optarg + 7);
425                                    }
426                                    else if (strncmp("printer", optarg, 7) == 0)
427                                    {
428                                            printer_enum_devices(&g_num_devices, optarg + 7);
429                                    }
430                                    else
431                                    {
432                                            warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n");
433                                    }
434                                    break;
435    
436                            case '0':
437                                    g_console_session = True;
438                                    break;
439    
440                            case '4':
441                                    g_use_rdp5 = False;
442                                    break;
443    
444                            case '5':
445                                    g_use_rdp5 = True;
446                                    break;
447    
448                            case 'h':
449                          case '?':                          case '?':
450                          default:                          default:
451                                  usage(argv[0]);                                  usage(argv[0]);
# Line 111  int main(int argc, char *argv[]) Line 459  int main(int argc, char *argv[])
459                  return 1;                  return 1;
460          }          }
461    
462          server = argv[optind];          STRNCPY(server, argv[optind], sizeof(server));
463            parse_server_and_port(server);
464    
465          if (username[0] == 0)          if (!username_option)
466          {          {
467                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
468                  if ((pw == NULL) || (pw->pw_name == NULL))                  if ((pw == NULL) || (pw->pw_name == NULL))
469                  {                  {
470                          STATUS("Could not determine user name.\n");                          error("could not determine username, use -u\n");
471                          return 1;                          return 1;
472                  }                  }
473    
474                  strncpy(username, pw->pw_name, sizeof(username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
475          }          }
476    
477          if (hostname[0] == 0)          if (hostname[0] == 0)
478          {          {
479                  if (gethostname(hostname, sizeof(hostname)) == -1)                  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
480                  {                  {
481                          STATUS("Could not determine host name.\n");                          error("could not determine local hostname, use -n\n");
482                          return 1;                          return 1;
483                  }                  }
484    
485                    p = strchr(fullhostname, '.');
486                    if (p != NULL)
487                            *p = 0;
488    
489                    STRNCPY(hostname, fullhostname, sizeof(hostname));
490            }
491    
492            if (prompt_password && read_password(password, sizeof(password)))
493                    flags |= RDP_LOGON_AUTO;
494    
495            if (g_title[0] == 0)
496            {
497                    strcpy(g_title, "rdesktop - ");
498                    strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
499          }          }
500    
501          if (!rdp_connect(server))  #ifdef RDP2VNC
502            rdp2vnc_connect(server, flags, domain, password, shell, directory);
503            return 0;
504    #else
505    
506            if (!ui_init())
507                    return 1;
508    
509    #ifdef WITH_RDPSND
510            if (g_rdpsnd)
511                    rdpsnd_init();
512    #endif
513            rdpdr_init();
514    
515            if (!rdp_connect(server, flags, domain, password, shell, directory))
516                  return 1;                  return 1;
517    
518          STATUS("Connection successful.\n");          /* By setting encryption to False here, we have an encrypted login
519               packet but unencrypted transfer of other packets */
520            if (!packet_encryption)
521                    g_encryption = False;
522    
523    
524          strcpy(title, "rdesktop - ");          DEBUG(("Connection successful.\n"));
525          strncat(title, server, sizeof(title));          memset(password, 0, sizeof(password));
526    
527          if (ui_create_window(title))          if (ui_create_window())
528          {          {
529                  rdp_main_loop();                  rdp_retval = rdp_main_loop();
530                  ui_destroy_window();                  ui_destroy_window();
531          }          }
532    
533            DEBUG(("Disconnecting...\n"));
534          rdp_disconnect();          rdp_disconnect();
535          return 0;          ui_deinit();
536    
537            if (True == rdp_retval)
538                    return 0;
539            else
540                    return 2;
541    
542    #endif
543    
544  }  }
545    
546  /* Generate a 32-byte random for the secure transport code. */  #ifdef EGD_SOCKET
547  void generate_random(uint8 *random)  /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
548    static BOOL
549    generate_random_egd(uint8 * buf)
550  {  {
551          struct stat st;          struct sockaddr_un addr;
552          uint32 *r = (uint32 *)random;          BOOL ret = False;
553          int fd;          int fd;
554    
555          /* If we have a kernel random device, use it. */          fd = socket(AF_UNIX, SOCK_STREAM, 0);
556          if ((fd = open("/dev/urandom", O_RDONLY)) != -1)          if (fd == -1)
557                    return False;
558    
559            addr.sun_family = AF_UNIX;
560            memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
561            if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
562                    goto err;
563    
564            /* PRNGD and EGD use a simple communications protocol */
565            buf[0] = 1;             /* Non-blocking (similar to /dev/urandom) */
566            buf[1] = 32;            /* Number of requested random bytes */
567            if (write(fd, buf, 2) != 2)
568                    goto err;
569    
570            if ((read(fd, buf, 1) != 1) || (buf[0] == 0))   /* Available? */
571                    goto err;
572    
573            if (read(fd, buf, 32) != 32)
574                    goto err;
575    
576            ret = True;
577    
578          err:
579            close(fd);
580            return ret;
581    }
582    #endif
583    
584    /* Generate a 32-byte random for the secure transport code. */
585    void
586    generate_random(uint8 * random)
587    {
588            struct stat st;
589            struct tms tmsbuf;
590            MD5_CTX md5;
591            uint32 *r;
592            int fd, n;
593    
594            /* If we have a kernel random device, try that first */
595            if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
596                || ((fd = open("/dev/random", O_RDONLY)) != -1))
597          {          {
598                  read(fd, random, 32);                  n = read(fd, random, 32);
599                  close(fd);                  close(fd);
600                  return;                  if (n == 32)
601                            return;
602          }          }
603    
604    #ifdef EGD_SOCKET
605            /* As a second preference use an EGD */
606            if (generate_random_egd(random))
607                    return;
608    #endif
609    
610          /* Otherwise use whatever entropy we can gather - ideas welcome. */          /* Otherwise use whatever entropy we can gather - ideas welcome. */
611            r = (uint32 *) random;
612          r[0] = (getpid()) | (getppid() << 16);          r[0] = (getpid()) | (getppid() << 16);
613          r[1] = (getuid()) | (getgid() << 16);          r[1] = (getuid()) | (getgid() << 16);
614          r[2] = times(NULL); /* system uptime (clocks) */          r[2] = times(&tmsbuf);  /* system uptime (clocks) */
615          gettimeofday((struct timeval *)&r[3], NULL); /* sec and usec */          gettimeofday((struct timeval *) &r[3], NULL);   /* sec and usec */
616          stat("/tmp", &st);          stat("/tmp", &st);
617          r[5] = st.st_atime;          r[5] = st.st_atime;
618          r[6] = st.st_mtime;          r[6] = st.st_mtime;
619          r[7] = st.st_ctime;          r[7] = st.st_ctime;
620    
621            /* Hash both halves with MD5 to obscure possible patterns */
622            MD5_Init(&md5);
623            MD5_Update(&md5, random, 16);
624            MD5_Final(random, &md5);
625            MD5_Update(&md5, random + 16, 16);
626            MD5_Final(random + 16, &md5);
627  }  }
628    
629  /* malloc; exit if out of memory */  /* malloc; exit if out of memory */
630  void *xmalloc(int size)  void *
631    xmalloc(int size)
632  {  {
633          void *mem = malloc(size);          void *mem = malloc(size);
634          if (mem == NULL)          if (mem == NULL)
635          {          {
636                  ERROR("xmalloc %d\n", size);                  error("xmalloc %d\n", size);
637                  exit(1);                  exit(1);
638          }          }
639          return mem;          return mem;
640  }  }
641    
642  /* realloc; exit if out of memory */  /* realloc; exit if out of memory */
643  void *xrealloc(void *oldmem, int size)  void *
644    xrealloc(void *oldmem, int size)
645  {  {
646          void *mem = realloc(oldmem, size);          void *mem = realloc(oldmem, size);
647          if (mem == NULL)          if (mem == NULL)
648          {          {
649                  ERROR("xrealloc %d\n", size);                  error("xrealloc %d\n", size);
650                  exit(1);                  exit(1);
651          }          }
652          return mem;          return mem;
653  }  }
654    
655  /* free */  /* free */
656  void xfree(void *mem)  void
657    xfree(void *mem)
658  {  {
659          free(mem);          free(mem);
660  }  }
661    
662  /* Produce a hex dump */  /* report an error */
663  void hexdump(unsigned char *p, unsigned int len)  void
664    error(char *format, ...)
665    {
666            va_list ap;
667    
668            fprintf(stderr, "ERROR: ");
669    
670            va_start(ap, format);
671            vfprintf(stderr, format, ap);
672            va_end(ap);
673    }
674    
675    /* report a warning */
676    void
677    warning(char *format, ...)
678    {
679            va_list ap;
680    
681            fprintf(stderr, "WARNING: ");
682    
683            va_start(ap, format);
684            vfprintf(stderr, format, ap);
685            va_end(ap);
686    }
687    
688    /* report an unimplemented protocol feature */
689    void
690    unimpl(char *format, ...)
691    {
692            va_list ap;
693    
694            fprintf(stderr, "NOT IMPLEMENTED: ");
695    
696            va_start(ap, format);
697            vfprintf(stderr, format, ap);
698            va_end(ap);
699    }
700    
701    /* produce a hex dump */
702    void
703    hexdump(unsigned char *p, unsigned int len)
704  {  {
705          unsigned char *line = p;          unsigned char *line = p;
706          unsigned int thisline, offset = 0;          int i, thisline, offset = 0;
         int i;  
707    
708          while (offset < len)          while (offset < len)
709          {          {
710                  STATUS("%04x ", offset);                  printf("%04x ", offset);
711                  thisline = len - offset;                  thisline = len - offset;
712                  if (thisline > 16)                  if (thisline > 16)
713                          thisline = 16;                          thisline = 16;
714    
715                  for (i = 0; i < thisline; i++)                  for (i = 0; i < thisline; i++)
716                          STATUS("%02x ", line[i])                          printf("%02x ", line[i]);
717    
718                  for (; i < 16; i++)                  for (; i < 16; i++)
719                          STATUS("   ");                          printf("   ");
720    
721                  for (i = 0; i < thisline; i++)                  for (i = 0; i < thisline; i++)
722                          STATUS("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');                          printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
723    
724                  STATUS("\n");                  printf("\n");
725                  offset += thisline;                  offset += thisline;
726                  line += thisline;                  line += thisline;
727          }          }
728  }  }
729    
730    /*
731      input: src is the string we look in for needle.
732             Needle may be escaped by a backslash, in
733             that case we ignore that particular needle.
734      return value: returns next src pointer, for
735            succesive executions, like in a while loop
736            if retval is 0, then there are no more args.
737      pitfalls:
738            src is modified. 0x00 chars are inserted to
739            terminate strings.
740            return val, points on the next val chr after ins
741            0x00
742    
743            example usage:
744            while( (pos = next_arg( optarg, ',')) ){
745                    printf("%s\n",optarg);
746                    optarg=pos;
747            }
748    
749    */
750    char *
751    next_arg(char *src, char needle)
752    {
753            char *nextval;
754            char *p;
755            char *mvp = 0;
756    
757            /* EOS */
758            if (*src == (char) 0x00)
759                    return 0;
760    
761            p = src;
762            /*  skip escaped needles */
763            while ((nextval = strchr(p, needle)))
764            {
765                    mvp = nextval - 1;
766                    /* found backslashed needle */
767                    if (*mvp == '\\' && (mvp > src))
768                    {
769                            /* move string one to the left */
770                            while (*(mvp + 1) != (char) 0x00)
771                            {
772                                    *mvp = *(mvp + 1);
773                                    *mvp++;
774                            }
775                            *mvp = (char) 0x00;
776                            p = nextval;
777                    }
778                    else
779                    {
780                            p = nextval + 1;
781                            break;
782                    }
783    
784            }
785    
786            /* more args available */
787            if (nextval)
788            {
789                    *nextval = (char) 0x00;
790                    return ++nextval;
791            }
792    
793            /* no more args after this, jump to EOS */
794            nextval = src + strlen(src);
795            return nextval;
796    }
797    
798    
799    void
800    toupper_str(char *p)
801    {
802            while (*p)
803            {
804                    if ((*p >= 'a') && (*p <= 'z'))
805                            *p = toupper((int) *p);
806                    p++;
807            }
808    }
809    
810    
811    /* not all clibs got ltoa */
812    #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
813    
814    char *
815    l_to_a(long N, int base)
816    {
817            static char ret[LTOA_BUFSIZE];
818    
819            char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf);
820    
821            register int divrem;
822    
823            if (base < 36 || 2 > base)
824                    base = 10;
825    
826            if (N < 0)
827            {
828                    *head++ = '-';
829                    N = -N;
830            }
831    
832            tail = buf + sizeof(buf);
833            *--tail = 0;
834    
835            do
836            {
837                    divrem = N % base;
838                    *--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10;
839                    N /= base;
840            }
841            while (N);
842    
843            strcpy(head, tail);
844            return ret;
845    }
846    
847    
848    int
849    load_licence(unsigned char **data)
850    {
851            char *home, *path;
852            struct stat st;
853            int fd, length;
854    
855            home = getenv("HOME");
856            if (home == NULL)
857                    return -1;
858    
859            path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
860            sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
861    
862            fd = open(path, O_RDONLY);
863            if (fd == -1)
864                    return -1;
865    
866            if (fstat(fd, &st))
867                    return -1;
868    
869            *data = (uint8 *) xmalloc(st.st_size);
870            length = read(fd, *data, st.st_size);
871            close(fd);
872            xfree(path);
873            return length;
874    }
875    
876    void
877    save_licence(unsigned char *data, int length)
878    {
879            char *home, *path, *tmppath;
880            int fd;
881    
882            home = getenv("HOME");
883            if (home == NULL)
884                    return;
885    
886            path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
887    
888            sprintf(path, "%s/.rdesktop", home);
889            if ((mkdir(path, 0700) == -1) && errno != EEXIST)
890            {
891                    perror(path);
892                    return;
893            }
894    
895            /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
896    
897            sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
898            tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
899            strcpy(tmppath, path);
900            strcat(tmppath, ".new");
901    
902            fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
903            if (fd == -1)
904            {
905                    perror(tmppath);
906                    return;
907            }
908    
909            if (write(fd, data, length) != length)
910            {
911                    perror(tmppath);
912                    unlink(tmppath);
913            }
914            else if (rename(tmppath, path) == -1)
915            {
916                    perror(path);
917                    unlink(tmppath);
918            }
919    
920            close(fd);
921            xfree(tmppath);
922            xfree(path);
923    }

Legend:
Removed from v.17  
changed lines
  Added in v.603

  ViewVC Help
Powered by ViewVC 1.1.26