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 #define DBGC_CLASS DBGC_WINBIND
30 * @file winbindd_util.c
32 * Winbind daemon for NT domain authentication nss module.
37 * Used to clobber name fields that have an undefined value.
39 * Correct code should never look at a field that has this value.
42 static const fstring name_deadbeef = "<deadbeef>";
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. */
49 static struct winbindd_domain *_domain_list;
51 struct winbindd_domain *domain_list(void)
61 /* Free all entries in the trusted domain list */
63 void free_domain_list(void)
65 struct winbindd_domain *domain = _domain_list;
68 struct winbindd_domain *next = domain->next;
70 DLIST_REMOVE(_domain_list, domain);
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,
82 struct winbindd_domain *domain;
84 /* We can't call domain_list() as this function is called from
85 init_domain_list() and we'll get stuck in a loop. */
86 for (domain = _domain_list; domain; domain = domain->next) {
87 if (strcasecmp(domain_name, domain->name) == 0 ||
88 strcasecmp(domain_name, domain->alt_name) == 0) {
91 if (alt_name && *alt_name) {
92 if (strcasecmp(alt_name, domain->name) == 0 ||
93 strcasecmp(alt_name, domain->alt_name) == 0) {
99 /* Create new domain entry */
101 if ((domain = (struct winbindd_domain *)
102 malloc(sizeof(*domain))) == NULL)
107 ZERO_STRUCTP(domain);
109 /* prioritise the short name */
110 if (strchr_m(domain_name, '.') && alt_name && *alt_name) {
111 fstrcpy(domain->name, alt_name);
112 fstrcpy(domain->alt_name, domain_name);
114 fstrcpy(domain->name, domain_name);
116 fstrcpy(domain->alt_name, alt_name);
120 domain->methods = methods;
121 domain->backend = NULL;
122 domain->sequence_number = DOM_SEQUENCE_NONE;
123 domain->last_seq_check = 0;
125 sid_copy(&domain->sid, sid);
128 /* see if this is a native mode win2k domain */
130 domain->native_mode = cm_check_for_native_mode_win2k( domain_name );
131 DEBUG(3,("add_trusted_domain: %s is a %s mode domain\n", domain_name,
132 domain->native_mode ? "native" : "mixed (or NT4)" ));
134 /* Link to domain list */
135 DLIST_ADD(_domain_list, domain);
137 DEBUG(1,("Added domain %s %s %s\n",
138 domain->name, domain->alt_name,
139 sid?sid_string_static(&domain->sid):""));
146 rescan our domains looking for new trusted domains
148 void rescan_trusted_domains(BOOL force)
150 struct winbindd_domain *domain;
152 static time_t last_scan;
153 time_t t = time(NULL);
155 /* trusted domains might be disabled */
156 if (!lp_allow_trusted_domains()) {
160 /* Only rescan every few minutes but force if necessary */
162 if (((unsigned)(t - last_scan) < WINBINDD_RESCAN_FREQ) && !force)
167 DEBUG(1, ("scanning trusted domain list\n"));
169 if (!(mem_ctx = talloc_init("init_domain_list")))
172 for (domain = _domain_list; domain; domain = domain->next) {
180 result = domain->methods->trusted_domains(domain, mem_ctx, &num_domains,
181 &names, &alt_names, &dom_sids);
182 if (!NT_STATUS_IS_OK(result)) {
186 /* Add each domain to the trusted domain list. Each domain inherits
187 the access methods of its parent */
188 for(i = 0; i < num_domains; i++) {
189 DEBUG(10,("Found domain %s\n", names[i]));
190 add_trusted_domain(names[i], alt_names?alt_names[i]:NULL,
191 domain->methods, &dom_sids[i]);
193 /* store trusted domain in the cache */
194 trustdom_cache_store(names[i], alt_names ? alt_names[i] : NULL,
195 &dom_sids[i], t + WINBINDD_RESCAN_FREQ);
199 talloc_destroy(mem_ctx);
202 /* Look up global info for the winbind daemon */
203 BOOL init_domain_list(void)
205 extern struct winbindd_methods cache_methods;
206 struct winbindd_domain *domain;
208 /* Free existing list */
211 /* Add ourselves as the first entry */
212 domain = add_trusted_domain(lp_workgroup(), NULL, &cache_methods, NULL);
213 if (!secrets_fetch_domain_sid(domain->name, &domain->sid)) {
214 DEBUG(1, ("Could not fetch sid for our domain %s\n",
219 /* get any alternate name for the primary domain */
220 cache_methods.alternate_name(domain);
222 /* do an initial scan for trusted domains */
223 rescan_trusted_domains(True);
228 /* Given a domain name, return the struct winbindd domain info for it
229 if it is actually working. */
231 struct winbindd_domain *find_domain_from_name(const char *domain_name)
233 struct winbindd_domain *domain;
235 /* Search through list */
237 for (domain = domain_list(); domain != NULL; domain = domain->next) {
238 if (strequal(domain_name, domain->name) ||
239 (domain->alt_name[0] && strequal(domain_name, domain->alt_name)))
248 /* Given a domain sid, return the struct winbindd domain info for it */
250 struct winbindd_domain *find_domain_from_sid(DOM_SID *sid)
252 struct winbindd_domain *domain;
254 /* Search through list */
256 for (domain = domain_list(); domain != NULL; domain = domain->next) {
257 if (sid_compare_domain(sid, &domain->sid) == 0)
266 /* Lookup a sid in a domain from a name */
268 BOOL winbindd_lookup_sid_by_name(struct winbindd_domain *domain,
269 const char *name, DOM_SID *sid,
270 enum SID_NAME_USE *type)
274 /* Don't bother with machine accounts */
276 if (name[strlen(name) - 1] == '$')
279 mem_ctx = talloc_init("lookup_sid_by_name for %s\n", name);
284 result = domain->methods->name_to_sid(domain, mem_ctx, name, sid, type);
286 talloc_destroy(mem_ctx);
288 /* Return rid and type if lookup successful */
289 if (!NT_STATUS_IS_OK(result)) {
290 *type = SID_NAME_UNKNOWN;
293 return NT_STATUS_IS_OK(result);
297 * @brief Lookup a name in a domain from a sid.
299 * @param sid Security ID you want to look up.
301 * @param name On success, set to the name corresponding to @p sid.
303 * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
305 * @param type On success, contains the type of name: alias, group or
308 * @retval True if the name exists, in which case @p name and @p type
309 * are set, otherwise False.
311 BOOL winbindd_lookup_name_by_sid(DOM_SID *sid,
314 enum SID_NAME_USE *type)
320 struct winbindd_domain *domain;
322 domain = find_domain_from_sid(sid);
325 DEBUG(1,("Can't find domain from sid\n"));
331 if (!(mem_ctx = talloc_init("winbindd_lookup_name_by_sid")))
334 result = domain->methods->sid_to_name(domain, mem_ctx, sid, &names, type);
336 /* Return name and type if successful */
338 if ((rv = NT_STATUS_IS_OK(result))) {
339 fstrcpy(dom_name, domain->name);
340 fstrcpy(name, names);
342 *type = SID_NAME_UNKNOWN;
343 fstrcpy(name, name_deadbeef);
346 talloc_destroy(mem_ctx);
352 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
354 void free_getent_state(struct getent_state *state)
356 struct getent_state *temp;
358 /* Iterate over state list */
362 while(temp != NULL) {
363 struct getent_state *next;
365 /* Free sam entries then list entry */
367 SAFE_FREE(state->sam_entries);
368 DLIST_REMOVE(state, state);
376 /* Parse winbindd related parameters */
378 BOOL winbindd_param_init(void)
380 /* Parse winbind uid and winbind_gid parameters */
382 if (!lp_idmap_uid(&server_state.uid_low, &server_state.uid_high)) {
383 DEBUG(0, ("winbindd: idmap uid range missing or invalid\n"));
384 DEBUG(0, ("winbindd: cannot continue, exiting.\n"));
388 if (!lp_idmap_gid(&server_state.gid_low, &server_state.gid_high)) {
389 DEBUG(0, ("winbindd: idmap gid range missing or invalid\n"));
390 DEBUG(0, ("winbindd: cannot continue, exiting.\n"));
397 /* Check if a domain is present in a comma-separated list of domains */
399 BOOL check_domain_env(char *domain_env, char *domain)
402 const char *tmp = domain_env;
404 while(next_token(&tmp, name, ",", sizeof(fstring))) {
405 if (strequal(name, domain))
412 /* Parse a string of the form DOMAIN/user into a domain and a user */
414 BOOL parse_domain_user(const char *domuser, fstring domain, fstring user)
416 char *p = strchr(domuser,*lp_winbind_separator());
418 if (!(p || lp_winbind_use_default_domain()))
421 if(!p && lp_winbind_use_default_domain()) {
422 fstrcpy(user, domuser);
423 fstrcpy(domain, lp_workgroup());
426 fstrcpy(domain, domuser);
427 domain[PTR_DIFF(p, domuser)] = 0;
434 Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
435 'winbind separator' options.
437 - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
441 void fill_domain_username(fstring name, const char *domain, const char *user)
443 if(lp_winbind_use_default_domain() &&
444 !strcmp(lp_workgroup(), domain)) {
445 strlcpy(name, user, sizeof(fstring));
447 slprintf(name, sizeof(fstring) - 1, "%s%s%s",
448 domain, lp_winbind_separator(),
454 * Winbindd socket accessor functions
457 char *get_winbind_priv_pipe_dir(void)
459 return lock_path(WINBINDD_PRIV_SOCKET_SUBDIR);
462 /* Open the winbindd socket */
464 static int _winbindd_socket = -1;
465 static int _winbindd_priv_socket = -1;
467 int open_winbindd_socket(void)
469 if (_winbindd_socket == -1) {
470 _winbindd_socket = create_pipe_sock(
471 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME, 0755);
472 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
476 return _winbindd_socket;
479 int open_winbindd_priv_socket(void)
481 if (_winbindd_priv_socket == -1) {
482 _winbindd_priv_socket = create_pipe_sock(
483 get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
484 DEBUG(10, ("open_winbindd_priv_socket: opened socket fd %d\n",
485 _winbindd_priv_socket));
488 return _winbindd_priv_socket;
491 /* Close the winbindd socket */
493 void close_winbindd_socket(void)
495 if (_winbindd_socket != -1) {
496 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
498 close(_winbindd_socket);
499 _winbindd_socket = -1;
501 if (_winbindd_priv_socket != -1) {
502 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
503 _winbindd_priv_socket));
504 close(_winbindd_priv_socket);
505 _winbindd_priv_socket = -1;
510 * Client list accessor functions
513 static struct winbindd_cli_state *_client_list;
514 static int _num_clients;
516 /* Return list of all connected clients */
518 struct winbindd_cli_state *winbindd_client_list(void)
523 /* Add a connection to the list */
525 void winbindd_add_client(struct winbindd_cli_state *cli)
527 DLIST_ADD(_client_list, cli);
531 /* Remove a client from the list */
533 void winbindd_remove_client(struct winbindd_cli_state *cli)
535 DLIST_REMOVE(_client_list, cli);
539 /* Close all open clients */
541 void winbindd_kill_all_clients(void)
543 struct winbindd_cli_state *cl = winbindd_client_list();
545 DEBUG(10, ("winbindd_kill_all_clients: going postal\n"));
548 struct winbindd_cli_state *next;
551 winbindd_remove_client(cl);
556 /* Return number of open clients */
558 int winbindd_num_clients(void)
563 /* Help with RID -> SID conversion */
565 DOM_SID *rid_to_talloced_sid(struct winbindd_domain *domain,
570 sid = talloc(mem_ctx, sizeof(*sid));
572 smb_panic("rid_to_to_talloced_sid: talloc for DOM_SID failed!\n");
574 sid_copy(sid, &domain->sid);
575 sid_append_rid(sid, rid);
579 /*****************************************************************************
580 For idmap conversion: convert one record to new format
581 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
583 *****************************************************************************/
584 static int convert_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data, void *state)
586 struct winbindd_domain *domain;
593 BOOL *failed = (BOOL *)state;
595 DEBUG(10,("Converting %s\n", key.dptr));
597 p = strchr(key.dptr, '/');
602 fstrcpy(dom_name, key.dptr);
605 domain = find_domain_from_name(dom_name);
606 if (domain == NULL) {
607 /* We must delete the old record. */
608 DEBUG(0,("Unable to find domain %s\n", dom_name ));
609 DEBUG(0,("deleting record %s\n", key.dptr ));
611 if (tdb_delete(tdb, key) != 0) {
612 DEBUG(0, ("Unable to delete record %s\n", key.dptr));
622 sid_copy(&sid, &domain->sid);
623 sid_append_rid(&sid, rid);
625 sid_to_string(keystr, &sid);
627 key2.dsize = strlen(keystr) + 1;
629 if (tdb_store(tdb, key2, data, TDB_INSERT) != 0) {
630 DEBUG(0,("Unable to add record %s\n", key2.dptr ));
635 if (tdb_store(tdb, data, key2, TDB_REPLACE) != 0) {
636 DEBUG(0,("Unable to update record %s\n", data.dptr ));
641 if (tdb_delete(tdb, key) != 0) {
642 DEBUG(0,("Unable to delete record %s\n", key.dptr ));
650 /* These definitions are from sam/idmap_tdb.c. Replicated here just
651 out of laziness.... :-( */
653 /* High water mark keys */
654 #define HWM_GROUP "GROUP HWM"
655 #define HWM_USER "USER HWM"
657 /* idmap version determines auto-conversion */
658 #define IDMAP_VERSION 2
661 /*****************************************************************************
662 Convert the idmap database from an older version.
663 *****************************************************************************/
665 static BOOL idmap_convert(const char *idmap_name)
668 BOOL bigendianheader;
670 TDB_CONTEXT *idmap_tdb;
672 if (!(idmap_tdb = tdb_open_log(idmap_name, 0,
675 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
679 bigendianheader = (idmap_tdb->flags & TDB_BIGENDIAN) ? True : False;
681 vers = tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION");
683 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
684 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
686 * high and low records were created on a
687 * big endian machine and will need byte-reversing.
692 wm = tdb_fetch_int32(idmap_tdb, HWM_USER);
697 wm = server_state.uid_low;
700 if (tdb_store_int32(idmap_tdb, HWM_USER, wm) == -1) {
701 DEBUG(0, ("idmap_convert: Unable to byteswap user hwm in idmap database\n"));
702 tdb_close(idmap_tdb);
706 wm = tdb_fetch_int32(idmap_tdb, HWM_GROUP);
710 wm = server_state.gid_low;
713 if (tdb_store_int32(idmap_tdb, HWM_GROUP, wm) == -1) {
714 DEBUG(0, ("idmap_convert: Unable to byteswap group hwm in idmap database\n"));
715 tdb_close(idmap_tdb);
720 /* the old format stored as DOMAIN/rid - now we store the SID direct */
721 tdb_traverse(idmap_tdb, convert_fn, &failed);
724 DEBUG(0, ("Problem during conversion\n"));
725 tdb_close(idmap_tdb);
729 if (tdb_store_int32(idmap_tdb, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
730 DEBUG(0, ("idmap_convert: Unable to dtore idmap version in databse\n"));
731 tdb_close(idmap_tdb);
735 tdb_close(idmap_tdb);
739 /*****************************************************************************
740 Convert the idmap database from an older version if necessary
741 *****************************************************************************/
743 BOOL winbindd_upgrade_idmap(void)
747 SMB_STRUCT_STAT stbuf;
748 TDB_CONTEXT *idmap_tdb;
750 pstrcpy(idmap_name, lock_path("winbindd_idmap.tdb"));
752 if (!file_exist(idmap_name, &stbuf)) {
753 /* nothing to convert return */
757 if (!(idmap_tdb = tdb_open_log(idmap_name, 0,
760 DEBUG(0, ("idmap_convert: Unable to open idmap database\n"));
764 if (tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION") == IDMAP_VERSION) {
765 /* nothing to convert return */
766 tdb_close(idmap_tdb);
770 /* backup_tdb expects the tdb not to be open */
771 tdb_close(idmap_tdb);
773 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
775 pstrcpy(backup_name, idmap_name);
776 pstrcat(backup_name, ".bak");
778 if (backup_tdb(idmap_name, backup_name) != 0) {
779 DEBUG(0, ("Could not backup idmap database\n"));
783 return idmap_convert(idmap_name);