snac.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #define XS_IMPLEMENTATION
  4. #include "xs.h"
  5. #include "xs_io.h"
  6. #include "xs_encdec.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 "snac.h"
  13. #include <sys/time.h>
  14. d_char *srv_basedir = NULL;
  15. d_char *srv_config = NULL;
  16. d_char *srv_baseurl = NULL;
  17. int srv_running = 0;
  18. int dbglevel = 0;
  19. d_char *xs_time(char *fmt, int local)
  20. /* returns a d_char with a formated time */
  21. {
  22. time_t t = time(NULL);
  23. struct tm tm;
  24. char tmp[64];
  25. if (local)
  26. localtime_r(&t, &tm);
  27. else
  28. gmtime_r(&t, &tm);
  29. strftime(tmp, sizeof(tmp), fmt, &tm);
  30. return xs_str_new(tmp);
  31. }
  32. d_char *tid(int offset)
  33. /* returns a time-based Id */
  34. {
  35. struct timeval tv;
  36. struct timezone tz;
  37. gettimeofday(&tv, &tz);
  38. return xs_fmt("%10d.%06d", tv.tv_sec + offset, tv.tv_usec);
  39. }
  40. void srv_debug(int level, d_char *str)
  41. /* logs a debug message */
  42. {
  43. xs *msg = str;
  44. if (xs_str_in(msg, srv_basedir) != -1) {
  45. /* replace basedir with ~ */
  46. xs *o_str = msg;
  47. msg = xs_replace(o_str, srv_basedir, "~");
  48. }
  49. if (dbglevel >= level) {
  50. xs *tm = xs_local_time("%H:%M:%S");
  51. fprintf(stderr, "%s %s\n", tm, msg);
  52. }
  53. }
  54. int validate_uid(char *uid)
  55. /* returns if uid is a valid identifier */
  56. {
  57. while (*uid) {
  58. if (!(isalnum(*uid) || *uid == '_'))
  59. return 0;
  60. uid++;
  61. }
  62. return 1;
  63. }
  64. void snac_debug(snac *snac, int level, d_char *str)
  65. /* prints a user debugging information */
  66. {
  67. xs *o_str = str;
  68. d_char *msg = xs_fmt("[%s] %s", snac->uid, o_str);
  69. if (xs_str_in(msg, snac->basedir) != -1) {
  70. /* replace long basedir references with ~ */
  71. xs *o_str = msg;
  72. msg = xs_replace(o_str, snac->basedir, "~");
  73. }
  74. srv_debug(level, msg);
  75. }
  76. d_char *hash_password(char *uid, char *passwd, char *nonce)
  77. /* hashes a password */
  78. {
  79. xs *d_nonce = NULL;
  80. xs *combi;
  81. xs *hash;
  82. if (nonce == NULL)
  83. nonce = d_nonce = xs_fmt("%08x", random());
  84. combi = xs_fmt("%s:%s:%s", nonce, uid, passwd);
  85. hash = xs_sha1_hex(combi, strlen(combi));
  86. return xs_fmt("%s:%s", nonce, hash);
  87. }
  88. int check_password(char *uid, char *passwd, char *hash)
  89. /* checks a password */
  90. {
  91. int ret = 0;
  92. xs *spl = xs_split_n(hash, ":", 1);
  93. if (xs_list_len(spl) == 2) {
  94. xs *n_hash = hash_password(uid, passwd, xs_list_get(spl, 0));
  95. ret = (strcmp(hash, n_hash) == 0);
  96. }
  97. return ret;
  98. }