2 * Sub-authentication Daemon: Commands
4 * (c) 2017 Martin Mares <mj@ucw.cz>
16 static void set_string(struct client *c, struct json_node *n, const char *key, const char *val)
19 json_object_set(n, key, json_new_string(c->json, val));
22 static void set_uint(struct client *c, struct json_node *n, const char *key, uint val)
24 json_object_set(n, key, json_new_number(c->json, val));
27 static void NONRET cmd_error(struct client *c, const char *err, ...)
32 vsnprintf(msg, sizeof(msg), err, args);
33 set_string(c, c->reply, "error", msg);
34 trans_throw("adhoc", NULL, "Error caught");
38 static void cmd_ok(struct client *c)
40 set_string(c, c->reply, "error", "");
43 const char *get_string(struct json_node *n, const char *key)
45 struct json_node *s = json_object_get(n, key);
46 if (s && s->type == JSON_STRING)
52 bool get_uint(struct json_node *n, const char *key, uint *dest)
54 struct json_node *s = json_object_get(n, key);
55 if (s && s->type == JSON_NUMBER)
57 uint u = (uint) s->number;
58 if ((double) u == s->number)
68 struct json_node **get_array(struct json_node *n, const char *key)
70 struct json_node *s = json_object_get(n, key);
71 if (s && s->type == JSON_ARRAY)
77 struct json_node *get_object(struct json_node *n, const char *key)
79 struct json_node *s = json_object_get(n, key);
80 if (s && s->type == JSON_OBJECT)
86 static const char *cmd_need_string(struct client *c, const char *key)
88 const char *val = get_string(c->request, key);
90 cmd_error(c, "Missing %s", key);
94 static struct auth_zone *cmd_need_zone(struct client *c)
96 const char *name = cmd_need_string(c, "zone");
97 struct auth_zone *az = auth_find_zone(name);
99 cmd_error(c, "No such zone");
103 static bool cmd_is_admin(struct client *c)
105 return (c->uid == 0);
108 static void cmd_require_admin(struct client *c)
110 if (!cmd_is_admin(c))
111 cmd_error(c, "Permission denied");
114 static const char *cmd_need_target_login(struct client *c)
116 const char *l = get_string(c->request, "login");
119 cmd_require_admin(c);
124 struct passwd *pw = getpwuid(c->uid);
126 cmd_error(c, "You do not exist");
127 return json_strdup(c->json, pw->pw_name);
131 static struct auth_acct *cmd_need_target_acct(struct client *c)
133 const char *login = cmd_need_target_login(c);
134 struct auth_zone *az = cmd_need_zone(c);
136 struct auth_user *au = auth_find_user(login, 0);
137 struct auth_acct *aa = au ? auth_find_acct(au, az, 0) : NULL;
141 if (!az->auto_create_acct)
142 cmd_error(c, "No such account");
146 msg(L_INFO, "Automatically creating user: login=<%s>", login);
147 au = auth_find_user(login, 1);
149 msg(L_INFO, "Automatically creating account: login=<%s> zone=<%s>", login, az->name);
150 return auth_find_acct(au, az, 1);
153 static void cmd_nop(struct client *c)
158 static void cmd_create_acct(struct client *c)
160 cmd_require_admin(c);
162 const char *login = get_string(c->request, "login");
164 cmd_error(c, "Login required");
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))
170 msg(L_INFO, "Creating account: login=<%s> zone=<%s>", login, az->name);
171 auth_find_acct(au, az, 1);
178 static void cmd_delete_acct(struct client *c)
180 cmd_require_admin(c);
182 const char *login = get_string(c->request, "login");
184 cmd_error(c, "Login required");
185 const char *zone_name = get_string(c->request, "zone");
187 struct auth_user *au = auth_find_user(login, 0);
191 if (zone_name && !strcmp(zone_name, "*"))
193 msg(L_INFO, "Deleting user: login=<%s>", login);
194 auth_delete_user(au);
198 struct auth_zone *az = cmd_need_zone(c);
199 struct auth_acct *aa = auth_find_acct(au, az, 0);
202 msg(L_INFO, "Deleting account: login=<%s> zone=<%s>", login, az->name);
203 auth_delete_acct(aa);
204 if (!clist_head(&au->accounts))
206 msg(L_INFO, "Deleting user with no accounts: login=<%s>", login);
207 auth_delete_user(au);
215 static void cmd_set_passwd(struct client *c)
217 struct auth_acct *aa = cmd_need_target_acct(c);
218 const char *passwd = cmd_need_string(c, "passwd");
220 if (!aa->zone->allow_passwd)
221 cmd_error(c, "This zone does not allow authentication by password");
223 if (strchr(passwd, '-'))
224 cmd_error(c, "The minus sign is forbidden in passwords");
226 msg(L_INFO, "Set password: login=<%s> zone=<%s>", aa->user->login, aa->zone->name);
228 struct auth_token *at_old = auth_find_token_passwd(aa);
230 auth_delete_token(at_old);
232 struct auth_token *at = auth_create_token(aa);
233 auth_set_token_passwd(at, passwd);
239 static void cmd_delete_passwd(struct client *c)
241 struct auth_acct *aa = cmd_need_target_acct(c);
242 struct auth_token *at = auth_find_token_passwd(aa);
244 cmd_error(c, "No password set");
246 msg(L_INFO, "Deleted password: login=<%s> zone=<%s>", aa->user->login, aa->zone->name);
247 auth_delete_token(at);
253 static void cmd_create_token(struct client *c)
255 struct auth_acct *aa = cmd_need_target_acct(c);
257 if (!aa->zone->allow_tokens)
258 cmd_error(c, "This zone does not allow authentication by tokens");
260 if (clist_size(&aa->tokens) >= aa->zone->allow_tokens)
261 cmd_error(c, "Maximum number of tokens was reached");
263 const char *comment = get_string(c->request, "comment");
264 if (comment && strlen(comment) > max_comment_size)
265 cmd_error(c, "Comment too long");
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 set_string(c, c->reply, "ident", at->ident);
272 msg(L_INFO, "Created token: login=<%s> zone=<%s> id=<%s>", aa->user->login, aa->zone->name, at->ident);
278 static void cmd_delete_token(struct client *c)
280 struct auth_acct *aa = cmd_need_target_acct(c);
281 const char *ident = cmd_need_string(c, "ident");
282 bool all = !strcmp(ident, "*");
285 struct auth_token *tmp;
286 CLIST_FOR_EACH_DELSAFE(struct auth_token *, at, aa->tokens, tmp)
287 if (at->type == TOKEN_GENERATED && (all || !strcmp(at->ident, ident)))
290 msg(L_INFO, "Deleted token: login=<%s> zone=<%s> id=<%s>", aa->user->login, aa->zone->name, at->ident);
291 auth_delete_token(at);
294 if (!all && !matched)
295 cmd_error(c, "No such token");
301 static void cmd_change_token(struct client *c)
303 struct auth_acct *aa = cmd_need_target_acct(c);
304 const char *ident = cmd_need_string(c, "ident");
305 struct auth_token *at = auth_find_token_generated(aa, ident);
307 cmd_error(c, "No such token");
309 const char *comment = get_string(c->request, "comment");
310 if (comment && !strcmp(comment, ""))
312 auth_change_token_comment(at, comment);
314 msg(L_INFO, "Changed token: login=<%s> zone=<%s> id=<%s>", aa->user->login, aa->zone->name, at->ident);
320 static void cmd_create_temp(struct client *c)
322 struct auth_acct *aa = cmd_need_target_acct(c);
325 if (!get_uint(c->request, "validity", &validity))
326 cmd_error(c, "Validity must be given");
328 if (!aa->zone->max_temp_validity)
329 cmd_error(c, "This zone does not allow temporary tokens");
331 if (validity > aa->zone->max_temp_validity)
332 cmd_error(c, "This zone limits temporary token validity to %d seconds", aa->zone->max_temp_validity);
334 char *tok = temp_generate(aa->zone->name, aa->user->login, validity, c->pool);
335 set_string(c, c->reply, "token", tok);
337 const char *shortened = temp_shorten(tok, c->pool);
339 msg(L_INFO, "Created temp token: login=<%s> zone=<%s> temp=<%s> validity=%u", aa->user->login, aa->zone->name, shortened, validity);
344 static void cmd_login_fake(struct client *c, const char *passwd)
346 auth_check_token(auth_fake_token, passwd);
347 cmd_error(c, "Invalid password");
350 static void cmd_login_by_temp(struct client *c, struct auth_zone *az, const char *given_passwd)
352 const char *login = cmd_need_string(c, "login");
353 const char *shortened = temp_shorten(given_passwd, c->pool);
355 const char *reason = temp_check(az->name, login, given_passwd, c->pool);
358 msg(L_INFO, "Login failed: %s user=<%s> zone=<%s> temp=<%s>", reason, login, az->name, shortened);
363 * The following checks test for improbable things like user
364 * disappearing since the token has been issued.
367 if (!az->max_temp_validity)
369 msg(L_INFO, "Login failed: Temporary tokens no longer accepted for zone=<%s>", az->name);
373 struct auth_user *au = auth_find_user(login, 0);
376 msg(L_INFO, "Login failed: No user=<%s> temp=<%s>", login, shortened);
380 struct auth_acct *aa = auth_find_acct(au, az, 0);
383 msg(L_INFO, "Login failed: No account user=<%s> zone=<%s> temp=<%s>", login, az->name, shortened);
387 msg(L_INFO, "Login successful: user=<%s> zone=<%s> temp=<%s>", login, az->name, shortened);
392 cmd_error(c, "Temporary token refused");
395 static void cmd_login(struct client *c)
397 struct auth_zone *az = cmd_need_zone(c);
398 const char *given_passwd = cmd_need_string(c, "passwd");
400 // Split password string to token ident and the password proper
401 char passbuf[strlen(given_passwd) + 1];
402 strcpy(passbuf, given_passwd);
403 char *ident, *passwd;
404 char *sep = strchr(passbuf, '-');
417 if (ident && !strcmp(ident, "t"))
418 return cmd_login_by_temp(c, az, given_passwd);
420 const char *login = cmd_need_string(c, "login");
421 struct auth_user *au = auth_find_user(login, 0);
424 msg(L_INFO, "Login failed: No user=<%s>", login);
425 return cmd_login_fake(c, passwd);
428 struct auth_acct *aa = auth_find_acct(au, az, 0);
431 msg(L_INFO, "Login failed: No account user=<%s> zone=<%s>", login, az->name);
432 return cmd_login_fake(c, passwd);
435 struct auth_token *at;
437 at = auth_find_token_generated(aa, ident);
439 at = auth_find_token_passwd(aa);
442 msg(L_INFO, "Login failed: No token user=<%s> zone=<%s> ident=<%s>", login, az->name, (ident ? : ""));
443 return cmd_login_fake(c, passwd);
446 if (!auth_check_token(at, passwd))
448 msg(L_INFO, "Login failed: Wrong password for user=<%s> zone=<%s> ident=<%s>", login, az->name, (ident ? : ""));
449 cmd_error(c, "Invalid password");
452 msg(L_INFO, "Login successful: user=<%s> zone=<%s> ident=<%s>", login, az->name, (ident ? : ""));
456 static void cmd_list_accts(struct client *c)
458 const char *login = cmd_need_target_login(c);
460 struct json_context *js = c->json;
461 set_string(c, c->reply, "login", login);
462 struct json_node *jas = json_new_array(js);
463 json_object_set(c->reply, "accounts", jas);
465 struct auth_user *au = auth_find_user(login, 0);
472 CLIST_FOR_EACH(struct auth_acct *, aa, au->accounts)
474 struct json_node *ja = json_new_object(js);
475 json_array_append(jas, ja);
476 set_string(c, ja, "zone", aa->zone->name);
478 struct json_node *jts = json_new_array(js);
479 json_object_set(ja, "tokens", jts);
481 CLIST_FOR_EACH(struct auth_token *, at, aa->tokens)
483 struct json_node *jt = json_new_object(js);
484 json_array_append(jts, jt);
489 case TOKEN_PASSWORD: type = "passwd"; break;
490 case TOKEN_GENERATED: type = "token"; break;
491 default: type = "unknown"; break;
493 set_string(c, jt, "type", type);
495 set_string(c, jt, "ident", at->ident);
496 set_string(c, jt, "comment", at->comment);
497 set_uint(c, jt, "lastmod", at->last_modified);
504 static void cmd_list_zones(struct client *c)
506 struct json_context *js = c->json;
508 struct json_node *jzs = json_new_array(js);
509 json_object_set(c->reply, "zones", jzs);
511 CLIST_FOR_EACH(struct auth_zone *, az, zone_list)
513 struct json_node *jz = json_new_object(js);
514 json_array_append(jzs, jz);
515 set_string(c, jz, "name", az->name);
516 set_string(c, jz, "desc", az->desc);
517 set_uint(c, jz, "allow-passwd", az->allow_passwd);
518 set_uint(c, jz, "allow-tokens", az->allow_tokens);
519 set_uint(c, jz, "max-temp-validity", az->max_temp_validity);
527 void (*handler)(struct client *c);
530 static const struct command command_table[] = {
532 { "create-acct", cmd_create_acct },
533 { "delete-acct", cmd_delete_acct },
534 { "create-token", cmd_create_token },
535 { "delete-token", cmd_delete_token },
536 { "change-token", cmd_change_token },
537 { "set-passwd", cmd_set_passwd },
538 { "delete-passwd", cmd_delete_passwd },
539 { "create-temp", cmd_create_temp },
540 { "login", cmd_login },
541 { "list-accts", cmd_list_accts },
542 { "list-zones", cmd_list_zones },
545 void cmd_dispatch(struct client *c)
547 struct json_node *rq = c->request;
550 if (rq->type != JSON_OBJECT || !(cmd = get_string(rq, "cmd")))
552 set_string(c, c->reply, "error", "Malformed request");
556 const struct command *command = NULL;
557 for (uint i=0; i < ARRAY_SIZE(command_table); i++)
558 if (!strcmp(cmd, command_table[i].cmd))
560 command = &command_table[i];
565 set_string(c, c->reply, "error", "No such command");