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
;
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 SMB_SIGNING_IPC_DEFAULT
);
158 if (!NT_STATUS_IS_OK(status
)) {
159 DBG_ERR("Cannot connect to server: %s\n", nt_errstr(status
));
163 if (get_cmdline_auth_info_smb_encrypt(auth
)) {
164 status
= cli_cm_force_encryption_creds(cli
, creds
, "IPC$");
165 if (!NT_STATUS_IS_OK(status
)) {
170 status
= cli_rpc_pipe_open_noauth_transport(cli
,
174 if (!NT_STATUS_IS_OK(status
)) {
178 status
= mdscli_connect(frame
,
179 rpccli
->binding_handle
,
183 if (!NT_STATUS_IS_OK(status
)) {
184 printf("Failed to connect mdssvc\n");
188 if (opt_path
== NULL
) {
189 basepath
= mdscli_get_basepath(frame
, mdscli_ctx
);
191 basepath
= talloc_strdup(frame
, opt_path
);
193 if (basepath
== NULL
) {
197 status
= mdscli_search(frame
,
201 opt_live
== 1 ? true : false,
203 if (!NT_STATUS_IS_OK(status
)) {
204 printf("mdscli_search failed\n");
213 status
= mdscli_get_results(frame
,
216 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MORE_MATCHES
)) {
223 if (!NT_STATUS_IS_OK(status
)) {
224 printf("mdscli_get_results failed\n");
228 ncnids
= talloc_array_length(cnids
);
233 for (i
= 0; i
< ncnids
; i
++) {
236 status
= mdscli_get_path(frame
,
240 if (!NT_STATUS_IS_OK(status
)) {
241 printf("Get path for CNID 0x%"PRIx64
" failed\n",
245 printf("%s\n", path
);
250 status
= mdscli_close_search(&search
);
251 if (!NT_STATUS_IS_OK(status
)) {
252 printf("mdscli_close_search failed\n");
256 status
= mdscli_disconnect(mdscli_ctx
);
257 if (!NT_STATUS_IS_OK(status
)) {
258 printf("mdscli_disconnect failed\n");