summaryrefslogtreecommitdiff
path: root/source4/libcli/smb2/find.c
blob: e8643f18684149d904879ea4d01a298b5239a2a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* 
   Unix SMB/CIFS implementation.

   SMB2 client find calls

   Copyright (C) Andrew Tridgell 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/smb2/smb2.h"
#include "libcli/smb2/smb2_calls.h"

/*
  send a find request
*/
struct smb2_request *smb2_find_send(struct smb2_tree *tree, struct smb2_find *io)
{
	struct smb2_request *req;
	NTSTATUS status;

	req = smb2_request_init_tree(tree, SMB2_OP_FIND, 0x20, True, 0);
	if (req == NULL) return NULL;

	SCVAL(req->out.body, 0x02, io->in.level);
	SCVAL(req->out.body, 0x03, io->in.continue_flags);
	SIVAL(req->out.body, 0x04, io->in.unknown);
	smb2_push_handle(req->out.body+0x08, &io->in.file.handle);

	status = smb2_push_o16s16_string(&req->out, 0x18, io->in.pattern);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(req);
		return NULL;
	}

	SIVAL(req->out.body, 0x1C, io->in.max_response_size);

	smb2_transport_send(req);

	return req;
}


/*
  recv a find reply
*/
NTSTATUS smb2_find_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
			   struct smb2_find *io)
{
	NTSTATUS status;

	if (!smb2_request_receive(req) || 
	    smb2_request_is_error(req)) {
		return smb2_request_destroy(req);
	}

	SMB2_CHECK_PACKET_RECV(req, 0x08, True);

	status = smb2_pull_o16s32_blob(&req->in, mem_ctx, 
				       req->in.body+0x02, &io->out.blob);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	return smb2_request_destroy(req);
}

/*
  sync find request
*/
NTSTATUS smb2_find(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
		   struct smb2_find *io)
{
	struct smb2_request *req = smb2_find_send(tree, io);
	return smb2_find_recv(req, mem_ctx, io);
}


/*
  a varient of smb2_find_recv that parses the resulting blob into
  smb_search_data structures
*/
NTSTATUS smb2_find_level_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
			      uint8_t level, uint_t *count,
			      union smb_search_data **io)
{
	struct smb2_find f;
	NTSTATUS status;
	DATA_BLOB b;
	enum smb_search_data_level smb_level;
	uint_t next_ofs=0;

	switch (level) {
	case SMB2_FIND_DIRECTORY_INFO:
		smb_level = RAW_SEARCH_DATA_DIRECTORY_INFO;
		break;
	case SMB2_FIND_FULL_DIRECTORY_INFO:
		smb_level = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO;
		break;
	case SMB2_FIND_BOTH_DIRECTORY_INFO:
		smb_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
		break;
	case SMB2_FIND_NAME_INFO:
		smb_level = RAW_SEARCH_DATA_NAME_INFO;
		break;
	case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
		smb_level = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO;
		break;
	case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
		smb_level = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO;
		break;
	default:
		return NT_STATUS_INVALID_INFO_CLASS;
	}

	status = smb2_find_recv(req, mem_ctx, &f);
	NT_STATUS_NOT_OK_RETURN(status);
	
	b = f.out.blob;
	*io = NULL;
	*count = 0;

	do {
		union smb_search_data *io2;

		io2 = talloc_realloc(mem_ctx, *io, union smb_search_data, (*count)+1);
		if (io2 == NULL) {
			data_blob_free(&f.out.blob);
			talloc_free(*io);
			return NT_STATUS_NO_MEMORY;
		}
		*io = io2;

		status = smb_raw_search_common(*io, smb_level, &b, (*io) + (*count), 
					       &next_ofs, STR_UNICODE);

		if (NT_STATUS_IS_OK(status) &&
		    next_ofs >= b.length) {
			data_blob_free(&f.out.blob);
			talloc_free(*io);
			return NT_STATUS_INFO_LENGTH_MISMATCH;			
		}

		(*count)++;

		b = data_blob_const(b.data+next_ofs, b.length - next_ofs);
	} while (NT_STATUS_IS_OK(status) && next_ofs != 0);

	data_blob_free(&f.out.blob);
	
	return NT_STATUS_OK;
}

/*
  a varient of smb2_find that parses the resulting blob into
  smb_search_data structures
*/
NTSTATUS smb2_find_level(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
			 struct smb2_find *f, 
			 uint_t *count, union smb_search_data **io)
{
	struct smb2_request *req;

	req = smb2_find_send(tree, f);
	return smb2_find_level_recv(req, mem_ctx, f->in.level, count, io);
}