hs_pirprocess.c 8.0 KB

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