s3-rpc_client: Move client pipe functions to own header.
[samba.git] / source3 / utils / smbtree.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Network neighbourhood browser.
4    
5    Copyright (C) Tim Potter      2000
6    Copyright (C) Jelmer Vernooij 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "popt_common.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
26
27 static int use_bcast;
28
29 /* How low can we go? */
30
31 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
32 static enum tree_level level = LEV_SHARE;
33
34 /* Holds a list of workgroups or servers */
35
36 struct smb_name_list {
37         struct smb_name_list *prev, *next;
38         char *name, *comment;
39         uint32 server_type;
40 };
41
42 static struct smb_name_list *workgroups, *servers, *shares;
43
44 static void free_name_list(struct smb_name_list *list)
45 {
46         while(list)
47                 DLIST_REMOVE(list, list);
48 }
49
50 static void add_name(const char *machine_name, uint32 server_type,
51                      const char *comment, void *state)
52 {
53         struct smb_name_list **name_list = (struct smb_name_list **)state;
54         struct smb_name_list *new_name;
55
56         new_name = SMB_MALLOC_P(struct smb_name_list);
57
58         if (!new_name)
59                 return;
60
61         ZERO_STRUCTP(new_name);
62
63         new_name->name = SMB_STRDUP(machine_name);
64         new_name->comment = SMB_STRDUP(comment);
65         new_name->server_type = server_type;
66
67         if (!new_name->name || !new_name->comment) {
68                 SAFE_FREE(new_name->name);
69                 SAFE_FREE(new_name->comment);
70                 SAFE_FREE(new_name);
71                 return;
72         }
73
74         DLIST_ADD(*name_list, new_name);
75 }
76
77 /****************************************************************************
78   display tree of smb workgroups, servers and shares
79 ****************************************************************************/
80 static bool get_workgroups(struct user_auth_info *user_info)
81 {
82         struct cli_state *cli;
83         struct sockaddr_storage server_ss;
84         TALLOC_CTX *ctx = talloc_tos();
85         char *master_workgroup = NULL;
86
87         /* Try to connect to a #1d name of our current workgroup.  If that
88            doesn't work broadcast for a master browser and then jump off
89            that workgroup. */
90
91         master_workgroup = talloc_strdup(ctx, lp_workgroup());
92         if (!master_workgroup) {
93                 return false;
94         }
95
96         if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ss)) {
97                 DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n", 
98                           master_workgroup));
99                                 use_bcast = True;
100                 } else if(!use_bcast) {
101                         char addr[INET6_ADDRSTRLEN];
102                         print_sockaddr(addr, sizeof(addr), &server_ss);
103                         if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
104                                 return False;
105                 }
106
107                 if (!(cli = get_ipc_connect_master_ip_bcast(talloc_tos(),
108                                                         user_info,
109                                                         &master_workgroup))) {
110                         DEBUG(4, ("Unable to find master browser by "
111                                   "broadcast\n"));
112                         return False;
113         }
114
115         if (!cli_NetServerEnum(cli, master_workgroup,
116                                SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
117                 return False;
118
119         return True;
120 }
121
122 /* Retrieve the list of servers for a given workgroup */
123
124 static bool get_servers(char *workgroup, struct user_auth_info *user_info)
125 {
126         struct cli_state *cli;
127         struct sockaddr_storage server_ss;
128         char addr[INET6_ADDRSTRLEN];
129
130         /* Open an IPC$ connection to the master browser for the workgroup */
131
132         if (!find_master_ip(workgroup, &server_ss)) {
133                 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
134                           workgroup));
135                 return False;
136         }
137
138         print_sockaddr(addr, sizeof(addr), &server_ss);
139         if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
140                 return False;
141
142         if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
143                                &servers))
144                 return False;
145
146         return True;
147 }
148
149 static bool get_rpc_shares(struct cli_state *cli,
150                            void (*fn)(const char *, uint32, const char *, void *),
151                            void *state)
152 {
153         NTSTATUS status;
154         struct rpc_pipe_client *pipe_hnd = NULL;
155         TALLOC_CTX *mem_ctx;
156         WERROR werr;
157         struct srvsvc_NetShareInfoCtr info_ctr;
158         struct srvsvc_NetShareCtr1 ctr1;
159         int i;
160         uint32_t resume_handle = 0;
161         uint32_t total_entries = 0;
162         struct dcerpc_binding_handle *b;
163
164         mem_ctx = talloc_new(NULL);
165         if (mem_ctx == NULL) {
166                 DEBUG(0, ("talloc_new failed\n"));
167                 return False;
168         }
169
170         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
171                                           &pipe_hnd);
172
173         if (!NT_STATUS_IS_OK(status)) {
174                 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
175                            nt_errstr(status)));
176                 TALLOC_FREE(mem_ctx);
177                 return False;
178         }
179
180         b = pipe_hnd->binding_handle;
181
182         ZERO_STRUCT(info_ctr);
183         ZERO_STRUCT(ctr1);
184
185         info_ctr.level = 1;
186         info_ctr.ctr.ctr1 = &ctr1;
187
188         status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
189                                                pipe_hnd->desthost,
190                                                &info_ctr,
191                                                0xffffffff,
192                                                &total_entries,
193                                                &resume_handle,
194                                                &werr);
195
196         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
197                 TALLOC_FREE(mem_ctx);
198                 TALLOC_FREE(pipe_hnd);
199                 return False;
200         }
201
202         for (i=0; i<total_entries; i++) {
203                 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
204                 fn(info.name, info.type, info.comment, state);
205         }
206
207         TALLOC_FREE(mem_ctx);
208         TALLOC_FREE(pipe_hnd);
209         return True;
210 }
211
212
213 static bool get_shares(char *server_name, struct user_auth_info *user_info)
214 {
215         struct cli_state *cli;
216
217         if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
218                 return False;
219
220         if (get_rpc_shares(cli, add_name, &shares))
221                 return True;
222
223         if (!cli_RNetShareEnum(cli, add_name, &shares))
224                 return False;
225
226         return True;
227 }
228
229 static bool print_tree(struct user_auth_info *user_info)
230 {
231         struct smb_name_list *wg, *sv, *sh;
232
233         /* List workgroups */
234
235         if (!get_workgroups(user_info))
236                 return False;
237
238         for (wg = workgroups; wg; wg = wg->next) {
239
240                 printf("%s\n", wg->name);
241
242                 /* List servers */
243
244                 free_name_list(servers);
245                 servers = NULL;
246
247                 if (level == LEV_WORKGROUP || 
248                     !get_servers(wg->name, user_info))
249                         continue;
250
251                 for (sv = servers; sv; sv = sv->next) {
252
253                         printf("\t\\\\%-15s\t\t%s\n", 
254                                sv->name, sv->comment);
255
256                         /* List shares */
257
258                         free_name_list(shares);
259                         shares = NULL;
260
261                         if (level == LEV_SERVER ||
262                             !get_shares(sv->name, user_info))
263                                 continue;
264
265                         for (sh = shares; sh; sh = sh->next) {
266                                 printf("\t\t\\\\%s\\%-15s\t%s\n", 
267                                        sv->name, sh->name, sh->comment);
268                         }
269                 }
270         }
271
272         return True;
273 }
274
275 /****************************************************************************
276   main program
277 ****************************************************************************/
278  int main(int argc,char *argv[])
279 {
280         TALLOC_CTX *frame = talloc_stackframe();
281         struct user_auth_info *auth_info;
282         struct poptOption long_options[] = {
283                 POPT_AUTOHELP
284                 { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
285                 { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
286                 { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
287                 POPT_COMMON_SAMBA
288                 POPT_COMMON_CREDENTIALS
289                 POPT_TABLEEND
290         };
291         poptContext pc;
292
293         /* Initialise samba stuff */
294         load_case_tables();
295
296         setlinebuf(stdout);
297
298         setup_logging(argv[0], DEBUG_STDERR);
299
300         auth_info = user_auth_info_init(frame);
301         if (auth_info == NULL) {
302                 exit(1);
303         }
304         popt_common_set_auth_info(auth_info);
305
306         pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
307                                                 POPT_CONTEXT_KEEP_FIRST);
308         while(poptGetNextOpt(pc) != -1);
309         poptFreeContext(pc);
310
311         lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
312         load_interfaces();
313
314         /* Parse command line args */
315
316         if (get_cmdline_auth_info_use_machine_account(auth_info) &&
317             !set_cmdline_auth_info_machine_account_creds(auth_info)) {
318                 TALLOC_FREE(frame);
319                 return 1;
320         }
321
322         set_cmdline_auth_info_getpass(auth_info);
323
324         /* Now do our stuff */
325
326         if (!print_tree(auth_info)) {
327                 TALLOC_FREE(frame);
328                 return 1;
329         }
330
331         TALLOC_FREE(frame);
332         return 0;
333 }