remove unused variable
[ira/wip.git] / source3 / rpc_client / cli_srvsvc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NT Domain Authentication SMB / MSRPC client
4    Copyright (C) Andrew Tridgell 1994-2000
5    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
6    Copyright (C) Tim Potter 2001
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 WERROR cli_srvsvc_net_srv_get_info(struct cli_state *cli, 
27                                    TALLOC_CTX *mem_ctx,
28                                    uint32 switch_value, SRV_INFO_CTR *ctr)
29 {
30         prs_struct qbuf, rbuf;
31         SRV_Q_NET_SRV_GET_INFO q;
32         SRV_R_NET_SRV_GET_INFO r;
33         WERROR result = W_ERROR(ERRgeneral);
34
35         ZERO_STRUCT(q);
36         ZERO_STRUCT(r);
37
38         /* Initialise parse structures */
39
40         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
41         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
42
43         /* Initialise input parameters */
44
45         init_srv_q_net_srv_get_info(&q, cli->srv_name_slash, switch_value);
46
47         /* Marshall data and send request */
48
49         if (!srv_io_q_net_srv_get_info("", &q, &qbuf, 0) ||
50             !rpc_api_pipe_req(cli, SRV_NET_SRV_GET_INFO, &qbuf, &rbuf))
51                 goto done;
52
53         /* Unmarshall response */
54
55         r.ctr = ctr;
56
57         if (!srv_io_r_net_srv_get_info("", &r, &rbuf, 0))
58                 goto done;
59
60         result = r.status;
61
62  done:
63         prs_mem_free(&qbuf);
64         prs_mem_free(&rbuf);
65
66         return result;
67 }
68
69 WERROR cli_srvsvc_net_share_enum(struct cli_state *cli, TALLOC_CTX *mem_ctx,
70                                  uint32 info_level, SRV_SHARE_INFO_CTR *ctr,
71                                  int preferred_len, ENUM_HND *hnd)
72 {
73         prs_struct qbuf, rbuf;
74         SRV_Q_NET_SHARE_ENUM q;
75         SRV_R_NET_SHARE_ENUM r;
76         WERROR result = W_ERROR(ERRgeneral);
77         int i;
78
79         ZERO_STRUCT(q);
80         ZERO_STRUCT(r);
81
82         /* Initialise parse structures */
83
84         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
85         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
86
87         /* Initialise input parameters */
88
89         init_srv_q_net_share_enum(
90                 &q, cli->srv_name_slash, info_level, preferred_len, hnd);
91
92         /* Marshall data and send request */
93
94         if (!srv_io_q_net_share_enum("", &q, &qbuf, 0) ||
95             !rpc_api_pipe_req(cli, SRV_NET_SHARE_ENUM_ALL, &qbuf, &rbuf))
96                 goto done;
97
98         /* Unmarshall response */
99
100         if (!srv_io_r_net_share_enum("", &r, &rbuf, 0))
101                 goto done;
102
103         result = r.status;
104
105         if (!W_ERROR_IS_OK(result))
106                 goto done;
107
108         /* Oh yuck yuck yuck - we have to copy all the info out of the
109            SRV_SHARE_INFO_CTR in the SRV_R_NET_SHARE_ENUM as when we do a
110            prs_mem_free() it will all be invalidated.  The various share
111            info structures suck badly too.  This really is gross. */
112
113         ZERO_STRUCTP(ctr);
114
115         if (!r.ctr.num_entries)
116                 goto done;
117
118         ctr->info_level = info_level;
119         ctr->num_entries = r.ctr.num_entries;
120
121         switch(info_level) {
122         case 1:
123                 ctr->share.info1 = (SRV_SHARE_INFO_1 *)talloc(
124                         mem_ctx, sizeof(SRV_SHARE_INFO_1) * ctr->num_entries);
125                 
126                 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
127
128                 for (i = 0; i < ctr->num_entries; i++) {
129                         SRV_SHARE_INFO_1 *info1 = &ctr->share.info1[i];
130                         char *s;
131                         
132                         /* Copy pointer crap */
133
134                         memcpy(&info1->info_1, &r.ctr.share.info1[i].info_1, 
135                                sizeof(SH_INFO_1));
136
137                         /* Duplicate strings */
138
139                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info1[i].info_1_str.uni_netname);
140                         if (s)
141                                 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
142                 
143                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info1[i].info_1_str.uni_remark);
144                         if (s)
145                                 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
146
147                 }               
148
149                 break;
150         case 2:
151                 ctr->share.info2 = (SRV_SHARE_INFO_2 *)talloc(
152                         mem_ctx, sizeof(SRV_SHARE_INFO_2) * ctr->num_entries);
153                 
154                 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
155
156                 for (i = 0; i < ctr->num_entries; i++) {
157                         SRV_SHARE_INFO_2 *info2 = &ctr->share.info2[i];
158                         char *s;
159                         
160                         /* Copy pointer crap */
161
162                         memcpy(&info2->info_2, &r.ctr.share.info2[i].info_2, 
163                                sizeof(SH_INFO_2));
164
165                         /* Duplicate strings */
166
167                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_netname);
168                         if (s)
169                                 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
170
171                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_remark);
172                         if (s)
173                                 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
174
175                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_path);
176                         if (s)
177                                 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
178
179                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_passwd);
180                         if (s)
181                                 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
182                 }
183                 break;
184         }
185  done:
186         prs_mem_free(&qbuf);
187         prs_mem_free(&rbuf);
188
189         return result;
190 }
191
192 WERROR cli_srvsvc_net_share_del(struct cli_state *cli, TALLOC_CTX *mem_ctx,
193                                 const char *sharename)
194 {
195         prs_struct qbuf, rbuf;
196         SRV_Q_NET_SHARE_DEL q;
197         SRV_R_NET_SHARE_DEL r;
198         WERROR result = W_ERROR(ERRgeneral);
199
200         ZERO_STRUCT(q);
201         ZERO_STRUCT(r);
202
203         /* Initialise parse structures */
204
205         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
206         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
207
208         /* Initialise input parameters */
209
210         init_srv_q_net_share_del(&q, cli->srv_name_slash, sharename);
211
212         /* Marshall data and send request */
213
214         if (!srv_io_q_net_share_del("", &q, &qbuf, 0) ||
215             !rpc_api_pipe_req(cli, SRV_NET_SHARE_DEL, &qbuf, &rbuf))
216                 goto done;
217
218         /* Unmarshall response */
219
220         if (!srv_io_r_net_share_del("", &r, &rbuf, 0))
221                 goto done;
222
223         result = r.status;
224
225  done:
226         prs_mem_free(&qbuf);
227         prs_mem_free(&rbuf);
228
229         return result;
230 }
231
232 WERROR cli_srvsvc_net_share_add(struct cli_state *cli, TALLOC_CTX *mem_ctx,
233                                 const char *netname, uint32 type, 
234                                 const char *remark, uint32 perms, 
235                                 uint32 max_uses, uint32 num_uses, 
236                                 const char *path, const char *passwd)
237 {
238         prs_struct qbuf, rbuf;
239         SRV_Q_NET_SHARE_ADD q;
240         SRV_R_NET_SHARE_ADD r;
241         WERROR result = W_ERROR(ERRgeneral);
242
243         ZERO_STRUCT(q);
244         ZERO_STRUCT(r);
245
246         /* Initialise parse structures */
247
248         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
249         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
250
251         init_srv_q_net_share_add(&q,cli->srv_name_slash, netname, type, remark,
252                                  perms, max_uses, num_uses, path, passwd);
253
254         /* Marshall data and send request */
255
256         if (!srv_io_q_net_share_add("", &q, &qbuf, 0) ||
257             !rpc_api_pipe_req(cli, SRV_NET_SHARE_ADD, &qbuf, &rbuf))
258                 goto done;
259
260         /* Unmarshall response */
261
262         if (!srv_io_r_net_share_add("", &r, &rbuf, 0))
263                 goto done;
264
265         result = r.status;
266
267  done:
268         prs_mem_free(&qbuf);
269         prs_mem_free(&rbuf);
270
271         return result;  
272 }
273
274 WERROR cli_srvsvc_net_remote_tod(struct cli_state *cli, TALLOC_CTX *mem_ctx,
275                                  char *server, TIME_OF_DAY_INFO *tod)
276 {
277         prs_struct qbuf, rbuf;
278         SRV_Q_NET_REMOTE_TOD q;
279         SRV_R_NET_REMOTE_TOD r;
280         WERROR result = W_ERROR(ERRgeneral);
281
282         ZERO_STRUCT(q);
283         ZERO_STRUCT(r);
284
285         /* Initialise parse structures */
286
287         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
288         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
289
290         /* Initialise input parameters */
291
292         init_srv_q_net_remote_tod(&q, cli->srv_name_slash);
293
294         /* Marshall data and send request */
295
296         if (!srv_io_q_net_remote_tod("", &q, &qbuf, 0) ||
297             !rpc_api_pipe_req(cli, SRV_NET_REMOTE_TOD, &qbuf, &rbuf))
298                 goto done;
299
300         /* Unmarshall response */
301
302         r.tod = tod;
303
304         if (!srv_io_r_net_remote_tod("", &r, &rbuf, 0))
305                 goto done;
306
307         result = r.status;
308
309         if (!W_ERROR_IS_OK(result))
310                 goto done;
311
312  done:
313         prs_mem_free(&qbuf);
314         prs_mem_free(&rbuf);
315
316         return result;  
317 }
318
319 WERROR cli_srvsvc_net_file_enum(struct cli_state *cli, TALLOC_CTX *mem_ctx,
320                                 uint32 file_level, const char *user_name,
321                                 SRV_FILE_INFO_CTR *ctr, int preferred_len,
322                                 ENUM_HND *hnd)
323 {
324         prs_struct qbuf, rbuf;
325         SRV_Q_NET_FILE_ENUM q;
326         SRV_R_NET_FILE_ENUM r;
327         WERROR result = W_ERROR(ERRgeneral);
328         int i;
329
330         ZERO_STRUCT(q);
331         ZERO_STRUCT(r);
332
333         /* Initialise parse structures */
334
335         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
336         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
337
338         /* Initialise input parameters */
339
340         init_srv_q_net_file_enum(&q, cli->srv_name_slash, NULL, user_name, 
341                                  file_level, ctr, preferred_len, hnd);
342
343         /* Marshall data and send request */
344
345         if (!srv_io_q_net_file_enum("", &q, &qbuf, 0) ||
346             !rpc_api_pipe_req(cli, SRV_NET_FILE_ENUM, &qbuf, &rbuf))
347                 goto done;
348
349         /* Unmarshall response */
350
351         if (!srv_io_r_net_file_enum("", &r, &rbuf, 0))
352                 goto done;
353
354         result = r.status;
355
356         if (!W_ERROR_IS_OK(result))
357                 goto done;
358
359         /* copy the data over to the ctr */
360
361         ZERO_STRUCTP(ctr);
362
363         ctr->switch_value = file_level;
364
365         ctr->num_entries = ctr->num_entries2 = r.ctr.num_entries;
366         
367         switch(file_level) {
368         case 3:
369                 ctr->file.info3 = (SRV_FILE_INFO_3 *)talloc(
370                         mem_ctx, sizeof(SRV_FILE_INFO_3) * ctr->num_entries);
371
372                 memset(ctr->file.info3, 0, 
373                        sizeof(SRV_FILE_INFO_3) * ctr->num_entries);
374
375                 for (i = 0; i < r.ctr.num_entries; i++) {
376                         SRV_FILE_INFO_3 *info3 = &ctr->file.info3[i];
377                         char *s;
378                         
379                         /* Copy pointer crap */
380
381                         memcpy(&info3->info_3, &r.ctr.file.info3[i].info_3, 
382                                sizeof(FILE_INFO_3));
383
384                         /* Duplicate strings */
385
386                         s = unistr2_tdup(mem_ctx, &r.ctr.file.info3[i].info_3_str.uni_path_name);
387                         if (s)
388                                 init_unistr2(&info3->info_3_str.uni_path_name, s, UNI_STR_TERMINATE);
389                 
390                         s = unistr2_tdup(mem_ctx, &r.ctr.file.info3[i].info_3_str.uni_user_name);
391                         if (s)
392                                 init_unistr2(&info3->info_3_str.uni_user_name, s, UNI_STR_TERMINATE);
393
394                 }               
395
396                 break;
397         }
398
399  done:
400         prs_mem_free(&qbuf);
401         prs_mem_free(&rbuf);
402
403         return result;
404 }
405
406 WERROR cli_srvsvc_net_file_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
407                                  uint32 file_id)
408 {
409         prs_struct qbuf, rbuf;
410         SRV_Q_NET_FILE_CLOSE q;
411         SRV_R_NET_FILE_CLOSE r;
412         WERROR result = W_ERROR(ERRgeneral);
413
414         ZERO_STRUCT(q);
415         ZERO_STRUCT(r);
416
417         /* Initialise parse structures */
418
419         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
420         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
421
422         /* Initialise input parameters */
423
424         init_srv_q_net_file_close(&q, cli->srv_name_slash, file_id);
425
426         /* Marshall data and send request */
427
428         if (!srv_io_q_net_file_close("", &q, &qbuf, 0) ||
429             !rpc_api_pipe_req(cli, SRV_NET_FILE_CLOSE, &qbuf, &rbuf))
430                 goto done;
431
432         /* Unmarshall response */
433
434         if (!srv_io_r_net_file_close("", &r, &rbuf, 0))
435                 goto done;
436
437         result = r.status;
438  done:
439         prs_mem_free(&qbuf);
440         prs_mem_free(&rbuf);
441         return result;
442 }