snac.c 3.1 KB

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