2 * Unix SMB/Netbios implementation.
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.
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.
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.
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.
25 /* This is the implementation of the lsa server code. */
29 extern int DEBUGLEVEL;
30 extern DOM_SID global_sam_sid;
31 extern fstring global_myworkgroup;
32 extern pstring global_myname;
34 /***************************************************************************
36 ***************************************************************************/
38 static void init_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
40 int domlen = (dom_name != NULL) ? strlen(dom_name) : 0;
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.
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 */
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++;
60 init_dom_sid2(&d_q->dom_sid, dom_sid);
63 /***************************************************************************
64 init_dom_ref - adds a domain if it's not already in, returns the index.
65 ***************************************************************************/
67 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
72 if (dom_name != NULL) {
73 for (num = 0; num < ref->num_ref_doms_1; num++) {
75 fstrcpy(domname, dos_unistr2_to_str(&ref->ref_dom[num].uni_dom_name));
76 if (strequal(domname, dom_name))
80 num = ref->num_ref_doms_1;
83 if (num >= MAX_REF_DOMAINS) {
84 /* index not found, already at maximum domain limit */
88 ref->num_ref_doms_1 = num+1;
90 ref->max_entries = MAX_REF_DOMAINS;
91 ref->num_ref_doms_2 = num+1;
93 len = (dom_name != NULL) ? strlen(dom_name) : 0;
94 if(dom_name != NULL && len == 0)
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;
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 );
106 /***************************************************************************
108 ***************************************************************************/
110 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
111 int num_entries, UNISTR2 *name,
112 uint32 *mapped_count, BOOL endian)
118 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
120 for (i = 0; i < num_entries; i++) {
123 uint32 rid = 0xffffffff;
126 fstring dom_name, user;
127 enum SID_NAME_USE name_type = SID_NAME_UNKNOWN;
129 /* Split name into domain and user component */
131 pstrcpy(full_name, dos_unistr2_to_str(&name[i]));
132 split_domain_name(full_name, dom_name, user);
136 DEBUG(5, ("init_lsa_rid2s: looking up name %s\n", full_name));
138 status = lookup_name(full_name, &sid, &name_type);
140 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" :
144 sid_split_rid(&sid, &rid);
145 dom_idx = init_dom_ref(ref, dom_name, &sid);
150 name_type = SID_NAME_UNKNOWN;
153 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
158 /***************************************************************************
159 init_reply_lookup_names
160 ***************************************************************************/
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)
166 r_l->ptr_dom_ref = 1;
169 r_l->num_entries = num_entries;
170 r_l->ptr_entries = 1;
171 r_l->num_entries2 = num_entries;
174 r_l->mapped_count = mapped_count;
176 if (mapped_count == 0)
177 r_l->status = NT_STATUS_NONE_MAPPED;
179 r_l->status = NT_STATUS_NO_PROBLEMO;
182 /***************************************************************************
183 Init lsa_trans_names.
184 ***************************************************************************/
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)
194 /* Allocate memory for list of names */
196 if (num_entries > 0) {
197 if (!(trn->name = (LSA_TRANS_NAME *)talloc(ctx, sizeof(LSA_TRANS_NAME) *
199 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
203 if (!(trn->uni_name = (UNISTR2 *)talloc(ctx, sizeof(UNISTR2) *
205 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
210 for (i = 0; i < num_entries; i++) {
212 DOM_SID find_sid = sid[i].sid;
213 uint32 rid = 0xffffffff;
215 fstring name, dom_name;
216 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
218 sid_to_string(name, &find_sid);
219 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
221 /* Lookup sid from winbindd */
223 memset(dom_name, '\0', sizeof(dom_name));
224 memset(name, '\0', sizeof(name));
226 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
228 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" :
232 sid_name_use = SID_NAME_UNKNOWN;
235 /* Store domain sid in ref array */
237 if (find_sid.num_auths == 5) {
238 sid_split_rid(&find_sid, &rid);
241 /* unistr routines take dos codepage strings */
243 unix_to_dos(dom_name, True);
244 unix_to_dos(name, True);
246 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
248 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
249 "referenced list.\n", dom_name, name ));
253 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
254 sid_name_use, name, dom_idx);
258 trn->num_entries = total;
259 trn->ptr_trans_names = 1;
260 trn->num_entries2 = total;
263 /***************************************************************************
264 Init_reply_lookup_sids.
265 ***************************************************************************/
267 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
268 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
271 r_l->ptr_dom_ref = 1;
274 r_l->mapped_count = mapped_count;
276 if (mapped_count == 0)
277 r_l->status = NT_STATUS_NONE_MAPPED;
279 r_l->status = NT_STATUS_NO_PROBLEMO;
282 /***************************************************************************
284 ***************************************************************************/
286 uint32 _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
288 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
290 /* set up the LSA QUERY INFO response */
291 if (!create_policy_hnd(p, &r_u->pol, NULL, NULL))
292 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
294 return NT_STATUS_NOPROBLEMO;
297 /***************************************************************************
299 ***************************************************************************/
301 uint32 _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
303 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
305 /* set up the LSA QUERY INFO response */
306 if (!create_policy_hnd(p, &r_u->pol, NULL, NULL))
307 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
309 return NT_STATUS_NOPROBLEMO;
312 /***************************************************************************
313 _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
314 ***************************************************************************/
316 uint32 _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
318 uint32 enum_context = 0;
319 char *dom_name = NULL;
320 DOM_SID *dom_sid = NULL;
322 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
323 return NT_STATUS_INVALID_HANDLE;
325 /* set up the LSA QUERY INFO response */
326 init_r_enum_trust_dom(r_u, enum_context, dom_name, dom_sid,
327 dom_name != NULL ? NT_STATUS_NO_PROBLEMO : NT_STATUS_UNABLE_TO_FREE_VM);
332 /***************************************************************************
333 _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
334 ***************************************************************************/
336 uint32 _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
338 LSA_INFO_UNION *info = &r_u->dom;
344 r_u->status = NT_STATUS_NO_PROBLEMO;
346 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
347 return NT_STATUS_INVALID_HANDLE;
349 fstrcpy(dos_domain, global_myworkgroup);
350 unix_to_dos(dos_domain, True);
352 switch (q_u->info_class) {
356 /* fake info: We audit everything. ;) */
357 info->id2.auditing_enabled = 1;
358 info->id2.count1 = 7;
359 info->id2.count2 = 7;
360 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
362 for (i = 0; i < 7; i++)
363 info->id2.auditsettings[i] = 3;
367 /* Request PolicyPrimaryDomainInformation. */
368 switch (lp_server_role()) {
369 case ROLE_DOMAIN_PDC:
370 case ROLE_DOMAIN_BDC:
372 sid = &global_sam_sid;
374 case ROLE_DOMAIN_MEMBER:
376 /* We need to return the Domain SID here. */
377 if (secrets_fetch_domain_sid(dos_domain,
381 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
383 case ROLE_STANDALONE:
385 sid = NULL; /* Tell it we're not in a domain. */
388 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
390 init_dom_query(&r_u->dom.id3, name, sid);
393 /* Request PolicyAccountDomainInformation. */
394 switch (lp_server_role()) {
395 case ROLE_DOMAIN_PDC:
396 case ROLE_DOMAIN_BDC:
398 sid = &global_sam_sid;
400 case ROLE_DOMAIN_MEMBER:
402 sid = &global_sam_sid;
404 case ROLE_STANDALONE:
406 sid = &global_sam_sid;
409 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
411 init_dom_query(&r_u->dom.id5, name, sid);
414 switch (lp_server_role()) {
415 case ROLE_DOMAIN_BDC:
417 * only a BDC is a backup controller
418 * of the domain, it controls.
420 info->id6.server_role = 2;
424 * any other role is a primary
425 * of the domain, it controls.
427 info->id6.server_role = 3;
432 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
433 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
437 if(r_u->status == NT_STATUS_NO_PROBLEMO) {
438 r_u->undoc_buffer = 0x22000000; /* bizarre */
439 r_u->info_class = q_u->info_class;
445 /***************************************************************************
447 ***************************************************************************/
449 uint32 _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
451 DOM_SID2 *sid = q_u->sids.sid;
452 int num_entries = q_u->sids.num_entries;
453 DOM_R_REF *ref = NULL;
454 LSA_TRANS_NAME_ENUM *names = NULL;
455 uint32 mapped_count = 0;
457 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
458 return NT_STATUS_INVALID_HANDLE;
460 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
461 names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
464 return NT_STATUS_NO_MEMORY;
466 /* set up the LSA Lookup SIDs response */
467 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
468 init_reply_lookup_sids(r_u, ref, names, mapped_count);
473 /***************************************************************************
474 lsa_reply_lookup_names
475 ***************************************************************************/
477 uint32 _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
479 UNISTR2 *names = q_u->uni_name;
480 int num_entries = q_u->num_entries;
483 uint32 mapped_count = 0;
485 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
486 return NT_STATUS_INVALID_HANDLE;
488 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
489 rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*MAX_LOOKUP_SIDS);
492 return NT_STATUS_NO_MEMORY;
494 /* set up the LSA Lookup RIDs response */
495 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
496 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
501 /***************************************************************************
502 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
503 ***************************************************************************/
505 uint32 _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
507 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
508 return NT_STATUS_INVALID_HANDLE;
510 close_policy_hnd(p, &q_u->pol);
511 return NT_STATUS_NO_PROBLEMO;
514 /***************************************************************************
515 "No more secrets Marty...." :-).
516 ***************************************************************************/
518 uint32 _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
520 return NT_STATUS_OBJECT_NAME_NOT_FOUND;