f54e8a4d6e0d2ebea272a6eeea7723e39bc28f33
[sgt/putty] / windows / winmisc.c
1 /*
2 * winmisc.c: miscellaneous Windows-specific things
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "putty.h"
8 #include <security.h>
9
10 OSVERSIONINFO osVersion;
11
12 char *platform_get_x_display(void) {
13 /* We may as well check for DISPLAY in case it's useful. */
14 return dupstr(getenv("DISPLAY"));
15 }
16
17 Filename *filename_from_str(const char *str)
18 {
19 Filename *ret = snew(Filename);
20 ret->path = dupstr(str);
21 return ret;
22 }
23
24 Filename *filename_copy(const Filename *fn)
25 {
26 return filename_from_str(fn->path);
27 }
28
29 const char *filename_to_str(const Filename *fn)
30 {
31 return fn->path;
32 }
33
34 int filename_equal(const Filename *f1, const Filename *f2)
35 {
36 return !strcmp(f1->path, f2->path);
37 }
38
39 int filename_is_null(const Filename *fn)
40 {
41 return !*fn->path;
42 }
43
44 void filename_free(Filename *fn)
45 {
46 sfree(fn->path);
47 sfree(fn);
48 }
49
50 int filename_serialise(const Filename *f, void *vdata)
51 {
52 char *data = (char *)vdata;
53 int len = strlen(f->path) + 1; /* include trailing NUL */
54 if (data) {
55 strcpy(data, f->path);
56 }
57 return len;
58 }
59 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
60 {
61 char *data = (char *)vdata;
62 char *end;
63 end = memchr(data, '\0', maxsize);
64 if (!end)
65 return NULL;
66 end++;
67 *used = end - data;
68 return filename_from_str(data);
69 }
70
71 char *get_username(void)
72 {
73 DWORD namelen;
74 char *user;
75 int got_username = FALSE;
76 DECL_WINDOWS_FUNCTION(static, BOOLEAN, GetUserNameExA,
77 (EXTENDED_NAME_FORMAT, LPSTR, PULONG));
78
79 {
80 static int tried_usernameex = FALSE;
81 if (!tried_usernameex) {
82 /* Not available on Win9x, so load dynamically */
83 HMODULE secur32 = load_system32_dll("secur32.dll");
84 GET_WINDOWS_FUNCTION(secur32, GetUserNameExA);
85 tried_usernameex = TRUE;
86 }
87 }
88
89 if (p_GetUserNameExA) {
90 /*
91 * If available, use the principal -- this avoids the problem
92 * that the local username is case-insensitive but Kerberos
93 * usernames are case-sensitive.
94 */
95
96 /* Get the length */
97 namelen = 0;
98 (void) p_GetUserNameExA(NameUserPrincipal, NULL, &namelen);
99
100 user = snewn(namelen, char);
101 got_username = p_GetUserNameExA(NameUserPrincipal, user, &namelen);
102 if (got_username) {
103 char *p = strchr(user, '@');
104 if (p) *p = 0;
105 } else {
106 sfree(user);
107 }
108 }
109
110 if (!got_username) {
111 /* Fall back to local user name */
112 namelen = 0;
113 if (GetUserName(NULL, &namelen) == FALSE) {
114 /*
115 * Apparently this doesn't work at least on Windows XP SP2.
116 * Thus assume a maximum of 256. It will fail again if it
117 * doesn't fit.
118 */
119 namelen = 256;
120 }
121
122 user = snewn(namelen, char);
123 got_username = GetUserName(user, &namelen);
124 if (!got_username) {
125 sfree(user);
126 }
127 }
128
129 return got_username ? user : NULL;
130 }
131
132 BOOL init_winver(void)
133 {
134 ZeroMemory(&osVersion, sizeof(osVersion));
135 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
136 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
137 }
138
139 HMODULE load_system32_dll(const char *libname)
140 {
141 /*
142 * Wrapper function to load a DLL out of c:\windows\system32
143 * without going through the full DLL search path. (Hence no
144 * attack is possible by placing a substitute DLL earlier on that
145 * path.)
146 */
147 static char *sysdir = NULL;
148 char *fullpath;
149 HMODULE ret;
150
151 if (!sysdir) {
152 int size = 0, len;
153 do {
154 size = 3*size/2 + 512;
155 sysdir = sresize(sysdir, size, char);
156 len = GetSystemDirectory(sysdir, size);
157 } while (len >= size);
158 }
159
160 fullpath = dupcat(sysdir, "\\", libname, NULL);
161 ret = LoadLibrary(fullpath);
162 sfree(fullpath);
163 return ret;
164 }
165
166 #ifdef DEBUG
167 static FILE *debug_fp = NULL;
168 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
169 static int debug_got_console = 0;
170
171 void dputs(char *buf)
172 {
173 DWORD dw;
174
175 if (!debug_got_console) {
176 if (AllocConsole()) {
177 debug_got_console = 1;
178 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
179 }
180 }
181 if (!debug_fp) {
182 debug_fp = fopen("debug.log", "w");
183 }
184
185 if (debug_hdl != INVALID_HANDLE_VALUE) {
186 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
187 }
188 fputs(buf, debug_fp);
189 fflush(debug_fp);
190 }
191 #endif
192
193 #ifdef MINEFIELD
194 /*
195 * Minefield - a Windows equivalent for Electric Fence
196 */
197
198 #define PAGESIZE 4096
199
200 /*
201 * Design:
202 *
203 * We start by reserving as much virtual address space as Windows
204 * will sensibly (or not sensibly) let us have. We flag it all as
205 * invalid memory.
206 *
207 * Any allocation attempt is satisfied by committing one or more
208 * pages, with an uncommitted page on either side. The returned
209 * memory region is jammed up against the _end_ of the pages.
210 *
211 * Freeing anything causes instantaneous decommitment of the pages
212 * involved, so stale pointers are caught as soon as possible.
213 */
214
215 static int minefield_initialised = 0;
216 static void *minefield_region = NULL;
217 static long minefield_size = 0;
218 static long minefield_npages = 0;
219 static long minefield_curpos = 0;
220 static unsigned short *minefield_admin = NULL;
221 static void *minefield_pages = NULL;
222
223 static void minefield_admin_hide(int hide)
224 {
225 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
226 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
227 }
228
229 static void minefield_init(void)
230 {
231 int size;
232 int admin_size;
233 int i;
234
235 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
236 minefield_region = VirtualAlloc(NULL, size,
237 MEM_RESERVE, PAGE_NOACCESS);
238 if (minefield_region)
239 break;
240 }
241 minefield_size = size;
242
243 /*
244 * Firstly, allocate a section of that to be the admin block.
245 * We'll need a two-byte field for each page.
246 */
247 minefield_admin = minefield_region;
248 minefield_npages = minefield_size / PAGESIZE;
249 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
250 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
251 minefield_pages = (char *) minefield_region + admin_size;
252
253 /*
254 * Commit the admin region.
255 */
256 VirtualAlloc(minefield_admin, minefield_npages * 2,
257 MEM_COMMIT, PAGE_READWRITE);
258
259 /*
260 * Mark all pages as unused (0xFFFF).
261 */
262 for (i = 0; i < minefield_npages; i++)
263 minefield_admin[i] = 0xFFFF;
264
265 /*
266 * Hide the admin region.
267 */
268 minefield_admin_hide(1);
269
270 minefield_initialised = 1;
271 }
272
273 static void minefield_bomb(void)
274 {
275 div(1, *(int *) minefield_pages);
276 }
277
278 static void *minefield_alloc(int size)
279 {
280 int npages;
281 int pos, lim, region_end, region_start;
282 int start;
283 int i;
284
285 npages = (size + PAGESIZE - 1) / PAGESIZE;
286
287 minefield_admin_hide(0);
288
289 /*
290 * Search from current position until we find a contiguous
291 * bunch of npages+2 unused pages.
292 */
293 pos = minefield_curpos;
294 lim = minefield_npages;
295 while (1) {
296 /* Skip over used pages. */
297 while (pos < lim && minefield_admin[pos] != 0xFFFF)
298 pos++;
299 /* Count unused pages. */
300 start = pos;
301 while (pos < lim && pos - start < npages + 2 &&
302 minefield_admin[pos] == 0xFFFF)
303 pos++;
304 if (pos - start == npages + 2)
305 break;
306 /* If we've reached the limit, reset the limit or stop. */
307 if (pos >= lim) {
308 if (lim == minefield_npages) {
309 /* go round and start again at zero */
310 lim = minefield_curpos;
311 pos = 0;
312 } else {
313 minefield_admin_hide(1);
314 return NULL;
315 }
316 }
317 }
318
319 minefield_curpos = pos - 1;
320
321 /*
322 * We have npages+2 unused pages starting at start. We leave
323 * the first and last of these alone and use the rest.
324 */
325 region_end = (start + npages + 1) * PAGESIZE;
326 region_start = region_end - size;
327 /* FIXME: could align here if we wanted */
328
329 /*
330 * Update the admin region.
331 */
332 for (i = start + 2; i < start + npages + 1; i++)
333 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
334 minefield_admin[start + 1] = region_start % PAGESIZE;
335
336 minefield_admin_hide(1);
337
338 VirtualAlloc((char *) minefield_pages + region_start, size,
339 MEM_COMMIT, PAGE_READWRITE);
340 return (char *) minefield_pages + region_start;
341 }
342
343 static void minefield_free(void *ptr)
344 {
345 int region_start, i, j;
346
347 minefield_admin_hide(0);
348
349 region_start = (char *) ptr - (char *) minefield_pages;
350 i = region_start / PAGESIZE;
351 if (i < 0 || i >= minefield_npages ||
352 minefield_admin[i] != region_start % PAGESIZE)
353 minefield_bomb();
354 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
355 minefield_admin[j] = 0xFFFF;
356 }
357
358 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
359
360 minefield_admin_hide(1);
361 }
362
363 static int minefield_get_size(void *ptr)
364 {
365 int region_start, i, j;
366
367 minefield_admin_hide(0);
368
369 region_start = (char *) ptr - (char *) minefield_pages;
370 i = region_start / PAGESIZE;
371 if (i < 0 || i >= minefield_npages ||
372 minefield_admin[i] != region_start % PAGESIZE)
373 minefield_bomb();
374 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
375
376 minefield_admin_hide(1);
377
378 return j * PAGESIZE - region_start;
379 }
380
381 void *minefield_c_malloc(size_t size)
382 {
383 if (!minefield_initialised)
384 minefield_init();
385 return minefield_alloc(size);
386 }
387
388 void minefield_c_free(void *p)
389 {
390 if (!minefield_initialised)
391 minefield_init();
392 minefield_free(p);
393 }
394
395 /*
396 * realloc _always_ moves the chunk, for rapid detection of code
397 * that assumes it won't.
398 */
399 void *minefield_c_realloc(void *p, size_t size)
400 {
401 size_t oldsize;
402 void *q;
403 if (!minefield_initialised)
404 minefield_init();
405 q = minefield_alloc(size);
406 oldsize = minefield_get_size(p);
407 memcpy(q, p, (oldsize < size ? oldsize : size));
408 minefield_free(p);
409 return q;
410 }
411
412 #endif /* MINEFIELD */
413
414 FontSpec *fontspec_new(const char *name,
415 int bold, int height, int charset)
416 {
417 FontSpec *f = snew(FontSpec);
418 f->name = dupstr(name);
419 f->isbold = bold;
420 f->height = height;
421 f->charset = charset;
422 return f;
423 }
424 FontSpec *fontspec_copy(const FontSpec *f)
425 {
426 return fontspec_new(f->name, f->isbold, f->height, f->charset);
427 }
428 void fontspec_free(FontSpec *f)
429 {
430 sfree(f->name);
431 sfree(f);
432 }
433 int fontspec_serialise(FontSpec *f, void *vdata)
434 {
435 char *data = (char *)vdata;
436 int len = strlen(f->name) + 1; /* include trailing NUL */
437 if (data) {
438 strcpy(data, f->name);
439 PUT_32BIT_MSB_FIRST(data + len, f->isbold);
440 PUT_32BIT_MSB_FIRST(data + len + 4, f->height);
441 PUT_32BIT_MSB_FIRST(data + len + 8, f->charset);
442 }
443 return len + 12; /* also include three 4-byte ints */
444 }
445 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
446 {
447 char *data = (char *)vdata;
448 char *end;
449 if (maxsize < 13)
450 return NULL;
451 end = memchr(data, '\0', maxsize-12);
452 if (!end)
453 return NULL;
454 end++;
455 *used = end - data + 12;
456 return fontspec_new(data,
457 GET_32BIT_MSB_FIRST(end),
458 GET_32BIT_MSB_FIRST(end + 4),
459 GET_32BIT_MSB_FIRST(end + 8));
460 }