r23792: convert Samba4 to GPLv3
[bbaumbach/samba-autobuild/.git] / source4 / libcli / smb2 / find.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client find calls
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26
27 /*
28   send a find request
29 */
30 struct smb2_request *smb2_find_send(struct smb2_tree *tree, struct smb2_find *io)
31 {
32         struct smb2_request *req;
33         NTSTATUS status;
34
35         req = smb2_request_init_tree(tree, SMB2_OP_FIND, 0x20, True, 0);
36         if (req == NULL) return NULL;
37
38         SCVAL(req->out.body, 0x02, io->in.level);
39         SCVAL(req->out.body, 0x03, io->in.continue_flags);
40         SIVAL(req->out.body, 0x04, io->in.unknown);
41         smb2_push_handle(req->out.body+0x08, &io->in.file.handle);
42
43         status = smb2_push_o16s16_string(&req->out, 0x18, io->in.pattern);
44         if (!NT_STATUS_IS_OK(status)) {
45                 talloc_free(req);
46                 return NULL;
47         }
48
49         SIVAL(req->out.body, 0x1C, io->in.max_response_size);
50
51         smb2_transport_send(req);
52
53         return req;
54 }
55
56
57 /*
58   recv a find reply
59 */
60 NTSTATUS smb2_find_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
61                            struct smb2_find *io)
62 {
63         NTSTATUS status;
64
65         if (!smb2_request_receive(req) || 
66             smb2_request_is_error(req)) {
67                 return smb2_request_destroy(req);
68         }
69
70         SMB2_CHECK_PACKET_RECV(req, 0x08, True);
71
72         status = smb2_pull_o16s32_blob(&req->in, mem_ctx, 
73                                        req->in.body+0x02, &io->out.blob);
74         if (!NT_STATUS_IS_OK(status)) {
75                 return status;
76         }
77
78         return smb2_request_destroy(req);
79 }
80
81 /*
82   sync find request
83 */
84 NTSTATUS smb2_find(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
85                    struct smb2_find *io)
86 {
87         struct smb2_request *req = smb2_find_send(tree, io);
88         return smb2_find_recv(req, mem_ctx, io);
89 }
90
91
92 /*
93   a varient of smb2_find_recv that parses the resulting blob into
94   smb_search_data structures
95 */
96 NTSTATUS smb2_find_level_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
97                               uint8_t level, uint_t *count,
98                               union smb_search_data **io)
99 {
100         struct smb2_find f;
101         NTSTATUS status;
102         DATA_BLOB b;
103         enum smb_search_data_level smb_level;
104         uint_t next_ofs=0;
105
106         switch (level) {
107         case SMB2_FIND_DIRECTORY_INFO:
108                 smb_level = RAW_SEARCH_DATA_DIRECTORY_INFO;
109                 break;
110         case SMB2_FIND_FULL_DIRECTORY_INFO:
111                 smb_level = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO;
112                 break;
113         case SMB2_FIND_BOTH_DIRECTORY_INFO:
114                 smb_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
115                 break;
116         case SMB2_FIND_NAME_INFO:
117                 smb_level = RAW_SEARCH_DATA_NAME_INFO;
118                 break;
119         case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
120                 smb_level = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO;
121                 break;
122         case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
123                 smb_level = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO;
124                 break;
125         default:
126                 return NT_STATUS_INVALID_INFO_CLASS;
127         }
128
129         status = smb2_find_recv(req, mem_ctx, &f);
130         NT_STATUS_NOT_OK_RETURN(status);
131         
132         b = f.out.blob;
133         *io = NULL;
134         *count = 0;
135
136         do {
137                 union smb_search_data *io2;
138
139                 io2 = talloc_realloc(mem_ctx, *io, union smb_search_data, (*count)+1);
140                 if (io2 == NULL) {
141                         data_blob_free(&f.out.blob);
142                         talloc_free(*io);
143                         return NT_STATUS_NO_MEMORY;
144                 }
145                 *io = io2;
146
147                 status = smb_raw_search_common(*io, smb_level, &b, (*io) + (*count), 
148                                                &next_ofs, STR_UNICODE);
149
150                 if (NT_STATUS_IS_OK(status) &&
151                     next_ofs >= b.length) {
152                         data_blob_free(&f.out.blob);
153                         talloc_free(*io);
154                         return NT_STATUS_INFO_LENGTH_MISMATCH;                  
155                 }
156
157                 (*count)++;
158
159                 b = data_blob_const(b.data+next_ofs, b.length - next_ofs);
160         } while (NT_STATUS_IS_OK(status) && next_ofs != 0);
161
162         data_blob_free(&f.out.blob);
163         
164         return NT_STATUS_OK;
165 }
166
167 /*
168   a varient of smb2_find that parses the resulting blob into
169   smb_search_data structures
170 */
171 NTSTATUS smb2_find_level(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
172                          struct smb2_find *f, 
173                          uint_t *count, union smb_search_data **io)
174 {
175         struct smb2_request *req;
176
177         req = smb2_find_send(tree, f);
178         return smb2_find_level_recv(req, mem_ctx, f->in.level, count, io);
179 }