const feeding frenzy
[ira/wip.git] / source3 / rpcclient / cmd_srvsvc.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NT Domain Authentication SMB / MSRPC client
5    Copyright (C) Andrew Tridgell 1994-1999
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1999
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23
24
25 #ifdef SYSLOG
26 #undef SYSLOG
27 #endif
28
29 #include "includes.h"
30 #include "nterr.h"
31
32 extern int DEBUGLEVEL;
33
34 #define DEBUG_TESTING
35
36 extern struct cli_state *smb_cli;
37
38 extern FILE* out_hnd;
39
40
41 /****************************************************************************
42 server get info query
43 ****************************************************************************/
44 BOOL net_srv_get_info(struct client_info *info,
45                 uint32 info_level,
46                 SRV_INFO_CTR *ctr)
47 {
48         uint16 fnum;
49         fstring dest_srv;
50
51         BOOL res = True;
52
53         fstrcpy(dest_srv, "\\\\");
54         fstrcat(dest_srv, info->dest_host);
55         strupper(dest_srv);
56
57         DEBUG(4,("net_srv_get_info: server:%s info level: %d\n",
58                                 dest_srv, (int)info_level));
59
60         /* open LSARPC session. */
61         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &fnum) : False;
62
63         /* send info level: receive requested info.  hopefully. */
64         res = res ? do_srv_net_srv_get_info(smb_cli, fnum,
65                                 dest_srv, info_level, ctr) : False;
66
67         /* close the session */
68         cli_nt_session_close(smb_cli, fnum);
69
70         return res;
71 }
72
73 /****************************************************************************
74 server get info query
75 ****************************************************************************/
76 void cmd_srv_query_info(struct client_info *info)
77 {
78         uint32 info_level = 101;
79         SRV_INFO_CTR ctr;
80         fstring tmp;
81
82         bzero(&ctr, sizeof(ctr));
83
84         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
85         {
86                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
87         }
88
89         DEBUG(5, ("cmd_srv_query_info: smb_cli->fd:%d\n", smb_cli->fd));
90
91         if (net_srv_get_info(info, info_level, &ctr))
92         {
93                 DEBUG(5,("cmd_srv_query_info: query succeeded\n"));
94
95                 display_srv_info_ctr(out_hnd, ACTION_HEADER   , &ctr);
96                 display_srv_info_ctr(out_hnd, ACTION_ENUMERATE, &ctr);
97                 display_srv_info_ctr(out_hnd, ACTION_FOOTER   , &ctr);
98         }
99         else
100         {
101                 DEBUG(5,("cmd_srv_query_info: query failed\n"));
102         }
103 }
104
105 /****************************************************************************
106 server enum transports
107 ****************************************************************************/
108 BOOL msrpc_srv_enum_tprt(struct cli_state *cli,
109                                 const char* dest_srv,
110                                 uint32 info_level,
111                                 SRV_TPRT_INFO_CTR *ctr,
112                                 TPRT_INFO_FN(tprt_fn))
113 {
114         uint16 fnum;
115
116         BOOL res = True;
117         BOOL res1 = True;
118
119         ENUM_HND hnd;
120
121         hnd.ptr_hnd = 1;
122         hnd.handle = 0;
123
124         /* open srvsvc session. */
125         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &fnum) : False;
126
127         /* enumerate transports on server */
128         res1 = res ? do_srv_net_srv_tprt_enum(smb_cli, fnum,
129                                 dest_srv, 
130                     info_level, ctr, 0xffffffff, &hnd) : False;
131
132         /* close the session */
133         cli_nt_session_close(smb_cli, fnum);
134
135         tprt_fn(ctr);
136
137         free_srv_tprt_ctr(ctr);
138
139         return res1;
140 }
141
142 static void srv_display_tprt_ctr(const SRV_TPRT_INFO_CTR *ctr)
143 {
144         display_srv_tprt_info_ctr(out_hnd, ACTION_HEADER   , ctr);
145         display_srv_tprt_info_ctr(out_hnd, ACTION_ENUMERATE, ctr);
146         display_srv_tprt_info_ctr(out_hnd, ACTION_FOOTER   , ctr);
147 }
148
149 /****************************************************************************
150 server enum transports
151 ****************************************************************************/
152 void cmd_srv_enum_tprt(struct client_info *info)
153 {
154         fstring dest_srv;
155         fstring tmp;
156         SRV_TPRT_INFO_CTR ctr;
157         uint32 info_level = 0;
158
159         bzero(&ctr, sizeof(ctr));
160
161         fstrcpy(dest_srv, "\\\\");
162         fstrcat(dest_srv, info->dest_host);
163         strupper(dest_srv);
164
165         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
166         {
167                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
168         }
169
170         DEBUG(4,("cmd_srv_enum_tprt: server:%s info level: %d\n",
171                                 dest_srv, (int)info_level));
172
173         DEBUG(5, ("cmd_srv_enum_tprt: smb_cli->fd:%d\n", smb_cli->fd));
174
175         /* enumerate transports on server */
176         msrpc_srv_enum_tprt(smb_cli, dest_srv, 
177                     info_level, &ctr, 
178                     srv_display_tprt_ctr);
179 }
180
181 /****************************************************************************
182 server enum connections
183 ****************************************************************************/
184 void cmd_srv_enum_conn(struct client_info *info)
185 {
186         uint16 fnum;
187         fstring dest_srv;
188         fstring qual_srv;
189         fstring tmp;
190         SRV_CONN_INFO_CTR ctr;
191         ENUM_HND hnd;
192         uint32 info_level = 0;
193
194         BOOL res = True;
195
196         bzero(&ctr, sizeof(ctr));
197
198         fstrcpy(qual_srv, "\\\\");
199         fstrcat(qual_srv, info->myhostname);
200         strupper(qual_srv);
201
202         fstrcpy(dest_srv, "\\\\");
203         fstrcat(dest_srv, info->dest_host);
204         strupper(dest_srv);
205
206         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
207         {
208                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
209         }
210
211         DEBUG(4,("cmd_srv_enum_conn: server:%s info level: %d\n",
212                                 dest_srv, (int)info_level));
213
214         DEBUG(5, ("cmd_srv_enum_conn: smb_cli->fd:%d\n", smb_cli->fd));
215
216         /* open srvsvc session. */
217         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &fnum) : False;
218
219         hnd.ptr_hnd = 1;
220         hnd.handle = 0;
221
222         /* enumerate connections on server */
223         res = res ? do_srv_net_srv_conn_enum(smb_cli, fnum,
224                                 dest_srv, qual_srv,
225                     info_level, &ctr, 0xffffffff, &hnd) : False;
226
227         if (res)
228         {
229                 display_srv_conn_info_ctr(out_hnd, ACTION_HEADER   , &ctr);
230                 display_srv_conn_info_ctr(out_hnd, ACTION_ENUMERATE, &ctr);
231                 display_srv_conn_info_ctr(out_hnd, ACTION_FOOTER   , &ctr);
232         }
233
234         /* close the session */
235         cli_nt_session_close(smb_cli, fnum);
236
237         if (res)
238         {
239                 DEBUG(5,("cmd_srv_enum_conn: query succeeded\n"));
240         }
241         else
242         {
243                 DEBUG(5,("cmd_srv_enum_conn: query failed\n"));
244         }
245 }
246
247 /****************************************************************************
248 server enum shares
249 ****************************************************************************/
250 void cmd_srv_enum_shares(struct client_info *info)
251 {
252         uint16 fnum;
253         fstring dest_srv;
254         fstring tmp;
255         SRV_SHARE_INFO_CTR ctr;
256         ENUM_HND hnd;
257         uint32 info_level = 1;
258
259         BOOL res = True;
260
261         bzero(&ctr, sizeof(ctr));
262
263         fstrcpy(dest_srv, "\\\\");
264         fstrcat(dest_srv, info->dest_host);
265         strupper(dest_srv);
266
267         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
268         {
269                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
270         }
271
272         DEBUG(4,("cmd_srv_enum_shares: server:%s info level: %d\n",
273                                 dest_srv, (int)info_level));
274
275         DEBUG(5, ("cmd_srv_enum_shares: smb_cli->fd:%d\n", smb_cli->fd));
276
277         /* open srvsvc session. */
278         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &fnum) : False;
279
280         hnd.ptr_hnd = 0;
281         hnd.handle = 0;
282
283         /* enumerate shares_files on server */
284         res = res ? do_srv_net_srv_share_enum(smb_cli, fnum,
285                                 dest_srv, 
286                     info_level, &ctr, 0xffffffff, &hnd) : False;
287
288         if (res)
289         {
290                 display_srv_share_info_ctr(out_hnd, ACTION_HEADER   , &ctr);
291                 display_srv_share_info_ctr(out_hnd, ACTION_ENUMERATE, &ctr);
292                 display_srv_share_info_ctr(out_hnd, ACTION_FOOTER   , &ctr);
293         }
294
295         /* close the session */
296         cli_nt_session_close(smb_cli, fnum);
297
298         if (res)
299         {
300                 DEBUG(5,("cmd_srv_enum_shares: query succeeded\n"));
301         }
302         else
303         {
304                 DEBUG(5,("cmd_srv_enum_shares: query failed\n"));
305         }
306 }
307
308 /****************************************************************************
309 server enum sessions
310 ****************************************************************************/
311 void cmd_srv_enum_sess(struct client_info *info)
312 {
313         uint16 fnum;
314         fstring dest_srv;
315         fstring tmp;
316         SRV_SESS_INFO_CTR ctr;
317         ENUM_HND hnd;
318         uint32 info_level = 0;
319
320         BOOL res = True;
321
322         bzero(&ctr, sizeof(ctr));
323
324         fstrcpy(dest_srv, "\\\\");
325         fstrcat(dest_srv, info->dest_host);
326         strupper(dest_srv);
327
328         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
329         {
330                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
331         }
332
333         DEBUG(4,("cmd_srv_enum_sess: server:%s info level: %d\n",
334                                 dest_srv, (int)info_level));
335
336         DEBUG(5, ("cmd_srv_enum_sess: smb_cli->fd:%d\n", smb_cli->fd));
337
338         /* open srvsvc session. */
339         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &fnum) : False;
340
341         hnd.ptr_hnd = 1;
342         hnd.handle = 0;
343
344         /* enumerate sessions on server */
345         res = res ? do_srv_net_srv_sess_enum(smb_cli, fnum,
346                                 dest_srv, NULL, NULL, info_level, &ctr, 0x1000, &hnd) : False;
347
348         if (res)
349         {
350                 display_srv_sess_info_ctr(out_hnd, ACTION_HEADER   , &ctr);
351                 display_srv_sess_info_ctr(out_hnd, ACTION_ENUMERATE, &ctr);
352                 display_srv_sess_info_ctr(out_hnd, ACTION_FOOTER   , &ctr);
353         }
354
355         /* close the session */
356         cli_nt_session_close(smb_cli, fnum);
357
358         if (res)
359         {
360                 DEBUG(5,("cmd_srv_enum_sess: query succeeded\n"));
361         }
362         else
363         {
364                 DEBUG(5,("cmd_srv_enum_sess: query failed\n"));
365         }
366 }
367
368 /****************************************************************************
369 server enum files
370 ****************************************************************************/
371 void cmd_srv_enum_files(struct client_info *info)
372 {
373         uint16 fnum;
374         fstring dest_srv;
375         fstring tmp;
376         SRV_FILE_INFO_CTR ctr;
377         ENUM_HND hnd;
378         uint32 info_level = 3;
379
380         BOOL res = True;
381
382         bzero(&ctr, sizeof(ctr));
383
384         fstrcpy(dest_srv, "\\\\");
385         fstrcat(dest_srv, info->dest_host);
386         strupper(dest_srv);
387
388         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
389         {
390                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
391         }
392
393         DEBUG(4,("cmd_srv_enum_files: server:%s info level: %d\n",
394                                 dest_srv, (int)info_level));
395
396         DEBUG(5, ("cmd_srv_enum_files: smb_cli->fd:%d\n", smb_cli->fd));
397
398         /* open srvsvc session. */
399         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &fnum) : False;
400
401         hnd.ptr_hnd = 1;
402         hnd.handle = 0;
403
404         /* enumerate files on server */
405         res = res ? do_srv_net_srv_file_enum(smb_cli, fnum,
406                                 dest_srv, NULL, 0, info_level, &ctr, 0x1000, &hnd) : False;
407
408         if (res)
409         {
410                 display_srv_file_info_ctr(out_hnd, ACTION_HEADER   , &ctr);
411                 display_srv_file_info_ctr(out_hnd, ACTION_ENUMERATE, &ctr);
412                 display_srv_file_info_ctr(out_hnd, ACTION_FOOTER   , &ctr);
413         }
414
415         /* close the session */
416         cli_nt_session_close(smb_cli, fnum);
417
418         if (res)
419         {
420                 DEBUG(5,("cmd_srv_enum_files: query succeeded\n"));
421         }
422         else
423         {
424                 DEBUG(5,("cmd_srv_enum_files: query failed\n"));
425         }
426 }
427
428 /****************************************************************************
429 display remote time
430 ****************************************************************************/
431 void cmd_time(struct client_info *info)
432 {
433         uint16 fnum;
434         fstring dest_srv;
435         TIME_OF_DAY_INFO tod;
436         BOOL res = True;
437
438         fstrcpy(dest_srv, "\\\\");
439         fstrcat(dest_srv, info->dest_host);
440         strupper(dest_srv);
441
442         DEBUG(4,("cmd_time: server:%s\n", dest_srv));
443
444         /* open srvsvc session. */
445         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &fnum) : False;
446
447         /* enumerate files on server */
448         res = res ? do_srv_net_remote_tod(smb_cli, fnum,
449                                           dest_srv, &tod) : False;
450
451         if (res)
452         {
453                 fprintf(out_hnd, "\tRemote Time:\t%s\n\n",
454                         http_timestring(tod.elapsedt));
455         }
456
457         /* Close the session */
458         cli_nt_session_close(smb_cli, fnum);
459
460         if (res)
461         {
462                 DEBUG(5,("cmd_srv_enum_files: query succeeded\n"));
463         }
464         else
465         {
466                 DEBUG(5,("cmd_srv_enum_files: query failed\n"));
467         }
468 }