s3: libsmb: In cli_ntrename_internal_send() (SMBntrename) check for DFS dst pathname.
[Samba.git] / source3 / utils / mdsearch.c
blobac0b75fca513b9dfbcf80b771b447ad26c37d1b7
1 /*
2 * Copyright (C) 2019, Ralph Boehme <slow@samba.org.>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "includes.h"
21 #include "lib/util/debug.h"
22 #include "lib/cmdline/cmdline.h"
23 #include "lib/cmdline_contexts.h"
24 #include "param.h"
25 #include "client.h"
26 #include "libsmb/proto.h"
27 #include "librpc/rpc/rpc_common.h"
28 #include "rpc_client/cli_pipe.h"
29 #include "rpc_client/cli_mdssvc.h"
30 #include "librpc/gen_ndr/ndr_mdssvc_c.h"
32 static char *opt_path;
33 static int opt_live;
35 int main(int argc, char **argv)
37 const char **const_argv = discard_const_p(const char *, argv);
38 TALLOC_CTX *frame = talloc_stackframe();
39 struct tevent_context *ev = NULL;
40 struct cli_credentials *creds = NULL;
41 struct rpc_pipe_client *rpccli = NULL;
42 struct mdscli_ctx *mdscli_ctx = NULL;
43 struct mdscli_search_ctx *search = NULL;
44 const char *server = NULL;
45 const char *share = NULL;
46 const char *mds_query = NULL;
47 struct cli_state *cli = NULL;
48 char *basepath = NULL;
49 uint32_t flags = CLI_FULL_CONNECTION_IPC;
50 uint64_t *cnids = NULL;
51 size_t ncnids;
52 size_t i;
53 int opt;
54 poptContext pc;
55 NTSTATUS status;
56 bool ok;
58 struct poptOption long_options[] = {
59 POPT_AUTOHELP
61 .longName = "path",
62 .shortName = 'p',
63 .argInfo = POPT_ARG_STRING,
64 .arg = &opt_path,
65 .descrip = "Server-relative search path",
68 .longName = "live",
69 .shortName = 'L',
70 .argInfo = POPT_ARG_NONE,
71 .arg = &opt_live,
72 .descrip = "live query",
74 POPT_COMMON_SAMBA
75 POPT_COMMON_CREDENTIALS
76 POPT_LEGACY_S3
77 POPT_COMMON_VERSION
78 POPT_TABLEEND
81 smb_init_locale();
83 ok = samba_cmdline_init(frame,
84 SAMBA_CMDLINE_CONFIG_CLIENT,
85 false /* require_smbconf */);
86 if (!ok) {
87 DBG_ERR("Failed to init cmdline parser!\n");
88 TALLOC_FREE(frame);
89 exit(1);
91 lp_set_cmdline("log level", "1");
93 pc = samba_popt_get_context(getprogname(),
94 argc,
95 const_argv,
96 long_options,
97 POPT_CONTEXT_KEEP_FIRST);
99 poptSetOtherOptionHelp(pc, "mdsearch [OPTIONS] <server> <share> <query>\n");
101 while ((opt = poptGetNextOpt(pc)) != -1) {
102 DBG_ERR("Invalid option %s: %s\n",
103 poptBadOption(pc, 0),
104 poptStrerror(opt));
105 poptPrintHelp(pc, stderr, 0);
106 goto fail;
109 poptGetArg(pc); /* Drop argv[0], the program name */
110 server = poptGetArg(pc);
111 share = poptGetArg(pc);
112 mds_query = poptGetArg(pc);
114 if (server == NULL || mds_query == NULL) {
115 poptPrintHelp(pc, stderr, 0);
116 goto fail;
119 samba_cmdline_burn(argc, argv);
121 if ((server[0] == '/' && server[1] == '/') ||
122 (server[0] == '\\' && server[1] == '\\'))
124 server += 2;
127 ev = samba_tevent_context_init(frame);
128 if (ev == NULL) {
129 goto fail;
132 cmdline_messaging_context(get_dyn_CONFIGFILE());
134 creds = samba_cmdline_get_creds();
136 status = cli_full_connection_creds(&cli,
137 lp_netbios_name(),
138 server,
139 NULL,
141 "IPC$",
142 "IPC",
143 creds,
144 flags);
145 if (!NT_STATUS_IS_OK(status)) {
146 DBG_ERR("Cannot connect to server: %s\n", nt_errstr(status));
147 goto fail;
150 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_mdssvc, &rpccli);
151 if (!NT_STATUS_IS_OK(status)) {
152 goto fail;
155 status = mdscli_connect(frame,
156 rpccli->binding_handle,
157 share,
158 "/foo/bar",
159 &mdscli_ctx);
160 if (!NT_STATUS_IS_OK(status)) {
161 printf("Failed to connect mdssvc\n");
162 goto fail;
165 if (opt_path == NULL) {
166 basepath = mdscli_get_basepath(frame, mdscli_ctx);
167 } else {
168 basepath = talloc_strdup(frame, opt_path);
170 if (basepath == NULL) {
171 goto fail;
174 status = mdscli_search(frame,
175 mdscli_ctx,
176 mds_query,
177 basepath,
178 opt_live == 1 ? true : false,
179 &search);
180 if (!NT_STATUS_IS_OK(status)) {
181 printf("mdscli_search failed\n");
182 goto fail;
185 if (!opt_live) {
186 sleep(1);
189 while (true) {
190 status = mdscli_get_results(frame,
191 search,
192 &cnids);
193 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_MATCHES)) {
194 if (opt_live) {
195 sleep(1);
196 continue;
198 break;
200 if (!NT_STATUS_IS_OK(status)) {
201 printf("mdscli_get_results failed\n");
202 goto fail;
205 ncnids = talloc_array_length(cnids);
206 if (ncnids == 0) {
207 break;
210 for (i = 0; i < ncnids; i++) {
211 char *path = NULL;
213 status = mdscli_get_path(frame,
214 mdscli_ctx,
215 cnids[i],
216 &path);
217 if (!NT_STATUS_IS_OK(status)) {
218 printf("Get path for CNID 0x%"PRIx64" failed\n",
219 cnids[i]);
220 goto fail;
222 printf("%s\n", path);
223 TALLOC_FREE(path);
227 status = mdscli_close_search(&search);
228 if (!NT_STATUS_IS_OK(status)) {
229 printf("mdscli_close_search failed\n");
230 goto fail;
233 status = mdscli_disconnect(mdscli_ctx);
234 if (!NT_STATUS_IS_OK(status)) {
235 printf("mdscli_disconnect failed\n");
236 goto fail;
239 cmdline_messaging_context_free();
240 TALLOC_FREE(frame);
241 poptFreeContext(pc);
242 return 0;
244 fail:
245 TALLOC_FREE(frame);
246 return 1;