last part of W2K support.
[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         int domlen = (dom_name != NULL) ? strlen(dom_name)+1 : 0;
92
93         d_q->uni_dom_max_len = domlen * 2;
94         d_q->uni_dom_str_len = domlen * 2;
95
96         d_q->buffer_dom_name = (dom_name != 0)   ? 1 : 0;
97         d_q->buffer_dom_sid  = (dom_sid != NULL) ? 1 : 0;
98
99         /* this string is supposed to be character short */
100         init_unistr2(&d_q->uni_domain_name, dom_name, domlen);
101         if (dom_sid != NULL)
102                 init_dom_sid2(&d_q->dom_sid, dom_sid);
103 }
104
105 /***************************************************************************
106  lsa_reply_enum_trust_dom
107  ***************************************************************************/
108
109 static void lsa_reply_enum_trust_dom(LSA_Q_ENUM_TRUST_DOM *q_e,
110                                 prs_struct *rdata,
111                                 uint32 enum_context, char *dom_name, DOM_SID *dom_sid)
112 {
113         LSA_R_ENUM_TRUST_DOM r_e;
114
115         ZERO_STRUCT(r_e);
116
117         /* set up the LSA QUERY INFO response */
118         init_r_enum_trust_dom(&r_e, enum_context, dom_name, dom_sid,
119               dom_name != NULL ? 0x0 : 0x80000000 | NT_STATUS_UNABLE_TO_FREE_VM);
120
121         /* store the response in the SMB stream */
122         lsa_io_r_enum_trust_dom("", &r_e, rdata, 0);
123 }
124
125 /***************************************************************************
126 lsa_reply_query_info
127  ***************************************************************************/
128
129 static BOOL lsa_reply_query_info(LSA_Q_QUERY_INFO *q_q, prs_struct *rdata,
130                                 char *dom_name, DOM_SID *dom_sid, uint32 status_code)
131 {
132         LSA_R_QUERY_INFO r_q;
133
134         ZERO_STRUCT(r_q);
135
136         /* set up the LSA QUERY INFO response */
137
138         if(status_code == 0) {
139                 r_q.undoc_buffer = 0x22000000; /* bizarre */
140                 r_q.info_class = q_q->info_class;
141
142                 init_dom_query(&r_q.dom.id5, dom_name, dom_sid);
143         }
144
145         r_q.status = status_code;
146
147         /* store the response in the SMB stream */
148         if(!lsa_io_r_query("", &r_q, rdata, 0)) {
149                 DEBUG(0,("lsa_reply_query_info: failed to marshall LSA_R_QUERY_INFO.\n"));
150                 return False;
151         }
152
153         return True;
154 }
155
156 /***************************************************************************
157  init_dom_ref - adds a domain if it's not already in, returns the index.
158 ***************************************************************************/
159
160 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
161 {
162         int num = 0;
163         int len;
164
165         if (dom_name != NULL) {
166                 for (num = 0; num < ref->num_ref_doms_1; num++) {
167                         fstring domname;
168                         fstrcpy(domname, dos_unistr2_to_str(&ref->ref_dom[num].uni_dom_name));
169                         if (strequal(domname, dom_name))
170                                 return num;
171                 }
172         } else {
173                 num = ref->num_ref_doms_1;
174         }
175
176         if (num >= MAX_REF_DOMAINS) {
177                 /* index not found, already at maximum domain limit */
178                 return -1;
179         }
180
181         ref->num_ref_doms_1 = num+1;
182         ref->ptr_ref_dom  = 1;
183         ref->max_entries = MAX_REF_DOMAINS;
184         ref->num_ref_doms_2 = num+1;
185
186         len = (dom_name != NULL) ? strlen(dom_name)+1 : 0;
187         if(dom_name != NULL && len == 0)
188                 len = 1;
189
190         init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, len);
191         ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
192
193         init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, len);
194         init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
195
196         return num;
197 }
198
199 /***************************************************************************
200  init_lsa_rid2s
201  ***************************************************************************/
202
203 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
204                                 int num_entries, UNISTR2 name[MAX_LOOKUP_SIDS],
205                                 uint32 *mapped_count)
206 {
207         int i;
208         int total = 0;
209         *mapped_count = 0;
210
211         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
212
213         for (i = 0; i < num_entries; i++) {
214                 BOOL status = False;
215                 DOM_SID dom_sid;
216                 DOM_SID sid;
217                 uint32 rid = 0xffffffff;
218                 int dom_idx = -1;
219                 pstring full_name;
220                 fstring dom_name;
221                 fstring user;
222                 enum SID_NAME_USE sid_name_use = SID_NAME_UNKNOWN;
223
224                 pstrcpy(full_name, dos_unistr2_to_str(&name[i]));
225
226                 /*
227                  * Try and split the name into a DOMAIN and
228                  * user component.
229                  */
230
231                 split_domain_name(full_name, dom_name, user);
232
233                 /*
234                  * We only do anything with this name if we
235                  * can map the Domain into a SID we know.
236                  */
237
238                 if (map_domain_name_to_sid(&dom_sid, dom_name)) {
239                         dom_idx = init_dom_ref(ref, dom_name, &dom_sid);
240
241                         if (local_lookup_name(dom_name, user, &sid, &sid_name_use) && sid_split_rid(&sid, &rid)) 
242                                 status = True;
243                 }
244
245                 if (status)
246                         (*mapped_count)++;
247                 else {
248                         dom_idx = -1;
249                         rid = 0xffffffff;
250                         sid_name_use = SID_NAME_UNKNOWN;
251                 }
252
253                 init_dom_rid2(&rid2[total], rid, sid_name_use, dom_idx);
254                 total++;
255         }
256 }
257
258 /***************************************************************************
259  init_reply_lookup_names
260  ***************************************************************************/
261
262 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
263                 DOM_R_REF *ref, uint32 num_entries,
264                 DOM_RID2 *rid2, uint32 mapped_count)
265 {
266         r_l->ptr_dom_ref  = 1;
267         r_l->dom_ref      = ref;
268
269         r_l->num_entries  = num_entries;
270         r_l->ptr_entries  = 1;
271         r_l->num_entries2 = num_entries;
272         r_l->dom_rid      = rid2;
273
274         r_l->mapped_count = mapped_count;
275
276         if (mapped_count == 0)
277                 r_l->status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
278         else
279                 r_l->status = 0x0;
280 }
281
282 /***************************************************************************
283  Init lsa_trans_names.
284  ***************************************************************************/
285
286 static void init_lsa_trans_names(DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
287                                 int num_entries, DOM_SID2 sid[MAX_LOOKUP_SIDS], uint32 *mapped_count)
288 {
289         int i;
290         int total = 0;
291         *mapped_count = 0;
292
293         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
294
295         for (i = 0; i < num_entries; i++) {
296                 BOOL status = False;
297                 DOM_SID find_sid = sid[i].sid;
298                 uint32 rid = 0xffffffff;
299                 int dom_idx = -1;
300                 fstring name, dom_name;
301                 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
302
303                 /* Lookup sid from winbindd */
304
305                 memset(dom_name, '\0', sizeof(dom_name));
306                 memset(name, '\0', sizeof(name));
307
308                 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
309
310                 if (!status) {
311                         sid_name_use = SID_NAME_UNKNOWN;
312                 }
313
314                 /* Store domain sid in ref array */
315
316                 if (find_sid.num_auths == 5) {
317                         sid_split_rid(&find_sid, &rid);
318                 }
319
320                 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
321
322                 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to referenced list.\n", dom_name, name ));
323
324                 (*mapped_count)++;
325
326                 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
327                                         sid_name_use, name, dom_idx);
328                 total++;
329         }
330
331         trn->num_entries = total;
332         trn->ptr_trans_names = 1;
333         trn->num_entries2 = total;
334 }
335
336 /***************************************************************************
337  Init_reply_lookup_sids.
338  ***************************************************************************/
339
340 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
341                 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
342                 uint32 mapped_count)
343 {
344         r_l->ptr_dom_ref  = 1;
345         r_l->dom_ref      = ref;
346         r_l->names        = names;
347         r_l->mapped_count = mapped_count;
348
349         if (mapped_count == 0)
350                 r_l->status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
351         else
352                 r_l->status = 0x0;
353 }
354
355 /***************************************************************************
356 lsa_reply_lookup_sids
357  ***************************************************************************/
358
359 static BOOL lsa_reply_lookup_sids(prs_struct *rdata, DOM_SID2 *sid, int num_entries)
360 {
361         LSA_R_LOOKUP_SIDS r_l;
362         DOM_R_REF ref;
363         LSA_TRANS_NAME_ENUM names;
364         uint32 mapped_count = 0;
365
366         ZERO_STRUCT(r_l);
367         ZERO_STRUCT(ref);
368         ZERO_STRUCT(names);
369
370         /* set up the LSA Lookup SIDs response */
371         init_lsa_trans_names(&ref, &names, num_entries, sid, &mapped_count);
372         init_reply_lookup_sids(&r_l, &ref, &names, mapped_count);
373
374         /* store the response in the SMB stream */
375         if(!lsa_io_r_lookup_sids("", &r_l, rdata, 0)) {
376                 DEBUG(0,("lsa_reply_lookup_sids: Failed to marshall LSA_R_LOOKUP_SIDS.\n"));
377                 return False;
378         }
379
380         return True;
381 }
382
383 /***************************************************************************
384 lsa_reply_lookup_names
385  ***************************************************************************/
386
387 static BOOL lsa_reply_lookup_names(prs_struct *rdata,
388                 UNISTR2 names[MAX_LOOKUP_SIDS], int num_entries)
389 {
390         LSA_R_LOOKUP_NAMES r_l;
391         DOM_R_REF ref;
392         DOM_RID2 rids[MAX_LOOKUP_SIDS];
393         uint32 mapped_count = 0;
394
395         ZERO_STRUCT(r_l);
396         ZERO_STRUCT(ref);
397         ZERO_ARRAY(rids);
398
399         /* set up the LSA Lookup RIDs response */
400         init_lsa_rid2s(&ref, rids, num_entries, names, &mapped_count);
401         init_reply_lookup_names(&r_l, &ref, num_entries, rids, mapped_count);
402
403         /* store the response in the SMB stream */
404         if(!lsa_io_r_lookup_names("", &r_l, rdata, 0)) {
405                 DEBUG(0,("lsa_reply_lookup_names: Failed to marshall LSA_R_LOOKUP_NAMES.\n"));
406                 return False;
407         }
408
409         return True;
410 }
411
412 /***************************************************************************
413  api_lsa_open_policy2
414  ***************************************************************************/
415
416 static BOOL api_lsa_open_policy2(pipes_struct *p)
417 {
418         prs_struct *data = &p->in_data.data;
419         prs_struct *rdata = &p->out_data.rdata;
420
421         LSA_Q_OPEN_POL2 q_o;
422
423         ZERO_STRUCT(q_o);
424
425         /* grab the server, object attributes and desired access flag...*/
426         if(!lsa_io_q_open_pol2("", &q_o, data, 0)) {
427                 DEBUG(0,("api_lsa_open_policy2: unable to unmarshall LSA_Q_OPEN_POL2.\n"));
428                 return False;
429         }
430
431         /* lkclXXXX having decoded it, ignore all fields in the open policy! */
432
433         /* return a 20 byte policy handle */
434         if(!lsa_reply_open_policy2(rdata))
435                 return False;
436
437         return True;
438 }
439
440 /***************************************************************************
441 api_lsa_open_policy
442  ***************************************************************************/
443 static BOOL api_lsa_open_policy(pipes_struct *p)
444 {
445         prs_struct *data = &p->in_data.data;
446         prs_struct *rdata = &p->out_data.rdata;
447
448         LSA_Q_OPEN_POL q_o;
449
450         ZERO_STRUCT(q_o);
451
452         /* grab the server, object attributes and desired access flag...*/
453         if(!lsa_io_q_open_pol("", &q_o, data, 0)) {
454                 DEBUG(0,("api_lsa_open_policy: unable to unmarshall LSA_Q_OPEN_POL.\n"));
455                 return False;
456         }
457
458         /* lkclXXXX having decoded it, ignore all fields in the open policy! */
459
460         /* return a 20 byte policy handle */
461         if(!lsa_reply_open_policy(rdata))
462                 return False;
463
464         return True;
465 }
466
467 /***************************************************************************
468 api_lsa_enum_trust_dom
469  ***************************************************************************/
470 static BOOL api_lsa_enum_trust_dom(pipes_struct *p)
471 {
472         LSA_Q_ENUM_TRUST_DOM q_e;
473         prs_struct *data = &p->in_data.data;
474         prs_struct *rdata = &p->out_data.rdata;
475
476         ZERO_STRUCT(q_e);
477
478         /* grab the enum trust domain context etc. */
479         if(!lsa_io_q_enum_trust_dom("", &q_e, data, 0))
480                 return False;
481
482         /* construct reply.  return status is always 0x0 */
483         lsa_reply_enum_trust_dom(&q_e, rdata, 0, NULL, NULL);
484
485         return True;
486 }
487
488 /***************************************************************************
489 api_lsa_query_info
490  ***************************************************************************/
491 static BOOL api_lsa_query_info(pipes_struct *p)
492 {
493         LSA_Q_QUERY_INFO q_i;
494         DOM_SID domain_sid;
495         char *name = NULL;
496         DOM_SID *sid = NULL;
497         uint32 status_code = 0;
498         prs_struct *data = &p->in_data.data;
499         prs_struct *rdata = &p->out_data.rdata;
500
501         ZERO_STRUCT(q_i);
502
503         /* grab the info class and policy handle */
504         if(!lsa_io_q_query("", &q_i, data, 0)) {
505                 DEBUG(0,("api_lsa_query_info: failed to unmarshall LSA_Q_QUERY_INFO.\n"));
506                 return False;
507         }
508
509         switch (q_i.info_class) {
510         case 0x03:
511                 switch (lp_server_role())
512                 {
513                         case ROLE_DOMAIN_PDC:
514                         case ROLE_DOMAIN_BDC:
515                                 name = global_myworkgroup;
516                                 sid = &global_sam_sid;
517                                 break;
518                         case ROLE_DOMAIN_MEMBER:
519                                 if (secrets_fetch_domain_sid(global_myworkgroup,
520                                         &domain_sid))
521                                 {
522                                         name = global_myworkgroup;
523                                         sid = &domain_sid;
524                                 }
525                         default:
526                                 break;
527                 }
528                 break;
529         case 0x05:
530                 name = global_myname;
531                 sid = &global_sam_sid;
532                 break;
533         default:
534                 DEBUG(0,("api_lsa_query_info: unknown info level in Lsa Query: %d\n", q_i.info_class));
535                 status_code = (NT_STATUS_INVALID_INFO_CLASS | 0xC0000000);
536                 break;
537         }
538
539         /* construct reply.  return status is always 0x0 */
540         if(!lsa_reply_query_info(&q_i, rdata, name, sid, status_code))
541                 return False;
542
543         return True;
544 }
545
546 /***************************************************************************
547  api_lsa_lookup_sids
548  ***************************************************************************/
549
550 static BOOL api_lsa_lookup_sids(pipes_struct *p)
551 {
552         LSA_Q_LOOKUP_SIDS q_l;
553         prs_struct *data = &p->in_data.data;
554         prs_struct *rdata = &p->out_data.rdata;
555
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(pipes_struct *p)
576 {
577         LSA_Q_LOOKUP_NAMES q_l;
578         prs_struct *data = &p->in_data.data;
579         prs_struct *rdata = &p->out_data.rdata;
580
581         ZERO_STRUCT(q_l);
582
583         /* grab the info class and policy handle */
584         if(!lsa_io_q_lookup_names("", &q_l, data, 0)) {
585                 DEBUG(0,("api_lsa_lookup_names: failed to unmarshall LSA_Q_LOOKUP_NAMES.\n"));
586                 return False;
587         }
588
589         SMB_ASSERT_ARRAY(q_l.uni_name, q_l.num_entries);
590
591         return lsa_reply_lookup_names(rdata, q_l.uni_name, q_l.num_entries);
592 }
593
594 /***************************************************************************
595  api_lsa_close
596  ***************************************************************************/
597 static BOOL api_lsa_close(pipes_struct *p)
598 {
599         LSA_R_CLOSE r_c;
600         prs_struct *rdata = &p->out_data.rdata;
601
602         ZERO_STRUCT(r_c);
603
604         /* store the response in the SMB stream */
605         if (!lsa_io_r_close("", &r_c, rdata, 0)) {
606                 DEBUG(0,("api_lsa_close: lsa_io_r_close failed.\n"));
607                 return False;
608         }
609
610         return True;
611 }
612
613 /***************************************************************************
614  api_lsa_open_secret
615  ***************************************************************************/
616 static BOOL api_lsa_open_secret(pipes_struct *p)
617 {
618         /* XXXX this is NOT good */
619         size_t i;
620         uint32 dummy = 0;
621         prs_struct *rdata = &p->out_data.rdata;
622
623         for(i =0; i < 4; i++) {
624                 if(!prs_uint32("api_lsa_close", rdata, 1, &dummy)) {
625                         DEBUG(0,("api_lsa_open_secret: prs_uint32 %d failed.\n",
626                                 (int)i ));
627                         return False;
628                 }
629         }
630
631         dummy = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
632         if(!prs_uint32("api_lsa_close", rdata, 1, &dummy)) {
633                 DEBUG(0,("api_lsa_open_secret: prs_uint32 status failed.\n"));
634                 return False;
635         }
636
637         return True;
638 }
639
640 /***************************************************************************
641  \PIPE\ntlsa commands
642  ***************************************************************************/
643 static struct api_struct api_lsa_cmds[] =
644 {
645         { "LSA_OPENPOLICY2"     , LSA_OPENPOLICY2     , api_lsa_open_policy2   },
646         { "LSA_OPENPOLICY"      , LSA_OPENPOLICY      , api_lsa_open_policy    },
647         { "LSA_QUERYINFOPOLICY" , LSA_QUERYINFOPOLICY , api_lsa_query_info     },
648         { "LSA_ENUMTRUSTDOM"    , LSA_ENUMTRUSTDOM    , api_lsa_enum_trust_dom },
649         { "LSA_CLOSE"           , LSA_CLOSE           , api_lsa_close          },
650         { "LSA_OPENSECRET"      , LSA_OPENSECRET      , api_lsa_open_secret    },
651         { "LSA_LOOKUPSIDS"      , LSA_LOOKUPSIDS      , api_lsa_lookup_sids    },
652         { "LSA_LOOKUPNAMES"     , LSA_LOOKUPNAMES     , api_lsa_lookup_names   },
653         { NULL                  , 0                   , NULL                   }
654 };
655
656 /***************************************************************************
657  api_ntLsarpcTNP
658  ***************************************************************************/
659 BOOL api_ntlsa_rpc(pipes_struct *p)
660 {
661         return api_rpcTNP(p, "api_ntlsa_rpc", api_lsa_cmds);
662 }
663
664 #undef OLD_NTDOMAIN