r3478: split out some more pieces of includes.h
[gd/samba-autobuild/.git] / source4 / utils / net / net.c
1 /* 
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)
9
10    Largely rewritten by metze in August 2004
11
12    Originally written by Steve and Jim. Largely rewritten by tridge in
13    November 2001.
14
15    Reworked again by abartlet in December 2001
16
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.
21    
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.
26    
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.
30 */
31  
32 /*****************************************************/
33 /*                                                   */
34 /*   Distributed SMB/CIFS Server Management Utility  */
35 /*                                                   */
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     */
40 /*   systems).                                       */
41 /*                                                   */
42 /*****************************************************/
43
44 #include "includes.h"
45 #include "utils/net/net.h"
46 #include "lib/cmdline/popt_common.h"
47
48 /*
49   run a function from a function table. If not found then
50   call the specified usage function 
51 */
52 int net_run_function(struct net_context *ctx,
53                         int argc, const char **argv,
54                         const struct net_functable *functable, 
55                         int (*usage_fn)(struct net_context *ctx, int argc, const char **argv))
56 {
57         int i;
58
59         if (argc < 1) {
60                 d_printf("Usage: \n");
61                 return usage_fn(ctx, argc, argv);
62         }
63
64         for (i=0; functable[i].name; i++) {
65                 if (StrCaseCmp(argv[0], functable[i].name) == 0)
66                         return functable[i].fn(ctx, argc-1, argv+1);
67         }
68
69         d_printf("No command: %s\n", argv[0]);
70         return usage_fn(ctx, argc, argv);
71 }
72
73 /*
74   run a usage function from a function table. If not found then fail
75 */
76 int net_run_usage(struct net_context *ctx,
77                         int argc, const char **argv,
78                         const struct net_functable *functable)
79 {
80         int i;
81
82         if (argc < 1) {
83                 d_printf("net_run_usage: TODO (argc < 1)\n");
84                 return 1;
85         }
86
87         for (i=0; functable[i].name; i++) {
88                 if (StrCaseCmp(argv[0], functable[i].name) == 0)
89                         if (functable[i].usage) {
90                                 return functable[i].usage(ctx, argc-1, argv+1);
91                         }
92         }
93
94         d_printf("No usage for command: %s\n", argv[0]);
95
96         return 1;
97 }
98
99 /*
100   run a usage function from a function table. If not found then fail
101 */
102 int net_run_help(struct net_context *ctx,
103                         int argc, const char **argv,
104                         const struct net_functable *functable)
105 {
106         int i;
107
108         if (argc < 1) {
109                 d_printf("net_run_help: TODO (argc < 1)\n");
110                 return 1;
111         }
112
113         for (i=0; functable[i].name; i++) {
114                 if (StrCaseCmp(argv[0], functable[i].name) == 0)
115                         if (functable[i].help) {
116                                 return functable[i].help(ctx, argc-1, argv+1);
117                         }
118         }
119
120         d_printf("No help for command: %s\n", argv[0]);
121
122         return 1;
123 }
124
125 static int net_help(struct net_context *ctx, int argc, const char **argv)
126 {
127         d_printf("net_help: TODO\n");
128         return 0;
129 }
130
131 static int net_help_usage(struct net_context *ctx, int argc, const char **argv)
132 {
133         d_printf("net_help_usage: TODO\n");
134         return 0;       
135 }
136
137 /* main function table */
138 static const struct net_functable const net_functable[] = {
139         {"password", net_password, net_password_usage, net_password_help},
140         {"time", net_time, net_time_usage, net_time_help},
141
142         {"help", net_help, net_help_usage, net_help},
143         {NULL, NULL}
144 };
145
146 static int net_usage(struct net_context *ctx, int argc, const char **argv)
147 {
148         return net_run_usage(ctx, argc, argv, net_functable);
149 }
150
151 /****************************************************************************
152   main program
153 ****************************************************************************/
154 static int binary_net(int argc, const char **argv)
155 {
156         int opt,i;
157         int rc;
158         int argc_new;
159         const char **argv_new;
160         TALLOC_CTX *mem_ctx;
161         struct net_context *ctx;
162         poptContext pc;
163         struct poptOption long_options[] = {
164                 POPT_AUTOHELP
165                 POPT_COMMON_SAMBA
166                 POPT_COMMON_CONNECTION
167                 POPT_COMMON_CREDENTIALS
168                 POPT_COMMON_VERSION
169                 POPT_TABLEEND
170         };
171
172         setup_logging("net", DEBUG_STDOUT);
173
174 #ifdef HAVE_SETBUFFER
175         setbuffer(stdout, NULL, 0);
176 #endif
177
178         pc = poptGetContext("net", argc, (const char **) argv, long_options, 
179                                 POPT_CONTEXT_KEEP_FIRST);
180
181         while((opt = poptGetNextOpt(pc)) != -1) {
182                 switch (opt) {
183                 default:
184                         d_printf("Invalid option %s: %s\n", 
185                                  poptBadOption(pc, 0), poptStrerror(opt));
186                         net_help(ctx, argc, argv);
187                         exit(1);
188                 }
189         }
190
191         lp_load(dyn_CONFIGFILE,True,False,False);
192         load_interfaces();
193
194         argv_new = (const char **)poptGetArgs(pc);
195
196         argc_new = argc;
197         for (i=0; i<argc; i++) {
198                 if (argv_new[i] == NULL) {
199                         argc_new = i;
200                         break;
201                 }
202         }
203
204         if (argc_new < 2) {
205                 d_printf("Usage: TODO\n");
206                 return 1;
207         }
208
209         mem_ctx = talloc_init("net_context");
210         ctx = talloc_p(mem_ctx, struct net_context);
211         if (!ctx) {
212                 d_printf("talloc_init(net_context) failed\n");
213                 exit(1);
214         }
215
216         ZERO_STRUCTP(ctx);
217         ctx->mem_ctx = mem_ctx;
218         ctx->user.account_name = talloc_strdup(ctx->mem_ctx, cmdline_get_username());
219         ctx->user.domain_name = talloc_strdup(ctx->mem_ctx, cmdline_get_userdomain());
220         ctx->user.password = talloc_strdup(ctx->mem_ctx, cmdline_get_userpassword());
221
222         rc = net_run_function(ctx, argc_new-1, argv_new+1, net_functable, net_usage);
223
224         if (rc != 0) {
225                 DEBUG(0,("return code = %d\n", rc));
226         }
227
228         talloc_destroy(mem_ctx);
229         return rc;
230 }
231
232  int main(int argc, const char **argv)
233 {
234         return binary_net(argc, argv);
235 }