first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[gd/samba-autobuild/.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  *  Copyright (C) Jeremy Allison                    1999.
10  *  
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *  
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *  
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26
27 #ifdef SYSLOG
28 #undef SYSLOG
29 #endif
30
31 #include "includes.h"
32
33 extern int DEBUGLEVEL;
34
35
36 /****************************************************************************
37 do a LSA Open Policy
38 ****************************************************************************/
39
40 BOOL do_lsa_open_policy(struct cli_state *cli,
41                         char *server_name, POLICY_HND *hnd,
42                         BOOL sec_qos)
43 {
44         prs_struct rbuf;
45         prs_struct buf; 
46         LSA_Q_OPEN_POL q_o;
47         LSA_SEC_QOS qos;
48         LSA_R_OPEN_POL r_o;
49
50         if (hnd == NULL)
51                 return False;
52
53         prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
54         prs_init(&rbuf, 0, 4, UNMARSHALL );
55
56         /* create and send a MSRPC command with api LSA_OPENPOLICY */
57
58         DEBUG(4,("LSA Open Policy\n"));
59
60         /* store the parameters */
61         if (sec_qos) {
62                 init_lsa_sec_qos(&qos, 2, 1, 0, 0x20000000);
63                 init_q_open_pol(&q_o, 0x5c, 0, 0, &qos);
64         } else {
65                 init_q_open_pol(&q_o, 0x5c, 0, 0x1, NULL);
66         }
67
68         /* turn parameters into data stream */
69         if(!lsa_io_q_open_pol("", &q_o, &buf, 0)) {
70                 prs_mem_free(&buf);
71                 prs_mem_free(&rbuf);
72                 return False;
73         }
74
75         /* send the data on \PIPE\ */
76         if (!rpc_api_pipe_req(cli, LSA_OPENPOLICY, &buf, &rbuf)) {
77                 prs_mem_free(&buf);
78                 prs_mem_free(&rbuf);
79                 return False;
80         }
81
82         prs_mem_free(&buf);
83
84         if(!lsa_io_r_open_pol("", &r_o, &rbuf, 0)) {
85                 DEBUG(0,("do_lsa_open_policy: Failed to unmarshall LSA_R_OPEN_POL\n"));
86                 prs_mem_free(&rbuf);
87                 return False;
88         }
89
90         if (r_o.status != 0) {
91                 /* report error code */
92                 DEBUG(0,("LSA_OPENPOLICY: %s\n", get_nt_error_msg(r_o.status)));
93                 prs_mem_free(&rbuf);
94                 return False;
95         } else {
96                 /* ok, at last: we're happy. return the policy handle */
97                 memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
98         }
99
100         prs_mem_free(&rbuf);
101
102         return True;
103 }
104
105 /****************************************************************************
106 do a LSA Lookup SIDs
107 ****************************************************************************/
108
109 BOOL do_lsa_lookup_sids(struct cli_state *cli,
110                         POLICY_HND *hnd,
111                         int num_sids,
112                         DOM_SID **sids,
113                         char ***names,
114                         int *num_names)
115 {
116         prs_struct rbuf;
117         prs_struct buf; 
118         LSA_Q_LOOKUP_SIDS q_l;
119         LSA_R_LOOKUP_SIDS r_l;
120         DOM_R_REF ref;
121         LSA_TRANS_NAME_ENUM t_names;
122         int i;
123         BOOL valid_response = False;
124
125         if (hnd == NULL || num_sids == 0 || sids == NULL)
126                 return False;
127
128         prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
129         prs_init(&rbuf, 0, 4, UNMARSHALL );
130
131         /* create and send a MSRPC command with api LSA_LOOKUP_SIDS */
132
133         DEBUG(4,("LSA Lookup SIDs\n"));
134
135         /* store the parameters */
136         init_q_lookup_sids(&q_l, hnd, num_sids, sids, 1);
137
138         /* turn parameters into data stream */
139         if(!lsa_io_q_lookup_sids("", &q_l, &buf, 0)) {
140                 prs_mem_free(&buf);
141                 prs_mem_free(&rbuf);
142                 return False;
143         }
144
145         /* send the data on \PIPE\ */
146         if (!rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &buf, &rbuf)) {
147                 prs_mem_free(&buf);
148                 prs_mem_free(&rbuf);
149                 return False;
150         }
151
152         prs_mem_free(&buf);
153
154         r_l.dom_ref = &ref;
155         r_l.names   = &t_names;
156
157         if(!lsa_io_r_lookup_sids("", &r_l, &rbuf, 0)) {
158                 DEBUG(0,("do_lsa_lookup_sids: Failed to unmarshall LSA_R_LOOKUP_SIDS\n"));
159                 prs_mem_free(&rbuf);
160                 return False;
161         }
162
163                 
164         if (r_l.status != 0) {
165                 /* report error code */
166                 DEBUG(0,("LSA_LOOKUP_SIDS: %s\n", get_nt_error_msg(r_l.status)));
167         } else {
168                 if (t_names.ptr_trans_names != 0)
169                         valid_response = True;
170         }
171
172         if(!valid_response) {
173                 prs_mem_free(&rbuf);
174                 return False;
175         }
176
177         if (num_names != NULL)
178                 (*num_names) = t_names.num_entries;
179
180         for (i = 0; i < t_names.num_entries; i++) {
181                 if (t_names.name[i].domain_idx >= ref.num_ref_doms_1) {
182                         DEBUG(0,("LSA_LOOKUP_SIDS: domain index out of bounds\n"));
183                         prs_mem_free(&rbuf);
184                         return False;
185                 }
186         }
187
188         if (names != NULL && t_names.num_entries != 0)
189                 (*names) = (char**)malloc((*num_names) * sizeof(char*));
190
191         if (names != NULL && (*names) != NULL) {
192                 /* take each name, construct a \DOMAIN\name string */
193                 for (i = 0; i < (*num_names); i++) {
194                         fstring name;
195                         fstring dom_name;
196                         fstring full_name;
197                         uint32 dom_idx = t_names.name[i].domain_idx;
198                         fstrcpy(dom_name, dos_unistr2(ref.ref_dom[dom_idx].uni_dom_name.buffer));
199                         fstrcpy(name, dos_unistr2(t_names.uni_name[i].buffer));
200                         
201                         slprintf(full_name, sizeof(full_name)-1, "\\%s\\%s",
202                                  dom_name, name);
203
204                         (*names)[i] = strdup(full_name);
205                 }
206         }
207
208         prs_mem_free(&rbuf);
209
210         return valid_response;
211 }
212
213 /****************************************************************************
214 do a LSA Query Info Policy
215 ****************************************************************************/
216 BOOL do_lsa_query_info_pol(struct cli_state *cli,
217                         POLICY_HND *hnd, uint16 info_class,
218                         fstring domain_name, DOM_SID *domain_sid)
219 {
220         prs_struct rbuf;
221         prs_struct buf; 
222         LSA_Q_QUERY_INFO q_q;
223         LSA_R_QUERY_INFO r_q;
224         fstring sid_str;
225
226         ZERO_STRUCTP(domain_sid);
227         domain_name[0] = 0;
228
229         if (hnd == NULL || domain_name == NULL || domain_sid == NULL)
230                 return False;
231
232         prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
233         prs_init(&rbuf, 0, 4, UNMARSHALL );
234
235         /* create and send a MSRPC command with api LSA_QUERYINFOPOLICY */
236
237         DEBUG(4,("LSA Query Info Policy\n"));
238
239         /* store the parameters */
240         init_q_query(&q_q, hnd, info_class);
241
242         /* turn parameters into data stream */
243         if(!lsa_io_q_query("", &q_q, &buf, 0)) {
244                 prs_mem_free(&buf);
245                 prs_mem_free(&rbuf);
246                 return False;
247         }
248
249         /* send the data on \PIPE\ */
250         if (!rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &buf, &rbuf)) {
251                 prs_mem_free(&buf);
252                 prs_mem_free(&rbuf);
253                 return False;
254         }
255
256         prs_mem_free(&buf);
257
258         if(!lsa_io_r_query("", &r_q, &rbuf, 0)) {
259                 prs_mem_free(&rbuf);
260                 return False;
261         }
262
263         if (r_q.status != 0) {
264                 /* report error code */
265                 DEBUG(0,("LSA_QUERYINFOPOLICY: %s\n", get_nt_error_msg(r_q.status)));
266                 prs_mem_free(&rbuf);
267                 return False;
268         }
269
270         if (r_q.info_class != q_q.info_class) {
271                 /* report different info classes */
272                 DEBUG(0,("LSA_QUERYINFOPOLICY: error info_class (q,r) differ - (%x,%x)\n",
273                                 q_q.info_class, r_q.info_class));
274                 prs_mem_free(&rbuf);
275                 return False;
276         }
277
278         /* ok, at last: we're happy. */
279         switch (r_q.info_class) {
280         case 3:
281                 if (r_q.dom.id3.buffer_dom_name != 0) {
282                         char *dom_name = dos_unistrn2(r_q.dom.id3.uni_domain_name.buffer,
283                                                   r_q.dom.id3.uni_domain_name.uni_str_len);
284                         fstrcpy(domain_name, dom_name);
285                 }
286                 if (r_q.dom.id3.buffer_dom_sid != 0)
287                         *domain_sid = r_q.dom.id3.dom_sid.sid;
288                 break;
289         case 5:
290                 if (r_q.dom.id5.buffer_dom_name != 0) {
291                         char *dom_name = dos_unistrn2(r_q.dom.id5.uni_domain_name.buffer,
292                                                   r_q.dom.id5.uni_domain_name.uni_str_len);
293                         fstrcpy(domain_name, dom_name);
294                 }
295                 if (r_q.dom.id5.buffer_dom_sid != 0)
296                         *domain_sid = r_q.dom.id5.dom_sid.sid;
297                 break;
298         default:
299                 DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
300                 domain_name[0] = 0;
301
302                 prs_mem_free(&rbuf);
303                 return False;
304         }
305                 
306         sid_to_string(sid_str, domain_sid);
307         DEBUG(3,("LSA_QUERYINFOPOLICY (level %x): domain:%s  domain sid:%s\n",
308                   r_q.info_class, domain_name, sid_str));
309
310         prs_mem_free(&rbuf);
311
312         return True;
313 }
314
315 /****************************************************************************
316 do a LSA Close
317 ****************************************************************************/
318
319 BOOL do_lsa_close(struct cli_state *cli, POLICY_HND *hnd)
320 {
321         prs_struct rbuf;
322         prs_struct buf; 
323         LSA_Q_CLOSE q_c;
324         LSA_R_CLOSE r_c;
325         int i;
326
327         if (hnd == NULL)
328                 return False;
329
330         /* create and send a MSRPC command with api LSA_OPENPOLICY */
331
332         prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
333         prs_init(&rbuf, 0, 4, UNMARSHALL );
334
335         DEBUG(4,("LSA Close\n"));
336
337         /* store the parameters */
338         init_lsa_q_close(&q_c, hnd);
339
340         /* turn parameters into data stream */
341         if(!lsa_io_q_close("", &q_c, &buf, 0)) {
342                 prs_mem_free(&buf);
343                 prs_mem_free(&rbuf);
344                 return False;
345         }
346
347         /* send the data on \PIPE\ */
348         if (!rpc_api_pipe_req(cli, LSA_CLOSE, &buf, &rbuf)) {
349                 prs_mem_free(&buf);
350                 prs_mem_free(&rbuf);
351                 return False;
352         }
353
354         prs_mem_free(&buf);
355
356         if(!lsa_io_r_close("", &r_c, &rbuf, 0)) {
357                 prs_mem_free(&rbuf);
358                 return False;
359         }
360
361         if (r_c.status != 0) {
362                 /* report error code */
363                 DEBUG(0,("LSA_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
364                 prs_mem_free(&rbuf);
365                 return False;
366         }
367
368         /* check that the returned policy handle is all zeros */
369
370         for (i = 0; i < sizeof(r_c.pol.data); i++) {
371                 if (r_c.pol.data[i] != 0) {
372                         DEBUG(0,("LSA_CLOSE: non-zero handle returned\n"));
373                         prs_mem_free(&rbuf);
374                         return False;
375                 }
376         }
377
378         prs_mem_free(&rbuf);
379
380         return True;
381 }