c28092941794cd7f381bc9f8fc2f3218b9463d7e
[samba.git] / source3 / rpc_client / cli_lsarpc.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 /****************************************************************************
36 do a LSA Open Policy
37 ****************************************************************************/
38 BOOL do_lsa_open_policy(struct cli_state *cli,
39                         char *server_name, POLICY_HND *hnd,
40                         BOOL sec_qos)
41 {
42         prs_struct rbuf;
43         prs_struct buf; 
44         LSA_Q_OPEN_POL q_o;
45         LSA_SEC_QOS qos;
46         BOOL valid_pol = False;
47
48         if (hnd == NULL) return False;
49
50         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
51         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
52
53         /* create and send a MSRPC command with api LSA_OPENPOLICY */
54
55         DEBUG(4,("LSA Open Policy\n"));
56
57         /* store the parameters */
58         if (sec_qos)
59         {
60                 make_lsa_sec_qos(&qos, 2, 1, 0, 0x20000000);
61                 make_q_open_pol(&q_o, 0x5c, 0, 0, &qos);
62         }
63         else
64         {
65                 make_q_open_pol(&q_o, 0x5c, 0, 0x1, NULL);
66         }
67
68         /* turn parameters into data stream */
69         lsa_io_q_open_pol("", &q_o, &buf, 0);
70
71         /* send the data on \PIPE\ */
72         if (rpc_api_pipe_req(cli, LSA_OPENPOLICY, &buf, &rbuf))
73         {
74                 LSA_R_OPEN_POL r_o;
75                 BOOL p;
76
77                 lsa_io_r_open_pol("", &r_o, &rbuf, 0);
78                 p = rbuf.offset != 0;
79
80                 if (p && r_o.status != 0)
81                 {
82                         /* report error code */
83                         DEBUG(0,("LSA_OPENPOLICY: %s\n", get_nt_error_msg(r_o.status)));
84                         p = False;
85                 }
86
87                 if (p)
88                 {
89                         /* ok, at last: we're happy. return the policy handle */
90                         memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
91                         valid_pol = True;
92                 }
93         }
94
95         prs_mem_free(&rbuf);
96         prs_mem_free(&buf );
97
98         return valid_pol;
99 }
100
101 /****************************************************************************
102 do a LSA Lookup SIDs
103 ****************************************************************************/
104 BOOL do_lsa_lookup_sids(struct cli_state *cli,
105                         POLICY_HND *hnd,
106                         int num_sids,
107                         DOM_SID **sids,
108                         char **names)
109 {
110         prs_struct rbuf;
111         prs_struct buf; 
112         LSA_Q_LOOKUP_SIDS q_l;
113         BOOL valid_response = False;
114
115         if (hnd == NULL || num_sids == 0 || sids == NULL) return False;
116
117         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
118         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
119
120         /* create and send a MSRPC command with api LSA_LOOKUP_SIDS */
121
122         DEBUG(4,("LSA Lookup SIDs\n"));
123
124         /* store the parameters */
125         make_q_lookup_sids(&q_l, hnd, num_sids, sids, 1);
126
127         /* turn parameters into data stream */
128         lsa_io_q_lookup_sids("", &q_l, &buf, 0);
129
130         /* send the data on \PIPE\ */
131         if (rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &buf, &rbuf))
132         {
133                 LSA_R_LOOKUP_SIDS r_l;
134                 DOM_R_REF ref;
135                 LSA_TRANS_NAME_ENUM t_names;
136                 BOOL p;
137
138                 r_l.dom_ref = &ref;
139                 r_l.names   = &t_names;
140
141                 lsa_io_r_lookup_sids("", &r_l, &rbuf, 0);
142                 p = rbuf.offset != 0;
143                 
144                 if (p && r_l.status != 0)
145                 {
146                         /* report error code */
147                         DEBUG(0,("LSA_LOOKUP_SIDS: %s\n", get_nt_error_msg(r_l.status)));
148                         p = False;
149                 }
150
151                 if (p)
152                 {
153                         valid_response = True;
154                         *names = NULL;
155                 }
156         }
157
158         prs_mem_free(&rbuf);
159         prs_mem_free(&buf );
160
161         return valid_response;
162 }
163
164 /****************************************************************************
165 do a LSA Query Info Policy
166 ****************************************************************************/
167 BOOL do_lsa_query_info_pol(struct cli_state *cli,
168                         POLICY_HND *hnd, uint16 info_class,
169                         fstring domain_name, fstring domain_sid)
170 {
171         prs_struct rbuf;
172         prs_struct buf; 
173         LSA_Q_QUERY_INFO q_q;
174         BOOL valid_response = False;
175
176         if (hnd == NULL || domain_name == NULL || domain_sid == NULL) return False;
177
178         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
179         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
180
181         /* create and send a MSRPC command with api LSA_QUERYINFOPOLICY */
182
183         DEBUG(4,("LSA Query Info Policy\n"));
184
185         /* store the parameters */
186         make_q_query(&q_q, hnd, info_class);
187
188         /* turn parameters into data stream */
189         lsa_io_q_query("", &q_q, &buf, 0);
190
191         /* send the data on \PIPE\ */
192         if (rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &buf, &rbuf))
193         {
194                 LSA_R_QUERY_INFO r_q;
195                 BOOL p;
196
197                 lsa_io_r_query("", &r_q, &rbuf, 0);
198                 p = rbuf.offset != 0;
199                 
200                 if (p && r_q.status != 0)
201                 {
202                         /* report error code */
203                         DEBUG(0,("LSA_QUERYINFOPOLICY: %s\n", get_nt_error_msg(r_q.status)));
204                         p = False;
205                 }
206
207                 if (p && r_q.info_class != q_q.info_class)
208                 {
209                         /* report different info classes */
210                         DEBUG(0,("LSA_QUERYINFOPOLICY: error info_class (q,r) differ - (%x,%x)\n",
211                                         q_q.info_class, r_q.info_class));
212                         p = False;
213                 }
214
215                 if (p)
216                 {
217                         /* ok, at last: we're happy. */
218                         switch (r_q.info_class)
219                         {
220                                 case 3:
221                                 {
222                                         char *dom_name = unistrn2(r_q.dom.id3.uni_domain_name.buffer,
223                                                                   r_q.dom.id3.uni_domain_name.uni_str_len);
224                                         fstrcpy(domain_name, dom_name);
225                                         sid_to_string(domain_sid, &(r_q.dom.id3.dom_sid.sid));
226
227                                         valid_response = True;
228                                         break;
229                                 }
230                                 case 5:
231                                 {
232                                         char *dom_name = unistrn2(r_q.dom.id5.uni_domain_name.buffer,
233                                                                   r_q.dom.id5.uni_domain_name.uni_str_len);
234                                         fstrcpy(domain_name, dom_name);
235                                         sid_to_string(domain_sid, &(r_q.dom.id5.dom_sid.sid));
236
237                                         valid_response = True;
238                                         break;
239                                 }
240                                 default:
241                                 {
242                                         DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
243                                         domain_name[0] = 0;
244                                         domain_sid [0] = 0;
245
246                                         break;
247                                 }
248                         }
249                         DEBUG(3,("LSA_QUERYINFOPOLICY (level %x): domain:%s  domain sid:%s\n",
250                                   r_q.info_class, domain_name, domain_sid));
251                 }
252         }
253
254         prs_mem_free(&rbuf);
255         prs_mem_free(&buf );
256
257         return valid_response;
258 }
259
260 /****************************************************************************
261 do a LSA Close
262 ****************************************************************************/
263 BOOL do_lsa_close(struct cli_state *cli, POLICY_HND *hnd)
264 {
265         prs_struct rbuf;
266         prs_struct buf; 
267         LSA_Q_CLOSE q_c;
268     BOOL valid_close = False;
269
270         if (hnd == NULL) return False;
271
272         /* create and send a MSRPC command with api LSA_OPENPOLICY */
273
274         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
275         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
276
277         DEBUG(4,("LSA Close\n"));
278
279         /* store the parameters */
280         make_lsa_q_close(&q_c, hnd);
281
282         /* turn parameters into data stream */
283         lsa_io_q_close("", &q_c, &buf, 0);
284
285         /* send the data on \PIPE\ */
286         if (rpc_api_pipe_req(cli, LSA_CLOSE, &buf, &rbuf))
287         {
288                 LSA_R_CLOSE r_c;
289                 BOOL p;
290
291                 lsa_io_r_close("", &r_c, &rbuf, 0);
292                 p = rbuf.offset != 0;
293
294                 if (p && r_c.status != 0)
295                 {
296                         /* report error code */
297                         DEBUG(0,("LSA_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
298                         p = False;
299                 }
300
301                 if (p)
302                 {
303                         /* check that the returned policy handle is all zeros */
304                         int i;
305                         valid_close = True;
306
307                         for (i = 0; i < sizeof(r_c.pol.data); i++)
308                         {
309                                 if (r_c.pol.data[i] != 0)
310                                 {
311                                         valid_close = False;
312                                         break;
313                                 }
314                         }       
315                         if (!valid_close)
316                         {
317                                 DEBUG(0,("LSA_CLOSE: non-zero handle returned\n"));
318                         }
319                 }
320         }
321
322         prs_mem_free(&rbuf);
323         prs_mem_free(&buf );
324
325         return valid_close;
326 }
327
328