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