hs_pirprocess.c 11 KB

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