-static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
- WPARAM wParam, LPARAM lParam) {
- int ret;
- static int menuinprogress;
-
- switch (message) {
- case WM_SYSTRAY:
- if (lParam == WM_RBUTTONUP) {
- POINT cursorpos;
- GetCursorPos(&cursorpos);
- PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
- } else if (lParam == WM_LBUTTONDBLCLK) {
- /* Equivalent to IDM_VIEWKEYS. */
- PostMessage(hwnd, WM_COMMAND, IDM_VIEWKEYS, 0);
- }
- break;
- case WM_SYSTRAY2:
- if (!menuinprogress) {
- menuinprogress = 1;
- SetForegroundWindow(hwnd);
- ret = TrackPopupMenu(systray_menu,
- TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
- TPM_RIGHTBUTTON,
- wParam, lParam, 0, hwnd, NULL);
- menuinprogress = 0;
- }
- break;
- case WM_COMMAND:
- case WM_SYSCOMMAND:
- switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
- case IDM_CLOSE:
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- break;
- case IDM_VIEWKEYS:
- if (!keylist) {
- keylist = CreateDialog (instance, MAKEINTRESOURCE(211),
- NULL, KeyListProc);
- ShowWindow (keylist, SW_SHOWNORMAL);
- /*
- * Sometimes the window comes up minimised / hidden
- * for no obvious reason. Prevent this.
- */
- SetForegroundWindow(keylist);
- SetWindowPos (keylist, HWND_TOP, 0, 0, 0, 0,
- SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
- }
- break;
- case IDM_ADDKEY:
- prompt_add_keyfile();
- break;
- case IDM_ABOUT:
- if (!aboutbox) {
- aboutbox = CreateDialog (instance, MAKEINTRESOURCE(213),
- NULL, AboutProc);
- ShowWindow (aboutbox, SW_SHOWNORMAL);
- /*
- * Sometimes the window comes up minimised / hidden
- * for no obvious reason. Prevent this.
- */
- SetForegroundWindow(aboutbox);
- SetWindowPos (aboutbox, HWND_TOP, 0, 0, 0, 0,
- SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
- }
- break;
- }
- break;
- case WM_DESTROY:
- PostQuitMessage (0);
- return 0;
- case WM_COPYDATA:
- {
- COPYDATASTRUCT *cds;
- char *mapname;
- void *p;
- HANDLE filemap, proc;
- PSID mapowner, procowner;
- PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;
- int ret = 0;
-
- cds = (COPYDATASTRUCT *)lParam;
- if (cds->dwData != AGENT_COPYDATA_ID)
- return 0; /* not our message, mate */
- mapname = (char *)cds->lpData;
- if (mapname[cds->cbData - 1] != '\0')
- return 0; /* failure to be ASCIZ! */
-#ifdef DEBUG_IPC
- debug(("mapname is :%s:\r\n", mapname));
-#endif
- filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);
-#ifdef DEBUG_IPC
- debug(("filemap is %p\r\n", filemap));
-#endif
- if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {
- int rc;
-#ifndef NO_SECURITY
- if (has_security) {
- if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
- GetCurrentProcessId())) == NULL) {
-#ifdef DEBUG_IPC
- debug(("couldn't get handle for process\r\n"));
-#endif
- return 0;
- }
- if (getsecurityinfo(proc, SE_KERNEL_OBJECT,
- OWNER_SECURITY_INFORMATION,
- &procowner, NULL, NULL, NULL,
- &psd2) != ERROR_SUCCESS) {
-#ifdef DEBUG_IPC
- debug(("couldn't get owner info for process\r\n"));
-#endif
- CloseHandle(proc);
- return 0; /* unable to get security info */
- }
- CloseHandle(proc);
- if ((rc = getsecurityinfo(filemap, SE_KERNEL_OBJECT,
- OWNER_SECURITY_INFORMATION,
- &mapowner, NULL, NULL, NULL,
- &psd1) != ERROR_SUCCESS)) {
-#ifdef DEBUG_IPC
- debug(("couldn't get owner info for filemap: %d\r\n", rc));
-#endif
- return 0;
- }
-#ifdef DEBUG_IPC
- debug(("got security stuff\r\n"));
-#endif
- if (!EqualSid(mapowner, procowner))
- return 0; /* security ID mismatch! */
-#ifdef DEBUG_IPC
- debug(("security stuff matched\r\n"));
-#endif
- LocalFree(psd1);
- LocalFree(psd2);
- } else {
-#ifdef DEBUG_IPC
- debug(("security APIs not present\r\n"));
-#endif
- }
-#endif
- p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
-#ifdef DEBUG_IPC
- debug(("p is %p\r\n", p));
- {int i; for(i=0;i<5;i++)debug(("p[%d]=%02x\r\n", i, ((unsigned char *)p)[i]));}
-#endif
- answer_msg(p);
- ret = 1;
- UnmapViewOfFile(p);
- }
- CloseHandle(filemap);
- return ret;