2.70 snac.c
  1/* snac - A simple, minimalistic ActivityPub instance */
  2/* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  3
  4#define XS_IMPLEMENTATION
  5
  6#include "xs.h"
  7#include "xs_hex.h"
  8#include "xs_io.h"
  9#include "xs_unicode_tbl.h"
 10#include "xs_unicode.h"
 11#include "xs_json.h"
 12#include "xs_curl.h"
 13#include "xs_openssl.h"
 14#include "xs_socket.h"
 15#include "xs_unix_socket.h"
 16#include "xs_url.h"
 17#include "xs_httpd.h"
 18#include "xs_mime.h"
 19#include "xs_regex.h"
 20#include "xs_set.h"
 21#include "xs_time.h"
 22#include "xs_glob.h"
 23#include "xs_random.h"
 24#include "xs_match.h"
 25#include "xs_fcgi.h"
 26#include "xs_html.h"
 27
 28#include "snac.h"
 29
 30#include <sys/time.h>
 31#include <sys/stat.h>
 32
 33xs_str *srv_basedir = NULL;
 34xs_dict *srv_config = NULL;
 35xs_str *srv_baseurl = NULL;
 36xs_str *srv_proxy_token_seed = NULL;
 37
 38int dbglevel = 0;
 39
 40
 41int mkdirx(const char *pathname)
 42/* creates a directory with special permissions */
 43{
 44    int ret;
 45
 46    if ((ret = mkdir(pathname, DIR_PERM)) != -1) {
 47        /* try to the set the setgid bit, to allow system users
 48           to create files in these directories using the
 49           command-line tool. This may fail in some restricted
 50           environments, but it's of no use there anyway */
 51        chmod(pathname, DIR_PERM_ADD);
 52    }
 53
 54    return ret;
 55}
 56
 57
 58int valid_status(int status)
 59/* is this HTTP status valid? */
 60{
 61    return status >= 200 && status <= 299;
 62}
 63
 64
 65xs_str *tid(int offset)
 66/* returns a time-based Id */
 67{
 68    struct timeval tv;
 69
 70    gettimeofday(&tv, NULL);
 71
 72    return xs_fmt("%10d.%06d", tv.tv_sec + offset, tv.tv_usec);
 73}
 74
 75
 76double ftime(void)
 77/* returns the UNIX time as a float */
 78{
 79    xs *ntid = tid(0);
 80
 81    return atof(ntid);
 82}
 83
 84
 85int validate_uid(const char *uid)
 86/* returns if uid is a valid identifier */
 87{
 88    if (!uid || *uid == '\0')
 89        return 0;
 90
 91    while (*uid) {
 92        if (!(isalnum(*uid) || *uid == '_'))
 93            return 0;
 94
 95        uid++;
 96    }
 97
 98    return 1;
 99}
100
101
102void srv_log(xs_str *str)
103/* logs a debug message */
104{
105    if (xs_str_in(str, srv_basedir) != -1) {
106        /* replace basedir with ~ */
107        str = xs_replace_i(str, srv_basedir, "~");
108    }
109
110    xs *tm = xs_str_localtime(0, "%H:%M:%S");
111    fprintf(stderr, "%s %s\n", tm, str);
112
113    /* if the ~/log/ folder exists, also write to a file there */
114    xs *dt = xs_str_localtime(0, "%Y-%m-%d");
115    xs *lf = xs_fmt("%s/log/%s.log", srv_basedir, dt);
116    FILE *f;
117    if ((f = fopen(lf, "a")) != NULL) {
118        fprintf(f, "%s %s\n", tm, str);
119        fclose(f);
120    }
121
122    xs_free(str);
123}
124
125
126void snac_log(snac *snac, xs_str *str)
127/* prints a user debugging information */
128{
129    xs *o_str = str;
130    xs_str *msg = xs_fmt("[%s] %s", snac->uid, o_str);
131
132    if (xs_str_in(msg, snac->basedir) != -1) {
133        /* replace long basedir references with ~ */
134        msg = xs_replace_i(msg, snac->basedir, "~");
135    }
136
137    srv_log(msg);
138}
139
140
141xs_str *hash_password(const char *uid, const char *passwd, const char *nonce)
142/* hashes a password */
143{
144    xs *d_nonce = NULL;
145    xs *combi;
146    xs *hash;
147
148    if (nonce == NULL) {
149        unsigned int r;
150        xs_rnd_buf(&r, sizeof(r));
151        d_nonce = xs_fmt("%08x", r);
152        nonce = d_nonce;
153    }
154
155    combi = xs_fmt("%s:%s:%s", nonce, uid, passwd);
156    hash  = xs_sha1_hex(combi, strlen(combi));
157
158    return xs_fmt("%s:%s", nonce, hash);
159}
160
161
162int check_password(const char *uid, const char *passwd, const char *hash)
163/* checks a password */
164{
165    int ret = 0;
166    xs *spl = xs_split_n(hash, ":", 1);
167
168    if (xs_list_len(spl) == 2) {
169        xs *n_hash = hash_password(uid, passwd, xs_list_get(spl, 0));
170
171        ret = (strcmp(hash, n_hash) == 0);
172    }
173
174    return ret;
175}
176
177
178const char *http_status_text(int status)
179/* translate status codes to canonical status texts */
180{
181    switch (status) {
182#define HTTP_STATUS(code, name, text) case HTTP_STATUS_ ## name: return #text;
183#include "http_codes.h"
184#undef HTTP_STATUS
185        default: return "Unknown";
186    }
187}