winpgntc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Pageant client code.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include "putty.h"
  8. #include "pageant.h" /* for AGENT_MAX_MSGLEN */
  9. #ifndef NO_SECURITY
  10. #include "winsecur.h"
  11. #endif
  12. #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
  13. bool agent_exists(void)
  14. {
  15. HWND hwnd;
  16. hwnd = FindWindow("Pageant", "Pageant");
  17. if (!hwnd)
  18. return false;
  19. else
  20. return true;
  21. }
  22. void agent_cancel_query(agent_pending_query *q)
  23. {
  24. unreachable("Windows agent queries are never asynchronous!");
  25. }
  26. agent_pending_query *agent_query(
  27. strbuf *query, void **out, int *outlen,
  28. void (*callback)(void *, void *, int), void *callback_ctx)
  29. {
  30. HWND hwnd;
  31. char *mapname;
  32. HANDLE filemap;
  33. unsigned char *p, *ret;
  34. int id, retlen;
  35. COPYDATASTRUCT cds;
  36. SECURITY_ATTRIBUTES sa, *psa;
  37. PSECURITY_DESCRIPTOR psd = NULL;
  38. PSID usersid = NULL;
  39. *out = NULL;
  40. *outlen = 0;
  41. if (query->len > AGENT_MAX_MSGLEN)
  42. return NULL; /* query too large */
  43. hwnd = FindWindow("Pageant", "Pageant");
  44. if (!hwnd)
  45. return NULL; /* *out == NULL, so failure */
  46. mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
  47. psa = NULL;
  48. #ifndef NO_SECURITY
  49. if (got_advapi()) {
  50. /*
  51. * Make the file mapping we create for communication with
  52. * Pageant owned by the user SID rather than the default. This
  53. * should make communication between processes with slightly
  54. * different contexts more reliable: in particular, command
  55. * prompts launched as administrator should still be able to
  56. * run PSFTPs which refer back to the owning user's
  57. * unprivileged Pageant.
  58. */
  59. usersid = get_user_sid();
  60. if (usersid) {
  61. psd = (PSECURITY_DESCRIPTOR)
  62. LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  63. if (psd) {
  64. if (p_InitializeSecurityDescriptor
  65. (psd, SECURITY_DESCRIPTOR_REVISION) &&
  66. p_SetSecurityDescriptorOwner(psd, usersid, false)) {
  67. sa.nLength = sizeof(sa);
  68. sa.bInheritHandle = true;
  69. sa.lpSecurityDescriptor = psd;
  70. psa = &sa;
  71. } else {
  72. LocalFree(psd);
  73. psd = NULL;
  74. }
  75. }
  76. }
  77. }
  78. #endif /* NO_SECURITY */
  79. filemap = CreateFileMapping(INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
  80. 0, AGENT_MAX_MSGLEN, mapname);
  81. if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) {
  82. sfree(mapname);
  83. return NULL; /* *out == NULL, so failure */
  84. }
  85. p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
  86. strbuf_finalise_agent_query(query);
  87. memcpy(p, query->s, query->len);
  88. cds.dwData = AGENT_COPYDATA_ID;
  89. cds.cbData = 1 + strlen(mapname);
  90. cds.lpData = mapname;
  91. /*
  92. * The user either passed a null callback (indicating that the
  93. * query is required to be synchronous) or CreateThread failed.
  94. * Either way, we need a synchronous request.
  95. */
  96. id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
  97. if (id > 0) {
  98. retlen = 4 + GET_32BIT_MSB_FIRST(p);
  99. ret = snewn(retlen, unsigned char);
  100. if (ret) {
  101. memcpy(ret, p, retlen);
  102. *out = ret;
  103. *outlen = retlen;
  104. }
  105. }
  106. UnmapViewOfFile(p);
  107. CloseHandle(filemap);
  108. sfree(mapname);
  109. if (psd)
  110. LocalFree(psd);
  111. return NULL;
  112. }