- renamed some of the passdb.c functions: they have a prefix pdb_ on them
[amitay/samba.git] / source3 / rpc_server / srv_lsa.c
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
27 #include "includes.h"
28 #include "nterr.h"
29
30 extern int DEBUGLEVEL;
31 extern DOM_SID global_machine_sid;
32
33 /***************************************************************************
34 lsa_reply_open_policy
35  ***************************************************************************/
36 static void lsa_reply_open_policy(prs_struct *rdata)
37 {
38         int i;
39         LSA_R_OPEN_POL r_o;
40
41         /* set up the LSA QUERY INFO response */
42         bzero(&(r_o.pol.data), POL_HND_SIZE);
43         for (i = 4; i < POL_HND_SIZE; i++)
44         {
45                 r_o.pol.data[i] = i;
46         }
47         r_o.status = 0x0;
48
49         /* store the response in the SMB stream */
50         lsa_io_r_open_pol("", &r_o, rdata, 0);
51 }
52
53 /***************************************************************************
54 make_dom_query
55  ***************************************************************************/
56 static void make_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
57 {
58         int domlen = strlen(dom_name);
59
60         d_q->uni_dom_max_len = domlen * 2;
61         d_q->uni_dom_str_len = domlen * 2;
62
63         d_q->buffer_dom_name = 4; /* domain buffer pointer */
64         d_q->buffer_dom_sid  = 2; /* domain sid pointer */
65
66         /* this string is supposed to be character short */
67         make_unistr2(&(d_q->uni_domain_name), dom_name, domlen);
68
69         make_dom_sid2(&(d_q->dom_sid), dom_sid);
70 }
71
72 /***************************************************************************
73 lsa_reply_query_info
74  ***************************************************************************/
75 static void lsa_reply_enum_trust_dom(LSA_Q_ENUM_TRUST_DOM *q_e,
76                                 prs_struct *rdata,
77                                 uint32 enum_context, char *dom_name, DOM_SID *dom_sid)
78 {
79         LSA_R_ENUM_TRUST_DOM r_e;
80
81         /* set up the LSA QUERY INFO response */
82         make_r_enum_trust_dom(&r_e, enum_context, dom_name, dom_sid,
83               dom_name != NULL ? 0x0 : 0x80000000 | NT_STATUS_UNABLE_TO_FREE_VM);
84
85         /* store the response in the SMB stream */
86         lsa_io_r_enum_trust_dom("", &r_e, rdata, 0);
87 }
88
89 /***************************************************************************
90 lsa_reply_query_info
91  ***************************************************************************/
92 static void lsa_reply_query_info(LSA_Q_QUERY_INFO *q_q, prs_struct *rdata,
93                                 char *dom_name, DOM_SID *dom_sid)
94 {
95         LSA_R_QUERY_INFO r_q;
96
97         /* set up the LSA QUERY INFO response */
98
99         r_q.undoc_buffer = 0x22000000; /* bizarre */
100         r_q.info_class = q_q->info_class;
101
102         make_dom_query(&r_q.dom.id5, dom_name, dom_sid);
103
104         r_q.status = 0x0;
105
106         /* store the response in the SMB stream */
107         lsa_io_r_query("", &r_q, rdata, 0);
108 }
109
110 /***************************************************************************
111 make_dom_ref
112
113  pretty much hard-coded choice of "other" sids, unfortunately...
114
115  ***************************************************************************/
116 static void make_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid,
117                          DOM_SID *other_sid1, DOM_SID *other_sid2, DOM_SID *other_sid3)
118 {
119         int len_dom_name   = strlen(dom_name);
120
121         ref->undoc_buffer = 1;
122         ref->num_ref_doms_1 = 4;
123         ref->buffer_dom_name = 1;
124         ref->max_entries = 32;
125         ref->num_ref_doms_2 = 4;
126
127         make_uni_hdr2(&(ref->hdr_dom_name  ), len_dom_name  , len_dom_name  , 0);
128         make_uni_hdr2(&(ref->hdr_ref_dom[0]), sizeof(DOM_SID), sizeof(DOM_SID), 0);
129         make_uni_hdr2(&(ref->hdr_ref_dom[1]), sizeof(DOM_SID), sizeof(DOM_SID), 0);
130         make_uni_hdr2(&(ref->hdr_ref_dom[2]), sizeof(DOM_SID), sizeof(DOM_SID), 0);
131
132         if (dom_name != NULL)
133         {
134                 make_unistr(&(ref->uni_dom_name), dom_name);
135         }
136
137         make_dom_sid2(&(ref->ref_dom[0]), dom_sid   );
138         make_dom_sid2(&(ref->ref_dom[1]), other_sid1);
139         make_dom_sid2(&(ref->ref_dom[2]), other_sid2);
140         make_dom_sid2(&(ref->ref_dom[3]), other_sid3);
141 }
142
143 /***************************************************************************
144 make_reply_lookup_rids
145  ***************************************************************************/
146 static void make_reply_lookup_rids(LSA_R_LOOKUP_RIDS *r_l,
147                                 int num_entries, uint32 dom_rids[MAX_LOOKUP_SIDS],
148                                 char *dom_name, DOM_SID *dom_sid,
149                                 DOM_SID *other_sid1, DOM_SID *other_sid2, DOM_SID *other_sid3)
150 {
151         int i;
152
153         make_dom_ref(&(r_l->dom_ref), dom_name, dom_sid,
154                      other_sid1, other_sid2, other_sid3);
155
156         r_l->num_entries = num_entries;
157         r_l->undoc_buffer = 1;
158         r_l->num_entries2 = num_entries;
159
160         for (i = 0; i < num_entries; i++)
161         {
162                 make_dom_rid2(&(r_l->dom_rid[i]), dom_rids[i]);
163         }
164
165         r_l->num_entries3 = num_entries;
166 }
167
168 /***************************************************************************
169 make_lsa_trans_names
170  ***************************************************************************/
171 static void make_lsa_trans_names(LSA_TRANS_NAME_ENUM *trn,
172                                 int num_entries, DOM_SID2 sid[MAX_LOOKUP_SIDS],
173                                 uint32 *total)
174 {
175         uint32 status = 0x0;
176         int i;
177         (*total) = 0;
178
179         for (i = 0; i < num_entries; i++)
180         {
181                 uint32 rid = 0xffffffff;
182                 uint8 num_auths = sid[i].sid.num_auths;
183                 fstring name;
184                 uint32 type;
185                 
186                 trn->ptr_name[i] = 0;
187                 trn->ptr_name[(*total)] = 0;
188
189                 /* find the rid to look up */
190                 if (num_auths != 0)
191                 {
192                         rid = sid[i].sid.sub_auths[num_auths-1];
193
194                         status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
195
196                         status = (status != 0x0) ? lookup_user_name (rid, name, &type) : status;
197                         status = (status != 0x0) ? lookup_group_name(rid, name, &type) : status;
198                         status = (status != 0x0) ? lookup_alias_name(rid, name, &type) : status;
199                 }
200
201                 if (status == 0x0)
202                 {
203                         trn->ptr_name[i] = 1;
204                         make_lsa_trans_name(&(trn->name[(*total)]), type, name, (*total));
205                         (*total)++;
206                 }
207         }
208
209         trn->num_entries = (*total);
210         trn->ptr_trans_names = 1;
211         trn->num_entries2 = (*total);
212 }
213
214 /***************************************************************************
215 make_reply_lookup_sids
216  ***************************************************************************/
217 static void make_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
218                                 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
219                                 uint32 mapped_count, uint32 status)
220 {
221         r_l->dom_ref      = ref;
222         r_l->names        = names;
223         r_l->mapped_count = mapped_count;
224         r_l->status       = status;
225 }
226
227 /***************************************************************************
228 lsa_reply_lookup_sids
229  ***************************************************************************/
230 static void lsa_reply_lookup_sids(prs_struct *rdata,
231                                 int num_entries, DOM_SID2 sid[MAX_LOOKUP_SIDS],
232                                 char *dom_name, DOM_SID *dom_sid,
233                                 DOM_SID *other_sid1, DOM_SID *other_sid2, DOM_SID *other_sid3)
234 {
235         LSA_R_LOOKUP_SIDS r_l;
236         DOM_R_REF ref;
237         LSA_TRANS_NAME_ENUM names;
238         uint32 mapped_count = 0;
239
240         /* set up the LSA Lookup SIDs response */
241         make_dom_ref(&ref, dom_name, dom_sid, other_sid1, other_sid2, other_sid3);
242         make_lsa_trans_names(&names, num_entries, sid, &mapped_count);
243         make_reply_lookup_sids(&r_l, &ref, &names, mapped_count, 0x0);
244
245         /* store the response in the SMB stream */
246         lsa_io_r_lookup_sids("", &r_l, rdata, 0);
247 }
248
249 /***************************************************************************
250 lsa_reply_lookup_rids
251  ***************************************************************************/
252 static void lsa_reply_lookup_rids(prs_struct *rdata,
253                                 int num_entries, uint32 dom_rids[MAX_LOOKUP_SIDS],
254                                 char *dom_name, DOM_SID *dom_sid,
255                                 DOM_SID *other_sid1, DOM_SID *other_sid2, DOM_SID *other_sid3)
256 {
257         LSA_R_LOOKUP_RIDS r_l;
258
259         /* set up the LSA Lookup RIDs response */
260         make_reply_lookup_rids(&r_l, num_entries, dom_rids,
261                                 dom_name, dom_sid, other_sid1, other_sid2, other_sid3);
262         r_l.status = 0x0;
263
264         /* store the response in the SMB stream */
265         lsa_io_r_lookup_rids("", &r_l, rdata, 0);
266 }
267
268 /***************************************************************************
269 api_lsa_open_policy
270  ***************************************************************************/
271 static void api_lsa_open_policy( int uid, prs_struct *data,
272                              prs_struct *rdata )
273 {
274         LSA_Q_OPEN_POL q_o;
275
276         /* grab the server, object attributes and desired access flag...*/
277         lsa_io_q_open_pol("", &q_o, data, 0);
278
279         /* lkclXXXX having decoded it, ignore all fields in the open policy! */
280
281         /* return a 20 byte policy handle */
282         lsa_reply_open_policy(rdata);
283 }
284
285 /***************************************************************************
286 api_lsa_enum_trust_dom
287  ***************************************************************************/
288 static void api_lsa_enum_trust_dom( int uid, prs_struct *data,
289                                     prs_struct *rdata )
290 {
291         LSA_Q_ENUM_TRUST_DOM q_e;
292
293         /* grab the enum trust domain context etc. */
294         lsa_io_q_enum_trust_dom("", &q_e, data, 0);
295
296         /* construct reply.  return status is always 0x0 */
297         lsa_reply_enum_trust_dom(&q_e, rdata,
298                                               0, NULL, NULL);
299 }
300
301 /***************************************************************************
302 api_lsa_query_info
303  ***************************************************************************/
304 static void api_lsa_query_info( int uid, prs_struct *data,
305                                 prs_struct *rdata )
306 {
307         LSA_Q_QUERY_INFO q_i;
308         pstring dom_name;
309
310         /* grab the info class and policy handle */
311         lsa_io_q_query("", &q_i, data, 0);
312
313         pstrcpy(dom_name, lp_workgroup());
314
315         /* construct reply.  return status is always 0x0 */
316         lsa_reply_query_info(&q_i, rdata, dom_name, &global_machine_sid);
317 }
318
319 /***************************************************************************
320 api_lsa_lookup_sids
321  ***************************************************************************/
322 static void api_lsa_lookup_sids( int uid, prs_struct *data,
323                                  prs_struct *rdata )
324 {
325         LSA_Q_LOOKUP_SIDS q_l;
326         pstring dom_name;
327         DOM_SID sid_S_1_1;
328         DOM_SID sid_S_1_3;
329         DOM_SID sid_S_1_5;
330
331         /* grab the info class and policy handle */
332         lsa_io_q_lookup_sids("", &q_l, data, 0);
333
334         pstrcpy(dom_name, lp_workgroup());
335
336         string_to_sid(&sid_S_1_1, "S-1-1");
337         string_to_sid(&sid_S_1_3, "S-1-3");
338         string_to_sid(&sid_S_1_5, "S-1-5");
339
340         /* construct reply.  return status is always 0x0 */
341         lsa_reply_lookup_sids(rdata,
342                               q_l.sids.num_entries, q_l.sids.sid, /* SIDs */
343                               dom_name, &global_machine_sid, /* domain name, domain SID */
344                               &sid_S_1_1, &sid_S_1_3, &sid_S_1_5); /* the three other SIDs */
345 }
346
347 /***************************************************************************
348 api_lsa_lookup_names
349  ***************************************************************************/
350 static void api_lsa_lookup_names( int uid, prs_struct *data,
351                                   prs_struct *rdata )
352 {
353         int i;
354         LSA_Q_LOOKUP_RIDS q_l;
355         pstring dom_name;
356         DOM_SID sid_S_1_1;
357         DOM_SID sid_S_1_3;
358         DOM_SID sid_S_1_5;
359         uint32 dom_rids[MAX_LOOKUP_SIDS];
360         uint32 dummy_g_rid;
361
362         /* grab the info class and policy handle */
363         lsa_io_q_lookup_rids("", &q_l, data, 0);
364
365         pstrcpy(dom_name, lp_workgroup());
366
367         string_to_sid(&sid_S_1_1, "S-1-1");
368         string_to_sid(&sid_S_1_3, "S-1-3");
369         string_to_sid(&sid_S_1_5, "S-1-5");
370
371         /* convert received RIDs to strings, so we can do them. */
372         for (i = 0; i < q_l.num_entries; i++)
373         {
374                 char *user_name = unistr2(q_l.lookup_name[i].str.buffer);
375                 if (!pdb_name_to_rid(user_name, &dom_rids[i], &dummy_g_rid))
376                 {
377                         /* WHOOPS!  we should really do something about this... */
378                         dom_rids[i] = 0;
379                 }
380         }
381
382         /* construct reply.  return status is always 0x0 */
383         lsa_reply_lookup_rids(rdata,
384                               q_l.num_entries, dom_rids, /* text-converted SIDs */
385                               dom_name, &global_machine_sid, /* domain name, domain SID */
386                               &sid_S_1_1, &sid_S_1_3, &sid_S_1_5); /* the three other SIDs */
387 }
388
389 /***************************************************************************
390  api_lsa_close
391  ***************************************************************************/
392 static void api_lsa_close( int uid, prs_struct *data,
393                                   prs_struct *rdata)
394 {
395         /* XXXX this is NOT good */
396         char *q = mem_data(&(rdata->data), rdata->offset);
397
398         SIVAL(q, 0, 0); q += 4;
399         SIVAL(q, 0, 0); q += 4;
400         SIVAL(q, 0, 0); q += 4;
401         SIVAL(q, 0, 0); q += 4;
402         SIVAL(q, 0, 0); q += 4;
403         SIVAL(q, 0, 0); q += 4;
404
405         rdata->offset += 24;
406 }
407
408 /***************************************************************************
409  api_lsa_open_secret
410  ***************************************************************************/
411 static void api_lsa_open_secret( int uid, prs_struct *data,
412                                   prs_struct *rdata)
413 {
414         /* XXXX this is NOT good */
415         char *q = mem_data(&(rdata->data), rdata->offset);
416
417         SIVAL(q, 0, 0); q += 4;
418         SIVAL(q, 0, 0); q += 4;
419         SIVAL(q, 0, 0); q += 4;
420         SIVAL(q, 0, 0); q += 4;
421         SIVAL(q, 0, 0); q += 4;
422         SIVAL(q, 0, 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND); q += 4;
423         
424         rdata->offset += 24;
425 }
426
427 /***************************************************************************
428  \PIPE\ntlsa commands
429  ***************************************************************************/
430 static struct api_struct api_lsa_cmds[] =
431 {
432         { "LSA_OPENPOLICY"      , LSA_OPENPOLICY      , api_lsa_open_policy    },
433         { "LSA_QUERYINFOPOLICY" , LSA_QUERYINFOPOLICY , api_lsa_query_info     },
434         { "LSA_ENUMTRUSTDOM"    , LSA_ENUMTRUSTDOM    , api_lsa_enum_trust_dom },
435         { "LSA_CLOSE"           , LSA_CLOSE           , api_lsa_close          },
436         { "LSA_OPENSECRET"      , LSA_OPENSECRET      , api_lsa_open_secret    },
437         { "LSA_LOOKUPSIDS"      , LSA_LOOKUPSIDS      , api_lsa_lookup_sids    },
438         { "LSA_LOOKUPNAMES"     , LSA_LOOKUPNAMES     , api_lsa_lookup_names   },
439         { NULL                  , 0                   , NULL                   }
440 };
441
442 /***************************************************************************
443  api_ntLsarpcTNP
444  ***************************************************************************/
445 BOOL api_ntlsa_rpc(pipes_struct *p, prs_struct *data)
446 {
447         return api_rpcTNP(p, "api_ntlsa_rpc", api_lsa_cmds, data);
448 }
449