1 /* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Entrypoint and utility functions
4 Copyright (C) Matthew Chapman 1999-2004
6 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
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <stdarg.h> /* va_list va_start va_end */
22 #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
23 #include <fcntl.h> /* open */
24 #include <pwd.h> /* getpwuid */
25 #include <termios.h> /* tcgetattr tcsetattr */
26 #include <sys/stat.h> /* stat */
27 #include <sys/time.h> /* gettimeofday */
28 #include <sys/times.h> /* times */
33 #include <sys/socket.h> /* socket connect */
34 #include <sys/un.h> /* sockaddr_un */
37 #include <openssl/md5.h>
40 /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
42 generate_random_egd(uint8 * buf)
44 struct sockaddr_un addr;
48 fd = socket(AF_UNIX, SOCK_STREAM, 0);
52 addr.sun_family = AF_UNIX;
53 memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
54 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
57 /* PRNGD and EGD use a simple communications protocol */
58 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
59 buf[1] = 32; /* Number of requested random bytes */
60 if (write(fd, buf, 2) != 2)
63 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
66 if (read(fd, buf, 32) != 32)
77 /* Generate a 32-byte random for the secure transport code. */
79 generate_random(uint8 * random)
87 /* If we have a kernel random device, try that first */
88 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
89 || ((fd = open("/dev/random", O_RDONLY)) != -1))
91 n = read(fd, random, 32);
98 /* As a second preference use an EGD */
99 if (generate_random_egd(random))
103 /* Otherwise use whatever entropy we can gather - ideas welcome. */
104 r = (uint32 *) random;
105 r[0] = (getpid()) | (getppid() << 16);
106 r[1] = (getuid()) | (getgid() << 16);
107 r[2] = times(&tmsbuf); /* system uptime (clocks) */
108 gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
114 /* Hash both halves with MD5 to obscure possible patterns */
116 MD5_Update(&md5, random, 16);
117 MD5_Final(random, &md5);
118 MD5_Update(&md5, random + 16, 16);
119 MD5_Final(random + 16, &md5);
122 /* malloc; exit if out of memory */
126 void *mem = malloc(size);
129 error("xmalloc %d\n", size);
135 /* realloc; exit if out of memory */
137 xrealloc(void *oldmem, int size)
139 void *mem = realloc(oldmem, size);
142 error("xrealloc %d\n", size);
155 /* report an error */
157 error(char *format, ...)
161 fprintf(stderr, "ERROR: ");
163 va_start(ap, format);
164 vfprintf(stderr, format, ap);
170 /* report a warning */
172 warning(char *format, ...)
176 fprintf(stderr, "WARNING: ");
178 va_start(ap, format);
179 vfprintf(stderr, format, ap);
183 /* report an unimplemented protocol feature */
185 unimpl(char *format, ...)
189 fprintf(stderr, "NOT IMPLEMENTED: ");
191 va_start(ap, format);
192 vfprintf(stderr, format, ap);
196 /* produce a hex dump */
198 hexdump(unsigned char *p, int len)
200 unsigned char *line = p;
201 int i, thisline, offset = 0;
205 printf("%04x ", offset);
206 thisline = len - offset;
210 for (i = 0; i < thisline; i++)
211 printf("%02x ", line[i]);
216 for (i = 0; i < thisline; i++)
217 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');