79588477ba7686c15926ae28bee195231de13124
[ddiss/samba.git] / source3 / nsswitch / winbindd_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon for ntdom nss module
5
6    Copyright (C) Tim Potter 2000-2001
7    Copyright (C) 2001 by Martin Pool <mbp@samba.org>
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 "winbindd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29 /**
30  * @file winbindd_util.c
31  *
32  * Winbind daemon for NT domain authentication nss module.
33  **/
34
35
36 /**
37  * Used to clobber name fields that have an undefined value.
38  *
39  * Correct code should never look at a field that has this value.
40  **/
41
42 static const fstring name_deadbeef = "<deadbeef>";
43
44 /* The list of trusted domains.  Note that the list can be deleted and
45    recreated using the init_domain_list() function so pointers to
46    individual winbindd_domain structures cannot be made.  Keep a copy of
47    the domain name instead. */
48
49 static struct winbindd_domain *_domain_list;
50
51 struct winbindd_domain *domain_list(void)
52 {
53         /* Initialise list */
54
55         if (!_domain_list)
56                 init_domain_list();
57
58         return _domain_list;
59 }
60
61 /* Free all entries in the trusted domain list */
62
63 void free_domain_list(void)
64 {
65         struct winbindd_domain *domain = _domain_list;
66
67         while(domain) {
68                 struct winbindd_domain *next = domain->next;
69                 
70                 DLIST_REMOVE(_domain_list, domain);
71                 SAFE_FREE(domain);
72                 domain = next;
73         }
74 }
75
76
77 /* Add a trusted domain to our list of domains */
78 static struct winbindd_domain *add_trusted_domain(const char *domain_name, const char *alt_name,
79                                                   struct winbindd_methods *methods,
80                                                   DOM_SID *sid)
81 {
82         struct winbindd_domain *domain;
83         char *contact_name;
84         
85         /* We can't call domain_list() as this function is called from
86            init_domain_list() and we'll get stuck in a loop. */
87         for (domain = _domain_list; domain; domain = domain->next) {
88                 if (strcasecmp(domain_name, domain->name) == 0 ||
89                     strcasecmp(domain_name, domain->alt_name) == 0) {
90                         return domain;
91                 }
92                 if (alt_name && *alt_name) {
93                         if (strcasecmp(alt_name, domain->name) == 0 ||
94                             strcasecmp(alt_name, domain->alt_name) == 0) {
95                                 return domain;
96                         }
97                 }
98         }
99         
100         /* Create new domain entry */
101
102         if ((domain = (struct winbindd_domain *)
103              malloc(sizeof(*domain))) == NULL)
104                 return NULL;
105
106         /* Fill in fields */
107         
108         ZERO_STRUCTP(domain);
109
110         /* prioritise the short name */
111         if (strchr_m(domain_name, '.') && alt_name && *alt_name) {
112                 fstrcpy(domain->name, alt_name);
113                 fstrcpy(domain->alt_name, domain_name);
114         } else {
115                 fstrcpy(domain->name, domain_name);
116                 if (alt_name) {
117                         fstrcpy(domain->alt_name, alt_name);
118                 }
119         }
120
121         domain->methods = methods;
122         domain->backend = NULL;
123         domain->sequence_number = DOM_SEQUENCE_NONE;
124         domain->last_seq_check = 0;
125         if (sid) {
126                 sid_copy(&domain->sid, sid);
127         }
128         
129         /* see if this is a native mode win2k domain (use realm name if possible) */
130            
131         contact_name = *domain->alt_name ? domain->alt_name : domain->name;
132         domain->native_mode = cm_check_for_native_mode_win2k( contact_name );
133         
134         DEBUG(3,("add_trusted_domain: %s is a %s mode domain\n", contact_name,
135                 domain->native_mode ? "native" : "mixed (or NT4)" ));
136
137         /* Link to domain list */
138         DLIST_ADD(_domain_list, domain);
139         
140         DEBUG(1,("Added domain %s %s %s\n", 
141                  domain->name, domain->alt_name,
142                  sid?sid_string_static(&domain->sid):""));
143         
144         return domain;
145 }
146
147
148 /*
149   rescan our domains looking for new trusted domains
150  */
151 void add_trusted_domains( struct winbindd_domain *domain )
152 {
153         TALLOC_CTX *mem_ctx;
154         NTSTATUS result;
155         time_t t;
156         char **names;
157         char **alt_names;
158         int num_domains = 0;
159         DOM_SID *dom_sids, null_sid;
160         int i;
161         struct winbindd_domain *new_domain;
162
163         /* trusted domains might be disabled */
164         if (!lp_allow_trusted_domains()) {
165                 return;
166         }
167
168         DEBUG(1, ("scanning trusted domain list\n"));
169
170         if (!(mem_ctx = talloc_init("init_domain_list")))
171                 return;
172            
173         ZERO_STRUCTP(&null_sid);
174
175         t = time(NULL);
176         
177         /* ask the DC what domains it trusts */
178         
179         result = domain->methods->trusted_domains(domain, mem_ctx, &num_domains,
180                 &names, &alt_names, &dom_sids);
181                 
182         if ( NT_STATUS_IS_OK(result) ) {
183
184                 /* Add each domain to the trusted domain list */
185                 
186                 for(i = 0; i < num_domains; i++) {
187                         DEBUG(10,("Found domain %s\n", names[i]));
188                         add_trusted_domain(names[i], alt_names?alt_names[i]:NULL,
189                                 domain->methods, &dom_sids[i]);
190                                            
191                         /* if the SID was empty, we better set it now */
192                         
193                         if ( sid_equal(&dom_sids[i], &null_sid) ) {
194                         
195                                 new_domain = find_domain_from_name(names[i]);
196                                  
197                                 /* this should never happen */
198                                 if ( !new_domain ) {    
199                                         DEBUG(0,("rescan_trust_domains: can't find the domain I just added! [%s]\n",
200                                                 names[i]));
201                                         break;
202                                 }
203                                  
204                                 /* call the cache method; which will operate on the winbindd_domain \
205                                    passed in and choose either rpc or ads as appropriate */
206
207                                 result = domain->methods->domain_sid( new_domain, &new_domain->sid );
208                                  
209                                 if ( NT_STATUS_IS_OK(result) )
210                                         sid_copy( &dom_sids[i], &new_domain->sid );
211                         }
212                         
213                         /* store trusted domain in the cache */
214                         trustdom_cache_store(names[i], alt_names ? alt_names[i] : NULL,
215                                              &dom_sids[i], t + WINBINDD_RESCAN_FREQ);
216                 }
217         }
218
219         talloc_destroy(mem_ctx);
220 }
221
222 /* Look up global info for the winbind daemon */
223 BOOL init_domain_list(void)
224 {
225         extern struct winbindd_methods cache_methods;
226         struct winbindd_domain *domain;
227
228         /* Free existing list */
229         free_domain_list();
230
231         /* Add ourselves as the first entry */
232         
233         domain = add_trusted_domain( lp_workgroup(), NULL, &cache_methods, NULL);
234         
235         if (!secrets_fetch_domain_sid(domain->name, &domain->sid)) {
236                 DEBUG(1, ("Could not fetch sid for our domain %s\n",
237                           domain->name));
238                 return False;
239         }
240
241         /* get any alternate name for the primary domain */
242         cache_methods.alternate_name(domain);
243
244         /* do an initial scan for trusted domains */
245         add_trusted_domains(domain);
246
247         return True;
248 }
249
250 /* Given a domain name, return the struct winbindd domain info for it 
251    if it is actually working. */
252
253 struct winbindd_domain *find_domain_from_name(const char *domain_name)
254 {
255         struct winbindd_domain *domain;
256
257         /* Search through list */
258
259         for (domain = domain_list(); domain != NULL; domain = domain->next) {
260                 if (strequal(domain_name, domain->name) ||
261                     (domain->alt_name[0] && strequal(domain_name, domain->alt_name)))
262                         return domain;
263         }
264
265         /* Not found */
266
267         return NULL;
268 }
269
270 /* Given a domain sid, return the struct winbindd domain info for it */
271
272 struct winbindd_domain *find_domain_from_sid(DOM_SID *sid)
273 {
274         struct winbindd_domain *domain;
275
276         /* Search through list */
277
278         for (domain = domain_list(); domain != NULL; domain = domain->next) {
279                 if (sid_compare_domain(sid, &domain->sid) == 0)
280                         return domain;
281         }
282
283         /* Not found */
284
285         return NULL;
286 }
287
288 /* Lookup a sid in a domain from a name */
289
290 BOOL winbindd_lookup_sid_by_name(struct winbindd_domain *domain, 
291                                  const char *name, DOM_SID *sid, 
292                                  enum SID_NAME_USE *type)
293 {
294         NTSTATUS result;
295         TALLOC_CTX *mem_ctx;
296         /* Don't bother with machine accounts */
297
298         if (name[strlen(name) - 1] == '$')
299                 return False;
300
301         mem_ctx = talloc_init("lookup_sid_by_name for %s\n", name);
302         if (!mem_ctx) 
303                 return False;
304         
305         /* Lookup name */
306         result = domain->methods->name_to_sid(domain, mem_ctx, name, sid, type);
307
308         talloc_destroy(mem_ctx);
309         
310         /* Return rid and type if lookup successful */
311         if (!NT_STATUS_IS_OK(result)) {
312                 *type = SID_NAME_UNKNOWN;
313         }
314
315         return NT_STATUS_IS_OK(result);
316 }
317
318 /**
319  * @brief Lookup a name in a domain from a sid.
320  *
321  * @param sid Security ID you want to look up.
322  * @param name On success, set to the name corresponding to @p sid.
323  * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
324  * @param type On success, contains the type of name: alias, group or
325  * user.
326  * @retval True if the name exists, in which case @p name and @p type
327  * are set, otherwise False.
328  **/
329 BOOL winbindd_lookup_name_by_sid(DOM_SID *sid,
330                                  fstring dom_name,
331                                  fstring name,
332                                  enum SID_NAME_USE *type)
333 {
334         char *names;
335         NTSTATUS result;
336         TALLOC_CTX *mem_ctx;
337         BOOL rv = False;
338         struct winbindd_domain *domain;
339
340         domain = find_domain_from_sid(sid);
341
342         if (!domain) {
343                 DEBUG(1,("Can't find domain from sid\n"));
344                 return False;
345         }
346
347         /* Lookup name */
348
349         if (!(mem_ctx = talloc_init("winbindd_lookup_name_by_sid")))
350                 return False;
351         
352         result = domain->methods->sid_to_name(domain, mem_ctx, sid, &names, type);
353
354         /* Return name and type if successful */
355         
356         if ((rv = NT_STATUS_IS_OK(result))) {
357                 fstrcpy(dom_name, domain->name);
358                 fstrcpy(name, names);
359         } else {
360                 *type = SID_NAME_UNKNOWN;
361                 fstrcpy(name, name_deadbeef);
362         }
363         
364         talloc_destroy(mem_ctx);
365
366         return rv;
367 }
368
369
370 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
371
372 void free_getent_state(struct getent_state *state)
373 {
374         struct getent_state *temp;
375
376         /* Iterate over state list */
377
378         temp = state;
379
380         while(temp != NULL) {
381                 struct getent_state *next;
382
383                 /* Free sam entries then list entry */
384
385                 SAFE_FREE(state->sam_entries);
386                 DLIST_REMOVE(state, state);
387                 next = temp->next;
388
389                 SAFE_FREE(temp);
390                 temp = next;
391         }
392 }
393
394 /* Parse winbindd related parameters */
395
396 BOOL winbindd_param_init(void)
397 {
398         /* Parse winbind uid and winbind_gid parameters */
399
400         if (!lp_idmap_uid(&server_state.uid_low, &server_state.uid_high)) {
401                 DEBUG(0, ("winbindd: idmap uid range missing or invalid\n"));
402                 DEBUG(0, ("winbindd: cannot continue, exiting.\n"));
403                 return False;
404         }
405         
406         if (!lp_idmap_gid(&server_state.gid_low, &server_state.gid_high)) {
407                 DEBUG(0, ("winbindd: idmap gid range missing or invalid\n"));
408                 DEBUG(0, ("winbindd: cannot continue, exiting.\n"));
409                 return False;
410         }
411         
412         return True;
413 }
414
415 /* Check if a domain is present in a comma-separated list of domains */
416
417 BOOL check_domain_env(char *domain_env, char *domain)
418 {
419         fstring name;
420         const char *tmp = domain_env;
421
422         while(next_token(&tmp, name, ",", sizeof(fstring))) {
423                 if (strequal(name, domain))
424                         return True;
425         }
426
427         return False;
428 }
429
430 /* Parse a string of the form DOMAIN/user into a domain and a user */
431
432 BOOL parse_domain_user(const char *domuser, fstring domain, fstring user)
433 {
434         char *p = strchr(domuser,*lp_winbind_separator());
435
436         if ( !p ) {
437                 fstrcpy(user, domuser);
438                 
439                 if ( lp_winbind_use_default_domain() )
440                         fstrcpy(domain, lp_workgroup());
441                 else
442                         fstrcpy( domain, "" );
443         } 
444         else {
445                 fstrcpy(user, p+1);
446                 fstrcpy(domain, domuser);
447                 domain[PTR_DIFF(p, domuser)] = 0;
448         }
449         
450         strupper_m(domain);
451         
452         return True;
453 }
454
455 /*
456     Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
457     'winbind separator' options.
458     This means:
459         - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
460         lp_workgroup
461          
462 */
463 void fill_domain_username(fstring name, const char *domain, const char *user)
464 {
465         if(lp_winbind_use_default_domain() &&
466             !strcmp(lp_workgroup(), domain)) {
467                 strlcpy(name, user, sizeof(fstring));
468         } else {
469                 slprintf(name, sizeof(fstring) - 1, "%s%s%s",
470                          domain, lp_winbind_separator(),
471                          user);
472         }
473 }
474
475 /*
476  * Winbindd socket accessor functions
477  */
478
479 char *get_winbind_priv_pipe_dir(void) 
480 {
481         return lock_path(WINBINDD_PRIV_SOCKET_SUBDIR);
482 }
483
484 /* Open the winbindd socket */
485
486 static int _winbindd_socket = -1;
487 static int _winbindd_priv_socket = -1;
488
489 int open_winbindd_socket(void)
490 {
491         if (_winbindd_socket == -1) {
492                 _winbindd_socket = create_pipe_sock(
493                         WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME, 0755);
494                 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
495                            _winbindd_socket));
496         }
497
498         return _winbindd_socket;
499 }
500
501 int open_winbindd_priv_socket(void)
502 {
503         if (_winbindd_priv_socket == -1) {
504                 _winbindd_priv_socket = create_pipe_sock(
505                         get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
506                 DEBUG(10, ("open_winbindd_priv_socket: opened socket fd %d\n",
507                            _winbindd_priv_socket));
508         }
509
510         return _winbindd_priv_socket;
511 }
512
513 /* Close the winbindd socket */
514
515 void close_winbindd_socket(void)
516 {
517         if (_winbindd_socket != -1) {
518                 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
519                            _winbindd_socket));
520                 close(_winbindd_socket);
521                 _winbindd_socket = -1;
522         }
523         if (_winbindd_priv_socket != -1) {
524                 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
525                            _winbindd_priv_socket));
526                 close(_winbindd_priv_socket);
527                 _winbindd_priv_socket = -1;
528         }
529 }
530
531 /*
532  * Client list accessor functions
533  */
534
535 static struct winbindd_cli_state *_client_list;
536 static int _num_clients;
537
538 /* Return list of all connected clients */
539
540 struct winbindd_cli_state *winbindd_client_list(void)
541 {
542         return _client_list;
543 }
544
545 /* Add a connection to the list */
546
547 void winbindd_add_client(struct winbindd_cli_state *cli)
548 {
549         DLIST_ADD(_client_list, cli);
550         _num_clients++;
551 }
552
553 /* Remove a client from the list */
554
555 void winbindd_remove_client(struct winbindd_cli_state *cli)
556 {
557         DLIST_REMOVE(_client_list, cli);
558         _num_clients--;
559 }
560
561 /* Close all open clients */
562
563 void winbindd_kill_all_clients(void)
564 {
565         struct winbindd_cli_state *cl = winbindd_client_list();
566
567         DEBUG(10, ("winbindd_kill_all_clients: going postal\n"));
568
569         while (cl) {
570                 struct winbindd_cli_state *next;
571                 
572                 next = cl->next;
573                 winbindd_remove_client(cl);
574                 cl = next;
575         }
576 }
577
578 /* Return number of open clients */
579
580 int winbindd_num_clients(void)
581 {
582         return _num_clients;
583 }
584
585 /* Help with RID -> SID conversion */
586
587 DOM_SID *rid_to_talloced_sid(struct winbindd_domain *domain,
588                                     TALLOC_CTX *mem_ctx,
589                                     uint32 rid) 
590 {
591         DOM_SID *sid;
592         sid = talloc(mem_ctx, sizeof(*sid));
593         if (!sid) {
594                 smb_panic("rid_to_to_talloced_sid: talloc for DOM_SID failed!\n");
595         }
596         sid_copy(sid, &domain->sid);
597         sid_append_rid(sid, rid);
598         return sid;
599 }
600         
601 /*****************************************************************************
602  For idmap conversion: convert one record to new format
603  Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
604  instead of the SID.
605 *****************************************************************************/
606 static int convert_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data, void *state)
607 {
608         struct winbindd_domain *domain;
609         char *p;
610         DOM_SID sid;
611         uint32 rid;
612         fstring keystr;
613         fstring dom_name;
614         TDB_DATA key2;
615         BOOL *failed = (BOOL *)state;
616
617         DEBUG(10,("Converting %s\n", key.dptr));
618
619         p = strchr(key.dptr, '/');
620         if (!p)
621                 return 0;
622
623         *p = 0;
624         fstrcpy(dom_name, key.dptr);
625         *p++ = '/';
626
627         domain = find_domain_from_name(dom_name);
628         if (domain == NULL) {
629                 /* We must delete the old record. */
630                 DEBUG(0,("Unable to find domain %s\n", dom_name ));
631                 DEBUG(0,("deleting record %s\n", key.dptr ));
632
633                 if (tdb_delete(tdb, key) != 0) {
634                         DEBUG(0, ("Unable to delete record %s\n", key.dptr));
635                         *failed = True;
636                         return -1;
637                 }
638
639                 return 0;
640         }
641
642         rid = atoi(p);
643
644         sid_copy(&sid, &domain->sid);
645         sid_append_rid(&sid, rid);
646
647         sid_to_string(keystr, &sid);
648         key2.dptr = keystr;
649         key2.dsize = strlen(keystr) + 1;
650
651         if (tdb_store(tdb, key2, data, TDB_INSERT) != 0) {
652                 DEBUG(0,("Unable to add record %s\n", key2.dptr ));
653                 *failed = True;
654                 return -1;
655         }
656
657         if (tdb_store(tdb, data, key2, TDB_REPLACE) != 0) {
658                 DEBUG(0,("Unable to update record %s\n", data.dptr ));
659                 *failed = True;
660                 return -1;
661         }
662
663         if (tdb_delete(tdb, key) != 0) {
664                 DEBUG(0,("Unable to delete record %s\n", key.dptr ));
665                 *failed = True;
666                 return -1;
667         }
668
669         return 0;
670 }
671
672 /* These definitions are from sam/idmap_tdb.c. Replicated here just
673    out of laziness.... :-( */
674
675 /* High water mark keys */
676 #define HWM_GROUP  "GROUP HWM"
677 #define HWM_USER   "USER HWM"
678
679 /* idmap version determines auto-conversion */
680 #define IDMAP_VERSION 2
681
682
683 /*****************************************************************************
684  Convert the idmap database from an older version.
685 *****************************************************************************/
686
687 static BOOL idmap_convert(const char *idmap_name)
688 {
689         int32 vers;
690         BOOL bigendianheader;
691         BOOL failed = False;
692         TDB_CONTEXT *idmap_tdb;
693
694         if (!(idmap_tdb = tdb_open_log(idmap_name, 0,
695                                         TDB_DEFAULT, O_RDWR,
696                                         0600))) {
697                 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
698                 return False;
699         }
700
701         bigendianheader = (idmap_tdb->flags & TDB_BIGENDIAN) ? True : False;
702
703         vers = tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION");
704
705         if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
706                 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
707                 /*
708                  * high and low records were created on a
709                  * big endian machine and will need byte-reversing.
710                  */
711
712                 int32 wm;
713
714                 wm = tdb_fetch_int32(idmap_tdb, HWM_USER);
715
716                 if (wm != -1) {
717                         wm = IREV(wm);
718                 }  else {
719                         wm = server_state.uid_low;
720                 }
721
722                 if (tdb_store_int32(idmap_tdb, HWM_USER, wm) == -1) {
723                         DEBUG(0, ("idmap_convert: Unable to byteswap user hwm in idmap database\n"));
724                         tdb_close(idmap_tdb);
725                         return False;
726                 }
727
728                 wm = tdb_fetch_int32(idmap_tdb, HWM_GROUP);
729                 if (wm != -1) {
730                         wm = IREV(wm);
731                 } else {
732                         wm = server_state.gid_low;
733                 }
734
735                 if (tdb_store_int32(idmap_tdb, HWM_GROUP, wm) == -1) {
736                         DEBUG(0, ("idmap_convert: Unable to byteswap group hwm in idmap database\n"));
737                         tdb_close(idmap_tdb);
738                         return False;
739                 }
740         }
741
742         /* the old format stored as DOMAIN/rid - now we store the SID direct */
743         tdb_traverse(idmap_tdb, convert_fn, &failed);
744
745         if (failed) {
746                 DEBUG(0, ("Problem during conversion\n"));
747                 tdb_close(idmap_tdb);
748                 return False;
749         }
750
751         if (tdb_store_int32(idmap_tdb, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
752                 DEBUG(0, ("idmap_convert: Unable to dtore idmap version in databse\n"));
753                 tdb_close(idmap_tdb);
754                 return False;
755         }
756
757         tdb_close(idmap_tdb);
758         return True;
759 }
760
761 /*****************************************************************************
762  Convert the idmap database from an older version if necessary
763 *****************************************************************************/
764
765 BOOL winbindd_upgrade_idmap(void)
766 {
767         pstring idmap_name;
768         pstring backup_name;
769         SMB_STRUCT_STAT stbuf;
770         TDB_CONTEXT *idmap_tdb;
771
772         pstrcpy(idmap_name, lock_path("winbindd_idmap.tdb"));
773
774         if (!file_exist(idmap_name, &stbuf)) {
775                 /* nothing to convert return */
776                 return True;
777         }
778
779         if (!(idmap_tdb = tdb_open_log(idmap_name, 0,
780                                         TDB_DEFAULT, O_RDWR,
781                                         0600))) {
782                 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
783                 return False;
784         }
785
786         if (tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION") == IDMAP_VERSION) {
787                 /* nothing to convert return */
788                 tdb_close(idmap_tdb);
789                 return True;
790         }
791
792         /* backup_tdb expects the tdb not to be open */
793         tdb_close(idmap_tdb);
794
795         DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
796
797         pstrcpy(backup_name, idmap_name);
798         pstrcat(backup_name, ".bak");
799
800         if (backup_tdb(idmap_name, backup_name) != 0) {
801                 DEBUG(0, ("Could not backup idmap database\n"));
802                 return False;
803         }
804
805         return idmap_convert(idmap_name);
806 }
807
808 /*******************************************************************
809  wrapper around retrieving the trust account password
810 *******************************************************************/
811
812 BOOL get_trust_pw(const char *domain, uint8 ret_pwd[16],
813                           time_t *pass_last_set_time, uint32 *channel)
814 {
815         DOM_SID sid;
816         char *pwd;
817
818         /* if we are a DC and this is not our domain, then lookup an account
819            for the domain trust */
820            
821         if ( IS_DC && !strequal(domain, lp_workgroup()) && lp_allow_trusted_domains() ) 
822         {
823                 if ( !secrets_fetch_trusted_domain_password(domain, &pwd, &sid, 
824                         pass_last_set_time) ) 
825                 {
826                         DEBUG(0, ("get_trust_pw: could not fetch trust account "
827                                   "password for trusted domain %s\n", domain));
828                         return False;
829                 }
830                 
831                 *channel = SEC_CHAN_DOMAIN;
832                 E_md4hash(pwd, ret_pwd);
833                 SAFE_FREE(pwd);
834
835                 return True;
836         }
837         else    /* just get the account for our domain (covers 
838                    ROLE_DOMAIN_MEMBER as well */
839         {
840                 /* get the machine trust account for our domain */
841
842                 if ( !secrets_fetch_trust_account_password (lp_workgroup(), ret_pwd,
843                         pass_last_set_time, channel) ) 
844                 {
845                         DEBUG(0, ("get_trust_pw: could not fetch trust account "
846                                   "password for my domain %s\n", domain));
847                         return False;
848                 }
849                 
850                 return True;
851         }
852         
853         /* Failure */
854         return False;
855 }
856