hs_pirprocess.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**
  2. * \file hs_pirprocess.c
  3. * \brief Handle hidden service PIR helper processes.
  4. **/
  5. #include "core/or/or.h"
  6. #include "feature/hs/hs_pirprocess.h"
  7. #define SUBPROCESS_PRIVATE
  8. #include "lib/process/env.h"
  9. #include "lib/process/subprocess.h"
  10. #include "lib/evloop/compat_libevent.h"
  11. #include "feature/dircommon/dir_connection_st.h"
  12. #include <event2/event.h>
  13. #ifdef HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. typedef enum {
  17. PIRPROCESS_READSTATE_HEADER,
  18. PIRPROCESS_READSTATE_BODY
  19. } PIRProcessReadState;
  20. struct pir_process_st {
  21. process_handle_t *process;
  22. buf_t *stdin_buf;
  23. buf_t *stderr_buf;
  24. struct event *stdin_ev;
  25. struct event *stdout_ev;
  26. struct event *stderr_ev;
  27. char *loglabel;
  28. pir_process_msghandler_t msghandler;
  29. PIRProcessReadState readstate;
  30. size_t readoff, readleft;
  31. unsigned char hdrbuf[PIRPROCESS_HDR_SIZE];
  32. char *bodybuf;
  33. };
  34. /* This is called when the pir process has output for us. */
  35. static void
  36. pirprocess_stdoutcb(evutil_socket_t fd, short what, void *arg)
  37. {
  38. pir_process_t handle = (pir_process_t)arg;
  39. if (!(what & EV_READ)) {
  40. /* Not sure why we're here */
  41. return;
  42. }
  43. if (handle->readstate == PIRPROCESS_READSTATE_HEADER) {
  44. int res = read(fd, handle->hdrbuf + handle->readoff, handle->readleft);
  45. if (res <= 0) {
  46. /* Tell the caller that the PIR process failed to send us any output. */
  47. tor_free(handle->bodybuf);
  48. handle->readoff = 0;
  49. handle->readleft = PIRPROCESS_HDR_SIZE;
  50. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  51. (handle->msghandler)(NULL, NULL, 0);
  52. return;
  53. }
  54. handle->readoff += res;
  55. handle->readleft -= res;
  56. if (handle->readleft == 0) {
  57. handle->readleft = ntohl(*(uint32_t*)
  58. (handle->hdrbuf+PIRPROCESS_HDR_SIZE-4));
  59. tor_free(handle->bodybuf);
  60. if (handle->readleft > 0) {
  61. handle->bodybuf = tor_malloc(handle->readleft+1);
  62. handle->bodybuf[handle->readleft] = '\0';
  63. handle->readoff = 0;
  64. handle->readstate = PIRPROCESS_READSTATE_BODY;
  65. } else {
  66. (handle->msghandler)(handle->hdrbuf, NULL, 0);
  67. handle->readoff = 0;
  68. handle->readleft = PIRPROCESS_HDR_SIZE;
  69. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  70. }
  71. }
  72. } else if (handle->readstate == PIRPROCESS_READSTATE_BODY) {
  73. int res = read(fd, handle->bodybuf + handle->readoff,
  74. handle->readleft);
  75. if (res <= 0) {
  76. /* Tell the caller that the PIR process failed to send us any output. */
  77. tor_free(handle->bodybuf);
  78. handle->readoff = 0;
  79. handle->readleft = PIRPROCESS_HDR_SIZE;
  80. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  81. (handle->msghandler)(NULL, NULL, 0);
  82. return;
  83. }
  84. handle->readoff += res;
  85. handle->readleft -= res;
  86. if (handle->readleft == 0) {
  87. /* Reading is complete */
  88. (handle->msghandler)(handle->hdrbuf, handle->bodybuf,
  89. handle->readoff);
  90. /* Prepare for the next output from the PIR server */
  91. tor_free(handle->bodybuf);
  92. handle->readoff = 0;
  93. handle->readleft = PIRPROCESS_HDR_SIZE;
  94. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  95. }
  96. }
  97. }
  98. /* This is called when the pir process is ready to read from its stdin. */
  99. static void
  100. pirprocess_stdincb(evutil_socket_t fd, short what, void *arg)
  101. {
  102. pir_process_t handle = (pir_process_t)arg;
  103. int res;
  104. size_t bufsize = buf_datalen(handle->stdin_buf);
  105. char *netbuf = NULL;
  106. if (!(what & EV_WRITE)) {
  107. /* Not sure why we're here */
  108. log_info(LD_DIRSERV,"PIRSERVER bailing");
  109. return;
  110. }
  111. if (bufsize == 0) {
  112. log_err(LD_DIRSERV,"PIRSERVER trying to write 0-length buffer");
  113. return;
  114. }
  115. netbuf = tor_malloc(bufsize);
  116. if (netbuf == NULL) {
  117. log_err(LD_DIRSERV,"PIRSERVER failed to allocate buffer");
  118. return;
  119. }
  120. /* One might think that just calling buf_flush_to_socket would be
  121. * the thing to do, but that function ends up calling sendto()
  122. * instead of write(), which doesn't work on pipes. So we do it
  123. * more manually. Using a bufferevent may be another option. */
  124. buf_peek(handle->stdin_buf, netbuf, bufsize);
  125. res = write(fd, netbuf, bufsize);
  126. tor_free(netbuf);
  127. if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
  128. /* Try writing again later */
  129. event_add(handle->stdin_ev, NULL);
  130. return;
  131. }
  132. if (res <= 0) {
  133. /* Stop trying to write. */
  134. return;
  135. }
  136. buf_drain(handle->stdin_buf, res);
  137. bufsize -= res;
  138. if (bufsize > 0) {
  139. /* There's more to write */
  140. event_add(handle->stdin_ev, NULL);
  141. }
  142. }
  143. /* This is called when the pir process writes something to its stderr. */
  144. static void
  145. pirprocess_stderrcb(evutil_socket_t fd, short what, void *arg)
  146. {
  147. pir_process_t handle = (pir_process_t)arg;
  148. if (!(what & EV_READ)) {
  149. /* Not sure why we're here */
  150. return;
  151. }
  152. /* Add the data to the stderr buffer */
  153. char buf[1000];
  154. int res = read(fd, buf, sizeof(buf)-1);
  155. if (res <= 0) return;
  156. buf[res] = '\0';
  157. buf_add(handle->stderr_buf, buf, res);
  158. /* Read lines from the stderr_buf */
  159. while (1) {
  160. size_t linesize = 0;
  161. res = buf_get_line(handle->stderr_buf, NULL, &linesize);
  162. if (res == 0) {
  163. /* No complete lines in the buffer */
  164. break;
  165. }
  166. char *linebuf = tor_malloc(linesize);
  167. buf_get_line(handle->stderr_buf, linebuf, &linesize);
  168. if (linesize > 0 && linebuf[linesize-1] == '\n') {
  169. linebuf[linesize-1] = '\0';
  170. }
  171. log_info(LD_DIRSERV,"%s %s", handle->loglabel, escaped(linebuf));
  172. tor_free(linebuf);
  173. }
  174. }
  175. void
  176. hs_pirprocess_close(pir_process_t *handlep)
  177. {
  178. pir_process_t handle;
  179. if (!handlep) return;
  180. if (!*handlep) return;
  181. handle = *handlep;
  182. if (handle->stdin_buf) {
  183. buf_free(handle->stdin_buf);
  184. }
  185. if (handle->stderr_buf) {
  186. buf_free(handle->stderr_buf);
  187. }
  188. if (handle->stdin_ev) {
  189. event_free(handle->stdin_ev);
  190. }
  191. if (handle->stdout_ev) {
  192. event_free(handle->stdout_ev);
  193. }
  194. if (handle->stderr_ev) {
  195. event_free(handle->stderr_ev);
  196. }
  197. if (handle->process) {
  198. tor_process_handle_destroy(handle->process, 1);
  199. }
  200. if (handle->loglabel) {
  201. tor_free(handle->loglabel);
  202. }
  203. if (handle->bodybuf) {
  204. tor_free(handle->bodybuf);
  205. }
  206. tor_free(handle);
  207. *handlep = NULL;
  208. }
  209. void
  210. hs_pirprocess_poke(const char *path, const char *loglabel,
  211. pir_process_msghandler_t msghandler, pir_process_t *handlep)
  212. {
  213. int res;
  214. smartlist_t *env_vars = get_current_process_environment_variables();
  215. const char *argv[2];
  216. process_environment_t *env;
  217. pir_process_t handle;
  218. if (!handlep) return;
  219. if (*handlep) {
  220. handle = *handlep;
  221. if (handle->process &&
  222. handle->process->status == PROCESS_STATUS_RUNNING) {
  223. /* The PIR process appears to be open */
  224. return;
  225. }
  226. }
  227. /* Close everything we've got and start over */
  228. hs_pirprocess_close(handlep);
  229. if (!path) {
  230. /* We don't have a configured PIR server */
  231. return;
  232. }
  233. *handlep = tor_malloc_zero(sizeof(struct pir_process_st));
  234. handle = *handlep;
  235. /* Start the process */
  236. argv[0] = path;
  237. argv[1] = NULL;
  238. env = process_environment_make(env_vars);
  239. res = tor_spawn_background(path, argv, env, &(handle->process));
  240. SMARTLIST_FOREACH(env_vars, void *, x, tor_free(x));
  241. smartlist_free(env_vars);
  242. if (res != PROCESS_STATUS_RUNNING) {
  243. /* Launch failure */
  244. hs_pirprocess_close(handlep);
  245. return;
  246. }
  247. handle->loglabel = tor_strdup(loglabel);
  248. handle->msghandler = msghandler;
  249. /* Create a libevent event to listen to the PIR process' responses. */
  250. handle->stdout_ev = event_new(tor_libevent_get_base(),
  251. handle->process->stdout_pipe, EV_READ|EV_PERSIST,
  252. pirprocess_stdoutcb, handle);
  253. event_add(handle->stdout_ev, NULL);
  254. /* And one to listen to the PIR server's stderr. */
  255. handle->stderr_ev = event_new(tor_libevent_get_base(),
  256. handle->process->stderr_pipe, EV_READ|EV_PERSIST,
  257. pirprocess_stderrcb, handle);
  258. event_add(handle->stderr_ev, NULL);
  259. /* And one for writability to the pir process' stdin, but don't add
  260. * it just yet. Also create the buffer it will use. */
  261. handle->stdin_buf = buf_new();
  262. handle->stderr_buf = buf_new();
  263. handle->stdin_ev = event_new(tor_libevent_get_base(),
  264. handle->process->stdin_pipe, EV_WRITE, pirprocess_stdincb,
  265. handle);
  266. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  267. handle->readoff = 0;
  268. handle->readleft = PIRPROCESS_HDR_SIZE;
  269. handle->bodybuf = NULL;
  270. }
  271. int
  272. hs_pirprocess_send(pir_process_t handle, const unsigned char *buf,
  273. size_t len)
  274. {
  275. if (handle == NULL || handle->process == NULL ||
  276. handle->process->status != PROCESS_STATUS_RUNNING) {
  277. /* We don't have a process to talk to */
  278. return -1;
  279. }
  280. /* Write the data to the stdin buffer */
  281. if (len > 0) {
  282. buf_add(handle->stdin_buf, (const char *)buf, len);
  283. event_add(handle->stdin_ev, NULL);
  284. }
  285. return len;
  286. }
  287. typedef struct {
  288. dir_connection_t *conn;
  289. pir_process_abort_fn abort_fn;
  290. } pirprocess_digestmap_entry_t;
  291. /* What we really want is a map from uint64_t to
  292. * pirprocess_digestmap_entry_t*, but we've got an implementation of
  293. * char[20] to void*, which we'll repurpose for this use. Note that the
  294. * dir_connection_t* pointers in this map are *not* owned by this map,
  295. * and this map must not free them. */
  296. static digestmap_t *pirprocess_reqid_map;
  297. /* The last-used request id */
  298. static uint64_t pirprocess_last_reqid;
  299. void
  300. hs_pirprocess_init(void)
  301. {
  302. pirprocess_reqid_map = digestmap_new();
  303. pirprocess_last_reqid = 0;
  304. }
  305. static void
  306. entry_abort_free(void *p)
  307. {
  308. pirprocess_digestmap_entry_t *entry = (pirprocess_digestmap_entry_t *)p;
  309. if (entry->abort_fn) {
  310. (entry->abort_fn)(entry->conn);
  311. }
  312. tor_free(entry);
  313. }
  314. void
  315. hs_pirprocess_abort_all(void)
  316. {
  317. digestmap_free(pirprocess_reqid_map, entry_abort_free);
  318. hs_pirprocess_init();
  319. }
  320. void
  321. hs_pirprocess_free_all(void)
  322. {
  323. digestmap_free(pirprocess_reqid_map, tor_free_);
  324. }
  325. uint64_t
  326. hs_pirprocess_alloc_reqid(dir_connection_t *dir_conn,
  327. pir_process_abort_fn abort_fn)
  328. {
  329. char mapbuf[DIGEST_LEN];
  330. pirprocess_digestmap_entry_t *entry;
  331. uint64_t reqid = dir_conn->pirprocess_reqid;
  332. if (reqid > 0) {
  333. return reqid;
  334. }
  335. ++pirprocess_last_reqid;
  336. if (pirprocess_last_reqid == 0) {
  337. /* We wrapped a 64-bit integer?! */
  338. pirprocess_last_reqid = 1;
  339. }
  340. reqid = pirprocess_last_reqid;
  341. memset(mapbuf, 0, DIGEST_LEN);
  342. memmove(mapbuf, &reqid, sizeof(reqid));
  343. entry = tor_malloc(sizeof(*entry));
  344. entry->conn = dir_conn;
  345. entry->abort_fn = abort_fn;
  346. digestmap_set(pirprocess_reqid_map, mapbuf, entry);
  347. dir_conn->pirprocess_reqid = reqid;
  348. return dir_conn->pirprocess_reqid;
  349. }
  350. void
  351. hs_pirprocess_dealloc_reqid(dir_connection_t *dir_conn)
  352. {
  353. char mapbuf[DIGEST_LEN];
  354. uint64_t reqid = dir_conn->pirprocess_reqid;
  355. pirprocess_digestmap_entry_t *entry;
  356. if (reqid == 0) return;
  357. memset(mapbuf, 0, DIGEST_LEN);
  358. memmove(mapbuf, &reqid, sizeof(reqid));
  359. entry = digestmap_remove(pirprocess_reqid_map, mapbuf);
  360. tor_free(entry);
  361. dir_conn->pirprocess_reqid = 0;
  362. }
  363. dir_connection_t *
  364. hs_pirprocess_lookup_reqid(uint64_t reqid)
  365. {
  366. char mapbuf[DIGEST_LEN];
  367. pirprocess_digestmap_entry_t *entry;
  368. memset(mapbuf, 0, DIGEST_LEN);
  369. memmove(mapbuf, &reqid, sizeof(reqid));
  370. entry = digestmap_get(pirprocess_reqid_map, mapbuf);
  371. return entry->conn;
  372. }