snac2/snac.c

230 lines
5.3 KiB
C
Raw Normal View History

2022-09-19 19:13:40 +00:00
/* snac - A simple, minimalistic ActivityPub instance */
2022-09-19 18:47:22 +00:00
/* 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"
2022-09-25 19:02:47 +00:00
#include "xs_mime.h"
2022-09-27 08:51:50 +00:00
#include "xs_regex.h"
2022-09-28 08:27:01 +00:00
#include "xs_set.h"
2022-10-02 07:27:17 +00:00
#include "xs_time.h"
2022-10-03 09:18:49 +00:00
#include "xs_glob.h"
2022-09-19 18:47:22 +00:00
#include "snac.h"
#include <sys/time.h>
2022-09-25 05:28:42 +00:00
#include <sys/stat.h>
2022-09-19 19:13:40 +00:00
d_char *srv_basedir = NULL;
d_char *srv_config = NULL;
d_char *srv_baseurl = NULL;
2022-09-21 16:13:11 +00:00
int srv_running = 0;
2022-09-19 19:13:40 +00:00
int dbglevel = 0;
2022-10-26 04:43:47 +00:00
int valid_status(int status)
/* is this HTTP status valid? */
{
return status >= 200 && status <= 299;
}
2022-09-20 10:00:13 +00:00
d_char *tid(int offset)
/* returns a time-based Id */
{
struct timeval tv;
gettimeofday(&tv, NULL);
2022-09-20 10:00:13 +00:00
return xs_fmt("%10d.%06d", tv.tv_sec + offset, tv.tv_usec);
}
2022-09-28 15:18:30 +00:00
double ftime(void)
/* returns the UNIX time as a float */
{
xs *ntid = tid(0);
return atof(ntid);
}
2022-12-04 20:14:18 +00:00
int validate_uid(const char *uid)
2022-09-25 05:28:42 +00:00
/* returns if uid is a valid identifier */
{
while (*uid) {
if (!(isalnum(*uid) || *uid == '_'))
return 0;
uid++;
}
return 1;
}
2022-09-19 19:30:19 +00:00
void srv_debug(int level, d_char *str)
/* logs a debug message */
2022-09-19 19:13:40 +00:00
{
if (xs_str_in(str, srv_basedir) != -1) {
2022-09-24 08:18:34 +00:00
/* replace basedir with ~ */
str = xs_replace_i(str, srv_basedir, "~");
2022-09-24 08:18:34 +00:00
}
2022-09-19 19:30:19 +00:00
if (dbglevel >= level) {
2022-10-02 07:27:17 +00:00
xs *tm = xs_str_localtime(0, "%H:%M:%S");
fprintf(stderr, "%s %s\n", tm, str);
2022-09-19 19:30:19 +00:00
}
xs_free(str);
2022-09-19 19:13:40 +00:00
}
void snac_debug(snac *snac, int level, d_char *str)
/* prints a user debugging information */
{
xs *o_str = str;
2022-09-24 08:22:30 +00:00
d_char *msg = xs_fmt("[%s] %s", snac->uid, o_str);
2022-09-24 08:22:30 +00:00
if (xs_str_in(msg, snac->basedir) != -1) {
2022-09-24 08:18:34 +00:00
/* replace long basedir references with ~ */
2022-09-27 08:20:33 +00:00
msg = xs_replace_i(msg, snac->basedir, "~");
2022-09-24 08:18:34 +00:00
}
2022-09-24 08:22:30 +00:00
srv_debug(level, msg);
}
2022-12-04 20:14:18 +00:00
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);
}
2022-12-04 20:14:18 +00:00
int check_password(const char *uid, const char *passwd, const char *hash)
/* checks a password */
{
int ret = 0;
2022-09-19 21:08:59 +00:00
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;
}
2022-09-25 05:28:42 +00:00
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);
2022-09-25 05:28:42 +00:00
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) {
2022-09-26 11:06:15 +00:00
xs *payload_fn = NULL;
xs *payload_fn_raw = NULL;
char *v = xs_dict_get(req, "content-type");
2022-09-25 05:28:42 +00:00
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);
2022-09-25 05:28:42 +00:00
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);
2022-09-25 05:28:42 +00:00
}
}
if (b_size && body) {
2022-09-26 11:06:15 +00:00
xs *body_fn = NULL;
2022-09-25 05:28:42 +00:00
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) {
2022-09-25 07:47:36 +00:00
xs *v1 = xs_json_loads(body);
xs *j1 = NULL;
if (v1 != NULL)
j1 = xs_json_dumps_pp(v1, 4);
2022-09-25 05:28:42 +00:00
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);
}
}
}
}
}