]> mj.ucw.cz Git - subauth.git/commitdiff
It is now possible to change the comment of an existing token
authorMartin Mares <mj@ucw.cz>
Wed, 6 Sep 2017 21:34:03 +0000 (23:34 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 6 Sep 2017 21:34:03 +0000 (23:34 +0200)
client/subauth.1.txt
client/subauth.c
server/auth.c
server/cmd.c
server/subauthd.h

index c0f456d76f39b35cebb489f6fa258129ef15f904..8f633b15804aabad5b7fc480153480fcb554a00f 100644 (file)
@@ -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,
index 44c9fefece6bc4956030d4e8b958321d2878d632..7b2d755cc3523ab5ebad81b4e36fe650a672f25c 100644 (file)
@@ -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(""),
index de94eca669467a2f81250059a0b9111f910ccb06..2205c2e9e52a0ca5e5ce7d368b7b03683debb7fc 100644 (file)
@@ -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);
index fb8d698b51a3224abe63788c03ee2da6a6fd08fa..98092b4f658289400e51edb6de9ccbdfb993a5f7 100644 (file)
@@ -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 },
index 1e867541ef2991231f41f3434d186bfd2ce30097..a2ca09957f0ec95c30d5690f288752d0927dbe28 100644 (file)
@@ -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;