netapi: implement NetFileEnum_r.
[Samba/gbeck.git] / source / lib / netapi / file.c
blob036af32f385afad2a5f37cf68a296e60416925bc
1 /*
2 * Unix SMB/CIFS implementation.
3 * NetApi File Support
4 * Copyright (C) Guenther Deschner 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 #include "librpc/gen_ndr/libnetapi.h"
23 #include "lib/netapi/netapi.h"
24 #include "lib/netapi/netapi_private.h"
25 #include "lib/netapi/libnetapi.h"
27 /****************************************************************
28 ****************************************************************/
30 WERROR NetFileClose_r(struct libnetapi_ctx *ctx,
31 struct NetFileClose *r)
33 WERROR werr;
34 NTSTATUS status;
35 struct cli_state *cli = NULL;
36 struct rpc_pipe_client *pipe_cli = NULL;
38 werr = libnetapi_open_pipe(ctx, r->in.server_name,
39 &ndr_table_srvsvc.syntax_id,
40 &cli,
41 &pipe_cli);
42 if (!W_ERROR_IS_OK(werr)) {
43 goto done;
46 status = rpccli_srvsvc_NetFileClose(pipe_cli, ctx,
47 r->in.server_name,
48 r->in.fileid,
49 &werr);
50 if (!W_ERROR_IS_OK(werr)) {
51 goto done;
54 done:
55 if (!cli) {
56 return werr;
59 return werr;
62 /****************************************************************
63 ****************************************************************/
65 WERROR NetFileClose_l(struct libnetapi_ctx *ctx,
66 struct NetFileClose *r)
68 LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetFileClose);
71 /****************************************************************
72 ****************************************************************/
74 static NTSTATUS map_srvsvc_FileInfo_to_FILE_INFO_buffer(TALLOC_CTX *mem_ctx,
75 uint32_t level,
76 union srvsvc_NetFileInfo *info,
77 uint8_t **buffer,
78 uint32_t *num_entries)
80 struct FILE_INFO_2 i2;
81 struct FILE_INFO_3 i3;
83 switch (level) {
84 case 2:
85 i2.fi2_id = info->info2->fid;
87 ADD_TO_ARRAY(mem_ctx, struct FILE_INFO_2, i2,
88 (struct FILE_INFO_2 **)buffer,
89 num_entries);
90 break;
91 case 3:
92 i3.fi3_id = info->info3->fid;
93 i3.fi3_permissions = info->info3->permissions;
94 i3.fi3_num_locks = info->info3->num_locks;
95 i3.fi3_pathname = talloc_strdup(mem_ctx, info->info3->path);
96 i3.fi3_username = talloc_strdup(mem_ctx, info->info3->user);
98 NT_STATUS_HAVE_NO_MEMORY(i3.fi3_pathname);
99 NT_STATUS_HAVE_NO_MEMORY(i3.fi3_username);
101 ADD_TO_ARRAY(mem_ctx, struct FILE_INFO_3, i3,
102 (struct FILE_INFO_3 **)buffer,
103 num_entries);
104 break;
105 default:
106 return NT_STATUS_INVALID_INFO_CLASS;
109 return NT_STATUS_OK;
112 /****************************************************************
113 ****************************************************************/
115 WERROR NetFileGetInfo_r(struct libnetapi_ctx *ctx,
116 struct NetFileGetInfo *r)
118 WERROR werr;
119 NTSTATUS status;
120 struct cli_state *cli = NULL;
121 struct rpc_pipe_client *pipe_cli = NULL;
122 union srvsvc_NetFileInfo info;
123 uint32_t num_entries = 0;
125 if (!r->out.buffer) {
126 return WERR_INVALID_PARAM;
129 switch (r->in.level) {
130 case 2:
131 case 3:
132 break;
133 default:
134 return WERR_UNKNOWN_LEVEL;
137 werr = libnetapi_open_pipe(ctx, r->in.server_name,
138 &ndr_table_srvsvc.syntax_id,
139 &cli,
140 &pipe_cli);
141 if (!W_ERROR_IS_OK(werr)) {
142 goto done;
145 status = rpccli_srvsvc_NetFileGetInfo(pipe_cli, ctx,
146 r->in.server_name,
147 r->in.fileid,
148 r->in.level,
149 &info,
150 &werr);
151 if (!W_ERROR_IS_OK(werr)) {
152 goto done;
155 status = map_srvsvc_FileInfo_to_FILE_INFO_buffer(ctx,
156 r->in.level,
157 &info,
158 r->out.buffer,
159 &num_entries);
160 if (!NT_STATUS_IS_OK(status)) {
161 werr = ntstatus_to_werror(status);
162 goto done;
164 done:
165 if (!cli) {
166 return werr;
169 return werr;
172 /****************************************************************
173 ****************************************************************/
175 WERROR NetFileGetInfo_l(struct libnetapi_ctx *ctx,
176 struct NetFileGetInfo *r)
178 LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetFileGetInfo);
181 /****************************************************************
182 ****************************************************************/
184 WERROR NetFileEnum_r(struct libnetapi_ctx *ctx,
185 struct NetFileEnum *r)
187 WERROR werr;
188 NTSTATUS status;
189 struct cli_state *cli = NULL;
190 struct rpc_pipe_client *pipe_cli = NULL;
191 struct srvsvc_NetFileInfoCtr info_ctr;
192 struct srvsvc_NetFileCtr2 ctr2;
193 struct srvsvc_NetFileCtr3 ctr3;
194 uint32_t num_entries = 0;
195 uint32_t i;
197 if (!r->out.buffer) {
198 return WERR_INVALID_PARAM;
201 switch (r->in.level) {
202 case 2:
203 case 3:
204 break;
205 default:
206 return WERR_UNKNOWN_LEVEL;
209 werr = libnetapi_open_pipe(ctx, r->in.server_name,
210 &ndr_table_srvsvc.syntax_id,
211 &cli,
212 &pipe_cli);
213 if (!W_ERROR_IS_OK(werr)) {
214 goto done;
217 ZERO_STRUCT(info_ctr);
219 info_ctr.level = r->in.level;
220 switch (r->in.level) {
221 case 2:
222 ZERO_STRUCT(ctr2);
223 info_ctr.ctr.ctr2 = &ctr2;
224 break;
225 case 3:
226 ZERO_STRUCT(ctr3);
227 info_ctr.ctr.ctr3 = &ctr3;
228 break;
231 status = rpccli_srvsvc_NetFileEnum(pipe_cli, ctx,
232 r->in.server_name,
233 r->in.base_path,
234 r->in.user_name,
235 &info_ctr,
236 r->in.prefmaxlen,
237 r->out.total_entries,
238 r->out.resume_handle,
239 &werr);
240 if (NT_STATUS_IS_ERR(status)) {
241 goto done;
244 for (i=0; i < info_ctr.ctr.ctr2->count; i++) {
245 union srvsvc_NetFileInfo _i;
246 switch (r->in.level) {
247 case 2:
248 _i.info2 = &info_ctr.ctr.ctr2->array[i];
249 break;
250 case 3:
251 _i.info3 = &info_ctr.ctr.ctr3->array[i];
252 break;
255 status = map_srvsvc_FileInfo_to_FILE_INFO_buffer(ctx,
256 r->in.level,
257 &_i,
258 r->out.buffer,
259 &num_entries);
260 if (!NT_STATUS_IS_OK(status)) {
261 werr = ntstatus_to_werror(status);
262 goto done;
266 if (r->out.entries_read) {
267 *r->out.entries_read = num_entries;
270 if (r->out.total_entries) {
271 *r->out.total_entries = num_entries;
274 done:
275 if (!cli) {
276 return werr;
279 return werr;
282 /****************************************************************
283 ****************************************************************/
285 WERROR NetFileEnum_l(struct libnetapi_ctx *ctx,
286 struct NetFileEnum *r)
288 LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetFileEnum);