hs_pirprocess.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <event2/event.h>
  12. #ifdef HAVE_UNISTD_H
  13. #include <unistd.h>
  14. #endif
  15. typedef enum {
  16. PIRPROCESS_READSTATE_HEADER,
  17. PIRPROCESS_READSTATE_BODY
  18. } PIRProcessReadState;
  19. struct pir_process_st {
  20. process_handle_t *process;
  21. buf_t *stdin_buf;
  22. buf_t *stderr_buf;
  23. struct event *stdin_ev;
  24. struct event *stdout_ev;
  25. struct event *stderr_ev;
  26. char *loglabel;
  27. pir_process_msghandler_t msghandler;
  28. PIRProcessReadState readstate;
  29. size_t readoff, readleft;
  30. unsigned char hdrbuf[PIRPROCESS_HDR_SIZE];
  31. char *bodybuf;
  32. };
  33. /* This is called when the pir process has output for us. */
  34. static void
  35. pirprocess_stdoutcb(evutil_socket_t fd, short what, void *arg)
  36. {
  37. pir_process_t handle = (pir_process_t)arg;
  38. if (!(what & EV_READ)) {
  39. /* Not sure why we're here */
  40. return;
  41. }
  42. if (handle->readstate == PIRPROCESS_READSTATE_HEADER) {
  43. int res = read(fd, handle->hdrbuf + handle->readoff, handle->readleft);
  44. if (res <= 0) return;
  45. handle->readoff += res;
  46. handle->readleft -= res;
  47. if (handle->readleft == 0) {
  48. handle->readleft = ntohl(*(uint32_t*)
  49. (handle->hdrbuf+PIRPROCESS_HDR_SIZE-4));
  50. tor_free(handle->bodybuf);
  51. if (handle->readleft > 0) {
  52. handle->bodybuf = tor_malloc(handle->readleft+1);
  53. handle->bodybuf[handle->readleft] = '\0';
  54. handle->readoff = 0;
  55. handle->readstate = PIRPROCESS_READSTATE_BODY;
  56. } else {
  57. (handle->msghandler)(handle->hdrbuf, NULL, 0);
  58. handle->readoff = 0;
  59. handle->readleft = PIRPROCESS_HDR_SIZE;
  60. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  61. }
  62. }
  63. } else if (handle->readstate == PIRPROCESS_READSTATE_BODY) {
  64. int res = read(fd, handle->bodybuf + handle->readoff,
  65. handle->readleft);
  66. if (res <= 0) return;
  67. handle->readoff += res;
  68. handle->readleft -= res;
  69. if (handle->readleft == 0) {
  70. /* Reading is complete */
  71. (handle->msghandler)(handle->hdrbuf, handle->bodybuf,
  72. handle->readoff);
  73. /* Prepare for the next output from the PIR server */
  74. tor_free(handle->bodybuf);
  75. handle->readoff = 0;
  76. handle->readleft = PIRPROCESS_HDR_SIZE;
  77. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  78. }
  79. }
  80. }
  81. /* This is called when the pir process is ready to read from its stdin. */
  82. static void
  83. pirprocess_stdincb(evutil_socket_t fd, short what, void *arg)
  84. {
  85. pir_process_t handle = (pir_process_t)arg;
  86. int res;
  87. size_t bufsize = buf_datalen(handle->stdin_buf);
  88. char *netbuf = NULL;
  89. if (!(what & EV_WRITE)) {
  90. /* Not sure why we're here */
  91. log_info(LD_DIRSERV,"PIRSERVER bailing");
  92. return;
  93. }
  94. if (bufsize == 0) {
  95. log_err(LD_DIRSERV,"PIRSERVER trying to write 0-length buffer");
  96. return;
  97. }
  98. netbuf = tor_malloc(bufsize);
  99. if (netbuf == NULL) {
  100. log_err(LD_DIRSERV,"PIRSERVER failed to allocate buffer");
  101. return;
  102. }
  103. /* One might think that just calling buf_flush_to_socket would be
  104. * the thing to do, but that function ends up calling sendto()
  105. * instead of write(), which doesn't work on pipes. So we do it
  106. * more manually. Using a bufferevent may be another option. */
  107. buf_peek(handle->stdin_buf, netbuf, bufsize);
  108. res = write(fd, netbuf, bufsize);
  109. tor_free(netbuf);
  110. if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
  111. /* Try writing again later */
  112. event_add(handle->stdin_ev, NULL);
  113. return;
  114. }
  115. if (res <= 0) {
  116. /* Stop trying to write. */
  117. return;
  118. }
  119. buf_drain(handle->stdin_buf, res);
  120. bufsize -= res;
  121. if (bufsize > 0) {
  122. /* There's more to write */
  123. event_add(handle->stdin_ev, NULL);
  124. }
  125. }
  126. /* This is called when the pir process writes something to its stderr. */
  127. static void
  128. pirprocess_stderrcb(evutil_socket_t fd, short what, void *arg)
  129. {
  130. pir_process_t handle = (pir_process_t)arg;
  131. if (!(what & EV_READ)) {
  132. /* Not sure why we're here */
  133. return;
  134. }
  135. /* Add the data to the stderr buffer */
  136. char buf[1000];
  137. int res = read(fd, buf, sizeof(buf)-1);
  138. if (res <= 0) return;
  139. buf[res] = '\0';
  140. buf_add(handle->stderr_buf, buf, res);
  141. /* Read lines from the stderr_buf */
  142. while (1) {
  143. size_t linesize = 0;
  144. res = buf_get_line(handle->stderr_buf, NULL, &linesize);
  145. if (res == 0) {
  146. /* No complete lines in the buffer */
  147. break;
  148. }
  149. char *linebuf = tor_malloc(linesize);
  150. buf_get_line(handle->stderr_buf, linebuf, &linesize);
  151. if (linesize > 0 && linebuf[linesize-1] == '\n') {
  152. linebuf[linesize-1] = '\0';
  153. }
  154. log_info(LD_DIRSERV,"%s %s", handle->loglabel, escaped(linebuf));
  155. tor_free(linebuf);
  156. }
  157. }
  158. void
  159. hs_pirprocess_close(pir_process_t *handlep)
  160. {
  161. pir_process_t handle;
  162. if (!handlep) return;
  163. if (!*handlep) return;
  164. handle = *handlep;
  165. if (handle->stdin_buf) {
  166. buf_free(handle->stdin_buf);
  167. }
  168. if (handle->stderr_buf) {
  169. buf_free(handle->stderr_buf);
  170. }
  171. if (handle->stdin_ev) {
  172. event_free(handle->stdin_ev);
  173. }
  174. if (handle->stdout_ev) {
  175. event_free(handle->stdout_ev);
  176. }
  177. if (handle->stderr_ev) {
  178. event_free(handle->stderr_ev);
  179. }
  180. if (handle->process) {
  181. tor_process_handle_destroy(handle->process, 1);
  182. }
  183. if (handle->loglabel) {
  184. tor_free(handle->loglabel);
  185. }
  186. if (handle->bodybuf) {
  187. tor_free(handle->bodybuf);
  188. }
  189. tor_free(handle);
  190. *handlep = NULL;
  191. }
  192. void
  193. hs_pirprocess_poke(const char *path, const char *loglabel,
  194. pir_process_msghandler_t msghandler, pir_process_t *handlep)
  195. {
  196. int res;
  197. smartlist_t *env_vars = get_current_process_environment_variables();
  198. const char *argv[2];
  199. process_environment_t *env;
  200. pir_process_t handle;
  201. if (!handlep) return;
  202. if (*handlep) {
  203. handle = *handlep;
  204. if (handle->process &&
  205. handle->process->status == PROCESS_STATUS_RUNNING) {
  206. /* The PIR process appears to be open */
  207. return;
  208. }
  209. }
  210. /* Close everything we've got and start over */
  211. hs_pirprocess_close(handlep);
  212. if (!path) {
  213. /* We don't have a configured PIR server */
  214. return;
  215. }
  216. *handlep = tor_malloc_zero(sizeof(struct pir_process_st));
  217. handle = *handlep;
  218. /* Start the process */
  219. argv[0] = path;
  220. argv[1] = NULL;
  221. env = process_environment_make(env_vars);
  222. res = tor_spawn_background(path, argv, env, &(handle->process));
  223. SMARTLIST_FOREACH(env_vars, void *, x, tor_free(x));
  224. smartlist_free(env_vars);
  225. if (res != PROCESS_STATUS_RUNNING) {
  226. /* Launch failure */
  227. hs_pirprocess_close(handlep);
  228. return;
  229. }
  230. handle->loglabel = tor_strdup(loglabel);
  231. handle->msghandler = msghandler;
  232. /* Create a libevent event to listen to the PIR process' responses. */
  233. handle->stdout_ev = event_new(tor_libevent_get_base(),
  234. handle->process->stdout_pipe, EV_READ|EV_PERSIST,
  235. pirprocess_stdoutcb, handle);
  236. event_add(handle->stdout_ev, NULL);
  237. /* And one to listen to the PIR server's stderr. */
  238. handle->stderr_ev = event_new(tor_libevent_get_base(),
  239. handle->process->stderr_pipe, EV_READ|EV_PERSIST,
  240. pirprocess_stderrcb, handle);
  241. event_add(handle->stderr_ev, NULL);
  242. /* And one for writability to the pir process' stdin, but don't add
  243. * it just yet. Also create the buffer it will use. */
  244. handle->stdin_buf = buf_new();
  245. handle->stderr_buf = buf_new();
  246. handle->stdin_ev = event_new(tor_libevent_get_base(),
  247. handle->process->stdin_pipe, EV_WRITE, pirprocess_stdincb,
  248. handle);
  249. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  250. handle->readoff = 0;
  251. handle->readleft = PIRPROCESS_HDR_SIZE;
  252. handle->bodybuf = NULL;
  253. }
  254. int
  255. hs_pirprocess_send(pir_process_t handle, const unsigned char *buf,
  256. size_t len)
  257. {
  258. if (handle->process == NULL ||
  259. handle->process->status != PROCESS_STATUS_RUNNING) {
  260. /* We don't have a process to talk to */
  261. return -1;
  262. }
  263. /* Write the data to the stdin buffer */
  264. if (len > 0) {
  265. buf_add(handle->stdin_buf, (const char *)buf, len);
  266. event_add(handle->stdin_ev, NULL);
  267. }
  268. return len;
  269. }