snac2/snac.c

137 lines
2.6 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"
#include "snac.h"
#include <sys/time.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-09-19 19:24:55 +00:00
d_char *xs_time(char *fmt, int local)
/* returns a d_char with a formated time */
{
time_t t = time(NULL);
struct tm tm;
char tmp[64];
if (local)
localtime_r(&t, &tm);
else
gmtime_r(&t, &tm);
strftime(tmp, sizeof(tmp), fmt, &tm);
return xs_str_new(tmp);
}
2022-09-20 10:00:13 +00:00
d_char *tid(int offset)
/* returns a time-based Id */
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
2022-09-20 10:00:13 +00:00
return xs_fmt("%10d.%06d", tv.tv_sec + offset, tv.tv_usec);
}
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
{
xs *msg = str;
2022-09-24 08:22:30 +00:00
if (xs_str_in(msg, srv_basedir) != -1) {
2022-09-24 08:18:34 +00:00
/* replace basedir with ~ */
2022-09-24 08:22:30 +00:00
xs *o_str = msg;
msg = xs_replace(o_str, srv_basedir, "~");
2022-09-24 08:18:34 +00:00
}
2022-09-19 19:30:19 +00:00
if (dbglevel >= level) {
xs *tm = xs_local_time("%H:%M:%S");
fprintf(stderr, "%s %s\n", tm, msg);
}
2022-09-19 19:13:40 +00:00
}
int validate_uid(char *uid)
/* returns if uid is a valid identifier */
{
while (*uid) {
if (!(isalnum(*uid) || *uid == '_'))
return 0;
uid++;
}
return 1;
}
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-24 08:22:30 +00:00
xs *o_str = msg;
msg = xs_replace(o_str, snac->basedir, "~");
2022-09-24 08:18:34 +00:00
}
2022-09-24 08:22:30 +00:00
srv_debug(level, msg);
}
d_char *hash_password(char *uid, char *passwd, char *nonce)
/* hashes a password */
{
xs *d_nonce = NULL;
xs *combi;
xs *hash;
if (nonce == NULL)
nonce = d_nonce = xs_fmt("%08x", random());
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(char *uid, char *passwd, 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;
}