[1] | 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 |
|
---|
[165] | 34 | struct smb_name_list {
|
---|
| 35 | struct smb_name_list *prev, *next;
|
---|
[1] | 36 | pstring name, comment;
|
---|
| 37 | uint32 server_type;
|
---|
| 38 | };
|
---|
| 39 |
|
---|
[165] | 40 | static struct smb_name_list *workgroups, *servers, *shares;
|
---|
[1] | 41 |
|
---|
[165] | 42 | static void free_name_list(struct smb_name_list *list)
|
---|
[1] | 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 | {
|
---|
[165] | 51 | struct smb_name_list **name_list = (struct smb_name_list **)state;
|
---|
| 52 | struct smb_name_list *new_name;
|
---|
[1] | 53 |
|
---|
[165] | 54 | new_name = SMB_MALLOC_P(struct smb_name_list);
|
---|
[1] | 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 | ENUM_HND enum_hnd;
|
---|
| 138 | WERROR werr;
|
---|
| 139 | SRV_SHARE_INFO_CTR ctr;
|
---|
| 140 | int i;
|
---|
| 141 |
|
---|
| 142 | mem_ctx = talloc_new(NULL);
|
---|
| 143 | if (mem_ctx == NULL) {
|
---|
| 144 | DEBUG(0, ("talloc_new failed\n"));
|
---|
| 145 | return False;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | init_enum_hnd(&enum_hnd, 0);
|
---|
| 149 |
|
---|
| 150 | pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
|
---|
| 151 |
|
---|
| 152 | if (pipe_hnd == NULL) {
|
---|
| 153 | DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
|
---|
| 154 | nt_errstr(status)));
|
---|
| 155 | TALLOC_FREE(mem_ctx);
|
---|
| 156 | return False;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | werr = rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, 1, &ctr,
|
---|
| 160 | 0xffffffff, &enum_hnd);
|
---|
| 161 |
|
---|
| 162 | if (!W_ERROR_IS_OK(werr)) {
|
---|
| 163 | TALLOC_FREE(mem_ctx);
|
---|
| 164 | cli_rpc_pipe_close(pipe_hnd);
|
---|
| 165 | return False;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | for (i=0; i<ctr.num_entries; i++) {
|
---|
| 169 | SRV_SHARE_INFO_1 *info = &ctr.share.info1[i];
|
---|
| 170 | char *name, *comment;
|
---|
| 171 | name = rpcstr_pull_unistr2_talloc(
|
---|
| 172 | mem_ctx, &info->info_1_str.uni_netname);
|
---|
| 173 | comment = rpcstr_pull_unistr2_talloc(
|
---|
| 174 | mem_ctx, &info->info_1_str.uni_remark);
|
---|
| 175 | fn(name, info->info_1.type, comment, state);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | TALLOC_FREE(mem_ctx);
|
---|
| 179 | cli_rpc_pipe_close(pipe_hnd);
|
---|
| 180 | return True;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
|
---|
| 185 | {
|
---|
| 186 | struct cli_state *cli;
|
---|
| 187 |
|
---|
| 188 | if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
|
---|
| 189 | return False;
|
---|
| 190 |
|
---|
| 191 | if (get_rpc_shares(cli, add_name, &shares))
|
---|
| 192 | return True;
|
---|
| 193 |
|
---|
| 194 | if (!cli_RNetShareEnum(cli, add_name, &shares))
|
---|
| 195 | return False;
|
---|
| 196 |
|
---|
| 197 | return True;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | static BOOL print_tree(struct user_auth_info *user_info)
|
---|
| 201 | {
|
---|
[165] | 202 | struct smb_name_list *wg, *sv, *sh;
|
---|
[1] | 203 |
|
---|
| 204 | /* List workgroups */
|
---|
| 205 |
|
---|
| 206 | if (!get_workgroups(user_info))
|
---|
| 207 | return False;
|
---|
| 208 |
|
---|
| 209 | for (wg = workgroups; wg; wg = wg->next) {
|
---|
| 210 |
|
---|
| 211 | printf("%s\n", wg->name);
|
---|
| 212 |
|
---|
| 213 | /* List servers */
|
---|
| 214 |
|
---|
| 215 | free_name_list(servers);
|
---|
| 216 | servers = NULL;
|
---|
| 217 |
|
---|
| 218 | if (level == LEV_WORKGROUP ||
|
---|
| 219 | !get_servers(wg->name, user_info))
|
---|
| 220 | continue;
|
---|
| 221 |
|
---|
| 222 | for (sv = servers; sv; sv = sv->next) {
|
---|
| 223 |
|
---|
| 224 | printf("\t\\\\%-15s\t\t%s\n",
|
---|
| 225 | sv->name, sv->comment);
|
---|
| 226 |
|
---|
| 227 | /* List shares */
|
---|
| 228 |
|
---|
| 229 | free_name_list(shares);
|
---|
| 230 | shares = NULL;
|
---|
| 231 |
|
---|
| 232 | if (level == LEV_SERVER ||
|
---|
| 233 | !get_shares(sv->name, user_info))
|
---|
| 234 | continue;
|
---|
| 235 |
|
---|
| 236 | for (sh = shares; sh; sh = sh->next) {
|
---|
| 237 | printf("\t\t\\\\%s\\%-15s\t%s\n",
|
---|
| 238 | sv->name, sh->name, sh->comment);
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | return True;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /****************************************************************************
|
---|
| 247 | main program
|
---|
| 248 | ****************************************************************************/
|
---|
| 249 | int main(int argc,char *argv[])
|
---|
| 250 | {
|
---|
| 251 | struct poptOption long_options[] = {
|
---|
| 252 | POPT_AUTOHELP
|
---|
| 253 | { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
|
---|
| 254 | { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
|
---|
| 255 | { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
|
---|
| 256 | POPT_COMMON_SAMBA
|
---|
| 257 | POPT_COMMON_CREDENTIALS
|
---|
| 258 | POPT_TABLEEND
|
---|
| 259 | };
|
---|
| 260 | poptContext pc;
|
---|
| 261 |
|
---|
| 262 | /* Initialise samba stuff */
|
---|
| 263 | load_case_tables();
|
---|
| 264 |
|
---|
| 265 | setlinebuf(stdout);
|
---|
| 266 |
|
---|
| 267 | dbf = x_stderr;
|
---|
| 268 |
|
---|
| 269 | setup_logging(argv[0],True);
|
---|
| 270 |
|
---|
| 271 | pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
|
---|
| 272 | POPT_CONTEXT_KEEP_FIRST);
|
---|
| 273 | while(poptGetNextOpt(pc) != -1);
|
---|
| 274 | poptFreeContext(pc);
|
---|
| 275 |
|
---|
| 276 | lp_load(dyn_CONFIGFILE,True,False,False,True);
|
---|
| 277 | load_interfaces();
|
---|
| 278 |
|
---|
| 279 | /* Parse command line args */
|
---|
| 280 |
|
---|
| 281 | if (!cmdline_auth_info.got_pass) {
|
---|
| 282 | char *pass = getpass("Password: ");
|
---|
| 283 | if (pass) {
|
---|
| 284 | pstrcpy(cmdline_auth_info.password, pass);
|
---|
| 285 | }
|
---|
| 286 | cmdline_auth_info.got_pass = True;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /* Now do our stuff */
|
---|
| 290 |
|
---|
| 291 | if (!print_tree(&cmdline_auth_info))
|
---|
| 292 | return 1;
|
---|
| 293 |
|
---|
| 294 | return 0;
|
---|
| 295 | }
|
---|