httpd.c 19 KB

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