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