snac.c 5.2 KB

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