snac.c 5.3 KB

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