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->sequence_number = DOM_SEQUENCE_NONE;
122 domain->last_seq_check = 0;
124 sid_copy(&domain->sid, sid);
127 /* see if this is a native mode win2k domain, but only for our own domain */
129 if ( strequal( lp_workgroup(), domain_name) ) {
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" ));
135 /* Link to domain list */
136 DLIST_ADD(_domain_list, domain);
138 DEBUG(1,("Added domain %s %s %s\n",
139 domain->name, domain->alt_name,
140 sid?sid_string_static(&domain->sid):""));
147 rescan our domains looking for new trusted domains
149 void rescan_trusted_domains(void)
151 struct winbindd_domain *domain;
153 static time_t last_scan;
154 time_t t = time(NULL);
156 /* trusted domains might be disabled */
157 if (!lp_allow_trusted_domains()) {
161 /* ony rescan every few minutes */
162 if ((unsigned)(t - last_scan) < WINBINDD_RESCAN_FREQ) {
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],
191 alt_names?alt_names[i]:NULL,
192 domain->methods, &dom_sids[i]);
196 talloc_destroy(mem_ctx);
199 /* Look up global info for the winbind daemon */
200 BOOL init_domain_list(void)
202 extern struct winbindd_methods cache_methods;
203 struct winbindd_domain *domain;
205 /* Free existing list */
208 /* Add ourselves as the first entry */
209 domain = add_trusted_domain(lp_workgroup(), NULL, &cache_methods, NULL);
210 if (!secrets_fetch_domain_sid(domain->name, &domain->sid)) {
211 DEBUG(1, ("Could not fetch sid for our domain %s\n",
216 /* get any alternate name for the primary domain */
217 cache_methods.alternate_name(domain);
219 /* do an initial scan for trusted domains */
220 rescan_trusted_domains();
225 /* Given a domain name, return the struct winbindd domain info for it
226 if it is actually working. */
228 struct winbindd_domain *find_domain_from_name(const char *domain_name)
230 struct winbindd_domain *domain;
232 /* Search through list */
234 for (domain = domain_list(); domain != NULL; domain = domain->next) {
235 if (strequal(domain_name, domain->name) ||
236 (domain->alt_name[0] && strequal(domain_name, domain->alt_name)))
245 /* Given a domain sid, return the struct winbindd domain info for it */
247 struct winbindd_domain *find_domain_from_sid(DOM_SID *sid)
249 struct winbindd_domain *domain;
251 /* Search through list */
253 for (domain = domain_list(); domain != NULL; domain = domain->next) {
254 if (sid_compare_domain(sid, &domain->sid) == 0)
263 /* Lookup a sid in a domain from a name */
265 BOOL winbindd_lookup_sid_by_name(struct winbindd_domain *domain,
266 const char *name, DOM_SID *sid,
267 enum SID_NAME_USE *type)
271 /* Don't bother with machine accounts */
273 if (name[strlen(name) - 1] == '$')
277 result = domain->methods->name_to_sid(domain, name, sid, type);
279 /* Return rid and type if lookup successful */
280 if (!NT_STATUS_IS_OK(result)) {
281 *type = SID_NAME_UNKNOWN;
284 return NT_STATUS_IS_OK(result);
288 * @brief Lookup a name in a domain from a sid.
290 * @param sid Security ID you want to look up.
292 * @param name On success, set to the name corresponding to @p sid.
294 * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
296 * @param type On success, contains the type of name: alias, group or
299 * @retval True if the name exists, in which case @p name and @p type
300 * are set, otherwise False.
302 BOOL winbindd_lookup_name_by_sid(DOM_SID *sid,
305 enum SID_NAME_USE *type)
311 struct winbindd_domain *domain;
313 domain = find_domain_from_sid(sid);
316 DEBUG(1,("Can't find domain from sid\n"));
322 if (!(mem_ctx = talloc_init("winbindd_lookup_name_by_sid")))
325 result = domain->methods->sid_to_name(domain, mem_ctx, sid, &names, type);
327 /* Return name and type if successful */
329 if ((rv = NT_STATUS_IS_OK(result))) {
330 fstrcpy(dom_name, domain->name);
331 fstrcpy(name, names);
333 *type = SID_NAME_UNKNOWN;
334 fstrcpy(name, name_deadbeef);
337 talloc_destroy(mem_ctx);
343 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
345 void free_getent_state(struct getent_state *state)
347 struct getent_state *temp;
349 /* Iterate over state list */
353 while(temp != NULL) {
354 struct getent_state *next;
356 /* Free sam entries then list entry */
358 SAFE_FREE(state->sam_entries);
359 DLIST_REMOVE(state, state);
367 /* Parse winbindd related parameters */
369 BOOL winbindd_param_init(void)
371 /* Parse winbind uid and winbind_gid parameters */
373 if (!lp_winbind_uid(&server_state.uid_low, &server_state.uid_high)) {
374 DEBUG(0, ("winbind uid range missing or invalid\n"));
378 if (!lp_winbind_gid(&server_state.gid_low, &server_state.gid_high)) {
379 DEBUG(0, ("winbind gid range missing or invalid\n"));
386 /* Check if a domain is present in a comma-separated list of domains */
388 BOOL check_domain_env(char *domain_env, char *domain)
391 const char *tmp = domain_env;
393 while(next_token(&tmp, name, ",", sizeof(fstring))) {
394 if (strequal(name, domain))
401 /* Parse a string of the form DOMAIN/user into a domain and a user */
403 BOOL parse_domain_user(const char *domuser, fstring domain, fstring user)
405 char *p = strchr(domuser,*lp_winbind_separator());
407 if (!(p || lp_winbind_use_default_domain()))
410 if(!p && lp_winbind_use_default_domain()) {
411 fstrcpy(user, domuser);
412 fstrcpy(domain, lp_workgroup());
415 fstrcpy(domain, domuser);
416 domain[PTR_DIFF(p, domuser)] = 0;
423 Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
424 'winbind separator' options.
426 - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
430 void fill_domain_username(fstring name, const char *domain, const char *user)
432 if(lp_winbind_use_default_domain() &&
433 !strcmp(lp_workgroup(), domain)) {
434 strlcpy(name, user, sizeof(fstring));
436 slprintf(name, sizeof(fstring) - 1, "%s%s%s",
437 domain, lp_winbind_separator(),
443 * Winbindd socket accessor functions
446 /* Open the winbindd socket */
448 static int _winbindd_socket = -1;
450 int open_winbindd_socket(void)
452 if (_winbindd_socket == -1) {
453 _winbindd_socket = create_pipe_sock(
454 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME, 0755);
455 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
459 return _winbindd_socket;
462 /* Close the winbindd socket */
464 void close_winbindd_socket(void)
466 if (_winbindd_socket != -1) {
467 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
469 close(_winbindd_socket);
470 _winbindd_socket = -1;
475 * Client list accessor functions
478 static struct winbindd_cli_state *_client_list;
479 static int _num_clients;
481 /* Return list of all connected clients */
483 struct winbindd_cli_state *winbindd_client_list(void)
488 /* Add a connection to the list */
490 void winbindd_add_client(struct winbindd_cli_state *cli)
492 DLIST_ADD(_client_list, cli);
496 /* Remove a client from the list */
498 void winbindd_remove_client(struct winbindd_cli_state *cli)
500 DLIST_REMOVE(_client_list, cli);
504 /* Close all open clients */
506 void winbindd_kill_all_clients(void)
508 struct winbindd_cli_state *cl = winbindd_client_list();
510 DEBUG(10, ("winbindd_kill_all_clients: going postal\n"));
513 struct winbindd_cli_state *next;
516 winbindd_remove_client(cl);
521 /* Return number of open clients */
523 int winbindd_num_clients(void)