d1c2711aed70245308718a2dab76d5722d987d1a
[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         fstring srv_name;
48
49         BOOL res = True;
50
51         fstrcpy(info->dom.level3_dom, "");
52         fstrcpy(info->dom.level3_sid, "");
53         fstrcpy(info->dom.level5_dom, "");
54         fstrcpy(info->dom.level5_sid, "");
55
56         fstrcpy(srv_name, "\\\\");
57         fstrcat(srv_name, info->myhostname);
58         strupper(srv_name);
59
60         DEBUG(4,("cmd_lsa_query_info: server:%s\n", srv_name));
61
62         DEBUG(5, ("cmd_lsa_query_info: smb_cli->fd:%d\n", smb_cli->fd));
63
64         /* open LSARPC session. */
65         res = res ? cli_nt_session_open(smb_cli, PIPE_LSARPC, False) : False;
66
67         /* lookup domain controller; receive a policy handle */
68         res = res ? do_lsa_open_policy(smb_cli,
69                                 srv_name,
70                                 &info->dom.lsa_info_pol, False) : False;
71
72         /* send client info query, level 3.  receive domain name and sid */
73         res = res ? do_lsa_query_info_pol(smb_cli, 
74                     &info->dom.lsa_info_pol, 0x03,
75                                 info->dom.level3_dom,
76                     info->dom.level3_sid) : False;
77
78         /* send client info query, level 5.  receive domain name and sid */
79         res = res ? do_lsa_query_info_pol(smb_cli,
80                     &info->dom.lsa_info_pol, 0x05,
81                                 info->dom.level5_dom,
82                     info->dom.level5_sid) : False;
83
84         res = res ? do_lsa_close(smb_cli, &info->dom.lsa_info_pol) : False;
85
86         /* close the session */
87         cli_nt_session_close(smb_cli);
88
89         if (res)
90         {
91                 BOOL domain_something = False;
92                 DEBUG(5,("cmd_lsa_query_info: query succeeded\n"));
93
94                 fprintf(out_hnd, "LSA Query Info Policy\n");
95
96                 if (info->dom.level3_sid[0] != 0)
97                 {
98                         fprintf(out_hnd, "Domain Member     - Domain: %s SID: %s\n",
99                                 info->dom.level3_dom, info->dom.level3_sid);
100                         domain_something = True;
101                 }
102                 if (info->dom.level5_sid[0] != 0)
103                 {
104                         fprintf(out_hnd, "Domain Controller - Domain: %s SID: %s\n",
105                                 info->dom.level5_dom, info->dom.level5_sid);
106                         domain_something = True;
107                 }
108                 if (!domain_something)
109                 {
110                         fprintf(out_hnd, "%s is not a Domain Member or Controller\n",
111                             info->dest_host);
112                 }
113         }
114         else
115         {
116                 DEBUG(5,("cmd_lsa_query_info: query failed\n"));
117         }
118 }
119
120 /****************************************************************************
121 nt lsa query
122 ****************************************************************************/
123 void cmd_lsa_lookup_sids(struct client_info *info)
124 {
125         fstring temp;
126         fstring sid_name;
127         fstring srv_name;
128         DOM_SID sid;
129         DOM_SID *sids[1];
130         char **names = NULL;
131
132         BOOL res = True;
133
134         DEBUG(5, ("cmd_lsa_lookup_sids: smb_cli->fd:%d\n", smb_cli->fd));
135
136         fstrcpy(srv_name, "\\\\");
137         fstrcat(srv_name, info->myhostname);
138         strupper(srv_name);
139
140         fstrcpy(sid_name, info->dom.level5_sid);
141
142         if (next_token(NULL, temp, NULL, sizeof(temp)))
143         {
144                 if (info->dom.level5_sid[0] == 0)
145                 {
146                         fprintf(out_hnd, "please use lsaquery first or specify a complete SID\n");
147                         return;
148                 }
149                         
150                 if (strnequal("S-", temp, 2))
151                 {
152                         fstrcpy(sid_name, temp);
153                 }
154                 else
155                 {
156                         fstrcat(sid_name, "-");
157                         fstrcat(sid_name, temp);
158                 }
159         }
160         else
161         {
162                 fprintf(out_hnd, "lsalookup RID or SID\n");
163                 return;
164         }
165
166         DEBUG(4,("cmd_lsa_lookup_sids: server: %s sid:%s\n",
167                         srv_name, sid_name));
168
169         make_dom_sid(&sid, sid_name);
170
171         sids[0] = &sid;
172
173         /* open LSARPC session. */
174         res = res ? cli_nt_session_open(smb_cli, PIPE_LSARPC, False) : False;
175
176         /* lookup domain controller; receive a policy handle */
177         res = res ? do_lsa_open_policy(smb_cli,
178                                 srv_name,
179                                 &info->dom.lsa_info_pol, True) : False;
180
181         /* send client info query, level 3.  receive domain name and sid */
182         res = res ? do_lsa_lookup_sids(smb_cli, 
183                     &info->dom.lsa_info_pol, 1, sids, names) : False;
184
185         res = res ? do_lsa_close(smb_cli, &info->dom.lsa_info_pol) : False;
186
187         /* close the session */
188         cli_nt_session_close(smb_cli);
189
190         if (res)
191         {
192                 DEBUG(5,("cmd_lsa_lookup_sids: query succeeded\n"));
193
194         }
195         else
196         {
197                 DEBUG(5,("cmd_lsa_lookup_sids: query failed\n"));
198         }
199 }
200