1
0

winpgntc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #include "wincapi.h"
  12. #endif
  13. #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
  14. static bool wm_copydata_agent_exists(void)
  15. {
  16. HWND hwnd;
  17. hwnd = FindWindow("Pageant", "Pageant");
  18. if (!hwnd)
  19. return false;
  20. else
  21. return true;
  22. }
  23. static void wm_copydata_agent_query(strbuf *query, void **out, int *outlen)
  24. {
  25. HWND hwnd;
  26. char *mapname;
  27. HANDLE filemap;
  28. unsigned char *p, *ret;
  29. int id, retlen;
  30. COPYDATASTRUCT cds;
  31. SECURITY_ATTRIBUTES sa, *psa;
  32. PSECURITY_DESCRIPTOR psd = NULL;
  33. PSID usersid = NULL;
  34. *out = NULL;
  35. *outlen = 0;
  36. if (query->len > AGENT_MAX_MSGLEN)
  37. return; /* query too large */
  38. hwnd = FindWindow("Pageant", "Pageant");
  39. if (!hwnd)
  40. return; /* *out == NULL, so failure */
  41. mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
  42. psa = NULL;
  43. #ifndef NO_SECURITY
  44. if (got_advapi()) {
  45. /*
  46. * Make the file mapping we create for communication with
  47. * Pageant owned by the user SID rather than the default. This
  48. * should make communication between processes with slightly
  49. * different contexts more reliable: in particular, command
  50. * prompts launched as administrator should still be able to
  51. * run PSFTPs which refer back to the owning user's
  52. * unprivileged Pageant.
  53. */
  54. usersid = get_user_sid();
  55. if (usersid) {
  56. psd = (PSECURITY_DESCRIPTOR)
  57. LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  58. if (psd) {
  59. if (p_InitializeSecurityDescriptor
  60. (psd, SECURITY_DESCRIPTOR_REVISION) &&
  61. p_SetSecurityDescriptorOwner(psd, usersid, false)) {
  62. sa.nLength = sizeof(sa);
  63. sa.bInheritHandle = true;
  64. sa.lpSecurityDescriptor = psd;
  65. psa = &sa;
  66. } else {
  67. LocalFree(psd);
  68. psd = NULL;
  69. }
  70. }
  71. }
  72. }
  73. #endif /* NO_SECURITY */
  74. filemap = CreateFileMapping(INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
  75. 0, AGENT_MAX_MSGLEN, mapname);
  76. if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) {
  77. sfree(mapname);
  78. return; /* *out == NULL, so failure */
  79. }
  80. p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
  81. strbuf_finalise_agent_query(query);
  82. memcpy(p, query->s, query->len);
  83. cds.dwData = AGENT_COPYDATA_ID;
  84. cds.cbData = 1 + strlen(mapname);
  85. cds.lpData = mapname;
  86. /*
  87. * The user either passed a null callback (indicating that the
  88. * query is required to be synchronous) or CreateThread failed.
  89. * Either way, we need a synchronous request.
  90. */
  91. id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
  92. if (id > 0) {
  93. uint32_t length_field = GET_32BIT_MSB_FIRST(p);
  94. if (length_field > 0 && length_field <= AGENT_MAX_MSGLEN - 4) {
  95. retlen = length_field + 4;
  96. ret = snewn(retlen, unsigned char);
  97. memcpy(ret, p, retlen);
  98. *out = ret;
  99. *outlen = retlen;
  100. } else {
  101. /*
  102. * If we get here, we received an out-of-range length
  103. * field, either without space for a message type code or
  104. * overflowing the FileMapping.
  105. *
  106. * Treat this as if Pageant didn't answer at all - which
  107. * actually means we do nothing, and just don't fill in
  108. * out and outlen.
  109. */
  110. }
  111. }
  112. UnmapViewOfFile(p);
  113. CloseHandle(filemap);
  114. sfree(mapname);
  115. if (psd)
  116. LocalFree(psd);
  117. }
  118. #ifndef NO_SECURITY
  119. char *agent_named_pipe_name(void)
  120. {
  121. char *username, *suffix, *pipename;
  122. username = get_username();
  123. suffix = capi_obfuscate_string("Pageant");
  124. pipename = dupprintf("\\\\.\\pipe\\pageant.%s.%s", username, suffix);
  125. sfree(username);
  126. sfree(suffix);
  127. return pipename;
  128. }
  129. Socket *agent_connect(Plug *plug)
  130. {
  131. char *pipename = agent_named_pipe_name();
  132. Socket *s = new_named_pipe_client(pipename, plug);
  133. sfree(pipename);
  134. return s;
  135. }
  136. static bool named_pipe_agent_exists(void)
  137. {
  138. char *pipename = agent_named_pipe_name();
  139. WIN32_FIND_DATA data;
  140. HANDLE ffh = FindFirstFile(pipename, &data);
  141. sfree(pipename);
  142. if (ffh == INVALID_HANDLE_VALUE)
  143. return false;
  144. FindClose(ffh);
  145. return true;
  146. }
  147. bool agent_exists(void)
  148. {
  149. return named_pipe_agent_exists() || wm_copydata_agent_exists();
  150. }
  151. struct agent_pending_query {
  152. struct handle *handle;
  153. HANDLE os_handle;
  154. strbuf *response;
  155. void (*callback)(void *, void *, int);
  156. void *callback_ctx;
  157. struct callback_set *callback_set; // WINSCP
  158. };
  159. static int named_pipe_agent_accumulate_response(
  160. strbuf *sb, const void *data, size_t len)
  161. {
  162. put_data(sb, data, len);
  163. if (sb->len >= 4) {
  164. uint32_t length_field = GET_32BIT_MSB_FIRST(sb->u);
  165. if (length_field > AGENT_MAX_MSGLEN)
  166. return -1; /* badly formatted message */
  167. { // WINSCP
  168. int overall_length = length_field + 4;
  169. if (sb->len >= overall_length)
  170. return overall_length;
  171. } // WINSCP
  172. }
  173. return 0; /* not done yet */
  174. }
  175. static size_t named_pipe_agent_gotdata(
  176. struct handle *h, const void *data, size_t len, int err)
  177. {
  178. agent_pending_query *pq = handle_get_privdata(h);
  179. if (err || len == 0) {
  180. pq->callback(pq->callback_ctx, NULL, 0);
  181. agent_cancel_query(pq);
  182. return 0;
  183. }
  184. { // WINSCP
  185. int status = named_pipe_agent_accumulate_response(pq->response, data, len);
  186. if (status == -1) {
  187. pq->callback(pq->callback_ctx, NULL, 0);
  188. agent_cancel_query(pq);
  189. } else if (status > 0) {
  190. void *response_buf = strbuf_to_str(pq->response);
  191. pq->response = NULL;
  192. pq->callback(pq->callback_ctx, response_buf, status);
  193. agent_cancel_query(pq);
  194. }
  195. return 0;
  196. } // WINSCP
  197. }
  198. static agent_pending_query *named_pipe_agent_query(
  199. strbuf *query, void **out, int *outlen,
  200. void (*callback)(void *, void *, int), void *callback_ctx, struct callback_set * callback_set) // WINSCP
  201. {
  202. agent_pending_query *pq = NULL;
  203. char *err = NULL, *pipename = NULL;
  204. strbuf *sb = NULL;
  205. HANDLE pipehandle;
  206. pipename = agent_named_pipe_name();
  207. pipehandle = connect_to_named_pipe(pipename, &err);
  208. if (pipehandle == INVALID_HANDLE_VALUE)
  209. goto failure;
  210. strbuf_finalise_agent_query(query);
  211. { // WINSCP
  212. DWORD done; // WINSCP
  213. for (done = 0; done < query->len ;) {
  214. DWORD nwritten;
  215. bool ret = WriteFile(pipehandle, query->s + done, query->len - done,
  216. &nwritten, NULL);
  217. if (!ret)
  218. goto failure;
  219. done += nwritten;
  220. }
  221. if (!callback) {
  222. int status;
  223. sb = strbuf_new_nm();
  224. do {
  225. char buf[1024];
  226. DWORD nread;
  227. bool ret = ReadFile(pipehandle, buf, sizeof(buf), &nread, NULL);
  228. if (!ret)
  229. goto failure;
  230. status = named_pipe_agent_accumulate_response(sb, buf, nread);
  231. } while (status == 0);
  232. if (status == -1)
  233. goto failure;
  234. *out = strbuf_to_str(sb);
  235. *outlen = status;
  236. sb = NULL;
  237. pq = NULL;
  238. goto out;
  239. }
  240. pq = snew(agent_pending_query);
  241. pq->callback_set = callback_set; // WINSCP
  242. pq->handle = handle_input_new(callback_set->handles_by_evtomain, pipehandle, named_pipe_agent_gotdata, pq, 0); // WINSCP
  243. pq->os_handle = pipehandle;
  244. pipehandle = INVALID_HANDLE_VALUE; /* prevent it being closed below */
  245. pq->response = strbuf_new_nm();
  246. pq->callback = callback;
  247. pq->callback_ctx = callback_ctx;
  248. goto out;
  249. failure:
  250. *out = NULL;
  251. *outlen = 0;
  252. pq = NULL;
  253. out:
  254. sfree(err);
  255. sfree(pipename);
  256. if (pipehandle != INVALID_HANDLE_VALUE)
  257. CloseHandle(pipehandle);
  258. if (sb)
  259. strbuf_free(sb);
  260. return pq;
  261. } // WINSCP
  262. }
  263. void agent_cancel_query(agent_pending_query *pq)
  264. {
  265. handle_free(pq->callback_set->handles_by_evtomain, pq->handle);
  266. CloseHandle(pq->os_handle);
  267. if (pq->response)
  268. strbuf_free(pq->response);
  269. sfree(pq);
  270. }
  271. agent_pending_query *agent_query(
  272. strbuf *query, void **out, int *outlen,
  273. void (*callback)(void *, void *, int), void *callback_ctx, struct callback_set * callback_set) // WINSCP
  274. {
  275. agent_pending_query *pq = named_pipe_agent_query(
  276. query, out, outlen, callback, callback_ctx, callback_set); // WINSCP
  277. if (pq || *out)
  278. return pq;
  279. wm_copydata_agent_query(query, out, outlen);
  280. return NULL;
  281. }
  282. #else /* NO_SECURITY */
  283. Socket *agent_connect(void *vctx, Plug *plug)
  284. {
  285. unreachable("no agent_connect_ctx can be constructed on this platform");
  286. }
  287. agent_connect_ctx *agent_get_connect_ctx(void)
  288. {
  289. return NULL;
  290. }
  291. void agent_free_connect_ctx(agent_connect_ctx *ctx)
  292. {
  293. }
  294. bool agent_exists(void)
  295. {
  296. return wm_copydata_agent_exists();
  297. }
  298. agent_pending_query *agent_query(
  299. strbuf *query, void **out, int *outlen,
  300. void (*callback)(void *, void *, int), void *callback_ctx)
  301. {
  302. wm_copydata_agent_query(query, out, outlen);
  303. return NULL;
  304. }
  305. void agent_cancel_query(agent_pending_query *q)
  306. {
  307. unreachable("Windows agent queries are never asynchronous!");
  308. }
  309. #endif /* NO_SECURITY */