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