r5917: First step in using the new cli_credentials structure. This patch
[bbaumbach/samba-autobuild/.git] / source4 / utils / net / net.c
index ddca5662a252a506f0eb9495a3922d2dd5199f85..96ae23c7e4bd00d56c44aad2afbf073dba50be90 100644 (file)
@@ -42,7 +42,9 @@
 /*****************************************************/
 
 #include "includes.h"
+#include "dynconfig.h"
 #include "utils/net/net.h"
+#include "lib/cmdline/popt_common.h"
 
 /*
   run a function from a function table. If not found then
 int net_run_function(struct net_context *ctx,
                        int argc, const char **argv,
                        const struct net_functable *functable, 
-                       int (*help_fn)(struct net_context *ctx, int argc, const char **argv))
+                       int (*usage_fn)(struct net_context *ctx, int argc, const char **argv))
 {
        int i;
-       
-       if (argc < 1) {
-               d_printf("Usage: \n");
-               return help_fn(ctx, argc, argv);
+
+       if (argc == 0) {
+               return usage_fn(ctx, argc, argv);
+
+       } else if (argc == 1 && strequal(argv[0], "help")) {
+               return net_help(ctx, functable);
        }
+
        for (i=0; functable[i].name; i++) {
                if (StrCaseCmp(argv[0], functable[i].name) == 0)
                        return functable[i].fn(ctx, argc-1, argv+1);
        }
+
        d_printf("No command: %s\n", argv[0]);
-       return help_fn(ctx, argc, argv);
+       return usage_fn(ctx, argc, argv);
 }
 
 /*
-  run a help function from a function table. If not found then fail
+  run a usage function from a function table. If not found then fail
 */
-int net_run_help(struct net_context *ctx,
+int net_run_usage(struct net_context *ctx,
                        int argc, const char **argv,
                        const struct net_functable *functable)
 {
        int i;
-       
+/*
        if (argc < 1) {
-               d_printf("net_run_help: TODO (argc < 1)\n");
+               d_printf("net_run_usage: TODO (argc < 1)\n");
                return 1;
        }
+*/
        for (i=0; functable[i].name; i++) {
                if (StrCaseCmp(argv[0], functable[i].name) == 0)
-                       if (functable[i].help) {
-                               return functable[i].help(ctx, argc-1, argv+1);
+                       if (functable[i].usage) {
+                               return functable[i].usage(ctx, argc-1, argv+1);
                        }
        }
-       d_printf("No help for command: %s\n", argv[0]);
-       return 1;
-}
 
-static int net_help_msg(struct net_context *ctx, int argc, const char **argv)
-{
-       d_printf("Help: TODO\n");
-       return 0;       
+       d_printf("No usage information for command: %s\n", argv[0]);
+
+       return 1;
 }
 
-static int net_help(struct net_context *ctx, int argc, const char **argv);
 
 /* main function table */
 static const struct net_functable net_functable[] = {
-/*     {"password", net_password, net_password_help},*/
-
-       {"help", net_help_msg, net_help_msg},
-       {NULL, NULL}
+       {"password", "change password\n", net_password, net_password_usage},
+       {"time", "get remote server's time\n", net_time, net_time_usage},
+       {"join", "join a domain\n", net_join, net_join_usage},
+       {"user", "manage user accounts\n", net_user, net_user_usage},
+       {NULL, NULL, NULL, NULL}
 };
 
-static int net_help(struct net_context *ctx, int argc, const char **argv)
+int net_help(struct net_context *ctx, const struct net_functable *ftable)
+{
+       int i = 0;
+       const char *name = ftable[i].name;
+       const char *desc = ftable[i].desc;
+
+       d_printf("Available commands:\n");
+       while (name && desc) {
+               d_printf("\t%s\t\t%s", name, desc);
+               name = ftable[++i].name;
+               desc = ftable[i].desc;
+       }
+
+       return 0;
+}
+
+static int net_usage(struct net_context *ctx, int argc, const char **argv)
 {
-       return net_run_help(ctx, argc, argv, net_functable);
+       d_printf("Usage:\n");
+       d_printf("net <command> [options]\n");
+       return 0;
 }
 
 /****************************************************************************
@@ -120,8 +141,17 @@ static int binary_net(int argc, const char **argv)
        int rc;
        int argc_new;
        const char **argv_new;
-       struct net_context ctx;
+       TALLOC_CTX *mem_ctx;
+       struct net_context *ctx = NULL;
        poptContext pc;
+       struct poptOption long_options[] = {
+               POPT_AUTOHELP
+               POPT_COMMON_SAMBA
+               POPT_COMMON_CONNECTION
+               POPT_COMMON_CREDENTIALS
+               POPT_COMMON_VERSION
+               POPT_TABLEEND
+       };
 
        setup_logging("net", DEBUG_STDOUT);
 
@@ -129,31 +159,15 @@ static int binary_net(int argc, const char **argv)
        setbuffer(stdout, NULL, 0);
 #endif
 
-       ctx.mem_ctx = talloc_init("net_context");
-       if (!ctx.mem_ctx) {
-               d_printf("talloc_init(net_context) failed\n");
-               exit(1);
-       }
-
-       struct poptOption long_options[] = {
-               {"help",        'h', POPT_ARG_NONE,   0, 'h'},
-               {NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version},
-               { 0, 0, 0, 0}
-       };
-
-       pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
-                           POPT_CONTEXT_KEEP_FIRST);
+       pc = poptGetContext("net", argc, (const char **) argv, long_options, 
+                               POPT_CONTEXT_KEEP_FIRST);
 
        while((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
-               case 'h':
-                       net_help(&ctx, argc, argv);
-                       exit(0);
-                       break;
                default:
                        d_printf("Invalid option %s: %s\n", 
                                 poptBadOption(pc, 0), poptStrerror(opt));
-                       net_help(&ctx, argc, argv);
+                       net_usage(ctx, argc, argv);
                        exit(1);
                }
        }
@@ -172,15 +186,29 @@ static int binary_net(int argc, const char **argv)
        }
 
        if (argc_new < 2) {
-               d_printf("Usage: TODO\n");
-               return 1;
+               return net_usage(ctx, argc, argv);
        }
 
-       rc = net_run_function(&ctx, argc_new-1, argv_new+1, net_functable, net_help_msg);
+       net_init_subsystems;
+
+       mem_ctx = talloc_init("net_context");
+       ctx = talloc(mem_ctx, struct net_context);
+       if (!ctx) {
+               d_printf("talloc_init(net_context) failed\n");
+               exit(1);
+       }
+
+       ZERO_STRUCTP(ctx);
+       ctx->mem_ctx = mem_ctx;
+       ctx->credentials = cmdline_credentials;
+
+       rc = net_run_function(ctx, argc_new-1, argv_new+1, net_functable, net_usage);
 
        if (rc != 0) {
                DEBUG(0,("return code = %d\n", rc));
        }
+
+       talloc_free(mem_ctx);
        return rc;
 }