snac.c 5.3 KB

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