snac.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #define XS_IMPLEMENTATION
  4. #include "xs.h"
  5. #include "xs_io.h"
  6. #include "xs_encdec.h"
  7. #include "xs_json.h"
  8. #include "xs_curl.h"
  9. #include "xs_openssl.h"
  10. #include "xs_socket.h"
  11. #include "xs_httpd.h"
  12. #include "xs_mime.h"
  13. #include "xs_regex.h"
  14. #include "xs_set.h"
  15. #include "xs_time.h"
  16. #include "xs_glob.h"
  17. #include "snac.h"
  18. #include <sys/time.h>
  19. #include <sys/stat.h>
  20. d_char *srv_basedir = NULL;
  21. d_char *srv_config = NULL;
  22. d_char *srv_baseurl = NULL;
  23. int srv_running = 0;
  24. int dbglevel = 0;
  25. d_char *tid(int offset)
  26. /* returns a time-based Id */
  27. {
  28. struct timeval tv;
  29. gettimeofday(&tv, NULL);
  30. return xs_fmt("%10d.%06d", tv.tv_sec + offset, tv.tv_usec);
  31. }
  32. double ftime(void)
  33. /* returns the UNIX time as a float */
  34. {
  35. xs *ntid = tid(0);
  36. return atof(ntid);
  37. }
  38. int validate_uid(char *uid)
  39. /* returns if uid is a valid identifier */
  40. {
  41. while (*uid) {
  42. if (!(isalnum(*uid) || *uid == '_'))
  43. return 0;
  44. uid++;
  45. }
  46. return 1;
  47. }
  48. void srv_debug(int level, d_char *str)
  49. /* logs a debug message */
  50. {
  51. xs *msg = str;
  52. if (xs_str_in(msg, srv_basedir) != -1) {
  53. /* replace basedir with ~ */
  54. msg = xs_replace_i(msg, srv_basedir, "~");
  55. }
  56. if (dbglevel >= level) {
  57. xs *tm = xs_str_localtime(0, "%H:%M:%S");
  58. fprintf(stderr, "%s %s\n", tm, msg);
  59. }
  60. }
  61. void snac_debug(snac *snac, int level, d_char *str)
  62. /* prints a user debugging information */
  63. {
  64. xs *o_str = str;
  65. d_char *msg = xs_fmt("[%s] %s", snac->uid, o_str);
  66. if (xs_str_in(msg, snac->basedir) != -1) {
  67. /* replace long basedir references with ~ */
  68. msg = xs_replace_i(msg, snac->basedir, "~");
  69. }
  70. srv_debug(level, msg);
  71. }
  72. d_char *hash_password(char *uid, char *passwd, char *nonce)
  73. /* hashes a password */
  74. {
  75. xs *d_nonce = NULL;
  76. xs *combi;
  77. xs *hash;
  78. if (nonce == NULL)
  79. nonce = d_nonce = xs_fmt("%08x", random());
  80. combi = xs_fmt("%s:%s:%s", nonce, uid, passwd);
  81. hash = xs_sha1_hex(combi, strlen(combi));
  82. return xs_fmt("%s:%s", nonce, hash);
  83. }
  84. int check_password(char *uid, char *passwd, char *hash)
  85. /* checks a password */
  86. {
  87. int ret = 0;
  88. xs *spl = xs_split_n(hash, ":", 1);
  89. if (xs_list_len(spl) == 2) {
  90. xs *n_hash = hash_password(uid, passwd, xs_list_get(spl, 0));
  91. ret = (strcmp(hash, n_hash) == 0);
  92. }
  93. return ret;
  94. }
  95. void srv_archive(char *direction, char *req, char *payload, int p_size,
  96. int status, char *headers, char *body, int b_size)
  97. /* archives a connection */
  98. {
  99. /* obsessive archiving */
  100. xs *date = tid(0);
  101. xs *dir = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
  102. FILE *f;
  103. if (mkdir(dir, 0755) != -1) {
  104. xs *meta_fn = xs_fmt("%s/_META", dir);
  105. if ((f = fopen(meta_fn, "w")) != NULL) {
  106. xs *j1 = xs_json_dumps_pp(req, 4);
  107. xs *j2 = xs_json_dumps_pp(headers, 4);
  108. fprintf(f, "dir: %s\n", direction);
  109. fprintf(f, "req: %s\n", j1);
  110. fprintf(f, "p_size: %d\n", p_size);
  111. fprintf(f, "status: %d\n", status);
  112. fprintf(f, "response: %s\n", j2);
  113. fprintf(f, "b_size: %d\n", b_size);
  114. fclose(f);
  115. }
  116. if (p_size && payload) {
  117. xs *payload_fn = NULL;
  118. xs *payload_fn_raw = NULL;
  119. char *v = xs_dict_get(req, "content-type");
  120. if (v && xs_str_in(v, "json") != -1) {
  121. payload_fn = xs_fmt("%s/payload.json", dir);
  122. if ((f = fopen(payload_fn, "w")) != NULL) {
  123. xs *v1 = xs_json_loads(payload);
  124. xs *j1 = NULL;
  125. if (v1 != NULL)
  126. j1 = xs_json_dumps_pp(v1, 4);
  127. if (j1 != NULL)
  128. fwrite(j1, strlen(j1), 1, f);
  129. else
  130. fwrite(payload, p_size, 1, f);
  131. fclose(f);
  132. }
  133. }
  134. payload_fn_raw = xs_fmt("%s/payload", dir);
  135. if ((f = fopen(payload_fn_raw, "w")) != NULL) {
  136. fwrite(payload, p_size, 1, f);
  137. fclose(f);
  138. }
  139. }
  140. if (b_size && body) {
  141. xs *body_fn = NULL;
  142. char *v = xs_dict_get(headers, "content-type");
  143. if (v && xs_str_in(v, "json") != -1) {
  144. body_fn = xs_fmt("%s/body.json", dir);
  145. if ((f = fopen(body_fn, "w")) != NULL) {
  146. xs *v1 = xs_json_loads(body);
  147. xs *j1 = NULL;
  148. if (v1 != NULL)
  149. j1 = xs_json_dumps_pp(v1, 4);
  150. if (j1 != NULL)
  151. fwrite(j1, strlen(j1), 1, f);
  152. else
  153. fwrite(body, b_size, 1, f);
  154. fclose(f);
  155. }
  156. }
  157. else {
  158. body_fn = xs_fmt("%s/body", dir);
  159. if ((f = fopen(body_fn, "w")) != NULL) {
  160. fwrite(body, b_size, 1, f);
  161. fclose(f);
  162. }
  163. }
  164. }
  165. }
  166. }