--- /dev/null
+#include <getopt.h>
+
+#include <cstdio>
+
+#include <dmlite/cpp/dmlite.h>
+#include <dmlite/cpp/authn.h>
+
+#define DMLITE_CONFIG_DEFAULT "/etc/dmlite.conf"
+
+struct option longopts[] = {
+ { "config", required_argument, NULL, 'c' },
+ { "help", no_argument, NULL, 'h' },
+};
+
+const char *optstring = "hc:u:";
+
+
+
+static void usage(const char *prg) {
+ printf("Usage: %s [OPTIONS] [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\n");
+ printf(" listgroups,lg\n");
+}
+
+
+
+void dumpExtensible(const dmlite::Extensible& data) {
+ std::vector<std::string> keys;
+
+ keys = data.getKeys();
+ for (size_t i = 0; i < keys.size(); i++)
+ printf("%s %s\t", keys[i].c_str(), data.getString(keys[i]).c_str());
+}
+
+
+
+void dumpInfo(const std::string& name, const dmlite::Extensible& data) {
+ dumpExtensible(data);
+ printf("name '%s'\n", name.c_str());
+}
+
+
+
+int main(int argc, char *argv[]) {
+ dmlite::PluginManager manager;
+ dmlite::StackInstance *stack = NULL;
+ dmlite::SecurityCredentials creds;
+ dmlite::Authn *authn = NULL;
+ int opt;
+
+ std::string dmlite_config = DMLITE_CONFIG_DEFAULT;
+ std::string user;
+
+ while ((opt = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {
+ switch (opt) {
+ case 'c':
+ dmlite_config = optarg;
+ break;
+ case 'h':
+ usage(argv[0]);
+ return 0;
+ break;
+ case 'u':
+ user = optarg;
+ break;
+ default:
+ usage(argv[0]);
+ throw dmlite::DmException();
+ }
+ }
+ if (dmlite_config[0] != '/') {
+ char *cwd = get_current_dir_name();
+ dmlite_config = std::string(cwd) + "/" + dmlite_config;
+ free(cwd);
+ }
+
+
+ // load configuration
+ try {
+ manager.loadConfiguration(dmlite_config);
+ } catch (dmlite::DmException& e) {
+ std::cout << "Could not load the configuration file." << std::endl
+ << "Reason: " << e.what() << std::endl;
+ throw;
+ }
+ stack = new dmlite::StackInstance(&manager);
+
+ // security credentials
+ if (!user.empty()) {
+ dmlite::SecurityCredentials creds;
+
+ creds.clientName = user;
+ try {
+ stack->setSecurityCredentials(creds);
+ } catch (dmlite::DmException& e) {
+ std::cout << "Could not set the credentials." << std::endl
+ << "Reason: " << e.what() << std::endl;
+ throw;
+ }
+ }
+
+ while (optind < argc) {
+ std::string operation;
+
+ try {
+ authn = stack->getAuthn();
+
+ if (strcasecmp(argv[optind], "listusers") == 0 ||
+ strcasecmp(argv[optind], "lu") == 0
+ ) {
+ std::vector<dmlite::UserInfo> uids;
+
+ operation = "list users";
+ printf("List users:\n");
+ uids = authn->getUsers();
+ for (size_t i = 0; i < uids.size(); i++)
+ dumpInfo(uids[i].name, uids[i]);
+ } else if (
+ strcasecmp(argv[optind], "listgroups") == 0 ||
+ strcasecmp(argv[optind], "lg") == 0
+ ) {
+ std::vector<dmlite::GroupInfo> gids;
+
+ operation = "list groups";
+ printf("List groups:\n");
+ gids = authn->getGroups();
+ for (size_t i = 0; i < gids.size(); i++)
+ dumpInfo(gids[i].name, gids[i]);
+ } else {
+ printf("Unknown command.\n");
+ usage(argv[0]);
+ return 1;
+ }
+
+ optind++;
+ } catch (dmlite::DmException& e) {
+ std::cout << "Could not " + operation + "." << std::endl
+ << "Reason: " << e.what() << std::endl;
+ throw;
+ }
+ }
+
+ delete stack;
+}