httpd.c 17 KB

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