snac.c 5.4 KB

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