auth/credentials: don't ignore "client use kerberos" and --use-kerberos for machine...
[Samba.git] / source4 / libcli / smb2 / find.c
blob559c77dc69f25381a0b9c0e592e2ea92842f2e03
1 /*
2 Unix SMB/CIFS implementation.
4 SMB2 client find calls
6 Copyright (C) Andrew Tridgell 2005
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
29 send a find request
31 struct smb2_request *smb2_find_send(struct smb2_tree *tree, struct smb2_find *io)
33 struct smb2_request *req;
34 NTSTATUS status;
36 req = smb2_request_init_tree(tree, SMB2_OP_QUERY_DIRECTORY, 0x20, true, 0);
37 if (req == NULL) return NULL;
38 req->credit_charge = (MAX(io->in.max_response_size, 1) - 1)/ 65536 + 1;
40 SCVAL(req->out.body, 0x02, io->in.level);
41 SCVAL(req->out.body, 0x03, io->in.continue_flags);
42 SIVAL(req->out.body, 0x04, io->in.file_index);
43 smb2_push_handle(req->out.body+0x08, &io->in.file.handle);
45 status = smb2_push_o16s16_string(&req->out, 0x18, io->in.pattern);
46 if (!NT_STATUS_IS_OK(status)) {
47 talloc_free(req);
48 return NULL;
51 SIVAL(req->out.body, 0x1C, io->in.max_response_size);
53 smb2_transport_send(req);
55 return req;
60 recv a find reply
62 NTSTATUS smb2_find_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
63 struct smb2_find *io)
65 NTSTATUS status;
67 if (!smb2_request_receive(req) ||
68 smb2_request_is_error(req)) {
69 return smb2_request_destroy(req);
72 SMB2_CHECK_PACKET_RECV(req, 0x08, true);
74 status = smb2_pull_o16s32_blob(&req->in, mem_ctx,
75 req->in.body+0x02, &io->out.blob);
76 if (!NT_STATUS_IS_OK(status)) {
77 return status;
80 return smb2_request_destroy(req);
84 sync find request
86 NTSTATUS smb2_find(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
87 struct smb2_find *io)
89 struct smb2_request *req = smb2_find_send(tree, io);
90 return smb2_find_recv(req, mem_ctx, io);
95 a variant of smb2_find_recv that parses the resulting blob into
96 smb_search_data structures
98 NTSTATUS smb2_find_level_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
99 uint8_t level, unsigned int *count,
100 union smb_search_data **io)
102 struct smb2_find f;
103 NTSTATUS status;
104 DATA_BLOB b;
105 enum smb_search_data_level smb_level;
106 unsigned int next_ofs=0;
108 switch (level) {
109 case SMB2_FIND_DIRECTORY_INFO:
110 smb_level = RAW_SEARCH_DATA_DIRECTORY_INFO;
111 break;
112 case SMB2_FIND_FULL_DIRECTORY_INFO:
113 smb_level = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO;
114 break;
115 case SMB2_FIND_BOTH_DIRECTORY_INFO:
116 smb_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
117 break;
118 case SMB2_FIND_NAME_INFO:
119 smb_level = RAW_SEARCH_DATA_NAME_INFO;
120 break;
121 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
122 smb_level = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO;
123 break;
124 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
125 smb_level = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO;
126 break;
127 default:
128 return NT_STATUS_INVALID_INFO_CLASS;
131 status = smb2_find_recv(req, mem_ctx, &f);
132 NT_STATUS_NOT_OK_RETURN(status);
134 b = f.out.blob;
135 *io = NULL;
136 *count = 0;
138 do {
139 union smb_search_data *io2;
141 io2 = talloc_realloc(mem_ctx, *io, union smb_search_data, (*count)+1);
142 if (io2 == NULL) {
143 data_blob_free(&f.out.blob);
144 talloc_free(*io);
145 return NT_STATUS_NO_MEMORY;
147 *io = io2;
149 status = smb_raw_search_common(*io, smb_level, &b, (*io) + (*count),
150 &next_ofs, STR_UNICODE);
152 if (NT_STATUS_IS_OK(status) &&
153 next_ofs >= b.length) {
154 data_blob_free(&f.out.blob);
155 talloc_free(*io);
156 return NT_STATUS_INFO_LENGTH_MISMATCH;
159 (*count)++;
161 b = data_blob_const(b.data+next_ofs, b.length - next_ofs);
162 } while (NT_STATUS_IS_OK(status) && next_ofs != 0);
164 data_blob_free(&f.out.blob);
166 return NT_STATUS_OK;
170 a variant of smb2_find that parses the resulting blob into
171 smb_search_data structures
173 NTSTATUS smb2_find_level(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
174 struct smb2_find *f,
175 unsigned int *count, union smb_search_data **io)
177 struct smb2_request *req;
179 req = smb2_find_send(tree, f);
180 return smb2_find_level_recv(req, mem_ctx, f->in.level, count, io);