📄 pageant.c
字号:
int index_key, index_menu; if (!putty_path) return; if(ERROR_SUCCESS != RegOpenKey(HKEY_CURRENT_USER, PUTTY_REGKEY, &hkey)) return; for(num_entries = GetMenuItemCount(session_menu); num_entries > initial_menuitems_count; num_entries--) RemoveMenu(session_menu, 0, MF_BYPOSITION); index_key = 0; index_menu = 0; while(ERROR_SUCCESS == RegEnumKey(hkey, index_key, buf, MAX_PATH)) { TCHAR session_name[MAX_PATH + 1]; unmungestr(buf, session_name, MAX_PATH); if(strcmp(buf, PUTTY_DEFAULT) != 0) { memset(&mii, 0, sizeof(mii)); mii.cbSize = sizeof(mii); mii.fMask = MIIM_TYPE | MIIM_STATE | MIIM_ID; mii.fType = MFT_STRING; mii.fState = MFS_ENABLED; mii.wID = (index_menu * 16) + IDM_SESSIONS_BASE; mii.dwTypeData = session_name; InsertMenuItem(session_menu, index_menu, TRUE, &mii); index_menu++; } index_key++; } RegCloseKey(hkey); if(index_menu == 0) { mii.cbSize = sizeof(mii); mii.fMask = MIIM_TYPE | MIIM_STATE; mii.fType = MFT_STRING; mii.fState = MFS_GRAYED; mii.dwTypeData = _T("(No sessions)"); InsertMenuItem(session_menu, index_menu, TRUE, &mii); }}static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ int ret; static int menuinprogress; static UINT msgTaskbarCreated = 0; switch (message) { case WM_CREATE: msgTaskbarCreated = RegisterWindowMessage(_T("TaskbarCreated")); break; default: if (message==msgTaskbarCreated) { /* * Explorer has been restarted, so the tray icon will * have been lost. */ AddTrayIcon(hwnd); } break; 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; update_sessions(); 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_PUTTY: if((int)ShellExecute(hwnd, NULL, putty_path, _T(""), _T(""), SW_SHOW) <= 32) { MessageBox(NULL, "Unable to execute PuTTY!", "Error", MB_OK | MB_ICONERROR); } break; case IDM_CLOSE: if (passphrase_box) SendMessage(passphrase_box, WM_CLOSE, 0, 0); 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. This also brings it * to the front if it's already present (the user * selected View Keys because they wanted to _see_ the * thing). */ SetForegroundWindow(keylist); SetWindowPos(keylist, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); break; case IDM_ADDKEY: if (passphrase_box) { MessageBeep(MB_ICONERROR); SetForegroundWindow(passphrase_box); break; } 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; case IDM_HELP: if (help_path) { WinHelp(main_hwnd, help_path, HELP_COMMAND, (DWORD)"JI(`',`pageant.general')"); requested_help = TRUE; } break; default: { if(wParam >= IDM_SESSIONS_BASE && wParam <= IDM_SESSIONS_MAX) { MENUITEMINFO mii; TCHAR buf[MAX_PATH + 1]; TCHAR param[MAX_PATH + 1]; memset(&mii, 0, sizeof(mii)); mii.cbSize = sizeof(mii); mii.fMask = MIIM_TYPE; mii.cch = MAX_PATH; mii.dwTypeData = buf; GetMenuItemInfo(session_menu, wParam, FALSE, &mii); strcpy(param, "@"); strcat(param, mii.dwTypeData); if((int)ShellExecute(hwnd, NULL, putty_path, param, _T(""), SW_SHOW) <= 32) { MessageBox(NULL, "Unable to execute PuTTY!", "Error", MB_OK | MB_ICONERROR); } } } break; } break; case WM_DESTROY: if (requested_help) { WinHelp(main_hwnd, help_path, HELP_QUIT, 0); requested_help = FALSE; } PostQuitMessage(0); return 0; case WM_COPYDATA: { COPYDATASTRUCT *cds; char *mapname; void *p; HANDLE filemap;#ifndef NO_SECURITY HANDLE proc; PSID mapowner, procowner; PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;#endif 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:\n", mapname));#endif filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);#ifdef DEBUG_IPC debug(("filemap is %p\n", filemap));#endif if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {#ifndef NO_SECURITY int rc; if (has_security) { if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE, GetCurrentProcessId())) == NULL) {#ifdef DEBUG_IPC debug(("couldn't get handle for process\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\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\n", rc));#endif return 0; }#ifdef DEBUG_IPC debug(("got security stuff\n"));#endif if (!EqualSid(mapowner, procowner)) return 0; /* security ID mismatch! */#ifdef DEBUG_IPC debug(("security stuff matched\n"));#endif LocalFree(psd1); LocalFree(psd2); } else {#ifdef DEBUG_IPC debug(("security APIs not present\n"));#endif }#endif p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);#ifdef DEBUG_IPC debug(("p is %p\n", p)); { int i; for (i = 0; i < 5; i++) debug( ("p[%d]=%02x\n", i, ((unsigned char *) p)[i]));}#endif answer_msg(p); ret = 1; UnmapViewOfFile(p); } CloseHandle(filemap); return ret; } } return DefWindowProc(hwnd, message, wParam, lParam);}/* * Fork and Exec the command in cmdline. [DBW] */void spawn_cmd(char *cmdline, char * args, int show){ if (ShellExecute(NULL, _T("open"), cmdline, args, NULL, show) <= (HINSTANCE) 32) { char *msg; msg = dupprintf("Failed to run \"%.100s\", Error: %d", cmdline, (int)GetLastError()); MessageBox(NULL, msg, APPNAME, MB_OK | MB_ICONEXCLAMATION); sfree(msg); }}/* * This is a can't-happen stub, since Pageant never makes * asynchronous agent requests. */void agent_schedule_callback(void (*callback)(void *, void *, int), void *callback_ctx, void *data, int len){ assert(!"We shouldn't get here");}void cleanup_exit(int code) { exit(code); }int flags = FLAG_SYNCAGENT;int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show){ WNDCLASS wndclass; MSG msg; HMODULE advapi; char *command = NULL; int added_keys = 0; int argc, i; char **argv, **argstart; /* * Determine whether we're an NT system (should have security * APIs) or a non-NT system (don't do security). */ if (!init_winver()) { modalfatalbox("Windows refuses to report a version"); } if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) { has_security = TRUE; } else has_security = FALSE; if (has_security) {#ifndef NO_SECURITY /* * Attempt to get the security API we need. */ advapi = LoadLibrary("ADVAPI32.DLL"); getsecurityinfo = (gsi_fn_t) GetProcAddress(advapi, "GetSecurityInfo"); if (!getsecurityinfo) { MessageBox(NULL, "Unable to access security APIs. Pageant will\n" "not run, in case it causes a security breach.", "Pageant Fatal Error", MB_ICONERROR | MB_OK); return 1; }#else MessageBox(NULL, "This program has been compiled for Win9X and will\n" "not run on NT, in case it causes a security breach.", "Pageant Fatal Error", MB_ICONERROR | MB_OK); return 1;#endif } else advapi = NULL; instance = inst; /* * See if we can find our Help file. */ { char b[2048], *p, *q, *r; FILE *fp; GetModuleFileName(NULL, b, sizeof(b) - 1); r = b; p = strrchr(b, '\\'); if (p && p >= r) r = p+1; q = strrchr(b, ':'); if (q && q >= r) r = q+1; strcpy(r, "putty.hlp"); if ( (fp = fopen(b, "r")) != NULL) { help_path = dupstr(b); fclose(fp); } else help_path = NULL; } /* * Look for the PuTTY binary (we will enable the saved session * submenu if we find it). */ { char b[2048], *p, *q, *r; FILE *fp; GetModuleFileName(NULL, b, sizeof(b) - 1); r = b; p = strrchr(b, '\\'); if (p && p >= r) r = p+1; q = strrchr(b, ':'); if (q && q >= r) r = q+1; strcpy(r, "putty.exe"); if ( (fp = fopen(b, "r")) != NULL) { putty_path = dupstr(b); fclose(fp); } else putty_path = NULL; } /* * Find out if Pageant is already running. */ already_running = FALSE; if (agent_exists()) already_running = TRUE; else { if (!prev) { wndclass.style = 0; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = inst; wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON)); wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM); wndclass.hbrBackground = GetStockObject(BLACK_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = APPNAME; RegisterClass(&wndclass); } main_hwnd = keylist = NULL; main_hwnd = CreateWindow(APPNAME, APPNAME, WS_OVERLAPPEDWINDOW | WS_VSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, 100, 100, NULL, NULL, inst, NULL); /* Set up a system tray icon */ AddTrayIcon(main_hwnd); /* Accelerators used: nsvkxa */ systray_menu = CreatePopupMenu(); if (putty_path) { session_menu = CreateMenu(); AppendMenu(systray_menu, MF_ENABLED, IDM_PUTTY, "&New Session"); AppendMenu(systray_menu, MF_POPUP | MF_ENABLED, (UINT) session_menu, "&Saved Sessions"); AppendMenu(systray_menu, MF_SEPARATOR, 0, 0); } AppendMenu(systray_menu, MF_ENABLED, IDM_VIEWKEYS, "&View Keys"); AppendMenu(systray_menu, MF_ENABLED, IDM_ADDKEY, "Add &Key"); AppendMenu(systray_menu, MF_SEPARATOR, 0, 0); if (help_path) AppendMenu(systray_menu, MF_ENABLED, IDM_HELP, "&Help"); AppendMenu(systray_menu, MF_ENABLED, IDM_ABOUT, "&About"); AppendMenu(systray_menu, MF_SEPARATOR, 0, 0); AppendMenu(systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit"); initial_menuitems_count = GetMenuItemCount(session_menu); ShowWindow(main_hwnd, SW_HIDE); /* * Initialise storage for RSA keys. */ rsakeys = newtree234(cmpkeys_rsa); ssh2keys = newtree234(cmpkeys_ssh2); } /* * Initialise storage for short-term passphrase cache. */ passphrases = newtree234(NULL); /* * Process the command line and add keys as listed on it. */ split_into_argv(cmdline, &argc, &argv, &argstart); for (i = 0; i < argc; i++) { if (!strcmp(argv[i], "-c")) { /* * If we see `-c', then the rest of the * command line should be treated as a * command to be spawned. */ if (i < argc-1) command = argstart[i+1]; else command = ""; break; } else { add_keyfile(filename_from_str(argv[i])); added_keys = TRUE; } } /* * Forget any passphrase that we retained while going over * command line keyfiles. */ forget_passphrases(); if (command) { char *args; if (command[0] == '"') args = strchr(++command, '"'); else args = strchr(command, ' '); if (args) { *args++ = 0; while(*args && isspace(*args)) args++; } spawn_cmd(command, args, show); } /* * If Pageant was already running, we leave now. If we haven't * even taken any auxiliary action (spawned a command or added * keys), complain. */ if (already_running) { if (!command && !added_keys) { MessageBox(NULL, "Pageant is already running", "Pageant Error", MB_ICONERROR | MB_OK); } if (advapi) FreeLibrary(advapi); return 0; } /* * Main message loop. */ while (GetMessage(&msg, NULL, 0, 0) == 1) { if (!(IsWindow(keylist) && IsDialogMessage(keylist, &msg)) && !(IsWindow(aboutbox) && IsDialogMessage(aboutbox, &msg))) { TranslateMessage(&msg); DispatchMessage(&msg); } } /* Clean up the system tray icon */ { NOTIFYICONDATA tnid; tnid.cbSize = sizeof(NOTIFYICONDATA); tnid.hWnd = main_hwnd; tnid.uID = 1; Shell_NotifyIcon(NIM_DELETE, &tnid); DestroyMenu(systray_menu); } if (advapi) FreeLibrary(advapi); return msg.wParam;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -