using "abstracted" cli_connection, got \PIPE\srvsvc commands up and running
[nivanova/samba-autobuild/.git] / source3 / rpc_client / cli_srvsvc.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1997,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
8  *  Copyright (C) Paul Ashton                       1997.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #ifdef SYSLOG
27 #undef SYSLOG
28 #endif
29
30 #include "includes.h"
31
32 extern int DEBUGLEVEL;
33
34 /****************************************************************************
35 do a server net tprt enum
36 ****************************************************************************/
37 BOOL do_srv_net_srv_tprt_enum(
38                         const char *srv_name, 
39                         uint32 switch_value, SRV_TPRT_INFO_CTR *ctr,
40                         uint32 preferred_len,
41                         ENUM_HND *hnd)
42 {
43         prs_struct data; 
44         prs_struct rdata;
45         SRV_Q_NET_TPRT_ENUM q_o;
46         BOOL valid_enum = False;
47         struct cli_connection *con = NULL;
48
49         if (ctr == NULL || preferred_len == 0) return False;
50
51         if (!cli_connection_init(srv_name, PIPE_SRVSVC, &con))
52         {
53                 return False;
54         }
55
56         prs_init(&data , 1024, 4, SAFETY_MARGIN, False);
57         prs_init(&rdata, 0   , 4, SAFETY_MARGIN, True );
58
59         /* create and send a MSRPC command with api SRV_NETTPRTENUM */
60
61         DEBUG(4,("SRV Net Server Transport Enum, level %d, enum:%8x\n",
62                                 switch_value, get_enum_hnd(hnd)));
63                                 
64         ctr->switch_value = switch_value;
65         ctr->ptr_tprt_ctr = 1;
66         ctr->tprt.info0.num_entries_read = 0;
67         ctr->tprt.info0.ptr_tprt_info    = 1;
68
69         /* store the parameters */
70         make_srv_q_net_tprt_enum(&q_o, srv_name,
71                                  switch_value, ctr,
72                                  preferred_len,
73                                  hnd);
74
75         /* turn parameters into data stream */
76         srv_io_q_net_tprt_enum("", &q_o, &data, 0);
77
78         /* send the data on \PIPE\ */
79         if (rpc_con_pipe_req(con, SRV_NETTRANSPORTENUM, &data, &rdata))
80         {
81                 SRV_R_NET_TPRT_ENUM r_o;
82                 BOOL p;
83
84                 r_o.ctr = ctr;
85
86                 srv_io_r_net_tprt_enum("", &r_o, &rdata, 0);
87                 p = rdata.offset != 0;
88                 
89                 if (p && r_o.status != 0)
90                 {
91                         /* report error code */
92                         DEBUG(0,("SRV_R_NET_SRV_GET_INFO: %s\n", get_nt_error_msg(r_o.status)));
93                         p = 0;
94                 }
95
96                 if (p && r_o.ctr->switch_value != switch_value)
97                 {
98                         /* different switch levels.  oops. */
99                         DEBUG(0,("SRV_R_NET_SRV_TPRT_ENUM: info class %d does not match request %d\n",
100                                 r_o.ctr->switch_value, switch_value));
101                         p = 0;
102                 }
103
104                 if (p)
105                 {
106                         /* ok, at last: we're happy. */
107                         valid_enum = True;
108                 }
109         }
110
111         prs_mem_free(&data   );
112         prs_mem_free(&rdata  );
113         
114         cli_connection_unlink(con);
115
116         return valid_enum;
117 }
118
119 /****************************************************************************
120 do a server net conn enum
121 ****************************************************************************/
122 BOOL do_srv_net_srv_conn_enum( char *srv_name, char *qual_name,
123                         uint32 switch_value, SRV_CONN_INFO_CTR *ctr,
124                         uint32 preferred_len,
125                         ENUM_HND *hnd)
126 {
127         prs_struct data; 
128         prs_struct rdata;
129         SRV_Q_NET_CONN_ENUM q_o;
130         BOOL valid_enum = False;
131         struct cli_connection *con = NULL;
132
133         if (ctr == NULL || preferred_len == 0) return False;
134
135         if (!cli_connection_init(srv_name, PIPE_SRVSVC, &con))
136         {
137                 return False;
138         }
139
140         prs_init(&data , 1024, 4, SAFETY_MARGIN, False);
141         prs_init(&rdata, 0   , 4, SAFETY_MARGIN, True );
142
143         /* create and send a MSRPC command with api SRV_NETCONNENUM */
144
145         DEBUG(4,("SRV Net Server Connection Enum %s), level %d, enum:%8x\n",
146                                 qual_name, switch_value, get_enum_hnd(hnd)));
147                                 
148         ctr->switch_value = switch_value;
149         ctr->ptr_conn_ctr = 1;
150         ctr->conn.info0.num_entries_read = 0;
151         ctr->conn.info0.ptr_conn_info    = 1;
152
153         /* store the parameters */
154         make_srv_q_net_conn_enum(&q_o, srv_name, qual_name,
155                                  switch_value, ctr,
156                                  preferred_len,
157                                  hnd);
158
159         /* turn parameters into data stream */
160         srv_io_q_net_conn_enum("", &q_o, &data, 0);
161
162         /* send the data on \PIPE\ */
163         if (rpc_con_pipe_req(con, SRV_NETCONNENUM, &data, &rdata))
164         {
165                 SRV_R_NET_CONN_ENUM r_o;
166                 BOOL p;
167
168                 r_o.ctr = ctr;
169
170                 srv_io_r_net_conn_enum("", &r_o, &rdata, 0);
171                 p = rdata.offset != 0;
172                 
173                 if (p && r_o.status != 0)
174                 {
175                         /* report error code */
176                         DEBUG(0,("SRV_R_NET_SRV_GET_INFO: %s\n", get_nt_error_msg(r_o.status)));
177                         p = 0;
178                 }
179
180                 if (p && r_o.ctr->switch_value != switch_value)
181                 {
182                         /* different switch levels.  oops. */
183                         DEBUG(0,("SRV_R_NET_SRV_CONN_ENUM: info class %d does not match request %d\n",
184                                 r_o.ctr->switch_value, switch_value));
185                         p = 0;
186                 }
187
188                 if (p)
189                 {
190                         /* ok, at last: we're happy. */
191                         valid_enum = True;
192                 }
193         }
194
195         prs_mem_free(&data   );
196         prs_mem_free(&rdata  );
197         
198         cli_connection_unlink(con);
199
200         return valid_enum;
201 }
202
203 /****************************************************************************
204 do a server net sess enum
205 ****************************************************************************/
206 BOOL do_srv_net_srv_sess_enum( char *srv_name, char *qual_name, char *user_name,
207                         uint32 switch_value, SRV_SESS_INFO_CTR *ctr,
208                         uint32 preferred_len,
209                         ENUM_HND *hnd)
210 {
211         prs_struct data; 
212         prs_struct rdata;
213         SRV_Q_NET_SESS_ENUM q_o;
214         BOOL valid_enum = False;
215         struct cli_connection *con = NULL;
216
217         if (ctr == NULL || preferred_len == 0) return False;
218
219         if (!cli_connection_init(srv_name, PIPE_SRVSVC, &con))
220         {
221                 return False;
222         }
223
224         prs_init(&data , 1024, 4, SAFETY_MARGIN, False);
225         prs_init(&rdata, 0   , 4, SAFETY_MARGIN, True );
226
227         /* create and send a MSRPC command with api SRV_NETSESSENUM */
228
229         DEBUG(4,("SRV Net Session Enum, level %d, enum:%8x\n",
230                                 switch_value, get_enum_hnd(hnd)));
231                                 
232         ctr->switch_value = switch_value;
233         ctr->ptr_sess_ctr = 1;
234         ctr->sess.info0.num_entries_read = 0;
235         ctr->sess.info0.ptr_sess_info    = 1;
236
237         /* store the parameters */
238         make_srv_q_net_sess_enum(&q_o, srv_name, qual_name, user_name,
239                                  switch_value, ctr,
240                                  preferred_len,
241                                  hnd);
242
243         /* turn parameters into data stream */
244         srv_io_q_net_sess_enum("", &q_o, &data, 0);
245
246         /* send the data on \PIPE\ */
247         if (rpc_con_pipe_req(con, SRV_NETSESSENUM, &data, &rdata))
248         {
249                 SRV_R_NET_SESS_ENUM r_o;
250                 BOOL p;
251
252                 r_o.ctr = ctr;
253
254                 srv_io_r_net_sess_enum("", &r_o, &rdata, 0);
255                 p = rdata.offset != 0;
256                 
257                 if (p && r_o.status != 0)
258                 {
259                         /* report error code */
260                         DEBUG(0,("SRV_R_NET_SRV_SESS_ENUM: %s\n", get_nt_error_msg(r_o.status)));
261                         p = 0;
262                 }
263
264                 if (p && r_o.ctr->switch_value != switch_value)
265                 {
266                         /* different switch levels.  oops. */
267                         DEBUG(0,("SRV_R_NET_SRV_SESS_ENUM: info class %d does not match request %d\n",
268                                 r_o.ctr->switch_value, switch_value));
269                         p = 0;
270                 }
271
272                 if (p)
273                 {
274                         /* ok, at last: we're happy. */
275                         valid_enum = True;
276                 }
277         }
278
279         prs_mem_free(&data   );
280         prs_mem_free(&rdata  );
281         
282         cli_connection_unlink(con);
283
284         return valid_enum;
285 }
286
287 /****************************************************************************
288 do a server net share enum
289 ****************************************************************************/
290 BOOL do_srv_net_srv_share_enum( char *srv_name, 
291                         uint32 switch_value, SRV_SHARE_INFO_CTR *ctr,
292                         uint32 preferred_len,
293                         ENUM_HND *hnd)
294 {
295         prs_struct data; 
296         prs_struct rdata;
297         SRV_Q_NET_SHARE_ENUM q_o;
298         BOOL valid_enum = False;
299         struct cli_connection *con = NULL;
300
301         if (ctr == NULL || preferred_len == 0) return False;
302
303         if (!cli_connection_init(srv_name, PIPE_SRVSVC, &con))
304         {
305                 return False;
306         }
307
308         prs_init(&data , 1024, 4, SAFETY_MARGIN, False);
309         prs_init(&rdata, 0   , 4, SAFETY_MARGIN, True );
310
311         /* create and send a MSRPC command with api SRV_NETSHAREENUM */
312
313         DEBUG(4,("SRV Get Share Info, level %d, enum:%8x\n",
314                                 switch_value, get_enum_hnd(hnd)));
315                                 
316         q_o.share_level = switch_value;
317
318         ctr->switch_value = switch_value;
319         ctr->ptr_share_ctr = 1;
320         ctr->share.info1.num_entries_read = 0;
321         ctr->share.info1.ptr_share_info    = 1;
322
323         /* store the parameters */
324         make_srv_q_net_share_enum(&q_o, srv_name, 
325                                  switch_value, ctr,
326                                  preferred_len,
327                                  hnd);
328
329         /* turn parameters into data stream */
330         srv_io_q_net_share_enum("", &q_o, &data, 0);
331
332         /* send the data on \PIPE\ */
333         if (rpc_con_pipe_req(con, SRV_NETSHAREENUM, &data, &rdata))
334         {
335                 SRV_R_NET_SHARE_ENUM r_o;
336                 BOOL p;
337
338                 r_o.ctr = ctr;
339
340                 srv_io_r_net_share_enum("", &r_o, &rdata, 0);
341                 p = rdata.offset != 0;
342                 
343                 if (p && r_o.status != 0)
344                 {
345                         /* report error code */
346                         DEBUG(0,("SRV_R_NET_SRV_GET_INFO: %s\n", get_nt_error_msg(r_o.status)));
347                         p = 0;
348                 }
349
350                 if (p && r_o.ctr->switch_value != switch_value)
351                 {
352                         /* different switch levels.  oops. */
353                         DEBUG(0,("SRV_R_NET_SRV_SHARE_ENUM: info class %d does not match request %d\n",
354                                 r_o.ctr->switch_value, switch_value));
355                         p = 0;
356                 }
357
358                 if (p)
359                 {
360                         /* ok, at last: we're happy. */
361                         valid_enum = True;
362                 }
363         }
364
365         prs_mem_free(&data   );
366         prs_mem_free(&rdata  );
367         
368         cli_connection_unlink(con);
369
370         return valid_enum;
371 }
372
373 /****************************************************************************
374 do a server net file enum
375 ****************************************************************************/
376 BOOL do_srv_net_srv_file_enum( char *srv_name, char *qual_name, uint32 file_id,
377                         uint32 switch_value, SRV_FILE_INFO_CTR *ctr,
378                         uint32 preferred_len,
379                         ENUM_HND *hnd)
380 {
381         prs_struct data; 
382         prs_struct rdata;
383         SRV_Q_NET_FILE_ENUM q_o;
384         BOOL valid_enum = False;
385         struct cli_connection *con = NULL;
386
387         if (ctr == NULL || preferred_len == 0) return False;
388
389         if (!cli_connection_init(srv_name, PIPE_SRVSVC, &con))
390         {
391                 return False;
392         }
393
394         prs_init(&data , 1024, 4, SAFETY_MARGIN, False);
395         prs_init(&rdata, 0   , 4, SAFETY_MARGIN, True );
396
397         /* create and send a MSRPC command with api SRV_NETFILEENUM */
398
399         DEBUG(4,("SRV Get File Info level %d, enum:%8x\n",
400                                 switch_value, get_enum_hnd(hnd)));
401                                 
402         q_o.file_level = switch_value;
403
404         ctr->switch_value = switch_value;
405         ctr->ptr_file_ctr = 1;
406         ctr->file.info3.num_entries_read = 0;
407         ctr->file.info3.ptr_file_info    = 1;
408
409         /* store the parameters */
410         make_srv_q_net_file_enum(&q_o, srv_name, qual_name, file_id,
411                                  switch_value, ctr,
412                                  preferred_len,
413                                  hnd);
414
415         /* turn parameters into data stream */
416         srv_io_q_net_file_enum("", &q_o, &data, 0);
417
418         /* send the data on \PIPE\ */
419         if (rpc_con_pipe_req(con, SRV_NETFILEENUM, &data, &rdata))
420         {
421                 SRV_R_NET_FILE_ENUM r_o;
422                 BOOL p;
423
424                 r_o.ctr = ctr;
425
426                 srv_io_r_net_file_enum("", &r_o, &rdata, 0);
427                 p = rdata.offset != 0;
428                 
429                 if (p && r_o.status != 0)
430                 {
431                         /* report error code */
432                         DEBUG(0,("SRV_R_NET_SRV_GET_INFO: %s\n", get_nt_error_msg(r_o.status)));
433                         p = 0;
434                 }
435
436                 if (p && r_o.ctr->switch_value != switch_value)
437                 {
438                         /* different switch levels.  oops. */
439                         DEBUG(0,("SRV_R_NET_SRV_FILE_ENUM: info class %d does not match request %d\n",
440                                 r_o.ctr->switch_value, switch_value));
441                         p = 0;
442                 }
443
444                 if (p)
445                 {
446                         /* ok, at last: we're happy. */
447                         valid_enum = True;
448                 }
449         }
450
451         prs_mem_free(&data   );
452         prs_mem_free(&rdata  );
453         
454         cli_connection_unlink(con);
455
456         return valid_enum;
457 }
458
459 /****************************************************************************
460 do a server get info 
461 ****************************************************************************/
462 BOOL do_srv_net_srv_get_info( char *srv_name, uint32 switch_value,
463                                 SRV_INFO_CTR *ctr)
464 {
465         prs_struct data; 
466         prs_struct rdata;
467         SRV_Q_NET_SRV_GET_INFO q_o;
468         BOOL valid_info = False;
469         struct cli_connection *con = NULL;
470
471         if (switch_value == 0 || ctr == NULL) return False;
472
473         if (!cli_connection_init(srv_name, PIPE_SRVSVC, &con))
474         {
475                 return False;
476         }
477
478         prs_init(&data , 1024, 4, SAFETY_MARGIN, False);
479         prs_init(&rdata, 0   , 4, SAFETY_MARGIN, True );
480
481         /* create and send a MSRPC command with api SRV_NET_SRV_GET_INFO */
482
483         DEBUG(4,("SRV Get Server Info level %d\n", switch_value));
484
485         /* store the parameters */
486         make_srv_q_net_srv_get_info(&q_o, srv_name, switch_value);
487
488         /* turn parameters into data stream */
489         srv_io_q_net_srv_get_info("", &q_o, &data, 0);
490
491         /* send the data on \PIPE\ */
492         if (rpc_con_pipe_req(con, SRV_NET_SRV_GET_INFO, &data, &rdata))
493         {
494                 SRV_R_NET_SRV_GET_INFO r_o;
495                 BOOL p;
496
497                 r_o.ctr = ctr;
498
499                 srv_io_r_net_srv_get_info("", &r_o, &rdata, 0);
500                 p = rdata.offset != 0;
501                 p = rdata.offset != 0;
502                 
503                 if (p && r_o.status != 0)
504                 {
505                         /* report error code */
506                         DEBUG(0,("SRV_R_NET_SRV_GET_INFO: %s\n", get_nt_error_msg(r_o.status)));
507                         p = 0;
508                 }
509
510                 if (p && r_o.ctr->switch_value != q_o.switch_value)
511                 {
512                         /* different switch levels.  oops. */
513                         DEBUG(0,("SRV_R_NET_SRV_GET_INFO: info class %d does not match request %d\n",
514                                 r_o.ctr->switch_value, q_o.switch_value));
515                         p = 0;
516                 }
517
518                 if (p)
519                 {
520                         /* ok, at last: we're happy. */
521                         valid_info = True;
522                 }
523         }
524
525         prs_mem_free(&data   );
526         prs_mem_free(&rdata  );
527         
528         cli_connection_unlink(con);
529
530         return valid_info;
531 }
532
533 /****************************************************************************
534 get server time
535 ****************************************************************************/
536 BOOL do_srv_net_remote_tod( char *srv_name, TIME_OF_DAY_INFO *tod)
537 {
538         prs_struct data; 
539         prs_struct rdata;
540         SRV_Q_NET_REMOTE_TOD q_t;
541         BOOL valid_info = False;
542         struct cli_connection *con = NULL;
543
544         if (tod == NULL) return False;
545
546         if (!cli_connection_init(srv_name, PIPE_SRVSVC, &con))
547         {
548                 return False;
549         }
550
551         prs_init(&data , 1024, 4, SAFETY_MARGIN, False);
552         prs_init(&rdata, 0   , 4, SAFETY_MARGIN, True );
553
554         /* create and send a MSRPC command with api SRV_NET_REMOTE_TOD */
555
556         DEBUG(4,("SRV Remote TOD (%s)\n", srv_name));
557
558         /* store the parameters */
559         make_srv_q_net_remote_tod(&q_t, srv_name);
560
561         /* turn parameters into data stream */
562         srv_io_q_net_remote_tod("", &q_t, &data, 0);
563
564         /* send the data on \PIPE\ */
565         if (rpc_con_pipe_req(con, SRV_NET_REMOTE_TOD, &data, &rdata))
566         {
567                 SRV_R_NET_REMOTE_TOD r_t;
568                 BOOL p;
569
570                 r_t.tod = tod;
571
572                 srv_io_r_net_remote_tod("", &r_t, &rdata, 0);
573                 p = rdata.offset != 0;
574                 p = rdata.offset != 0;
575                 
576                 if (p && r_t.status != 0)
577                 {
578                         /* report error code */
579                         DEBUG(0,("SRV_R_NET_REMOTE_TOD: %s\n", get_nt_error_msg(r_t.status)));
580                         p = False;
581                 }
582
583                 if (p)
584                 {
585                         valid_info = True;
586                 }
587         }
588
589         prs_mem_free(&data   );
590         prs_mem_free(&rdata  );
591         
592         cli_connection_unlink(con);
593
594         return valid_info;
595 }