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