httpd.c 16 KB

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