summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c118
1 files changed, 118 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 028f863..422c907 100644
--- a/utils.c
+++ b/utils.c
@@ -5,10 +5,13 @@
5#include "xs_io.h" 5#include "xs_io.h"
6#include "xs_encdec.h" 6#include "xs_encdec.h"
7#include "xs_json.h" 7#include "xs_json.h"
8#include "xs_time.h"
9#include "xs_openssl.h"
8 10
9#include "snac.h" 11#include "snac.h"
10 12
11#include <sys/stat.h> 13#include <sys/stat.h>
14#include <stdlib.h>
12 15
13const char *default_srv_config = "{" 16const char *default_srv_config = "{"
14 "\"host\": \"\"," 17 "\"host\": \"\","
@@ -172,3 +175,118 @@ int initdb(const char *basedir)
172 printf("Done.\n"); 175 printf("Done.\n");
173 return 0; 176 return 0;
174} 177}
178
179
180int adduser(char *uid)
181/* creates a new user */
182{
183 snac snac;
184 xs *config = xs_dict_new();
185 xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
186 int rndbuf[3];
187 xs *pwd = NULL;
188 xs *pwd_f = NULL;
189 xs *key = NULL;
190 FILE *f;
191
192 if (uid == NULL) {
193 printf("User id:\n");
194 uid = xs_strip(xs_readline(stdin));
195 }
196
197 if (!validate_uid(uid)) {
198 printf("ERROR: only alphanumeric characters and _ are allowed in user ids.\n");
199 return 1;
200 }
201
202 if (user_open(&snac, uid)) {
203 printf("ERROR: user '%s' already exists\n", uid);
204 return 1;
205 }
206
207 srandom(time(NULL) ^ getpid());
208 rndbuf[0] = random() & 0xffffffff;
209 rndbuf[1] = random() & 0xffffffff;
210 rndbuf[2] = random() & 0xffffffff;
211
212 pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
213 pwd_f = hash_password(uid, pwd, NULL);
214
215 config = xs_dict_append(config, "uid", uid);
216 config = xs_dict_append(config, "name", uid);
217 config = xs_dict_append(config, "avatar", "");
218 config = xs_dict_append(config, "bio", "");
219 config = xs_dict_append(config, "published", date);
220 config = xs_dict_append(config, "password", pwd_f);
221
222 xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
223
224 if (mkdir(basedir, 0755) == -1) {
225 printf("ERROR: cannot create directory '%s'\n", basedir);
226 return 0;
227 }
228
229 const char *dirs[] = {
230 "actors", "followers", "following", "local", "muted",
231 "queue", "static", "timeline", "history", NULL };
232 int n;
233
234 for (n = 0; dirs[n]; n++) {
235 xs *d = xs_fmt("%s/%s", basedir, dirs[n]);
236 mkdir(d, 0755);
237 }
238
239 xs *scssfn = xs_fmt("%s/style.css", srv_basedir);
240 xs *ucssfn = xs_fmt("%s/static/style.css", basedir);
241
242 if ((f = fopen(scssfn, "r")) != NULL) {
243 FILE *i;
244
245 if ((i = fopen(ucssfn, "w")) == NULL) {
246 printf("ERROR: cannot create file '%s'\n", ucssfn);
247 return 1;
248 }
249 else {
250 xs *c = xs_readall(f);
251 fwrite(c, strlen(c), 1, i);
252
253 fclose(i);
254 }
255
256 fclose(f);
257 }
258
259 xs *cfn = xs_fmt("%s/user.json", basedir);
260
261 if ((f = fopen(cfn, "w")) == NULL) {
262 printf("ERROR: cannot create '%s'\n", cfn);
263 return 1;
264 }
265 else {
266 xs *j = xs_json_dumps_pp(config, 4);
267 fwrite(j, strlen(j), 1, f);
268 fclose(f);
269 }
270
271 printf("\nCreating RSA key...\n");
272 key = xs_rsa_genkey(4096);
273 printf("Done.\n");
274
275 xs *kfn = xs_fmt("%s/key.json", basedir);
276
277 if ((f = fopen(kfn, "w")) == NULL) {
278 printf("ERROR: cannot create '%s'\n", kfn);
279 return 1;
280 }
281 else {
282 xs *j = xs_json_dumps_pp(key, 4);
283 fwrite(j, strlen(j), 1, f);
284 fclose(f);
285 }
286
287 printf("\nUser password is %s\n", pwd);
288
289 printf("\nGo to %s/%s and keep configuring your user.\n", srv_baseurl, uid);
290
291 return 0;
292}