From: Martin Mares Date: Wed, 6 Sep 2017 21:34:03 +0000 (+0200) Subject: It is now possible to change the comment of an existing token X-Git-Tag: v0.9~13 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=51a224b5afd31fdf92eaa5f25a6a7825e0c3bdbb;p=subauth.git It is now possible to change the comment of an existing token --- diff --git a/client/subauth.1.txt b/client/subauth.1.txt index c0f456d..8f633b1 100644 --- a/client/subauth.1.txt +++ b/client/subauth.1.txt @@ -81,6 +81,11 @@ OPERATIONS *--delete-token*:: Delete a given token. Requires *--zone* and *--ident*. +*--change-token*:: + Change parameters of an existing token. Currently, only the comment + can be changed; setting to an empty string removes the comment. + Requires *--zone* and *--ident*. + *--temp-token*:: Create a temporary token. Requires *--zone*. Optionally, token validity can be set with *--expire* (otherwise, diff --git a/client/subauth.c b/client/subauth.c index 44c9fef..7b2d755 100644 --- a/client/subauth.c +++ b/client/subauth.c @@ -187,6 +187,19 @@ static void cmd_delete_token(void) op_run(); } +static void cmd_change_token(void) +{ + if (!arg_zone || !arg_ident) + opt_failure("--zone and --ident must be given"); + + op_new("change-token"); + set_string(rq, "zone", arg_zone); + set_string(rq, "ident", arg_ident); + set_string(rq, "comment", arg_comment); + + op_run(); +} + static void cmd_create_acct(void) { if (!arg_zone || !arg_user) @@ -371,6 +384,7 @@ enum command { CMD_DELETE_PASSWD, CMD_CREATE_TOKEN, CMD_DELETE_TOKEN, + CMD_CHANGE_TOKEN, CMD_CREATE_ACCT, CMD_DELETE_ACCT, CMD_DELETE_USER, @@ -387,6 +401,7 @@ void (* const command_handlers[CMD_MAX])(void) = { [CMD_DELETE_PASSWD] = cmd_delete_passwd, [CMD_CREATE_TOKEN] = cmd_create_token, [CMD_DELETE_TOKEN] = cmd_delete_token, + [CMD_CHANGE_TOKEN] = cmd_change_token, [CMD_CREATE_ACCT] = cmd_create_acct, [CMD_DELETE_ACCT] = cmd_delete_acct, [CMD_DELETE_USER] = cmd_delete_user, @@ -418,6 +433,7 @@ static const struct opt_section options = { OPT_SWITCH(0, "delete-passwd", command, CMD_DELETE_PASSWD, OPT_SINGLE, "\tRemove password"), OPT_SWITCH(0, "create-token", command, CMD_CREATE_TOKEN, OPT_SINGLE, "\tCreate a new token"), OPT_SWITCH(0, "delete-token", command, CMD_DELETE_TOKEN, OPT_SINGLE, "\tRemove an existing token"), + OPT_SWITCH(0, "change-token", command, CMD_CHANGE_TOKEN, OPT_SINGLE, "\tChange the comment of an existing token"), OPT_SWITCH(0, "login", command, CMD_LOGIN, OPT_SINGLE, "\tTry to log in"), OPT_SWITCH(0, "temp-token", command, CMD_TEMP_TOKEN, OPT_SINGLE, "\tCreate a temporary token"), OPT_HELP(""), diff --git a/server/auth.c b/server/auth.c index de94eca..2205c2e 100644 --- a/server/auth.c +++ b/server/auth.c @@ -303,6 +303,12 @@ static void db_parse_user(struct json_node *ju) } } +void auth_change_token_comment(struct auth_token *at, const char *comment) +{ + xfree(at->comment); + at->comment = xstrdup(comment); +} + static void db_read(void) { struct fastbuf *fb = bopen_try(database_name, O_RDONLY, 65536); diff --git a/server/cmd.c b/server/cmd.c index fb8d698..98092b4 100644 --- a/server/cmd.c +++ b/server/cmd.c @@ -297,6 +297,25 @@ static void cmd_delete_token(struct client *c) cmd_ok(c); } +static void cmd_change_token(struct client *c) +{ + struct auth_acct *aa = cmd_need_target_acct(c); + const char *ident = cmd_need_string(c, "ident"); + struct auth_token *at = auth_find_token_generated(aa, ident); + if (!at) + cmd_error(c, "No such token"); + + const char *comment = get_string(c->request, "comment"); + if (comment && !strcmp(comment, "")) + comment = NULL; + auth_change_token_comment(at, comment); + + msg(L_INFO, "Changed token: login=<%s> zone=<%s> id=<%s>", aa->user->login, aa->zone->name, at->ident); + + db_write(); + cmd_ok(c); +} + static void cmd_create_temp(struct client *c) { struct auth_acct *aa = cmd_need_target_acct(c); @@ -513,6 +532,7 @@ static const struct command command_table[] = { { "delete-acct", cmd_delete_acct }, { "create-token", cmd_create_token }, { "delete-token", cmd_delete_token }, + { "change-token", cmd_change_token }, { "set-passwd", cmd_set_passwd }, { "delete-passwd", cmd_delete_passwd }, { "create-temp", cmd_create_temp }, diff --git a/server/subauthd.h b/server/subauthd.h index 1e86754..a2ca099 100644 --- a/server/subauthd.h +++ b/server/subauthd.h @@ -104,6 +104,7 @@ struct auth_token *auth_create_token(struct auth_acct *aa); void auth_set_token_passwd(struct auth_token *at, const char *passwd); char *auth_set_token_generated(struct auth_token *at, const char *comment, struct mempool *pool); bool auth_check_token(struct auth_token *at, const char *passwd); +void auth_change_token_comment(struct auth_token *at, const char *comment); extern struct auth_token *auth_fake_token;