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