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