be148a7f1d86bce08b8698d63fcbedebd8e2541b
[samba.git] / source3 / rpcclient / cmd_lsarpc.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 extern int smb_tidx;
38
39 extern FILE* out_hnd;
40
41
42 /****************************************************************************
43 nt lsa query
44 ****************************************************************************/
45 void cmd_lsa_query_info(struct client_info *info)
46 {
47         uint16 nt_pipe_fnum;
48         fstring srv_name;
49
50         BOOL res = True;
51
52         fstrcpy(info->dom.level3_dom, "");
53         fstrcpy(info->dom.level5_dom, "");
54         ZERO_STRUCT(info->dom.level3_sid);
55         ZERO_STRUCT(info->dom.level5_sid);
56
57         fstrcpy(srv_name, "\\\\");
58         fstrcat(srv_name, info->myhostname);
59         strupper(srv_name);
60
61         DEBUG(4,("cmd_lsa_query_info: server:%s\n", srv_name));
62
63         DEBUG(5, ("cmd_lsa_query_info: smb_cli->fd:%d\n", smb_cli->fd));
64
65         /* open LSARPC session. */
66         res = res ? cli_nt_session_open(smb_cli, PIPE_LSARPC, &nt_pipe_fnum) : False;
67
68         /* lookup domain controller; receive a policy handle */
69         res = res ? lsa_open_policy(smb_cli, nt_pipe_fnum,
70                                 srv_name,
71                                 &info->dom.lsa_info_pol, False) : False;
72
73         /* send client info query, level 3.  receive domain name and sid */
74         res = res ? lsa_query_info_pol(smb_cli, nt_pipe_fnum, 
75                                           &info->dom.lsa_info_pol, 0x03,
76                                           info->dom.level3_dom,
77                                           &info->dom.level3_sid) : False;
78
79         /* send client info query, level 5.  receive domain name and sid */
80         res = res ? lsa_query_info_pol(smb_cli, nt_pipe_fnum,
81                                 &info->dom.lsa_info_pol, 0x05,
82                                 info->dom.level5_dom,
83                                 &info->dom.level5_sid) : False;
84
85         res = res ? lsa_close(smb_cli, nt_pipe_fnum, &info->dom.lsa_info_pol) : False;
86
87         /* close the session */
88         cli_nt_session_close(smb_cli, nt_pipe_fnum);
89
90         if (res)
91         {
92                 BOOL domain_something = False;
93                 fstring sid;
94                 DEBUG(5,("cmd_lsa_query_info: query succeeded\n"));
95
96                 fprintf(out_hnd, "LSA Query Info Policy\n");
97
98                 if (info->dom.level3_dom[0] != 0)
99                 {
100                         sid_to_string(sid, &info->dom.level3_sid);
101                         fprintf(out_hnd, "Domain Member     - Domain: %s SID: %s\n",
102                                 info->dom.level3_dom, sid);
103                         domain_something = True;
104                 }
105                 if (info->dom.level5_dom[0] != 0)
106                 {
107                         sid_to_string(sid, &info->dom.level5_sid);
108                         fprintf(out_hnd, "Domain Controller - Domain: %s SID: %s\n",
109                                 info->dom.level5_dom, sid);
110                         domain_something = True;
111                 }
112                 if (!domain_something)
113                 {
114                         fprintf(out_hnd, "%s is not a Domain Member or Controller\n",
115                             info->dest_host);
116                 }
117         }
118         else
119         {
120                 DEBUG(5,("cmd_lsa_query_info: query failed\n"));
121         }
122 }
123
124 /****************************************************************************
125 lookup names
126 ****************************************************************************/
127 void cmd_lsa_lookup_names(struct client_info *info)
128 {
129         uint16 nt_pipe_fnum;
130         fstring temp;
131         int i;
132         fstring srv_name;
133         int num_names = 0;
134         const char *names[10];
135         DOM_SID *sids = NULL;
136         int num_sids = 0;
137 #if 0
138         DOM_SID sid[10];
139         DOM_SID *sids[10];
140 #endif
141         BOOL res = True;
142
143         fstrcpy(srv_name, "\\\\");
144         fstrcat(srv_name, info->myhostname);
145         strupper(srv_name);
146
147         DEBUG(4,("cmd_lsa_lookup_names: server: %s\n", srv_name));
148
149         while (num_names < 10 && next_token(NULL, temp, NULL, sizeof(temp)))
150         {
151                 names[num_names] = strdup(temp);
152                 num_names++;
153         }
154
155         if (num_names == 0)
156         {
157                 fprintf(out_hnd, "lookupnames <name> [<name> ...]\n");
158                 return;
159         }
160
161         /* open LSARPC session. */
162         res = res ? cli_nt_session_open(smb_cli, PIPE_LSARPC, &nt_pipe_fnum) : False;
163
164         /* lookup domain controller; receive a policy handle */
165         res = res ? lsa_open_policy(smb_cli, nt_pipe_fnum,
166                                 srv_name,
167                                 &info->dom.lsa_info_pol, True) : False;
168
169         /* send lsa lookup sids call */
170         res = res ? lsa_lookup_names(smb_cli, nt_pipe_fnum, 
171                                        &info->dom.lsa_info_pol,
172                                        num_names, names,
173                                        &sids, NULL, &num_sids) : False;
174
175         res = res ? lsa_close(smb_cli, nt_pipe_fnum, &info->dom.lsa_info_pol) : False;
176
177         /* close the session */
178         cli_nt_session_close(smb_cli, nt_pipe_fnum);
179
180         if (res)
181         {
182                 DEBUG(5,("cmd_lsa_lookup_names: query succeeded\n"));
183         }
184         else
185         {
186                 DEBUG(5,("cmd_lsa_lookup_names: query failed\n"));
187         }
188
189         if (sids != NULL)
190         {
191                 fprintf(out_hnd,"Lookup Names:\n");
192                 for (i = 0; i < num_sids; i++)
193                 {
194                         sid_to_string(temp, &sids[i]);
195                         fprintf(out_hnd, "SID: %s -> %s\n", names[i], temp);
196 #if 0
197                         if (sids[i] != NULL)
198                         {
199                                 free(sids[i]);
200                         }
201 #endif
202                 }
203                 free(sids);
204         }
205
206         for (i = 0; i < num_names; i++)
207         {
208                 if (names[i] != NULL)
209                 {
210                         free(((char **)names)[i]);
211                 }
212         }
213 }
214
215 /****************************************************************************
216 lookup sids
217 ****************************************************************************/
218 void cmd_lsa_lookup_sids(struct client_info *info)
219 {
220         uint16 nt_pipe_fnum;
221         fstring temp;
222         int i;
223         pstring sid_name;
224         fstring srv_name;
225         DOM_SID sid[10];
226         DOM_SID *sids[10];
227         int num_sids = 0;
228         char **names = NULL;
229         int num_names = 0;
230
231         BOOL res = True;
232
233         fstrcpy(srv_name, "\\\\");
234         fstrcat(srv_name, info->myhostname);
235         strupper(srv_name);
236
237         DEBUG(4,("cmd_lsa_lookup_sids: server: %s\n", srv_name));
238
239         while (num_sids < 10 && next_token(NULL, temp, NULL, sizeof(temp)))
240         {
241                 if (strnequal("S-", temp, 2))
242                 {
243                         fstrcpy(sid_name, temp);
244                 }
245                 else
246                 {
247                         sid_to_string(sid_name, &info->dom.level5_sid);
248
249                         if (sid_name[0] == 0)
250                         {
251                                 fprintf(out_hnd, "please use lsaquery first or specify a complete SID\n");
252                                 return;
253                         }
254                                 
255                         fstrcat(sid_name, "-");
256                         fstrcat(sid_name, temp);
257                 }
258                 string_to_sid(&sid[num_sids], sid_name);
259                 sids[num_sids] = &sid[num_sids];
260                 num_sids++;
261         }
262
263         if (num_sids == 0)
264         {
265                 fprintf(out_hnd, "lookupsid RID or SID\n");
266                 return;
267         }
268
269         /* open LSARPC session. */
270         res = res ? cli_nt_session_open(smb_cli, PIPE_LSARPC, &nt_pipe_fnum) : False;
271
272         /* lookup domain controller; receive a policy handle */
273         res = res ? lsa_open_policy(smb_cli, nt_pipe_fnum,
274                                 srv_name,
275                                 &info->dom.lsa_info_pol, True) : False;
276
277         /* send lsa lookup sids call */
278         res = res ? lsa_lookup_sids(smb_cli, nt_pipe_fnum, 
279                                        &info->dom.lsa_info_pol,
280                                        num_sids, sids,
281                                        &names, NULL, &num_names) : False;
282
283         res = res ? lsa_close(smb_cli, nt_pipe_fnum, &info->dom.lsa_info_pol) : False;
284
285         /* close the session */
286         cli_nt_session_close(smb_cli, nt_pipe_fnum);
287
288         if (res)
289         {
290                 DEBUG(5,("cmd_lsa_lookup_sids: query succeeded\n"));
291         }
292         else
293         {
294                 DEBUG(5,("cmd_lsa_lookup_sids: query failed\n"));
295         }
296         if (names != NULL)
297         {
298                 fprintf(out_hnd,"Lookup SIDS:\n");
299                 for (i = 0; i < num_names; i++)
300                 {
301                         sid_to_string(temp, sids[i]);
302                         fprintf(out_hnd, "SID: %s -> %s\n", temp, names[i]);
303                         if (names[i] != NULL)
304                         {
305                                 free(names[i]);
306                         }
307                 }
308                 free(names);
309         }
310 }
311