snac.c 3.5 KB

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