638222590d6b2752f0b51117665763f863bdb976
[samba.git] / source3 / rpc_parse / parse_lsa.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  RPC Pipe client / server routines
5  *  Copyright (C) Andrew Tridgell              1992-1997,
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7  *  Copyright (C) Paul Ashton                       1997.
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "includes.h"
25 #include "nterr.h"
26
27 extern int DEBUGLEVEL;
28
29
30 /*******************************************************************
31 creates a LSA_TRANS_NAME structure.
32 ********************************************************************/
33 void make_lsa_trans_name(LSA_TRANS_NAME *trn, uint32 sid_name_use, char *name, uint32 idx)
34 {
35         int len_name = strlen(name);
36
37         trn->sid_name_use = sid_name_use;
38         make_uni_hdr(&(trn->hdr_name), len_name, len_name, len_name != 0);
39         make_unistr2(&(trn->uni_name), name, len_name);
40         trn->domain_idx = idx;
41 }
42
43 /*******************************************************************
44 reads or writes a LSA_TRANS_NAME structure.
45 ********************************************************************/
46 void lsa_io_trans_name(char *desc, LSA_TRANS_NAME *trn, prs_struct *ps, int depth)
47 {
48         if (trn == NULL) return;
49
50         prs_debug(ps, depth, desc, "lsa_io_trans_name");
51         depth++;
52
53         prs_align(ps);
54         
55         prs_uint32("sid_name_use", ps, depth, &(trn->sid_name_use));
56
57         smb_io_unihdr ("hdr_name", &(trn->hdr_name), ps, depth);
58         smb_io_unistr2("uni_name", &(trn->uni_name), trn->hdr_name.buffer, ps, depth);
59
60         prs_uint32("domain_idx  ", ps, depth, &(trn->domain_idx  ));
61 }
62
63 /*******************************************************************
64 reads or writes a DOM_R_REF structure.
65 ********************************************************************/
66 void lsa_io_dom_r_ref(char *desc,  DOM_R_REF *r_r, prs_struct *ps, int depth)
67 {
68         int i;
69
70         prs_debug(ps, depth, desc, "smb_io_dom_r_ref");
71         depth++;
72
73         if (r_r == NULL) return;
74
75         prs_align(ps);
76         
77         prs_uint32("undoc_buffer   ", ps, depth, &(r_r->undoc_buffer)); /* undocumented buffer pointer. */
78         prs_uint32("num_ref_doms_1 ", ps, depth, &(r_r->num_ref_doms_1)); /* num referenced domains? */
79         prs_uint32("buffer_dom_name", ps, depth, &(r_r->buffer_dom_name)); /* undocumented domain name buffer pointer. */
80         prs_uint32("max_entries    ", ps, depth, &(r_r->max_entries)); /* 32 - max number of entries */
81         prs_uint32("num_ref_doms_2 ", ps, depth, &(r_r->num_ref_doms_2)); /* 4 - num referenced domains? */
82
83         smb_io_unihdr2("", &(r_r->hdr_dom_name), ps, depth); /* domain name unicode string header */
84
85         SMB_ASSERT_ARRAY(r_r->hdr_ref_dom, r_r->num_ref_doms_1-1);
86
87         for (i = 0; i < r_r->num_ref_doms_1-1; i++)
88         {
89                 smb_io_unihdr2("", &(r_r->hdr_ref_dom[i]), ps, depth);
90         }
91
92         smb_io_unistr("", &(r_r->uni_dom_name), ps, depth); /* domain name unicode string */
93
94         SMB_ASSERT_ARRAY(r_r->ref_dom, r_r->num_ref_doms_2);
95
96         for (i = 0; i < r_r->num_ref_doms_2; i++)
97         {
98                 smb_io_dom_sid2("", &(r_r->ref_dom[i]), ps, depth); /* referenced domain SIDs */
99         }
100 }
101
102 /*******************************************************************
103 makes an LSA_OBJ_ATTR structure.
104 ********************************************************************/
105 void make_lsa_obj_attr(LSA_OBJ_ATTR *attr, uint32 attributes, uint32 sec_qos)
106 {
107         if (attr == NULL) return;
108
109         DEBUG(5,("make_lsa_obj_attr\n"));
110
111         attr->len = 0x18; /* length of object attribute block, in bytes */
112         attr->ptr_root_dir = 0;
113         attr->ptr_obj_name = 0;
114         attr->attributes = attributes;
115         attr->ptr_sec_desc = 0;
116         attr->sec_qos = sec_qos;
117 }
118
119 /*******************************************************************
120 reads or writes an LSA_OBJ_ATTR structure.
121 ********************************************************************/
122 void lsa_io_obj_attr(char *desc,  LSA_OBJ_ATTR *attr, prs_struct *ps, int depth)
123 {
124         int start;
125
126         if (attr == NULL) return;
127
128         prs_debug(ps, depth, desc, "lsa_io_obj_attr");
129         depth++;
130
131         prs_align(ps);
132         
133         start = ps->offset;
134
135         /* these pointers had _better_ be zero, because we don't know
136            what they point to!
137          */
138         prs_uint32("len"         , ps, depth, &(attr->len         )); /* 0x18 - length (in bytes) inc. the length field. */
139         prs_uint32("ptr_root_dir", ps, depth, &(attr->ptr_root_dir)); /* 0 - root directory (pointer) */
140         prs_uint32("ptr_obj_name", ps, depth, &(attr->ptr_obj_name)); /* 0 - object name (pointer) */
141         prs_uint32("attributes"  , ps, depth, &(attr->attributes  )); /* 0 - attributes (undocumented) */
142         prs_uint32("ptr_sec_desc", ps, depth, &(attr->ptr_sec_desc)); /* 0 - security descriptior (pointer) */
143         prs_uint32("sec_qos"     , ps, depth, &(attr->sec_qos     )); /* 0 - security quality of service */
144
145         if (attr->len != ps->offset - start)
146         {
147                 DEBUG(3,("lsa_io_obj_attr: length %x does not match size %x\n",
148                          attr->len, ps->offset - start));
149         }
150 }
151 /*******************************************************************
152 makes an LSA_Q_OPEN_POL structure.
153 ********************************************************************/
154 void make_q_open_pol(LSA_Q_OPEN_POL *r_q, char *server_name,
155                         uint32 attributes, uint32 sec_qos,
156                         uint32 desired_access)
157 {
158         if (r_q == NULL) return;
159
160         DEBUG(5,("make_open_pol\n"));
161
162         r_q->ptr = 1; /* undocumented pointer */
163
164         make_unistr2     (&(r_q->uni_server_name), server_name, strlen(server_name));
165         make_lsa_obj_attr(&(r_q->attr           ), attributes, sec_qos);
166
167         r_q->des_access = desired_access;
168 }
169
170 /*******************************************************************
171 reads or writes an LSA_Q_OPEN_POL structure.
172 ********************************************************************/
173 void lsa_io_q_open_pol(char *desc,  LSA_Q_OPEN_POL *r_q, prs_struct *ps, int depth)
174 {
175         if (r_q == NULL) return;
176
177         prs_debug(ps, depth, desc, "lsa_io_q_open_pol");
178         depth++;
179
180         prs_uint32("ptr       ", ps, depth, &(r_q->ptr       ));
181
182         smb_io_unistr2 ("", &(r_q->uni_server_name), r_q->ptr, ps, depth);
183         lsa_io_obj_attr("", &(r_q->attr           ), ps, depth);
184
185         prs_uint32("des_access", ps, depth, &(r_q->des_access));
186 }
187
188 /*******************************************************************
189 reads or writes an LSA_R_OPEN_POL structure.
190 ********************************************************************/
191 void lsa_io_r_open_pol(char *desc,  LSA_R_OPEN_POL *r_p, prs_struct *ps, int depth)
192 {
193         if (r_p == NULL) return;
194
195         prs_debug(ps, depth, desc, "lsa_io_r_open_pol");
196         depth++;
197
198
199         smb_io_pol_hnd("", &(r_p->pol), ps, depth);
200
201         prs_uint32("status", ps, depth, &(r_p->status));
202 }
203
204 /*******************************************************************
205 makes an LSA_Q_QUERY_INFO structure.
206 ********************************************************************/
207 void make_q_query(LSA_Q_QUERY_INFO *q_q, POLICY_HND *hnd, uint16 info_class)
208 {
209         if (q_q == NULL || hnd == NULL) return;
210
211         DEBUG(5,("make_q_query\n"));
212
213         memcpy(&(q_q->pol), hnd, sizeof(q_q->pol));
214
215         q_q->info_class = info_class;
216 }
217
218 /*******************************************************************
219 reads or writes an LSA_Q_QUERY_INFO structure.
220 ********************************************************************/
221 void lsa_io_q_query(char *desc,  LSA_Q_QUERY_INFO *q_q, prs_struct *ps, int depth)
222 {
223         if (q_q == NULL) return;
224
225         prs_debug(ps, depth, desc, "lsa_io_q_query");
226         depth++;
227
228         smb_io_pol_hnd("", &(q_q->pol), ps, depth);
229
230         prs_uint16("info_class", ps, depth, &(q_q->info_class));
231 }
232
233 /*******************************************************************
234 reads or writes an LSA_Q_ENUM_TRUST_DOM structure.
235 ********************************************************************/
236 void lsa_io_q_enum_trust_dom(char *desc,  LSA_Q_ENUM_TRUST_DOM *q_e, prs_struct *ps, int depth)
237 {
238         if (q_e == NULL) return;
239
240         prs_debug(ps, depth, desc, "lsa_io_q_enum_trust_dom");
241         depth++;
242
243
244         smb_io_pol_hnd("", &(q_e->pol), ps, depth);
245
246         prs_uint32("enum_context ", ps, depth, &(q_e->enum_context ));
247         prs_uint32("preferred_len", ps, depth, &(q_e->preferred_len));
248 }
249
250 /*******************************************************************
251 makes an LSA_R_ENUM_TRUST_DOM structure.
252 ********************************************************************/
253 void make_r_enum_trust_dom(LSA_R_ENUM_TRUST_DOM *r_e,
254                            uint32 enum_context, char *domain_name, DOM_SID *domain_sid,
255                            uint32 status)
256 {
257         if (r_e == NULL) return;
258
259         DEBUG(5,("make_r_enum_trust_dom\n"));
260
261         r_e->enum_context = enum_context;
262
263         if (status == 0)
264         {
265                 int len_domain_name = strlen(domain_name);
266
267                 r_e->num_domains  = 1;
268                 r_e->ptr_enum_domains = 1;
269                 r_e->num_domains2 = 1;
270
271                 make_uni_hdr2(&(r_e->hdr_domain_name ), len_domain_name, len_domain_name, 4);
272                 make_unistr2 (&(r_e->uni_domain_name ), domain_name, len_domain_name);
273                 make_dom_sid2(&(r_e->other_domain_sid), domain_sid);
274         }
275         else
276         {
277                 r_e->num_domains = 0;
278                 r_e->ptr_enum_domains = 0;
279         }
280
281         r_e->status = status;
282 }
283
284 /*******************************************************************
285 reads or writes an LSA_R_ENUM_TRUST_DOM structure.
286 ********************************************************************/
287 void lsa_io_r_enum_trust_dom(char *desc,  LSA_R_ENUM_TRUST_DOM *r_e, prs_struct *ps, int depth)
288 {
289         if (r_e == NULL) return;
290
291         prs_debug(ps, depth, desc, "lsa_io_r_enum_trust_dom");
292         depth++;
293
294         prs_uint32("enum_context    ", ps, depth, &(r_e->enum_context    ));
295         prs_uint32("num_domains     ", ps, depth, &(r_e->num_domains     ));
296         prs_uint32("ptr_enum_domains", ps, depth, &(r_e->ptr_enum_domains));
297
298         if (r_e->ptr_enum_domains != 0)
299         {
300                 prs_uint32("num_domains2", ps, depth, &(r_e->num_domains2));
301                 smb_io_unihdr2 ("", &(r_e->hdr_domain_name ), ps, depth);
302                 smb_io_unistr2 ("", &(r_e->uni_domain_name ), r_e->hdr_domain_name.buffer, ps, depth);
303                 smb_io_dom_sid2("", &(r_e->other_domain_sid), ps, depth);
304         }
305
306         prs_uint32("status", ps, depth, &(r_e->status));
307 }
308
309 /*******************************************************************
310 makes an LSA_Q_CLOSE structure.
311 ********************************************************************/
312 void make_lsa_q_close(LSA_Q_CLOSE *q_c, POLICY_HND *hnd)
313 {
314         if (q_c == NULL || hnd == NULL) return;
315
316         DEBUG(5,("make_lsa_q_close\n"));
317
318         memcpy(&(q_c->pol), hnd, sizeof(q_c->pol));
319 }
320
321
322 /*******************************************************************
323 reads or writes an LSA_Q_CLOSE structure.
324 ********************************************************************/
325 void lsa_io_q_close(char *desc,  LSA_Q_CLOSE *q_c, prs_struct *ps, int depth)
326 {
327         if (q_c == NULL) return;
328
329         prs_debug(ps, depth, desc, "lsa_io_q_close");
330         depth++;
331
332         smb_io_pol_hnd("", &(q_c->pol), ps, depth);
333 }
334
335 /*******************************************************************
336 makes an LSA_R_CLOSE structure.
337 ********************************************************************/
338 void make_lsa_r_close(LSA_R_CLOSE *q_r, POLICY_HND *hnd)
339 {
340         if (q_r == NULL || hnd == NULL) return;
341
342         DEBUG(5,("make_lsa_r_close\n"));
343
344         memcpy(&(q_r->pol), hnd, sizeof(q_r->pol));
345 }
346
347
348 /*******************************************************************
349 reads or writes an LSA_R_CLOSE structure.
350 ********************************************************************/
351 void lsa_io_r_close(char *desc,  LSA_R_CLOSE *r_c, prs_struct *ps, int depth)
352 {
353         if (r_c == NULL) return;
354
355         prs_debug(ps, depth, desc, "lsa_io_r_close");
356         depth++;
357
358         smb_io_pol_hnd("", &(r_c->pol), ps, depth);
359
360         prs_uint32("status", ps, depth, &(r_c->status));
361 }
362
363 /*******************************************************************
364 reads or writes an LSA_Q_QUERY_INFO structure.
365 ********************************************************************/
366 void lsa_io_r_query(char *desc,  LSA_R_QUERY_INFO *r_q, prs_struct *ps, int depth)
367 {
368         if (r_q == NULL) return;
369
370         prs_debug(ps, depth, desc, "lsa_io_r_query");
371         depth++;
372
373         prs_uint32("undoc_buffer", ps, depth, &(r_q->undoc_buffer));
374
375         if (r_q->undoc_buffer != 0)
376         {
377                 prs_uint16("info_class", ps, depth, &(r_q->info_class));
378
379                 switch (r_q->info_class)
380                 {
381                         case 3:
382                         {
383                                 smb_io_dom_query_3("", &(r_q->dom.id3), ps, depth);
384                                 break;
385                         }
386                         case 5:
387                         {
388                                 smb_io_dom_query_5("", &(r_q->dom.id3), ps, depth);
389                                 break;
390                         }
391                         default:
392                         {
393                                 /* PANIC! */
394                                 break;
395                         }
396                 }
397         }
398
399         prs_uint32("status", ps, depth, &(r_q->status));
400 }
401
402 /*******************************************************************
403 reads or writes a LSA_SID_ENUM structure.
404 ********************************************************************/
405 void lsa_io_sid_enum(char *desc, LSA_SID_ENUM *sen, prs_struct *ps, int depth)
406 {
407         int i;
408
409         if (sen == NULL) return;
410
411         prs_debug(ps, depth, desc, "lsa_io_sid_enum");
412         depth++;
413
414         prs_align(ps);
415         
416         prs_uint32("num_entries ", ps, depth, &(sen->num_entries));
417         prs_uint32("ptr_sid_enum", ps, depth, &(sen->ptr_sid_enum)); 
418         prs_uint32("num_entries2", ps, depth, &(sen->num_entries2)); 
419
420         SMB_ASSERT_ARRAY(sen->ptr_sid, sen->num_entries);
421
422         for (i = 0; i < sen->num_entries; i++)
423         {       
424                 fstring temp;
425                 slprintf(temp, sizeof(temp) - 1, "ptr_sid[%d]", i);
426                 prs_uint32(temp, ps, depth, &(sen->ptr_sid[i])); /* domain SID pointers to be looked up. */
427         }
428
429         SMB_ASSERT_ARRAY(sen->sid, sen->num_entries);
430
431         for (i = 0; i < sen->num_entries; i++)
432         {
433                 fstring temp;
434                 slprintf(temp, sizeof(temp) - 1, "sid[%d]", i);
435                 smb_io_dom_sid2(temp, &(sen->sid[i]), ps, depth); /* domain SIDs to be looked up. */
436         }
437 }
438
439 /*******************************************************************
440 reads or writes a LSA_Q_LOOKUP_SIDS structure.
441 ********************************************************************/
442 void lsa_io_q_lookup_sids(char *desc, LSA_Q_LOOKUP_SIDS *q_s, prs_struct *ps, int depth)
443 {
444         if (q_s == NULL) return;
445
446         prs_debug(ps, depth, desc, "lsa_io_q_lookup_sids");
447         depth++;
448
449         prs_align(ps);
450         
451     smb_io_pol_hnd     ("pol_hnd", &(q_s->pol_hnd), ps, depth); /* policy handle */
452     lsa_io_sid_enum    ("sids   ", &(q_s->sids   ), ps, depth); /* sids to be looked up */
453     lsa_io_trans_names ("names  ", &(q_s->names  ), ps, depth); /* translated names */
454     smb_io_lookup_level("switch ", &(q_s->level  ), ps, depth); /* lookup level */
455
456         prs_uint32("mapped_count", ps, depth, &(q_s->mapped_count));
457 }
458
459 /*******************************************************************
460 reads or writes a structure.
461 ********************************************************************/
462 void lsa_io_trans_names(char *desc, LSA_TRANS_NAME_ENUM *trn, prs_struct *ps, int depth)
463 {
464         int i;
465         int i2;
466
467         if (trn == NULL) return;
468
469         prs_debug(ps, depth, desc, "lsa_io_trans_names");
470         depth++;
471
472         prs_align(ps);
473         
474         prs_uint32("num_entries    ", ps, depth, &(trn->num_entries));
475         prs_uint32("ptr_trans_names", ps, depth, &(trn->ptr_trans_names));
476         prs_uint32("num_entries2   ", ps, depth, &(trn->num_entries2));
477
478         SMB_ASSERT_ARRAY(trn->ptr_name, trn->num_entries);
479
480         for (i = 0; i < trn->num_entries; i++)
481         {       
482                 fstring temp;
483                 slprintf(temp, sizeof(temp) - 1, "ptr_name[%d] ", i);
484                 prs_uint32(temp, ps, depth, &(trn->ptr_name[i])); /* pointer to translated name */
485         }
486
487         for (i = 0, i2 = 0; i < trn->num_entries2; i++)
488         {
489                 if (trn->ptr_name[i] != 0)
490                 {
491                         fstring temp;
492                         slprintf(temp, sizeof(temp) - 1, "name[%d] ", i);
493                         lsa_io_trans_name(temp, &(trn->name[i2]), ps, depth); /* translated name */
494                         i2++;
495                 }
496         }
497 }
498
499 /*******************************************************************
500 reads or writes a structure.
501 ********************************************************************/
502 void lsa_io_r_lookup_sids(char *desc,  LSA_R_LOOKUP_SIDS *r_s, prs_struct *ps, int depth)
503 {
504         if (r_s == NULL) return;
505
506         prs_debug(ps, depth, desc, "lsa_io_r_lookup_sids");
507         depth++;
508
509         prs_align(ps);
510         
511         lsa_io_dom_r_ref  ("dom_ref", r_s->dom_ref, ps, depth); /* domain reference info */
512         lsa_io_trans_names("names  ", r_s->names  , ps, depth); /* translated names */
513
514         prs_uint32("mapped_count", ps, depth, &(r_s->mapped_count));
515
516         prs_uint32("status      ", ps, depth, &(r_s->status));
517 }
518
519 /*******************************************************************
520 reads or writes a structure.
521 ********************************************************************/
522 void lsa_io_q_lookup_rids(char *desc,  LSA_Q_LOOKUP_RIDS *q_r, prs_struct *ps, int depth)
523 {
524         int i;
525
526         if (q_r == NULL) return;
527
528         prs_debug(ps, depth, desc, "lsa_io_q_lookup_rids");
529         depth++;
530
531         prs_align(ps);
532         
533     smb_io_pol_hnd("", &(q_r->pol_hnd), ps, depth); /* policy handle */
534
535         prs_uint32("num_entries    ", ps, depth, &(q_r->num_entries));
536         prs_uint32("num_entries2   ", ps, depth, &(q_r->num_entries2));
537         prs_uint32("buffer_dom_sid ", ps, depth, &(q_r->buffer_dom_sid)); /* undocumented domain SID buffer pointer */
538         prs_uint32("buffer_dom_name", ps, depth, &(q_r->buffer_dom_name)); /* undocumented domain name buffer pointer */
539
540         SMB_ASSERT_ARRAY(q_r->lookup_name, q_r->num_entries);
541
542         for (i = 0; i < q_r->num_entries; i++)
543         {
544                 smb_io_dom_name("", &(q_r->lookup_name[i]), ps, depth); /* names to be looked up */
545         }
546
547         prs_uint8s (False, "undoc          ", ps, depth, q_r->undoc, UNKNOWN_LEN);
548 }
549
550 /*******************************************************************
551 reads or writes a structure.
552 ********************************************************************/
553 void lsa_io_r_lookup_rids(char *desc,  LSA_R_LOOKUP_RIDS *r_r, prs_struct *ps, int depth)
554 {
555         int i;
556
557         if (r_r == NULL) return;
558
559         prs_debug(ps, depth, desc, "lsa_io_r_lookup_rids");
560         depth++;
561
562         prs_align(ps);
563         
564         lsa_io_dom_r_ref("", &(r_r->dom_ref), ps, depth); /* domain reference info */
565
566         prs_uint32("num_entries ", ps, depth, &(r_r->num_entries));
567         prs_uint32("undoc_buffer", ps, depth, &(r_r->undoc_buffer));
568         prs_uint32("num_entries2", ps, depth, &(r_r->num_entries2));
569
570         SMB_ASSERT_ARRAY(r_r->dom_rid, r_r->num_entries2);
571
572         for (i = 0; i < r_r->num_entries2; i++)
573         {
574                 smb_io_dom_rid2("", &(r_r->dom_rid[i]), ps, depth); /* domain RIDs being looked up */
575         }
576
577         prs_uint32("num_entries3", ps, depth, &(r_r->num_entries3));
578
579         prs_uint32("status      ", ps, depth, &(r_r->status));
580 }
581