httpd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "xs_socket.h"
  7. #include "xs_httpd.h"
  8. #include "xs_mime.h"
  9. #include "xs_time.h"
  10. #include "xs_openssl.h"
  11. #include "snac.h"
  12. #include <setjmp.h>
  13. #include <pthread.h>
  14. #include <semaphore.h>
  15. #include <fcntl.h>
  16. #include <stdint.h>
  17. #include <sys/resource.h> // for getrlimit()
  18. #ifdef USE_POLL_FOR_SLEEP
  19. #include <poll.h>
  20. #endif
  21. int srv_running = 0;
  22. /* nodeinfo 2.0 template */
  23. const char *nodeinfo_2_0_template = ""
  24. "{\"version\":\"2.0\","
  25. "\"software\":{\"name\":\"snac\",\"version\":\"" VERSION "\"},"
  26. "\"protocols\":[\"activitypub\"],"
  27. "\"services\":{\"outbound\":[],\"inbound\":[]},"
  28. "\"usage\":{\"users\":{\"total\":%d,\"activeMonth\":%d,\"activeHalfyear\":%d},"
  29. "\"localPosts\":%d},"
  30. "\"openRegistrations\":false,\"metadata\":{}}";
  31. d_char *nodeinfo_2_0(void)
  32. /* builds a nodeinfo json object */
  33. {
  34. xs *users = user_list();
  35. int n_users = xs_list_len(users);
  36. int n_posts = 0; /* to be implemented someday */
  37. return xs_fmt(nodeinfo_2_0_template, n_users, n_users, n_users, n_posts);
  38. }
  39. int server_get_handler(xs_dict *req, char *q_path,
  40. char **body, int *b_size, char **ctype)
  41. /* basic server services */
  42. {
  43. int status = 0;
  44. (void)req;
  45. /* is it the server root? */
  46. if (*q_path == '\0') {
  47. /* try to open greeting.html */
  48. xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
  49. FILE *f;
  50. if ((f = fopen(fn, "r")) != NULL) {
  51. d_char *s = xs_readall(f);
  52. fclose(f);
  53. status = 200;
  54. /* replace %host% */
  55. s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host"));
  56. const char *adm_email = xs_dict_get(srv_config, "admin_email");
  57. if (xs_is_null(adm_email) || *adm_email == '\0')
  58. adm_email = "the administrator of this instance";
  59. /* replace %admin_email */
  60. s = xs_replace_i(s, "%admin_email%", adm_email);
  61. /* does it have a %userlist% mark? */
  62. if (xs_str_in(s, "%userlist%") != -1) {
  63. char *host = xs_dict_get(srv_config, "host");
  64. xs *list = user_list();
  65. char *p, *uid;
  66. xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n");
  67. p = list;
  68. while (xs_list_iter(&p, &uid)) {
  69. snac snac;
  70. if (user_open(&snac, uid)) {
  71. xs *u = xs_fmt(
  72. "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n",
  73. snac.actor, uid, host,
  74. xs_dict_get(snac.config, "name"));
  75. ul = xs_str_cat(ul, u);
  76. user_free(&snac);
  77. }
  78. }
  79. ul = xs_str_cat(ul, "</ul>\n");
  80. s = xs_replace_i(s, "%userlist%", ul);
  81. }
  82. *body = s;
  83. }
  84. }
  85. else
  86. if (strcmp(q_path, "/susie.png") == 0 || strcmp(q_path, "/favicon.ico") == 0 ) {
  87. status = 200;
  88. *body = xs_base64_dec(default_avatar_base64(), b_size);
  89. *ctype = "image/png";
  90. }
  91. else
  92. if (strcmp(q_path, "/.well-known/nodeinfo") == 0) {
  93. status = 200;
  94. *ctype = "application/json; charset=utf-8";
  95. *body = xs_fmt("{\"links\":["
  96. "{\"rel\":\"http:/" "/nodeinfo.diaspora.software/ns/schema/2.0\","
  97. "\"href\":\"%s/nodeinfo_2_0\"}]}",
  98. srv_baseurl);
  99. }
  100. else
  101. if (strcmp(q_path, "/nodeinfo_2_0") == 0) {
  102. status = 200;
  103. *ctype = "application/json; charset=utf-8";
  104. *body = nodeinfo_2_0();
  105. }
  106. else
  107. if (strcmp(q_path, "/robots.txt") == 0) {
  108. status = 200;
  109. *ctype = "text/plain";
  110. *body = xs_str_new("User-agent: *\n"
  111. "Disallow: /\n");
  112. }
  113. if (status != 0)
  114. srv_debug(1, xs_fmt("server_get_handler serving '%s' %d", q_path, status));
  115. return status;
  116. }
  117. void httpd_connection(FILE *f)
  118. /* the connection processor */
  119. {
  120. xs *req;
  121. char *method;
  122. int status = 0;
  123. d_char *body = NULL;
  124. int b_size = 0;
  125. char *ctype = NULL;
  126. xs *headers = NULL;
  127. xs *q_path = NULL;
  128. xs *payload = NULL;
  129. xs *etag = NULL;
  130. int p_size = 0;
  131. char *p;
  132. req = xs_httpd_request(f, &payload, &p_size);
  133. if (req == NULL) {
  134. /* probably because a timeout */
  135. fclose(f);
  136. return;
  137. }
  138. method = xs_dict_get(req, "method");
  139. q_path = xs_dup(xs_dict_get(req, "path"));
  140. /* crop the q_path from leading / and the prefix */
  141. if (xs_endswith(q_path, "/"))
  142. q_path = xs_crop_i(q_path, 0, -1);
  143. p = xs_dict_get(srv_config, "prefix");
  144. if (xs_startswith(q_path, p))
  145. q_path = xs_crop_i(q_path, strlen(p), 0);
  146. if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) {
  147. /* cascade through */
  148. if (status == 0)
  149. status = server_get_handler(req, q_path, &body, &b_size, &ctype);
  150. if (status == 0)
  151. status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
  152. if (status == 0)
  153. status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
  154. #ifndef NO_MASTODON_API
  155. if (status == 0)
  156. status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
  157. if (status == 0)
  158. status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype);
  159. #endif /* NO_MASTODON_API */
  160. if (status == 0)
  161. status = html_get_handler(req, q_path, &body, &b_size, &ctype, &etag);
  162. }
  163. else
  164. if (strcmp(method, "POST") == 0) {
  165. #ifndef NO_MASTODON_API
  166. if (status == 0)
  167. status = oauth_post_handler(req, q_path,
  168. payload, p_size, &body, &b_size, &ctype);
  169. if (status == 0)
  170. status = mastoapi_post_handler(req, q_path,
  171. payload, p_size, &body, &b_size, &ctype);
  172. #endif
  173. if (status == 0)
  174. status = activitypub_post_handler(req, q_path,
  175. payload, p_size, &body, &b_size, &ctype);
  176. if (status == 0)
  177. status = html_post_handler(req, q_path,
  178. payload, p_size, &body, &b_size, &ctype);
  179. }
  180. else
  181. if (strcmp(method, "PUT") == 0) {
  182. #ifndef NO_MASTODON_API
  183. if (status == 0)
  184. status = mastoapi_put_handler(req, q_path,
  185. payload, p_size, &body, &b_size, &ctype);
  186. #endif
  187. }
  188. /* let's go */
  189. headers = xs_dict_new();
  190. /* unattended? it's an error */
  191. if (status == 0) {
  192. srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
  193. status = 404;
  194. }
  195. if (status == 404)
  196. body = xs_str_new("<h1>404 Not Found</h1>");
  197. if (status == 400 && body != NULL)
  198. body = xs_str_new("<h1>400 Bad Request</h1>");
  199. if (status == 303)
  200. headers = xs_dict_append(headers, "location", body);
  201. if (status == 401) {
  202. xs *www_auth = xs_fmt("Basic realm=\"@%s@%s snac login\"",
  203. body, xs_dict_get(srv_config, "host"));
  204. headers = xs_dict_append(headers, "WWW-Authenticate", www_auth);
  205. }
  206. if (ctype == NULL)
  207. ctype = "text/html; charset=utf-8";
  208. headers = xs_dict_append(headers, "content-type", ctype);
  209. headers = xs_dict_append(headers, "x-creator", USER_AGENT);
  210. if (!xs_is_null(etag))
  211. headers = xs_dict_append(headers, "etag", etag);
  212. if (b_size == 0 && body != NULL)
  213. b_size = strlen(body);
  214. /* if it was a HEAD, no body will be sent */
  215. if (strcmp(method, "HEAD") == 0)
  216. body = xs_free(body);
  217. xs_httpd_response(f, status, headers, body, b_size);
  218. fclose(f);
  219. srv_archive("RECV", NULL, req, payload, p_size, status, headers, body, b_size);
  220. /* JSON validation check */
  221. if (strcmp(ctype, "application/json") == 0) {
  222. xs *j = xs_json_loads(body);
  223. if (j == NULL) {
  224. srv_log(xs_fmt("bad JSON"));
  225. srv_archive_error("bad_json", "bad JSON", req, body);
  226. }
  227. }
  228. xs_free(body);
  229. }
  230. static jmp_buf on_break;
  231. void term_handler(int s)
  232. {
  233. (void)s;
  234. longjmp(on_break, 1);
  235. }
  236. /** job control **/
  237. /* mutex to access the lists of jobs */
  238. static pthread_mutex_t job_mutex;
  239. /* semaphre to trigger job processing */
  240. static sem_t *job_sem;
  241. /* fifo of jobs */
  242. xs_list *job_fifo = NULL;
  243. int job_fifo_ready(void)
  244. /* returns true if the job fifo is ready */
  245. {
  246. return job_fifo != NULL;
  247. }
  248. void job_post(const xs_val *job, int urgent)
  249. /* posts a job for the threads to process it */
  250. {
  251. if (job != NULL) {
  252. /* lock the mutex */
  253. pthread_mutex_lock(&job_mutex);
  254. /* add to the fifo */
  255. if (job_fifo != NULL) {
  256. if (urgent)
  257. job_fifo = xs_list_insert(job_fifo, 0, job);
  258. else
  259. job_fifo = xs_list_append(job_fifo, job);
  260. }
  261. /* unlock the mutex */
  262. pthread_mutex_unlock(&job_mutex);
  263. }
  264. /* ask for someone to attend it */
  265. sem_post(job_sem);
  266. }
  267. void job_wait(xs_val **job)
  268. /* waits for an available job */
  269. {
  270. *job = NULL;
  271. if (sem_wait(job_sem) == 0) {
  272. /* lock the mutex */
  273. pthread_mutex_lock(&job_mutex);
  274. /* dequeue */
  275. if (job_fifo != NULL)
  276. job_fifo = xs_list_shift(job_fifo, job);
  277. /* unlock the mutex */
  278. pthread_mutex_unlock(&job_mutex);
  279. }
  280. }
  281. #ifndef MAX_THREADS
  282. #define MAX_THREADS 256
  283. #endif
  284. static void *job_thread(void *arg)
  285. /* job thread */
  286. {
  287. int pid = (int)(uintptr_t)arg;
  288. srv_debug(1, xs_fmt("job thread %d started", pid));
  289. for (;;) {
  290. xs *job = NULL;
  291. job_wait(&job);
  292. srv_debug(2, xs_fmt("job thread %d wake up", pid));
  293. if (job == NULL)
  294. break;
  295. if (xs_type(job) == XSTYPE_DATA) {
  296. /* it's a socket */
  297. FILE *f = NULL;
  298. xs_data_get(job, &f);
  299. if (f != NULL)
  300. httpd_connection(f);
  301. }
  302. else {
  303. /* it's a q_item */
  304. process_queue_item(job);
  305. }
  306. }
  307. srv_debug(1, xs_fmt("job thread %d stopped", pid));
  308. return NULL;
  309. }
  310. /* background thread sleep control */
  311. static pthread_mutex_t sleep_mutex;
  312. static pthread_cond_t sleep_cond;
  313. static void *background_thread(void *arg)
  314. /* background thread (queue management and other things) */
  315. {
  316. time_t purge_time;
  317. (void)arg;
  318. /* first purge time */
  319. purge_time = time(NULL) + 10 * 60;
  320. srv_log(xs_fmt("background thread started"));
  321. while (srv_running) {
  322. time_t t;
  323. int cnt = 0;
  324. {
  325. xs *list = user_list();
  326. char *p, *uid;
  327. /* process queues for all users */
  328. p = list;
  329. while (xs_list_iter(&p, &uid)) {
  330. snac snac;
  331. if (user_open(&snac, uid)) {
  332. cnt += process_user_queue(&snac);
  333. user_free(&snac);
  334. }
  335. }
  336. }
  337. /* global queue */
  338. cnt += process_queue();
  339. /* time to purge? */
  340. if ((t = time(NULL)) > purge_time) {
  341. /* next purge time is tomorrow */
  342. purge_time = t + 24 * 60 * 60;
  343. xs *q_item = xs_dict_new();
  344. q_item = xs_dict_append(q_item, "type", "purge");
  345. job_post(q_item, 0);
  346. }
  347. if (cnt == 0) {
  348. /* sleep 3 seconds */
  349. #ifdef USE_POLL_FOR_SLEEP
  350. poll(NULL, 0, 3 * 1000);
  351. #else
  352. struct timespec ts;
  353. clock_gettime(CLOCK_REALTIME, &ts);
  354. ts.tv_sec += 3;
  355. pthread_mutex_lock(&sleep_mutex);
  356. while (pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &ts) == 0);
  357. pthread_mutex_unlock(&sleep_mutex);
  358. #endif
  359. }
  360. }
  361. srv_log(xs_fmt("background thread stopped"));
  362. return NULL;
  363. }
  364. void httpd(void)
  365. /* starts the server */
  366. {
  367. char *address;
  368. int port;
  369. int rs;
  370. pthread_t threads[MAX_THREADS] = {0};
  371. int n_threads = 0;
  372. int n;
  373. time_t start_time = time(NULL);
  374. char sem_name[24];
  375. sem_t anon_job_sem;
  376. address = xs_dict_get(srv_config, "address");
  377. port = xs_number_get(xs_dict_get(srv_config, "port"));
  378. if ((rs = xs_socket_server(address, port)) == -1) {
  379. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  380. return;
  381. }
  382. srv_running = 1;
  383. signal(SIGPIPE, SIG_IGN);
  384. signal(SIGTERM, term_handler);
  385. signal(SIGINT, term_handler);
  386. srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT));
  387. /* show the number of usable file descriptors */
  388. struct rlimit r;
  389. getrlimit(RLIMIT_NOFILE, &r);
  390. srv_debug(0, xs_fmt("available (rlimit) fds: %d (cur) / %d (max)",
  391. (int) r.rlim_cur, (int) r.rlim_max));
  392. /* initialize the job control engine */
  393. pthread_mutex_init(&job_mutex, NULL);
  394. snprintf(sem_name, sizeof(sem_name), "/job_%d", getpid());
  395. job_sem = sem_open(sem_name, O_CREAT, 0644, 0);
  396. if (job_sem == NULL) {
  397. /* error opening a named semaphore; try with an anonymous one */
  398. if (sem_init(&anon_job_sem, 0, 0) != -1)
  399. job_sem = &anon_job_sem;
  400. }
  401. if (job_sem == NULL) {
  402. srv_log(xs_fmt("fatal error: cannot create semaphore -- cannot continue"));
  403. return;
  404. }
  405. job_fifo = xs_list_new();
  406. /* initialize sleep control */
  407. pthread_mutex_init(&sleep_mutex, NULL);
  408. pthread_cond_init(&sleep_cond, NULL);
  409. n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
  410. #ifdef _SC_NPROCESSORS_ONLN
  411. if (n_threads == 0) {
  412. /* get number of CPUs on the machine */
  413. n_threads = sysconf(_SC_NPROCESSORS_ONLN);
  414. }
  415. #endif
  416. if (n_threads < 4)
  417. n_threads = 4;
  418. if (n_threads > MAX_THREADS)
  419. n_threads = MAX_THREADS;
  420. srv_debug(0, xs_fmt("using %d threads", n_threads));
  421. /* thread #0 is the background thread */
  422. pthread_create(&threads[0], NULL, background_thread, NULL);
  423. /* the rest of threads are for job processing */
  424. char *ptr = (char *) 0x1;
  425. for (n = 1; n < n_threads; n++)
  426. pthread_create(&threads[n], NULL, job_thread, ptr++);
  427. if (setjmp(on_break) == 0) {
  428. for (;;) {
  429. FILE *f = xs_socket_accept(rs);
  430. if (f != NULL) {
  431. xs *job = xs_data_new(&f, sizeof(FILE *));
  432. job_post(job, 1);
  433. }
  434. else
  435. break;
  436. }
  437. }
  438. srv_running = 0;
  439. /* send as many empty jobs as working threads */
  440. for (n = 1; n < n_threads; n++)
  441. job_post(NULL, 0);
  442. /* wait for all the threads to exit */
  443. for (n = 0; n < n_threads; n++)
  444. pthread_join(threads[n], NULL);
  445. pthread_mutex_lock(&job_mutex);
  446. job_fifo = xs_free(job_fifo);
  447. pthread_mutex_unlock(&job_mutex);
  448. sem_close(job_sem);
  449. sem_unlink(sem_name);
  450. xs *uptime = xs_str_time_diff(time(NULL) - start_time);
  451. srv_log(xs_fmt("httpd stop %s:%d (run time: %s)", address, port, uptime));
  452. }