Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-test
[ira/wip.git] / source3 / passdb / pdb_interface.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett                        2002
5    Copyright (C) Jelmer Vernooij                        2002
6    Copyright (C) Simo Sorce                             2003
7    Copyright (C) Volker Lendecke                        2006
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_PASSDB
27
28 static_decl_pdb;
29
30 static struct pdb_init_function_entry *backends = NULL;
31
32 static void lazy_initialize_passdb(void)
33 {
34         static bool initialized = False;
35         if(initialized) {
36                 return;
37         }
38         static_init_pdb;
39         initialized = True;
40 }
41
42 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
43                                   const char **name,
44                                   enum lsa_SidType *psid_name_use,
45                                   union unid_t *unix_id);
46
47 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init) 
48 {
49         struct pdb_init_function_entry *entry = backends;
50
51         if(version != PASSDB_INTERFACE_VERSION) {
52                 DEBUG(0,("Can't register passdb backend!\n"
53                          "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
54                          "while this version of samba uses version %d\n", 
55                          version,PASSDB_INTERFACE_VERSION));
56                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
57         }
58
59         if (!name || !init) {
60                 return NT_STATUS_INVALID_PARAMETER;
61         }
62
63         DEBUG(5,("Attempting to register passdb backend %s\n", name));
64
65         /* Check for duplicates */
66         if (pdb_find_backend_entry(name)) {
67                 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
68                 return NT_STATUS_OBJECT_NAME_COLLISION;
69         }
70
71         entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
72         entry->name = smb_xstrdup(name);
73         entry->init = init;
74
75         DLIST_ADD(backends, entry);
76         DEBUG(5,("Successfully added passdb backend '%s'\n", name));
77         return NT_STATUS_OK;
78 }
79
80 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
81 {
82         struct pdb_init_function_entry *entry = backends;
83
84         while(entry) {
85                 if (strcmp(entry->name, name)==0) return entry;
86                 entry = entry->next;
87         }
88
89         return NULL;
90 }
91
92 /*
93  * The event context for the passdb backend. I know this is a bad hack and yet
94  * another static variable, but our pdb API is a global thing per
95  * definition. The first use for this is the LDAP idle function, more might be
96  * added later.
97  *
98  * I don't feel too bad about this static variable, it replaces the
99  * smb_idle_event_list that used to exist in lib/module.c.  -- VL
100  */
101
102 static struct event_context *pdb_event_ctx;
103
104 struct event_context *pdb_get_event_context(void)
105 {
106         return pdb_event_ctx;
107 }
108
109 /******************************************************************
110   Make a pdb_methods from scratch
111  *******************************************************************/
112
113 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
114 {
115         char *module_name = smb_xstrdup(selected);
116         char *module_location = NULL, *p;
117         struct pdb_init_function_entry *entry;
118         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
119
120         lazy_initialize_passdb();
121
122         p = strchr(module_name, ':');
123
124         if (p) {
125                 *p = 0;
126                 module_location = p+1;
127                 trim_char(module_location, ' ', ' ');
128         }
129
130         trim_char(module_name, ' ', ' ');
131
132
133         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
134
135         entry = pdb_find_backend_entry(module_name);
136         
137         /* Try to find a module that contains this module */
138         if (!entry) { 
139                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
140                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
141                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
142                         SAFE_FREE(module_name);
143                         return NT_STATUS_UNSUCCESSFUL;
144                 }
145         }
146         
147         /* No such backend found */
148         if(!entry) { 
149                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
150                 SAFE_FREE(module_name);
151                 return NT_STATUS_INVALID_PARAMETER;
152         }
153
154         DEBUG(5,("Found pdb backend %s\n", module_name));
155
156         if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
157                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", 
158                         selected, nt_errstr(nt_status)));
159                 SAFE_FREE(module_name);
160                 return nt_status;
161         }
162
163         SAFE_FREE(module_name);
164
165         DEBUG(5,("pdb backend %s has a valid init\n", selected));
166
167         return nt_status;
168 }
169
170 /******************************************************************
171  Return an already initialised pdn_methods structure
172 *******************************************************************/
173
174 static struct pdb_methods *pdb_get_methods_reload( bool reload ) 
175 {
176         static struct pdb_methods *pdb = NULL;
177
178         if ( pdb && reload ) {
179                 pdb->free_private_data( &(pdb->private_data) );
180                 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
181                         char *msg = NULL;
182                         asprintf(&msg, "pdb_get_methods_reload: "
183                                 "failed to get pdb methods for backend %s\n",
184                                 lp_passdb_backend());
185                         smb_panic(msg);
186                 }
187         }
188
189         if ( !pdb ) {
190                 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
191                         char *msg = NULL;
192                         asprintf(&msg, "pdb_get_methods_reload: "
193                                 "failed to get pdb methods for backend %s\n",
194                                 lp_passdb_backend());
195                         smb_panic(msg);
196                 }
197         }
198
199         return pdb;
200 }
201
202 static struct pdb_methods *pdb_get_methods(void)
203 {
204         return pdb_get_methods_reload(False);
205 }
206
207 bool pdb_getsampwnam(struct samu *sam_acct, const char *username) 
208 {
209         struct pdb_methods *pdb = pdb_get_methods();
210         struct samu *cache_copy;
211         const struct dom_sid *user_sid;
212
213         if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
214                 return False;
215         }
216
217         cache_copy = samu_new(NULL);
218         if (cache_copy == NULL) {
219                 return False;
220         }
221
222         if (!pdb_copy_sam_account(cache_copy, sam_acct)) {
223                 TALLOC_FREE(cache_copy);
224                 return False;
225         }
226
227         user_sid = pdb_get_user_sid(cache_copy);
228
229         memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
230                             data_blob_const(user_sid, sizeof(*user_sid)),
231                             cache_copy);
232
233         return True;
234 }
235
236 /**********************************************************************
237 **********************************************************************/
238
239 bool guest_user_info( struct samu *user )
240 {
241         struct passwd *pwd;
242         NTSTATUS result;
243         const char *guestname = lp_guestaccount();
244         
245         if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
246                 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n", 
247                         guestname));
248                 return False;
249         }
250         
251         result = samu_set_unix(user, pwd );
252
253         TALLOC_FREE( pwd );
254
255         return NT_STATUS_IS_OK( result );
256 }
257
258 /**********************************************************************
259 **********************************************************************/
260
261 bool pdb_getsampwsid(struct samu *sam_acct, const DOM_SID *sid) 
262 {
263         struct pdb_methods *pdb = pdb_get_methods();
264         uint32 rid;
265         void *cache_data;
266
267         /* hard code the Guest RID of 501 */
268
269         if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
270                 return False;
271
272         if ( rid == DOMAIN_USER_RID_GUEST ) {
273                 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
274                 return guest_user_info( sam_acct );
275         }
276         
277         /* check the cache first */
278
279         cache_data = memcache_lookup_talloc(
280                 NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
281
282         if (cache_data != NULL) {
283                 struct samu *cache_copy = talloc_get_type_abort(
284                         cache_data, struct samu);
285
286                 return pdb_copy_sam_account(sam_acct, cache_copy);
287         }
288
289         return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
290 }
291
292 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
293                                         TALLOC_CTX *tmp_ctx, const char *name,
294                                         uint32 acb_info, uint32 *rid)
295 {
296         struct samu *sam_pass;
297         NTSTATUS status;
298         struct passwd *pwd;
299
300         if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
301                 return NT_STATUS_NO_MEMORY;
302         }
303
304         if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
305                 char *add_script = NULL;
306                 int add_ret;
307                 fstring name2;
308
309                 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
310                         add_script = talloc_strdup(tmp_ctx,
311                                         lp_adduser_script());
312                 } else {
313                         add_script = talloc_strdup(tmp_ctx,
314                                         lp_addmachine_script());
315                 }
316
317                 if (!add_script || add_script[0] == '\0') {
318                         DEBUG(3, ("Could not find user %s and no add script "
319                                   "defined\n", name));
320                         return NT_STATUS_NO_SUCH_USER;
321                 }
322
323                 /* lowercase the username before creating the Unix account for 
324                    compatibility with previous Samba releases */
325                 fstrcpy( name2, name );
326                 strlower_m( name2 );
327                 add_script = talloc_all_string_sub(tmp_ctx,
328                                         add_script,
329                                         "%u",
330                                         name2);
331                 if (!add_script) {
332                         return NT_STATUS_NO_MEMORY;
333                 }
334                 add_ret = smbrun(add_script,NULL);
335                 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
336                                         add_script, add_ret));
337                 if (add_ret == 0) {
338                         smb_nscd_flush_user_cache();
339                 }
340
341                 flush_pwnam_cache();
342
343                 pwd = Get_Pwnam_alloc(tmp_ctx, name);
344         }
345
346         /* we have a valid SID coming out of this call */
347
348         status = samu_alloc_rid_unix( sam_pass, pwd );
349
350         TALLOC_FREE( pwd );
351
352         if (!NT_STATUS_IS_OK(status)) {
353                 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
354                 return status;
355         }
356
357         if (!sid_peek_check_rid(get_global_sam_sid(),
358                                 pdb_get_user_sid(sam_pass), rid)) {
359                 DEBUG(0, ("Could not get RID of fresh user\n"));
360                 return NT_STATUS_INTERNAL_ERROR;
361         }
362
363         /* Use the username case specified in the original request */
364
365         pdb_set_username( sam_pass, name, PDB_SET );
366
367         /* Disable the account on creation, it does not have a reasonable password yet. */
368
369         acb_info |= ACB_DISABLED;
370
371         pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
372
373         status = pdb_add_sam_account(sam_pass);
374
375         TALLOC_FREE(sam_pass);
376
377         return status;
378 }
379
380 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
381                          uint32 *rid)
382 {
383         struct pdb_methods *pdb = pdb_get_methods();
384         return pdb->create_user(pdb, mem_ctx, name, flags, rid);
385 }
386
387 /****************************************************************************
388  Delete a UNIX user on demand.
389 ****************************************************************************/
390
391 static int smb_delete_user(const char *unix_user)
392 {
393         char *del_script = NULL;
394         int ret;
395
396         /* safety check */
397
398         if ( strequal( unix_user, "root" ) ) {
399                 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
400                 return -1;
401         }
402
403         del_script = talloc_strdup(talloc_tos(), lp_deluser_script());
404         if (!del_script || !*del_script) {
405                 return -1;
406         }
407         del_script = talloc_all_string_sub(talloc_tos(),
408                                 del_script,
409                                 "%u",
410                                 unix_user);
411         if (!del_script) {
412                 return -1;
413         }
414         ret = smbrun(del_script,NULL);
415         flush_pwnam_cache();
416         if (ret == 0) {
417                 smb_nscd_flush_user_cache();
418         }
419         DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
420
421         return ret;
422 }
423
424 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
425                                         TALLOC_CTX *mem_ctx,
426                                         struct samu *sam_acct)
427 {
428         NTSTATUS status;
429         fstring username;
430
431         status = pdb_delete_sam_account(sam_acct);
432         if (!NT_STATUS_IS_OK(status)) {
433                 return status;
434         }
435
436         /*
437          * Now delete the unix side ....
438          * note: we don't check if the delete really happened as the script is
439          * not necessary present and maybe the sysadmin doesn't want to delete
440          * the unix side
441          */
442
443         /* always lower case the username before handing it off to 
444            external scripts */
445
446         fstrcpy( username, pdb_get_username(sam_acct) );
447         strlower_m( username );
448
449         smb_delete_user( username );
450         
451         return status;
452 }
453
454 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
455 {
456         struct pdb_methods *pdb = pdb_get_methods();
457         uid_t uid = -1;
458
459         /* sanity check to make sure we don't delete root */
460
461         if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
462                 return NT_STATUS_NO_SUCH_USER;
463         }
464
465         if ( uid == 0 ) {
466                 return NT_STATUS_ACCESS_DENIED;
467         }
468
469         return pdb->delete_user(pdb, mem_ctx, sam_acct);
470 }
471
472 NTSTATUS pdb_add_sam_account(struct samu *sam_acct) 
473 {
474         struct pdb_methods *pdb = pdb_get_methods();
475         return pdb->add_sam_account(pdb, sam_acct);
476 }
477
478 NTSTATUS pdb_update_sam_account(struct samu *sam_acct) 
479 {
480         struct pdb_methods *pdb = pdb_get_methods();
481
482         memcache_flush(NULL, PDB_GETPWSID_CACHE);
483
484         return pdb->update_sam_account(pdb, sam_acct);
485 }
486
487 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct) 
488 {
489         struct pdb_methods *pdb = pdb_get_methods();
490
491         memcache_flush(NULL, PDB_GETPWSID_CACHE);
492
493         return pdb->delete_sam_account(pdb, sam_acct);
494 }
495
496 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
497 {
498         struct pdb_methods *pdb = pdb_get_methods();
499         uid_t uid;
500         NTSTATUS status;
501
502         memcache_flush(NULL, PDB_GETPWSID_CACHE);
503
504         /* sanity check to make sure we don't rename root */
505
506         if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
507                 return NT_STATUS_NO_SUCH_USER;
508         }
509
510         if ( uid == 0 ) {
511                 return NT_STATUS_ACCESS_DENIED;
512         }
513
514         status = pdb->rename_sam_account(pdb, oldname, newname);
515
516         /* always flush the cache here just to be safe */
517         flush_pwnam_cache();
518
519         return status;
520 }
521
522 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
523 {
524         struct pdb_methods *pdb = pdb_get_methods();
525         return pdb->update_login_attempts(pdb, sam_acct, success);
526 }
527
528 bool pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
529 {
530         struct pdb_methods *pdb = pdb_get_methods();
531         return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
532 }
533
534 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
535 {
536         struct pdb_methods *pdb = pdb_get_methods();
537         return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
538 }
539
540 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
541 {
542         struct pdb_methods *pdb = pdb_get_methods();
543         return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
544 }
545
546 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
547                                              TALLOC_CTX *mem_ctx,
548                                              const char *name,
549                                              uint32 *rid)
550 {
551         DOM_SID group_sid;
552         struct group *grp;
553         fstring tmp;
554
555         grp = getgrnam(name);
556
557         if (grp == NULL) {
558                 gid_t gid;
559
560                 if (smb_create_group(name, &gid) != 0) {
561                         return NT_STATUS_ACCESS_DENIED;
562                 }
563
564                 grp = getgrgid(gid);
565         }
566
567         if (grp == NULL) {
568                 return NT_STATUS_ACCESS_DENIED;
569         }
570
571         if (pdb_rid_algorithm()) {
572                 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
573         } else {
574                 if (!pdb_new_rid(rid)) {
575                         return NT_STATUS_ACCESS_DENIED;
576                 }
577         }
578
579         sid_compose(&group_sid, get_global_sam_sid(), *rid);
580                 
581         return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
582                                  SID_NAME_DOM_GRP, name, NULL);
583 }
584
585 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
586                               uint32 *rid)
587 {
588         struct pdb_methods *pdb = pdb_get_methods();
589         return pdb->create_dom_group(pdb, mem_ctx, name, rid);
590 }
591
592 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
593                                              TALLOC_CTX *mem_ctx,
594                                              uint32 rid)
595 {
596         DOM_SID group_sid;
597         GROUP_MAP map;
598         NTSTATUS status;
599         struct group *grp;
600         const char *grp_name;
601
602         sid_compose(&group_sid, get_global_sam_sid(), rid);
603
604         if (!get_domain_group_from_sid(group_sid, &map)) {
605                 DEBUG(10, ("Could not find group for rid %d\n", rid));
606                 return NT_STATUS_NO_SUCH_GROUP;
607         }
608
609         /* We need the group name for the smb_delete_group later on */
610
611         if (map.gid == (gid_t)-1) {
612                 return NT_STATUS_NO_SUCH_GROUP;
613         }
614
615         grp = getgrgid(map.gid);
616         if (grp == NULL) {
617                 return NT_STATUS_NO_SUCH_GROUP;
618         }
619
620         /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
621
622         grp_name = talloc_strdup(mem_ctx, grp->gr_name);
623         if (grp_name == NULL) {
624                 return NT_STATUS_NO_MEMORY;
625         }
626
627         status = pdb_delete_group_mapping_entry(group_sid);
628
629         if (!NT_STATUS_IS_OK(status)) {
630                 return status;
631         }
632
633         /* Don't check the result of smb_delete_group */
634         
635         smb_delete_group(grp_name);
636
637         return NT_STATUS_OK;
638 }
639
640 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
641 {
642         struct pdb_methods *pdb = pdb_get_methods();
643         return pdb->delete_dom_group(pdb, mem_ctx, rid);
644 }
645
646 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
647 {
648         struct pdb_methods *pdb = pdb_get_methods();
649         return pdb->add_group_mapping_entry(pdb, map);
650 }
651
652 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
653 {
654         struct pdb_methods *pdb = pdb_get_methods();
655         return pdb->update_group_mapping_entry(pdb, map);
656 }
657
658 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
659 {
660         struct pdb_methods *pdb = pdb_get_methods();
661         return pdb->delete_group_mapping_entry(pdb, sid);
662 }
663
664 bool pdb_enum_group_mapping(const DOM_SID *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
665                             size_t *p_num_entries, bool unix_only)
666 {
667         struct pdb_methods *pdb = pdb_get_methods();
668         return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
669                 pp_rmap, p_num_entries, unix_only));
670 }
671
672 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
673                                 const DOM_SID *sid,
674                                 uint32 **pp_member_rids,
675                                 size_t *p_num_members)
676 {
677         struct pdb_methods *pdb = pdb_get_methods();
678         NTSTATUS result;
679
680         result = pdb->enum_group_members(pdb, mem_ctx, 
681                         sid, pp_member_rids, p_num_members);
682                 
683         /* special check for rid 513 */
684                 
685         if ( !NT_STATUS_IS_OK( result ) ) {
686                 uint32 rid;
687                 
688                 sid_peek_rid( sid, &rid );
689                 
690                 if ( rid == DOMAIN_GROUP_RID_USERS ) {
691                         *p_num_members = 0;
692                         *pp_member_rids = NULL;
693                         
694                         return NT_STATUS_OK;
695                 }
696         }
697         
698         return result;
699 }
700
701 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
702                                     DOM_SID **pp_sids, gid_t **pp_gids,
703                                     size_t *p_num_groups)
704 {
705         struct pdb_methods *pdb = pdb_get_methods();
706         return pdb->enum_group_memberships(
707                 pdb, mem_ctx, user,
708                 pp_sids, pp_gids, p_num_groups);
709 }
710
711 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
712                                                    TALLOC_CTX *mem_ctx,
713                                                    struct samu *sampass)
714 {
715         struct group *grp;
716         gid_t gid;
717
718         if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
719             (grp = getgrgid(gid)) == NULL) {
720                 return NT_STATUS_INVALID_PRIMARY_GROUP;
721         }
722
723         if (smb_set_primary_group(grp->gr_name,
724                                   pdb_get_username(sampass)) != 0) {
725                 return NT_STATUS_ACCESS_DENIED;
726         }
727
728         return NT_STATUS_OK;
729 }
730
731 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
732 {
733         struct pdb_methods *pdb = pdb_get_methods();
734         return pdb->set_unix_primary_group(pdb, mem_ctx, user);
735 }
736
737 /*
738  * Helper function to see whether a user is in a group. We can't use
739  * user_in_group_sid here because this creates dependencies only smbd can
740  * fulfil.
741  */
742
743 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
744                               const DOM_SID *group_sid)
745 {
746         DOM_SID *sids;
747         gid_t *gids;
748         size_t i, num_groups;
749
750         if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
751                                                         &sids, &gids,
752                                                         &num_groups))) {
753                 return False;
754         }
755
756         for (i=0; i<num_groups; i++) {
757                 if (sid_equal(group_sid, &sids[i])) {
758                         return True;
759                 }
760         }
761         return False;
762 }
763
764 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
765                                          TALLOC_CTX *mem_ctx,
766                                          uint32 group_rid,
767                                          uint32 member_rid)
768 {
769         DOM_SID group_sid, member_sid;
770         struct samu *account = NULL;
771         GROUP_MAP map;
772         struct group *grp;
773         struct passwd *pwd;
774         const char *group_name;
775         uid_t uid;
776
777         sid_compose(&group_sid, get_global_sam_sid(), group_rid);
778         sid_compose(&member_sid, get_global_sam_sid(), member_rid);
779
780         if (!get_domain_group_from_sid(group_sid, &map) ||
781             (map.gid == (gid_t)-1) ||
782             ((grp = getgrgid(map.gid)) == NULL)) {
783                 return NT_STATUS_NO_SUCH_GROUP;
784         }
785
786         group_name = talloc_strdup(mem_ctx, grp->gr_name);
787         if (group_name == NULL) {
788                 return NT_STATUS_NO_MEMORY;
789         }
790
791         if ( !(account = samu_new( NULL )) ) {
792                 return NT_STATUS_NO_MEMORY;
793         }
794
795         if (!pdb_getsampwsid(account, &member_sid) ||
796             !sid_to_uid(&member_sid, &uid) ||
797             ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
798                 return NT_STATUS_NO_SUCH_USER;
799         }
800
801         if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
802                 return NT_STATUS_MEMBER_IN_GROUP;
803         }
804
805         /* 
806          * ok, the group exist, the user exist, the user is not in the group,
807          * we can (finally) add it to the group !
808          */
809
810         smb_add_user_group(group_name, pwd->pw_name);
811
812         if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
813                 return NT_STATUS_ACCESS_DENIED;
814         }
815
816         return NT_STATUS_OK;
817 }
818
819 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
820                           uint32 member_rid)
821 {
822         struct pdb_methods *pdb = pdb_get_methods();
823         return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
824 }
825
826 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
827                                          TALLOC_CTX *mem_ctx,
828                                          uint32 group_rid,
829                                          uint32 member_rid)
830 {
831         DOM_SID group_sid, member_sid;
832         struct samu *account = NULL;
833         GROUP_MAP map;
834         struct group *grp;
835         struct passwd *pwd;
836         const char *group_name;
837         uid_t uid;
838
839         sid_compose(&group_sid, get_global_sam_sid(), group_rid);
840         sid_compose(&member_sid, get_global_sam_sid(), member_rid);
841
842         if (!get_domain_group_from_sid(group_sid, &map) ||
843             (map.gid == (gid_t)-1) ||
844             ((grp = getgrgid(map.gid)) == NULL)) {
845                 return NT_STATUS_NO_SUCH_GROUP;
846         }
847
848         group_name = talloc_strdup(mem_ctx, grp->gr_name);
849         if (group_name == NULL) {
850                 return NT_STATUS_NO_MEMORY;
851         }
852
853         if ( !(account = samu_new( NULL )) ) {
854                 return NT_STATUS_NO_MEMORY;
855         }
856
857         if (!pdb_getsampwsid(account, &member_sid) ||
858             !sid_to_uid(&member_sid, &uid) ||
859             ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
860                 return NT_STATUS_NO_SUCH_USER;
861         }
862
863         if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
864                 return NT_STATUS_MEMBER_NOT_IN_GROUP;
865         }
866
867         /* 
868          * ok, the group exist, the user exist, the user is in the group,
869          * we can (finally) delete it from the group!
870          */
871
872         smb_delete_user_group(group_name, pwd->pw_name);
873
874         if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
875                 return NT_STATUS_ACCESS_DENIED;
876         }
877
878         return NT_STATUS_OK;
879 }
880
881 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
882                           uint32 member_rid)
883 {
884         struct pdb_methods *pdb = pdb_get_methods();
885         return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
886 }
887
888 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
889 {
890         struct pdb_methods *pdb = pdb_get_methods();
891         return pdb->create_alias(pdb, name, rid);
892 }
893
894 NTSTATUS pdb_delete_alias(const DOM_SID *sid)
895 {
896         struct pdb_methods *pdb = pdb_get_methods();
897         return pdb->delete_alias(pdb, sid);
898 }
899
900 NTSTATUS pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
901 {
902         struct pdb_methods *pdb = pdb_get_methods();
903         return pdb->get_aliasinfo(pdb, sid, info);
904 }
905
906 NTSTATUS pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
907 {
908         struct pdb_methods *pdb = pdb_get_methods();
909         return pdb->set_aliasinfo(pdb, sid, info);
910 }
911
912 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
913 {
914         struct pdb_methods *pdb = pdb_get_methods();
915         return pdb->add_aliasmem(pdb, alias, member);
916 }
917
918 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
919 {
920         struct pdb_methods *pdb = pdb_get_methods();
921         return pdb->del_aliasmem(pdb, alias, member);
922 }
923
924 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
925                            DOM_SID **pp_members, size_t *p_num_members)
926 {
927         struct pdb_methods *pdb = pdb_get_methods();
928         return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
929 }
930
931 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
932                                     const DOM_SID *domain_sid,
933                                     const DOM_SID *members, size_t num_members,
934                                     uint32 **pp_alias_rids,
935                                     size_t *p_num_alias_rids)
936 {
937         struct pdb_methods *pdb = pdb_get_methods();
938         return pdb->enum_alias_memberships(pdb, mem_ctx,
939                                                        domain_sid,
940                                                        members, num_members,
941                                                        pp_alias_rids,
942                                                        p_num_alias_rids);
943 }
944
945 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
946                          int num_rids,
947                          uint32 *rids,
948                          const char **names,
949                          enum lsa_SidType *attrs)
950 {
951         struct pdb_methods *pdb = pdb_get_methods();
952         return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
953 }
954
955 /* 
956  * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere 
957  *       in the samba code.
958  *       Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually 
959  *       also ask pdb_lookup_rids, thus looking up a bunch of rids at a time, 
960  *       the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
961  *       down to are pdb_getsampwnam and pdb_getgrnam instead of
962  *       pdb_lookup_names.
963  *       But in principle, it the call belongs to the API and might get
964  *       used in this context some day. 
965  */
966 #if 0
967 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
968                           int num_names,
969                           const char **names,
970                           uint32 *rids,
971                           enum lsa_SidType *attrs)
972 {
973         struct pdb_methods *pdb = pdb_get_methods();
974         return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
975 }
976 #endif
977
978 bool pdb_get_account_policy(int policy_index, uint32 *value)
979 {
980         struct pdb_methods *pdb = pdb_get_methods();
981         NTSTATUS status;
982         
983         become_root();
984         status = pdb->get_account_policy(pdb, policy_index, value);
985         unbecome_root();
986         
987         return NT_STATUS_IS_OK(status); 
988 }
989
990 bool pdb_set_account_policy(int policy_index, uint32 value)
991 {
992         struct pdb_methods *pdb = pdb_get_methods();
993         NTSTATUS status;
994
995         become_root();
996         status = pdb->set_account_policy(pdb, policy_index, value);
997         unbecome_root();
998
999         return NT_STATUS_IS_OK(status);
1000 }
1001
1002 bool pdb_get_seq_num(time_t *seq_num)
1003 {
1004         struct pdb_methods *pdb = pdb_get_methods();
1005         return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1006 }
1007
1008 bool pdb_uid_to_rid(uid_t uid, uint32 *rid)
1009 {
1010         struct pdb_methods *pdb = pdb_get_methods();
1011         return pdb->uid_to_rid(pdb, uid, rid);
1012 }
1013
1014 bool pdb_uid_to_sid(uid_t uid, DOM_SID *sid)
1015 {
1016         struct pdb_methods *pdb = pdb_get_methods();
1017         return pdb->uid_to_sid(pdb, uid, sid);
1018 }
1019
1020 bool pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1021 {
1022         struct pdb_methods *pdb = pdb_get_methods();
1023         return pdb->gid_to_sid(pdb, gid, sid);
1024 }
1025
1026 bool pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1027                    enum lsa_SidType *type)
1028 {
1029         struct pdb_methods *pdb = pdb_get_methods();
1030         return pdb->sid_to_id(pdb, sid, id, type);
1031 }
1032
1033 bool pdb_rid_algorithm(void)
1034 {
1035         struct pdb_methods *pdb = pdb_get_methods();
1036         return pdb->rid_algorithm(pdb);
1037 }
1038
1039 /********************************************************************
1040  Allocate a new RID from the passdb backend.  Verify that it is free
1041  by calling lookup_global_sam_rid() to verify that the RID is not
1042  in use.  This handles servers that have existing users or groups
1043  with add RIDs (assigned from previous algorithmic mappings)
1044 ********************************************************************/
1045
1046 bool pdb_new_rid(uint32 *rid)
1047 {
1048         struct pdb_methods *pdb = pdb_get_methods();
1049         const char *name = NULL;
1050         enum lsa_SidType type;
1051         uint32 allocated_rid = 0;
1052         int i;
1053         TALLOC_CTX *ctx;
1054
1055         if (pdb_rid_algorithm()) {
1056                 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1057                           "are active\n"));
1058                 return False;
1059         }
1060
1061         if (algorithmic_rid_base() != BASE_RID) {
1062                 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1063                           "without algorithmic RIDs is chosen.\n"));
1064                 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1065                              "add', set the maximum used RID using\n"));
1066                 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1067                 return False;
1068         }
1069
1070         if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1071                 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1072                 return False;
1073         }
1074
1075         /* Attempt to get an unused RID (max tires is 250...yes that it is 
1076            and arbitrary number I pulkled out of my head).   -- jerry */
1077
1078         for ( i=0; allocated_rid==0 && i<250; i++ ) {
1079                 /* get a new RID */
1080
1081                 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1082                         return False;
1083                 }
1084
1085                 /* validate that the RID is not in use */
1086
1087                 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1088                         allocated_rid = 0;
1089                 }
1090         }
1091
1092         TALLOC_FREE( ctx );
1093
1094         if ( allocated_rid == 0 ) {
1095                 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1096                 return False;
1097         }
1098
1099         *rid = allocated_rid;
1100
1101         return True;
1102 }
1103
1104 /***************************************************************
1105   Initialize the static context (at smbd startup etc). 
1106
1107   If uninitialised, context will auto-init on first use.
1108  ***************************************************************/
1109
1110 bool initialize_password_db(bool reload, struct event_context *event_ctx)
1111 {
1112         pdb_event_ctx = event_ctx;
1113         return (pdb_get_methods_reload(reload) != NULL);
1114 }
1115
1116
1117 /***************************************************************************
1118   Default implementations of some functions.
1119  ****************************************************************************/
1120
1121 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1122 {
1123         return NT_STATUS_NO_SUCH_USER;
1124 }
1125
1126 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1127 {
1128         return NT_STATUS_NO_SUCH_USER;
1129 }
1130
1131 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1132 {
1133         return NT_STATUS_NOT_IMPLEMENTED;
1134 }
1135
1136 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1137 {
1138         return NT_STATUS_NOT_IMPLEMENTED;
1139 }
1140
1141 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1142 {
1143         return NT_STATUS_NOT_IMPLEMENTED;
1144 }
1145
1146 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1147 {
1148         return NT_STATUS_NOT_IMPLEMENTED;
1149 }
1150
1151 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1152 {
1153         return NT_STATUS_NOT_IMPLEMENTED;
1154 }
1155
1156 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1157 {
1158         return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1159 }
1160
1161 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1162 {
1163         return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1164 }
1165
1166 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1167 {
1168         *seq_num = time(NULL);
1169         return NT_STATUS_OK;
1170 }
1171
1172 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1173                                    DOM_SID *sid)
1174 {
1175         struct samu *sampw = NULL;
1176         struct passwd *unix_pw;
1177         bool ret;
1178         
1179         unix_pw = sys_getpwuid( uid );
1180
1181         if ( !unix_pw ) {
1182                 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1183                          "%lu\n", (unsigned long)uid));
1184                 return False;
1185         }
1186         
1187         if ( !(sampw = samu_new( NULL )) ) {
1188                 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1189                 return False;
1190         }
1191
1192         become_root();
1193         ret = NT_STATUS_IS_OK(
1194                 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1195         unbecome_root();
1196
1197         if (!ret) {
1198                 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1199                           "%s (%d)\n", unix_pw->pw_name, uid));
1200                 TALLOC_FREE(sampw);
1201                 return False;
1202         }
1203
1204         sid_copy(sid, pdb_get_user_sid(sampw));
1205
1206         TALLOC_FREE(sampw);
1207
1208         return True;
1209 }
1210
1211 static bool pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1212                                    uint32 *rid)
1213 {
1214         DOM_SID sid;
1215         bool ret;
1216
1217         ret = pdb_default_uid_to_sid(methods, uid, &sid);
1218         if (!ret) {
1219                 return ret;
1220         }
1221         
1222         ret = sid_peek_check_rid(get_global_sam_sid(), &sid, rid);
1223
1224         if (!ret) {
1225                 DEBUG(1, ("Could not peek rid out of sid %s\n",
1226                           sid_string_dbg(&sid)));
1227         }
1228
1229         return ret;
1230 }
1231
1232 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1233                                    DOM_SID *sid)
1234 {
1235         GROUP_MAP map;
1236
1237         if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1238                 return False;
1239         }
1240
1241         sid_copy(sid, &map.sid);
1242         return True;
1243 }
1244
1245 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1246                                   const DOM_SID *sid,
1247                                   union unid_t *id, enum lsa_SidType *type)
1248 {
1249         TALLOC_CTX *mem_ctx;
1250         bool ret = False;
1251         const char *name;
1252         uint32 rid;
1253
1254         mem_ctx = talloc_new(NULL);
1255
1256         if (mem_ctx == NULL) {
1257                 DEBUG(0, ("talloc_new failed\n"));
1258                 return False;
1259         }
1260
1261         if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1262                 /* Here we might have users as well as groups and aliases */
1263                 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1264                 goto done;
1265         }
1266
1267         /* check for "Unix User" */
1268
1269         if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1270                 id->uid = rid;
1271                 *type = SID_NAME_USER;
1272                 ret = True;             
1273                 goto done;              
1274         }
1275         
1276         /* check for "Unix Group" */
1277
1278         if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1279                 id->gid = rid;
1280                 *type = SID_NAME_ALIAS;
1281                 ret = True;             
1282                 goto done;              
1283         }
1284         
1285         /* BUILTIN */
1286
1287         if (sid_check_is_in_builtin(sid) ||
1288             sid_check_is_in_wellknown_domain(sid)) {
1289                 /* Here we only have aliases */
1290                 GROUP_MAP map;
1291                 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1292                         DEBUG(10, ("Could not find map for sid %s\n",
1293                                    sid_string_dbg(sid)));
1294                         goto done;
1295                 }
1296                 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1297                     (map.sid_name_use != SID_NAME_WKN_GRP)) {
1298                         DEBUG(10, ("Map for sid %s is a %s, expected an "
1299                                    "alias\n", sid_string_dbg(sid),
1300                                    sid_type_lookup(map.sid_name_use)));
1301                         goto done;
1302                 }
1303
1304                 id->gid = map.gid;
1305                 *type = SID_NAME_ALIAS;
1306                 ret = True;
1307                 goto done;
1308         }
1309
1310         DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1311                   sid_string_dbg(sid)));
1312
1313  done:
1314
1315         TALLOC_FREE(mem_ctx);
1316         return ret;
1317 }
1318
1319 static bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1320                                     uid_t uid, uid_t **pp_uids, size_t *p_num)
1321 {
1322         size_t i;
1323
1324         for (i=0; i<*p_num; i++) {
1325                 if ((*pp_uids)[i] == uid)
1326                         return True;
1327         }
1328         
1329         *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1330
1331         if (*pp_uids == NULL)
1332                 return False;
1333
1334         (*pp_uids)[*p_num] = uid;
1335         *p_num += 1;
1336         return True;
1337 }
1338
1339 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1340 {
1341         struct group *grp;
1342         char **gr;
1343         struct passwd *pwd;
1344         bool winbind_env;
1345         bool ret = False;
1346  
1347         *pp_uids = NULL;
1348         *p_num = 0;
1349
1350         /* We only look at our own sam, so don't care about imported stuff */
1351         winbind_env = winbind_env_set();
1352         winbind_off();
1353
1354         if ((grp = getgrgid(gid)) == NULL) {
1355                 /* allow winbindd lookups, but only if they weren't already disabled */
1356                 goto done;
1357         }
1358
1359         /* Primary group members */
1360         setpwent();
1361         while ((pwd = getpwent()) != NULL) {
1362                 if (pwd->pw_gid == gid) {
1363                         if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1364                                                 pp_uids, p_num)) {
1365                                 goto done;
1366                         }
1367                 }
1368         }
1369         endpwent();
1370
1371         /* Secondary group members */
1372         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1373                 struct passwd *pw = getpwnam(*gr);
1374
1375                 if (pw == NULL)
1376                         continue;
1377                 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1378                         goto done;
1379                 }
1380         }
1381
1382         ret = True;
1383
1384   done:
1385
1386         /* allow winbindd lookups, but only if they weren't already disabled */
1387         if (!winbind_env) {
1388                 winbind_on();
1389         }
1390         
1391         return ret;
1392 }
1393
1394 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1395                                                TALLOC_CTX *mem_ctx,
1396                                                const DOM_SID *group,
1397                                                uint32 **pp_member_rids,
1398                                                size_t *p_num_members)
1399 {
1400         gid_t gid;
1401         uid_t *uids;
1402         size_t i, num_uids;
1403
1404         *pp_member_rids = NULL;
1405         *p_num_members = 0;
1406
1407         if (!sid_to_gid(group, &gid))
1408                 return NT_STATUS_NO_SUCH_GROUP;
1409
1410         if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1411                 return NT_STATUS_NO_SUCH_GROUP;
1412
1413         if (num_uids == 0)
1414                 return NT_STATUS_OK;
1415
1416         *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1417
1418         for (i=0; i<num_uids; i++) {
1419                 DOM_SID sid;
1420
1421                 uid_to_sid(&sid, uids[i]);
1422
1423                 if (!sid_check_is_in_our_domain(&sid)) {
1424                         DEBUG(5, ("Inconsistent SAM -- group member uid not "
1425                                   "in our domain\n"));
1426                         continue;
1427                 }
1428
1429                 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1430                 *p_num_members += 1;
1431         }
1432
1433         return NT_STATUS_OK;
1434 }
1435
1436 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1437                                                    TALLOC_CTX *mem_ctx,
1438                                                    struct samu *user,
1439                                                    DOM_SID **pp_sids,
1440                                                    gid_t **pp_gids,
1441                                                    size_t *p_num_groups)
1442 {
1443         size_t i;
1444         gid_t gid;
1445         struct passwd *pw;
1446         const char *username = pdb_get_username(user);
1447         
1448
1449         /* Ignore the primary group SID.  Honor the real Unix primary group.
1450            The primary group SID is only of real use to Windows clients */
1451            
1452         if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1453                 return NT_STATUS_NO_SUCH_USER;
1454         }
1455         
1456         gid = pw->pw_gid;
1457         
1458         TALLOC_FREE( pw );
1459
1460         if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1461                 return NT_STATUS_NO_SUCH_USER;
1462         }
1463
1464         if (*p_num_groups == 0) {
1465                 smb_panic("primary group missing");
1466         }
1467
1468         *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1469
1470         if (*pp_sids == NULL) {
1471                 TALLOC_FREE(*pp_gids);
1472                 return NT_STATUS_NO_MEMORY;
1473         }
1474
1475         for (i=0; i<*p_num_groups; i++) {
1476                 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1477         }
1478
1479         return NT_STATUS_OK;
1480 }
1481
1482 /*******************************************************************
1483  Look up a rid in the SAM we're responsible for (i.e. passdb)
1484  ********************************************************************/
1485
1486 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1487                                   const char **name,
1488                                   enum lsa_SidType *psid_name_use,
1489                                   union unid_t *unix_id)
1490 {
1491         struct samu *sam_account = NULL;
1492         GROUP_MAP map;
1493         bool ret;
1494         DOM_SID sid;
1495
1496         *psid_name_use = SID_NAME_UNKNOWN;
1497         
1498         DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1499                  (unsigned int)rid));
1500
1501         sid_copy(&sid, get_global_sam_sid());
1502         sid_append_rid(&sid, rid);
1503         
1504         /* see if the passdb can help us with the name of the user */
1505
1506         if ( !(sam_account = samu_new( NULL )) ) {
1507                 return False;
1508         }
1509
1510         /* BEING ROOT BLLOCK */
1511         become_root();
1512         if (pdb_getsampwsid(sam_account, &sid)) {
1513                 struct passwd *pw;
1514
1515                 unbecome_root();                /* -----> EXIT BECOME_ROOT() */
1516                 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1517                 if (!*name) {
1518                         TALLOC_FREE(sam_account);
1519                         return False;
1520                 }
1521
1522                 *psid_name_use = SID_NAME_USER;
1523
1524                 TALLOC_FREE(sam_account);
1525
1526                 if (unix_id == NULL) {
1527                         return True;
1528                 }
1529
1530                 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1531                 if (pw == NULL) {
1532                         return False;
1533                 }
1534                 unix_id->uid = pw->pw_uid;
1535                 TALLOC_FREE(pw);
1536                 return True;
1537         }
1538         TALLOC_FREE(sam_account);
1539         
1540         ret = pdb_getgrsid(&map, sid);
1541         unbecome_root();
1542         /* END BECOME_ROOT BLOCK */
1543   
1544         /* do not resolve SIDs to a name unless there is a valid 
1545            gid associated with it */
1546                    
1547         if ( ret && (map.gid != (gid_t)-1) ) {
1548                 *name = talloc_strdup(mem_ctx, map.nt_name);
1549                 *psid_name_use = map.sid_name_use;
1550
1551                 if ( unix_id ) {
1552                         unix_id->gid = map.gid;
1553                 }
1554
1555                 return True;
1556         }
1557         
1558         /* Windows will always map RID 513 to something.  On a non-domain 
1559            controller, this gets mapped to SERVER\None. */
1560
1561         if ( unix_id ) {
1562                 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1563                 return False;
1564         }
1565         
1566         if ( rid == DOMAIN_GROUP_RID_USERS ) {
1567                 *name = talloc_strdup(mem_ctx, "None" );
1568                 *psid_name_use = SID_NAME_DOM_GRP;
1569                 
1570                 return True;
1571         }
1572
1573         return False;
1574 }
1575
1576 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1577                                         const DOM_SID *domain_sid,
1578                                         int num_rids,
1579                                         uint32 *rids,
1580                                         const char **names,
1581                                         enum lsa_SidType *attrs)
1582 {
1583         int i;
1584         NTSTATUS result;
1585         bool have_mapped = False;
1586         bool have_unmapped = False;
1587
1588         if (sid_check_is_builtin(domain_sid)) {
1589
1590                 for (i=0; i<num_rids; i++) {
1591                         const char *name;
1592
1593                         if (lookup_builtin_rid(names, rids[i], &name)) {
1594                                 attrs[i] = SID_NAME_ALIAS;
1595                                 names[i] = name;
1596                                 DEBUG(5,("lookup_rids: %s:%d\n",
1597                                          names[i], attrs[i]));
1598                                 have_mapped = True;
1599                         } else {
1600                                 have_unmapped = True;
1601                                 attrs[i] = SID_NAME_UNKNOWN;
1602                         }
1603                 }
1604                 goto done;
1605         }
1606
1607         /* Should not happen, but better check once too many */
1608         if (!sid_check_is_domain(domain_sid)) {
1609                 return NT_STATUS_INVALID_HANDLE;
1610         }
1611
1612         for (i = 0; i < num_rids; i++) {
1613                 const char *name;
1614
1615                 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1616                                           NULL)) {
1617                         if (name == NULL) {
1618                                 return NT_STATUS_NO_MEMORY;
1619                         }
1620                         names[i] = name;
1621                         DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1622                         have_mapped = True;
1623                 } else {
1624                         have_unmapped = True;
1625                         attrs[i] = SID_NAME_UNKNOWN;
1626                 }
1627         }
1628
1629  done:
1630
1631         result = NT_STATUS_NONE_MAPPED;
1632
1633         if (have_mapped)
1634                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1635
1636         return result;
1637 }
1638
1639 #if 0
1640 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1641                                          const DOM_SID *domain_sid,
1642                                          int num_names,
1643                                          const char **names,
1644                                          uint32 *rids,
1645                                          enum lsa_SidType *attrs)
1646 {
1647         int i;
1648         NTSTATUS result;
1649         bool have_mapped = False;
1650         bool have_unmapped = False;
1651
1652         if (sid_check_is_builtin(domain_sid)) {
1653
1654                 for (i=0; i<num_names; i++) {
1655                         uint32 rid;
1656
1657                         if (lookup_builtin_name(names[i], &rid)) {
1658                                 attrs[i] = SID_NAME_ALIAS;
1659                                 rids[i] = rid;
1660                                 DEBUG(5,("lookup_rids: %s:%d\n",
1661                                          names[i], attrs[i]));
1662                                 have_mapped = True;
1663                         } else {
1664                                 have_unmapped = True;
1665                                 attrs[i] = SID_NAME_UNKNOWN;
1666                         }
1667                 }
1668                 goto done;
1669         }
1670
1671         /* Should not happen, but better check once too many */
1672         if (!sid_check_is_domain(domain_sid)) {
1673                 return NT_STATUS_INVALID_HANDLE;
1674         }
1675
1676         for (i = 0; i < num_names; i++) {
1677                 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1678                         DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1679                                  rids[i], attrs[i]));
1680                         have_mapped = True;
1681                 } else {
1682                         have_unmapped = True;
1683                         attrs[i] = SID_NAME_UNKNOWN;
1684                 }
1685         }
1686
1687  done:
1688
1689         result = NT_STATUS_NONE_MAPPED;
1690
1691         if (have_mapped)
1692                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1693
1694         return result;
1695 }
1696 #endif
1697
1698 struct pdb_search *pdb_search_init(enum pdb_search_type type)
1699 {
1700         TALLOC_CTX *mem_ctx;
1701         struct pdb_search *result;
1702
1703         mem_ctx = talloc_init("pdb_search");
1704         if (mem_ctx == NULL) {
1705                 DEBUG(0, ("talloc_init failed\n"));
1706                 return NULL;
1707         }
1708
1709         result = TALLOC_P(mem_ctx, struct pdb_search);
1710         if (result == NULL) {
1711                 DEBUG(0, ("talloc failed\n"));
1712                 return NULL;
1713         }
1714
1715         result->mem_ctx = mem_ctx;
1716         result->type = type;
1717         result->cache = NULL;
1718         result->num_entries = 0;
1719         result->cache_size = 0;
1720         result->search_ended = False;
1721
1722         /* Segfault appropriately if not initialized */
1723         result->next_entry = NULL;
1724         result->search_end = NULL;
1725
1726         return result;
1727 }
1728
1729 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1730                               uint16 acct_flags,
1731                               const char *account_name,
1732                               const char *fullname,
1733                               const char *description,
1734                               struct samr_displayentry *entry)
1735 {
1736         entry->rid = rid;
1737         entry->acct_flags = acct_flags;
1738
1739         if (account_name != NULL)
1740                 entry->account_name = talloc_strdup(mem_ctx, account_name);
1741         else
1742                 entry->account_name = "";
1743
1744         if (fullname != NULL)
1745                 entry->fullname = talloc_strdup(mem_ctx, fullname);
1746         else
1747                 entry->fullname = "";
1748
1749         if (description != NULL)
1750                 entry->description = talloc_strdup(mem_ctx, description);
1751         else
1752                 entry->description = "";
1753 }
1754
1755 struct group_search {
1756         GROUP_MAP *groups;
1757         size_t num_groups, current_group;
1758 };
1759
1760 static bool next_entry_groups(struct pdb_search *s,
1761                               struct samr_displayentry *entry)
1762 {
1763         struct group_search *state = (struct group_search *)s->private_data;
1764         uint32 rid;
1765         GROUP_MAP *map = &state->groups[state->current_group];
1766
1767         if (state->current_group == state->num_groups)
1768                 return False;
1769
1770         sid_peek_rid(&map->sid, &rid);
1771
1772         fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1773                           entry);
1774
1775         state->current_group += 1;
1776         return True;
1777 }
1778
1779 static void search_end_groups(struct pdb_search *search)
1780 {
1781         struct group_search *state =
1782                 (struct group_search *)search->private_data;
1783         SAFE_FREE(state->groups);
1784 }
1785
1786 static bool pdb_search_grouptype(struct pdb_search *search,
1787                                  const DOM_SID *sid, enum lsa_SidType type)
1788 {
1789         struct group_search *state;
1790
1791         state = TALLOC_P(search->mem_ctx, struct group_search);
1792         if (state == NULL) {
1793                 DEBUG(0, ("talloc failed\n"));
1794                 return False;
1795         }
1796
1797         if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1798                                     True)) {
1799                 DEBUG(0, ("Could not enum groups\n"));
1800                 return False;
1801         }
1802
1803         state->current_group = 0;
1804         search->private_data = state;
1805         search->next_entry = next_entry_groups;
1806         search->search_end = search_end_groups;
1807         return True;
1808 }
1809
1810 static bool pdb_default_search_groups(struct pdb_methods *methods,
1811                                       struct pdb_search *search)
1812 {
1813         return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1814 }
1815
1816 static bool pdb_default_search_aliases(struct pdb_methods *methods,
1817                                        struct pdb_search *search,
1818                                        const DOM_SID *sid)
1819 {
1820
1821         return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1822 }
1823
1824 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1825                                                      uint32 idx)
1826 {
1827         if (idx < search->num_entries)
1828                 return &search->cache[idx];
1829
1830         if (search->search_ended)
1831                 return NULL;
1832
1833         while (idx >= search->num_entries) {
1834                 struct samr_displayentry entry;
1835
1836                 if (!search->next_entry(search, &entry)) {
1837                         search->search_end(search);
1838                         search->search_ended = True;
1839                         break;
1840                 }
1841
1842                 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1843                                    entry, &search->cache, &search->num_entries,
1844                                    &search->cache_size);
1845         }
1846
1847         return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1848 }
1849
1850 struct pdb_search *pdb_search_users(uint32 acct_flags)
1851 {
1852         struct pdb_methods *pdb = pdb_get_methods();
1853         struct pdb_search *result;
1854
1855         result = pdb_search_init(PDB_USER_SEARCH);
1856         if (result == NULL) {
1857                 return NULL;
1858         }
1859
1860         if (!pdb->search_users(pdb, result, acct_flags)) {
1861                 talloc_destroy(result->mem_ctx);
1862                 return NULL;
1863         }
1864         return result;
1865 }
1866
1867 struct pdb_search *pdb_search_groups(void)
1868 {
1869         struct pdb_methods *pdb = pdb_get_methods();
1870         struct pdb_search *result;
1871
1872         result = pdb_search_init(PDB_GROUP_SEARCH);
1873         if (result == NULL) {
1874                  return NULL;
1875         }
1876
1877         if (!pdb->search_groups(pdb, result)) {
1878                 talloc_destroy(result->mem_ctx);
1879                 return NULL;
1880         }
1881         return result;
1882 }
1883
1884 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1885 {
1886         struct pdb_methods *pdb = pdb_get_methods();
1887         struct pdb_search *result;
1888
1889         if (pdb == NULL) return NULL;
1890
1891         result = pdb_search_init(PDB_ALIAS_SEARCH);
1892         if (result == NULL) return NULL;
1893
1894         if (!pdb->search_aliases(pdb, result, sid)) {
1895                 talloc_destroy(result->mem_ctx);
1896                 return NULL;
1897         }
1898         return result;
1899 }
1900
1901 uint32 pdb_search_entries(struct pdb_search *search,
1902                           uint32 start_idx, uint32 max_entries,
1903                           struct samr_displayentry **result)
1904 {
1905         struct samr_displayentry *end_entry;
1906         uint32 end_idx = start_idx+max_entries-1;
1907
1908         /* The first entry needs to be searched after the last. Otherwise the
1909          * first entry might have moved due to a realloc during the search for
1910          * the last entry. */
1911
1912         end_entry = pdb_search_getentry(search, end_idx);
1913         *result = pdb_search_getentry(search, start_idx);
1914
1915         if (end_entry != NULL)
1916                 return max_entries;
1917
1918         if (start_idx >= search->num_entries)
1919                 return 0;
1920
1921         return search->num_entries - start_idx;
1922 }
1923
1924 void pdb_search_destroy(struct pdb_search *search)
1925 {
1926         if (search == NULL)
1927                 return;
1928
1929         if (!search->search_ended)
1930                 search->search_end(search);
1931
1932         talloc_destroy(search->mem_ctx);
1933 }
1934
1935 /*******************************************************************
1936  trustodm methods
1937  *******************************************************************/
1938
1939 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, DOM_SID *sid, 
1940                            time_t *pass_last_set_time)
1941 {
1942         struct pdb_methods *pdb = pdb_get_methods();
1943         return pdb->get_trusteddom_pw(pdb, domain, pwd, sid, 
1944                         pass_last_set_time);
1945 }
1946
1947 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
1948                            const DOM_SID *sid)
1949 {
1950         struct pdb_methods *pdb = pdb_get_methods();
1951         return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
1952 }
1953
1954 bool pdb_del_trusteddom_pw(const char *domain)
1955 {
1956         struct pdb_methods *pdb = pdb_get_methods();
1957         return pdb->del_trusteddom_pw(pdb, domain);
1958 }
1959
1960 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32 *num_domains,
1961                               struct trustdom_info ***domains)
1962 {
1963         struct pdb_methods *pdb = pdb_get_methods();
1964         return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
1965 }
1966
1967 /*******************************************************************
1968  the defaults for trustdom methods: 
1969  these simply call the original passdb/secrets.c actions,
1970  to be replaced by pdb_ldap.
1971  *******************************************************************/
1972
1973 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
1974                                           const char *domain, 
1975                                           char** pwd, 
1976                                           DOM_SID *sid, 
1977                                           time_t *pass_last_set_time)
1978 {
1979         return secrets_fetch_trusted_domain_password(domain, pwd,
1980                                 sid, pass_last_set_time);
1981
1982 }
1983
1984 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods, 
1985                                           const char* domain, 
1986                                           const char* pwd,
1987                                           const DOM_SID *sid)
1988 {
1989         return secrets_store_trusted_domain_password(domain, pwd, sid);
1990 }
1991
1992 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods, 
1993                                           const char *domain)
1994 {
1995         return trusted_domain_password_delete(domain);
1996 }
1997
1998 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
1999                                              TALLOC_CTX *mem_ctx, 
2000                                              uint32 *num_domains,
2001                                              struct trustdom_info ***domains)
2002 {
2003         return secrets_trusted_domains(mem_ctx, num_domains, domains);
2004 }
2005
2006 /*******************************************************************
2007  Create a pdb_methods structure and initialize it with the default
2008  operations.  In this way a passdb module can simply implement
2009  the functionality it cares about.  However, normally this is done 
2010  in groups of related functions.
2011 *******************************************************************/
2012
2013 NTSTATUS make_pdb_method( struct pdb_methods **methods ) 
2014 {
2015         /* allocate memory for the structure as its own talloc CTX */
2016
2017         if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
2018                 return NT_STATUS_NO_MEMORY;
2019         }
2020
2021         (*methods)->getsampwnam = pdb_default_getsampwnam;
2022         (*methods)->getsampwsid = pdb_default_getsampwsid;
2023         (*methods)->create_user = pdb_default_create_user;
2024         (*methods)->delete_user = pdb_default_delete_user;
2025         (*methods)->add_sam_account = pdb_default_add_sam_account;
2026         (*methods)->update_sam_account = pdb_default_update_sam_account;
2027         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2028         (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2029         (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2030
2031         (*methods)->getgrsid = pdb_default_getgrsid;
2032         (*methods)->getgrgid = pdb_default_getgrgid;
2033         (*methods)->getgrnam = pdb_default_getgrnam;
2034         (*methods)->create_dom_group = pdb_default_create_dom_group;
2035         (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2036         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2037         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2038         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2039         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2040         (*methods)->enum_group_members = pdb_default_enum_group_members;
2041         (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2042         (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2043         (*methods)->add_groupmem = pdb_default_add_groupmem;
2044         (*methods)->del_groupmem = pdb_default_del_groupmem;
2045         (*methods)->create_alias = pdb_default_create_alias;
2046         (*methods)->delete_alias = pdb_default_delete_alias;
2047         (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2048         (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2049         (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2050         (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2051         (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2052         (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2053         (*methods)->lookup_rids = pdb_default_lookup_rids;
2054         (*methods)->get_account_policy = pdb_default_get_account_policy;
2055         (*methods)->set_account_policy = pdb_default_set_account_policy;
2056         (*methods)->get_seq_num = pdb_default_get_seq_num;
2057         (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2058         (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2059         (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2060         (*methods)->sid_to_id = pdb_default_sid_to_id;
2061
2062         (*methods)->search_groups = pdb_default_search_groups;
2063         (*methods)->search_aliases = pdb_default_search_aliases;
2064
2065         (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2066         (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2067         (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2068         (*methods)->enum_trusteddoms  = pdb_default_enum_trusteddoms;
2069
2070         return NT_STATUS_OK;
2071 }