d5668a2bb68eee85b892a52bfc26df5d8d30a70d
[jra/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 /* Add a trusted domain to our list of domains */
77
78 static struct winbindd_domain *add_trusted_domain(char *domain_name,
79                                                   struct winbindd_methods *methods)
80 {
81         struct winbindd_domain *domain;
82         
83         /* We can't call domain_list() as this function is called from
84            init_domain_list() and we'll get stuck in a loop. */
85
86         for (domain = _domain_list; domain; domain = domain->next) {
87                 if (strcmp(domain_name, domain->name) == 0) {
88                         DEBUG(3, ("domain %s already in domain list\n", 
89                                   domain_name));
90                         return domain;
91                 }
92         }
93         
94         /* Create new domain entry */
95
96         if ((domain = (struct winbindd_domain *)
97              malloc(sizeof(*domain))) == NULL)
98                 return NULL;
99
100         /* Fill in fields */
101         
102         ZERO_STRUCTP(domain);
103
104         fstrcpy(domain->name, domain_name);
105         domain->methods = methods;
106         domain->sequence_number = DOM_SEQUENCE_NONE;
107         domain->last_seq_check = 0;
108
109         /* Link to domain list */
110         
111         DLIST_ADD(_domain_list, domain);
112         
113         return domain;
114 }
115
116 /* Look up global info for the winbind daemon */
117
118 BOOL init_domain_list(void)
119 {
120         NTSTATUS result;
121         TALLOC_CTX *mem_ctx;
122         extern struct winbindd_methods cache_methods;
123         struct winbindd_domain *domain;
124         DOM_SID *dom_sids;
125         char **names;
126         int num_domains = 0;
127
128         if (!(mem_ctx = talloc_init_named("init_domain_list")))
129                 return False;
130
131         /* Free existing list */
132
133         free_domain_list();
134
135         /* Add ourselves as the first entry */
136
137         domain = add_trusted_domain(lp_workgroup(), &cache_methods);
138
139         /* Now we *must* get the domain sid for our primary domain. Go into
140            a holding pattern until that is available */
141
142         result = cache_methods.domain_sid(domain, &domain->sid);
143         while (!NT_STATUS_IS_OK(result)) {
144                 sleep(10);
145                 DEBUG(1,("Retrying startup domain sid fetch for %s\n",
146                          domain->name));
147                 result = cache_methods.domain_sid(domain, &domain->sid);
148         }
149        
150         DEBUG(1,("Added domain %s (%s)\n", 
151                  domain->name, 
152                  sid_string_static(&domain->sid)));
153
154         DEBUG(1, ("getting trusted domain list\n"));
155
156         result = cache_methods.trusted_domains(domain, mem_ctx, &num_domains,
157                                                &names, &dom_sids);
158
159         /* Add each domain to the trusted domain list */
160         if (NT_STATUS_IS_OK(result)) {
161                 int i;
162                 for(i = 0; i < num_domains; i++) {
163                         domain = add_trusted_domain(names[i], &cache_methods);
164                         if (!domain) continue;
165                         sid_copy(&domain->sid, &dom_sids[i]);
166                         DEBUG(1,("Added domain %s (%s)\n", 
167                                  domain->name, 
168                                  sid_string_static(&domain->sid)));
169                 }
170         }
171
172         talloc_destroy(mem_ctx);
173         return True;
174 }
175
176 /* Given a domain name, return the struct winbindd domain info for it 
177    if it is actually working. */
178
179 struct winbindd_domain *find_domain_from_name(const char *domain_name)
180 {
181         struct winbindd_domain *domain;
182
183         /* Search through list */
184
185         for (domain = domain_list(); domain != NULL; domain = domain->next) {
186                 if (strequal(domain_name, domain->name) ||
187                     strequal(domain_name, domain->full_name))
188                         return domain;
189         }
190
191         /* Not found */
192
193         return NULL;
194 }
195
196 /* Given a domain sid, return the struct winbindd domain info for it */
197
198 struct winbindd_domain *find_domain_from_sid(DOM_SID *sid)
199 {
200         struct winbindd_domain *domain;
201
202         /* Search through list */
203
204         for (domain = domain_list(); domain != NULL; domain = domain->next) {
205                 if (sid_compare_domain(sid, &domain->sid) == 0)
206                         return domain;
207         }
208
209         /* Not found */
210
211         return NULL;
212 }
213
214 /* Lookup a sid in a domain from a name */
215
216 BOOL winbindd_lookup_sid_by_name(struct winbindd_domain *domain, 
217                                  const char *name, DOM_SID *sid, 
218                                  enum SID_NAME_USE *type)
219 {
220         NTSTATUS result;
221         
222         /* Don't bother with machine accounts */
223         
224         if (name[strlen(name) - 1] == '$')
225                 return False;
226
227         /* Lookup name */
228         result = domain->methods->name_to_sid(domain, name, sid, type);
229         
230         /* Return rid and type if lookup successful */
231         if (!NT_STATUS_IS_OK(result)) {
232                 *type = SID_NAME_UNKNOWN;
233         }
234
235         return NT_STATUS_IS_OK(result);
236 }
237
238 /**
239  * @brief Lookup a name in a domain from a sid.
240  *
241  * @param sid Security ID you want to look up.
242  *
243  * @param name On success, set to the name corresponding to @p sid.
244  * 
245  * @param dom_name On success, set to the 'domain name' corresponding to @p sid.
246  * 
247  * @param type On success, contains the type of name: alias, group or
248  * user.
249  *
250  * @retval True if the name exists, in which case @p name and @p type
251  * are set, otherwise False.
252  **/
253 BOOL winbindd_lookup_name_by_sid(DOM_SID *sid,
254                                  fstring dom_name,
255                                  fstring name,
256                                  enum SID_NAME_USE *type)
257 {
258         char *names;
259         NTSTATUS result;
260         TALLOC_CTX *mem_ctx;
261         BOOL rv = False;
262         struct winbindd_domain *domain;
263
264         domain = find_domain_from_sid(sid);
265
266         if (!domain) {
267                 DEBUG(1,("Can't find domain from sid\n"));
268                 return False;
269         }
270
271         /* Lookup name */
272
273         if (!(mem_ctx = talloc_init_named("winbindd_lookup_name_by_sid")))
274                 return False;
275         
276         result = domain->methods->sid_to_name(domain, mem_ctx, sid, &names, type);
277
278         /* Return name and type if successful */
279         
280         if ((rv = NT_STATUS_IS_OK(result))) {
281                 fstrcpy(dom_name, domain->name);
282                 fstrcpy(name, names);
283         } else {
284                 *type = SID_NAME_UNKNOWN;
285                 fstrcpy(name, name_deadbeef);
286         }
287         
288         talloc_destroy(mem_ctx);
289
290         return rv;
291 }
292
293
294 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
295
296 void free_getent_state(struct getent_state *state)
297 {
298         struct getent_state *temp;
299
300         /* Iterate over state list */
301
302         temp = state;
303
304         while(temp != NULL) {
305                 struct getent_state *next;
306
307                 /* Free sam entries then list entry */
308
309                 SAFE_FREE(state->sam_entries);
310                 DLIST_REMOVE(state, state);
311                 next = temp->next;
312
313                 SAFE_FREE(temp);
314                 temp = next;
315         }
316 }
317
318 /* Initialise trusted domain info */
319
320 BOOL winbindd_param_init(void)
321 {
322         /* Parse winbind uid and winbind_gid parameters */
323
324         if (!lp_winbind_uid(&server_state.uid_low, &server_state.uid_high)) {
325                 DEBUG(0, ("winbind uid range missing or invalid\n"));
326                 return False;
327         }
328         
329         if (!lp_winbind_gid(&server_state.gid_low, &server_state.gid_high)) {
330                 DEBUG(0, ("winbind gid range missing or invalid\n"));
331                 return False;
332         }
333         
334         return True;
335 }
336
337 /* Check if a domain is present in a comma-separated list of domains */
338
339 BOOL check_domain_env(char *domain_env, char *domain)
340 {
341         fstring name;
342         char *tmp = domain_env;
343
344         while(next_token(&tmp, name, ",", sizeof(fstring))) {
345                 if (strequal(name, domain))
346                         return True;
347         }
348
349         return False;
350 }
351
352 /* Parse a string of the form DOMAIN/user into a domain and a user */
353 extern fstring global_myworkgroup;
354
355 BOOL parse_domain_user(const char *domuser, fstring domain, fstring user)
356 {
357         char *p = strchr(domuser,*lp_winbind_separator());
358
359         if (!(p || lp_winbind_use_default_domain()))
360                 return False;
361         
362         if(!p && lp_winbind_use_default_domain()) {
363                 fstrcpy(user, domuser);
364                 fstrcpy(domain, global_myworkgroup);
365         } else {
366                 fstrcpy(user, p+1);
367                 fstrcpy(domain, domuser);
368                 domain[PTR_DIFF(p, domuser)] = 0;
369         }
370         strupper(domain);
371         return True;
372 }
373
374 /*
375     Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
376     'winbind separator' options.
377     This means:
378         - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
379         global_myworkgroup
380          
381 */
382 void fill_domain_username(fstring name, const char *domain, const char *user)
383 {
384         if(lp_winbind_use_default_domain() &&
385             !strcmp(global_myworkgroup, domain)) {
386                 strlcpy(name, user, sizeof(fstring));
387         } else {
388                 slprintf(name, sizeof(fstring) - 1, "%s%s%s",
389                          domain, lp_winbind_separator(),
390                          user);
391         }
392 }