]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winstuff.h
Lock down the search path for Windows DLL loading.
[PuTTY.git] / windows / winstuff.h
1 /*
2  * winstuff.h: Windows-specific inter-module stuff.
3  */
4
5 #ifndef PUTTY_WINSTUFF_H
6 #define PUTTY_WINSTUFF_H
7
8 #ifndef AUTO_WINSOCK
9 #include <winsock2.h>
10 #endif
11 #include <windows.h>
12 #include <stdio.h>                     /* for FILENAME_MAX */
13
14 /* We use uintptr_t for Win32/Win64 portability, so we should in
15  * principle include stdint.h, which defines it according to the C
16  * standard. But older versions of Visual Studio - including the one
17  * used for official PuTTY builds as of 2015-09-28 - don't provide
18  * stdint.h at all, but do (non-standardly) define uintptr_t in
19  * stddef.h. So here we try to make sure _some_ standard header is
20  * included which defines uintptr_t. */
21 #include <stddef.h>
22 #if !defined _MSC_VER || _MSC_VER >= 1600
23 #include <stdint.h>
24 #endif
25
26 #include "tree234.h"
27
28 #include "winhelp.h"
29
30 struct Filename {
31     char *path;
32 };
33 #define f_open(filename, mode, isprivate) ( fopen((filename)->path, (mode)) )
34
35 struct FontSpec {
36     char *name;
37     int isbold;
38     int height;
39     int charset;
40 };
41 struct FontSpec *fontspec_new(const char *name,
42                                int bold, int height, int charset);
43
44 #ifndef CLEARTYPE_QUALITY
45 #define CLEARTYPE_QUALITY 5
46 #endif
47 #define FONT_QUALITY(fq) ( \
48     (fq) == FQ_DEFAULT ? DEFAULT_QUALITY : \
49     (fq) == FQ_ANTIALIASED ? ANTIALIASED_QUALITY : \
50     (fq) == FQ_NONANTIALIASED ? NONANTIALIASED_QUALITY : \
51     CLEARTYPE_QUALITY)
52
53 #define PLATFORM_IS_UTF16 /* enable UTF-16 processing when exchanging
54                            * wchar_t strings with environment */
55
56 /*
57  * Where we can, we use GetWindowLongPtr and friends because they're
58  * more useful on 64-bit platforms, but they're a relatively recent
59  * innovation, missing from VC++ 6 and older MinGW.  Degrade nicely.
60  * (NB that on some systems, some of these things are available but
61  * not others...)
62  */
63
64 #ifndef GCLP_HCURSOR
65 /* GetClassLongPtr and friends */
66 #undef  GetClassLongPtr
67 #define GetClassLongPtr GetClassLong
68 #undef  SetClassLongPtr
69 #define SetClassLongPtr SetClassLong
70 #define GCLP_HCURSOR GCL_HCURSOR
71 /* GetWindowLongPtr and friends */
72 #undef  GetWindowLongPtr
73 #define GetWindowLongPtr GetWindowLong
74 #undef  SetWindowLongPtr
75 #define SetWindowLongPtr SetWindowLong
76 #undef  GWLP_USERDATA
77 #define GWLP_USERDATA GWL_USERDATA
78 #undef  DWLP_MSGRESULT
79 #define DWLP_MSGRESULT DWL_MSGRESULT
80 /* Since we've clobbered the above functions, we should clobber the
81  * associated type regardless of whether it's defined. */
82 #undef LONG_PTR
83 #define LONG_PTR LONG
84 #endif
85
86 #define BOXFLAGS DLGWINDOWEXTRA
87 #define BOXRESULT (DLGWINDOWEXTRA + sizeof(LONG_PTR))
88 #define DF_END 0x0001
89
90 #ifndef NO_SECUREZEROMEMORY
91 #define PLATFORM_HAS_SMEMCLR /* inhibit cross-platform one in misc.c */
92 #endif
93
94 #define BROKEN_PIPE_ERROR_CODE ERROR_BROKEN_PIPE   /* used in sshshare.c */
95
96 /*
97  * Dynamically linked functions. These come in two flavours:
98  *
99  *  - GET_WINDOWS_FUNCTION does not expose "name" to the preprocessor,
100  *    so will always dynamically link against exactly what is specified
101  *    in "name". If you're not sure, use this one.
102  *
103  *  - GET_WINDOWS_FUNCTION_PP allows "name" to be redirected via
104  *    preprocessor definitions like "#define foo bar"; this is principally
105  *    intended for the ANSI/Unicode DoSomething/DoSomethingA/DoSomethingW.
106  *    If your function has an argument of type "LPTSTR" or similar, this
107  *    is the variant to use.
108  *    (However, it can't always be used, as it trips over more complicated
109  *    macro trickery such as the WspiapiGetAddrInfo wrapper for getaddrinfo.)
110  *
111  * (DECL_WINDOWS_FUNCTION works with both these variants.)
112  */
113 #define DECL_WINDOWS_FUNCTION(linkage, rettype, name, params) \
114     typedef rettype (WINAPI *t_##name) params; \
115     linkage t_##name p_##name
116 #define STR1(x) #x
117 #define STR(x) STR1(x)
118 #define GET_WINDOWS_FUNCTION_PP(module, name) \
119     (p_##name = module ? (t_##name) GetProcAddress(module, STR(name)) : NULL)
120 #define GET_WINDOWS_FUNCTION(module, name) \
121     (p_##name = module ? (t_##name) GetProcAddress(module, #name) : NULL)
122
123 /*
124  * Global variables. Most modules declare these `extern', but
125  * window.c will do `#define PUTTY_DO_GLOBALS' before including this
126  * module, and so will get them properly defined.
127 */
128 #ifndef GLOBAL
129 #ifdef PUTTY_DO_GLOBALS
130 #define GLOBAL
131 #else
132 #define GLOBAL extern
133 #endif
134 #endif
135
136 #ifndef DONE_TYPEDEFS
137 #define DONE_TYPEDEFS
138 typedef struct conf_tag Conf;
139 typedef struct backend_tag Backend;
140 typedef struct terminal_tag Terminal;
141 #endif
142
143 #define PUTTY_REG_POS "Software\\SimonTatham\\PuTTY"
144 #define PUTTY_REG_PARENT "Software\\SimonTatham"
145 #define PUTTY_REG_PARENT_CHILD "PuTTY"
146 #define PUTTY_REG_GPARENT "Software"
147 #define PUTTY_REG_GPARENT_CHILD "SimonTatham"
148
149 /* Result values for the jumplist registry functions. */
150 #define JUMPLISTREG_OK 0
151 #define JUMPLISTREG_ERROR_INVALID_PARAMETER 1
152 #define JUMPLISTREG_ERROR_KEYOPENCREATE_FAILURE 2
153 #define JUMPLISTREG_ERROR_VALUEREAD_FAILURE 3
154 #define JUMPLISTREG_ERROR_VALUEWRITE_FAILURE 4
155 #define JUMPLISTREG_ERROR_INVALID_VALUE 5
156
157 #define PUTTY_HELP_FILE "putty.hlp"
158 #define PUTTY_CHM_FILE "putty.chm"
159 #define PUTTY_HELP_CONTENTS "putty.cnt"
160
161 #define GETTICKCOUNT GetTickCount
162 #define CURSORBLINK GetCaretBlinkTime()
163 #define TICKSPERSEC 1000               /* GetTickCount returns milliseconds */
164
165 #define DEFAULT_CODEPAGE CP_ACP
166 #define USES_VTLINE_HACK
167
168 typedef HDC Context;
169
170 typedef unsigned int uint32; /* int is 32-bits on Win32 and Win64. */
171 #define PUTTY_UINT32_DEFINED
172
173 #ifndef NO_GSSAPI
174 /*
175  * GSS-API stuff
176  */
177 #define GSS_CC CALLBACK
178 /*
179 typedef struct Ssh_gss_buf {
180     size_t length;
181     char *value;
182 } Ssh_gss_buf;
183
184 #define SSH_GSS_EMPTY_BUF (Ssh_gss_buf) {0,NULL}
185 typedef void *Ssh_gss_name;
186 */
187 #endif
188
189 /*
190  * Window handles for the windows that can be running during a
191  * PuTTY session.
192  */
193 GLOBAL HWND hwnd;       /* the main terminal window */
194 GLOBAL HWND logbox;
195
196 /*
197  * The all-important instance handle.
198  */
199 GLOBAL HINSTANCE hinst;
200
201 /*
202  * Help file stuff in winhelp.c.
203  */
204 void init_help(void);
205 void shutdown_help(void);
206 int has_help(void);
207 void launch_help(HWND hwnd, const char *topic);
208 void quit_help(HWND hwnd);
209
210 /*
211  * The terminal and logging context are notionally local to the
212  * Windows front end, but they must be shared between window.c and
213  * windlg.c. Likewise the saved-sessions list.
214  */
215 GLOBAL Terminal *term;
216 GLOBAL void *logctx;
217
218 #define WM_NETEVENT  (WM_APP + 5)
219
220 /*
221  * On Windows, we send MA_2CLK as the only event marking the second
222  * press of a mouse button. Compare unix.h.
223  */
224 #define MULTICLICK_ONLY_EVENT 1
225
226 /*
227  * On Windows, data written to the clipboard must be NUL-terminated.
228  */
229 #define SELECTION_NUL_TERMINATED 1
230
231 /*
232  * On Windows, copying to the clipboard terminates lines with CRLF.
233  */
234 #define SEL_NL { 13, 10 }
235
236 /*
237  * sk_getxdmdata() does not exist under Windows (not that I
238  * couldn't write it if I wanted to, but I haven't bothered), so
239  * it's a macro which always returns NULL. With any luck this will
240  * cause the compiler to notice it can optimise away the
241  * implementation of XDM-AUTHORIZATION-1 in x11fwd.c :-)
242  */
243 #define sk_getxdmdata(socket, lenp) (NULL)
244
245 /*
246  * File-selector filter strings used in the config box. On Windows,
247  * these strings are of exactly the type needed to go in
248  * `lpstrFilter' in an OPENFILENAME structure.
249  */
250 #define FILTER_KEY_FILES ("PuTTY Private Key Files (*.ppk)\0*.ppk\0" \
251                               "All Files (*.*)\0*\0\0\0")
252 #define FILTER_WAVE_FILES ("Wave Files (*.wav)\0*.WAV\0" \
253                                "All Files (*.*)\0*\0\0\0")
254 #define FILTER_DYNLIB_FILES ("Dynamic Library Files (*.dll)\0*.dll\0" \
255                                  "All Files (*.*)\0*\0\0\0")
256
257 /*
258  * Exports from winnet.c.
259  */
260 extern int select_result(WPARAM, LPARAM);
261
262 /*
263  * winnet.c dynamically loads WinSock 2 or WinSock 1 depending on
264  * what it can get, which means any WinSock routines used outside
265  * that module must be exported from it as function pointers. So
266  * here they are.
267  */
268 DECL_WINDOWS_FUNCTION(GLOBAL, int, WSAAsyncSelect,
269                       (SOCKET, HWND, u_int, long));
270 DECL_WINDOWS_FUNCTION(GLOBAL, int, WSAEventSelect,
271                       (SOCKET, WSAEVENT, long));
272 DECL_WINDOWS_FUNCTION(GLOBAL, int, select,
273                       (int, fd_set FAR *, fd_set FAR *,
274                        fd_set FAR *, const struct timeval FAR *));
275 DECL_WINDOWS_FUNCTION(GLOBAL, int, WSAGetLastError, (void));
276 DECL_WINDOWS_FUNCTION(GLOBAL, int, WSAEnumNetworkEvents,
277                       (SOCKET, WSAEVENT, LPWSANETWORKEVENTS));
278
279 extern int socket_writable(SOCKET skt);
280
281 extern void socket_reselect_all(void);
282
283 /*
284  * Exports from winctrls.c.
285  */
286
287 struct ctlpos {
288     HWND hwnd;
289     WPARAM font;
290     int dlu4inpix;
291     int ypos, width;
292     int xoff;
293     int boxystart, boxid;
294     char *boxtext;
295 };
296
297 /*
298  * Exports from winutils.c.
299  */
300 typedef struct filereq_tag filereq; /* cwd for file requester */
301 BOOL request_file(filereq *state, OPENFILENAME *of, int preserve, int save);
302 filereq *filereq_new(void);
303 void filereq_free(filereq *state);
304 int message_box(LPCTSTR text, LPCTSTR caption, DWORD style, DWORD helpctxid);
305 char *GetDlgItemText_alloc(HWND hwnd, int id);
306 void split_into_argv(char *, int *, char ***, char ***);
307
308 /*
309  * Private structure for prefslist state. Only in the header file
310  * so that we can delegate allocation to callers.
311  */
312 struct prefslist {
313     int listid, upbid, dnbid;
314     int srcitem;
315     int dummyitem;
316     int dragging;
317 };
318
319 /*
320  * This structure is passed to event handler functions as the `dlg'
321  * parameter, and hence is passed back to winctrls access functions.
322  */
323 struct dlgparam {
324     HWND hwnd;                         /* the hwnd of the dialog box */
325     struct winctrls *controltrees[8];  /* can have several of these */
326     int nctrltrees;
327     char *wintitle;                    /* title of actual window */
328     char *errtitle;                    /* title of error sub-messageboxes */
329     void *data;                        /* data to pass in refresh events */
330     union control *focused, *lastfocused; /* which ctrl has focus now/before */
331     char shortcuts[128];               /* track which shortcuts in use */
332     int coloursel_wanted;              /* has an event handler asked for
333                                         * a colour selector? */
334     struct { unsigned char r, g, b, ok; } coloursel_result;   /* 0-255 */
335     tree234 *privdata;                 /* stores per-control private data */
336     int ended, endresult;              /* has the dialog been ended? */
337     int fixed_pitch_fonts;             /* are we constrained to fixed fonts? */
338 };
339
340 /*
341  * Exports from winctrls.c.
342  */
343 void ctlposinit(struct ctlpos *cp, HWND hwnd,
344                 int leftborder, int rightborder, int topborder);
345 HWND doctl(struct ctlpos *cp, RECT r,
346            char *wclass, int wstyle, int exstyle, char *wtext, int wid);
347 void bartitle(struct ctlpos *cp, char *name, int id);
348 void beginbox(struct ctlpos *cp, char *name, int idbox);
349 void endbox(struct ctlpos *cp);
350 void editboxfw(struct ctlpos *cp, int password, char *text,
351                int staticid, int editid);
352 void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...);
353 void bareradioline(struct ctlpos *cp, int nacross, ...);
354 void radiobig(struct ctlpos *cp, char *text, int id, ...);
355 void checkbox(struct ctlpos *cp, char *text, int id);
356 void statictext(struct ctlpos *cp, char *text, int lines, int id);
357 void staticbtn(struct ctlpos *cp, char *stext, int sid,
358                char *btext, int bid);
359 void static2btn(struct ctlpos *cp, char *stext, int sid,
360                 char *btext1, int bid1, char *btext2, int bid2);
361 void staticedit(struct ctlpos *cp, char *stext,
362                 int sid, int eid, int percentedit);
363 void staticddl(struct ctlpos *cp, char *stext,
364                int sid, int lid, int percentlist);
365 void combobox(struct ctlpos *cp, char *text, int staticid, int listid);
366 void staticpassedit(struct ctlpos *cp, char *stext,
367                     int sid, int eid, int percentedit);
368 void bigeditctrl(struct ctlpos *cp, char *stext,
369                  int sid, int eid, int lines);
370 void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id);
371 void editbutton(struct ctlpos *cp, char *stext, int sid,
372                 int eid, char *btext, int bid);
373 void sesssaver(struct ctlpos *cp, char *text,
374                int staticid, int editid, int listid, ...);
375 void envsetter(struct ctlpos *cp, char *stext, int sid,
376                char *e1stext, int e1sid, int e1id,
377                char *e2stext, int e2sid, int e2id,
378                int listid, char *b1text, int b1id, char *b2text, int b2id);
379 void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
380                char *btext, int bid, int eid, char *s2text, int s2id);
381 void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
382                 char *btext, int bid, ...);
383 void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
384                char *stext, int sid, int listid, int upbid, int dnbid);
385 int handle_prefslist(struct prefslist *hdl,
386                      int *array, int maxmemb,
387                      int is_dlmsg, HWND hwnd,
388                      WPARAM wParam, LPARAM lParam);
389 void progressbar(struct ctlpos *cp, int id);
390 void fwdsetter(struct ctlpos *cp, int listid, char *stext, int sid,
391                char *e1stext, int e1sid, int e1id,
392                char *e2stext, int e2sid, int e2id,
393                char *btext, int bid,
394                char *r1text, int r1id, char *r2text, int r2id);
395
396 void dlg_auto_set_fixed_pitch_flag(void *dlg);
397 int dlg_get_fixed_pitch_flag(void *dlg);
398 void dlg_set_fixed_pitch_flag(void *dlg, int flag);
399
400 #define MAX_SHORTCUTS_PER_CTRL 16
401
402 /*
403  * This structure is what's stored for each `union control' in the
404  * portable-dialog interface.
405  */
406 struct winctrl {
407     union control *ctrl;
408     /*
409      * The control may have several components at the Windows
410      * level, with different dialog IDs. To avoid needing N
411      * separate platformsidectrl structures (which could be stored
412      * separately in a tree234 so that lookup by ID worked), we
413      * impose the constraint that those IDs must be in a contiguous
414      * block.
415      */
416     int base_id;
417     int num_ids;
418     /*
419      * Remember what keyboard shortcuts were used by this control,
420      * so that when we remove it again we can take them out of the
421      * list in the dlgparam.
422      */
423     char shortcuts[MAX_SHORTCUTS_PER_CTRL];
424     /*
425      * Some controls need a piece of allocated memory in which to
426      * store temporary data about the control.
427      */
428     void *data;
429 };
430 /*
431  * And this structure holds a set of the above, in two separate
432  * tree234s so that it can find an item by `union control' or by
433  * dialog ID.
434  */
435 struct winctrls {
436     tree234 *byctrl, *byid;
437 };
438 struct controlset;
439 struct controlbox;
440
441 void winctrl_init(struct winctrls *);
442 void winctrl_cleanup(struct winctrls *);
443 void winctrl_add(struct winctrls *, struct winctrl *);
444 void winctrl_remove(struct winctrls *, struct winctrl *);
445 struct winctrl *winctrl_findbyctrl(struct winctrls *, union control *);
446 struct winctrl *winctrl_findbyid(struct winctrls *, int);
447 struct winctrl *winctrl_findbyindex(struct winctrls *, int);
448 void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
449                     struct ctlpos *cp, struct controlset *s, int *id);
450 int winctrl_handle_command(struct dlgparam *dp, UINT msg,
451                            WPARAM wParam, LPARAM lParam);
452 void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c);
453 int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id);
454
455 void dp_init(struct dlgparam *dp);
456 void dp_add_tree(struct dlgparam *dp, struct winctrls *tree);
457 void dp_cleanup(struct dlgparam *dp);
458
459 /*
460  * Exports from wincfg.c.
461  */
462 void win_setup_config_box(struct controlbox *b, HWND *hwndp, int has_help,
463                           int midsession, int protocol);
464
465 /*
466  * Exports from windlg.c.
467  */
468 void defuse_showwindow(void);
469 int do_config(void);
470 int do_reconfig(HWND, int);
471 void showeventlog(HWND);
472 void showabout(HWND);
473 void force_normal(HWND hwnd);
474 void modal_about_box(HWND hwnd);
475 void show_help(HWND hwnd);
476
477 /*
478  * Exports from winmisc.c.
479  */
480 extern OSVERSIONINFO osVersion;
481 void dll_hijacking_protection(void);
482 BOOL init_winver(void);
483 HMODULE load_system32_dll(const char *libname);
484 const char *win_strerror(int error);
485
486 /*
487  * Exports from sizetip.c.
488  */
489 void UpdateSizeTip(HWND src, int cx, int cy);
490 void EnableSizeTip(int bEnable);
491
492 /*
493  * Exports from unicode.c.
494  */
495 struct unicode_data;
496 void init_ucs(Conf *, struct unicode_data *);
497
498 /*
499  * Exports from winhandl.c.
500  */
501 #define HANDLE_FLAG_OVERLAPPED 1
502 #define HANDLE_FLAG_IGNOREEOF 2
503 #define HANDLE_FLAG_UNITBUFFER 4
504 struct handle;
505 typedef int (*handle_inputfn_t)(struct handle *h, void *data, int len);
506 typedef void (*handle_outputfn_t)(struct handle *h, int new_backlog);
507 struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata,
508                                 void *privdata, int flags);
509 struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata,
510                                  void *privdata, int flags);
511 int handle_write(struct handle *h, const void *data, int len);
512 void handle_write_eof(struct handle *h);
513 HANDLE *handle_get_events(int *nevents);
514 void handle_free(struct handle *h);
515 void handle_got_event(HANDLE event);
516 void handle_unthrottle(struct handle *h, int backlog);
517 int handle_backlog(struct handle *h);
518 void *handle_get_privdata(struct handle *h);
519 struct handle *handle_add_foreign_event(HANDLE event,
520                                         void (*callback)(void *), void *ctx);
521
522 /*
523  * winpgntc.c needs to schedule callbacks for asynchronous agent
524  * requests. This has to be done differently in GUI and console, so
525  * there's an exported function used for the purpose.
526  * 
527  * Also, we supply FLAG_SYNCAGENT to force agent requests to be
528  * synchronous in pscp and psftp.
529  */
530 void agent_schedule_callback(void (*callback)(void *, void *, int),
531                              void *callback_ctx, void *data, int len);
532 #define FLAG_SYNCAGENT 0x1000
533
534 /*
535  * Exports from winser.c.
536  */
537 extern Backend serial_backend;
538
539 /*
540  * Exports from winjump.c.
541  */
542 #define JUMPLIST_SUPPORTED             /* suppress #defines in putty.h */
543 void add_session_to_jumplist(const char * const sessionname);
544 void remove_session_from_jumplist(const char * const sessionname);
545 void clear_jumplist(void);
546
547 /*
548  * Extra functions in winstore.c over and above the interface in
549  * storage.h.
550  *
551  * These functions manipulate the Registry section which mirrors the
552  * current Windows 7 jump list. (Because the real jump list storage is
553  * write-only, we need to keep another copy of whatever we put in it,
554  * so that we can put in a slightly modified version the next time.)
555  */
556
557 /* Adds a saved session to the registry jump list mirror. 'item' is a
558  * string naming a saved session. */
559 int add_to_jumplist_registry(const char *item);
560
561 /* Removes an item from the registry jump list mirror. */
562 int remove_from_jumplist_registry(const char *item);
563
564 /* Returns the current jump list entries from the registry. Caller
565  * must free the returned pointer, which points to a contiguous
566  * sequence of NUL-terminated strings in memory, terminated with an
567  * empty one. */
568 char *get_jumplist_registry_entries(void);
569
570 #endif