snac.c 3.5 KB

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