r12620: Get rid of automatically generated lists of init functions of subsystems.
[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 "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 == 0) {
60                 return usage_fn(ctx, argc, argv);
61
62         } else if (argc == 1 && strequal(argv[0], "help")) {
63                 return net_help(ctx, functable);
64         }
65
66         for (i=0; functable[i].name; i++) {
67                 if (strcasecmp_m(argv[0], functable[i].name) == 0)
68                         return functable[i].fn(ctx, argc-1, argv+1);
69         }
70
71         d_printf("No command: %s\n", argv[0]);
72         return usage_fn(ctx, argc, argv);
73 }
74
75 /*
76   run a usage function from a function table. If not found then fail
77 */
78 int net_run_usage(struct net_context *ctx,
79                         int argc, const char **argv,
80                         const struct net_functable *functable)
81 {
82         int i;
83 /*
84         if (argc < 1) {
85                 d_printf("net_run_usage: TODO (argc < 1)\n");
86                 return 1;
87         }
88 */
89         for (i=0; functable[i].name; i++) {
90                 if (strcasecmp_m(argv[0], functable[i].name) == 0)
91                         if (functable[i].usage) {
92                                 return functable[i].usage(ctx, argc-1, argv+1);
93                         }
94         }
95
96         d_printf("No usage information for command: %s\n", argv[0]);
97
98         return 1;
99 }
100
101
102 /* main function table */
103 static const struct net_functable net_functable[] = {
104         {"password", "change password\n", net_password, net_password_usage},
105         {"time", "get remote server's time\n", net_time, net_time_usage},
106         {"join", "join a domain\n", net_join, net_join_usage},
107         {"samdump", "dump the sam of a domain\n", net_samdump, net_samdump_usage},
108         {"samsync", "syncrosnise into the local ldb the sam of a domain\n", net_samsync_ldb, net_samsync_ldb_usage},
109         {"user", "manage user accounts\n", net_user, net_user_usage},
110         {NULL, NULL, NULL, NULL}
111 };
112
113 int net_help(struct net_context *ctx, const struct net_functable *ftable)
114 {
115         int i = 0;
116         const char *name = ftable[i].name;
117         const char *desc = ftable[i].desc;
118
119         d_printf("Available commands:\n");
120         while (name && desc) {
121                 d_printf("\t%s\t\t%s", name, desc);
122                 name = ftable[++i].name;
123                 desc = ftable[i].desc;
124         }
125
126         return 0;
127 }
128
129 static int net_usage(struct net_context *ctx, int argc, const char **argv)
130 {
131         d_printf("Usage:\n");
132         d_printf("net <command> [options]\n");
133         return 0;
134 }
135
136 /****************************************************************************
137   main program
138 ****************************************************************************/
139 static int binary_net(int argc, const char **argv)
140 {
141         int opt,i;
142         int rc;
143         int argc_new;
144         const char **argv_new;
145         TALLOC_CTX *mem_ctx;
146         struct net_context *ctx = NULL;
147         poptContext pc;
148         struct poptOption long_options[] = {
149                 POPT_AUTOHELP
150                 POPT_COMMON_SAMBA
151                 POPT_COMMON_CONNECTION
152                 POPT_COMMON_CREDENTIALS
153                 POPT_COMMON_VERSION
154                 POPT_TABLEEND
155         };
156
157 #ifdef HAVE_SETBUFFER
158         setbuffer(stdout, NULL, 0);
159 #endif
160
161         pc = poptGetContext("net", argc, (const char **) argv, long_options, 
162                             POPT_CONTEXT_KEEP_FIRST);
163
164         while((opt = poptGetNextOpt(pc)) != -1) {
165                 switch (opt) {
166                 default:
167                         d_printf("Invalid option %s: %s\n", 
168                                  poptBadOption(pc, 0), poptStrerror(opt));
169                         net_usage(ctx, argc, argv);
170                         exit(1);
171                 }
172         }
173
174         argv_new = (const char **)poptGetArgs(pc);
175
176         argc_new = argc;
177         for (i=0; i<argc; i++) {
178                 if (argv_new[i] == NULL) {
179                         argc_new = i;
180                         break;
181                 }
182         }
183
184         if (argc_new < 2) {
185                 return net_usage(ctx, argc, argv);
186         }
187
188         dcerpc_init();
189
190         mem_ctx = talloc_init("net_context");
191         ctx = talloc(mem_ctx, struct net_context);
192         if (!ctx) {
193                 d_printf("talloc_init(net_context) failed\n");
194                 exit(1);
195         }
196
197         ZERO_STRUCTP(ctx);
198         ctx->mem_ctx = mem_ctx;
199         ctx->credentials = cmdline_credentials;
200
201         rc = net_run_function(ctx, argc_new-1, argv_new+1, net_functable, net_usage);
202
203         if (rc != 0) {
204                 DEBUG(0,("return code = %d\n", rc));
205         }
206
207         talloc_free(mem_ctx);
208         return rc;
209 }
210
211  int main(int argc, const char **argv)
212 {
213         return binary_net(argc, argv);
214 }