snac.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  3. #define XS_IMPLEMENTATION
  4. #include "xs.h"
  5. #include "xs_hex.h"
  6. #include "xs_io.h"
  7. #include "xs_unicode_tbl.h"
  8. #include "xs_unicode.h"
  9. #include "xs_json.h"
  10. #include "xs_curl.h"
  11. #include "xs_openssl.h"
  12. #include "xs_socket.h"
  13. #include "xs_unix_socket.h"
  14. #include "xs_url.h"
  15. #include "xs_httpd.h"
  16. #include "xs_mime.h"
  17. #include "xs_regex.h"
  18. #include "xs_set.h"
  19. #include "xs_time.h"
  20. #include "xs_glob.h"
  21. #include "xs_random.h"
  22. #include "xs_match.h"
  23. #include "xs_fcgi.h"
  24. #include "xs_html.h"
  25. #include "xs_po.h"
  26. #include "xs_webmention.h"
  27. #include "snac.h"
  28. #include <sys/time.h>
  29. #include <sys/stat.h>
  30. xs_str *srv_basedir = NULL;
  31. xs_dict *srv_config = NULL;
  32. xs_str *srv_baseurl = NULL;
  33. xs_str *srv_proxy_token_seed = NULL;
  34. xs_dict *srv_langs = NULL;
  35. int dbglevel = 0;
  36. int mkdirx(const char *pathname)
  37. /* creates a directory with special permissions */
  38. {
  39. int ret;
  40. if ((ret = mkdir(pathname, DIR_PERM)) != -1) {
  41. /* try to the set the setgid bit, to allow system users
  42. to create files in these directories using the
  43. command-line tool. This may fail in some restricted
  44. environments, but it's of no use there anyway */
  45. chmod(pathname, DIR_PERM_ADD);
  46. }
  47. return ret;
  48. }
  49. int valid_status(int status)
  50. /* is this HTTP status valid? */
  51. {
  52. return status >= 200 && status <= 299;
  53. }
  54. xs_str *tid(int offset)
  55. /* returns a time-based Id */
  56. {
  57. struct timeval tv;
  58. gettimeofday(&tv, NULL);
  59. return xs_fmt("%010ld.%06ld", (long)tv.tv_sec + (long)offset, (long)tv.tv_usec);
  60. }
  61. double ftime(void)
  62. /* returns the UNIX time as a float */
  63. {
  64. xs *ntid = tid(0);
  65. return atof(ntid);
  66. }
  67. int validate_uid(const char *uid)
  68. /* returns if uid is a valid identifier */
  69. {
  70. if (!uid || *uid == '\0')
  71. return 0;
  72. while (*uid) {
  73. if (!(isalnum(*uid) || *uid == '_'))
  74. return 0;
  75. uid++;
  76. }
  77. return 1;
  78. }
  79. void srv_log(xs_str *str)
  80. /* logs a debug message */
  81. {
  82. if (xs_str_in(str, srv_basedir) != -1) {
  83. /* replace basedir with ~ */
  84. str = xs_replace_i(str, srv_basedir, "~");
  85. }
  86. xs *tm = xs_str_localtime(0, "%H:%M:%S");
  87. fprintf(stderr, "%s %s\n", tm, str);
  88. /* if the ~/log/ folder exists, also write to a file there */
  89. xs *dt = xs_str_localtime(0, "%Y-%m-%d");
  90. xs *lf = xs_fmt("%s/log/%s.log", srv_basedir, dt);
  91. FILE *f;
  92. if ((f = fopen(lf, "a")) != NULL) {
  93. fprintf(f, "%s %s\n", tm, str);
  94. fclose(f);
  95. }
  96. xs_free(str);
  97. }
  98. void snac_log(snac *snac, xs_str *str)
  99. /* prints a user debugging information */
  100. {
  101. xs *o_str = str;
  102. xs_str *msg = xs_fmt("[%s] %s", snac->uid, o_str);
  103. if (xs_str_in(msg, snac->basedir) != -1) {
  104. /* replace long basedir references with ~ */
  105. msg = xs_replace_i(msg, snac->basedir, "~");
  106. }
  107. srv_log(msg);
  108. }
  109. xs_str *hash_password(const char *uid, const char *passwd, const char *nonce)
  110. /* hashes a password */
  111. {
  112. xs *d_nonce = NULL;
  113. xs *combi;
  114. xs *hash;
  115. if (nonce == NULL) {
  116. unsigned int r;
  117. xs_rnd_buf(&r, sizeof(r));
  118. d_nonce = xs_fmt("%08x", r);
  119. nonce = d_nonce;
  120. }
  121. combi = xs_fmt("%s:%s:%s", nonce, uid, passwd);
  122. hash = xs_sha1_hex(combi, strlen(combi));
  123. return xs_fmt("%s:%s", nonce, hash);
  124. }
  125. int check_password(const char *uid, const char *passwd, const char *hash)
  126. /* checks a password */
  127. {
  128. int ret = 0;
  129. xs *spl = xs_split_n(hash, ":", 1);
  130. if (xs_list_len(spl) == 2) {
  131. xs *n_hash = hash_password(uid, passwd, xs_list_get(spl, 0));
  132. ret = (strcmp(hash, n_hash) == 0);
  133. }
  134. return ret;
  135. }
  136. const char *http_status_text(int status)
  137. /* translate status codes to canonical status texts */
  138. {
  139. switch (status) {
  140. case 599: return "Timeout";
  141. #define HTTP_STATUS(code, name, text) case HTTP_STATUS_ ## name: return #text;
  142. #include "http_codes.h"
  143. #undef HTTP_STATUS
  144. default: return "Unknown";
  145. }
  146. }