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