r7534: Add missing cli_srvsvc_net_share_set_info-function and
[sfrench/samba-autobuild/.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, PI_SRVSVC, 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, PI_SRVSVC, 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 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, ctr->num_entries);
124                 
125                 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
126
127                 for (i = 0; i < ctr->num_entries; i++) {
128                         SRV_SHARE_INFO_1 *info1 = &ctr->share.info1[i];
129                         char *s;
130                         
131                         /* Copy pointer crap */
132
133                         memcpy(&info1->info_1, &r.ctr.share.info1[i].info_1, 
134                                sizeof(SH_INFO_1));
135
136                         /* Duplicate strings */
137
138                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info1[i].info_1_str.uni_netname);
139                         if (s)
140                                 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
141                 
142                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info1[i].info_1_str.uni_remark);
143                         if (s)
144                                 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
145
146                 }               
147
148                 break;
149         case 2:
150                 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, ctr->num_entries);
151                 
152                 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
153
154                 for (i = 0; i < ctr->num_entries; i++) {
155                         SRV_SHARE_INFO_2 *info2 = &ctr->share.info2[i];
156                         char *s;
157                         
158                         /* Copy pointer crap */
159
160                         memcpy(&info2->info_2, &r.ctr.share.info2[i].info_2, 
161                                sizeof(SH_INFO_2));
162
163                         /* Duplicate strings */
164
165                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_netname);
166                         if (s)
167                                 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
168
169                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_remark);
170                         if (s)
171                                 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
172
173                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_path);
174                         if (s)
175                                 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
176
177                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info2[i].info_2_str.uni_passwd);
178                         if (s)
179                                 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
180                 }
181                 break;
182         /* adding info-level 502 here */
183         case 502:
184                 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, ctr->num_entries);
185                 
186                 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
187
188                 for (i = 0; i < ctr->num_entries; i++) {
189                         SRV_SHARE_INFO_502 *info502 = &ctr->share.info502[i];
190                         char *s;
191                         
192                         /* Copy pointer crap */
193                         memcpy(&info502->info_502, &r.ctr.share.info502[i].info_502, 
194                                sizeof(SH_INFO_502));
195
196                         /* Duplicate strings */
197
198                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info502[i].info_502_str.uni_netname);
199                         if (s)
200                                 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
201
202                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info502[i].info_502_str.uni_remark);
203                         if (s)
204                                 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
205
206                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info502[i].info_502_str.uni_path);
207                         if (s)
208                                 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
209
210                         s = unistr2_tdup(mem_ctx, &r.ctr.share.info502[i].info_502_str.uni_passwd);
211                         if (s)
212                                 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
213                 
214                         info502->info_502_str.sd = dup_sec_desc(mem_ctx, r.ctr.share.info502[i].info_502_str.sd);
215                 }
216                 break;
217         }
218  done:
219         prs_mem_free(&qbuf);
220         prs_mem_free(&rbuf);
221
222         return result;
223 }
224
225 WERROR cli_srvsvc_net_share_get_info(struct cli_state *cli,
226                                      TALLOC_CTX *mem_ctx,
227                                      const char *sharename,
228                                      uint32 info_level,
229                                      SRV_SHARE_INFO *info)
230 {
231         prs_struct qbuf, rbuf;
232         SRV_Q_NET_SHARE_GET_INFO q;
233         SRV_R_NET_SHARE_GET_INFO r;
234         WERROR result = W_ERROR(ERRgeneral);
235
236         ZERO_STRUCT(q);
237         ZERO_STRUCT(r);
238
239         /* Initialise parse structures */
240
241         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
242         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
243
244         /* Initialise input parameters */
245
246         init_srv_q_net_share_get_info(&q, cli->srv_name_slash, sharename,
247                                       info_level);
248
249         /* Marshall data and send request */
250
251         if (!srv_io_q_net_share_get_info("", &q, &qbuf, 0) ||
252             !rpc_api_pipe_req(cli, PI_SRVSVC, SRV_NET_SHARE_GET_INFO, &qbuf, &rbuf))
253                 goto done;
254
255         /* Unmarshall response */
256
257         if (!srv_io_r_net_share_get_info("", &r, &rbuf, 0))
258                 goto done;
259
260         result = r.status;
261
262         if (!W_ERROR_IS_OK(result))
263                 goto done;
264
265         ZERO_STRUCTP(info);
266
267         info->switch_value = info_level;
268
269         switch(info_level) {
270         case 502:
271         {
272                 SRV_SHARE_INFO_502 *info502 = &info->share.info502;
273                 SH_INFO_502_STR *info502_str = &info502->info_502_str;
274                 
275                 char *s;
276
277                 info->share.info502 = r.info.share.info502;
278
279                 /* Duplicate strings */
280
281                 s = unistr2_tdup(mem_ctx, &info502_str->uni_netname);
282                 if (s)
283                         init_unistr2(&info502_str->uni_netname,
284                                      s, UNI_STR_TERMINATE);
285
286                 s = unistr2_tdup(mem_ctx, &info502_str->uni_remark);
287                 if (s)
288                         init_unistr2(&info502_str->uni_remark,
289                                      s, UNI_STR_TERMINATE);
290
291                 s = unistr2_tdup(mem_ctx, &info502_str->uni_path);
292                 if (s)
293                         init_unistr2(&info502_str->uni_path,
294                                      s, UNI_STR_TERMINATE);
295
296                 s = unistr2_tdup(mem_ctx, &info502_str->uni_passwd);
297                 if (s)
298                         init_unistr2(&info502_str->uni_passwd,
299                                      s, UNI_STR_TERMINATE);
300
301                 info502_str->sd = dup_sec_desc(mem_ctx, info502_str->sd);
302                 break;
303         }
304         default:
305                 DEBUG(0,("unimplemented info-level: %d\n", info_level));
306                 break;
307         }
308
309  done:
310         prs_mem_free(&qbuf);
311         prs_mem_free(&rbuf);
312
313         return result;
314 }
315
316 WERROR cli_srvsvc_net_share_set_info(struct cli_state *cli,
317                                      TALLOC_CTX *mem_ctx,
318                                      const char *sharename,
319                                      uint32 info_level,
320                                      SRV_SHARE_INFO *info)
321 {
322         prs_struct qbuf, rbuf;
323         SRV_Q_NET_SHARE_SET_INFO q;
324         SRV_R_NET_SHARE_SET_INFO r;
325         WERROR result = W_ERROR(ERRgeneral);
326
327         ZERO_STRUCT(q);
328         ZERO_STRUCT(r);
329
330         /* Initialise parse structures */
331
332         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
333         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
334
335         /* Initialise input parameters */
336
337         init_srv_q_net_share_set_info(&q, cli->srv_name_slash, sharename,
338                                       info_level, info);
339
340         /* Marshall data and send request */
341
342         if (!srv_io_q_net_share_set_info("", &q, &qbuf, 0) ||
343             !rpc_api_pipe_req(cli, PI_SRVSVC, SRV_NET_SHARE_SET_INFO, &qbuf, &rbuf))
344                 goto done;
345
346         /* Unmarshall response */
347
348         if (!srv_io_r_net_share_set_info("", &r, &rbuf, 0))
349                 goto done;
350
351         result = r.status;
352
353         if (!W_ERROR_IS_OK(result))
354                 goto done;
355
356  done:
357         prs_mem_free(&qbuf);
358         prs_mem_free(&rbuf);
359
360         return result;
361 }
362
363 WERROR cli_srvsvc_net_share_del(struct cli_state *cli, TALLOC_CTX *mem_ctx,
364                                 const char *sharename)
365 {
366         prs_struct qbuf, rbuf;
367         SRV_Q_NET_SHARE_DEL q;
368         SRV_R_NET_SHARE_DEL r;
369         WERROR result = W_ERROR(ERRgeneral);
370
371         ZERO_STRUCT(q);
372         ZERO_STRUCT(r);
373
374         /* Initialise parse structures */
375
376         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
377         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
378
379         /* Initialise input parameters */
380
381         init_srv_q_net_share_del(&q, cli->srv_name_slash, sharename);
382
383         /* Marshall data and send request */
384
385         if (!srv_io_q_net_share_del("", &q, &qbuf, 0) ||
386             !rpc_api_pipe_req(cli, PI_SRVSVC, SRV_NET_SHARE_DEL, &qbuf, &rbuf))
387                 goto done;
388
389         /* Unmarshall response */
390
391         if (!srv_io_r_net_share_del("", &r, &rbuf, 0))
392                 goto done;
393
394         result = r.status;
395
396  done:
397         prs_mem_free(&qbuf);
398         prs_mem_free(&rbuf);
399
400         return result;
401 }
402
403 WERROR cli_srvsvc_net_share_add(struct cli_state *cli, TALLOC_CTX *mem_ctx,
404                                 const char *netname, uint32 type, 
405                                 const char *remark, uint32 perms, 
406                                 uint32 max_uses, uint32 num_uses, 
407                                 const char *path, const char *passwd,
408                                 int level, SEC_DESC *sd)
409 {
410         prs_struct qbuf, rbuf;
411         SRV_Q_NET_SHARE_ADD q;
412         SRV_R_NET_SHARE_ADD r;
413         WERROR result = W_ERROR(ERRgeneral);
414
415         ZERO_STRUCT(q);
416         ZERO_STRUCT(r);
417
418         /* Initialise parse structures */
419
420         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
421         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
422
423         init_srv_q_net_share_add(&q,cli->srv_name_slash, netname, type, remark,
424                                  perms, max_uses, num_uses, path, passwd, 
425                                  level, sd);
426
427         /* Marshall data and send request */
428
429         if (!srv_io_q_net_share_add("", &q, &qbuf, 0) ||
430             !rpc_api_pipe_req(cli, PI_SRVSVC, SRV_NET_SHARE_ADD, &qbuf, &rbuf))
431                 goto done;
432
433         /* Unmarshall response */
434
435         if (!srv_io_r_net_share_add("", &r, &rbuf, 0))
436                 goto done;
437
438         result = r.status;
439
440  done:
441         prs_mem_free(&qbuf);
442         prs_mem_free(&rbuf);
443
444         return result;  
445 }
446
447 WERROR cli_srvsvc_net_remote_tod(struct cli_state *cli, TALLOC_CTX *mem_ctx,
448                                  char *server, TIME_OF_DAY_INFO *tod)
449 {
450         prs_struct qbuf, rbuf;
451         SRV_Q_NET_REMOTE_TOD q;
452         SRV_R_NET_REMOTE_TOD r;
453         WERROR result = W_ERROR(ERRgeneral);
454
455         ZERO_STRUCT(q);
456         ZERO_STRUCT(r);
457
458         /* Initialise parse structures */
459
460         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
461         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
462
463         /* Initialise input parameters */
464
465         init_srv_q_net_remote_tod(&q, cli->srv_name_slash);
466
467         /* Marshall data and send request */
468
469         if (!srv_io_q_net_remote_tod("", &q, &qbuf, 0) ||
470             !rpc_api_pipe_req(cli, PI_SRVSVC, SRV_NET_REMOTE_TOD, &qbuf, &rbuf))
471                 goto done;
472
473         /* Unmarshall response */
474
475         r.tod = tod;
476
477         if (!srv_io_r_net_remote_tod("", &r, &rbuf, 0))
478                 goto done;
479
480         result = r.status;
481
482         if (!W_ERROR_IS_OK(result))
483                 goto done;
484
485  done:
486         prs_mem_free(&qbuf);
487         prs_mem_free(&rbuf);
488
489         return result;  
490 }
491
492 WERROR cli_srvsvc_net_file_enum(struct cli_state *cli, TALLOC_CTX *mem_ctx,
493                                 uint32 file_level, const char *user_name,
494                                 SRV_FILE_INFO_CTR *ctr, int preferred_len,
495                                 ENUM_HND *hnd)
496 {
497         prs_struct qbuf, rbuf;
498         SRV_Q_NET_FILE_ENUM q;
499         SRV_R_NET_FILE_ENUM r;
500         WERROR result = W_ERROR(ERRgeneral);
501         int i;
502
503         ZERO_STRUCT(q);
504         ZERO_STRUCT(r);
505
506         /* Initialise parse structures */
507
508         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
509         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
510
511         /* Initialise input parameters */
512
513         init_srv_q_net_file_enum(&q, cli->srv_name_slash, NULL, user_name, 
514                                  file_level, ctr, preferred_len, hnd);
515
516         /* Marshall data and send request */
517
518         if (!srv_io_q_net_file_enum("", &q, &qbuf, 0) ||
519             !rpc_api_pipe_req(cli, PI_SRVSVC, SRV_NET_FILE_ENUM, &qbuf, &rbuf))
520                 goto done;
521
522         /* Unmarshall response */
523
524         if (!srv_io_r_net_file_enum("", &r, &rbuf, 0))
525                 goto done;
526
527         result = r.status;
528
529         if (!W_ERROR_IS_OK(result))
530                 goto done;
531
532         /* copy the data over to the ctr */
533
534         ZERO_STRUCTP(ctr);
535
536         ctr->switch_value = file_level;
537
538         ctr->num_entries = ctr->num_entries2 = r.ctr.num_entries;
539         
540         switch(file_level) {
541         case 3:
542                 ctr->file.info3 = TALLOC_ARRAY(mem_ctx, SRV_FILE_INFO_3, ctr->num_entries);
543
544                 memset(ctr->file.info3, 0, 
545                        sizeof(SRV_FILE_INFO_3) * ctr->num_entries);
546
547                 for (i = 0; i < r.ctr.num_entries; i++) {
548                         SRV_FILE_INFO_3 *info3 = &ctr->file.info3[i];
549                         char *s;
550                         
551                         /* Copy pointer crap */
552
553                         memcpy(&info3->info_3, &r.ctr.file.info3[i].info_3, 
554                                sizeof(FILE_INFO_3));
555
556                         /* Duplicate strings */
557
558                         s = unistr2_tdup(mem_ctx, &r.ctr.file.info3[i].info_3_str.uni_path_name);
559                         if (s)
560                                 init_unistr2(&info3->info_3_str.uni_path_name, s, UNI_STR_TERMINATE);
561                 
562                         s = unistr2_tdup(mem_ctx, &r.ctr.file.info3[i].info_3_str.uni_user_name);
563                         if (s)
564                                 init_unistr2(&info3->info_3_str.uni_user_name, s, UNI_STR_TERMINATE);
565
566                 }               
567
568                 break;
569         }
570
571  done:
572         prs_mem_free(&qbuf);
573         prs_mem_free(&rbuf);
574
575         return result;
576 }
577
578 WERROR cli_srvsvc_net_file_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
579                                  uint32 file_id)
580 {
581         prs_struct qbuf, rbuf;
582         SRV_Q_NET_FILE_CLOSE q;
583         SRV_R_NET_FILE_CLOSE r;
584         WERROR result = W_ERROR(ERRgeneral);
585
586         ZERO_STRUCT(q);
587         ZERO_STRUCT(r);
588
589         /* Initialise parse structures */
590
591         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
592         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
593
594         /* Initialise input parameters */
595
596         init_srv_q_net_file_close(&q, cli->srv_name_slash, file_id);
597
598         /* Marshall data and send request */
599
600         if (!srv_io_q_net_file_close("", &q, &qbuf, 0) ||
601             !rpc_api_pipe_req(cli, PI_SRVSVC, SRV_NET_FILE_CLOSE, &qbuf, &rbuf))
602                 goto done;
603
604         /* Unmarshall response */
605
606         if (!srv_io_r_net_file_close("", &r, &rbuf, 0))
607                 goto done;
608
609         result = r.status;
610  done:
611         prs_mem_free(&qbuf);
612         prs_mem_free(&rbuf);
613         return result;
614 }