Makefile.in configure configure.in include/config.h.in: Changes for DGUX and UNIXWARE.
[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                         int *num_names)
110 {
111         prs_struct rbuf;
112         prs_struct buf; 
113         LSA_Q_LOOKUP_SIDS q_l;
114         BOOL valid_response = False;
115
116         if (hnd == NULL || num_sids == 0 || sids == NULL) return False;
117
118         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
119         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
120
121         /* create and send a MSRPC command with api LSA_LOOKUP_SIDS */
122
123         DEBUG(4,("LSA Lookup SIDs\n"));
124
125         /* store the parameters */
126         make_q_lookup_sids(&q_l, hnd, num_sids, sids, 1);
127
128         /* turn parameters into data stream */
129         lsa_io_q_lookup_sids("", &q_l, &buf, 0);
130
131         /* send the data on \PIPE\ */
132         if (rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &buf, &rbuf))
133         {
134                 LSA_R_LOOKUP_SIDS r_l;
135                 DOM_R_REF ref;
136                 LSA_TRANS_NAME_ENUM t_names;
137                 BOOL p;
138
139                 r_l.dom_ref = &ref;
140                 r_l.names   = &t_names;
141
142                 lsa_io_r_lookup_sids("", &r_l, &rbuf, 0);
143                 p = rbuf.offset != 0;
144                 
145                 if (p && r_l.status != 0)
146                 {
147                         /* report error code */
148                         DEBUG(0,("LSA_LOOKUP_SIDS: %s\n", get_nt_error_msg(r_l.status)));
149                         p = False;
150                 }
151
152                 if (p)
153                 {
154                         if (t_names.ptr_trans_names != 0 && ref.undoc_buffer != 0)
155                         {
156                                 valid_response = True;
157                         }
158                 }
159
160                 if (num_names != NULL && valid_response)
161                 {
162                         (*num_names) = t_names.num_entries;
163                 }
164                 if (valid_response)
165                 {
166                         int i;
167                         for (i = 0; i < t_names.num_entries; i++)
168                         {
169                                 if (t_names.name[i].domain_idx >= ref.num_ref_doms_1)
170                                 {
171                                         DEBUG(0,("LSA_LOOKUP_SIDS: domain index out of bounds\n"));
172                                         valid_response = False;
173                                         break;
174                                 }
175                         }
176                 }
177
178                 if (names != NULL && valid_response && t_names.num_entries != 0)
179                 {
180                         (*names) = (char**)malloc((*num_names) * sizeof(char*));
181                 }
182
183                 if (names != NULL && (*names) != NULL)
184                 {
185                         int i;
186                         /* take each name, construct a \DOMAIN\name string */
187                         for (i = 0; i < (*num_names); i++)
188                         {
189                                 fstring name;
190                                 fstring dom_name;
191                                 fstring full_name;
192                                 uint32 dom_idx = t_names.name[i].domain_idx;
193                                 fstrcpy(dom_name, unistr2(ref.ref_dom[dom_idx].uni_dom_name.buffer));
194                                 fstrcpy(name    , unistr2(t_names.uni_name[i].buffer));
195                                 
196                                 slprintf(full_name, sizeof(full_name)-1, "\\%s\\%s",
197                                          dom_name, name);
198
199                                 (*names)[i] = strdup(full_name);
200                         }
201                 }
202         }
203
204         prs_mem_free(&rbuf);
205         prs_mem_free(&buf );
206
207         return valid_response;
208 }
209
210 /****************************************************************************
211 do a LSA Query Info Policy
212 ****************************************************************************/
213 BOOL do_lsa_query_info_pol(struct cli_state *cli,
214                         POLICY_HND *hnd, uint16 info_class,
215                         fstring domain_name, DOM_SID *domain_sid)
216 {
217         prs_struct rbuf;
218         prs_struct buf; 
219         LSA_Q_QUERY_INFO q_q;
220         BOOL valid_response = False;
221
222         ZERO_STRUCTP(domain_sid);
223         domain_name[0] = 0;
224
225         if (hnd == NULL || domain_name == NULL || domain_sid == NULL) return False;
226
227         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
228         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
229
230         /* create and send a MSRPC command with api LSA_QUERYINFOPOLICY */
231
232         DEBUG(4,("LSA Query Info Policy\n"));
233
234         /* store the parameters */
235         make_q_query(&q_q, hnd, info_class);
236
237         /* turn parameters into data stream */
238         lsa_io_q_query("", &q_q, &buf, 0);
239
240         /* send the data on \PIPE\ */
241         if (rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &buf, &rbuf))
242         {
243                 LSA_R_QUERY_INFO r_q;
244                 BOOL p;
245
246                 lsa_io_r_query("", &r_q, &rbuf, 0);
247                 p = rbuf.offset != 0;
248                 
249                 if (p && r_q.status != 0)
250                 {
251                         /* report error code */
252                         DEBUG(0,("LSA_QUERYINFOPOLICY: %s\n", get_nt_error_msg(r_q.status)));
253                         p = False;
254                 }
255
256                 if (p && r_q.info_class != q_q.info_class)
257                 {
258                         /* report different info classes */
259                         DEBUG(0,("LSA_QUERYINFOPOLICY: error info_class (q,r) differ - (%x,%x)\n",
260                                         q_q.info_class, r_q.info_class));
261                         p = False;
262                 }
263
264                 if (p)
265                 {
266                         fstring sid_str;
267                         /* ok, at last: we're happy. */
268                         switch (r_q.info_class)
269                         {
270                                 case 3:
271                                 {
272                                         if (r_q.dom.id3.buffer_dom_name != 0)
273                                         {
274                                                 char *dom_name = unistrn2(r_q.dom.id3.uni_domain_name.buffer,
275                                                                           r_q.dom.id3.uni_domain_name.uni_str_len);
276                                                 fstrcpy(domain_name, dom_name);
277                                         }
278                                         if (r_q.dom.id3.buffer_dom_sid != 0)
279                                         {
280                                                 *domain_sid = r_q.dom.id3.dom_sid.sid;
281                                         }
282
283                                         valid_response = True;
284                                         break;
285                                 }
286                                 case 5:
287                                 {
288                                         if (r_q.dom.id5.buffer_dom_name != 0)
289                                         {
290                                                 char *dom_name = unistrn2(r_q.dom.id5.uni_domain_name.buffer,
291                                                                           r_q.dom.id5.uni_domain_name.uni_str_len);
292                                                 fstrcpy(domain_name, dom_name);
293                                         }
294                                         if (r_q.dom.id5.buffer_dom_sid != 0)
295                                         {
296                                                 *domain_sid = r_q.dom.id5.dom_sid.sid;
297                                         }
298
299                                         valid_response = True;
300                                         break;
301                                 }
302                                 default:
303                                 {
304                                         DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
305                                         domain_name[0] = 0;
306
307                                         break;
308                                 }
309                         }
310                 
311                         sid_to_string(sid_str, domain_sid);
312                         DEBUG(3,("LSA_QUERYINFOPOLICY (level %x): domain:%s  domain sid:%s\n",
313                                   r_q.info_class, domain_name, sid_str));
314                 }
315         }
316
317         prs_mem_free(&rbuf);
318         prs_mem_free(&buf );
319
320         return valid_response;
321 }
322
323 /****************************************************************************
324 do a LSA Close
325 ****************************************************************************/
326 BOOL do_lsa_close(struct cli_state *cli, POLICY_HND *hnd)
327 {
328         prs_struct rbuf;
329         prs_struct buf; 
330         LSA_Q_CLOSE q_c;
331     BOOL valid_close = False;
332
333         if (hnd == NULL) return False;
334
335         /* create and send a MSRPC command with api LSA_OPENPOLICY */
336
337         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
338         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
339
340         DEBUG(4,("LSA Close\n"));
341
342         /* store the parameters */
343         make_lsa_q_close(&q_c, hnd);
344
345         /* turn parameters into data stream */
346         lsa_io_q_close("", &q_c, &buf, 0);
347
348         /* send the data on \PIPE\ */
349         if (rpc_api_pipe_req(cli, LSA_CLOSE, &buf, &rbuf))
350         {
351                 LSA_R_CLOSE r_c;
352                 BOOL p;
353
354                 lsa_io_r_close("", &r_c, &rbuf, 0);
355                 p = rbuf.offset != 0;
356
357                 if (p && r_c.status != 0)
358                 {
359                         /* report error code */
360                         DEBUG(0,("LSA_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
361                         p = False;
362                 }
363
364                 if (p)
365                 {
366                         /* check that the returned policy handle is all zeros */
367                         int i;
368                         valid_close = True;
369
370                         for (i = 0; i < sizeof(r_c.pol.data); i++)
371                         {
372                                 if (r_c.pol.data[i] != 0)
373                                 {
374                                         valid_close = False;
375                                         break;
376                                 }
377                         }       
378                         if (!valid_close)
379                         {
380                                 DEBUG(0,("LSA_CLOSE: non-zero handle returned\n"));
381                         }
382                 }
383         }
384
385         prs_mem_free(&rbuf);
386         prs_mem_free(&buf );
387
388         return valid_close;
389 }
390
391