]> git.sesse.net Git - rdpsrv/blob - util.c
Handle MCS_CONNECT_INITIAL.
[rdpsrv] / util.c
1 /* -*- c-basic-offset: 8 -*-
2    rdesktop: A Remote Desktop Protocol client.
3    Entrypoint and utility functions
4    Copyright (C) Matthew Chapman 1999-2004
5
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.
10
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.
15
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.
19 */
20
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 */
29 #include <errno.h>
30 #include <X11/Xlib.h>           /* Window */
31 #include "rdesktop.h"
32
33 #ifdef EGD_SOCKET
34 #include <sys/socket.h>         /* socket connect */
35 #include <sys/un.h>             /* sockaddr_un */
36 #endif
37
38 #include <openssl/md5.h>
39
40 #ifdef EGD_SOCKET
41 /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
42 static BOOL
43 generate_random_egd(uint8 * buf)
44 {
45         struct sockaddr_un addr;
46         BOOL ret = False;
47         int fd;
48
49         fd = socket(AF_UNIX, SOCK_STREAM, 0);
50         if (fd == -1)
51                 return False;
52
53         addr.sun_family = AF_UNIX;
54         memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
55         if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
56                 goto err;
57
58         /* PRNGD and EGD use a simple communications protocol */
59         buf[0] = 1;             /* Non-blocking (similar to /dev/urandom) */
60         buf[1] = 32;            /* Number of requested random bytes */
61         if (write(fd, buf, 2) != 2)
62                 goto err;
63
64         if ((read(fd, buf, 1) != 1) || (buf[0] == 0))   /* Available? */
65                 goto err;
66
67         if (read(fd, buf, 32) != 32)
68                 goto err;
69
70         ret = True;
71
72       err:
73         close(fd);
74         return ret;
75 }
76 #endif
77
78 /* Generate a 32-byte random for the secure transport code. */
79 void
80 generate_random(uint8 * random)
81 {
82         struct stat st;
83         struct tms tmsbuf;
84         MD5_CTX md5;
85         uint32 *r;
86         int fd, n;
87
88         /* If we have a kernel random device, try that first */
89         if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
90                         || ((fd = open("/dev/random", O_RDONLY)) != -1))
91         {
92                 n = read(fd, random, 32);
93                 close(fd);
94                 if (n == 32)
95                         return;
96         }
97
98 #ifdef EGD_SOCKET
99         /* As a second preference use an EGD */
100         if (generate_random_egd(random))
101                 return;
102 #endif
103
104         /* Otherwise use whatever entropy we can gather - ideas welcome. */
105         r = (uint32 *) random;
106         r[0] = (getpid()) | (getppid() << 16);
107         r[1] = (getuid()) | (getgid() << 16);
108         r[2] = times(&tmsbuf);  /* system uptime (clocks) */
109         gettimeofday((struct timeval *) &r[3], NULL);   /* sec and usec */
110         stat("/tmp", &st);
111         r[5] = st.st_atime;
112         r[6] = st.st_mtime;
113         r[7] = st.st_ctime;
114
115         /* Hash both halves with MD5 to obscure possible patterns */
116         MD5_Init(&md5);
117         MD5_Update(&md5, random, 16);
118         MD5_Final(random, &md5);
119         MD5_Update(&md5, random + 16, 16);
120         MD5_Final(random + 16, &md5);
121 }
122
123 /* malloc; exit if out of memory */
124 void *
125 xmalloc(int size)
126 {
127         void *mem = malloc(size);
128         if (mem == NULL)
129         {
130                 error("xmalloc %d\n", size);
131                 exit(1);
132         }
133         return mem;
134 }
135
136 /* realloc; exit if out of memory */
137 void *
138 xrealloc(void *oldmem, int size)
139 {
140         void *mem = realloc(oldmem, size);
141         if (mem == NULL)
142         {
143                 error("xrealloc %d\n", size);
144                 exit(1);
145         }
146         return mem;
147 }
148
149 /* free */
150 void
151 xfree(void *mem)
152 {
153         free(mem);
154 }
155
156 /* report an error */
157 void
158 error(char *format, ...)
159 {
160         va_list ap;
161
162         fprintf(stderr, "ERROR: ");
163
164         va_start(ap, format);
165         vfprintf(stderr, format, ap);
166         va_end(ap);
167
168         exit(1);
169 }
170
171 /* report a warning */
172 void
173 warning(char *format, ...)
174 {
175         va_list ap;
176
177         fprintf(stderr, "WARNING: ");
178
179         va_start(ap, format);
180         vfprintf(stderr, format, ap);
181         va_end(ap);
182 }
183
184 /* report an unimplemented protocol feature */
185 void
186 unimpl(char *format, ...)
187 {
188         va_list ap;
189
190         fprintf(stderr, "NOT IMPLEMENTED: ");
191
192         va_start(ap, format);
193         vfprintf(stderr, format, ap);
194         va_end(ap);
195 }
196
197 /* produce a hex dump */
198 void
199 hexdump(unsigned char *p, int len)
200 {
201         unsigned char *line = p;
202         int i, thisline, offset = 0;
203
204         while (offset < len)
205         {
206                 printf("%04x ", offset);
207                 thisline = len - offset;
208                 if (thisline > 16)
209                         thisline = 16;
210
211                 for (i = 0; i < thisline; i++)
212                         printf("%02x ", line[i]);
213
214                 for (; i < 16; i++)
215                         printf("   ");
216
217                 for (i = 0; i < thisline; i++)
218                         printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
219
220                 printf("\n");
221                 offset += thisline;
222                 line += thisline;
223         }
224 }
225