snac.c 3.5 KB

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