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