95b0a413c746036812314f6266d9eb164cee2b80
[samba.git] / source / rpc_server / srv_samr.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  *  
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
26 #include "includes.h"
27 #include "nterr.h"
28
29 extern int DEBUGLEVEL;
30
31 extern BOOL sam_logon_in_ssb;
32 extern pstring samlogon_user;
33 extern DOM_SID global_machine_sid;
34
35 extern rid_name domain_group_rids[];
36 extern rid_name domain_alias_rids[];
37
38 /*******************************************************************
39   This next function should be replaced with something that
40   dynamically returns the correct user info..... JRA.
41  ********************************************************************/
42
43 static BOOL get_sampwd_entries(SAM_USER_INFO_21 *pw_buf,
44                                 int *total_entries, int *num_entries,
45                                 int max_num_entries,
46                                 uint16 acb_mask)
47 {
48         void *vp = NULL;
49         struct smb_passwd *pwd = NULL;
50
51         (*num_entries) = 0;
52         (*total_entries) = 0;
53
54         if (pw_buf == NULL) return False;
55
56         vp = startsmbpwent(False);
57         if (!vp)
58         {
59                 DEBUG(0, ("get_sampwd_entries: Unable to open SMB password database.\n"));
60                 return False;
61         }
62
63         while (((pwd = getsmbpwent(vp)) != NULL) && (*num_entries) < max_num_entries)
64         {
65                 int user_name_len = strlen(pwd->smb_name);
66                 make_unistr2(&(pw_buf[(*num_entries)].uni_user_name), pwd->smb_name, user_name_len-1);
67                 make_uni_hdr(&(pw_buf[(*num_entries)].hdr_user_name), user_name_len-1, 
68                                user_name_len-1, 1);
69                 pw_buf[(*num_entries)].user_rid = pwd->smb_userid;
70                 bzero( pw_buf[(*num_entries)].nt_pwd , 16);
71
72                 /* Now check if the NT compatible password is available. */
73                 if (pwd->smb_nt_passwd != NULL)
74                 {
75                         memcpy( pw_buf[(*num_entries)].nt_pwd , pwd->smb_nt_passwd, 16);
76                 }
77
78                 pw_buf[(*num_entries)].acb_info = (uint16)pwd->acct_ctrl;
79
80                 DEBUG(5, ("get_smbpwd_entries: idx: %d user %s, uid %d, acb %x",
81                 (*num_entries), pwd->smb_name, pwd->smb_userid, pwd->acct_ctrl));
82
83                 if (acb_mask == 0 || IS_BITS_SET_SOME(pwd->acct_ctrl, acb_mask))
84                 {
85                         DEBUG(5,(" acb_mask %x accepts\n", acb_mask));
86                         (*num_entries)++;
87                 }
88                 else
89                 {
90                         DEBUG(5,(" acb_mask %x rejects\n", acb_mask));
91                 }
92
93                 (*total_entries)++;
94         }
95
96         endsmbpwent(vp);
97
98         return (*num_entries) > 0;
99 }
100
101 /*******************************************************************
102  samr_reply_unknown_1
103  ********************************************************************/
104 static void samr_reply_close_hnd(SAMR_Q_CLOSE_HND *q_u,
105                                 prs_struct *rdata)
106 {
107         SAMR_R_CLOSE_HND r_u;
108
109         /* set up the SAMR unknown_1 response */
110         bzero(r_u.pol.data, POL_HND_SIZE);
111
112         /* close the policy handle */
113         if (close_lsa_policy_hnd(&(q_u->pol)))
114         {
115                 r_u.status = 0;
116         }
117         else
118         {
119                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_INVALID;
120         }
121
122         DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
123
124         /* store the response in the SMB stream */
125         samr_io_r_close_hnd("", &r_u, rdata, 0);
126
127         DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
128
129 }
130
131 /*******************************************************************
132  api_samr_close_hnd
133  ********************************************************************/
134 static void api_samr_close_hnd( int uid, prs_struct *data, prs_struct *rdata)
135 {
136         SAMR_Q_CLOSE_HND q_u;
137
138         /* grab the samr unknown 1 */
139         samr_io_q_close_hnd("", &q_u, data, 0);
140
141         /* construct reply.  always indicate success */
142         samr_reply_close_hnd(&q_u, rdata);
143 }
144
145
146 /*******************************************************************
147  samr_reply_open_domain
148  ********************************************************************/
149 static void samr_reply_open_domain(SAMR_Q_OPEN_DOMAIN *q_u,
150                                 prs_struct *rdata)
151 {
152         SAMR_R_OPEN_DOMAIN r_u;
153         BOOL pol_open = False;
154         int pol_idx;
155
156         r_u.status = 0x0;
157
158         /* find the connection policy handle. */
159         if (r_u.status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->connect_pol))) == -1))
160         {
161                 r_u.status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
162         }
163
164         /* get a (unique) handle.  open a policy on it. */
165         if (r_u.status == 0x0 && !(pol_open = open_lsa_policy_hnd(&(r_u.domain_pol))))
166         {
167                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
168         }
169
170         /* associate the domain SID with the (unique) handle. */
171         if (r_u.status == 0x0 && !set_lsa_policy_samr_sid(&(r_u.domain_pol), &(q_u->dom_sid.sid)))
172         {
173                 /* oh, whoops.  don't know what error message to return, here */
174                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
175         }
176
177         if (r_u.status != 0 && pol_open)
178         {
179                 close_lsa_policy_hnd(&(r_u.domain_pol));
180         }
181
182         DEBUG(5,("samr_open_domain: %d\n", __LINE__));
183
184         /* store the response in the SMB stream */
185         samr_io_r_open_domain("", &r_u, rdata, 0);
186
187         DEBUG(5,("samr_open_domain: %d\n", __LINE__));
188
189 }
190
191 /*******************************************************************
192  api_samr_open_domain
193  ********************************************************************/
194 static void api_samr_open_domain( int uid, prs_struct *data, prs_struct *rdata)
195 {
196         SAMR_Q_OPEN_DOMAIN q_u;
197
198         /* grab the samr open */
199         samr_io_q_open_domain("", &q_u, data, 0);
200
201         /* construct reply.  always indicate success */
202         samr_reply_open_domain(&q_u, rdata);
203 }
204
205
206 /*******************************************************************
207  samr_reply_unknown_3
208  ********************************************************************/
209 static void samr_reply_unknown_3(SAMR_Q_UNKNOWN_3 *q_u,
210                                 prs_struct *rdata)
211 {
212         SAMR_R_UNKNOWN_3 r_u;
213         DOM_SID3 sid[MAX_SAM_SIDS];
214         int pol_idx;
215         uint32 rid;
216         uint32 status;
217
218         status = 0x0;
219
220         /* find the policy handle.  open a policy on it. */
221         if (status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->user_pol))) == -1))
222         {
223                 status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
224         }
225
226         /* find the user's rid */
227         if (status == 0x0 && (rid = get_lsa_policy_samr_rid(&(q_u->user_pol))) == 0xffffffff)
228         {
229                 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
230         }
231
232         if (status == 0x0)
233         {
234         DOM_SID user_sid;
235         DOM_SID other_sid;
236
237         user_sid = global_machine_sid;
238
239         SMB_ASSERT_ARRAY(user_sid.sub_auths, user_sid.num_auths+1);
240
241         /*
242          * Add the user RID.
243          */
244         user_sid.sub_auths[user_sid.num_auths++] = rid;
245         
246                 string_to_sid(&other_sid, "S-1-1");
247
248                 /* maybe need another 1 or 2 (S-1-5-20-0x220 and S-1-5-20-0x224) */
249                 /* these two are DOMAIN_ADMIN and DOMAIN_ACCT_OP group RIDs */
250                 make_dom_sid3(&(sid[0]), 0x035b, 0x0002, &other_sid);
251                 make_dom_sid3(&(sid[1]), 0x0044, 0x0002, &user_sid);
252         }
253
254         make_samr_r_unknown_3(&r_u,
255                                 0x0001, 0x8004,
256                                 0x00000014, 0x0002, 0x0070,
257                                 2, sid, status);
258
259         DEBUG(5,("samr_unknown_3: %d\n", __LINE__));
260
261         /* store the response in the SMB stream */
262         samr_io_r_unknown_3("", &r_u, rdata, 0);
263
264         DEBUG(5,("samr_unknown_3: %d\n", __LINE__));
265
266 }
267
268 /*******************************************************************
269  api_samr_unknown_3
270  ********************************************************************/
271 static void api_samr_unknown_3( int uid, prs_struct *data, prs_struct *rdata)
272 {
273         SAMR_Q_UNKNOWN_3 q_u;
274
275         /* grab the samr open */
276         samr_io_q_unknown_3("", &q_u, data, 0);
277
278         /* construct reply.  always indicate success */
279         samr_reply_unknown_3(&q_u, rdata);
280 }
281
282
283 /*******************************************************************
284  samr_reply_enum_dom_users
285  ********************************************************************/
286 static void samr_reply_enum_dom_users(SAMR_Q_ENUM_DOM_USERS *q_u,
287                                 prs_struct *rdata)
288 {
289         SAMR_R_ENUM_DOM_USERS r_e;
290         SAM_USER_INFO_21 pass[MAX_SAM_ENTRIES];
291         int num_entries;
292         int total_entries;
293         int pol_idx;
294         BOOL got_pwds;
295
296         r_e.status = 0x0;
297         r_e.total_num_entries = 0;
298
299         /* find the policy handle.  open a policy on it. */
300         if (r_e.status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1))
301         {
302                 r_e.status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
303         }
304
305         DEBUG(5,("samr_reply_enum_dom_users: %d\n", __LINE__));
306
307         become_root(True);
308         got_pwds = get_sampwd_entries(pass, &total_entries, &num_entries, MAX_SAM_ENTRIES, q_u->acb_mask);
309         unbecome_root(True);
310
311         make_samr_r_enum_dom_users(&r_e, total_entries,
312                                    q_u->unknown_0, num_entries,
313                                    pass, r_e.status);
314
315         /* store the response in the SMB stream */
316         samr_io_r_enum_dom_users("", &r_e, rdata, 0);
317
318         DEBUG(5,("samr_enum_dom_users: %d\n", __LINE__));
319
320 }
321
322 /*******************************************************************
323  api_samr_enum_dom_users
324  ********************************************************************/
325 static void api_samr_enum_dom_users( int uid, prs_struct *data, prs_struct *rdata)
326 {
327         SAMR_Q_ENUM_DOM_USERS q_e;
328
329         /* grab the samr open */
330         samr_io_q_enum_dom_users("", &q_e, data, 0);
331
332         /* construct reply. */
333         samr_reply_enum_dom_users(&q_e, rdata);
334 }
335
336
337 /*******************************************************************
338  samr_reply_enum_dom_groups
339  ********************************************************************/
340 static void samr_reply_enum_dom_groups(SAMR_Q_ENUM_DOM_GROUPS *q_u,
341                                 prs_struct *rdata)
342 {
343         SAMR_R_ENUM_DOM_GROUPS r_e;
344         SAM_USER_INFO_21 pass[MAX_SAM_ENTRIES];
345         int num_entries;
346         int pol_idx;
347         BOOL got_grps;
348         char *dummy_group = "Domain Admins";
349
350         r_e.status = 0x0;
351         r_e.num_entries = 0;
352
353         /* find the policy handle.  open a policy on it. */
354         if (r_e.status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1))
355         {
356                 r_e.status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
357         }
358
359         DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
360
361         got_grps = True;
362         num_entries = 1;
363         make_unistr2(&(pass[0].uni_user_name), dummy_group, strlen(dummy_group));
364         pass[0].user_rid = DOMAIN_GROUP_RID_ADMINS;
365
366         if (r_e.status == 0 && got_grps)
367         {
368                 make_samr_r_enum_dom_groups(&r_e, q_u->start_idx, num_entries, pass, r_e.status);
369         }
370
371         /* store the response in the SMB stream */
372         samr_io_r_enum_dom_groups("", &r_e, rdata, 0);
373
374         DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
375
376 }
377
378 /*******************************************************************
379  api_samr_enum_dom_groups
380  ********************************************************************/
381 static void api_samr_enum_dom_groups( int uid, prs_struct *data, prs_struct *rdata)
382 {
383         SAMR_Q_ENUM_DOM_GROUPS q_e;
384
385         /* grab the samr open */
386         samr_io_q_enum_dom_groups("", &q_e, data, 0);
387
388         /* construct reply. */
389         samr_reply_enum_dom_groups(&q_e, rdata);
390 }
391
392
393 /*******************************************************************
394  samr_reply_enum_dom_aliases
395  ********************************************************************/
396 static void samr_reply_enum_dom_aliases(SAMR_Q_ENUM_DOM_ALIASES *q_u,
397                                 prs_struct *rdata)
398 {
399         SAMR_R_ENUM_DOM_ALIASES r_e;
400         SAM_USER_INFO_21 pass[MAX_SAM_ENTRIES];
401         int num_entries;
402         int pol_idx;
403         BOOL got_aliases;
404         char *dummy_alias = "admins";
405
406         r_e.status = 0x0;
407         r_e.num_entries = 0;
408
409         /* find the policy handle.  open a policy on it. */
410         if (r_e.status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1))
411         {
412                 r_e.status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
413         }
414
415         DEBUG(5,("samr_reply_enum_dom_aliases: %d\n", __LINE__));
416
417         got_aliases = True;
418         num_entries = 1;
419         make_unistr2(&(pass[0].uni_user_name), dummy_alias, strlen(dummy_alias));
420         pass[0].user_rid = BUILTIN_ALIAS_RID_ADMINS;
421
422         if (r_e.status == 0 && got_aliases)
423         {
424                 make_samr_r_enum_dom_aliases(&r_e, num_entries, pass, r_e.status);
425         }
426
427         /* store the response in the SMB stream */
428         samr_io_r_enum_dom_aliases("", &r_e, rdata, 0);
429
430         DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
431
432 }
433
434 /*******************************************************************
435  api_samr_enum_dom_aliases
436  ********************************************************************/
437 static void api_samr_enum_dom_aliases( int uid, prs_struct *data, prs_struct *rdata)
438 {
439         SAMR_Q_ENUM_DOM_ALIASES q_e;
440
441         /* grab the samr open */
442         samr_io_q_enum_dom_aliases("", &q_e, data, 0);
443
444         /* construct reply. */
445         samr_reply_enum_dom_aliases(&q_e, rdata);
446 }
447
448
449 /*******************************************************************
450  samr_reply_query_dispinfo
451  ********************************************************************/
452 static void samr_reply_query_dispinfo(SAMR_Q_QUERY_DISPINFO *q_u,
453                                 prs_struct *rdata)
454 {
455         SAMR_R_QUERY_DISPINFO r_e;
456         SAM_INFO_CTR ctr;
457         SAM_INFO_1 info1;
458         SAM_INFO_2 info2;
459         SAM_USER_INFO_21 pass[MAX_SAM_ENTRIES];
460         int num_entries;
461         int total_entries;
462         int pol_idx;
463         BOOL got_pwds;
464         uint16 switch_level = 0x0;
465
466         r_e.status = 0x0;
467
468         /* find the policy handle.  open a policy on it. */
469         if (r_e.status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1))
470         {
471                 r_e.status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
472         }
473
474         DEBUG(5,("samr_reply_query_dispinfo: %d\n", __LINE__));
475
476         become_root(True);
477         got_pwds = get_sampwd_entries(pass, &total_entries, &num_entries, MAX_SAM_ENTRIES, 0);
478         unbecome_root(True);
479
480         switch (q_u->switch_level)
481         {
482                 case 0x1:
483                 {
484                 
485                         /* query disp info is for users */
486                         switch_level = 0x1;
487                         make_sam_info_1(&info1, ACB_NORMAL,
488                                 q_u->start_idx, num_entries, pass);
489
490                         ctr.sam.info1 = &info1;
491
492                         break;
493                 }
494                 case 0x2:
495                 {
496                         /* query disp info is for servers */
497                         switch_level = 0x2;
498                         make_sam_info_2(&info2, ACB_WSTRUST,
499                                 q_u->start_idx, num_entries, pass);
500
501                         ctr.sam.info2 = &info2;
502
503                         break;
504                 }
505         }
506
507         if (r_e.status == 0 && got_pwds)
508         {
509                 make_samr_r_query_dispinfo(&r_e, switch_level, &ctr, r_e.status);
510         }
511
512         /* store the response in the SMB stream */
513         samr_io_r_query_dispinfo("", &r_e, rdata, 0);
514
515         DEBUG(5,("samr_query_dispinfo: %d\n", __LINE__));
516
517 }
518
519 /*******************************************************************
520  api_samr_query_dispinfo
521  ********************************************************************/
522 static void api_samr_query_dispinfo( int uid, prs_struct *data, prs_struct *rdata)
523 {
524         SAMR_Q_QUERY_DISPINFO q_e;
525
526         /* grab the samr open */
527         samr_io_q_query_dispinfo("", &q_e, data, 0);
528
529         /* construct reply. */
530         samr_reply_query_dispinfo(&q_e, rdata);
531 }
532
533
534 /*******************************************************************
535  samr_reply_query_aliasinfo
536  ********************************************************************/
537 static void samr_reply_query_aliasinfo(SAMR_Q_QUERY_ALIASINFO *q_u,
538                                 prs_struct *rdata)
539 {
540         SAMR_R_QUERY_ALIASINFO r_e;
541         int pol_idx;
542         BOOL got_alias;
543
544         r_e.status = 0x0;
545         r_e.ptr = 0;
546
547         /* find the policy handle.  open a policy on it. */
548         if (r_e.status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1))
549         {
550                 r_e.status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
551         }
552
553         DEBUG(5,("samr_reply_query_aliasinfo: %d\n", __LINE__));
554
555         if (r_e.status == 0x0)
556         {
557                 if (q_u->switch_level != 3)
558                 {
559                         r_e.status = NT_STATUS_INVALID_INFO_CLASS;
560                 }
561         }
562
563         if (r_e.status == 0x0)
564         {
565                 got_alias = True;
566         }
567
568         make_samr_r_query_aliasinfo(&r_e, q_u->switch_level,
569                             "<account description>",
570                                 r_e.status);
571
572         /* store the response in the SMB stream */
573         samr_io_r_query_aliasinfo("", &r_e, rdata, 0);
574
575         DEBUG(5,("samr_query_aliasinfo: %d\n", __LINE__));
576
577 }
578
579 /*******************************************************************
580  api_samr_query_aliasinfo
581  ********************************************************************/
582 static void api_samr_query_aliasinfo( int uid, prs_struct *data, prs_struct *rdata)
583 {
584         SAMR_Q_QUERY_ALIASINFO q_e;
585
586         /* grab the samr open */
587         samr_io_q_query_aliasinfo("", &q_e, data, 0);
588
589         /* construct reply. */
590         samr_reply_query_aliasinfo(&q_e, rdata);
591 }
592
593
594 /*******************************************************************
595  samr_reply_lookup_ids
596  ********************************************************************/
597 static void samr_reply_lookup_ids(SAMR_Q_LOOKUP_IDS *q_u,
598                                 prs_struct *rdata)
599 {
600         uint32 rid[MAX_SAM_ENTRIES];
601         uint32 status     = 0;
602         int num_rids = q_u->num_sids1;
603
604         SAMR_R_LOOKUP_IDS r_u;
605
606         DEBUG(5,("samr_lookup_ids: %d\n", __LINE__));
607
608         if (num_rids > MAX_SAM_ENTRIES)
609         {
610                 num_rids = MAX_SAM_ENTRIES;
611                 DEBUG(5,("samr_lookup_ids: truncating entries to %d\n", num_rids));
612         }
613
614 #if 0
615         int i;
616         SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
617
618         for (i = 0; i < num_rids && status == 0; i++)
619         {
620                 struct smb_passwd *smb_pass;
621                 fstring user_name;
622
623
624                 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
625                                                                         q_u->uni_user_name[i].uni_str_len));
626
627                 /* find the user account */
628                 become_root(True);
629                 smb_pass = get_sampwd_entry(user_name, 0);
630                 unbecome_root(True);
631
632                 if (smb_pass == NULL)
633                 {
634                         status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
635                         rid[i] = 0;
636                 }
637                 else
638                 {
639                         /* lkclXXXX SHOULD use name_to_rid() here! */
640                         rid[i] = smb_pass->smb_userid;
641                 }
642         }
643 #endif
644
645         num_rids = 1;
646         rid[0] = BUILTIN_ALIAS_RID_USERS;
647
648         make_samr_r_lookup_ids(&r_u, num_rids, rid, status);
649
650         /* store the response in the SMB stream */
651         samr_io_r_lookup_ids("", &r_u, rdata, 0);
652
653         DEBUG(5,("samr_lookup_ids: %d\n", __LINE__));
654
655 }
656
657 /*******************************************************************
658  api_samr_lookup_ids
659  ********************************************************************/
660 static void api_samr_lookup_ids( int uid, prs_struct *data, prs_struct *rdata)
661 {
662         SAMR_Q_LOOKUP_IDS q_u;
663
664         /* grab the samr 0x10 */
665         samr_io_q_lookup_ids("", &q_u, data, 0);
666
667         /* construct reply.  always indicate success */
668         samr_reply_lookup_ids(&q_u, rdata);
669 }
670
671 /*******************************************************************
672  samr_reply_lookup_names
673  ********************************************************************/
674 static void samr_reply_lookup_names(SAMR_Q_LOOKUP_NAMES *q_u,
675                                 prs_struct *rdata)
676 {
677         uint32 rid[MAX_SAM_ENTRIES];
678         uint32 status     = 0;
679         int i;
680         int num_rids = q_u->num_rids1;
681
682         SAMR_R_LOOKUP_NAMES r_u;
683
684         DEBUG(5,("samr_lookup_names: %d\n", __LINE__));
685
686         if (num_rids > MAX_SAM_ENTRIES)
687         {
688                 num_rids = MAX_SAM_ENTRIES;
689                 DEBUG(5,("samr_lookup_names: truncating entries to %d\n", num_rids));
690         }
691
692         SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
693
694         for (i = 0; i < num_rids && status == 0; i++)
695         {
696                 fstring name;
697
698                 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
699
700                 fstrcpy(name, unistrn2(q_u->uni_user_name[i].buffer, q_u->uni_user_name[i].uni_str_len));
701
702                 status = (status != 0x0) ? lookup_user_rid (name, &(rid[i])) : status;
703                 status = (status != 0x0) ? lookup_group_rid(name, &(rid[i])) : status;
704                 status = (status != 0x0) ? lookup_alias_rid(name, &(rid[i])) : status;
705         }
706
707         make_samr_r_lookup_names(&r_u, num_rids, rid, status);
708
709         /* store the response in the SMB stream */
710         samr_io_r_lookup_names("", &r_u, rdata, 0);
711
712         DEBUG(5,("samr_lookup_names: %d\n", __LINE__));
713
714 }
715
716 /*******************************************************************
717  api_samr_lookup_names
718  ********************************************************************/
719 static void api_samr_lookup_names( int uid, prs_struct *data, prs_struct *rdata)
720 {
721         SAMR_Q_LOOKUP_NAMES q_u;
722
723         /* grab the samr lookup names */
724         samr_io_q_lookup_names("", &q_u, data, 0);
725
726         /* construct reply.  always indicate success */
727         samr_reply_lookup_names(&q_u, rdata);
728 }
729
730
731 /*******************************************************************
732  samr_reply_unknown_12
733  ********************************************************************/
734 static void samr_reply_unknown_12(SAMR_Q_UNKNOWN_12 *q_u,
735                                 prs_struct *rdata)
736 {
737         fstring group_names[MAX_SAM_ENTRIES];
738         uint32  group_attrs[MAX_SAM_ENTRIES];
739         uint32 status     = 0;
740         int num_gids = q_u->num_gids1;
741         uint32 pol_idx;
742
743         SAMR_R_UNKNOWN_12 r_u;
744
745         DEBUG(5,("samr_unknown_12: %d\n", __LINE__));
746
747         /* find the policy handle.  open a policy on it. */
748         if (status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1))
749         {
750                 status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
751         }
752
753         if (status == 0x0)
754         {
755                 int i;
756                 if (num_gids > MAX_SAM_ENTRIES)
757                 {
758                         num_gids = MAX_SAM_ENTRIES;
759                         DEBUG(5,("samr_unknown_12: truncating entries to %d\n", num_gids));
760                 }
761
762                 for (i = 0; i < num_gids && status == 0; i++)
763                 {
764                         fstrcpy(group_names[i], "dummy group");
765                         group_attrs[i] = 0x2;
766                 }
767         }
768
769         make_samr_r_unknown_12(&r_u, num_gids, group_names, group_attrs, status);
770
771         /* store the response in the SMB stream */
772         samr_io_r_unknown_12("", &r_u, rdata, 0);
773
774         DEBUG(5,("samr_unknown_12: %d\n", __LINE__));
775
776 }
777
778 /*******************************************************************
779  api_samr_unknown_12
780  ********************************************************************/
781 static void api_samr_unknown_12( int uid, prs_struct *data, prs_struct *rdata)
782 {
783         SAMR_Q_UNKNOWN_12 q_u;
784
785         /* grab the samr lookup names */
786         samr_io_q_unknown_12("", &q_u, data, 0);
787
788         /* construct reply.  always indicate success */
789         samr_reply_unknown_12(&q_u, rdata);
790 }
791
792
793 /*******************************************************************
794  samr_reply_open_user
795  ********************************************************************/
796 static void samr_reply_open_user(SAMR_Q_OPEN_USER *q_u,
797                                 prs_struct *rdata,
798                                 int status)
799 {
800         SAMR_R_OPEN_USER r_u;
801         struct smb_passwd *smb_pass;
802         int pol_idx;
803         BOOL pol_open = False;
804
805         /* set up the SAMR open_user response */
806         bzero(r_u.user_pol.data, POL_HND_SIZE);
807
808         r_u.status = 0x0;
809
810         /* find the policy handle.  open a policy on it. */
811         if (r_u.status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->domain_pol))) == -1))
812         {
813                 r_u.status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
814         }
815
816         /* get a (unique) handle.  open a policy on it. */
817         if (r_u.status == 0x0 && !(pol_open = open_lsa_policy_hnd(&(r_u.user_pol))))
818         {
819                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
820         }
821
822         become_root(True);
823         smb_pass = getsmbpwuid(q_u->user_rid);
824         unbecome_root(True);
825
826         /* check that the RID exists in our domain. */
827         if (r_u.status == 0x0 && smb_pass == NULL)
828         {
829                 r_u.status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
830         }
831
832         /* associate the RID with the (unique) handle. */
833         if (r_u.status == 0x0 && !set_lsa_policy_samr_rid(&(r_u.user_pol), q_u->user_rid))
834         {
835                 /* oh, whoops.  don't know what error message to return, here */
836                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
837         }
838
839         if (r_u.status != 0 && pol_open)
840         {
841                 close_lsa_policy_hnd(&(r_u.user_pol));
842         }
843
844         DEBUG(5,("samr_open_user: %d\n", __LINE__));
845
846         /* store the response in the SMB stream */
847         samr_io_r_open_user("", &r_u, rdata, 0);
848
849         DEBUG(5,("samr_open_user: %d\n", __LINE__));
850
851 }
852
853 /*******************************************************************
854  api_samr_open_user
855  ********************************************************************/
856 static void api_samr_open_user( int uid, prs_struct *data, prs_struct *rdata)
857 {
858         SAMR_Q_OPEN_USER q_u;
859
860         /* grab the samr unknown 22 */
861         samr_io_q_open_user("", &q_u, data, 0);
862
863         /* construct reply.  always indicate success */
864         samr_reply_open_user(&q_u, rdata, 0x0);
865 }
866
867
868 /*************************************************************************
869  get_user_info_21
870  *************************************************************************/
871 static BOOL get_user_info_21(SAM_USER_INFO_21 *id21, uint32 user_rid)
872 {
873         NTTIME dummy_time;
874         struct sam_passwd *sam_pass;
875         LOGON_HRS hrs;
876         int i;
877
878         if (!pdb_rid_is_user(user_rid))
879         {
880                 return False;
881         }
882
883         become_root(True);
884         sam_pass = getsam21pwrid(user_rid);
885         unbecome_root(True);
886
887         if (sam_pass == NULL)
888         {
889                 return False;
890         }
891
892         DEBUG(3,("User:[%s]\n", sam_pass->smb_name));
893
894         dummy_time.low  = 0xffffffff;
895         dummy_time.high = 0x7fffffff;
896
897         DEBUG(0,("get_user_info_21 - TODO: convert unix times to NTTIMEs\n"));
898
899         /* create a LOGON_HRS structure */
900         hrs.len = sam_pass->hours_len;
901         SMB_ASSERT_ARRAY(hrs.hours, hrs.len);
902         for (i = 0; i < hrs.len; i++)
903         {
904                 hrs.hours[i] = sam_pass->hours[i];
905         }
906
907         make_sam_user_info21(id21,
908
909                            &dummy_time, /* logon_time */
910                            &dummy_time, /* logoff_time */
911                            &dummy_time, /* kickoff_time */
912                            &dummy_time, /* pass_last_set_time */
913                            &dummy_time, /* pass_can_change_time */
914                            &dummy_time, /* pass_must_change_time */
915
916                            sam_pass->smb_name, /* user_name */
917                            sam_pass->full_name, /* full_name */
918                            sam_pass->home_dir, /* home_dir */
919                            sam_pass->dir_drive, /* dir_drive */
920                            sam_pass->logon_script, /* logon_script */
921                            sam_pass->profile_path, /* profile_path */
922                            sam_pass->acct_desc, /* description */
923                            sam_pass->workstations, /* workstations user can log in from */
924                            sam_pass->unknown_str, /* don't know, yet */
925                            sam_pass->munged_dial, /* dialin info.  contains dialin path and tel no */
926
927                            sam_pass->user_rid, /* RID user_id */
928                            sam_pass->group_rid, /* RID group_id */
929                        sam_pass->acct_ctrl,
930
931                    sam_pass->unknown_3, /* unknown_3 */
932                        sam_pass->logon_divs, /* divisions per week */
933                            &hrs, /* logon hours */
934                        sam_pass->unknown_5,
935                        sam_pass->unknown_6);
936
937         return True;
938 }
939
940 /*******************************************************************
941  samr_reply_query_userinfo
942  ********************************************************************/
943 static void samr_reply_query_userinfo(SAMR_Q_QUERY_USERINFO *q_u,
944                                 prs_struct *rdata)
945 {
946         SAMR_R_QUERY_USERINFO r_u;
947 #if 0
948         SAM_USER_INFO_11 id11;
949 #endif
950         SAM_USER_INFO_21 id21;
951         void *info = NULL;
952
953         uint32 status = 0x0;
954         uint32 rid;
955         int obj_idx;
956
957         DEBUG(5,("samr_reply_query_userinfo: %d\n", __LINE__));
958
959         /* search for the handle */
960         if (status == 0x0 && (obj_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1)
961         {
962                 status = NT_STATUS_INVALID_HANDLE;
963         }
964
965         /* find the user's rid */
966         if (status == 0x0 && (rid = get_lsa_policy_samr_rid(&(q_u->pol))) == 0xffffffff)
967         {
968                 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
969         }
970
971         /* ok!  user info levels (there are lots: see MSDEV help), off we go... */
972         if (status == 0x0)
973         {
974                 switch (q_u->switch_value)
975                 {
976 #if 0
977 /* whoops - got this wrong.  i think.  or don't understand what's happening. */
978                         case 0x11:
979                         {
980                                 NTTIME expire;
981                                 info = (void*)&id11;
982                                 
983                                 expire.low  = 0xffffffff;
984                                 expire.high = 0x7fffffff;
985
986                                 make_sam_user_info11(&id11, &expire, "BROOKFIELDS$", 0x03ef, 0x201, 0x0080);
987
988                                 break;
989                         }
990 #endif
991                         case 21:
992                         {
993                                 info = (void*)&id21;
994                                 status = get_user_info_21(&id21, rid) ? 0 : NT_STATUS_NO_SUCH_USER;
995                                 break;
996                         }
997
998                         default:
999                         {
1000                                 status = NT_STATUS_INVALID_INFO_CLASS;
1001
1002                                 break;
1003                         }
1004                 }
1005         }
1006
1007         make_samr_r_query_userinfo(&r_u, q_u->switch_value, info, status);
1008
1009         /* store the response in the SMB stream */
1010         samr_io_r_query_userinfo("", &r_u, rdata, 0);
1011
1012         DEBUG(5,("samr_reply_query_userinfo: %d\n", __LINE__));
1013
1014 }
1015
1016 /*******************************************************************
1017  api_samr_query_userinfo
1018  ********************************************************************/
1019 static void api_samr_query_userinfo( int uid, prs_struct *data, prs_struct *rdata)
1020 {
1021         SAMR_Q_QUERY_USERINFO q_u;
1022
1023         /* grab the samr unknown 24 */
1024         samr_io_q_query_userinfo("", &q_u, data, 0);
1025
1026         /* construct reply.  always indicate success */
1027         samr_reply_query_userinfo(&q_u, rdata);
1028 }
1029
1030
1031 /*******************************************************************
1032  samr_reply_query_usergroups
1033  ********************************************************************/
1034 static void samr_reply_query_usergroups(SAMR_Q_QUERY_USERGROUPS *q_u,
1035                                 prs_struct *rdata)
1036 {
1037         SAMR_R_QUERY_USERGROUPS r_u;
1038         uint32 status = 0x0;
1039
1040         struct smb_passwd *smb_pass;
1041         DOM_GID *gids = NULL;
1042         int num_groups = 0;
1043         int pol_idx;
1044         uint32 rid;
1045
1046         DEBUG(5,("samr_query_usergroups: %d\n", __LINE__));
1047
1048         /* find the policy handle.  open a policy on it. */
1049         if (status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1))
1050         {
1051                 status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
1052         }
1053
1054         /* find the user's rid */
1055         if (status == 0x0 && (rid = get_lsa_policy_samr_rid(&(q_u->pol))) == 0xffffffff)
1056         {
1057                 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1058         }
1059
1060         if (status == 0x0)
1061         {
1062                 become_root(True);
1063                 smb_pass = getsmbpwuid(rid);
1064                 unbecome_root(True);
1065
1066                 if (smb_pass == NULL)
1067                 {
1068                         status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1069                 }
1070         }
1071
1072         if (status == 0x0)
1073         {
1074                 pstring groups;
1075                 get_domain_user_groups(groups, smb_pass->smb_name);
1076                 gids = NULL;
1077                 num_groups = make_dom_gids(groups, &gids);
1078         }
1079
1080         /* construct the response.  lkclXXXX: gids are not copied! */
1081         make_samr_r_query_usergroups(&r_u, num_groups, gids, status);
1082
1083         /* store the response in the SMB stream */
1084         samr_io_r_query_usergroups("", &r_u, rdata, 0);
1085
1086         if(gids)
1087           free((char *)gids);
1088         DEBUG(5,("samr_query_usergroups: %d\n", __LINE__));
1089
1090 }
1091
1092 /*******************************************************************
1093  api_samr_query_usergroups
1094  ********************************************************************/
1095 static void api_samr_query_usergroups( int uid, prs_struct *data, prs_struct *rdata)
1096 {
1097         SAMR_Q_QUERY_USERGROUPS q_u;
1098         /* grab the samr unknown 32 */
1099         samr_io_q_query_usergroups("", &q_u, data, 0);
1100
1101         /* construct reply. */
1102         samr_reply_query_usergroups(&q_u, rdata);
1103 }
1104
1105
1106 /*******************************************************************
1107  samr_reply_unknown_32
1108  ********************************************************************/
1109 static void samr_reply_unknown_32(SAMR_Q_UNKNOWN_32 *q_u,
1110                                 prs_struct *rdata,
1111                                 int status)
1112 {
1113         int i;
1114         SAMR_R_UNKNOWN_32 r_u;
1115
1116         /* set up the SAMR unknown_32 response */
1117         bzero(r_u.pol.data, POL_HND_SIZE);
1118         if (status == 0)
1119         {
1120                 for (i = 4; i < POL_HND_SIZE; i++)
1121                 {
1122                         r_u.pol.data[i] = i+1;
1123                 }
1124         }
1125
1126         make_dom_rid4(&(r_u.rid4), 0x0030, 0, 0);
1127         r_u.status    = status;
1128
1129         DEBUG(5,("samr_unknown_32: %d\n", __LINE__));
1130
1131         /* store the response in the SMB stream */
1132         samr_io_r_unknown_32("", &r_u, rdata, 0);
1133
1134         DEBUG(5,("samr_unknown_32: %d\n", __LINE__));
1135
1136 }
1137
1138 /*******************************************************************
1139  api_samr_unknown_32
1140  ********************************************************************/
1141 static void api_samr_unknown_32( int uid, prs_struct *data, prs_struct *rdata)
1142 {
1143         uint32 status = 0;
1144         struct smb_passwd *smb_pass;
1145         fstring mach_acct;
1146
1147         SAMR_Q_UNKNOWN_32 q_u;
1148
1149         /* grab the samr unknown 32 */
1150         samr_io_q_unknown_32("", &q_u, data, 0);
1151
1152         /* find the machine account: tell the caller if it exists.
1153            lkclXXXX i have *no* idea if this is a problem or not
1154            or even if you are supposed to construct a different
1155            reply if the account already exists...
1156          */
1157
1158         fstrcpy(mach_acct, unistrn2(q_u.uni_mach_acct.buffer,
1159                                     q_u.uni_mach_acct.uni_str_len));
1160
1161         become_root(True);
1162         smb_pass = getsmbpwnam(mach_acct);
1163         unbecome_root(True);
1164
1165         if (smb_pass != NULL)
1166         {
1167                 /* machine account exists: say so */
1168                 status = 0xC0000000 | NT_STATUS_USER_EXISTS;
1169         }
1170         else
1171         {
1172                 /* this could cause trouble... */
1173                 status = 0;
1174         }
1175
1176         /* construct reply. */
1177         samr_reply_unknown_32(&q_u, rdata, status);
1178 }
1179
1180
1181 /*******************************************************************
1182  samr_reply_connect
1183  ********************************************************************/
1184 static void samr_reply_connect(SAMR_Q_CONNECT *q_u,
1185                                 prs_struct *rdata)
1186 {
1187         SAMR_R_CONNECT r_u;
1188         BOOL pol_open = False;
1189
1190         /* set up the SAMR connect response */
1191
1192         r_u.status = 0x0;
1193         /* get a (unique) handle.  open a policy on it. */
1194         if (r_u.status == 0x0 && !(pol_open = open_lsa_policy_hnd(&(r_u.connect_pol))))
1195         {
1196                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
1197         }
1198
1199         /* associate the domain SID with the (unique) handle. */
1200         if (r_u.status == 0x0 && !set_lsa_policy_samr_pol_status(&(r_u.connect_pol), q_u->unknown_0))
1201         {
1202                 /* oh, whoops.  don't know what error message to return, here */
1203                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
1204         }
1205
1206         if (r_u.status != 0 && pol_open)
1207         {
1208                 close_lsa_policy_hnd(&(r_u.connect_pol));
1209         }
1210
1211         DEBUG(5,("samr_connect: %d\n", __LINE__));
1212
1213         /* store the response in the SMB stream */
1214         samr_io_r_connect("", &r_u, rdata, 0);
1215
1216         DEBUG(5,("samr_connect: %d\n", __LINE__));
1217
1218 }
1219
1220 /*******************************************************************
1221  api_samr_connect
1222  ********************************************************************/
1223 static void api_samr_connect( int uid, prs_struct *data, prs_struct *rdata)
1224 {
1225         SAMR_Q_CONNECT q_u;
1226
1227         /* grab the samr open policy */
1228         samr_io_q_connect("", &q_u, data, 0);
1229
1230         /* construct reply.  always indicate success */
1231         samr_reply_connect(&q_u, rdata);
1232 }
1233
1234 /*******************************************************************
1235  samr_reply_open_alias
1236  ********************************************************************/
1237 static void samr_reply_open_alias(SAMR_Q_OPEN_ALIAS *q_u,
1238                                 prs_struct *rdata)
1239 {
1240         SAMR_R_OPEN_ALIAS r_u;
1241         BOOL pol_open = False;
1242
1243         /* set up the SAMR open_alias response */
1244
1245         r_u.status = 0x0;
1246         /* get a (unique) handle.  open a policy on it. */
1247         if (r_u.status == 0x0 && !(pol_open = open_lsa_policy_hnd(&(r_u.pol))))
1248         {
1249                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
1250         }
1251
1252         /* associate a RID with the (unique) handle. */
1253         if (r_u.status == 0x0 && !set_lsa_policy_samr_rid(&(r_u.pol), q_u->rid_alias))
1254         {
1255                 /* oh, whoops.  don't know what error message to return, here */
1256                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
1257         }
1258
1259         if (r_u.status != 0 && pol_open)
1260         {
1261                 close_lsa_policy_hnd(&(r_u.pol));
1262         }
1263
1264         DEBUG(5,("samr_open_alias: %d\n", __LINE__));
1265
1266         /* store the response in the SMB stream */
1267         samr_io_r_open_alias("", &r_u, rdata, 0);
1268
1269         DEBUG(5,("samr_open_alias: %d\n", __LINE__));
1270
1271 }
1272
1273 /*******************************************************************
1274  api_samr_open_alias
1275  ********************************************************************/
1276 static void api_samr_open_alias( int uid, prs_struct *data, prs_struct *rdata)
1277                                 
1278 {
1279         SAMR_Q_OPEN_ALIAS q_u;
1280
1281         /* grab the samr open policy */
1282         samr_io_q_open_alias("", &q_u, data, 0);
1283
1284         /* construct reply.  always indicate success */
1285         samr_reply_open_alias(&q_u, rdata);
1286 }
1287
1288 /*******************************************************************
1289  array of \PIPE\samr operations
1290  ********************************************************************/
1291 static struct api_struct api_samr_cmds [] =
1292 {
1293         { "SAMR_CLOSE_HND"        , SAMR_CLOSE_HND        , api_samr_close_hnd        },
1294         { "SAMR_CONNECT"          , SAMR_CONNECT          , api_samr_connect          },
1295         { "SAMR_ENUM_DOM_USERS"   , SAMR_ENUM_DOM_USERS   , api_samr_enum_dom_users   },
1296         { "SAMR_ENUM_DOM_GROUPS"  , SAMR_ENUM_DOM_GROUPS  , api_samr_enum_dom_groups  },
1297         { "SAMR_ENUM_DOM_ALIASES" , SAMR_ENUM_DOM_ALIASES , api_samr_enum_dom_aliases },
1298         { "SAMR_LOOKUP_IDS"       , SAMR_LOOKUP_IDS       , api_samr_lookup_ids       },
1299         { "SAMR_LOOKUP_NAMES"     , SAMR_LOOKUP_NAMES     , api_samr_lookup_names     },
1300         { "SAMR_OPEN_USER"        , SAMR_OPEN_USER        , api_samr_open_user        },
1301         { "SAMR_QUERY_USERINFO"   , SAMR_QUERY_USERINFO   , api_samr_query_userinfo   },
1302         { "SAMR_QUERY_USERGROUPS" , SAMR_QUERY_USERGROUPS , api_samr_query_usergroups },
1303         { "SAMR_QUERY_DISPINFO"   , SAMR_QUERY_DISPINFO   , api_samr_query_dispinfo   },
1304         { "SAMR_QUERY_ALIASINFO"  , SAMR_QUERY_ALIASINFO  , api_samr_query_aliasinfo  },
1305         { "SAMR_0x32"             , 0x32                  , api_samr_unknown_32       },
1306         { "SAMR_UNKNOWN_12"       , SAMR_UNKNOWN_12       , api_samr_unknown_12       },
1307         { "SAMR_OPEN_ALIAS"       , SAMR_OPEN_ALIAS       , api_samr_open_alias       },
1308         { "SAMR_OPEN_DOMAIN"      , SAMR_OPEN_DOMAIN      , api_samr_open_domain      },
1309         { "SAMR_UNKNOWN_3"        , SAMR_UNKNOWN_3        , api_samr_unknown_3        },
1310     { NULL                    , 0                     , NULL                      }
1311 };
1312
1313 /*******************************************************************
1314  receives a samr pipe and responds.
1315  ********************************************************************/
1316 BOOL api_samr_rpc(pipes_struct *p, prs_struct *data)
1317 {
1318     return api_rpcTNP(p, "api_samr_rpc", api_samr_cmds, data);
1319 }
1320