Updating users/groups in idm client.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Sat, 19 Oct 2013 18:32:34 +0000 (20:32 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Sat, 19 Oct 2013 18:32:34 +0000 (20:32 +0200)
idm.cpp

diff --git a/idm.cpp b/idm.cpp
index 6b92837..8aa885b 100644 (file)
--- a/idm.cpp
+++ b/idm.cpp
@@ -17,16 +17,23 @@ const char *optstring = "hc:u:";
 
 
 static void usage(const char *prg) {
-       printf("Usage: %s [OPTIONS] [COMMANDS]\n\n", prg);
+       printf("Usage: %s [OPTION...] [COMMANDS...]\n\n", prg);
        printf("OPTIONS are:\n");
        printf("  -h,--help ...... usage information\n");
        printf("  -c,--config .... dmlite config file [" DMLITE_CONFIG_DEFAULT "]\n");
        printf("  -u,--user ...... user name []\n\n");
        printf("COMMANDS are:\n");
-       printf("  listusers,lu ........... list users\n");
-       printf("  listgroups,lg .......... list groups\n");
-       printf("  deleteuser,du USER ..... delete user\n");
-       printf("  deletegroup,dg GROUP ... delete group\n");
+       printf("  listusers,lu ....................... list users\n");
+       printf("  listgroups,lg ...................... list groups\n");
+       printf("  deleteuser,du USER ................. delete user\n");
+       printf("  deletegroup,dg GROUP ............... delete group\n");
+       printf("  updateuser,uu USER FIELD[,...] ..... update user\n");
+       printf("  updategroup,ug GROUP FIELD[,...] ... update group\n");
+       printf("\n");
+       printf("FIELDS are:\n");
+       printf("  KEY=VALUE\n");
+       printf("  KEY\n");
+       printf("  !KEY\n");
 }
 
 
@@ -48,6 +55,46 @@ void dumpInfo(const std::string& name, const dmlite::Extensible& data) {
 
 
 
+bool getArgument(int argc, char *argv[], std::string *arg) {
+       if (optind + 1 < argc) {
+               optind++;
+               *arg = argv[optind];
+               return true;
+       } else {
+               printf("Missing argument.\n");
+               usage(argv[0]);
+               return false;
+       };
+}
+
+
+void arg2extensible(std::string arg, dmlite::Extensible& data) {
+       char *all, *ptr, *pair, *value;
+       bool bvalue;
+
+       all = ptr = strdup(arg.c_str());
+       while ((pair = strsep(&ptr, ",")) != NULL) {
+               value = index(pair, '=');
+               if (value) {
+                       value[0] = '\0';
+                       value++;
+//printf("(pair='%s',value='%s')", pair, value);
+                       data[pair] = std::string(value);
+               } else {
+                       if (pair[0] == '!') {
+                               pair++;
+                               bvalue = false;
+                       } else {
+                               bvalue = true;
+                       }
+//printf("(pair='%s',value='%u')", pair, bvalue);
+                       data[pair] = bvalue;
+               }
+       }
+       free(all);
+}
+
+
 int main(int argc, char *argv[]) {
        dmlite::PluginManager manager;
        dmlite::StackInstance *stack = NULL;
@@ -107,10 +154,8 @@ int main(int argc, char *argv[]) {
        }
 
        while (optind < argc) {
-               std::string operation, arg;
+               std::string operation, arg, arg2;
 
-               if (optind + 1 < argc) arg = argv[optind + 1];
-               else arg = "";
                try {
                        authn = stack->getAuthn();
 
@@ -142,12 +187,7 @@ int main(int argc, char *argv[]) {
                            strcasecmp(argv[optind], "du") == 0
                        ) {
                                operation = "delete user";
-                               if (arg.empty()) {
-                                       printf("Missing argument.\n");
-                                       usage(argv[0]);
-                                       return 1;
-                               }
-                               optind++;
+                               if (!getArgument(argc, argv, &arg)) return 1;
 
                                authn->deleteUser(arg);
                                printf("User '%s' deleted.\n\n", arg.c_str());
@@ -156,15 +196,46 @@ int main(int argc, char *argv[]) {
                            strcasecmp(argv[optind], "dg") == 0
                        ) {
                                operation = "delete group";
-                               if (arg.empty()) {
-                                       printf("Missing argument.\n");
-                                       usage(argv[0]);
-                                       return 1;
-                               }
-                               optind++;
+                               if (!getArgument(argc, argv, &arg)) return 1;
 
                                authn->deleteGroup(arg);
                                printf("Group '%s' deleted.\n\n", arg.c_str());
+                       } else if (
+                           strcasecmp(argv[optind], "updateuser") == 0 ||
+                           strcasecmp(argv[optind], "uu") == 0
+                       ) {
+                               dmlite::UserInfo ui;
+
+                               operation = "update user";
+                               if (!getArgument(argc, argv, &arg)) return 1;
+                               if (!getArgument(argc, argv, &arg2)) return 1;
+
+                               ui.name = arg;
+                               arg2extensible(arg2, ui);
+
+                               printf("Updating user:\n");
+                               dumpInfo(ui.name, ui);
+
+                               authn->updateUser(ui);
+                               printf("User '%s' updated.\n\n", ui.name.c_str());
+                       } else if (
+                           strcasecmp(argv[optind], "updategroup") == 0 ||
+                           strcasecmp(argv[optind], "ug") == 0
+                       ) {
+                               dmlite::GroupInfo gi;
+
+                               operation = "update group";
+                               if (!getArgument(argc, argv, &arg)) return 1;
+                               if (!getArgument(argc, argv, &arg2)) return 1;
+
+                               gi.name = arg;
+                               arg2extensible(arg2, gi);
+
+                               printf("Updating group:\n");
+                               dumpInfo(gi.name, gi);
+
+                               authn->updateGroup(gi);
+                               printf("Group '%s' updated.\n\n", gi.name.c_str());
                        } else {
                                printf("Unknown command.\n");
                                usage(argv[0]);