more merging voodoo
[kai/samba.git] / source / rpc_server / srv_lsa.c
1 #define OLD_NTDOMAIN 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  *  Copyright (C) Jeremy Allison                    1998.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *  
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *  
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include "includes.h"
27
28 extern int DEBUGLEVEL;
29 extern DOM_SID global_sam_sid;
30 extern fstring global_myworkgroup;
31 extern pstring global_myname;
32
33 /***************************************************************************
34  lsa_reply_open_policy2
35  ***************************************************************************/
36
37 static BOOL lsa_reply_open_policy2(prs_struct *rdata)
38 {
39         int i;
40         LSA_R_OPEN_POL2 r_o;
41
42         ZERO_STRUCT(r_o);
43
44         /* set up the LSA QUERY INFO response */
45
46         for (i = 4; i < POL_HND_SIZE; i++)
47                 r_o.pol.data[i] = i;
48         r_o.status = 0x0;
49
50         /* store the response in the SMB stream */
51         if(!lsa_io_r_open_pol2("", &r_o, rdata, 0)) {
52                 DEBUG(0,("lsa_reply_open_policy2: unable to marshall LSA_R_OPEN_POL2.\n"));
53                 return False;
54         }
55
56         return True;
57 }
58
59 /***************************************************************************
60 lsa_reply_open_policy
61  ***************************************************************************/
62
63 static BOOL lsa_reply_open_policy(prs_struct *rdata)
64 {
65         int i;
66         LSA_R_OPEN_POL r_o;
67
68         ZERO_STRUCT(r_o);
69
70         /* set up the LSA QUERY INFO response */
71
72         for (i = 4; i < POL_HND_SIZE; i++)
73                 r_o.pol.data[i] = i;
74         r_o.status = 0x0;
75
76         /* store the response in the SMB stream */
77         if(!lsa_io_r_open_pol("", &r_o, rdata, 0)) {
78                 DEBUG(0,("lsa_reply_open_policy: unable to marshall LSA_R_OPEN_POL.\n"));
79                 return False;
80         }
81
82         return True;
83 }
84
85 /***************************************************************************
86 Init dom_query
87  ***************************************************************************/
88
89 static void init_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
90 {
91         fstring sid_str;
92         int domlen = strlen(dom_name);
93
94         *sid_str = '\0';
95
96         d_q->uni_dom_max_len = domlen * 2;
97         d_q->uni_dom_str_len = domlen * 2;
98
99         d_q->buffer_dom_name = domlen  != 0    ? 1 : 0; /* domain buffer pointer */
100         d_q->buffer_dom_sid  = dom_sid != NULL ? 1 : 0; /* domain sid pointer */
101
102         /* this string is supposed to be character short */
103         init_unistr2(&d_q->uni_domain_name, dom_name, domlen);
104
105         if(dom_sid) {
106                 sid_to_string(sid_str, dom_sid);
107                 init_dom_sid2(&d_q->dom_sid, dom_sid);
108         }
109 }
110
111 /***************************************************************************
112  lsa_reply_enum_trust_dom
113  ***************************************************************************/
114
115 static void lsa_reply_enum_trust_dom(LSA_Q_ENUM_TRUST_DOM *q_e,
116                                 prs_struct *rdata,
117                                 uint32 enum_context, char *dom_name, DOM_SID *dom_sid)
118 {
119         LSA_R_ENUM_TRUST_DOM r_e;
120
121         ZERO_STRUCT(r_e);
122
123         /* set up the LSA QUERY INFO response */
124         init_r_enum_trust_dom(&r_e, enum_context, dom_name, dom_sid,
125               dom_name != NULL ? 0x0 : 0x80000000 | NT_STATUS_UNABLE_TO_FREE_VM);
126
127         /* store the response in the SMB stream */
128         lsa_io_r_enum_trust_dom("", &r_e, rdata, 0);
129 }
130
131 /***************************************************************************
132 lsa_reply_query_info
133  ***************************************************************************/
134
135 static BOOL lsa_reply_query_info(LSA_Q_QUERY_INFO *q_q, prs_struct *rdata,
136                                 char *dom_name, DOM_SID *dom_sid, uint32 status_code)
137 {
138         LSA_R_QUERY_INFO r_q;
139
140         ZERO_STRUCT(r_q);
141
142         /* set up the LSA QUERY INFO response */
143
144         if(status_code == 0) {
145                 r_q.undoc_buffer = 0x22000000; /* bizarre */
146                 r_q.info_class = q_q->info_class;
147
148                 init_dom_query(&r_q.dom.id5, dom_name, dom_sid);
149         }
150
151         r_q.status = status_code;
152
153         /* store the response in the SMB stream */
154         if(!lsa_io_r_query("", &r_q, rdata, 0)) {
155                 DEBUG(0,("lsa_reply_query_info: failed to marshall LSA_R_QUERY_INFO.\n"));
156                 return False;
157         }
158
159         return True;
160 }
161
162 /***************************************************************************
163  init_dom_ref - adds a domain if it's not already in, returns the index.
164 ***************************************************************************/
165
166 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
167 {
168         int num = 0;
169         int len;
170
171         if (dom_name != NULL) {
172                 for (num = 0; num < ref->num_ref_doms_1; num++) {
173                         fstring domname;
174                         fstrcpy(domname, dos_unistr2_to_str(&ref->ref_dom[num].uni_dom_name));
175                         if (strequal(domname, dom_name))
176                                 return num;
177                 }
178         } else {
179                 num = ref->num_ref_doms_1;
180         }
181
182         if (num >= MAX_REF_DOMAINS) {
183                 /* index not found, already at maximum domain limit */
184                 return -1;
185         }
186
187         ref->num_ref_doms_1 = num+1;
188         ref->ptr_ref_dom  = 1;
189         ref->max_entries = MAX_REF_DOMAINS;
190         ref->num_ref_doms_2 = num+1;
191
192         len = (dom_name != NULL) ? strlen(dom_name) : 0;
193         if(dom_name != NULL && len == 0)
194                 len = 1;
195
196         init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, len);
197         ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
198
199         init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, len);
200         init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
201
202         return num;
203 }
204
205 /***************************************************************************
206  init_lsa_rid2s
207  ***************************************************************************/
208
209 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
210                                 int num_entries, UNISTR2 name[MAX_LOOKUP_SIDS],
211                                 uint32 *mapped_count)
212 {
213         int i;
214         int total = 0;
215         *mapped_count = 0;
216
217         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
218
219         for (i = 0; i < num_entries; i++) {
220                 BOOL status = False;
221                 DOM_SID dom_sid;
222                 DOM_SID sid;
223                 uint32 rid = 0xffffffff;
224                 int dom_idx = -1;
225                 pstring full_name;
226                 fstring dom_name;
227                 fstring user;
228                 uint8 sid_name_use = SID_NAME_UNKNOWN;
229
230                 pstrcpy(full_name, dos_unistr2_to_str(&name[i]));
231
232                 /*
233                  * Try and split the name into a DOMAIN and
234                  * user component.
235                  */
236
237                 split_domain_name(full_name, dom_name, user);
238
239                 /*
240                  * We only do anything with this name if we
241                  * can map the Domain into a SID we know.
242                  */
243
244                 if (map_domain_name_to_sid(&dom_sid, dom_name)) {
245                         dom_idx = init_dom_ref(ref, dom_name, &dom_sid);
246
247                         if (lookup_local_name(dom_name, user, &sid, &sid_name_use) && sid_split_rid(&sid, &rid)) 
248                                 status = True;
249                 }
250
251                 if (status)
252                         (*mapped_count)++;
253                 else {
254                         dom_idx = -1;
255                         rid = 0xffffffff;
256                         sid_name_use = SID_NAME_UNKNOWN;
257                 }
258
259                 init_dom_rid2(&rid2[total], rid, sid_name_use, dom_idx);
260                 total++;
261         }
262 }
263
264 /***************************************************************************
265  init_reply_lookup_names
266  ***************************************************************************/
267
268 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
269                 DOM_R_REF *ref, uint32 num_entries,
270                 DOM_RID2 *rid2, uint32 mapped_count)
271 {
272         r_l->ptr_dom_ref  = 1;
273         r_l->dom_ref      = ref;
274
275         r_l->num_entries  = num_entries;
276         r_l->ptr_entries  = 1;
277         r_l->num_entries2 = num_entries;
278         r_l->dom_rid      = rid2;
279
280         r_l->mapped_count = mapped_count;
281
282         if (mapped_count == 0)
283                 r_l->status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
284         else
285                 r_l->status = 0x0;
286 }
287
288 /***************************************************************************
289  Init lsa_trans_names.
290  ***************************************************************************/
291
292 static void init_lsa_trans_names(DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
293                                 int num_entries, DOM_SID2 sid[MAX_LOOKUP_SIDS], uint32 *mapped_count)
294 {
295         extern DOM_SID global_sid_S_1_5_0x20; /* BUILTIN sid. */
296         int i;
297         int total = 0;
298         *mapped_count = 0;
299
300         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
301
302         for (i = 0; i < num_entries; i++) {
303                 BOOL status = False;
304                 DOM_SID find_sid = sid[i].sid;
305                 uint32 rid = 0xffffffff;
306                 int dom_idx = -1;
307                 fstring name;
308                 fstring dom_name;
309                 uint8 sid_name_use = 0;
310
311                 memset(dom_name, '\0', sizeof(dom_name));
312                 memset(name, '\0', sizeof(name));
313
314                 /*
315                  * First, check to see if the SID is one of the well
316                  * known ones (this includes our own domain SID).
317                  * Next, check if the domain prefix is one of the
318                  * well known ones. If so and the domain prefix was
319                  * either BUILTIN or our own global sid, then lookup
320                  * the RID as a user or group id and translate to
321                  * a name.
322                  */
323
324                 if (map_domain_sid_to_name(&find_sid, dom_name)) {
325                         sid_name_use = SID_NAME_DOMAIN;
326                 } else if (sid_split_rid(&find_sid, &rid) && map_domain_sid_to_name(&find_sid, dom_name)) {
327                         if (sid_equal(&find_sid, &global_sam_sid) ||
328                                 sid_equal(&find_sid, &global_sid_S_1_5_0x20)) {
329                                 status = lookup_local_rid(rid, name, &sid_name_use);
330                         } else  {
331                                 status = lookup_known_rid(&find_sid, rid, name, &sid_name_use);
332                         }
333                 }
334
335                 DEBUG(10,("init_lsa_trans_names: adding domain '%s' sid %s to referenced list.\n",
336                                 dom_name, name ));
337
338                 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
339
340                 if(!status) {
341                         slprintf(name, sizeof(name)-1, "unix.%08x", rid);
342                         sid_name_use = SID_NAME_UNKNOWN;
343                 }
344
345                 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to referenced list.\n", dom_name, name ));
346
347                 (*mapped_count)++;
348
349                 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
350                                         sid_name_use, name, dom_idx);
351                 total++;
352         }
353
354         trn->num_entries = total;
355         trn->ptr_trans_names = 1;
356         trn->num_entries2 = total;
357 }
358
359 /***************************************************************************
360  Init_reply_lookup_sids.
361  ***************************************************************************/
362
363 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
364                 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
365                 uint32 mapped_count)
366 {
367         r_l->ptr_dom_ref  = 1;
368         r_l->dom_ref      = ref;
369         r_l->names        = names;
370         r_l->mapped_count = mapped_count;
371
372         if (mapped_count == 0)
373                 r_l->status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
374         else
375                 r_l->status = 0x0;
376 }
377
378 /***************************************************************************
379 lsa_reply_lookup_sids
380  ***************************************************************************/
381
382 static BOOL lsa_reply_lookup_sids(prs_struct *rdata, DOM_SID2 *sid, int num_entries)
383 {
384         LSA_R_LOOKUP_SIDS r_l;
385         DOM_R_REF ref;
386         LSA_TRANS_NAME_ENUM names;
387         uint32 mapped_count = 0;
388
389         ZERO_STRUCT(r_l);
390         ZERO_STRUCT(ref);
391         ZERO_STRUCT(names);
392
393         /* set up the LSA Lookup SIDs response */
394         init_lsa_trans_names(&ref, &names, num_entries, sid, &mapped_count);
395         init_reply_lookup_sids(&r_l, &ref, &names, mapped_count);
396
397         /* store the response in the SMB stream */
398         if(!lsa_io_r_lookup_sids("", &r_l, rdata, 0)) {
399                 DEBUG(0,("lsa_reply_lookup_sids: Failed to marshall LSA_R_LOOKUP_SIDS.\n"));
400                 return False;
401         }
402
403         return True;
404 }
405
406 /***************************************************************************
407 lsa_reply_lookup_names
408  ***************************************************************************/
409
410 static BOOL lsa_reply_lookup_names(prs_struct *rdata,
411                 UNISTR2 names[MAX_LOOKUP_SIDS], int num_entries)
412 {
413         LSA_R_LOOKUP_NAMES r_l;
414         DOM_R_REF ref;
415         DOM_RID2 rids[MAX_LOOKUP_SIDS];
416         uint32 mapped_count = 0;
417
418         ZERO_STRUCT(r_l);
419         ZERO_STRUCT(ref);
420         ZERO_ARRAY(rids);
421
422         /* set up the LSA Lookup RIDs response */
423         init_lsa_rid2s(&ref, rids, num_entries, names, &mapped_count);
424         init_reply_lookup_names(&r_l, &ref, num_entries, rids, mapped_count);
425
426         /* store the response in the SMB stream */
427         if(!lsa_io_r_lookup_names("", &r_l, rdata, 0)) {
428                 DEBUG(0,("lsa_reply_lookup_names: Failed to marshall LSA_R_LOOKUP_NAMES.\n"));
429                 return False;
430         }
431
432         return True;
433 }
434
435 /***************************************************************************
436  api_lsa_open_policy2
437  ***************************************************************************/
438
439 static BOOL api_lsa_open_policy2(prs_struct *data, prs_struct *rdata)
440 {
441         LSA_Q_OPEN_POL2 q_o;
442
443         ZERO_STRUCT(q_o);
444
445         /* grab the server, object attributes and desired access flag...*/
446         if(!lsa_io_q_open_pol2("", &q_o, data, 0)) {
447                 DEBUG(0,("api_lsa_open_policy2: unable to unmarshall LSA_Q_OPEN_POL2.\n"));
448                 return False;
449         }
450
451         /* lkclXXXX having decoded it, ignore all fields in the open policy! */
452
453         /* return a 20 byte policy handle */
454         if(!lsa_reply_open_policy2(rdata))
455                 return False;
456
457         return True;
458 }
459
460 /***************************************************************************
461 api_lsa_open_policy
462  ***************************************************************************/
463 static BOOL api_lsa_open_policy(prs_struct *data, prs_struct *rdata)
464 {
465         LSA_Q_OPEN_POL q_o;
466
467         ZERO_STRUCT(q_o);
468
469         /* grab the server, object attributes and desired access flag...*/
470         if(!lsa_io_q_open_pol("", &q_o, data, 0)) {
471                 DEBUG(0,("api_lsa_open_policy: unable to unmarshall LSA_Q_OPEN_POL.\n"));
472                 return False;
473         }
474
475         /* lkclXXXX having decoded it, ignore all fields in the open policy! */
476
477         /* return a 20 byte policy handle */
478         if(!lsa_reply_open_policy(rdata))
479                 return False;
480
481         return True;
482 }
483
484 /***************************************************************************
485 api_lsa_enum_trust_dom
486  ***************************************************************************/
487 static BOOL api_lsa_enum_trust_dom(prs_struct *data, prs_struct *rdata)
488 {
489         LSA_Q_ENUM_TRUST_DOM q_e;
490
491         ZERO_STRUCT(q_e);
492
493         /* grab the enum trust domain context etc. */
494         if(!lsa_io_q_enum_trust_dom("", &q_e, data, 0))
495                 return False;
496
497         /* construct reply.  return status is always 0x0 */
498         lsa_reply_enum_trust_dom(&q_e, rdata, 0, NULL, NULL);
499
500         return True;
501 }
502
503 /***************************************************************************
504 api_lsa_query_info
505  ***************************************************************************/
506 static BOOL api_lsa_query_info(prs_struct *data, prs_struct *rdata)
507 {
508         LSA_Q_QUERY_INFO q_i;
509         fstring name;
510         DOM_SID *sid = NULL;
511         uint32 status_code = 0;
512
513         memset(name, 0, sizeof(name));
514
515         ZERO_STRUCT(q_i);
516
517         /* grab the info class and policy handle */
518         if(!lsa_io_q_query("", &q_i, data, 0)) {
519                 DEBUG(0,("api_lsa_query_info: failed to unmarshall LSA_Q_QUERY_INFO.\n"));
520                 return False;
521         }
522
523         switch (q_i.info_class) {
524         case 0x03:
525                 if(lp_domain_logons()) {
526                         fstrcpy(name, global_myworkgroup);
527                         sid = &global_sam_sid;
528                 } else {
529                         *name = '\0';
530                 }
531                 break;
532         case 0x05:
533                 fstrcpy(name, global_myname);
534                 sid = &global_sam_sid;
535                 break;
536         default:
537                 DEBUG(0,("api_lsa_query_info: unknown info level in Lsa Query: %d\n", q_i.info_class));
538                 status_code = (NT_STATUS_INVALID_INFO_CLASS | 0xC0000000);
539                 break;
540         }
541
542         /* construct reply.  return status is always 0x0 */
543         if(!lsa_reply_query_info(&q_i, rdata, name, sid, status_code))
544                 return False;
545
546         return True;
547 }
548
549 /***************************************************************************
550  api_lsa_lookup_sids
551  ***************************************************************************/
552
553 static BOOL api_lsa_lookup_sids(prs_struct *data, prs_struct *rdata)
554 {
555         LSA_Q_LOOKUP_SIDS q_l;
556         ZERO_STRUCT(q_l);
557
558         /* grab the info class and policy handle */
559         if(!lsa_io_q_lookup_sids("", &q_l, data, 0)) {
560                 DEBUG(0,("api_lsa_lookup_sids: failed to unmarshall LSA_Q_LOOKUP_SIDS.\n"));
561                 return False;
562         }
563
564         /* construct reply.  return status is always 0x0 */
565         if(!lsa_reply_lookup_sids(rdata, q_l.sids.sid, q_l.sids.num_entries))
566                 return False;
567
568         return True;
569 }
570
571 /***************************************************************************
572  api_lsa_lookup_names
573  ***************************************************************************/
574
575 static BOOL api_lsa_lookup_names(prs_struct *data, prs_struct *rdata)
576 {
577         LSA_Q_LOOKUP_NAMES q_l;
578         ZERO_STRUCT(q_l);
579
580         /* grab the info class and policy handle */
581         if(!lsa_io_q_lookup_names("", &q_l, data, 0)) {
582                 DEBUG(0,("api_lsa_lookup_names: failed to unmarshall LSA_Q_LOOKUP_NAMES.\n"));
583                 return False;
584         }
585
586         SMB_ASSERT_ARRAY(q_l.uni_name, q_l.num_entries);
587
588         return lsa_reply_lookup_names(rdata, q_l.uni_name, q_l.num_entries);
589 }
590
591 /***************************************************************************
592  api_lsa_close
593  ***************************************************************************/
594 static BOOL api_lsa_close(prs_struct *data, prs_struct *rdata)
595 {
596         LSA_R_CLOSE r_c;
597
598         ZERO_STRUCT(r_c);
599
600         /* store the response in the SMB stream */
601         if (!lsa_io_r_close("", &r_c, rdata, 0)) {
602                 DEBUG(0,("api_lsa_close: lsa_io_r_close failed.\n"));
603                 return False;
604         }
605
606         return True;
607 }
608
609 /***************************************************************************
610  api_lsa_open_secret
611  ***************************************************************************/
612 static BOOL api_lsa_open_secret(prs_struct *data, prs_struct *rdata)
613 {
614         /* XXXX this is NOT good */
615         size_t i;
616         uint32 dummy = 0;
617
618         for(i =0; i < 4; i++) {
619                 if(!prs_uint32("api_lsa_close", rdata, 1, &dummy)) {
620                         DEBUG(0,("api_lsa_open_secret: prs_uint32 %d failed.\n",
621                                 (int)i ));
622                         return False;
623                 }
624         }
625
626         dummy = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
627         if(!prs_uint32("api_lsa_close", rdata, 1, &dummy)) {
628                 DEBUG(0,("api_lsa_open_secret: prs_uint32 status failed.\n"));
629                 return False;
630         }
631
632         return True;
633 }
634
635 /***************************************************************************
636  \PIPE\ntlsa commands
637  ***************************************************************************/
638 static struct api_struct api_lsa_cmds[] =
639 {
640         { "LSA_OPENPOLICY2"     , LSA_OPENPOLICY2     , api_lsa_open_policy2   },
641         { "LSA_OPENPOLICY"      , LSA_OPENPOLICY      , api_lsa_open_policy    },
642         { "LSA_QUERYINFOPOLICY" , LSA_QUERYINFOPOLICY , api_lsa_query_info     },
643         { "LSA_ENUMTRUSTDOM"    , LSA_ENUMTRUSTDOM    , api_lsa_enum_trust_dom },
644         { "LSA_CLOSE"           , LSA_CLOSE           , api_lsa_close          },
645         { "LSA_OPENSECRET"      , LSA_OPENSECRET      , api_lsa_open_secret    },
646         { "LSA_LOOKUPSIDS"      , LSA_LOOKUPSIDS      , api_lsa_lookup_sids    },
647         { "LSA_LOOKUPNAMES"     , LSA_LOOKUPNAMES     , api_lsa_lookup_names   },
648         { NULL                  , 0                   , NULL                   }
649 };
650
651 /***************************************************************************
652  api_ntLsarpcTNP
653  ***************************************************************************/
654 BOOL api_ntlsa_rpc(pipes_struct *p, prs_struct *data)
655 {
656         return api_rpcTNP(p, "api_ntlsa_rpc", api_lsa_cmds, data);
657 }
658
659 #undef OLD_NTDOMAIN