r23792: convert Samba4 to GPLv3
[abartlet/samba.git/.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 3 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, see <http://www.gnu.org/licenses/>.
29 */
30  
31 /*****************************************************/
32 /*                                                   */
33 /*   Distributed SMB/CIFS Server Management Utility  */
34 /*                                                   */
35 /*   The intent was to make the syntax similar       */
36 /*   to the NET utility (first developed in DOS      */
37 /*   with additional interesting & useful functions  */
38 /*   added in later SMB server network operating     */
39 /*   systems).                                       */
40 /*                                                   */
41 /*****************************************************/
42
43 #include "includes.h"
44 #include "utils/net/net.h"
45 #include "lib/cmdline/popt_common.h"
46 #include "lib/ldb/include/ldb.h"
47 #include "librpc/rpc/dcerpc.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 == 0) {
61                 return usage_fn(ctx, argc, argv);
62
63         } else if (argc == 1 && strequal(argv[0], "help")) {
64                 return net_help(ctx, functable);
65         }
66
67         for (i=0; functable[i].name; i++) {
68                 if (strcasecmp_m(argv[0], functable[i].name) == 0)
69                         return functable[i].fn(ctx, argc-1, argv+1);
70         }
71
72         d_printf("No command: %s\n", argv[0]);
73         return usage_fn(ctx, argc, argv);
74 }
75
76 /*
77   run a usage function from a function table. If not found then fail
78 */
79 int net_run_usage(struct net_context *ctx,
80                         int argc, const char **argv,
81                         const struct net_functable *functable)
82 {
83         int i;
84
85         for (i=0; functable[i].name; i++) {
86                 if (strcasecmp_m(argv[0], functable[i].name) == 0)
87                         if (functable[i].usage) {
88                                 return functable[i].usage(ctx, argc-1, argv+1);
89                         }
90         }
91
92         d_printf("No usage information for command: %s\n", argv[0]);
93
94         return 1;
95 }
96
97
98 /* main function table */
99 static const struct net_functable net_functable[] = {
100         {"password", "change password\n", net_password, net_password_usage},
101         {"time", "get remote server's time\n", net_time, net_time_usage},
102         {"join", "join a domain\n", net_join, net_join_usage},
103         {"samdump", "dump the sam of a domain\n", net_samdump, net_samdump_usage},
104         {"samsync", "synchronise into the local ldb the sam of a domain\n", net_samsync_ldb, net_samsync_ldb_usage},
105         {"user", "manage user accounts\n", net_user, net_user_usage},
106         {NULL, NULL, NULL, NULL}
107 };
108
109 int net_help(struct net_context *ctx, const struct net_functable *ftable)
110 {
111         int i = 0;
112         const char *name = ftable[i].name;
113         const char *desc = ftable[i].desc;
114
115         d_printf("Available commands:\n");
116         while (name && desc) {
117                 d_printf("\t%s\t\t%s", name, desc);
118                 name = ftable[++i].name;
119                 desc = ftable[i].desc;
120         }
121
122         return 0;
123 }
124
125 static int net_usage(struct net_context *ctx, int argc, const char **argv)
126 {
127         d_printf("Usage:\n");
128         d_printf("net <command> [options]\n");
129         return 0;
130 }
131
132 /****************************************************************************
133   main program
134 ****************************************************************************/
135 static int binary_net(int argc, const char **argv)
136 {
137         int opt,i;
138         int rc;
139         int argc_new;
140         const char **argv_new;
141         TALLOC_CTX *mem_ctx;
142         struct net_context *ctx = NULL;
143         poptContext pc;
144         struct poptOption long_options[] = {
145                 POPT_AUTOHELP
146                 POPT_COMMON_SAMBA
147                 POPT_COMMON_CONNECTION
148                 POPT_COMMON_CREDENTIALS
149                 POPT_COMMON_VERSION
150                 { NULL }
151         };
152
153         setlinebuf(stdout);
154
155         pc = poptGetContext("net", argc, (const char **) argv, long_options, 
156                             POPT_CONTEXT_KEEP_FIRST);
157
158         while((opt = poptGetNextOpt(pc)) != -1) {
159                 switch (opt) {
160                 default:
161                         d_printf("Invalid option %s: %s\n", 
162                                  poptBadOption(pc, 0), poptStrerror(opt));
163                         net_usage(ctx, argc, argv);
164                         exit(1);
165                 }
166         }
167
168         argv_new = (const char **)poptGetArgs(pc);
169
170         argc_new = argc;
171         for (i=0; i<argc; i++) {
172                 if (argv_new[i] == NULL) {
173                         argc_new = i;
174                         break;
175                 }
176         }
177
178         if (argc_new < 2) {
179                 return net_usage(ctx, argc, argv);
180         }
181
182         dcerpc_init();
183
184         ldb_global_init();
185
186         mem_ctx = talloc_init("net_context");
187         ctx = talloc(mem_ctx, struct net_context);
188         if (!ctx) {
189                 d_printf("talloc_init(net_context) failed\n");
190                 exit(1);
191         }
192
193         ZERO_STRUCTP(ctx);
194         ctx->mem_ctx = mem_ctx;
195         ctx->credentials = cmdline_credentials;
196
197         rc = net_run_function(ctx, argc_new-1, argv_new+1, net_functable, net_usage);
198
199         if (rc != 0) {
200                 DEBUG(0,("return code = %d\n", rc));
201         }
202
203         talloc_free(mem_ctx);
204         return rc;
205 }
206
207  int main(int argc, const char **argv)
208 {
209         return binary_net(argc, argv);
210 }