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.
21 #include "lib/util/debug.h"
22 #include "popt_common.h"
23 #include "popt_common_cmdline.h"
24 #include "lib/cmdline_contexts.h"
26 #include "auth_info.h"
28 #include "libsmb/proto.h"
29 #include "librpc/rpc/rpc_common.h"
30 #include "rpc_client/cli_pipe.h"
31 #include "rpc_client/cli_mdssvc.h"
32 #include "librpc/gen_ndr/ndr_mdssvc_c.h"
34 static char *opt_path;
37 static struct poptOption long_options[] = {
42 .argInfo = POPT_ARG_STRING,
44 .descrip = "Server-relative search path",
49 .argInfo = POPT_ARG_NONE,
51 .descrip = "live query",
54 POPT_COMMON_CREDENTIALS
58 int main(int argc, char **argv)
60 const char **const_argv = discard_const_p(const char *, argv);
61 TALLOC_CTX *frame = talloc_stackframe();
62 struct tevent_context *ev = NULL;
63 struct user_auth_info *auth = NULL;
64 struct cli_credentials *creds = NULL;
65 struct rpc_pipe_client *rpccli = NULL;
66 struct mdscli_ctx *mdscli_ctx = NULL;
67 struct mdscli_search_ctx *search = NULL;
68 const char *server = NULL;
69 const char *share = NULL;
70 const char *mds_query = NULL;
71 struct cli_state *cli = NULL;
72 char *basepath = NULL;
73 uint32_t flags = CLI_FULL_CONNECTION_IPC;
74 int signing_state = SMB_SIGNING_IPC_DEFAULT;
75 uint64_t *cnids = NULL;
83 setup_logging(argv[0], DEBUG_STDERR);
85 lp_set_cmdline("log level", "1");
87 pc = poptGetContext(argv[0],
91 POPT_CONTEXT_KEEP_FIRST);
93 poptSetOtherOptionHelp(pc, "mdfind [OPTIONS] <server> <share> <query>\n");
95 while ((opt = poptGetNextOpt(pc)) != -1) {
96 DBG_ERR("Invalid option %s: %s\n",
99 poptPrintHelp(pc, stderr, 0);
103 poptGetArg(pc); /* Drop argv[0], the program name */
104 server = poptGetArg(pc);
105 share = poptGetArg(pc);
106 mds_query = poptGetArg(pc);
108 if (server == NULL || mds_query == NULL) {
109 poptPrintHelp(pc, stderr, 0);
113 popt_burn_cmdline_password(argc, argv);
115 if ((server[0] == '/' && server[1] == '/') ||
116 (server[0] == '\\' && server[1] == '\\'))
121 auth = popt_get_cmdline_auth_info();
122 creds = get_cmdline_auth_info_creds(auth);
124 signing_state = get_cmdline_auth_info_signing_state(auth);
125 switch (signing_state) {
126 case SMB_SIGNING_OFF:
127 lp_set_cmdline("client ipc signing", "no");
129 case SMB_SIGNING_REQUIRED:
130 lp_set_cmdline("client ipc signing", "required");
134 ev = samba_tevent_context_init(frame);
139 cmdline_messaging_context(get_dyn_CONFIGFILE());
141 ok = lp_load_client(get_dyn_CONFIGFILE());
143 fprintf(stderr, "ERROR: Can't load %s\n",
144 get_dyn_CONFIGFILE());
148 status = cli_full_connection_creds(&cli,
157 if (!NT_STATUS_IS_OK(status)) {
158 DBG_ERR("Cannot connect to server: %s\n", nt_errstr(status));
162 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_mdssvc, &rpccli);
163 if (!NT_STATUS_IS_OK(status)) {
167 status = mdscli_connect(frame,
168 rpccli->binding_handle,
172 if (!NT_STATUS_IS_OK(status)) {
173 printf("Failed to connect mdssvc\n");
177 if (opt_path == NULL) {
178 basepath = mdscli_get_basepath(frame, mdscli_ctx);
180 basepath = talloc_strdup(frame, opt_path);
182 if (basepath == NULL) {
186 status = mdscli_search(frame,
190 opt_live == 1 ? true : false,
192 if (!NT_STATUS_IS_OK(status)) {
193 printf("mdscli_search failed\n");
202 status = mdscli_get_results(frame,
205 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_MATCHES)) {
212 if (!NT_STATUS_IS_OK(status)) {
213 printf("mdscli_get_results failed\n");
217 ncnids = talloc_array_length(cnids);
222 for (i = 0; i < ncnids; i++) {
225 status = mdscli_get_path(frame,
229 if (!NT_STATUS_IS_OK(status)) {
230 printf("Get path for CNID 0x%"PRIx64" failed\n",
234 printf("%s\n", path);
239 status = mdscli_close_search(&search);
240 if (!NT_STATUS_IS_OK(status)) {
241 printf("mdscli_close_search failed\n");
245 status = mdscli_disconnect(mdscli_ctx);
246 if (!NT_STATUS_IS_OK(status)) {
247 printf("mdscli_disconnect failed\n");
251 cmdline_messaging_context_free();