Small update to clitar.c to omit warnings about servers not
[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 sam_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 = getsam21pwent(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->user_rid;
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, ("entry idx: %d user %s, rid 0x%x, acb %x",
81                 (*num_entries), pwd->smb_name, pwd->user_rid, 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 sam_passwd *sam_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                 sam_pass = get_smb21pwd_entry(user_name, 0);
630                 unbecome_root(True);
631
632                 if (sam_pass == NULL)
633                 {
634                         status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
635                         rid[i] = 0;
636                 }
637                 else
638                 {
639                         rid[i] = sam_pass->user_rid;
640                 }
641         }
642 #endif
643
644         num_rids = 1;
645         rid[0] = BUILTIN_ALIAS_RID_USERS;
646
647         make_samr_r_lookup_ids(&r_u, num_rids, rid, status);
648
649         /* store the response in the SMB stream */
650         samr_io_r_lookup_ids("", &r_u, rdata, 0);
651
652         DEBUG(5,("samr_lookup_ids: %d\n", __LINE__));
653
654 }
655
656 /*******************************************************************
657  api_samr_lookup_ids
658  ********************************************************************/
659 static void api_samr_lookup_ids( int uid, prs_struct *data, prs_struct *rdata)
660 {
661         SAMR_Q_LOOKUP_IDS q_u;
662
663         /* grab the samr 0x10 */
664         samr_io_q_lookup_ids("", &q_u, data, 0);
665
666         /* construct reply.  always indicate success */
667         samr_reply_lookup_ids(&q_u, rdata);
668 }
669
670 /*******************************************************************
671  samr_reply_lookup_names
672  ********************************************************************/
673 static void samr_reply_lookup_names(SAMR_Q_LOOKUP_NAMES *q_u,
674                                 prs_struct *rdata)
675 {
676         uint32 rid[MAX_SAM_ENTRIES];
677         uint32 status     = 0;
678         int i;
679         int num_rids = q_u->num_rids1;
680
681         SAMR_R_LOOKUP_NAMES r_u;
682
683         DEBUG(5,("samr_lookup_names: %d\n", __LINE__));
684
685         if (num_rids > MAX_SAM_ENTRIES)
686         {
687                 num_rids = MAX_SAM_ENTRIES;
688                 DEBUG(5,("samr_lookup_names: truncating entries to %d\n", num_rids));
689         }
690
691         SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
692
693         for (i = 0; i < num_rids && status == 0; i++)
694         {
695                 fstring name;
696
697                 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
698
699                 fstrcpy(name, unistrn2(q_u->uni_user_name[i].buffer, q_u->uni_user_name[i].uni_str_len));
700
701                 status = (status != 0x0) ? lookup_user_rid (name, &(rid[i])) : status;
702                 status = (status != 0x0) ? lookup_group_rid(name, &(rid[i])) : status;
703                 status = (status != 0x0) ? lookup_alias_rid(name, &(rid[i])) : status;
704         }
705
706         make_samr_r_lookup_names(&r_u, num_rids, rid, status);
707
708         /* store the response in the SMB stream */
709         samr_io_r_lookup_names("", &r_u, rdata, 0);
710
711         DEBUG(5,("samr_lookup_names: %d\n", __LINE__));
712
713 }
714
715 /*******************************************************************
716  api_samr_lookup_names
717  ********************************************************************/
718 static void api_samr_lookup_names( int uid, prs_struct *data, prs_struct *rdata)
719 {
720         SAMR_Q_LOOKUP_NAMES q_u;
721
722         /* grab the samr lookup names */
723         samr_io_q_lookup_names("", &q_u, data, 0);
724
725         /* construct reply.  always indicate success */
726         samr_reply_lookup_names(&q_u, rdata);
727 }
728
729
730 /*******************************************************************
731  samr_reply_unknown_12
732  ********************************************************************/
733 static void samr_reply_unknown_12(SAMR_Q_UNKNOWN_12 *q_u,
734                                 prs_struct *rdata)
735 {
736         fstring group_names[MAX_SAM_ENTRIES];
737         uint32  group_attrs[MAX_SAM_ENTRIES];
738         uint32 status     = 0;
739         int num_gids = q_u->num_gids1;
740         uint32 pol_idx;
741
742         SAMR_R_UNKNOWN_12 r_u;
743
744         DEBUG(5,("samr_unknown_12: %d\n", __LINE__));
745
746         /* find the policy handle.  open a policy on it. */
747         if (status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1))
748         {
749                 status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
750         }
751
752         if (status == 0x0)
753         {
754                 int i;
755                 if (num_gids > MAX_SAM_ENTRIES)
756                 {
757                         num_gids = MAX_SAM_ENTRIES;
758                         DEBUG(5,("samr_unknown_12: truncating entries to %d\n", num_gids));
759                 }
760
761                 for (i = 0; i < num_gids && status == 0; i++)
762                 {
763                         fstrcpy(group_names[i], "dummy group");
764                         group_attrs[i] = 0x2;
765                 }
766         }
767
768         make_samr_r_unknown_12(&r_u, num_gids, group_names, group_attrs, status);
769
770         /* store the response in the SMB stream */
771         samr_io_r_unknown_12("", &r_u, rdata, 0);
772
773         DEBUG(5,("samr_unknown_12: %d\n", __LINE__));
774
775 }
776
777 /*******************************************************************
778  api_samr_unknown_12
779  ********************************************************************/
780 static void api_samr_unknown_12( int uid, prs_struct *data, prs_struct *rdata)
781 {
782         SAMR_Q_UNKNOWN_12 q_u;
783
784         /* grab the samr lookup names */
785         samr_io_q_unknown_12("", &q_u, data, 0);
786
787         /* construct reply.  always indicate success */
788         samr_reply_unknown_12(&q_u, rdata);
789 }
790
791
792 /*******************************************************************
793  samr_reply_open_user
794  ********************************************************************/
795 static void samr_reply_open_user(SAMR_Q_OPEN_USER *q_u,
796                                 prs_struct *rdata,
797                                 int status)
798 {
799         SAMR_R_OPEN_USER r_u;
800         struct sam_passwd *sam_pass;
801         int pol_idx;
802         BOOL pol_open = False;
803
804         /* set up the SAMR open_user response */
805         bzero(r_u.user_pol.data, POL_HND_SIZE);
806
807         r_u.status = 0x0;
808
809         /* find the policy handle.  open a policy on it. */
810         if (r_u.status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->domain_pol))) == -1))
811         {
812                 r_u.status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
813         }
814
815         /* get a (unique) handle.  open a policy on it. */
816         if (r_u.status == 0x0 && !(pol_open = open_lsa_policy_hnd(&(r_u.user_pol))))
817         {
818                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
819         }
820
821         become_root(True);
822         sam_pass = getsam21pwrid(q_u->user_rid);
823         unbecome_root(True);
824
825         /* check that the RID exists in our domain. */
826         if (r_u.status == 0x0 && sam_pass == NULL)
827         {
828                 r_u.status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
829         }
830
831         /* associate the RID with the (unique) handle. */
832         if (r_u.status == 0x0 && !set_lsa_policy_samr_rid(&(r_u.user_pol), q_u->user_rid))
833         {
834                 /* oh, whoops.  don't know what error message to return, here */
835                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
836         }
837
838         if (r_u.status != 0 && pol_open)
839         {
840                 close_lsa_policy_hnd(&(r_u.user_pol));
841         }
842
843         DEBUG(5,("samr_open_user: %d\n", __LINE__));
844
845         /* store the response in the SMB stream */
846         samr_io_r_open_user("", &r_u, rdata, 0);
847
848         DEBUG(5,("samr_open_user: %d\n", __LINE__));
849
850 }
851
852 /*******************************************************************
853  api_samr_open_user
854  ********************************************************************/
855 static void api_samr_open_user( int rid, prs_struct *data, prs_struct *rdata)
856 {
857         SAMR_Q_OPEN_USER q_u;
858
859         /* grab the samr unknown 22 */
860         samr_io_q_open_user("", &q_u, data, 0);
861
862         /* construct reply.  always indicate success */
863         samr_reply_open_user(&q_u, rdata, 0x0);
864 }
865
866
867 /*************************************************************************
868  get_user_info_21
869  *************************************************************************/
870 static BOOL get_user_info_21(SAM_USER_INFO_21 *id21, uint32 user_rid)
871 {
872         NTTIME dummy_time;
873         struct sam_passwd *sam_pass;
874         LOGON_HRS hrs;
875         int i;
876
877 #ifdef DONT_CHECK_THIS_FOR_NOW
878         if (!pdb_rid_is_user(user_rid))
879         {
880                 DEBUG(4,("RID 0x%x is not a user RID\n", user_rid));
881                 return False;
882         }
883 #endif
884
885         become_root(True);
886         sam_pass = getsam21pwrid(user_rid);
887         unbecome_root(True);
888
889         if (sam_pass == NULL)
890         {
891                 DEBUG(4,("User 0x%x not found\n", user_rid));
892                 return False;
893         }
894
895         DEBUG(3,("User:[%s]\n", sam_pass->smb_name));
896
897         dummy_time.low  = 0xffffffff;
898         dummy_time.high = 0x7fffffff;
899
900         DEBUG(0,("get_user_info_21 - TODO: convert unix times to NTTIMEs\n"));
901
902         /* create a LOGON_HRS structure */
903         hrs.len = sam_pass->hours_len;
904         SMB_ASSERT_ARRAY(hrs.hours, hrs.len);
905         for (i = 0; i < hrs.len; i++)
906         {
907                 hrs.hours[i] = sam_pass->hours[i];
908         }
909
910         make_sam_user_info21(id21,
911
912                            &dummy_time, /* logon_time */
913                            &dummy_time, /* logoff_time */
914                            &dummy_time, /* kickoff_time */
915                            &dummy_time, /* pass_last_set_time */
916                            &dummy_time, /* pass_can_change_time */
917                            &dummy_time, /* pass_must_change_time */
918
919                            sam_pass->smb_name, /* user_name */
920                            sam_pass->full_name, /* full_name */
921                            sam_pass->home_dir, /* home_dir */
922                            sam_pass->dir_drive, /* dir_drive */
923                            sam_pass->logon_script, /* logon_script */
924                            sam_pass->profile_path, /* profile_path */
925                            sam_pass->acct_desc, /* description */
926                            sam_pass->workstations, /* workstations user can log in from */
927                            sam_pass->unknown_str, /* don't know, yet */
928                            sam_pass->munged_dial, /* dialin info.  contains dialin path and tel no */
929
930                            sam_pass->user_rid, /* RID user_id */
931                            sam_pass->group_rid, /* RID group_id */
932                        sam_pass->acct_ctrl,
933
934                    sam_pass->unknown_3, /* unknown_3 */
935                        sam_pass->logon_divs, /* divisions per week */
936                            &hrs, /* logon hours */
937                        sam_pass->unknown_5,
938                        sam_pass->unknown_6);
939
940         return True;
941 }
942
943 /*******************************************************************
944  samr_reply_query_userinfo
945  ********************************************************************/
946 static void samr_reply_query_userinfo(SAMR_Q_QUERY_USERINFO *q_u,
947                                 prs_struct *rdata)
948 {
949         SAMR_R_QUERY_USERINFO r_u;
950 #if 0
951         SAM_USER_INFO_11 id11;
952 #endif
953         SAM_USER_INFO_21 id21;
954         void *info = NULL;
955
956         uint32 status = 0x0;
957         uint32 rid = 0x0;
958         int obj_idx;
959
960         DEBUG(5,("samr_reply_query_userinfo: %d\n", __LINE__));
961
962         /* search for the handle */
963         if (status == 0x0 && (obj_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1)
964         {
965                 status = NT_STATUS_INVALID_HANDLE;
966         }
967
968         /* find the user's rid */
969         if (status == 0x0 && (rid = get_lsa_policy_samr_rid(&(q_u->pol))) == 0xffffffff)
970         {
971                 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
972         }
973
974         DEBUG(5,("samr_reply_query_userinfo: rid:0x%x\n", rid));
975
976         /* ok!  user info levels (there are lots: see MSDEV help), off we go... */
977         if (status == 0x0)
978         {
979                 switch (q_u->switch_value)
980                 {
981 #if 0
982 /* whoops - got this wrong.  i think.  or don't understand what's happening. */
983                         case 0x11:
984                         {
985                                 NTTIME expire;
986                                 info = (void*)&id11;
987                                 
988                                 expire.low  = 0xffffffff;
989                                 expire.high = 0x7fffffff;
990
991                                 make_sam_user_info11(&id11, &expire, "BROOKFIELDS$", 0x03ef, 0x201, 0x0080);
992
993                                 break;
994                         }
995 #endif
996                         case 21:
997                         {
998                                 info = (void*)&id21;
999                                 status = get_user_info_21(&id21, rid) ? 0 : NT_STATUS_NO_SUCH_USER;
1000                                 break;
1001                         }
1002
1003                         default:
1004                         {
1005                                 status = NT_STATUS_INVALID_INFO_CLASS;
1006
1007                                 break;
1008                         }
1009                 }
1010         }
1011
1012         make_samr_r_query_userinfo(&r_u, q_u->switch_value, info, status);
1013
1014         /* store the response in the SMB stream */
1015         samr_io_r_query_userinfo("", &r_u, rdata, 0);
1016
1017         DEBUG(5,("samr_reply_query_userinfo: %d\n", __LINE__));
1018
1019 }
1020
1021 /*******************************************************************
1022  api_samr_query_userinfo
1023  ********************************************************************/
1024 static void api_samr_query_userinfo( int uid, prs_struct *data, prs_struct *rdata)
1025 {
1026         SAMR_Q_QUERY_USERINFO q_u;
1027
1028         /* grab the samr unknown 24 */
1029         samr_io_q_query_userinfo("", &q_u, data, 0);
1030
1031         /* construct reply.  always indicate success */
1032         samr_reply_query_userinfo(&q_u, rdata);
1033 }
1034
1035
1036 /*******************************************************************
1037  samr_reply_query_usergroups
1038  ********************************************************************/
1039 static void samr_reply_query_usergroups(SAMR_Q_QUERY_USERGROUPS *q_u,
1040                                 prs_struct *rdata)
1041 {
1042         SAMR_R_QUERY_USERGROUPS r_u;
1043         uint32 status = 0x0;
1044
1045         struct sam_passwd *sam_pass;
1046         DOM_GID *gids = NULL;
1047         int num_groups = 0;
1048         int pol_idx;
1049         uint32 rid;
1050
1051         DEBUG(5,("samr_query_usergroups: %d\n", __LINE__));
1052
1053         /* find the policy handle.  open a policy on it. */
1054         if (status == 0x0 && ((pol_idx = find_lsa_policy_by_hnd(&(q_u->pol))) == -1))
1055         {
1056                 status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
1057         }
1058
1059         /* find the user's rid */
1060         if (status == 0x0 && (rid = get_lsa_policy_samr_rid(&(q_u->pol))) == 0xffffffff)
1061         {
1062                 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1063         }
1064
1065         if (status == 0x0)
1066         {
1067                 become_root(True);
1068                 sam_pass = getsam21pwrid(rid);
1069                 unbecome_root(True);
1070
1071                 if (sam_pass == NULL)
1072                 {
1073                         status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1074                 }
1075         }
1076
1077         if (status == 0x0)
1078         {
1079                 pstring groups;
1080                 get_domain_user_groups(groups, sam_pass->smb_name);
1081                 gids = NULL;
1082                 num_groups = make_dom_gids(groups, &gids);
1083         }
1084
1085         /* construct the response.  lkclXXXX: gids are not copied! */
1086         make_samr_r_query_usergroups(&r_u, num_groups, gids, status);
1087
1088         /* store the response in the SMB stream */
1089         samr_io_r_query_usergroups("", &r_u, rdata, 0);
1090
1091         if (gids)
1092         {
1093                 free((char *)gids);
1094         }
1095
1096         DEBUG(5,("samr_query_usergroups: %d\n", __LINE__));
1097
1098 }
1099
1100 /*******************************************************************
1101  api_samr_query_usergroups
1102  ********************************************************************/
1103 static void api_samr_query_usergroups( int uid, prs_struct *data, prs_struct *rdata)
1104 {
1105         SAMR_Q_QUERY_USERGROUPS q_u;
1106         /* grab the samr unknown 32 */
1107         samr_io_q_query_usergroups("", &q_u, data, 0);
1108
1109         /* construct reply. */
1110         samr_reply_query_usergroups(&q_u, rdata);
1111 }
1112
1113
1114 /*******************************************************************
1115  samr_reply_unknown_32
1116  ********************************************************************/
1117 static void samr_reply_unknown_32(SAMR_Q_UNKNOWN_32 *q_u,
1118                                 prs_struct *rdata,
1119                                 int status)
1120 {
1121         int i;
1122         SAMR_R_UNKNOWN_32 r_u;
1123
1124         /* set up the SAMR unknown_32 response */
1125         bzero(r_u.pol.data, POL_HND_SIZE);
1126         if (status == 0)
1127         {
1128                 for (i = 4; i < POL_HND_SIZE; i++)
1129                 {
1130                         r_u.pol.data[i] = i+1;
1131                 }
1132         }
1133
1134         make_dom_rid4(&(r_u.rid4), 0x0030, 0, 0);
1135         r_u.status    = status;
1136
1137         DEBUG(5,("samr_unknown_32: %d\n", __LINE__));
1138
1139         /* store the response in the SMB stream */
1140         samr_io_r_unknown_32("", &r_u, rdata, 0);
1141
1142         DEBUG(5,("samr_unknown_32: %d\n", __LINE__));
1143
1144 }
1145
1146 /*******************************************************************
1147  api_samr_unknown_32
1148  ********************************************************************/
1149 static void api_samr_unknown_32( int uid, prs_struct *data, prs_struct *rdata)
1150 {
1151         uint32 status = 0;
1152         struct sam_passwd *sam_pass;
1153         fstring mach_acct;
1154
1155         SAMR_Q_UNKNOWN_32 q_u;
1156
1157         /* grab the samr unknown 32 */
1158         samr_io_q_unknown_32("", &q_u, data, 0);
1159
1160         /* find the machine account: tell the caller if it exists.
1161            lkclXXXX i have *no* idea if this is a problem or not
1162            or even if you are supposed to construct a different
1163            reply if the account already exists...
1164          */
1165
1166         fstrcpy(mach_acct, unistrn2(q_u.uni_mach_acct.buffer,
1167                                     q_u.uni_mach_acct.uni_str_len));
1168
1169         become_root(True);
1170         sam_pass = getsam21pwnam(mach_acct);
1171         unbecome_root(True);
1172
1173         if (sam_pass != NULL)
1174         {
1175                 /* machine account exists: say so */
1176                 status = 0xC0000000 | NT_STATUS_USER_EXISTS;
1177         }
1178         else
1179         {
1180                 /* this could cause trouble... */
1181                 DEBUG(0,("trouble!\n"));
1182                 status = 0;
1183         }
1184
1185         /* construct reply. */
1186         samr_reply_unknown_32(&q_u, rdata, status);
1187 }
1188
1189
1190 /*******************************************************************
1191  samr_reply_connect
1192  ********************************************************************/
1193 static void samr_reply_connect(SAMR_Q_CONNECT *q_u,
1194                                 prs_struct *rdata)
1195 {
1196         SAMR_R_CONNECT r_u;
1197         BOOL pol_open = False;
1198
1199         /* set up the SAMR connect response */
1200
1201         r_u.status = 0x0;
1202         /* get a (unique) handle.  open a policy on it. */
1203         if (r_u.status == 0x0 && !(pol_open = open_lsa_policy_hnd(&(r_u.connect_pol))))
1204         {
1205                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
1206         }
1207
1208         /* associate the domain SID with the (unique) handle. */
1209         if (r_u.status == 0x0 && !set_lsa_policy_samr_pol_status(&(r_u.connect_pol), q_u->unknown_0))
1210         {
1211                 /* oh, whoops.  don't know what error message to return, here */
1212                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
1213         }
1214
1215         if (r_u.status != 0 && pol_open)
1216         {
1217                 close_lsa_policy_hnd(&(r_u.connect_pol));
1218         }
1219
1220         DEBUG(5,("samr_connect: %d\n", __LINE__));
1221
1222         /* store the response in the SMB stream */
1223         samr_io_r_connect("", &r_u, rdata, 0);
1224
1225         DEBUG(5,("samr_connect: %d\n", __LINE__));
1226
1227 }
1228
1229 /*******************************************************************
1230  api_samr_connect
1231  ********************************************************************/
1232 static void api_samr_connect( int uid, prs_struct *data, prs_struct *rdata)
1233 {
1234         SAMR_Q_CONNECT q_u;
1235
1236         /* grab the samr open policy */
1237         samr_io_q_connect("", &q_u, data, 0);
1238
1239         /* construct reply.  always indicate success */
1240         samr_reply_connect(&q_u, rdata);
1241 }
1242
1243 /*******************************************************************
1244  samr_reply_open_alias
1245  ********************************************************************/
1246 static void samr_reply_open_alias(SAMR_Q_OPEN_ALIAS *q_u,
1247                                 prs_struct *rdata)
1248 {
1249         SAMR_R_OPEN_ALIAS r_u;
1250         BOOL pol_open = False;
1251
1252         /* set up the SAMR open_alias response */
1253
1254         r_u.status = 0x0;
1255         /* get a (unique) handle.  open a policy on it. */
1256         if (r_u.status == 0x0 && !(pol_open = open_lsa_policy_hnd(&(r_u.pol))))
1257         {
1258                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
1259         }
1260
1261         /* associate a RID with the (unique) handle. */
1262         if (r_u.status == 0x0 && !set_lsa_policy_samr_rid(&(r_u.pol), q_u->rid_alias))
1263         {
1264                 /* oh, whoops.  don't know what error message to return, here */
1265                 r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
1266         }
1267
1268         if (r_u.status != 0 && pol_open)
1269         {
1270                 close_lsa_policy_hnd(&(r_u.pol));
1271         }
1272
1273         DEBUG(5,("samr_open_alias: %d\n", __LINE__));
1274
1275         /* store the response in the SMB stream */
1276         samr_io_r_open_alias("", &r_u, rdata, 0);
1277
1278         DEBUG(5,("samr_open_alias: %d\n", __LINE__));
1279
1280 }
1281
1282 /*******************************************************************
1283  api_samr_open_alias
1284  ********************************************************************/
1285 static void api_samr_open_alias( int uid, prs_struct *data, prs_struct *rdata)
1286                                 
1287 {
1288         SAMR_Q_OPEN_ALIAS q_u;
1289
1290         /* grab the samr open policy */
1291         samr_io_q_open_alias("", &q_u, data, 0);
1292
1293         /* construct reply.  always indicate success */
1294         samr_reply_open_alias(&q_u, rdata);
1295 }
1296
1297 /*******************************************************************
1298  array of \PIPE\samr operations
1299  ********************************************************************/
1300 static struct api_struct api_samr_cmds [] =
1301 {
1302         { "SAMR_CLOSE_HND"        , SAMR_CLOSE_HND        , api_samr_close_hnd        },
1303         { "SAMR_CONNECT"          , SAMR_CONNECT          , api_samr_connect          },
1304         { "SAMR_ENUM_DOM_USERS"   , SAMR_ENUM_DOM_USERS   , api_samr_enum_dom_users   },
1305         { "SAMR_ENUM_DOM_GROUPS"  , SAMR_ENUM_DOM_GROUPS  , api_samr_enum_dom_groups  },
1306         { "SAMR_ENUM_DOM_ALIASES" , SAMR_ENUM_DOM_ALIASES , api_samr_enum_dom_aliases },
1307         { "SAMR_LOOKUP_IDS"       , SAMR_LOOKUP_IDS       , api_samr_lookup_ids       },
1308         { "SAMR_LOOKUP_NAMES"     , SAMR_LOOKUP_NAMES     , api_samr_lookup_names     },
1309         { "SAMR_OPEN_USER"        , SAMR_OPEN_USER        , api_samr_open_user        },
1310         { "SAMR_QUERY_USERINFO"   , SAMR_QUERY_USERINFO   , api_samr_query_userinfo   },
1311         { "SAMR_QUERY_USERGROUPS" , SAMR_QUERY_USERGROUPS , api_samr_query_usergroups },
1312         { "SAMR_QUERY_DISPINFO"   , SAMR_QUERY_DISPINFO   , api_samr_query_dispinfo   },
1313         { "SAMR_QUERY_ALIASINFO"  , SAMR_QUERY_ALIASINFO  , api_samr_query_aliasinfo  },
1314         { "SAMR_0x32"             , 0x32                  , api_samr_unknown_32       },
1315         { "SAMR_UNKNOWN_12"       , SAMR_UNKNOWN_12       , api_samr_unknown_12       },
1316         { "SAMR_OPEN_ALIAS"       , SAMR_OPEN_ALIAS       , api_samr_open_alias       },
1317         { "SAMR_OPEN_DOMAIN"      , SAMR_OPEN_DOMAIN      , api_samr_open_domain      },
1318         { "SAMR_UNKNOWN_3"        , SAMR_UNKNOWN_3        , api_samr_unknown_3        },
1319     { NULL                    , 0                     , NULL                      }
1320 };
1321
1322 /*******************************************************************
1323  receives a samr pipe and responds.
1324  ********************************************************************/
1325 BOOL api_samr_rpc(pipes_struct *p, prs_struct *data)
1326 {
1327     return api_rpcTNP(p, "api_samr_rpc", api_samr_cmds, data);
1328 }
1329