This removes the 3rd argument from init_unistr2(). There were 240
[tprouty/samba.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 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 NTSTATUS 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         NTSTATUS result;
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                 result = NT_STATUS_UNSUCCESSFUL;
52                 goto done;
53         }
54
55         /* Unmarshall response */
56
57         r.ctr = ctr;
58
59         if (!srv_io_r_net_srv_get_info("", &r, &rbuf, 0)) {
60                 result = NT_STATUS_UNSUCCESSFUL;
61                 goto done;
62         }
63
64         result = werror_to_ntstatus(r.status);
65
66  done:
67         prs_mem_free(&qbuf);
68         prs_mem_free(&rbuf);
69
70         return result;
71 }
72
73 WERROR cli_srvsvc_net_share_enum(struct cli_state *cli, TALLOC_CTX *mem_ctx,
74                                  uint32 info_level, SRV_SHARE_INFO_CTR *ctr,
75                                  int preferred_len, ENUM_HND *hnd)
76 {
77         prs_struct qbuf, rbuf;
78         SRV_Q_NET_SHARE_ENUM q;
79         SRV_R_NET_SHARE_ENUM r;
80         WERROR result = W_ERROR(ERRgeneral);
81         int i;
82
83         ZERO_STRUCT(q);
84         ZERO_STRUCT(r);
85
86         /* Initialise parse structures */
87
88         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
89         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
90
91         /* Initialise input parameters */
92
93         init_srv_q_net_share_enum(
94                 &q, cli->srv_name_slash, info_level, preferred_len, hnd);
95
96         /* Marshall data and send request */
97
98         if (!srv_io_q_net_share_enum("", &q, &qbuf, 0) ||
99             !rpc_api_pipe_req(cli, SRV_NET_SHARE_ENUM_ALL, &qbuf, &rbuf))
100                 goto done;
101
102         /* Unmarshall response */
103
104         if (!srv_io_r_net_share_enum("", &r, &rbuf, 0))
105                 goto done;
106
107         result = r.status;
108
109         if (!W_ERROR_IS_OK(result))
110                 goto done;
111
112         /* Oh yuck yuck yuck - we have to copy all the info out of the
113            SRV_SHARE_INFO_CTR in the SRV_R_NET_SHARE_ENUM as when we do a
114            prs_mem_free() it will all be invalidated.  The various share
115            info structures suck badly too.  This really is gross. */
116
117         ZERO_STRUCTP(ctr);
118
119         if (!r.ctr.num_entries)
120                 goto done;
121
122         ctr->info_level = info_level;
123         ctr->num_entries = r.ctr.num_entries;
124
125         switch(info_level) {
126         case 1:
127                 ctr->share.info1 = (SRV_SHARE_INFO_1 *)talloc(
128                         mem_ctx, sizeof(SRV_SHARE_INFO_1) * ctr->num_entries);
129                 
130                 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
131
132                 for (i = 0; i < ctr->num_entries; i++) {
133                         SRV_SHARE_INFO_1 *info1 = &ctr->share.info1[i];
134                         char *s;
135                         
136                         /* Copy pointer crap */
137
138                         memcpy(&info1->info_1, &r.ctr.share.info1[i].info_1, 
139                                sizeof(SH_INFO_1));
140
141                         /* Duplicate strings */
142
143                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info1[i].info_1_str.uni_netname);
144                         if (s)
145                                 init_unistr2(&info1->info_1_str.uni_netname, s);
146                 
147                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info1[i].info_1_str.uni_remark);
148                         if (s)
149                                 init_unistr2(&info1->info_1_str.uni_remark, s);
150
151                 }               
152
153                 break;
154         case 2:
155                 ctr->share.info2 = (SRV_SHARE_INFO_2 *)talloc(
156                         mem_ctx, sizeof(SRV_SHARE_INFO_2) * ctr->num_entries);
157                 
158                 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
159
160                 for (i = 0; i < ctr->num_entries; i++) {
161                         SRV_SHARE_INFO_2 *info2 = &ctr->share.info2[i];
162                         char *s;
163                         
164                         /* Copy pointer crap */
165
166                         memcpy(&info2->info_2, &r.ctr.share.info2[i].info_2, 
167                                sizeof(SH_INFO_2));
168
169                         /* Duplicate strings */
170
171                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_netname);
172                         if (s)
173                                 init_unistr2(&info2->info_2_str.uni_netname, s);
174
175                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_remark);
176                         if (s)
177                                 init_unistr2(&info2->info_2_str.uni_remark, s);
178
179                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_path);
180                         if (s)
181                                 init_unistr2(&info2->info_2_str.uni_path, s);
182
183                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_passwd);
184                         if (s)
185                                 init_unistr2(&info2->info_2_str.uni_passwd, s);
186                 }
187                 break;
188         }
189  done:
190         prs_mem_free(&qbuf);
191         prs_mem_free(&rbuf);
192
193         return result;
194 }
195
196 WERROR cli_srvsvc_net_share_del(struct cli_state *cli, TALLOC_CTX *mem_ctx,
197                                 const char *sharename)
198 {
199         prs_struct qbuf, rbuf;
200         SRV_Q_NET_SHARE_DEL q;
201         SRV_R_NET_SHARE_DEL r;
202         WERROR result = W_ERROR(ERRgeneral);
203
204         ZERO_STRUCT(q);
205         ZERO_STRUCT(r);
206
207         /* Initialise parse structures */
208
209         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
210         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
211
212         /* Initialise input parameters */
213
214         init_srv_q_net_share_del(&q, cli->srv_name_slash, sharename);
215
216         /* Marshall data and send request */
217
218         if (!srv_io_q_net_share_del("", &q, &qbuf, 0) ||
219             !rpc_api_pipe_req(cli, SRV_NET_SHARE_DEL, &qbuf, &rbuf))
220                 goto done;
221
222         /* Unmarshall response */
223
224         if (!srv_io_r_net_share_del("", &r, &rbuf, 0))
225                 goto done;
226
227         result = r.status;
228
229  done:
230         prs_mem_free(&qbuf);
231         prs_mem_free(&rbuf);
232
233         return result;
234 }
235
236 WERROR cli_srvsvc_net_share_add(struct cli_state *cli, TALLOC_CTX *mem_ctx,
237                                 char *netname, uint32 type, char *remark, 
238                                 uint32 perms, uint32 max_uses, uint32 num_uses,
239                                 char *path, char *passwd)
240 {
241         prs_struct qbuf, rbuf;
242         SRV_Q_NET_SHARE_ADD q;
243         SRV_R_NET_SHARE_ADD r;
244         WERROR result = W_ERROR(ERRgeneral);
245
246         ZERO_STRUCT(q);
247         ZERO_STRUCT(r);
248
249         /* Initialise parse structures */
250
251         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
252         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
253
254         init_srv_q_net_share_add(&q,cli->srv_name_slash, netname, type, remark,
255                                  perms, max_uses, num_uses, path, passwd);
256
257         /* Marshall data and send request */
258
259         if (!srv_io_q_net_share_add("", &q, &qbuf, 0) ||
260             !rpc_api_pipe_req(cli, SRV_NET_SHARE_ADD, &qbuf, &rbuf))
261                 goto done;
262
263         /* Unmarshall response */
264
265         if (!srv_io_r_net_share_add("", &r, &rbuf, 0))
266                 goto done;
267
268         result = r.status;
269
270  done:
271         prs_mem_free(&qbuf);
272         prs_mem_free(&rbuf);
273
274         return result;  
275 }
276
277 WERROR cli_srvsvc_net_remote_tod(struct cli_state *cli, TALLOC_CTX *mem_ctx,
278                                  char *server, TIME_OF_DAY_INFO *tod)
279 {
280         prs_struct qbuf, rbuf;
281         SRV_Q_NET_REMOTE_TOD q;
282         SRV_R_NET_REMOTE_TOD r;
283         WERROR result = W_ERROR(ERRgeneral);
284
285         ZERO_STRUCT(q);
286         ZERO_STRUCT(r);
287
288         /* Initialise parse structures */
289
290         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
291         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
292
293         /* Initialise input parameters */
294
295         init_srv_q_net_remote_tod(&q, cli->srv_name_slash);
296
297         /* Marshall data and send request */
298
299         if (!srv_io_q_net_remote_tod("", &q, &qbuf, 0) ||
300             !rpc_api_pipe_req(cli, SRV_NET_REMOTE_TOD, &qbuf, &rbuf))
301                 goto done;
302
303         /* Unmarshall response */
304
305         r.tod = tod;
306
307         if (!srv_io_r_net_remote_tod("", &r, &rbuf, 0))
308                 goto done;
309
310         result = r.status;
311
312         if (!W_ERROR_IS_OK(result))
313                 goto done;
314
315  done:
316         prs_mem_free(&qbuf);
317         prs_mem_free(&rbuf);
318
319         return result;  
320 }
321
322 WERROR cli_srvsvc_net_file_enum(struct cli_state *cli, TALLOC_CTX *mem_ctx,
323                                 uint32 file_level, const char *user_name,
324                                 SRV_FILE_INFO_CTR *ctr, int preferred_len,
325                                 ENUM_HND *hnd)
326 {
327         prs_struct qbuf, rbuf;
328         SRV_Q_NET_FILE_ENUM q;
329         SRV_R_NET_FILE_ENUM r;
330         WERROR result = W_ERROR(ERRgeneral);
331         int i;
332
333         ZERO_STRUCT(q);
334         ZERO_STRUCT(r);
335
336         /* Initialise parse structures */
337
338         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
339         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
340
341         /* Initialise input parameters */
342
343         init_srv_q_net_file_enum(&q, cli->srv_name_slash, NULL, user_name, 
344                                  file_level, ctr, preferred_len, hnd);
345
346         /* Marshall data and send request */
347
348         if (!srv_io_q_net_file_enum("", &q, &qbuf, 0) ||
349             !rpc_api_pipe_req(cli, SRV_NET_FILE_ENUM, &qbuf, &rbuf))
350                 goto done;
351
352         /* Unmarshall response */
353
354         if (!srv_io_r_net_file_enum("", &r, &rbuf, 0))
355                 goto done;
356
357         result = r.status;
358
359         if (!W_ERROR_IS_OK(result))
360                 goto done;
361
362         /* copy the data over to the ctr */
363
364         ZERO_STRUCTP(ctr);
365
366         ctr->switch_value = file_level;
367
368         ctr->num_entries = ctr->num_entries2 = r.ctr.num_entries;
369         
370         switch(file_level) {
371         case 3:
372                 ctr->file.info3 = (SRV_FILE_INFO_3 *)talloc(
373                         mem_ctx, sizeof(SRV_FILE_INFO_3) * ctr->num_entries);
374
375                 memset(ctr->file.info3, 0, 
376                        sizeof(SRV_FILE_INFO_3) * ctr->num_entries);
377
378                 for (i = 0; i < r.ctr.num_entries; i++) {
379                         SRV_FILE_INFO_3 *info3 = &ctr->file.info3[i];
380                         char *s;
381                         
382                         /* Copy pointer crap */
383
384                         memcpy(&info3->info_3, &r.ctr.file.info3[i].info_3, 
385                                sizeof(FILE_INFO_3));
386
387                         /* Duplicate strings */
388
389                         s = unistr2_tdup(mem_ctx, &r.ctr.file.info3[i].info_3_str.uni_path_name);
390                         if (s)
391                                 init_unistr2(&info3->info_3_str.uni_path_name, s);
392                 
393                         s = unistr2_tdup(mem_ctx, &r.ctr.file.info3[i].info_3_str.uni_user_name);
394                         if (s)
395                                 init_unistr2(&info3->info_3_str.uni_user_name, s);
396
397                 }               
398
399                 break;
400         }
401
402  done:
403         prs_mem_free(&qbuf);
404         prs_mem_free(&rbuf);
405
406         return result;
407 }
408
409 WERROR cli_srvsvc_net_file_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
410                                  uint32 file_id)
411 {
412         prs_struct qbuf, rbuf;
413         SRV_Q_NET_FILE_CLOSE q;
414         SRV_R_NET_FILE_CLOSE r;
415         WERROR result = W_ERROR(ERRgeneral);
416
417         ZERO_STRUCT(q);
418         ZERO_STRUCT(r);
419
420         /* Initialise parse structures */
421
422         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
423         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
424
425         /* Initialise input parameters */
426
427         init_srv_q_net_file_close(&q, cli->srv_name_slash, file_id);
428
429         /* Marshall data and send request */
430
431         if (!srv_io_q_net_file_close("", &q, &qbuf, 0) ||
432             !rpc_api_pipe_req(cli, SRV_NET_FILE_CLOSE, &qbuf, &rbuf))
433                 goto done;
434
435         /* Unmarshall response */
436
437         if (!srv_io_r_net_file_close("", &r, &rbuf, 0))
438                 goto done;
439
440         result = r.status;
441  done:
442         prs_mem_free(&qbuf);
443         prs_mem_free(&rbuf);
444         return result;
445 }