]> mj.ucw.cz Git - subauth.git/blob - server/cmd.c
98092b4f658289400e51edb6de9ccbdfb993a5f7
[subauth.git] / server / cmd.c
1 /*
2  *      Sub-authentication Daemon: Commands
3  *
4  *      (c) 2017 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/trans.h>
9
10 #include <pwd.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13
14 #include "subauthd.h"
15
16 static void set_string(struct client *c, struct json_node *n, const char *key, const char *val)
17 {
18   if (val)
19     json_object_set(n, key, json_new_string(c->json, val));
20 }
21
22 static void set_uint(struct client *c, struct json_node *n, const char *key, uint val)
23 {
24   json_object_set(n, key, json_new_number(c->json, val));
25 }
26
27 static void NONRET cmd_error(struct client *c, const char *err, ...)
28 {
29   va_list args;
30   char msg[1024];
31   va_start(args, err);
32   vsnprintf(msg, sizeof(msg), err, args);
33   set_string(c, c->reply, "error", msg);
34   trans_throw("adhoc", NULL, "Error caught");
35   va_end(args);
36 }
37
38 static void cmd_ok(struct client *c)
39 {
40   set_string(c, c->reply, "error", "");
41 }
42
43 const char *get_string(struct json_node *n, const char *key)
44 {
45   struct json_node *s = json_object_get(n, key);
46   if (s && s->type == JSON_STRING)
47     return s->string;
48   else
49     return NULL;
50 }
51
52 bool get_uint(struct json_node *n, const char *key, uint *dest)
53 {
54   struct json_node *s = json_object_get(n, key);
55   if (s && s->type == JSON_NUMBER)
56     {
57       uint u = (uint) s->number;
58       if ((double) u == s->number)
59         {
60           *dest = u;
61           return 1;
62         }
63     }
64   *dest = 0;
65   return 0;
66 }
67
68 struct json_node **get_array(struct json_node *n, const char *key)
69 {
70   struct json_node *s = json_object_get(n, key);
71   if (s && s->type == JSON_ARRAY)
72     return s->elements;
73   else
74     return NULL;
75 }
76
77 struct json_node *get_object(struct json_node *n, const char *key)
78 {
79   struct json_node *s = json_object_get(n, key);
80   if (s && s->type == JSON_OBJECT)
81     return s;
82   else
83     return NULL;
84 }
85
86 static const char *cmd_need_string(struct client *c, const char *key)
87 {
88   const char *val = get_string(c->request, key);
89   if (!val)
90     cmd_error(c, "Missing %s", key);
91   return val;
92 }
93
94 static struct auth_zone *cmd_need_zone(struct client *c)
95 {
96   const char *name = cmd_need_string(c, "zone");
97   struct auth_zone *az = auth_find_zone(name);
98   if (!az)
99     cmd_error(c, "No such zone");
100   return az;
101 }
102
103 static bool cmd_is_admin(struct client *c)
104 {
105   return (c->uid == 0);
106 }
107
108 static void cmd_require_admin(struct client *c)
109 {
110   if (!cmd_is_admin(c))
111     cmd_error(c, "Permission denied");
112 }
113
114 static const char *cmd_need_target_login(struct client *c)
115 {
116   const char *l = get_string(c->request, "login");
117   if (l)
118     {
119       cmd_require_admin(c);
120       return l;
121     }
122   else
123     {
124       struct passwd *pw = getpwuid(c->uid);
125       if (!pw)
126         cmd_error(c, "You do not exist");
127       return json_strdup(c->json, pw->pw_name);
128     }
129 }
130
131 static struct auth_acct *cmd_need_target_acct(struct client *c)
132 {
133   const char *login = cmd_need_target_login(c);
134   struct auth_zone *az = cmd_need_zone(c);
135
136   struct auth_user *au = auth_find_user(login, 0);
137   struct auth_acct *aa = au ? auth_find_acct(au, az, 0) : NULL;
138   if (aa)
139     return aa;
140
141   if (!az->auto_create_acct)
142     cmd_error(c, "No such account");
143
144   if (!au)
145     {
146       msg(L_INFO, "Automatically creating user: login=<%s>", login);
147       au = auth_find_user(login, 1);
148     }
149   msg(L_INFO, "Automatically creating account: login=<%s> zone=<%s>", login, az->name);
150   return auth_find_acct(au, az, 1);
151 }
152
153 static void cmd_nop(struct client *c)
154 {
155   cmd_ok(c);
156 }
157
158 static void cmd_create_acct(struct client *c)
159 {
160   cmd_require_admin(c);
161
162   const char *login = get_string(c->request, "login");
163   if (!login)
164     cmd_error(c, "Login required");
165
166   struct auth_zone *az = cmd_need_zone(c);
167   struct auth_user *au = auth_find_user(login, 1);
168   if (!auth_find_acct(au, az, 0))
169     {
170       msg(L_INFO, "Creating account: login=<%s> zone=<%s>", login, az->name);
171       auth_find_acct(au, az, 1);
172       db_write();
173     }
174
175   cmd_ok(c);
176 }
177
178 static void cmd_delete_acct(struct client *c)
179 {
180   cmd_require_admin(c);
181
182   const char *login = get_string(c->request, "login");
183   if (!login)
184     cmd_error(c, "Login required");
185   const char *zone_name = get_string(c->request, "zone");
186
187   struct auth_user *au = auth_find_user(login, 0);
188   if (!au)
189     return cmd_ok(c);
190
191   if (zone_name && !strcmp(zone_name, "*"))
192     {
193       msg(L_INFO, "Deleting user: login=<%s>", login);
194       auth_delete_user(au);
195     }
196   else
197     {
198       struct auth_zone *az = cmd_need_zone(c);
199       struct auth_acct *aa = auth_find_acct(au, az, 0);
200       if (!aa)
201         return cmd_ok(c);
202       msg(L_INFO, "Deleting account: login=<%s> zone=<%s>", login, az->name);
203       auth_delete_acct(aa);
204       if (!clist_head(&au->accounts))
205         {
206           msg(L_INFO, "Deleting user with no accounts: login=<%s>", login);
207           auth_delete_user(au);
208         }
209     }
210
211   db_write();
212   cmd_ok(c);
213 }
214
215 static void cmd_set_passwd(struct client *c)
216 {
217   struct auth_acct *aa = cmd_need_target_acct(c);
218   const char *passwd = cmd_need_string(c, "passwd");
219
220   if (!aa->zone->allow_passwd)
221     cmd_error(c, "This zone does not allow authentication by password");
222
223   if (strchr(passwd, '-'))
224     cmd_error(c, "The minus sign is forbidden in passwords");
225
226   msg(L_INFO, "Set password: login=<%s> zone=<%s>", aa->user->login, aa->zone->name);
227
228   struct auth_token *at_old = auth_find_token_passwd(aa);
229   if (at_old)
230     auth_delete_token(at_old);
231
232   struct auth_token *at = auth_create_token(aa);
233   auth_set_token_passwd(at, passwd);
234
235   db_write();
236   cmd_ok(c);
237 }
238
239 static void cmd_delete_passwd(struct client *c)
240 {
241   struct auth_acct *aa = cmd_need_target_acct(c);
242   struct auth_token *at = auth_find_token_passwd(aa);
243   if (!at)
244     cmd_error(c, "No password set");
245
246   msg(L_INFO, "Deleted password: login=<%s> zone=<%s>", aa->user->login, aa->zone->name);
247   auth_delete_token(at);
248
249   db_write();
250   cmd_ok(c);
251 }
252
253 static void cmd_create_token(struct client *c)
254 {
255   struct auth_acct *aa = cmd_need_target_acct(c);
256
257   if (!aa->zone->allow_tokens)
258     cmd_error(c, "This zone does not allow authentication by tokens");
259
260   if (clist_size(&aa->tokens) >= aa->zone->allow_tokens)
261     cmd_error(c, "Maximum number of tokens was reached");
262
263   const char *comment = get_string(c->request, "comment");
264   if (comment && strlen(comment) > max_comment_size)
265     cmd_error(c, "Comment too long");
266
267   struct auth_token *at = auth_create_token(aa);
268   char *tok = auth_set_token_generated(at, comment, c->pool);
269   set_string(c, c->reply, "token", tok);
270
271   msg(L_INFO, "Created token: login=<%s> zone=<%s> id=<%s>", aa->user->login, aa->zone->name, at->ident);
272
273   db_write();
274   cmd_ok(c);
275 }
276
277 static void cmd_delete_token(struct client *c)
278 {
279   struct auth_acct *aa = cmd_need_target_acct(c);
280   const char *ident = cmd_need_string(c, "ident");
281   bool all = !strcmp(ident, "*");
282   bool matched = 0;
283
284   struct auth_token *tmp;
285   CLIST_FOR_EACH_DELSAFE(struct auth_token *, at, aa->tokens, tmp)
286     if (at->type == TOKEN_GENERATED && (all || !strcmp(at->ident, ident)))
287       {
288         matched = 1;
289         msg(L_INFO, "Deleted token: login=<%s> zone=<%s> id=<%s>", aa->user->login, aa->zone->name, at->ident);
290         auth_delete_token(at);
291       }
292
293   if (!all && !matched)
294     cmd_error(c, "No such token");
295
296   db_write();
297   cmd_ok(c);
298 }
299
300 static void cmd_change_token(struct client *c)
301 {
302   struct auth_acct *aa = cmd_need_target_acct(c);
303   const char *ident = cmd_need_string(c, "ident");
304   struct auth_token *at = auth_find_token_generated(aa, ident);
305   if (!at)
306     cmd_error(c, "No such token");
307
308   const char *comment = get_string(c->request, "comment");
309   if (comment && !strcmp(comment, ""))
310     comment = NULL;
311   auth_change_token_comment(at, comment);
312
313   msg(L_INFO, "Changed token: login=<%s> zone=<%s> id=<%s>", aa->user->login, aa->zone->name, at->ident);
314
315   db_write();
316   cmd_ok(c);
317 }
318
319 static void cmd_create_temp(struct client *c)
320 {
321   struct auth_acct *aa = cmd_need_target_acct(c);
322
323   uint validity;
324   if (!get_uint(c->request, "validity", &validity))
325     cmd_error(c, "Validity must be given");
326
327   if (!aa->zone->max_temp_validity)
328     cmd_error(c, "This zone does not allow temporary tokens");
329
330   if (validity > aa->zone->max_temp_validity)
331     cmd_error(c, "This zone limits temporary token validity to %d seconds", aa->zone->max_temp_validity);
332
333   char *tok = temp_generate(aa->zone->name, aa->user->login, validity, c->pool);
334   set_string(c, c->reply, "token", tok);
335
336   const char *shortened = temp_shorten(tok, c->pool);
337
338   msg(L_INFO, "Created temp token: login=<%s> zone=<%s> temp=<%s> validity=%u", aa->user->login, aa->zone->name, shortened, validity);
339
340   cmd_ok(c);
341 }
342
343 static void cmd_login_fake(struct client *c, const char *passwd)
344 {
345   auth_check_token(auth_fake_token, passwd);
346   cmd_error(c, "Invalid password");
347 }
348
349 static void cmd_login_by_temp(struct client *c, struct auth_zone *az, const char *given_passwd)
350 {
351   const char *login = cmd_need_string(c, "login");
352   const char *shortened = temp_shorten(given_passwd, c->pool);
353
354   const char *reason = temp_check(az->name, login, given_passwd, c->pool);
355   if (reason)
356     {
357       msg(L_INFO, "Login failed: %s user=<%s> zone=<%s> temp=<%s>", reason, login, az->name, shortened);
358       goto reject;
359     }
360
361   /*
362    * The following checks test for improbable things like user
363    * disappearing since the token has been issued.
364    */
365
366   if (!az->max_temp_validity)
367     {
368       msg(L_INFO, "Login failed: Temporary tokens no longer accepted for zone=<%s>", az->name);
369       goto reject;
370     }
371
372   struct auth_user *au = auth_find_user(login, 0);
373   if (!au)
374     {
375       msg(L_INFO, "Login failed: No user=<%s> temp=<%s>", login, shortened);
376       goto reject;
377     }
378
379   struct auth_acct *aa = auth_find_acct(au, az, 0);
380   if (!aa)
381     {
382       msg(L_INFO, "Login failed: No account user=<%s> zone=<%s> temp=<%s>", login, az->name, shortened);
383       goto reject;
384     }
385
386   msg(L_INFO, "Login successful: user=<%s> zone=<%s> temp=<%s>", login, az->name, shortened);
387   cmd_ok(c);
388   return;
389
390 reject:
391   cmd_error(c, "Temporary token refused");
392 }
393
394 static void cmd_login(struct client *c)
395 {
396   struct auth_zone *az = cmd_need_zone(c);
397   const char *given_passwd = cmd_need_string(c, "passwd");
398
399   // Split password string to token ident and the password proper
400   char passbuf[strlen(given_passwd) + 1];
401   strcpy(passbuf, given_passwd);
402   char *ident, *passwd;
403   char *sep = strchr(passbuf, '-');
404   if (sep)
405     {
406       *sep = 0;
407       ident = passbuf;
408       passwd = sep + 1;
409     }
410   else
411     {
412       ident = NULL;
413       passwd = passbuf;
414     }
415
416   if (ident && !strcmp(ident, "t"))
417     return cmd_login_by_temp(c, az, given_passwd);
418
419   const char *login = cmd_need_string(c, "login");
420   struct auth_user *au = auth_find_user(login, 0);
421   if (!au)
422     {
423       msg(L_INFO, "Login failed: No user=<%s>", login);
424       return cmd_login_fake(c, passwd);
425     }
426
427   struct auth_acct *aa = auth_find_acct(au, az, 0);
428   if (!aa)
429     {
430       msg(L_INFO, "Login failed: No account user=<%s> zone=<%s>", login, az->name);
431       return cmd_login_fake(c, passwd);
432     }
433
434   struct auth_token *at;
435   if (ident)
436     at = auth_find_token_generated(aa, ident);
437   else
438     at = auth_find_token_passwd(aa);
439   if (!at)
440     {
441       msg(L_INFO, "Login failed: No token user=<%s> zone=<%s> ident=<%s>", login, az->name, (ident ? : ""));
442       return cmd_login_fake(c, passwd);
443     }
444
445   if (!auth_check_token(at, passwd))
446     {
447       msg(L_INFO, "Login failed: Wrong password for user=<%s> zone=<%s> ident=<%s>", login, az->name, (ident ? : ""));
448       cmd_error(c, "Invalid password");
449     }
450
451   msg(L_INFO, "Login successful: user=<%s> zone=<%s> ident=<%s>", login, az->name, (ident ? : ""));
452   cmd_ok(c);
453 }
454
455 static void cmd_list_accts(struct client *c)
456 {
457   const char *login = cmd_need_target_login(c);
458
459   struct json_context *js = c->json;
460   set_string(c, c->reply, "login", login);
461   struct json_node *jas = json_new_array(js);
462   json_object_set(c->reply, "accounts", jas);
463
464   struct auth_user *au = auth_find_user(login, 0);
465   if (!au)
466     {
467       cmd_ok(c);
468       return;
469     }
470
471   CLIST_FOR_EACH(struct auth_acct *, aa, au->accounts)
472     {
473       struct json_node *ja = json_new_object(js);
474       json_array_append(jas, ja);
475       set_string(c, ja, "zone", aa->zone->name);
476
477       struct json_node *jts = json_new_array(js);
478       json_object_set(ja, "tokens", jts);
479
480       CLIST_FOR_EACH(struct auth_token *, at, aa->tokens)
481         {
482           struct json_node *jt = json_new_object(js);
483           json_array_append(jts, jt);
484
485           const char *type;
486           switch (at->type)
487             {
488             case TOKEN_PASSWORD:        type = "passwd"; break;
489             case TOKEN_GENERATED:       type = "token"; break;
490             default:                    type = "unknown"; break;
491             }
492           set_string(c, jt, "type", type);
493
494           set_string(c, jt, "ident", at->ident);
495           set_string(c, jt, "comment", at->comment);
496           set_uint(c, jt, "lastmod", at->last_modified);
497         }
498     }
499
500   cmd_ok(c);
501 }
502
503 static void cmd_list_zones(struct client *c)
504 {
505   struct json_context *js = c->json;
506
507   struct json_node *jzs = json_new_array(js);
508   json_object_set(c->reply, "zones", jzs);
509
510   CLIST_FOR_EACH(struct auth_zone *, az, zone_list)
511     {
512       struct json_node *jz = json_new_object(js);
513       json_array_append(jzs, jz);
514       set_string(c, jz, "name", az->name);
515       set_string(c, jz, "desc", az->desc);
516       set_uint(c, jz, "allow-passwd", az->allow_passwd);
517       set_uint(c, jz, "allow-tokens", az->allow_tokens);
518       set_uint(c, jz, "max-temp-validity", az->max_temp_validity);
519     }
520
521   cmd_ok(c);
522 }
523
524 struct command {
525   const char *cmd;
526   void (*handler)(struct client *c);
527 };
528
529 static const struct command command_table[] = {
530   { "nop",              cmd_nop },
531   { "create-acct",      cmd_create_acct },
532   { "delete-acct",      cmd_delete_acct },
533   { "create-token",     cmd_create_token },
534   { "delete-token",     cmd_delete_token },
535   { "change-token",     cmd_change_token },
536   { "set-passwd",       cmd_set_passwd },
537   { "delete-passwd",    cmd_delete_passwd },
538   { "create-temp",      cmd_create_temp },
539   { "login",            cmd_login },
540   { "list-accts",       cmd_list_accts },
541   { "list-zones",       cmd_list_zones },
542 };
543
544 void cmd_dispatch(struct client *c)
545 {
546   struct json_node *rq = c->request;
547   const char *cmd;
548
549   if (rq->type != JSON_OBJECT || !(cmd = get_string(rq, "cmd")))
550     {
551       set_string(c, c->reply, "error", "Malformed request");
552       return;
553     }
554
555   const struct command *command = NULL;
556   for (uint i=0; i < ARRAY_SIZE(command_table); i++)
557     if (!strcmp(cmd, command_table[i].cmd))
558       {
559         command = &command_table[i];
560         break;
561       }
562   if (!command)
563     {
564       set_string(c, c->reply, "error", "No such command");
565       return;
566     }
567
568   TRANS_TRY
569     {
570       if (command)
571         command->handler(c);
572     }
573   TRANS_CATCH(x)
574     {
575     }
576   TRANS_END;
577 }