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[MAX_LOOKUP_SIDS],
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 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
243 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
244 "referenced list.\n", dom_name, name ));
248 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
249 sid_name_use, name, dom_idx);
253 trn->num_entries = total;
254 trn->ptr_trans_names = 1;
255 trn->num_entries2 = total;
258 /***************************************************************************
259 Init_reply_lookup_sids.
260 ***************************************************************************/
262 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
263 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
266 r_l->ptr_dom_ref = 1;
269 r_l->mapped_count = mapped_count;
271 if (mapped_count == 0)
272 r_l->status = NT_STATUS_NONE_MAPPED;
274 r_l->status = NT_STATUS_NO_PROBLEMO;
277 /***************************************************************************
279 ***************************************************************************/
281 uint32 _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
283 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
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;
289 return NT_STATUS_NOPROBLEMO;
292 /***************************************************************************
294 ***************************************************************************/
296 uint32 _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
298 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
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;
304 return NT_STATUS_NOPROBLEMO;
307 /***************************************************************************
308 _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
309 ***************************************************************************/
311 uint32 _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
313 uint32 enum_context = 0;
314 char *dom_name = NULL;
315 DOM_SID *dom_sid = NULL;
317 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
318 return NT_STATUS_INVALID_HANDLE;
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);
327 /***************************************************************************
328 _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
329 ***************************************************************************/
331 uint32 _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
333 LSA_INFO_UNION *info = &r_u->dom;
338 r_u->status = NT_STATUS_NO_PROBLEMO;
340 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
341 return NT_STATUS_INVALID_HANDLE;
343 switch (q_u->info_class) {
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)
353 for (i = 0; i < 7; i++)
354 info->id2.auditsettings[i] = 3;
358 switch (lp_server_role())
360 case ROLE_DOMAIN_PDC:
361 case ROLE_DOMAIN_BDC:
362 name = global_myworkgroup;
363 sid = &global_sam_sid;
365 case ROLE_DOMAIN_MEMBER:
366 name = global_myname;
367 if (secrets_fetch_domain_sid(global_myworkgroup,
371 sid = &global_sam_sid;
373 case ROLE_STANDALONE:
374 name = global_myname;
375 sid = &global_sam_sid;
378 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
380 init_dom_query(&r_u->dom.id3, name, sid);
383 /* AS/U shows this needs to be the same as level 3. JRA. */
384 switch (lp_server_role())
386 case ROLE_DOMAIN_PDC:
387 case ROLE_DOMAIN_BDC:
388 name = global_myworkgroup;
389 sid = &global_sam_sid;
391 case ROLE_DOMAIN_MEMBER:
392 name = global_myname;
393 if (secrets_fetch_domain_sid(global_myworkgroup,
397 sid = &global_sam_sid;
399 case ROLE_STANDALONE:
400 name = global_myname;
401 sid = &global_sam_sid;
404 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
406 init_dom_query(&r_u->dom.id5, name, sid);
409 switch (lp_server_role())
411 case ROLE_DOMAIN_BDC:
413 * only a BDC is a backup controller
414 * of the domain, it controls.
416 info->id6.server_role = 2;
420 * any other role is a primary
421 * of the domain, it controls.
423 info->id6.server_role = 3;
428 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
429 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
433 if(r_u->status == NT_STATUS_NO_PROBLEMO) {
434 r_u->undoc_buffer = 0x22000000; /* bizarre */
435 r_u->info_class = q_u->info_class;
441 /***************************************************************************
443 ***************************************************************************/
445 uint32 _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
447 DOM_SID2 *sid = q_u->sids.sid;
448 int num_entries = q_u->sids.num_entries;
449 DOM_R_REF *ref = NULL;
450 LSA_TRANS_NAME_ENUM *names = NULL;
451 uint32 mapped_count = 0;
453 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
454 return NT_STATUS_INVALID_HANDLE;
456 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
457 names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
460 return NT_STATUS_NO_MEMORY;
462 /* set up the LSA Lookup SIDs response */
463 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
464 init_reply_lookup_sids(r_u, ref, names, mapped_count);
469 /***************************************************************************
470 lsa_reply_lookup_names
471 ***************************************************************************/
473 uint32 _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
475 UNISTR2 *names = q_u->uni_name;
476 int num_entries = q_u->num_entries;
479 uint32 mapped_count = 0;
481 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
482 return NT_STATUS_INVALID_HANDLE;
484 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
485 rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*MAX_LOOKUP_SIDS);
488 return NT_STATUS_NO_MEMORY;
490 /* set up the LSA Lookup RIDs response */
491 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
492 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
497 /***************************************************************************
498 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
499 ***************************************************************************/
501 uint32 _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
503 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
504 return NT_STATUS_INVALID_HANDLE;
506 close_policy_hnd(p, &q_u->pol);
507 return NT_STATUS_NO_PROBLEMO;
510 /***************************************************************************
511 "No more secrets Marty...." :-).
512 ***************************************************************************/
514 uint32 _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
516 return NT_STATUS_OBJECT_NAME_NOT_FOUND;