diff options
| -rw-r--r-- | Makefile | 8 | ||||
| -rw-r--r-- | data.c | 133 | ||||
| -rw-r--r-- | snac.c | 124 | ||||
| -rw-r--r-- | snac.h | 2 | ||||
| -rw-r--r-- | xs.h | 1 |
5 files changed, 141 insertions, 127 deletions
| @@ -2,16 +2,18 @@ CFLAGS=-g -Wall | |||
| 2 | 2 | ||
| 3 | all: snac | 3 | all: snac |
| 4 | 4 | ||
| 5 | snac: snac.o main.o | 5 | snac: snac.o main.o data.o |
| 6 | $(CC) -L/usr/local/lib *.o -lcurl -lcrypto -o $@ | 6 | $(CC) -L/usr/local/lib *.o -lcurl -lcrypto -o $@ |
| 7 | 7 | ||
| 8 | .c.o: | 8 | .c.o: |
| 9 | $(CC) $(CFLAGS) -I/usr/local/include -c $< | 9 | $(CC) $(CFLAGS) -I/usr/local/include -c $< |
| 10 | 10 | ||
| 11 | clean: | ||
| 12 | rm -rf *.o *.core snac | ||
| 13 | |||
| 11 | snac.o: snac.c snac.h \ | 14 | snac.o: snac.c snac.h \ |
| 12 | xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_httpd.h | 15 | xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_httpd.h |
| 13 | 16 | ||
| 14 | main.o: main.c snac.h xs.h | 17 | main.o: main.c snac.h xs.h |
| 15 | 18 | ||
| 16 | clean: | 19 | data.o: data.c snac.h xs.h xs_json.h |
| 17 | rm -rf *.o *.core snac | ||
| @@ -0,0 +1,133 @@ | |||
| 1 | /* snac - A simple, minimalistic ActivityPub instance */ | ||
| 2 | /* copyright (c) 2022 grunfink - MIT license */ | ||
| 3 | |||
| 4 | #include "xs.h" | ||
| 5 | #include "xs_io.h" | ||
| 6 | #include "xs_json.h" | ||
| 7 | |||
| 8 | #include "snac.h" | ||
| 9 | |||
| 10 | int srv_open(char *basedir) | ||
| 11 | /* opens a server */ | ||
| 12 | { | ||
| 13 | int ret = 0; | ||
| 14 | xs *cfg_file = NULL; | ||
| 15 | FILE *f; | ||
| 16 | |||
| 17 | srv_basedir = xs_str_new(basedir); | ||
| 18 | |||
| 19 | cfg_file = xs_fmt("%s/server.json", basedir); | ||
| 20 | |||
| 21 | if ((f = fopen(cfg_file, "r")) == NULL) | ||
| 22 | srv_log(xs_fmt("error opening '%s'", cfg_file)); | ||
| 23 | else { | ||
| 24 | xs *cfg_data; | ||
| 25 | |||
| 26 | /* read full config file */ | ||
| 27 | cfg_data = xs_readall(f); | ||
| 28 | |||
| 29 | /* parse */ | ||
| 30 | srv_config = xs_json_loads(cfg_data); | ||
| 31 | |||
| 32 | if (srv_config == NULL) | ||
| 33 | srv_log(xs_fmt("cannot parse '%s'", cfg_file)); | ||
| 34 | else { | ||
| 35 | char *host; | ||
| 36 | char *prefix; | ||
| 37 | char *dbglvl; | ||
| 38 | |||
| 39 | host = xs_dict_get(srv_config, "host"); | ||
| 40 | prefix = xs_dict_get(srv_config, "prefix"); | ||
| 41 | dbglvl = xs_dict_get(srv_config, "dbglevel"); | ||
| 42 | |||
| 43 | if (host == NULL || prefix == NULL) | ||
| 44 | srv_log(xs_str_new("cannot get server data")); | ||
| 45 | else { | ||
| 46 | srv_baseurl = xs_fmt("https://%s%s", host, prefix); | ||
| 47 | |||
| 48 | dbglevel = (int) xs_number_get(dbglvl); | ||
| 49 | |||
| 50 | if ((dbglvl = getenv("DEBUG")) != NULL) { | ||
| 51 | dbglevel = atoi(dbglvl); | ||
| 52 | srv_log(xs_fmt("DEBUG level set to %d from environment", dbglevel)); | ||
| 53 | } | ||
| 54 | |||
| 55 | ret = 1; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | return ret; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | void snac_free(snac *snac) | ||
| 65 | /* frees a user snac */ | ||
| 66 | { | ||
| 67 | free(snac->uid); | ||
| 68 | free(snac->basedir); | ||
| 69 | free(snac->config); | ||
| 70 | free(snac->key); | ||
| 71 | free(snac->actor); | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | int snac_open(snac *snac, char *uid) | ||
| 76 | /* opens a user */ | ||
| 77 | { | ||
| 78 | int ret = 0; | ||
| 79 | |||
| 80 | memset(snac, '\0', sizeof(struct _snac)); | ||
| 81 | |||
| 82 | if (validate_uid(uid)) { | ||
| 83 | xs *cfg_file; | ||
| 84 | FILE *f; | ||
| 85 | |||
| 86 | snac->uid = xs_str_new(uid); | ||
| 87 | |||
| 88 | snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid); | ||
| 89 | |||
| 90 | cfg_file = xs_fmt("%s/user.json", snac->basedir); | ||
| 91 | |||
| 92 | if ((f = fopen(cfg_file, "r")) != NULL) { | ||
| 93 | xs *cfg_data; | ||
| 94 | |||
| 95 | /* read full config file */ | ||
| 96 | cfg_data = xs_readall(f); | ||
| 97 | fclose(f); | ||
| 98 | |||
| 99 | if ((snac->config = xs_json_loads(cfg_data)) != NULL) { | ||
| 100 | xs *key_file = xs_fmt("%s/key.json", snac->basedir); | ||
| 101 | |||
| 102 | if ((f = fopen(key_file, "r")) != NULL) { | ||
| 103 | xs *key_data; | ||
| 104 | |||
| 105 | key_data = xs_readall(f); | ||
| 106 | fclose(f); | ||
| 107 | |||
| 108 | if ((snac->key = xs_json_loads(key_data)) != NULL) { | ||
| 109 | snac->actor = xs_fmt("%s/%s", srv_baseurl, uid); | ||
| 110 | ret = 1; | ||
| 111 | } | ||
| 112 | else | ||
| 113 | srv_log(xs_fmt("cannot parse '%s'", key_file)); | ||
| 114 | } | ||
| 115 | else | ||
| 116 | srv_log(xs_fmt("error opening '%s'", key_file)); | ||
| 117 | } | ||
| 118 | else | ||
| 119 | srv_log(xs_fmt("cannot parse '%s'", cfg_file)); | ||
| 120 | } | ||
| 121 | else | ||
| 122 | srv_log(xs_fmt("error opening '%s'", cfg_file)); | ||
| 123 | } | ||
| 124 | else | ||
| 125 | srv_log(xs_fmt("invalid user '%s'", uid)); | ||
| 126 | |||
| 127 | if (!ret) | ||
| 128 | snac_free(snac); | ||
| 129 | |||
| 130 | return ret; | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| @@ -66,60 +66,6 @@ void srv_debug(int level, d_char *str) | |||
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | 68 | ||
| 69 | int srv_open(char *basedir) | ||
| 70 | /* opens a server */ | ||
| 71 | { | ||
| 72 | int ret = 0; | ||
| 73 | xs *cfg_file = NULL; | ||
| 74 | FILE *f; | ||
| 75 | |||
| 76 | srv_basedir = xs_str_new(basedir); | ||
| 77 | |||
| 78 | cfg_file = xs_fmt("%s/server.json", basedir); | ||
| 79 | |||
| 80 | if ((f = fopen(cfg_file, "r")) == NULL) | ||
| 81 | srv_log(xs_fmt("error opening '%s'", cfg_file)); | ||
| 82 | else { | ||
| 83 | xs *cfg_data; | ||
| 84 | |||
| 85 | /* read full config file */ | ||
| 86 | cfg_data = xs_readall(f); | ||
| 87 | |||
| 88 | /* parse */ | ||
| 89 | srv_config = xs_json_loads(cfg_data); | ||
| 90 | |||
| 91 | if (srv_config == NULL) | ||
| 92 | srv_log(xs_fmt("cannot parse '%s'", cfg_file)); | ||
| 93 | else { | ||
| 94 | char *host; | ||
| 95 | char *prefix; | ||
| 96 | char *dbglvl; | ||
| 97 | |||
| 98 | host = xs_dict_get(srv_config, "host"); | ||
| 99 | prefix = xs_dict_get(srv_config, "prefix"); | ||
| 100 | dbglvl = xs_dict_get(srv_config, "dbglevel"); | ||
| 101 | |||
| 102 | if (host == NULL || prefix == NULL) | ||
| 103 | srv_log(xs_str_new("cannot get server data")); | ||
| 104 | else { | ||
| 105 | srv_baseurl = xs_fmt("https://%s%s", host, prefix); | ||
| 106 | |||
| 107 | dbglevel = (int) xs_number_get(dbglvl); | ||
| 108 | |||
| 109 | if ((dbglvl = getenv("DEBUG")) != NULL) { | ||
| 110 | dbglevel = atoi(dbglvl); | ||
| 111 | srv_log(xs_fmt("DEBUG level set to %d from environment", dbglevel)); | ||
| 112 | } | ||
| 113 | |||
| 114 | ret = 1; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | return ret; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | int validate_uid(char *uid) | 69 | int validate_uid(char *uid) |
| 124 | /* returns if uid is a valid identifier */ | 70 | /* returns if uid is a valid identifier */ |
| 125 | { | 71 | { |
| @@ -134,76 +80,6 @@ int validate_uid(char *uid) | |||
| 134 | } | 80 | } |
| 135 | 81 | ||
| 136 | 82 | ||
| 137 | void snac_free(snac *snac) | ||
| 138 | /* frees a user snac */ | ||
| 139 | { | ||
| 140 | free(snac->uid); | ||
| 141 | free(snac->basedir); | ||
| 142 | free(snac->config); | ||
| 143 | free(snac->key); | ||
| 144 | free(snac->actor); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | int snac_open(snac *snac, char *uid) | ||
| 149 | /* opens a user */ | ||
| 150 | { | ||
| 151 | int ret = 0; | ||
| 152 | |||
| 153 | memset(snac, '\0', sizeof(struct _snac)); | ||
| 154 | |||
| 155 | if (validate_uid(uid)) { | ||
| 156 | xs *cfg_file; | ||
| 157 | FILE *f; | ||
| 158 | |||
| 159 | snac->uid = xs_str_new(uid); | ||
| 160 | |||
| 161 | snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid); | ||
| 162 | |||
| 163 | cfg_file = xs_fmt("%s/user.json", snac->basedir); | ||
| 164 | |||
| 165 | if ((f = fopen(cfg_file, "r")) != NULL) { | ||
| 166 | xs *cfg_data; | ||
| 167 | |||
| 168 | /* read full config file */ | ||
| 169 | cfg_data = xs_readall(f); | ||
| 170 | fclose(f); | ||
| 171 | |||
| 172 | if ((snac->config = xs_json_loads(cfg_data)) != NULL) { | ||
| 173 | xs *key_file = xs_fmt("%s/key.json", snac->basedir); | ||
| 174 | |||
| 175 | if ((f = fopen(key_file, "r")) != NULL) { | ||
| 176 | xs *key_data; | ||
| 177 | |||
| 178 | key_data = xs_readall(f); | ||
| 179 | fclose(f); | ||
| 180 | |||
| 181 | if ((snac->key = xs_json_loads(key_data)) != NULL) { | ||
| 182 | snac->actor = xs_fmt("%s/%s", srv_baseurl, uid); | ||
| 183 | ret = 1; | ||
| 184 | } | ||
| 185 | else | ||
| 186 | srv_log(xs_fmt("cannot parse '%s'", key_file)); | ||
| 187 | } | ||
| 188 | else | ||
| 189 | srv_log(xs_fmt("error opening '%s'", key_file)); | ||
| 190 | } | ||
| 191 | else | ||
| 192 | srv_log(xs_fmt("cannot parse '%s'", cfg_file)); | ||
| 193 | } | ||
| 194 | else | ||
| 195 | srv_log(xs_fmt("error opening '%s'", cfg_file)); | ||
| 196 | } | ||
| 197 | else | ||
| 198 | srv_log(xs_fmt("invalid user '%s'", uid)); | ||
| 199 | |||
| 200 | if (!ret) | ||
| 201 | snac_free(snac); | ||
| 202 | |||
| 203 | return ret; | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | void snac_debug(snac *snac, int level, d_char *str) | 83 | void snac_debug(snac *snac, int level, d_char *str) |
| 208 | /* prints a user debugging information */ | 84 | /* prints a user debugging information */ |
| 209 | { | 85 | { |
| @@ -31,3 +31,5 @@ void snac_free(snac *snac); | |||
| 31 | 31 | ||
| 32 | void snac_debug(snac *snac, int level, d_char *str); | 32 | void snac_debug(snac *snac, int level, d_char *str); |
| 33 | #define snac_log(snac, str) snac_debug(snac, 0, str) | 33 | #define snac_log(snac, str) snac_debug(snac, 0, str) |
| 34 | |||
| 35 | int validate_uid(char *uid); | ||
| @@ -37,6 +37,7 @@ typedef char d_char; | |||
| 37 | #define _XS_BLK_SIZE 16 | 37 | #define _XS_BLK_SIZE 16 |
| 38 | #define _xs_blk_size(sz) ((((sz) + _XS_BLK_SIZE) / _XS_BLK_SIZE) * _XS_BLK_SIZE) | 38 | #define _xs_blk_size(sz) ((((sz) + _XS_BLK_SIZE) / _XS_BLK_SIZE) * _XS_BLK_SIZE) |
| 39 | 39 | ||
| 40 | void _xs_destroy(char **var); | ||
| 40 | #define xs_debug() kill(getpid(), 5) | 41 | #define xs_debug() kill(getpid(), 5) |
| 41 | xstype xs_type(const char *data); | 42 | xstype xs_type(const char *data); |
| 42 | int xs_size(const char *data); | 43 | int xs_size(const char *data); |