2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Steve French (sfrench@us.ibm.com)
5 Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
7 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
8 Copyright (C) 2004 Stefan Metzmacher (metze@samba.org)
10 Largely rewritten by metze in August 2004
12 Originally written by Steve and Jim. Largely rewritten by tridge in
15 Reworked again by abartlet in December 2001
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 /*****************************************************/
34 /* Distributed SMB/CIFS Server Management Utility */
36 /* The intent was to make the syntax similar */
37 /* to the NET utility (first developed in DOS */
38 /* with additional interesting & useful functions */
39 /* added in later SMB server network operating */
42 /*****************************************************/
45 #include "dynconfig.h"
46 #include "utils/net/net.h"
47 #include "lib/cmdline/popt_common.h"
50 run a function from a function table. If not found then
51 call the specified usage function
53 int net_run_function(struct net_context *ctx,
54 int argc, const char **argv,
55 const struct net_functable *functable,
56 int (*usage_fn)(struct net_context *ctx, int argc, const char **argv))
61 return usage_fn(ctx, argc, argv);
63 } else if (argc == 1 && strequal(argv[0], "help")) {
64 return net_help(ctx, functable);
67 for (i=0; functable[i].name; i++) {
68 if (StrCaseCmp(argv[0], functable[i].name) == 0)
69 return functable[i].fn(ctx, argc-1, argv+1);
72 d_printf("No command: %s\n", argv[0]);
73 return usage_fn(ctx, argc, argv);
77 run a usage function from a function table. If not found then fail
79 int net_run_usage(struct net_context *ctx,
80 int argc, const char **argv,
81 const struct net_functable *functable)
86 d_printf("net_run_usage: TODO (argc < 1)\n");
90 for (i=0; functable[i].name; i++) {
91 if (StrCaseCmp(argv[0], functable[i].name) == 0)
92 if (functable[i].usage) {
93 return functable[i].usage(ctx, argc-1, argv+1);
97 d_printf("No usage information for command: %s\n", argv[0]);
103 /* main function table */
104 static const struct net_functable net_functable[] = {
105 {"password", "change password\n", net_password, net_password_usage},
106 {"time", "get remote server's time\n", net_time, net_time_usage},
107 {"join", "join a domain\n", net_join, net_join_usage},
108 {"user", "manage user accounts\n", net_user, net_user_usage},
109 {NULL, NULL, NULL, NULL}
112 int net_help(struct net_context *ctx, const struct net_functable *ftable)
115 const char *name = ftable[i].name;
116 const char *desc = ftable[i].desc;
118 d_printf("Available commands:\n");
119 while (name && desc) {
120 d_printf("\t%s\t\t%s", name, desc);
121 name = ftable[++i].name;
122 desc = ftable[i].desc;
128 static int net_usage(struct net_context *ctx, int argc, const char **argv)
130 d_printf("Usage:\n");
131 d_printf("net <command> [options]\n");
135 /****************************************************************************
137 ****************************************************************************/
138 static int binary_net(int argc, const char **argv)
143 const char **argv_new;
145 struct net_context *ctx = NULL;
147 struct poptOption long_options[] = {
150 POPT_COMMON_CONNECTION
151 POPT_COMMON_CREDENTIALS
156 setup_logging("net", DEBUG_STDOUT);
158 #ifdef HAVE_SETBUFFER
159 setbuffer(stdout, NULL, 0);
162 pc = poptGetContext("net", argc, (const char **) argv, long_options,
163 POPT_CONTEXT_KEEP_FIRST);
165 while((opt = poptGetNextOpt(pc)) != -1) {
168 d_printf("Invalid option %s: %s\n",
169 poptBadOption(pc, 0), poptStrerror(opt));
170 net_usage(ctx, argc, argv);
175 lp_load(dyn_CONFIGFILE,True,False,False);
178 argv_new = (const char **)poptGetArgs(pc);
181 for (i=0; i<argc; i++) {
182 if (argv_new[i] == NULL) {
189 return net_usage(ctx, argc, argv);
194 mem_ctx = talloc_init("net_context");
195 ctx = talloc(mem_ctx, struct net_context);
197 d_printf("talloc_init(net_context) failed\n");
202 ctx->mem_ctx = mem_ctx;
203 ctx->credentials = cmdline_credentials;
205 rc = net_run_function(ctx, argc_new-1, argv_new+1, net_functable, net_usage);
208 DEBUG(0,("return code = %d\n", rc));
211 talloc_free(mem_ctx);
215 int main(int argc, const char **argv)
217 return binary_net(argc, argv);