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