2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) Tim Potter 2000-2001
7 Copyright (C) 2001 by Martin Pool <mbp@samba.org>
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.
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.
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.
27 extern struct winbindd_methods cache_methods;
28 extern struct winbindd_methods passdb_methods;
31 #define DBGC_CLASS DBGC_WINBIND
34 * @file winbindd_util.c
36 * Winbind daemon for NT domain authentication nss module.
41 * Used to clobber name fields that have an undefined value.
43 * Correct code should never look at a field that has this value.
46 static const fstring name_deadbeef = "<deadbeef>";
48 /* The list of trusted domains. Note that the list can be deleted and
49 recreated using the init_domain_list() function so pointers to
50 individual winbindd_domain structures cannot be made. Keep a copy of
51 the domain name instead. */
53 static struct winbindd_domain *_domain_list;
56 When was the last scan of trusted domains done?
61 static time_t last_trustdom_scan;
63 struct winbindd_domain *domain_list(void)
68 if (!init_domain_list())
74 /* Free all entries in the trusted domain list */
76 void free_domain_list(void)
78 struct winbindd_domain *domain = _domain_list;
81 struct winbindd_domain *next = domain->next;
83 DLIST_REMOVE(_domain_list, domain);
89 static BOOL is_internal_domain(const DOM_SID *sid)
94 return (sid_check_is_domain(sid) || sid_check_is_builtin(sid));
98 /* Add a trusted domain to our list of domains */
99 static struct winbindd_domain *add_trusted_domain(const char *domain_name, const char *alt_name,
100 struct winbindd_methods *methods,
103 struct winbindd_domain *domain;
104 const char *alternative_name = NULL;
105 static const DOM_SID null_sid;
107 /* ignore alt_name if we are not in an AD domain */
109 if ( (lp_security() == SEC_ADS) && alt_name && *alt_name) {
110 alternative_name = alt_name;
113 /* We can't call domain_list() as this function is called from
114 init_domain_list() and we'll get stuck in a loop. */
115 for (domain = _domain_list; domain; domain = domain->next) {
116 if (strequal(domain_name, domain->name) ||
117 strequal(domain_name, domain->alt_name)) {
120 if (alternative_name && *alternative_name) {
121 if (strequal(alternative_name, domain->name) ||
122 strequal(alternative_name, domain->alt_name)) {
127 if (sid_equal(sid, &null_sid) ) {
129 } else if (sid_equal(sid, &domain->sid)) {
135 /* Create new domain entry */
137 if ((domain = SMB_MALLOC_P(struct winbindd_domain)) == NULL)
142 ZERO_STRUCTP(domain);
144 /* prioritise the short name */
145 if (strchr_m(domain_name, '.') && alternative_name && *alternative_name) {
146 fstrcpy(domain->name, alternative_name);
147 fstrcpy(domain->alt_name, domain_name);
149 fstrcpy(domain->name, domain_name);
150 if (alternative_name) {
151 fstrcpy(domain->alt_name, alternative_name);
155 domain->methods = methods;
156 domain->backend = NULL;
157 domain->internal = is_internal_domain(sid);
158 domain->sequence_number = DOM_SEQUENCE_NONE;
159 domain->last_seq_check = 0;
160 domain->initialized = False;
162 sid_copy(&domain->sid, sid);
165 /* Link to domain list */
166 DLIST_ADD(_domain_list, domain);
168 DEBUG(2,("Added domain %s %s %s\n",
169 domain->name, domain->alt_name,
170 &domain->sid?sid_string_static(&domain->sid):""));
175 /********************************************************************
176 rescan our domains looking for new trusted domains
177 ********************************************************************/
179 static void add_trusted_domains( struct winbindd_domain *domain )
187 DOM_SID *dom_sids, null_sid;
189 struct winbindd_domain *new_domain;
191 /* trusted domains might be disabled */
192 if (!lp_allow_trusted_domains()) {
196 DEBUG(5, ("scanning trusted domain list\n"));
198 if (!(mem_ctx = talloc_init("init_domain_list")))
201 ZERO_STRUCTP(&null_sid);
205 /* ask the DC what domains it trusts */
207 result = domain->methods->trusted_domains(domain, mem_ctx, (unsigned int *)&num_domains,
208 &names, &alt_names, &dom_sids);
210 if ( NT_STATUS_IS_OK(result) ) {
212 /* Add each domain to the trusted domain list */
214 for(i = 0; i < num_domains; i++) {
215 DEBUG(10,("Found domain %s\n", names[i]));
216 add_trusted_domain(names[i], alt_names?alt_names[i]:NULL,
217 &cache_methods, &dom_sids[i]);
219 /* if the SID was empty, we better set it now */
221 if ( sid_equal(&dom_sids[i], &null_sid) ) {
223 new_domain = find_domain_from_name(names[i]);
225 /* this should never happen */
227 DEBUG(0,("rescan_trust_domains: can't find the domain I just added! [%s]\n",
232 /* call the cache method; which will operate on the winbindd_domain \
233 passed in and choose either rpc or ads as appropriate */
235 result = domain->methods->domain_sid( new_domain, &new_domain->sid );
237 if ( NT_STATUS_IS_OK(result) )
238 sid_copy( &dom_sids[i], &new_domain->sid );
241 /* store trusted domain in the cache */
242 trustdom_cache_store(names[i], alt_names ? alt_names[i] : NULL,
243 &dom_sids[i], t + WINBINDD_RESCAN_FREQ);
247 talloc_destroy(mem_ctx);
250 /********************************************************************
251 Periodically we need to refresh the trusted domain cache for smbd
252 ********************************************************************/
254 void rescan_trusted_domains( void )
256 time_t now = time(NULL);
257 struct winbindd_domain *mydomain = NULL;
259 /* see if the time has come... */
261 if ( (now > last_trustdom_scan) && ((now-last_trustdom_scan) < WINBINDD_RESCAN_FREQ) )
264 if ( (mydomain = find_our_domain()) == NULL ) {
265 DEBUG(0,("rescan_trusted_domains: Can't find my own domain!\n"));
269 /* this will only add new domains we didn't already know about */
271 add_trusted_domains( mydomain );
273 last_trustdom_scan = now;
278 /* Look up global info for the winbind daemon */
279 BOOL init_domain_list(void)
281 struct winbindd_domain *domain;
283 /* Free existing list */
286 /* Add ourselves as the first entry. */
289 domain = add_trusted_domain(get_global_sam_name(), NULL,
290 &passdb_methods, get_global_sam_sid());
293 domain = add_trusted_domain( lp_workgroup(), lp_realm(),
294 &cache_methods, NULL);
296 /* set flags about native_mode, active_directory */
297 set_dc_type_and_flags(domain);
300 domain->primary = True;
302 /* get any alternate name for the primary domain */
304 cache_methods.alternate_name(domain);
306 /* now we have the correct netbios (short) domain name */
309 set_global_myworkgroup( domain->name );
311 if (!secrets_fetch_domain_sid(domain->name, &domain->sid)) {
312 DEBUG(1, ("Could not fetch sid for our domain %s\n",
317 /* do an initial scan for trusted domains */
318 add_trusted_domains(domain);
321 /* Add our local SAM domains */
323 add_trusted_domain("BUILTIN", NULL, &passdb_methods,
324 &global_sid_Builtin);
327 add_trusted_domain(get_global_sam_name(), NULL,
328 &passdb_methods, get_global_sam_sid());
331 /* avoid rescanning this right away */
332 last_trustdom_scan = time(NULL);
337 * Given a domain name, return the struct winbindd domain info for it
339 * @note Do *not* pass lp_workgroup() to this function. domain_list
340 * may modify it's value, and free that pointer. Instead, our local
341 * domain may be found by calling find_our_domain().
345 * @return The domain structure for the named domain, if it is working.
348 struct winbindd_domain *find_domain_from_name(const char *domain_name)
350 struct winbindd_domain *domain;
352 /* Search through list */
354 for (domain = domain_list(); domain != NULL; domain = domain->next) {
355 if (strequal(domain_name, domain->name) ||
356 (domain->alt_name[0] && strequal(domain_name, domain->alt_name))) {
357 if (!domain->initialized)
358 set_dc_type_and_flags(domain);
369 /* Given a domain sid, return the struct winbindd domain info for it */
371 struct winbindd_domain *find_domain_from_sid(const DOM_SID *sid)
373 struct winbindd_domain *domain;
375 /* Search through list */
377 for (domain = domain_list(); domain != NULL; domain = domain->next) {
378 if (sid_compare_domain(sid, &domain->sid) == 0) {
379 if (!domain->initialized)
380 set_dc_type_and_flags(domain);
390 /* Given a domain sid, return the struct winbindd domain info for it */
392 struct winbindd_domain *find_our_domain(void)
394 struct winbindd_domain *domain;
396 /* Search through list */
398 for (domain = domain_list(); domain != NULL; domain = domain->next) {
408 /* Find the appropriate domain to lookup a name or SID */
410 struct winbindd_domain *find_lookup_domain_from_sid(const DOM_SID *sid)
412 /* A DC can't ask the local smbd for remote SIDs, here winbindd is the
413 * one to contact the external DC's. On member servers the internal
414 * domains are different: These are part of the local SAM. */
416 if (IS_DC || is_internal_domain(sid))
417 return find_domain_from_sid(sid);
419 /* On a member server a query for SID or name can always go to our
422 return find_our_domain();
425 struct winbindd_domain *find_lookup_domain_from_name(const char *domain_name)
427 if (IS_DC || strequal(domain_name, "BUILTIN") ||
428 strequal(domain_name, get_global_sam_name()))
429 return find_domain_from_name(domain_name);
431 return find_our_domain();
434 /* Lookup a sid in a domain from a name */
436 BOOL winbindd_lookup_sid_by_name(struct winbindd_domain *domain,
437 const char *domain_name,
438 const char *name, DOM_SID *sid,
439 enum SID_NAME_USE *type)
444 mem_ctx = talloc_init("lookup_sid_by_name for %s\\%s\n",
450 result = domain->methods->name_to_sid(domain, mem_ctx, domain_name, name, sid, type);
452 talloc_destroy(mem_ctx);
454 /* Return rid and type if lookup successful */
455 if (!NT_STATUS_IS_OK(result)) {
456 *type = SID_NAME_UNKNOWN;
459 return NT_STATUS_IS_OK(result);
463 * @brief Lookup a name in a domain from a sid.
465 * @param sid Security ID you want to look up.
466 * @param name On success, set to the name corresponding to @p sid.
467 * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
468 * @param type On success, contains the type of name: alias, group or
470 * @retval True if the name exists, in which case @p name and @p type
471 * are set, otherwise False.
473 BOOL winbindd_lookup_name_by_sid(DOM_SID *sid,
476 enum SID_NAME_USE *type)
483 struct winbindd_domain *domain;
485 domain = find_lookup_domain_from_sid(sid);
488 DEBUG(1,("Can't find domain from sid\n"));
494 if (!(mem_ctx = talloc_init("winbindd_lookup_name_by_sid")))
497 result = domain->methods->sid_to_name(domain, mem_ctx, sid, &dom_names, &names, type);
499 /* Return name and type if successful */
501 if ((rv = NT_STATUS_IS_OK(result))) {
502 fstrcpy(dom_name, dom_names);
503 fstrcpy(name, names);
505 *type = SID_NAME_UNKNOWN;
506 fstrcpy(name, name_deadbeef);
509 talloc_destroy(mem_ctx);
515 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
517 void free_getent_state(struct getent_state *state)
519 struct getent_state *temp;
521 /* Iterate over state list */
525 while(temp != NULL) {
526 struct getent_state *next;
528 /* Free sam entries then list entry */
530 SAFE_FREE(state->sam_entries);
531 DLIST_REMOVE(state, state);
539 /* Parse winbindd related parameters */
541 BOOL winbindd_param_init(void)
543 /* Parse winbind uid and winbind_gid parameters */
545 if (!lp_idmap_uid(&server_state.uid_low, &server_state.uid_high)) {
546 DEBUG(2, ("winbindd: idmap uid range missing or invalid\n"));
550 if (!lp_idmap_gid(&server_state.gid_low, &server_state.gid_high)) {
551 DEBUG(2, ("winbindd: idmap gid range missing or invalid\n"));
558 /* Check if a domain is present in a comma-separated list of domains */
560 BOOL check_domain_env(char *domain_env, char *domain)
563 const char *tmp = domain_env;
565 while(next_token(&tmp, name, ",", sizeof(fstring))) {
566 if (strequal(name, domain))
573 /* Is this a domain which we may assume no DOMAIN\ prefix? */
575 static BOOL assume_domain(const char *domain) {
576 if ((lp_winbind_use_default_domain()
577 || lp_winbind_trusted_domains_only()) &&
578 strequal(lp_workgroup(), domain))
581 if (strequal(get_global_sam_name(), domain))
587 /* Parse a string of the form DOMAIN/user into a domain and a user */
589 BOOL parse_domain_user(const char *domuser, fstring domain, fstring user)
591 char *p = strchr(domuser,*lp_winbind_separator());
594 fstrcpy(user, domuser);
596 if ( assume_domain(lp_workgroup())) {
597 fstrcpy(domain, lp_workgroup());
599 fstrcpy( domain, get_global_sam_name() );
604 fstrcpy(domain, domuser);
605 domain[PTR_DIFF(p, domuser)] = 0;
614 Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
615 'winbind separator' options.
617 - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
620 If we are a PDC or BDC, and this is for our domain, do likewise.
622 Also, if omit DOMAIN if 'winbind trusted domains only = true', as the
623 username is then unqualified in unix
626 void fill_domain_username(fstring name, const char *domain, const char *user)
628 strlower_m(CONST_DISCARD(char *, user));
630 if (assume_domain(domain)) {
631 strlcpy(name, user, sizeof(fstring));
633 slprintf(name, sizeof(fstring) - 1, "%s%c%s",
634 domain, *lp_winbind_separator(),
640 * Winbindd socket accessor functions
643 char *get_winbind_priv_pipe_dir(void)
645 return lock_path(WINBINDD_PRIV_SOCKET_SUBDIR);
648 /* Open the winbindd socket */
650 static int _winbindd_socket = -1;
651 static int _winbindd_priv_socket = -1;
653 int open_winbindd_socket(void)
655 if (_winbindd_socket == -1) {
656 _winbindd_socket = create_pipe_sock(
657 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME, 0755);
658 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
662 return _winbindd_socket;
665 int open_winbindd_priv_socket(void)
667 if (_winbindd_priv_socket == -1) {
668 _winbindd_priv_socket = create_pipe_sock(
669 get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
670 DEBUG(10, ("open_winbindd_priv_socket: opened socket fd %d\n",
671 _winbindd_priv_socket));
674 return _winbindd_priv_socket;
677 /* Close the winbindd socket */
679 void close_winbindd_socket(void)
681 if (_winbindd_socket != -1) {
682 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
684 close(_winbindd_socket);
685 _winbindd_socket = -1;
687 if (_winbindd_priv_socket != -1) {
688 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
689 _winbindd_priv_socket));
690 close(_winbindd_priv_socket);
691 _winbindd_priv_socket = -1;
696 * Client list accessor functions
699 static struct winbindd_cli_state *_client_list;
700 static int _num_clients;
702 /* Return list of all connected clients */
704 struct winbindd_cli_state *winbindd_client_list(void)
709 /* Add a connection to the list */
711 void winbindd_add_client(struct winbindd_cli_state *cli)
713 DLIST_ADD(_client_list, cli);
717 /* Remove a client from the list */
719 void winbindd_remove_client(struct winbindd_cli_state *cli)
721 DLIST_REMOVE(_client_list, cli);
725 /* Demote a client to be the last in the list */
727 void winbindd_demote_client(struct winbindd_cli_state *cli)
729 struct winbindd_cli_state *tmp;
730 DLIST_DEMOTE(_client_list, cli, tmp);
733 /* Close all open clients */
735 void winbindd_kill_all_clients(void)
737 struct winbindd_cli_state *cl = winbindd_client_list();
739 DEBUG(10, ("winbindd_kill_all_clients: going postal\n"));
742 struct winbindd_cli_state *next;
745 winbindd_remove_client(cl);
750 /* Return number of open clients */
752 int winbindd_num_clients(void)
757 /* Help with RID -> SID conversion */
759 DOM_SID *rid_to_talloced_sid(struct winbindd_domain *domain,
764 sid = TALLOC_P(mem_ctx, DOM_SID);
766 smb_panic("rid_to_to_talloced_sid: talloc for DOM_SID failed!\n");
768 sid_copy(sid, &domain->sid);
769 sid_append_rid(sid, rid);
773 /*****************************************************************************
774 For idmap conversion: convert one record to new format
775 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
777 *****************************************************************************/
778 static int convert_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data, void *state)
780 struct winbindd_domain *domain;
787 BOOL *failed = (BOOL *)state;
789 DEBUG(10,("Converting %s\n", key.dptr));
791 p = strchr(key.dptr, '/');
796 fstrcpy(dom_name, key.dptr);
799 domain = find_domain_from_name(dom_name);
800 if (domain == NULL) {
801 /* We must delete the old record. */
802 DEBUG(0,("Unable to find domain %s\n", dom_name ));
803 DEBUG(0,("deleting record %s\n", key.dptr ));
805 if (tdb_delete(tdb, key) != 0) {
806 DEBUG(0, ("Unable to delete record %s\n", key.dptr));
816 sid_copy(&sid, &domain->sid);
817 sid_append_rid(&sid, rid);
819 sid_to_string(keystr, &sid);
821 key2.dsize = strlen(keystr) + 1;
823 if (tdb_store(tdb, key2, data, TDB_INSERT) != 0) {
824 DEBUG(0,("Unable to add record %s\n", key2.dptr ));
829 if (tdb_store(tdb, data, key2, TDB_REPLACE) != 0) {
830 DEBUG(0,("Unable to update record %s\n", data.dptr ));
835 if (tdb_delete(tdb, key) != 0) {
836 DEBUG(0,("Unable to delete record %s\n", key.dptr ));
844 /* These definitions are from sam/idmap_tdb.c. Replicated here just
845 out of laziness.... :-( */
847 /* High water mark keys */
848 #define HWM_GROUP "GROUP HWM"
849 #define HWM_USER "USER HWM"
851 /* idmap version determines auto-conversion */
852 #define IDMAP_VERSION 2
855 /*****************************************************************************
856 Convert the idmap database from an older version.
857 *****************************************************************************/
859 static BOOL idmap_convert(const char *idmap_name)
862 BOOL bigendianheader;
864 TDB_CONTEXT *idmap_tdb;
866 if (!(idmap_tdb = tdb_open_log(idmap_name, 0,
869 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
873 bigendianheader = (idmap_tdb->flags & TDB_BIGENDIAN) ? True : False;
875 vers = tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION");
877 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
878 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
880 * high and low records were created on a
881 * big endian machine and will need byte-reversing.
886 wm = tdb_fetch_int32(idmap_tdb, HWM_USER);
891 wm = server_state.uid_low;
894 if (tdb_store_int32(idmap_tdb, HWM_USER, wm) == -1) {
895 DEBUG(0, ("idmap_convert: Unable to byteswap user hwm in idmap database\n"));
896 tdb_close(idmap_tdb);
900 wm = tdb_fetch_int32(idmap_tdb, HWM_GROUP);
904 wm = server_state.gid_low;
907 if (tdb_store_int32(idmap_tdb, HWM_GROUP, wm) == -1) {
908 DEBUG(0, ("idmap_convert: Unable to byteswap group hwm in idmap database\n"));
909 tdb_close(idmap_tdb);
914 /* the old format stored as DOMAIN/rid - now we store the SID direct */
915 tdb_traverse(idmap_tdb, convert_fn, &failed);
918 DEBUG(0, ("Problem during conversion\n"));
919 tdb_close(idmap_tdb);
923 if (tdb_store_int32(idmap_tdb, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
924 DEBUG(0, ("idmap_convert: Unable to dtore idmap version in databse\n"));
925 tdb_close(idmap_tdb);
929 tdb_close(idmap_tdb);
933 /*****************************************************************************
934 Convert the idmap database from an older version if necessary
935 *****************************************************************************/
937 BOOL winbindd_upgrade_idmap(void)
941 SMB_STRUCT_STAT stbuf;
942 TDB_CONTEXT *idmap_tdb;
944 pstrcpy(idmap_name, lock_path("winbindd_idmap.tdb"));
946 if (!file_exist(idmap_name, &stbuf)) {
947 /* nothing to convert return */
951 if (!(idmap_tdb = tdb_open_log(idmap_name, 0,
954 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
958 if (tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION") == IDMAP_VERSION) {
959 /* nothing to convert return */
960 tdb_close(idmap_tdb);
964 /* backup_tdb expects the tdb not to be open */
965 tdb_close(idmap_tdb);
967 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
969 pstrcpy(backup_name, idmap_name);
970 pstrcat(backup_name, ".bak");
972 if (backup_tdb(idmap_name, backup_name) != 0) {
973 DEBUG(0, ("Could not backup idmap database\n"));
977 return idmap_convert(idmap_name);
980 /*******************************************************************
981 wrapper around retrieving the trust account password
982 *******************************************************************/
984 BOOL get_trust_pw(const char *domain, uint8 ret_pwd[16],
985 time_t *pass_last_set_time, uint32 *channel)
990 /* if we are a DC and this is not our domain, then lookup an account
991 for the domain trust */
993 if ( IS_DC && !strequal(domain, lp_workgroup()) && lp_allow_trusted_domains() )
995 if ( !secrets_fetch_trusted_domain_password(domain, &pwd, &sid,
996 pass_last_set_time) )
998 DEBUG(0, ("get_trust_pw: could not fetch trust account "
999 "password for trusted domain %s\n", domain));
1003 *channel = SEC_CHAN_DOMAIN;
1004 E_md4hash(pwd, ret_pwd);
1009 else /* just get the account for our domain (covers
1010 ROLE_DOMAIN_MEMBER as well */
1012 /* get the machine trust account for our domain */
1014 if ( !secrets_fetch_trust_account_password (lp_workgroup(), ret_pwd,
1015 pass_last_set_time, channel) )
1017 DEBUG(0, ("get_trust_pw: could not fetch trust account "
1018 "password for my domain %s\n", domain));