error-code checking in lsa_lookup_sids. anonymous connections for
[samba.git] / source / 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-1997
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1997
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 nt_pipe_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, &nt_pipe_fnum) : False;
62
63         /* send info level: receive requested info.  hopefully. */
64         res = res ? do_srv_net_srv_get_info(smb_cli, nt_pipe_fnum,
65                                 dest_srv, info_level, ctr) : False;
66
67         /* close the session */
68         cli_nt_session_close(smb_cli, nt_pipe_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 connections
107 ****************************************************************************/
108 void cmd_srv_enum_conn(struct client_info *info)
109 {
110         uint16 nt_pipe_fnum;
111         fstring dest_srv;
112         fstring qual_srv;
113         fstring tmp;
114         SRV_CONN_INFO_CTR ctr;
115         ENUM_HND hnd;
116         uint32 info_level = 0;
117
118         BOOL res = True;
119
120         bzero(&ctr, sizeof(ctr));
121
122         fstrcpy(qual_srv, "\\\\");
123         fstrcat(qual_srv, info->myhostname);
124         strupper(qual_srv);
125
126         fstrcpy(dest_srv, "\\\\");
127         fstrcat(dest_srv, info->dest_host);
128         strupper(dest_srv);
129
130         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
131         {
132                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
133         }
134
135         DEBUG(4,("cmd_srv_enum_conn: server:%s info level: %d\n",
136                                 dest_srv, (int)info_level));
137
138         DEBUG(5, ("cmd_srv_enum_conn: smb_cli->fd:%d\n", smb_cli->fd));
139
140         /* open srvsvc session. */
141         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &nt_pipe_fnum) : False;
142
143         hnd.ptr_hnd = 1;
144         hnd.handle = 0;
145
146         /* enumerate connections on server */
147         res = res ? do_srv_net_srv_conn_enum(smb_cli, nt_pipe_fnum,
148                                 dest_srv, qual_srv,
149                     info_level, &ctr, 0xffffffff, &hnd) : False;
150
151         if (res)
152         {
153                 display_srv_conn_info_ctr(out_hnd, ACTION_HEADER   , &ctr);
154                 display_srv_conn_info_ctr(out_hnd, ACTION_ENUMERATE, &ctr);
155                 display_srv_conn_info_ctr(out_hnd, ACTION_FOOTER   , &ctr);
156         }
157
158         /* close the session */
159         cli_nt_session_close(smb_cli, nt_pipe_fnum);
160
161         if (res)
162         {
163                 DEBUG(5,("cmd_srv_enum_conn: query succeeded\n"));
164         }
165         else
166         {
167                 DEBUG(5,("cmd_srv_enum_conn: query failed\n"));
168         }
169 }
170
171 /****************************************************************************
172 server enum shares
173 ****************************************************************************/
174 void cmd_srv_enum_shares(struct client_info *info)
175 {
176         uint16 nt_pipe_fnum;
177         fstring dest_srv;
178         fstring tmp;
179         SRV_SHARE_INFO_CTR ctr;
180         ENUM_HND hnd;
181         uint32 info_level = 1;
182
183         BOOL res = True;
184
185         bzero(&ctr, sizeof(ctr));
186
187         fstrcpy(dest_srv, "\\\\");
188         fstrcat(dest_srv, info->dest_host);
189         strupper(dest_srv);
190
191         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
192         {
193                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
194         }
195
196         DEBUG(4,("cmd_srv_enum_shares: server:%s info level: %d\n",
197                                 dest_srv, (int)info_level));
198
199         DEBUG(5, ("cmd_srv_enum_shares: smb_cli->fd:%d\n", smb_cli->fd));
200
201         /* open srvsvc session. */
202         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &nt_pipe_fnum) : False;
203
204         hnd.ptr_hnd = 0;
205         hnd.handle = 0;
206
207         /* enumerate shares_files on server */
208         res = res ? do_srv_net_srv_share_enum(smb_cli, nt_pipe_fnum,
209                                 dest_srv, 
210                     info_level, &ctr, 0xffffffff, &hnd) : False;
211
212         if (res)
213         {
214                 display_srv_share_info_ctr(out_hnd, ACTION_HEADER   , &ctr);
215                 display_srv_share_info_ctr(out_hnd, ACTION_ENUMERATE, &ctr);
216                 display_srv_share_info_ctr(out_hnd, ACTION_FOOTER   , &ctr);
217         }
218
219         /* close the session */
220         cli_nt_session_close(smb_cli, nt_pipe_fnum);
221
222         if (res)
223         {
224                 DEBUG(5,("cmd_srv_enum_shares: query succeeded\n"));
225         }
226         else
227         {
228                 DEBUG(5,("cmd_srv_enum_shares: query failed\n"));
229         }
230 }
231
232 /****************************************************************************
233 server enum sessions
234 ****************************************************************************/
235 void cmd_srv_enum_sess(struct client_info *info)
236 {
237         uint16 nt_pipe_fnum;
238         fstring dest_srv;
239         fstring tmp;
240         SRV_SESS_INFO_CTR ctr;
241         ENUM_HND hnd;
242         uint32 info_level = 0;
243
244         BOOL res = True;
245
246         bzero(&ctr, sizeof(ctr));
247
248         fstrcpy(dest_srv, "\\\\");
249         fstrcat(dest_srv, info->dest_host);
250         strupper(dest_srv);
251
252         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
253         {
254                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
255         }
256
257         DEBUG(4,("cmd_srv_enum_sess: server:%s info level: %d\n",
258                                 dest_srv, (int)info_level));
259
260         DEBUG(5, ("cmd_srv_enum_sess: smb_cli->fd:%d\n", smb_cli->fd));
261
262         /* open srvsvc session. */
263         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &nt_pipe_fnum) : False;
264
265         hnd.ptr_hnd = 1;
266         hnd.handle = 0;
267
268         /* enumerate sessions on server */
269         res = res ? do_srv_net_srv_sess_enum(smb_cli, nt_pipe_fnum,
270                                 dest_srv, NULL, NULL, info_level, &ctr, 0x1000, &hnd) : False;
271
272         if (res)
273         {
274                 display_srv_sess_info_ctr(out_hnd, ACTION_HEADER   , &ctr);
275                 display_srv_sess_info_ctr(out_hnd, ACTION_ENUMERATE, &ctr);
276                 display_srv_sess_info_ctr(out_hnd, ACTION_FOOTER   , &ctr);
277         }
278
279         /* close the session */
280         cli_nt_session_close(smb_cli, nt_pipe_fnum);
281
282         if (res)
283         {
284                 DEBUG(5,("cmd_srv_enum_sess: query succeeded\n"));
285         }
286         else
287         {
288                 DEBUG(5,("cmd_srv_enum_sess: query failed\n"));
289         }
290 }
291
292 /****************************************************************************
293 server enum files
294 ****************************************************************************/
295 void cmd_srv_enum_files(struct client_info *info)
296 {
297         uint16 nt_pipe_fnum;
298         fstring dest_srv;
299         fstring tmp;
300         SRV_FILE_INFO_CTR ctr;
301         ENUM_HND hnd;
302         uint32 info_level = 3;
303
304         BOOL res = True;
305
306         bzero(&ctr, sizeof(ctr));
307
308         fstrcpy(dest_srv, "\\\\");
309         fstrcat(dest_srv, info->dest_host);
310         strupper(dest_srv);
311
312         if (next_token(NULL, tmp, NULL, sizeof(tmp)-1))
313         {
314                 info_level = (uint32)strtol(tmp, (char**)NULL, 10);
315         }
316
317         DEBUG(4,("cmd_srv_enum_files: server:%s info level: %d\n",
318                                 dest_srv, (int)info_level));
319
320         DEBUG(5, ("cmd_srv_enum_files: smb_cli->fd:%d\n", smb_cli->fd));
321
322         /* open srvsvc session. */
323         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &nt_pipe_fnum) : False;
324
325         hnd.ptr_hnd = 1;
326         hnd.handle = 0;
327
328         /* enumerate files on server */
329         res = res ? do_srv_net_srv_file_enum(smb_cli, nt_pipe_fnum,
330                                 dest_srv, NULL, 0, info_level, &ctr, 0x1000, &hnd) : False;
331
332         if (res)
333         {
334                 display_srv_file_info_ctr(out_hnd, ACTION_HEADER   , &ctr);
335                 display_srv_file_info_ctr(out_hnd, ACTION_ENUMERATE, &ctr);
336                 display_srv_file_info_ctr(out_hnd, ACTION_FOOTER   , &ctr);
337         }
338
339         /* close the session */
340         cli_nt_session_close(smb_cli, nt_pipe_fnum);
341
342         if (res)
343         {
344                 DEBUG(5,("cmd_srv_enum_files: query succeeded\n"));
345         }
346         else
347         {
348                 DEBUG(5,("cmd_srv_enum_files: query failed\n"));
349         }
350 }
351
352 /****************************************************************************
353 display remote time
354 ****************************************************************************/
355 void cmd_time(struct client_info *info)
356 {
357         uint16 nt_pipe_fnum;
358         fstring dest_srv;
359         TIME_OF_DAY_INFO tod;
360         BOOL res = True;
361
362         fstrcpy(dest_srv, "\\\\");
363         fstrcat(dest_srv, info->dest_host);
364         strupper(dest_srv);
365
366         DEBUG(4,("cmd_time: server:%s\n", dest_srv));
367
368         /* open srvsvc session. */
369         res = res ? cli_nt_session_open(smb_cli, PIPE_SRVSVC, &nt_pipe_fnum) : False;
370
371         /* enumerate files on server */
372         res = res ? do_srv_net_remote_tod(smb_cli, nt_pipe_fnum,
373                                           dest_srv, &tod) : False;
374
375         if (res)
376         {
377                 fprintf(out_hnd, "\tRemote Time:\t%s\n\n",
378                         http_timestring(tod.elapsedt));
379         }
380
381         /* Close the session */
382         cli_nt_session_close(smb_cli, nt_pipe_fnum);
383
384         if (res)
385         {
386                 DEBUG(5,("cmd_srv_enum_files: query succeeded\n"));
387         }
388         else
389         {
390                 DEBUG(5,("cmd_srv_enum_files: query failed\n"));
391         }
392 }