0e28174b8b32b7c3d7fc715b43c5e9ce8d817e59
[samba.git] / source / 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 lsa_open_policy(struct cli_state *cli, uint16 fnum,
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, fnum, 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 Names
103 ****************************************************************************/
104 BOOL lsa_lookup_names(struct cli_state *cli, uint16 fnum,
105                         POLICY_HND *hnd,
106                         int num_names,
107                         const char **names,
108                         DOM_SID **sids,
109                         int *num_sids)
110 {
111         prs_struct rbuf;
112         prs_struct buf; 
113         LSA_Q_LOOKUP_NAMES 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_NAMES */
122
123         DEBUG(4,("LSA Lookup NAMEs\n"));
124
125         /* store the parameters */
126         make_q_lookup_names(&q_l, hnd, num_names, names);
127
128         /* turn parameters into data stream */
129         lsa_io_q_lookup_names("", &q_l, &buf, 0);
130
131         /* send the data on \PIPE\ */
132         if (rpc_api_pipe_req(cli, fnum, LSA_LOOKUPNAMES, &buf, &rbuf))
133         {
134                 LSA_R_LOOKUP_NAMES r_l;
135                 DOM_R_REF ref;
136                 DOM_RID2 t_rids[MAX_LOOKUP_SIDS];
137                 BOOL p;
138
139                 ZERO_STRUCT(ref);
140                 ZERO_STRUCT(t_rids);
141
142                 r_l.dom_ref = &ref;
143                 r_l.dom_rid = t_rids;
144
145                 lsa_io_r_lookup_names("", &r_l, &rbuf, 0);
146                 p = rbuf.offset != 0;
147                 
148                 if (p && r_l.status != 0)
149                 {
150                         /* report error code */
151                         DEBUG(0,("LSA_LOOKUP_NAMES: %s\n", get_nt_error_msg(r_l.status)));
152                         p = False;
153                 }
154
155                 if (p)
156                 {
157                         if (r_l.undoc_buffer != 0 && ref.undoc_buffer != 0)
158                         {
159                                 valid_response = True;
160                         }
161                 }
162
163                 if (num_sids != NULL && valid_response)
164                 {
165                         (*num_sids) = r_l.num_entries;
166                 }
167                 if (valid_response)
168                 {
169                         int i;
170                         for (i = 0; i < r_l.num_entries; i++)
171                         {
172                                 if (t_rids[i].rid_idx >= ref.num_ref_doms_1 &&
173                                     t_rids[i].rid_idx != 0xffffffff)
174                                 {
175                                         DEBUG(0,("LSA_LOOKUP_NAMES: domain index %d out of bounds\n",
176                                                   t_rids[i].rid_idx));
177                                         valid_response = False;
178                                         break;
179                                 }
180                         }
181                 }
182
183                 if (sids != NULL && valid_response && r_l.num_entries != 0)
184                 {
185                         (*sids) = (DOM_SID*)malloc((*num_sids) * sizeof(DOM_SID));
186                 }
187
188                 if (sids != NULL && (*sids) != NULL)
189                 {
190                         int i;
191                         /* take each name, construct a SID */
192                         for (i = 0; i < (*num_sids); i++)
193                         {
194                                 uint32 dom_idx = t_rids[i].rid_idx;
195                                 uint32 dom_rid = t_rids[i].rid;
196                                 DOM_SID *sid = &(*sids)[i];
197                                 if (dom_idx != 0xffffffff)
198                                 {
199                                         sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
200                                         if (dom_rid != 0xffffffff)
201                                         {
202                                                 sid_append_rid(sid, dom_rid);
203                                         }
204                                 }
205                                 else
206                                 {
207                                         ZERO_STRUCTP(sid);
208                                 }
209                         }
210                 }
211         }
212
213         prs_mem_free(&rbuf);
214         prs_mem_free(&buf );
215
216         return valid_response;
217 }
218
219 /****************************************************************************
220 do a LSA Lookup SIDs
221 ****************************************************************************/
222 BOOL lsa_lookup_sids(struct cli_state *cli, uint16 fnum,
223                         POLICY_HND *hnd,
224                         int num_sids,
225                         DOM_SID **sids,
226                         char ***names,
227                         int *num_names)
228 {
229         prs_struct rbuf;
230         prs_struct buf; 
231         LSA_Q_LOOKUP_SIDS q_l;
232         BOOL valid_response = False;
233
234         if (hnd == NULL || num_sids == 0 || sids == NULL) return False;
235
236         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
237         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
238
239         /* create and send a MSRPC command with api LSA_LOOKUP_SIDS */
240
241         DEBUG(4,("LSA Lookup SIDs\n"));
242
243         /* store the parameters */
244         make_q_lookup_sids(&q_l, hnd, num_sids, sids, 1);
245
246         /* turn parameters into data stream */
247         lsa_io_q_lookup_sids("", &q_l, &buf, 0);
248
249         /* send the data on \PIPE\ */
250         if (rpc_api_pipe_req(cli, fnum, LSA_LOOKUPSIDS, &buf, &rbuf))
251         {
252                 LSA_R_LOOKUP_SIDS r_l;
253                 DOM_R_REF ref;
254                 LSA_TRANS_NAME_ENUM t_names;
255                 BOOL p;
256
257                 r_l.dom_ref = &ref;
258                 r_l.names   = &t_names;
259
260                 lsa_io_r_lookup_sids("", &r_l, &rbuf, 0);
261                 p = rbuf.offset != 0;
262                 
263                 if (p && r_l.status != 0)
264                 {
265                         /* report error code */
266                         DEBUG(0,("LSA_LOOKUP_SIDS: %s\n", get_nt_error_msg(r_l.status)));
267                         p = False;
268                 }
269
270                 if (p)
271                 {
272                         if (t_names.ptr_trans_names != 0 && ref.undoc_buffer != 0)
273                         {
274                                 valid_response = True;
275                         }
276                 }
277
278                 if (num_names != NULL && valid_response)
279                 {
280                         (*num_names) = t_names.num_entries;
281                 }
282                 if (valid_response)
283                 {
284                         int i;
285                         for (i = 0; i < t_names.num_entries; i++)
286                         {
287                                 if (t_names.name[i].domain_idx >= ref.num_ref_doms_1)
288                                 {
289                                         DEBUG(0,("LSA_LOOKUP_SIDS: domain index out of bounds\n"));
290                                         valid_response = False;
291                                         break;
292                                 }
293                         }
294                 }
295
296                 if (names != NULL && valid_response && t_names.num_entries != 0)
297                 {
298                         (*names) = (char**)malloc((*num_names) * sizeof(char*));
299                 }
300
301                 if (names != NULL && (*names) != NULL)
302                 {
303                         int i;
304                         /* take each name, construct a \DOMAIN\name string */
305                         for (i = 0; i < (*num_names); i++)
306                         {
307                                 fstring name;
308                                 fstring dom_name;
309                                 fstring full_name;
310                                 uint32 dom_idx = t_names.name[i].domain_idx;
311
312                                 if (dom_idx != 0xffffffff)
313                                 {
314                                         fstrcpy(dom_name, unistr2_to_str(&ref.ref_dom[dom_idx].uni_dom_name));
315                                         fstrcpy(name    , unistr2_to_str(&t_names.uni_name[i]));
316                                         
317                                         memset(full_name, 0, sizeof(full_name));
318
319                                         slprintf(full_name, sizeof(full_name)-1, "%s\\%s",
320                                                  dom_name, name);
321
322                                         (*names)[i] = strdup(full_name);
323                                 }
324                                 else
325                                 {
326                                         (*names)[i] = NULL;
327                                 }
328                         }
329                 }
330         }
331
332         prs_mem_free(&rbuf);
333         prs_mem_free(&buf );
334
335         return valid_response;
336 }
337
338 /****************************************************************************
339 do a LSA Query Info Policy
340 ****************************************************************************/
341 BOOL lsa_query_info_pol(struct cli_state *cli, uint16 fnum,
342                         POLICY_HND *hnd, uint16 info_class,
343                         fstring domain_name, DOM_SID *domain_sid)
344 {
345         prs_struct rbuf;
346         prs_struct buf; 
347         LSA_Q_QUERY_INFO q_q;
348         BOOL valid_response = False;
349
350         ZERO_STRUCTP(domain_sid);
351         domain_name[0] = 0;
352
353         if (hnd == NULL || domain_name == NULL || domain_sid == NULL) return False;
354
355         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
356         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
357
358         /* create and send a MSRPC command with api LSA_QUERYINFOPOLICY */
359
360         DEBUG(4,("LSA Query Info Policy\n"));
361
362         /* store the parameters */
363         make_q_query(&q_q, hnd, info_class);
364
365         /* turn parameters into data stream */
366         lsa_io_q_query("", &q_q, &buf, 0);
367
368         /* send the data on \PIPE\ */
369         if (rpc_api_pipe_req(cli, fnum, LSA_QUERYINFOPOLICY, &buf, &rbuf))
370         {
371                 LSA_R_QUERY_INFO r_q;
372                 BOOL p;
373
374                 lsa_io_r_query("", &r_q, &rbuf, 0);
375                 p = rbuf.offset != 0;
376                 
377                 if (p && r_q.status != 0)
378                 {
379                         /* report error code */
380                         DEBUG(0,("LSA_QUERYINFOPOLICY: %s\n", get_nt_error_msg(r_q.status)));
381                         p = False;
382                 }
383
384                 if (p && r_q.info_class != q_q.info_class)
385                 {
386                         /* report different info classes */
387                         DEBUG(0,("LSA_QUERYINFOPOLICY: error info_class (q,r) differ - (%x,%x)\n",
388                                         q_q.info_class, r_q.info_class));
389                         p = False;
390                 }
391
392                 if (p)
393                 {
394                         fstring sid_str;
395                         /* ok, at last: we're happy. */
396                         switch (r_q.info_class)
397                         {
398                                 case 3:
399                                 {
400                                         if (r_q.dom.id3.buffer_dom_name != 0)
401                                         {
402                                                 char *dom_name = unistrn2(r_q.dom.id3.uni_domain_name.buffer,
403                                                                           r_q.dom.id3.uni_domain_name.uni_str_len);
404                                                 fstrcpy(domain_name, dom_name);
405                                         }
406                                         if (r_q.dom.id3.buffer_dom_sid != 0)
407                                         {
408                                                 *domain_sid = r_q.dom.id3.dom_sid.sid;
409                                         }
410
411                                         valid_response = True;
412                                         break;
413                                 }
414                                 case 5:
415                                 {
416                                         if (r_q.dom.id5.buffer_dom_name != 0)
417                                         {
418                                                 char *dom_name = unistrn2(r_q.dom.id5.uni_domain_name.buffer,
419                                                                           r_q.dom.id5.uni_domain_name.uni_str_len);
420                                                 fstrcpy(domain_name, dom_name);
421                                         }
422                                         if (r_q.dom.id5.buffer_dom_sid != 0)
423                                         {
424                                                 *domain_sid = r_q.dom.id5.dom_sid.sid;
425                                         }
426
427                                         valid_response = True;
428                                         break;
429                                 }
430                                 default:
431                                 {
432                                         DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
433                                         domain_name[0] = 0;
434
435                                         break;
436                                 }
437                         }
438                 
439                         sid_to_string(sid_str, domain_sid);
440                         DEBUG(3,("LSA_QUERYINFOPOLICY (level %x): domain:%s  domain sid:%s\n",
441                                   r_q.info_class, domain_name, sid_str));
442                 }
443         }
444
445         prs_mem_free(&rbuf);
446         prs_mem_free(&buf );
447
448         return valid_response;
449 }
450
451 /****************************************************************************
452 do a LSA Close
453 ****************************************************************************/
454 BOOL lsa_close(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd)
455 {
456         prs_struct rbuf;
457         prs_struct buf; 
458         LSA_Q_CLOSE q_c;
459     BOOL valid_close = False;
460
461         if (hnd == NULL) return False;
462
463         /* create and send a MSRPC command with api LSA_OPENPOLICY */
464
465         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
466         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
467
468         DEBUG(4,("LSA Close\n"));
469
470         /* store the parameters */
471         make_lsa_q_close(&q_c, hnd);
472
473         /* turn parameters into data stream */
474         lsa_io_q_close("", &q_c, &buf, 0);
475
476         /* send the data on \PIPE\ */
477         if (rpc_api_pipe_req(cli, fnum, LSA_CLOSE, &buf, &rbuf))
478         {
479                 LSA_R_CLOSE r_c;
480                 BOOL p;
481
482                 lsa_io_r_close("", &r_c, &rbuf, 0);
483                 p = rbuf.offset != 0;
484
485                 if (p && r_c.status != 0)
486                 {
487                         /* report error code */
488                         DEBUG(0,("LSA_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
489                         p = False;
490                 }
491
492                 if (p)
493                 {
494                         /* check that the returned policy handle is all zeros */
495                         int i;
496                         valid_close = True;
497
498                         for (i = 0; i < sizeof(r_c.pol.data); i++)
499                         {
500                                 if (r_c.pol.data[i] != 0)
501                                 {
502                                         valid_close = False;
503                                         break;
504                                 }
505                         }       
506                         if (!valid_close)
507                         {
508                                 DEBUG(0,("LSA_CLOSE: non-zero handle returned\n"));
509                         }
510                 }
511         }
512
513         prs_mem_free(&rbuf);
514         prs_mem_free(&buf );
515
516         return valid_close;
517 }
518
519