winpgntc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. strbuf *response;
  154. void (*callback)(void *, void *, int);
  155. void *callback_ctx;
  156. };
  157. static int named_pipe_agent_accumulate_response(
  158. strbuf *sb, const void *data, size_t len)
  159. {
  160. put_data(sb, data, len);
  161. if (sb->len >= 4) {
  162. uint32_t length_field = GET_32BIT_MSB_FIRST(sb->u);
  163. if (length_field > AGENT_MAX_MSGLEN)
  164. return -1; /* badly formatted message */
  165. { // WINSCP
  166. int overall_length = length_field + 4;
  167. if (sb->len >= overall_length)
  168. return overall_length;
  169. } // WINSCP
  170. }
  171. return 0; /* not done yet */
  172. }
  173. static size_t named_pipe_agent_gotdata(
  174. struct handle *h, const void *data, size_t len, int err)
  175. {
  176. agent_pending_query *pq = handle_get_privdata(h);
  177. if (err || len == 0) {
  178. pq->callback(pq->callback_ctx, NULL, 0);
  179. agent_cancel_query(pq);
  180. return 0;
  181. }
  182. { // WINSCP
  183. int status = named_pipe_agent_accumulate_response(pq->response, data, len);
  184. if (status == -1) {
  185. pq->callback(pq->callback_ctx, NULL, 0);
  186. agent_cancel_query(pq);
  187. } else if (status > 0) {
  188. void *response_buf = strbuf_to_str(pq->response);
  189. pq->response = NULL;
  190. pq->callback(pq->callback_ctx, response_buf, status);
  191. agent_cancel_query(pq);
  192. }
  193. return 0;
  194. } // WINSCP
  195. }
  196. static agent_pending_query *named_pipe_agent_query(
  197. strbuf *query, void **out, int *outlen,
  198. void (*callback)(void *, void *, int), void *callback_ctx)
  199. {
  200. agent_pending_query *pq = NULL;
  201. char *err = NULL, *pipename = NULL;
  202. strbuf *sb = NULL;
  203. HANDLE pipehandle;
  204. pipename = agent_named_pipe_name();
  205. pipehandle = connect_to_named_pipe(pipename, &err);
  206. if (pipehandle == INVALID_HANDLE_VALUE)
  207. goto failure;
  208. strbuf_finalise_agent_query(query);
  209. { // WINSCP
  210. DWORD done; // WINSCP
  211. for (done = 0; done < query->len ;) {
  212. DWORD nwritten;
  213. bool ret = WriteFile(pipehandle, query->s + done, query->len - done,
  214. &nwritten, NULL);
  215. if (!ret)
  216. goto failure;
  217. done += nwritten;
  218. }
  219. if (!callback) {
  220. int status;
  221. sb = strbuf_new_nm();
  222. do {
  223. char buf[1024];
  224. DWORD nread;
  225. bool ret = ReadFile(pipehandle, buf, sizeof(buf), &nread, NULL);
  226. if (!ret)
  227. goto failure;
  228. status = named_pipe_agent_accumulate_response(sb, buf, nread);
  229. } while (status == 0);
  230. if (status == -1)
  231. goto failure;
  232. *out = strbuf_to_str(sb);
  233. *outlen = status;
  234. sb = NULL;
  235. pq = NULL;
  236. goto out;
  237. }
  238. pq = snew(agent_pending_query);
  239. pq->handle = handle_input_new(pipehandle, named_pipe_agent_gotdata, pq, 0);
  240. pipehandle = NULL; /* prevent it being closed below */
  241. pq->response = strbuf_new_nm();
  242. pq->callback = callback;
  243. pq->callback_ctx = callback_ctx;
  244. goto out;
  245. failure:
  246. *out = NULL;
  247. *outlen = 0;
  248. pq = NULL;
  249. out:
  250. sfree(err);
  251. sfree(pipename);
  252. if (pipehandle != INVALID_HANDLE_VALUE)
  253. CloseHandle(pipehandle);
  254. if (sb)
  255. strbuf_free(sb);
  256. return pq;
  257. } // WINSCP
  258. }
  259. void agent_cancel_query(agent_pending_query *pq)
  260. {
  261. handle_free(pq->handle);
  262. if (pq->response)
  263. strbuf_free(pq->response);
  264. sfree(pq);
  265. }
  266. agent_pending_query *agent_query(
  267. strbuf *query, void **out, int *outlen,
  268. void (*callback)(void *, void *, int), void *callback_ctx)
  269. {
  270. agent_pending_query *pq = named_pipe_agent_query(
  271. query, out, outlen, callback, callback_ctx);
  272. if (pq || *out)
  273. return pq;
  274. wm_copydata_agent_query(query, out, outlen);
  275. return NULL;
  276. }
  277. #else /* NO_SECURITY */
  278. Socket *agent_connect(void *vctx, Plug *plug)
  279. {
  280. unreachable("no agent_connect_ctx can be constructed on this platform");
  281. }
  282. agent_connect_ctx *agent_get_connect_ctx(void)
  283. {
  284. return NULL;
  285. }
  286. void agent_free_connect_ctx(agent_connect_ctx *ctx)
  287. {
  288. }
  289. bool agent_exists(void)
  290. {
  291. return wm_copydata_agent_exists();
  292. }
  293. agent_pending_query *agent_query(
  294. strbuf *query, void **out, int *outlen,
  295. void (*callback)(void *, void *, int), void *callback_ctx)
  296. {
  297. wm_copydata_agent_query(query, out, outlen);
  298. return NULL;
  299. }
  300. void agent_cancel_query(agent_pending_query *q)
  301. {
  302. unreachable("Windows agent queries are never asynchronous!");
  303. }
  304. #endif /* NO_SECURITY */