httpd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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_archive_error("unattended_method", "unattended method", req, payload);
  193. srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
  194. status = 404;
  195. }
  196. if (status == 404)
  197. body = xs_str_new("<h1>404 Not Found</h1>");
  198. if (status == 400 && body != NULL)
  199. body = xs_str_new("<h1>400 Bad Request</h1>");
  200. if (status == 303)
  201. headers = xs_dict_append(headers, "location", body);
  202. if (status == 401) {
  203. xs *www_auth = xs_fmt("Basic realm=\"@%s@%s snac login\"",
  204. body, xs_dict_get(srv_config, "host"));
  205. headers = xs_dict_append(headers, "WWW-Authenticate", www_auth);
  206. }
  207. if (ctype == NULL)
  208. ctype = "text/html; charset=utf-8";
  209. headers = xs_dict_append(headers, "content-type", ctype);
  210. headers = xs_dict_append(headers, "x-creator", USER_AGENT);
  211. if (!xs_is_null(etag))
  212. headers = xs_dict_append(headers, "etag", etag);
  213. if (b_size == 0 && body != NULL)
  214. b_size = strlen(body);
  215. /* if it was a HEAD, no body will be sent */
  216. if (strcmp(method, "HEAD") == 0)
  217. body = xs_free(body);
  218. xs_httpd_response(f, status, headers, body, b_size);
  219. fclose(f);
  220. srv_archive("RECV", NULL, req, payload, p_size, status, headers, body, b_size);
  221. /* JSON validation check */
  222. if (strcmp(ctype, "application/json") == 0) {
  223. xs *j = xs_json_loads(body);
  224. if (j == NULL) {
  225. srv_log(xs_fmt("bad JSON"));
  226. srv_archive_error("bad_json", "bad JSON", req, body);
  227. }
  228. }
  229. xs_free(body);
  230. }
  231. static jmp_buf on_break;
  232. void term_handler(int s)
  233. {
  234. (void)s;
  235. longjmp(on_break, 1);
  236. }
  237. /** job control **/
  238. /* mutex to access the lists of jobs */
  239. static pthread_mutex_t job_mutex;
  240. /* semaphre to trigger job processing */
  241. static sem_t *job_sem;
  242. /* fifo of jobs */
  243. xs_list *job_fifo = NULL;
  244. int job_fifo_ready(void)
  245. /* returns true if the job fifo is ready */
  246. {
  247. return job_fifo != NULL;
  248. }
  249. void job_post(const xs_val *job, int urgent)
  250. /* posts a job for the threads to process it */
  251. {
  252. if (job != NULL) {
  253. /* lock the mutex */
  254. pthread_mutex_lock(&job_mutex);
  255. /* add to the fifo */
  256. if (job_fifo != NULL) {
  257. if (urgent)
  258. job_fifo = xs_list_insert(job_fifo, 0, job);
  259. else
  260. job_fifo = xs_list_append(job_fifo, job);
  261. }
  262. /* unlock the mutex */
  263. pthread_mutex_unlock(&job_mutex);
  264. }
  265. /* ask for someone to attend it */
  266. sem_post(job_sem);
  267. }
  268. void job_wait(xs_val **job)
  269. /* waits for an available job */
  270. {
  271. *job = NULL;
  272. if (sem_wait(job_sem) == 0) {
  273. /* lock the mutex */
  274. pthread_mutex_lock(&job_mutex);
  275. /* dequeue */
  276. if (job_fifo != NULL)
  277. job_fifo = xs_list_shift(job_fifo, job);
  278. /* unlock the mutex */
  279. pthread_mutex_unlock(&job_mutex);
  280. }
  281. }
  282. #ifndef MAX_THREADS
  283. #define MAX_THREADS 256
  284. #endif
  285. static void *job_thread(void *arg)
  286. /* job thread */
  287. {
  288. int pid = (int)(uintptr_t)arg;
  289. srv_debug(1, xs_fmt("job thread %d started", pid));
  290. for (;;) {
  291. xs *job = NULL;
  292. job_wait(&job);
  293. srv_debug(2, xs_fmt("job thread %d wake up", pid));
  294. if (job == NULL)
  295. break;
  296. if (xs_type(job) == XSTYPE_DATA) {
  297. /* it's a socket */
  298. FILE *f = NULL;
  299. xs_data_get(job, &f);
  300. if (f != NULL)
  301. httpd_connection(f);
  302. }
  303. else {
  304. /* it's a q_item */
  305. process_queue_item(job);
  306. }
  307. }
  308. srv_debug(1, xs_fmt("job thread %d stopped", pid));
  309. return NULL;
  310. }
  311. /* background thread sleep control */
  312. static pthread_mutex_t sleep_mutex;
  313. static pthread_cond_t sleep_cond;
  314. static void *background_thread(void *arg)
  315. /* background thread (queue management and other things) */
  316. {
  317. time_t purge_time;
  318. (void)arg;
  319. /* first purge time */
  320. purge_time = time(NULL) + 10 * 60;
  321. srv_log(xs_fmt("background thread started"));
  322. while (srv_running) {
  323. time_t t;
  324. int cnt = 0;
  325. {
  326. xs *list = user_list();
  327. char *p, *uid;
  328. /* process queues for all users */
  329. p = list;
  330. while (xs_list_iter(&p, &uid)) {
  331. snac snac;
  332. if (user_open(&snac, uid)) {
  333. cnt += process_user_queue(&snac);
  334. user_free(&snac);
  335. }
  336. }
  337. }
  338. /* global queue */
  339. cnt += process_queue();
  340. /* time to purge? */
  341. if ((t = time(NULL)) > purge_time) {
  342. /* next purge time is tomorrow */
  343. purge_time = t + 24 * 60 * 60;
  344. xs *q_item = xs_dict_new();
  345. q_item = xs_dict_append(q_item, "type", "purge");
  346. job_post(q_item, 0);
  347. }
  348. if (cnt == 0) {
  349. /* sleep 3 seconds */
  350. #ifdef USE_POLL_FOR_SLEEP
  351. poll(NULL, 0, 3 * 1000);
  352. #else
  353. struct timespec ts;
  354. clock_gettime(CLOCK_REALTIME, &ts);
  355. ts.tv_sec += 3;
  356. pthread_mutex_lock(&sleep_mutex);
  357. while (pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &ts) == 0);
  358. pthread_mutex_unlock(&sleep_mutex);
  359. #endif
  360. }
  361. }
  362. srv_log(xs_fmt("background thread stopped"));
  363. return NULL;
  364. }
  365. void httpd(void)
  366. /* starts the server */
  367. {
  368. char *address;
  369. int port;
  370. int rs;
  371. pthread_t threads[MAX_THREADS] = {0};
  372. int n_threads = 0;
  373. int n;
  374. time_t start_time = time(NULL);
  375. char sem_name[24];
  376. sem_t anon_job_sem;
  377. address = xs_dict_get(srv_config, "address");
  378. port = xs_number_get(xs_dict_get(srv_config, "port"));
  379. if ((rs = xs_socket_server(address, port)) == -1) {
  380. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  381. return;
  382. }
  383. srv_running = 1;
  384. signal(SIGPIPE, SIG_IGN);
  385. signal(SIGTERM, term_handler);
  386. signal(SIGINT, term_handler);
  387. srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT));
  388. /* show the number of usable file descriptors */
  389. struct rlimit r;
  390. getrlimit(RLIMIT_NOFILE, &r);
  391. srv_debug(0, xs_fmt("available (rlimit) fds: %d (cur) / %d (max)",
  392. (int) r.rlim_cur, (int) r.rlim_max));
  393. /* initialize the job control engine */
  394. pthread_mutex_init(&job_mutex, NULL);
  395. snprintf(sem_name, sizeof(sem_name), "/job_%d", getpid());
  396. job_sem = sem_open(sem_name, O_CREAT, 0644, 0);
  397. if (job_sem == NULL) {
  398. /* error opening a named semaphore; try with an anonymous one */
  399. if (sem_init(&anon_job_sem, 0, 0) != -1)
  400. job_sem = &anon_job_sem;
  401. }
  402. if (job_sem == NULL) {
  403. srv_log(xs_fmt("fatal error: cannot create semaphore -- cannot continue"));
  404. return;
  405. }
  406. job_fifo = xs_list_new();
  407. /* initialize sleep control */
  408. pthread_mutex_init(&sleep_mutex, NULL);
  409. pthread_cond_init(&sleep_cond, NULL);
  410. n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
  411. #ifdef _SC_NPROCESSORS_ONLN
  412. if (n_threads == 0) {
  413. /* get number of CPUs on the machine */
  414. n_threads = sysconf(_SC_NPROCESSORS_ONLN);
  415. }
  416. #endif
  417. if (n_threads < 4)
  418. n_threads = 4;
  419. if (n_threads > MAX_THREADS)
  420. n_threads = MAX_THREADS;
  421. srv_debug(0, xs_fmt("using %d threads", n_threads));
  422. /* thread #0 is the background thread */
  423. pthread_create(&threads[0], NULL, background_thread, NULL);
  424. /* the rest of threads are for job processing */
  425. char *ptr = (char *) 0x1;
  426. for (n = 1; n < n_threads; n++)
  427. pthread_create(&threads[n], NULL, job_thread, ptr++);
  428. if (setjmp(on_break) == 0) {
  429. for (;;) {
  430. FILE *f = xs_socket_accept(rs);
  431. if (f != NULL) {
  432. xs *job = xs_data_new(&f, sizeof(FILE *));
  433. job_post(job, 1);
  434. }
  435. else
  436. break;
  437. }
  438. }
  439. srv_running = 0;
  440. /* send as many empty jobs as working threads */
  441. for (n = 1; n < n_threads; n++)
  442. job_post(NULL, 0);
  443. /* wait for all the threads to exit */
  444. for (n = 0; n < n_threads; n++)
  445. pthread_join(threads[n], NULL);
  446. pthread_mutex_lock(&job_mutex);
  447. job_fifo = xs_free(job_fifo);
  448. pthread_mutex_unlock(&job_mutex);
  449. sem_close(job_sem);
  450. sem_unlink(sem_name);
  451. xs *uptime = xs_str_time_diff(time(NULL) - start_time);
  452. srv_log(xs_fmt("httpd stop %s:%d (run time: %s)", address, port, uptime));
  453. }