Merge of lsa_lookup_names() i18n bugfix from appliance.
[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,
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                 /* unistr routines take dos codepage strings */
242
243                 unix_to_dos(dom_name, True);
244                 unix_to_dos(name, True);
245
246                 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
247
248                 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
249                           "referenced list.\n", dom_name, name ));
250
251                 (*mapped_count)++;
252
253                 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
254                                         sid_name_use, name, dom_idx);
255                 total++;
256         }
257
258         trn->num_entries = total;
259         trn->ptr_trans_names = 1;
260         trn->num_entries2 = total;
261 }
262
263 /***************************************************************************
264  Init_reply_lookup_sids.
265  ***************************************************************************/
266
267 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
268                 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
269                 uint32 mapped_count)
270 {
271         r_l->ptr_dom_ref  = 1;
272         r_l->dom_ref      = ref;
273         r_l->names        = names;
274         r_l->mapped_count = mapped_count;
275
276         if (mapped_count == 0)
277                 r_l->status = NT_STATUS_NONE_MAPPED;
278         else
279                 r_l->status = NT_STATUS_NO_PROBLEMO;
280 }
281
282 /***************************************************************************
283  _lsa_open_policy2.
284  ***************************************************************************/
285
286 uint32 _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
287 {
288         /* lkclXXXX having decoded it, ignore all fields in the open policy! */
289
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;
293
294         return NT_STATUS_NOPROBLEMO;
295 }
296
297 /***************************************************************************
298  _lsa_open_policy
299  ***************************************************************************/
300
301 uint32 _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
302 {
303         /* lkclXXXX having decoded it, ignore all fields in the open policy! */
304
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;
308
309         return NT_STATUS_NOPROBLEMO;
310 }
311
312 /***************************************************************************
313  _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
314  ***************************************************************************/
315
316 uint32 _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
317 {
318         uint32 enum_context = 0;
319         char *dom_name = NULL;
320         DOM_SID *dom_sid = NULL;
321
322         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
323                 return NT_STATUS_INVALID_HANDLE;
324
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);
328
329         return r_u->status;
330 }
331
332 /***************************************************************************
333  _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
334  ***************************************************************************/
335
336 uint32 _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
337 {
338         LSA_INFO_UNION *info = &r_u->dom;
339         DOM_SID domain_sid;
340         fstring dos_domain;
341         char *name = NULL;
342         DOM_SID *sid = NULL;
343
344         r_u->status = NT_STATUS_NO_PROBLEMO;
345
346         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
347                 return NT_STATUS_INVALID_HANDLE;
348
349         fstrcpy(dos_domain, global_myworkgroup);
350         unix_to_dos(dos_domain, True);
351
352         switch (q_u->info_class) {
353         case 0x02:
354                 {
355                         unsigned int i;
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)
361                                 return False;
362             for (i = 0; i < 7; i++)
363                 info->id2.auditsettings[i] = 3;
364             break;
365                 }
366         case 0x03:
367                 /* Request PolicyPrimaryDomainInformation. */
368                 switch (lp_server_role()) {
369                         case ROLE_DOMAIN_PDC:
370                         case ROLE_DOMAIN_BDC:
371                                 name = dos_domain;
372                                 sid = &global_sam_sid;
373                                 break;
374                         case ROLE_DOMAIN_MEMBER:
375                                 name = dos_domain;
376                                 /* We need to return the Domain SID here. */
377                                 if (secrets_fetch_domain_sid(dos_domain,
378                                                              &domain_sid))
379                                         sid = &domain_sid;
380                                 else
381                                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
382                                 break;
383                         case ROLE_STANDALONE:
384                                 name = dos_domain;
385                                 sid = NULL; /* Tell it we're not in a domain. */
386                                 break;
387                         default:
388                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
389                 }
390                 init_dom_query(&r_u->dom.id3, name, sid);
391                 break;
392         case 0x05:
393                 /* Request PolicyAccountDomainInformation. */
394                 switch (lp_server_role()) {
395                         case ROLE_DOMAIN_PDC:
396                         case ROLE_DOMAIN_BDC:
397                                 name = dos_domain;
398                                 sid = &global_sam_sid;
399                                 break;
400                         case ROLE_DOMAIN_MEMBER:
401                                 name = dos_domain;
402                                 sid = &global_sam_sid;
403                                 break;
404                         case ROLE_STANDALONE:
405                                 name = dos_domain;
406                                 sid = &global_sam_sid;
407                                 break;
408                         default:
409                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
410                 }
411                 init_dom_query(&r_u->dom.id5, name, sid);
412                 break;
413         case 0x06:
414                 switch (lp_server_role()) {
415                         case ROLE_DOMAIN_BDC:
416                                 /*
417                                  * only a BDC is a backup controller
418                                  * of the domain, it controls.
419                                  */
420                                 info->id6.server_role = 2;
421                                 break;
422                         default:
423                                 /*
424                                  * any other role is a primary
425                                  * of the domain, it controls.
426                                  */
427                                 info->id6.server_role = 3;
428                                 break; 
429                 }
430                 break;
431         default:
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;
434                 break;
435         }
436
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;
440         }
441
442         return r_u->status;
443 }
444
445 /***************************************************************************
446  _lsa_lookup_sids
447  ***************************************************************************/
448
449 uint32 _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
450 {
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;
456
457         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
458                 return NT_STATUS_INVALID_HANDLE;
459
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));
462
463         if (!ref || !names)
464                 return NT_STATUS_NO_MEMORY;
465
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);
469
470         return r_u->status;
471 }
472
473 /***************************************************************************
474 lsa_reply_lookup_names
475  ***************************************************************************/
476
477 uint32 _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
478 {
479         UNISTR2 *names = q_u->uni_name;
480         int num_entries = q_u->num_entries;
481         DOM_R_REF *ref;
482         DOM_RID2 *rids;
483         uint32 mapped_count = 0;
484
485         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
486                 return NT_STATUS_INVALID_HANDLE;
487
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);
490
491         if (!ref || !rids)
492                 return NT_STATUS_NO_MEMORY;
493
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);
497
498         return r_u->status;
499 }
500
501 /***************************************************************************
502  _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
503  ***************************************************************************/
504
505 uint32 _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
506 {
507         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
508                 return NT_STATUS_INVALID_HANDLE;
509
510         close_policy_hnd(p, &q_u->pol);
511         return NT_STATUS_NO_PROBLEMO;
512 }
513
514 /***************************************************************************
515   "No more secrets Marty...." :-).
516  ***************************************************************************/
517
518 uint32 _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
519 {
520         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
521 }