JF pointed out we were returning the wrong info for Domain member with info
[nivanova/samba-autobuild/.git] / source / rpc_server / srv_lsa_nt.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  *  Copyright (C) Jeremy Allison                    2001.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 /* This is the implementation of the lsa server code. */
26
27 #include "includes.h"
28
29 extern int DEBUGLEVEL;
30 extern DOM_SID global_sam_sid;
31 extern fstring global_myworkgroup;
32 extern pstring global_myname;
33
34 /***************************************************************************
35 Init dom_query
36  ***************************************************************************/
37
38 static void init_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
39 {
40         int domlen = (dom_name != NULL) ? strlen(dom_name) : 0;
41
42         /*
43          * I'm not sure why this really odd combination of length
44          * values works, but it does appear to. I need to look at
45          * this *much* more closely - but at the moment leave alone
46          * until it's understood. This allows a W2k client to join
47          * a domain with both odd and even length names... JRA.
48          */
49
50         d_q->uni_dom_str_len = domlen ? ((domlen + 1) * 2) : 0;
51         d_q->uni_dom_max_len = domlen * 2;
52         d_q->buffer_dom_name = domlen != 0 ? 1 : 0; /* domain buffer pointer */
53         d_q->buffer_dom_sid = dom_sid != NULL ? 1 : 0;  /* domain sid pointer */
54
55         /* this string is supposed to be character short */
56         init_unistr2(&d_q->uni_domain_name, dom_name, domlen);
57         d_q->uni_domain_name.uni_max_len++;
58
59         if (dom_sid != NULL)
60                 init_dom_sid2(&d_q->dom_sid, dom_sid);
61 }
62
63 /***************************************************************************
64  init_dom_ref - adds a domain if it's not already in, returns the index.
65 ***************************************************************************/
66
67 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
68 {
69         int num = 0;
70         int len;
71
72         if (dom_name != NULL) {
73                 for (num = 0; num < ref->num_ref_doms_1; num++) {
74                         fstring domname;
75                         fstrcpy(domname, dos_unistr2_to_str(&ref->ref_dom[num].uni_dom_name));
76                         if (strequal(domname, dom_name))
77                                 return num;
78                 }
79         } else {
80                 num = ref->num_ref_doms_1;
81         }
82
83         if (num >= MAX_REF_DOMAINS) {
84                 /* index not found, already at maximum domain limit */
85                 return -1;
86         }
87
88         ref->num_ref_doms_1 = num+1;
89         ref->ptr_ref_dom  = 1;
90         ref->max_entries = MAX_REF_DOMAINS;
91         ref->num_ref_doms_2 = num+1;
92
93         len = (dom_name != NULL) ? strlen(dom_name) : 0;
94         if(dom_name != NULL && len == 0)
95                 len = 1;
96
97         init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, len);
98         ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
99
100         init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, len);
101         init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
102
103         return num;
104 }
105
106 /***************************************************************************
107  init_lsa_rid2s
108  ***************************************************************************/
109
110 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
111                                 int num_entries, UNISTR2 name[MAX_LOOKUP_SIDS],
112                                 uint32 *mapped_count, BOOL endian)
113 {
114         int i;
115         int total = 0;
116         *mapped_count = 0;
117
118         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
119
120         for (i = 0; i < num_entries; i++) {
121                 BOOL status = False;
122                 DOM_SID sid;
123                 uint32 rid = 0xffffffff;
124                 int dom_idx = -1;
125                 pstring full_name;
126                 fstring dom_name, user;
127                 enum SID_NAME_USE name_type = SID_NAME_UNKNOWN;
128
129                 /* Split name into domain and user component */
130
131                 pstrcpy(full_name, dos_unistr2_to_str(&name[i]));
132                 split_domain_name(full_name, dom_name, user);
133
134                 /* Lookup name */
135
136                 DEBUG(5, ("init_lsa_rid2s: looking up name %s\n", full_name));
137
138                 status = lookup_name(full_name, &sid, &name_type);
139
140                 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" : 
141                           "not found"));
142
143                 if (status) {
144                         sid_split_rid(&sid, &rid);
145                         dom_idx = init_dom_ref(ref, dom_name, &sid);
146                         (*mapped_count)++;
147                 } else {
148                         dom_idx = -1;
149                         rid = 0xffffffff;
150                         name_type = SID_NAME_UNKNOWN;
151                 }
152
153                 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
154                 total++;
155         }
156 }
157
158 /***************************************************************************
159  init_reply_lookup_names
160  ***************************************************************************/
161
162 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
163                 DOM_R_REF *ref, uint32 num_entries,
164                 DOM_RID2 *rid2, uint32 mapped_count)
165 {
166         r_l->ptr_dom_ref  = 1;
167         r_l->dom_ref      = ref;
168
169         r_l->num_entries  = num_entries;
170         r_l->ptr_entries  = 1;
171         r_l->num_entries2 = num_entries;
172         r_l->dom_rid      = rid2;
173
174         r_l->mapped_count = mapped_count;
175
176         if (mapped_count == 0)
177                 r_l->status = NT_STATUS_NONE_MAPPED;
178         else
179                 r_l->status = NT_STATUS_NO_PROBLEMO;
180 }
181
182 /***************************************************************************
183  Init lsa_trans_names.
184  ***************************************************************************/
185
186 static void init_lsa_trans_names(TALLOC_CTX *ctx, DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
187                                  int num_entries, DOM_SID2 *sid,
188                                  uint32 *mapped_count)
189 {
190         int i;
191         int total = 0;
192         *mapped_count = 0;
193
194         /* Allocate memory for list of names */
195
196         if (num_entries > 0) {
197                 if (!(trn->name = (LSA_TRANS_NAME *)talloc(ctx, sizeof(LSA_TRANS_NAME) *
198                                                           num_entries))) {
199                         DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
200                         return;
201                 }
202
203                 if (!(trn->uni_name = (UNISTR2 *)talloc(ctx, sizeof(UNISTR2) * 
204                                                         num_entries))) {
205                         DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
206                         return;
207                 }
208         }
209
210         for (i = 0; i < num_entries; i++) {
211                 BOOL status = False;
212                 DOM_SID find_sid = sid[i].sid;
213                 uint32 rid = 0xffffffff;
214                 int dom_idx = -1;
215                 fstring name, dom_name;
216                 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
217
218                 sid_to_string(name, &find_sid);
219                 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
220
221                 /* Lookup sid from winbindd */
222
223                 memset(dom_name, '\0', sizeof(dom_name));
224                 memset(name, '\0', sizeof(name));
225
226                 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
227
228                 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" : 
229                           "not found"));
230
231                 if (!status) {
232                         sid_name_use = SID_NAME_UNKNOWN;
233                 }
234
235                 /* Store domain sid in ref array */
236
237                 if (find_sid.num_auths == 5) {
238                         sid_split_rid(&find_sid, &rid);
239                 }
240
241                 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
242
243                 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
244                           "referenced list.\n", dom_name, name ));
245
246                 (*mapped_count)++;
247
248                 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
249                                         sid_name_use, name, dom_idx);
250                 total++;
251         }
252
253         trn->num_entries = total;
254         trn->ptr_trans_names = 1;
255         trn->num_entries2 = total;
256 }
257
258 /***************************************************************************
259  Init_reply_lookup_sids.
260  ***************************************************************************/
261
262 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
263                 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
264                 uint32 mapped_count)
265 {
266         r_l->ptr_dom_ref  = 1;
267         r_l->dom_ref      = ref;
268         r_l->names        = names;
269         r_l->mapped_count = mapped_count;
270
271         if (mapped_count == 0)
272                 r_l->status = NT_STATUS_NONE_MAPPED;
273         else
274                 r_l->status = NT_STATUS_NO_PROBLEMO;
275 }
276
277 /***************************************************************************
278  _lsa_open_policy2.
279  ***************************************************************************/
280
281 uint32 _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
282 {
283         /* lkclXXXX having decoded it, ignore all fields in the open policy! */
284
285         /* set up the LSA QUERY INFO response */
286         if (!create_policy_hnd(p, &r_u->pol, NULL, NULL))
287                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
288
289         return NT_STATUS_NOPROBLEMO;
290 }
291
292 /***************************************************************************
293  _lsa_open_policy
294  ***************************************************************************/
295
296 uint32 _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
297 {
298         /* lkclXXXX having decoded it, ignore all fields in the open policy! */
299
300         /* set up the LSA QUERY INFO response */
301         if (!create_policy_hnd(p, &r_u->pol, NULL, NULL))
302                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
303
304         return NT_STATUS_NOPROBLEMO;
305 }
306
307 /***************************************************************************
308  _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
309  ***************************************************************************/
310
311 uint32 _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
312 {
313         uint32 enum_context = 0;
314         char *dom_name = NULL;
315         DOM_SID *dom_sid = NULL;
316
317         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
318                 return NT_STATUS_INVALID_HANDLE;
319
320         /* set up the LSA QUERY INFO response */
321         init_r_enum_trust_dom(r_u, enum_context, dom_name, dom_sid,
322               dom_name != NULL ? NT_STATUS_NO_PROBLEMO : NT_STATUS_UNABLE_TO_FREE_VM);
323
324         return r_u->status;
325 }
326
327 /***************************************************************************
328  _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
329  ***************************************************************************/
330
331 uint32 _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
332 {
333         LSA_INFO_UNION *info = &r_u->dom;
334         DOM_SID domain_sid;
335         char *name = NULL;
336         DOM_SID *sid = NULL;
337
338         r_u->status = NT_STATUS_NO_PROBLEMO;
339
340         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
341                 return NT_STATUS_INVALID_HANDLE;
342
343         switch (q_u->info_class) {
344         case 0x02:
345                 {
346                         unsigned int i;
347                         /* fake info: We audit everything. ;) */
348                         info->id2.auditing_enabled = 1;
349             info->id2.count1 = 7;
350             info->id2.count2 = 7;
351                         if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
352                                 return False;
353             for (i = 0; i < 7; i++)
354                 info->id2.auditsettings[i] = 3;
355             break;
356                 }
357         case 0x03:
358                 /* Request PolicyPrimaryDomainInformation. */
359                 switch (lp_server_role()) {
360                         case ROLE_DOMAIN_PDC:
361                         case ROLE_DOMAIN_BDC:
362                                 name = global_myworkgroup;
363                                 sid = &global_sam_sid;
364                                 break;
365                         case ROLE_DOMAIN_MEMBER:
366                                 name = global_myworkgroup;
367                                 /* We need to return the Domain SID here. */
368                                 if (secrets_fetch_domain_sid(global_myworkgroup,
369                                         &domain_sid))
370                                                 sid = &domain_sid;
371                                 else
372                                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
373                                 break;
374                         case ROLE_STANDALONE:
375                                 name = global_myname;
376                                 sid = NULL; /* Tell it we're not in a domain. */
377                                 break;
378                         default:
379                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
380                 }
381                 init_dom_query(&r_u->dom.id3, name, sid);
382                 break;
383         case 0x05:
384                 /* Request PolicyAccountDomainInformation. */
385                 switch (lp_server_role()) {
386                         case ROLE_DOMAIN_PDC:
387                         case ROLE_DOMAIN_BDC:
388                                 name = global_myworkgroup;
389                                 sid = &global_sam_sid;
390                                 break;
391                         case ROLE_DOMAIN_MEMBER:
392                         case ROLE_STANDALONE:
393                                 name = global_myname;
394                                 sid = &global_sam_sid;
395                                 break;
396                         default:
397                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
398                 }
399                 init_dom_query(&r_u->dom.id5, name, sid);
400                 break;
401         case 0x06:
402                 switch (lp_server_role()) {
403                         case ROLE_DOMAIN_BDC:
404                                 /*
405                                  * only a BDC is a backup controller
406                                  * of the domain, it controls.
407                                  */
408                                 info->id6.server_role = 2;
409                                 break;
410                         default:
411                                 /*
412                                  * any other role is a primary
413                                  * of the domain, it controls.
414                                  */
415                                 info->id6.server_role = 3;
416                                 break; 
417                 }
418                 break;
419         default:
420                 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
421                 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
422                 break;
423         }
424
425         if(r_u->status == NT_STATUS_NO_PROBLEMO) {
426                 r_u->undoc_buffer = 0x22000000; /* bizarre */
427                 r_u->info_class = q_u->info_class;
428         }
429
430         return r_u->status;
431 }
432
433 /***************************************************************************
434  _lsa_lookup_sids
435  ***************************************************************************/
436
437 uint32 _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
438 {
439         DOM_SID2 *sid = q_u->sids.sid;
440         int num_entries = q_u->sids.num_entries;
441         DOM_R_REF *ref = NULL;
442         LSA_TRANS_NAME_ENUM *names = NULL;
443         uint32 mapped_count = 0;
444
445         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
446                 return NT_STATUS_INVALID_HANDLE;
447
448         ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
449         names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
450
451         if (!ref || !names)
452                 return NT_STATUS_NO_MEMORY;
453
454         /* set up the LSA Lookup SIDs response */
455         init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
456         init_reply_lookup_sids(r_u, ref, names, mapped_count);
457
458         return r_u->status;
459 }
460
461 /***************************************************************************
462 lsa_reply_lookup_names
463  ***************************************************************************/
464
465 uint32 _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
466 {
467         UNISTR2 *names = q_u->uni_name;
468         int num_entries = q_u->num_entries;
469         DOM_R_REF *ref;
470         DOM_RID2 *rids;
471         uint32 mapped_count = 0;
472
473         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
474                 return NT_STATUS_INVALID_HANDLE;
475
476         ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
477         rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*MAX_LOOKUP_SIDS);
478
479         if (!ref || !rids)
480                 return NT_STATUS_NO_MEMORY;
481
482         /* set up the LSA Lookup RIDs response */
483         init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
484         init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
485
486         return r_u->status;
487 }
488
489 /***************************************************************************
490  _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
491  ***************************************************************************/
492
493 uint32 _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
494 {
495         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
496                 return NT_STATUS_INVALID_HANDLE;
497
498         close_policy_hnd(p, &q_u->pol);
499         return NT_STATUS_NO_PROBLEMO;
500 }
501
502 /***************************************************************************
503   "No more secrets Marty...." :-).
504  ***************************************************************************/
505
506 uint32 _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
507 {
508         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
509 }