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