r23271: merge service control pidl change for CloseServiceHandle() from SAMBA_3_0_26
[sfrench/samba-autobuild/.git] / source / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 static BOOL use_bcast;
26
27 /* How low can we go? */
28
29 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
30 static enum tree_level level = LEV_SHARE;
31
32 /* Holds a list of workgroups or servers */
33
34 struct name_list {
35         struct name_list *prev, *next;
36         pstring name, comment;
37         uint32 server_type;
38 };
39
40 static struct name_list *workgroups, *servers, *shares;
41
42 static void free_name_list(struct name_list *list)
43 {
44         while(list)
45                 DLIST_REMOVE(list, list);
46 }
47
48 static void add_name(const char *machine_name, uint32 server_type,
49                      const char *comment, void *state)
50 {
51         struct name_list **name_list = (struct name_list **)state;
52         struct name_list *new_name;
53
54         new_name = SMB_MALLOC_P(struct name_list);
55
56         if (!new_name)
57                 return;
58
59         ZERO_STRUCTP(new_name);
60
61         pstrcpy(new_name->name, machine_name);
62         pstrcpy(new_name->comment, comment);
63         new_name->server_type = server_type;
64
65         DLIST_ADD(*name_list, new_name);
66 }
67
68 /****************************************************************************
69   display tree of smb workgroups, servers and shares
70 ****************************************************************************/
71 static BOOL get_workgroups(struct user_auth_info *user_info)
72 {
73         struct cli_state *cli;
74         struct in_addr server_ip;
75         pstring master_workgroup;
76
77         /* Try to connect to a #1d name of our current workgroup.  If that
78            doesn't work broadcast for a master browser and then jump off
79            that workgroup. */
80
81         pstrcpy(master_workgroup, lp_workgroup());
82
83         if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ip)) {
84                 DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n", 
85                           master_workgroup));
86                                 use_bcast = True;
87                 } else if(!use_bcast) {
88                 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
89                                 return False;
90                 }
91                 
92                 if (!(cli = get_ipc_connect_master_ip_bcast(master_workgroup, user_info))) {
93                         DEBUG(4, ("Unable to find master browser by "
94                                   "broadcast\n"));
95                         return False;
96         }
97
98         if (!cli_NetServerEnum(cli, master_workgroup, 
99                                SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
100                 return False;
101
102         return True;
103 }
104
105 /* Retrieve the list of servers for a given workgroup */
106
107 static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
108 {
109         struct cli_state *cli;
110         struct in_addr server_ip;
111
112         /* Open an IPC$ connection to the master browser for the workgroup */
113
114         if (!find_master_ip(workgroup, &server_ip)) {
115                 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
116                           workgroup));
117                 return False;
118         }
119
120         if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
121                 return False;
122
123         if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name, 
124                                &servers))
125                 return False;
126
127         return True;
128 }
129
130 static BOOL get_rpc_shares(struct cli_state *cli, 
131                            void (*fn)(const char *, uint32, const char *, void *),
132                            void *state)
133 {
134         NTSTATUS status;
135         struct rpc_pipe_client *pipe_hnd;
136         TALLOC_CTX *mem_ctx;
137         uint32 enum_hnd;
138         struct srvsvc_NetShareCtr1 ctr1;
139         union srvsvc_NetShareCtr ctr;
140         uint32 numentries;
141         int i;
142         uint32 info_level = 1;
143
144         mem_ctx = talloc_new(NULL);
145         if (mem_ctx == NULL) {
146                 DEBUG(0, ("talloc_new failed\n"));
147                 return False;
148         }
149
150         enum_hnd = 0;
151         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
152
153         if (pipe_hnd == NULL) {
154                 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
155                            nt_errstr(status)));
156                 TALLOC_FREE(mem_ctx);
157                 return False;
158         }
159
160         ZERO_STRUCT(ctr1);
161         level = 1;
162         ctr.ctr1 = &ctr1;
163
164         status = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
165                                             &info_level, &ctr,
166                                             0xffffffff, &numentries,
167                                             &enum_hnd);
168
169         if (!NT_STATUS_IS_OK(status)) {
170                 TALLOC_FREE(mem_ctx);
171                 cli_rpc_pipe_close(pipe_hnd);
172                 return False;
173         }
174
175         for (i=0; i<numentries; i++) {
176                 fn(ctr.ctr1->array[i].name, ctr.ctr1->array[i].type, 
177                    ctr.ctr1->array[i].comment, state);
178         }
179
180         TALLOC_FREE(mem_ctx);
181         cli_rpc_pipe_close(pipe_hnd);
182         return True;
183 }
184
185
186 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
187 {
188         struct cli_state *cli;
189
190         if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
191                 return False;
192
193         if (get_rpc_shares(cli, add_name, &shares))
194                 return True;
195         
196         if (!cli_RNetShareEnum(cli, add_name, &shares))
197                 return False;
198
199         return True;
200 }
201
202 static BOOL print_tree(struct user_auth_info *user_info)
203 {
204         struct name_list *wg, *sv, *sh;
205
206         /* List workgroups */
207
208         if (!get_workgroups(user_info))
209                 return False;
210
211         for (wg = workgroups; wg; wg = wg->next) {
212
213                 printf("%s\n", wg->name);
214
215                 /* List servers */
216
217                 free_name_list(servers);
218                 servers = NULL;
219
220                 if (level == LEV_WORKGROUP || 
221                     !get_servers(wg->name, user_info))
222                         continue;
223
224                 for (sv = servers; sv; sv = sv->next) {
225
226                         printf("\t\\\\%-15s\t\t%s\n", 
227                                sv->name, sv->comment);
228
229                         /* List shares */
230
231                         free_name_list(shares);
232                         shares = NULL;
233
234                         if (level == LEV_SERVER ||
235                             !get_shares(sv->name, user_info))
236                                 continue;
237
238                         for (sh = shares; sh; sh = sh->next) {
239                                 printf("\t\t\\\\%s\\%-15s\t%s\n", 
240                                        sv->name, sh->name, sh->comment);
241                         }
242                 }
243         }
244
245         return True;
246 }
247
248 /****************************************************************************
249   main program
250 ****************************************************************************/
251  int main(int argc,char *argv[])
252 {
253         struct poptOption long_options[] = {
254                 POPT_AUTOHELP
255                 { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
256                 { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
257                 { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
258                 POPT_COMMON_SAMBA
259                 POPT_COMMON_CREDENTIALS
260                 POPT_TABLEEND
261         };
262         poptContext pc;
263         
264         /* Initialise samba stuff */
265         load_case_tables();
266
267         setlinebuf(stdout);
268
269         dbf = x_stderr;
270
271         setup_logging(argv[0],True);
272
273         pc = poptGetContext("smbtree", argc, (const char **)argv, long_options, 
274                                                 POPT_CONTEXT_KEEP_FIRST);
275         while(poptGetNextOpt(pc) != -1);
276         poptFreeContext(pc);
277
278         lp_load(dyn_CONFIGFILE,True,False,False,True);
279         load_interfaces();
280
281         /* Parse command line args */
282
283         if (!cmdline_auth_info.got_pass) {
284                 char *pass = getpass("Password: ");
285                 if (pass) {
286                         pstrcpy(cmdline_auth_info.password, pass);
287                 }
288         cmdline_auth_info.got_pass = True;
289         }
290
291         /* Now do our stuff */
292
293         if (!print_tree(&cmdline_auth_info))
294                 return 1;
295
296         return 0;
297 }