-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnac.c
230 lines (173 loc) · 5.31 KB
/
snac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* snac - A simple, minimalistic ActivityPub instance */
/* copyright (c) 2022 grunfink - MIT license */
#define XS_IMPLEMENTATION
#include "xs.h"
#include "xs_io.h"
#include "xs_encdec.h"
#include "xs_json.h"
#include "xs_curl.h"
#include "xs_openssl.h"
#include "xs_socket.h"
#include "xs_httpd.h"
#include "xs_mime.h"
#include "xs_regex.h"
#include "xs_set.h"
#include "xs_time.h"
#include "xs_glob.h"
#include "snac.h"
#include <sys/time.h>
#include <sys/stat.h>
d_char *srv_basedir = NULL;
d_char *srv_config = NULL;
d_char *srv_baseurl = NULL;
int srv_running = 0;
int dbglevel = 0;
int valid_status(int status)
/* is this HTTP status valid? */
{
return status >= 200 && status <= 299;
}
d_char *tid(int offset)
/* returns a time-based Id */
{
struct timeval tv;
gettimeofday(&tv, NULL);
return xs_fmt("%10d.%06d", tv.tv_sec + offset, tv.tv_usec);
}
double ftime(void)
/* returns the UNIX time as a float */
{
xs *ntid = tid(0);
return atof(ntid);
}
int validate_uid(const char *uid)
/* returns if uid is a valid identifier */
{
while (*uid) {
if (!(isalnum(*uid) || *uid == '_'))
return 0;
uid++;
}
return 1;
}
void srv_debug(int level, d_char *str)
/* logs a debug message */
{
if (xs_str_in(str, srv_basedir) != -1) {
/* replace basedir with ~ */
str = xs_replace_i(str, srv_basedir, "~");
}
if (dbglevel >= level) {
xs *tm = xs_str_localtime(0, "%H:%M:%S");
fprintf(stderr, "%s %s\n", tm, str);
}
xs_free(str);
}
void snac_debug(snac *snac, int level, d_char *str)
/* prints a user debugging information */
{
xs *o_str = str;
d_char *msg = xs_fmt("[%s] %s", snac->uid, o_str);
if (xs_str_in(msg, snac->basedir) != -1) {
/* replace long basedir references with ~ */
msg = xs_replace_i(msg, snac->basedir, "~");
}
srv_debug(level, msg);
}
d_char *hash_password(const char *uid, const char *passwd, const char *nonce)
/* hashes a password */
{
xs *d_nonce = NULL;
xs *combi;
xs *hash;
if (nonce == NULL) {
d_nonce = xs_fmt("%08x", random());
nonce = d_nonce;
}
combi = xs_fmt("%s:%s:%s", nonce, uid, passwd);
hash = xs_sha1_hex(combi, strlen(combi));
return xs_fmt("%s:%s", nonce, hash);
}
int check_password(const char *uid, const char *passwd, const char *hash)
/* checks a password */
{
int ret = 0;
xs *spl = xs_split_n(hash, ":", 1);
if (xs_list_len(spl) == 2) {
xs *n_hash = hash_password(uid, passwd, xs_list_get(spl, 0));
ret = (strcmp(hash, n_hash) == 0);
}
return ret;
}
void srv_archive(char *direction, char *req, char *payload, int p_size,
int status, char *headers, char *body, int b_size)
/* archives a connection */
{
/* obsessive archiving */
xs *date = tid(0);
xs *dir = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
FILE *f;
if (mkdir(dir, 0755) != -1) {
xs *meta_fn = xs_fmt("%s/_META", dir);
if ((f = fopen(meta_fn, "w")) != NULL) {
xs *j1 = xs_json_dumps_pp(req, 4);
xs *j2 = xs_json_dumps_pp(headers, 4);
fprintf(f, "dir: %s\n", direction);
fprintf(f, "req: %s\n", j1);
fprintf(f, "p_size: %d\n", p_size);
fprintf(f, "status: %d\n", status);
fprintf(f, "response: %s\n", j2);
fprintf(f, "b_size: %d\n", b_size);
fclose(f);
}
if (p_size && payload) {
xs *payload_fn = NULL;
xs *payload_fn_raw = NULL;
char *v = xs_dict_get(req, "content-type");
if (v && xs_str_in(v, "json") != -1) {
payload_fn = xs_fmt("%s/payload.json", dir);
if ((f = fopen(payload_fn, "w")) != NULL) {
xs *v1 = xs_json_loads(payload);
xs *j1 = NULL;
if (v1 != NULL)
j1 = xs_json_dumps_pp(v1, 4);
if (j1 != NULL)
fwrite(j1, strlen(j1), 1, f);
else
fwrite(payload, p_size, 1, f);
fclose(f);
}
}
payload_fn_raw = xs_fmt("%s/payload", dir);
if ((f = fopen(payload_fn_raw, "w")) != NULL) {
fwrite(payload, p_size, 1, f);
fclose(f);
}
}
if (b_size && body) {
xs *body_fn = NULL;
char *v = xs_dict_get(headers, "content-type");
if (v && xs_str_in(v, "json") != -1) {
body_fn = xs_fmt("%s/body.json", dir);
if ((f = fopen(body_fn, "w")) != NULL) {
xs *v1 = xs_json_loads(body);
xs *j1 = NULL;
if (v1 != NULL)
j1 = xs_json_dumps_pp(v1, 4);
if (j1 != NULL)
fwrite(j1, strlen(j1), 1, f);
else
fwrite(body, b_size, 1, f);
fclose(f);
}
}
else {
body_fn = xs_fmt("%s/body", dir);
if ((f = fopen(body_fn, "w")) != NULL) {
fwrite(body, b_size, 1, f);
fclose(f);
}
}
}
}
}