httpd.c 18 KB

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