r23779: Change from v2 or later to v3 or later.
[kai/samba.git] / source / nsswitch / winbindd_passdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind rpc backend functions
5
6    Copyright (C) Tim Potter 2000-2001,2003
7    Copyright (C) Simo Sorce 2003
8    Copyright (C) Volker Lendecke 2004
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "winbindd.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_WINBIND
30
31 /* Query display info for a domain.  This returns enough information plus a
32    bit extra to give an overview of domain users for the User Manager
33    application. */
34 static NTSTATUS query_user_list(struct winbindd_domain *domain,
35                                TALLOC_CTX *mem_ctx,
36                                uint32 *num_entries, 
37                                WINBIND_USERINFO **info)
38 {
39         /* We don't have users */
40         *num_entries = 0;
41         *info = NULL;
42         return NT_STATUS_OK;
43 }
44
45 /* list all domain groups */
46 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
47                                 TALLOC_CTX *mem_ctx,
48                                 uint32 *num_entries, 
49                                 struct acct_info **info)
50 {
51         /* We don't have domain groups */
52         *num_entries = 0;
53         *info = NULL;
54         return NT_STATUS_OK;
55 }
56
57 /* List all domain groups */
58
59 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
60                                 TALLOC_CTX *mem_ctx,
61                                 uint32 *num_entries, 
62                                 struct acct_info **info)
63 {
64         struct pdb_search *search;
65         struct samr_displayentry *aliases;
66         int i;
67         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
68
69         search = pdb_search_aliases(&domain->sid);
70         if (search == NULL) goto done;
71
72         *num_entries = pdb_search_entries(search, 0, 0xffffffff, &aliases);
73         if (*num_entries == 0) goto done;
74
75         *info = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
76         if (*info == NULL) {
77                 result = NT_STATUS_NO_MEMORY;
78                 goto done;
79         }
80
81         for (i=0; i<*num_entries; i++) {
82                 fstrcpy((*info)[i].acct_name, aliases[i].account_name);
83                 fstrcpy((*info)[i].acct_desc, aliases[i].description);
84                 (*info)[i].rid = aliases[i].rid;
85         }
86
87         result = NT_STATUS_OK;
88  done:
89         pdb_search_destroy(search);
90         return result;
91 }
92
93 /* convert a single name to a sid in a domain */
94 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
95                             TALLOC_CTX *mem_ctx,
96                             enum winbindd_cmd original_cmd,
97                             const char *domain_name,
98                             const char *name,
99                             DOM_SID *sid,
100                             enum lsa_SidType *type)
101 {
102         uint32 flags = LOOKUP_NAME_ALL;
103
104         switch ( original_cmd ) {
105         case WINBINDD_LOOKUPNAME:
106                 /* This call is ok */
107                 break;
108         default:
109                 /* Avoid any NSS calls in the lookup_name by default */
110                 flags |= LOOKUP_NAME_EXPLICIT;
111                 DEBUG(10,("winbindd_passdb: limiting name_to_sid() to explicit mappings\n"));
112                 break;
113         }
114         
115         DEBUG(10, ("Finding name %s\n", name));
116
117         if ( !lookup_name( mem_ctx, name, flags, NULL, NULL, sid, type ) ) {
118                 return NT_STATUS_NONE_MAPPED;
119         }
120
121         return NT_STATUS_OK;
122 }
123
124 /*
125   convert a domain SID to a user or group name
126 */
127 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
128                             TALLOC_CTX *mem_ctx,
129                             const DOM_SID *sid,
130                             char **domain_name,
131                             char **name,
132                             enum lsa_SidType *type)
133 {
134         const char *dom, *nam;
135
136         DEBUG(10, ("Converting SID %s\n", sid_string_static(sid)));
137
138         /* Paranoia check */
139         if (!sid_check_is_in_builtin(sid) &&
140             !sid_check_is_in_our_domain(sid) &&
141             !sid_check_is_in_unix_users(sid) &&
142             !sid_check_is_unix_users(sid) &&
143             !sid_check_is_in_unix_groups(sid) &&
144             !sid_check_is_unix_groups(sid) )
145         {
146                 DEBUG(0, ("Possible deadlock: Trying to lookup SID %s with "
147                           "passdb backend\n", sid_string_static(sid)));
148                 return NT_STATUS_NONE_MAPPED;
149         }
150
151         if (!lookup_sid(mem_ctx, sid, &dom, &nam, type)) {
152                 return NT_STATUS_NONE_MAPPED;
153         }
154
155         *domain_name = talloc_strdup(mem_ctx, dom);
156         *name = talloc_strdup(mem_ctx, nam);
157
158         return NT_STATUS_OK;
159 }
160
161 static NTSTATUS rids_to_names(struct winbindd_domain *domain,
162                               TALLOC_CTX *mem_ctx,
163                               const DOM_SID *sid,
164                               uint32 *rids,
165                               size_t num_rids,
166                               char **domain_name,
167                               char ***names,
168                               enum lsa_SidType **types)
169 {
170         return NT_STATUS_UNSUCCESSFUL;
171 }
172
173 /* Lookup user information from a rid or username. */
174 static NTSTATUS query_user(struct winbindd_domain *domain, 
175                            TALLOC_CTX *mem_ctx, 
176                            const DOM_SID *user_sid,
177                            WINBIND_USERINFO *user_info)
178 {
179         return NT_STATUS_NO_SUCH_USER;
180 }
181
182 /* Lookup groups a user is a member of.  I wish Unix had a call like this! */
183 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
184                                   TALLOC_CTX *mem_ctx,
185                                   const DOM_SID *user_sid,
186                                   uint32 *num_groups, DOM_SID **user_gids)
187 {
188         NTSTATUS result;
189         DOM_SID *groups = NULL;
190         gid_t *gids = NULL;
191         size_t ngroups = 0;
192         struct samu *user;
193
194         if ( (user = samu_new(mem_ctx)) == NULL ) {
195                 return NT_STATUS_NO_MEMORY;
196         }
197
198         if ( !pdb_getsampwsid( user, user_sid ) ) {
199                 return NT_STATUS_NO_SUCH_USER;
200         }
201
202         result = pdb_enum_group_memberships( mem_ctx, user, &groups, &gids, &ngroups );
203
204         TALLOC_FREE( user );
205
206         *num_groups = (uint32)ngroups;
207         *user_gids = groups;
208
209         return result;
210 }
211
212 static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
213                                    TALLOC_CTX *mem_ctx,
214                                    uint32 num_sids, const DOM_SID *sids,
215                                    uint32 *p_num_aliases, uint32 **rids)
216 {
217         NTSTATUS result;
218         size_t num_aliases = 0;
219
220         result = pdb_enum_alias_memberships(mem_ctx, &domain->sid,
221                                             sids, num_sids, rids, &num_aliases);
222
223         *p_num_aliases = num_aliases;
224         return result;
225 }
226
227 /* Lookup group membership given a rid.   */
228 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
229                                 TALLOC_CTX *mem_ctx,
230                                 const DOM_SID *group_sid, uint32 *num_names, 
231                                 DOM_SID **sid_mem, char ***names, 
232                                 uint32 **name_types)
233 {
234         size_t i, num_members, num_mapped;
235         uint32 *rids;
236         NTSTATUS result;
237         const DOM_SID **sids;
238         struct lsa_dom_info *lsa_domains;
239         struct lsa_name_info *lsa_names;
240         TALLOC_CTX *tmp_ctx;
241
242         if (!sid_check_is_in_our_domain(group_sid)) {
243                 /* There's no groups, only aliases in BUILTIN */
244                 return NT_STATUS_NO_SUCH_GROUP;
245         }
246
247         if (!(tmp_ctx = talloc_init("lookup_groupmem"))) {
248                 return NT_STATUS_NO_MEMORY;
249         }
250
251         result = pdb_enum_group_members(tmp_ctx, group_sid, &rids,
252                                         &num_members);
253         if (!NT_STATUS_IS_OK(result)) {
254                 TALLOC_FREE(tmp_ctx);
255                 return result;
256         }
257
258         if (num_members == 0) {
259                 *num_names = 0;
260                 *sid_mem = NULL;
261                 *names = NULL;
262                 *name_types = NULL;
263                 TALLOC_FREE(tmp_ctx);
264                 return NT_STATUS_OK;
265         }
266
267         *sid_mem = TALLOC_ARRAY(mem_ctx, DOM_SID, num_members);
268         *names = TALLOC_ARRAY(mem_ctx, char *, num_members);
269         *name_types = TALLOC_ARRAY(mem_ctx, uint32, num_members);
270         sids = TALLOC_ARRAY(tmp_ctx, const DOM_SID *, num_members);
271
272         if (((*sid_mem) == NULL) || ((*names) == NULL) ||
273             ((*name_types) == NULL) || (sids == NULL)) {
274                 TALLOC_FREE(tmp_ctx);
275                 return NT_STATUS_NO_MEMORY;
276         }
277
278         /*
279          * Prepare an array of sid pointers for the lookup_sids calling
280          * convention.
281          */
282
283         for (i=0; i<num_members; i++) {
284                 DOM_SID *sid = &((*sid_mem)[i]);
285                 if (!sid_compose(sid, &domain->sid, rids[i])) {
286                         TALLOC_FREE(tmp_ctx);
287                         return NT_STATUS_INTERNAL_ERROR;
288                 }
289                 sids[i] = sid;
290         }
291
292         result = lookup_sids(tmp_ctx, num_members, sids, 1,
293                              &lsa_domains, &lsa_names);
294         if (!NT_STATUS_IS_OK(result)) {
295                 TALLOC_FREE(tmp_ctx);
296                 return result;
297         }
298
299         num_mapped = 0;
300         for (i=0; i<num_members; i++) {
301                 if (lsa_names[i].type != SID_NAME_USER) {
302                         DEBUG(2, ("Got %s as group member -- ignoring\n",
303                                   sid_type_lookup(lsa_names[i].type)));
304                         continue;
305                 }
306                 if (!((*names)[i] = talloc_strdup((*names),
307                                                   lsa_names[i].name))) {
308                         TALLOC_FREE(tmp_ctx);
309                         return NT_STATUS_NO_MEMORY;
310                 }
311
312                 (*name_types)[i] = lsa_names[i].type;
313
314                 num_mapped += 1;
315         }
316
317         *num_names = num_mapped;
318
319         TALLOC_FREE(tmp_ctx);
320         return NT_STATUS_OK;
321 }
322
323 /* find the sequence number for a domain */
324 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
325 {
326         BOOL result;
327         time_t seq_num;
328
329         result = pdb_get_seq_num(&seq_num);
330         if (!result) {
331                 *seq = 1;
332         }
333
334         *seq = (int) seq_num;
335         /* *seq = 1; */
336         return NT_STATUS_OK;
337 }
338
339 static NTSTATUS lockout_policy(struct winbindd_domain *domain,
340                                TALLOC_CTX *mem_ctx,
341                                SAM_UNK_INFO_12 *policy)
342 {
343         /* actually we have that */
344         return NT_STATUS_NOT_IMPLEMENTED;
345 }
346
347 static NTSTATUS password_policy(struct winbindd_domain *domain,
348                                 TALLOC_CTX *mem_ctx,
349                                 SAM_UNK_INFO_1 *policy)
350 {
351         uint32 min_pass_len,pass_hist,password_properties;
352         time_t u_expire, u_min_age;
353         NTTIME nt_expire, nt_min_age;
354         uint32 account_policy_temp;
355
356         if ((policy = TALLOC_ZERO_P(mem_ctx, SAM_UNK_INFO_1)) == NULL) {
357                 return NT_STATUS_NO_MEMORY;
358         }
359
360         if (!pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp)) {
361                 return NT_STATUS_ACCESS_DENIED;
362         }
363         min_pass_len = account_policy_temp;
364
365         if (!pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp)) {
366                 return NT_STATUS_ACCESS_DENIED;
367         }
368         pass_hist = account_policy_temp;
369
370         if (!pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp)) {
371                 return NT_STATUS_ACCESS_DENIED;
372         }
373         password_properties = account_policy_temp;
374         
375         if (!pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp)) {
376                 return NT_STATUS_ACCESS_DENIED;
377         }
378         u_expire = account_policy_temp;
379
380         if (!pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp)) {
381                 return NT_STATUS_ACCESS_DENIED;
382         }
383         u_min_age = account_policy_temp;
384
385         unix_to_nt_time_abs(&nt_expire, u_expire);
386         unix_to_nt_time_abs(&nt_min_age, u_min_age);
387
388         init_unk_info1(policy, (uint16)min_pass_len, (uint16)pass_hist, 
389                        password_properties, nt_expire, nt_min_age);
390
391         return NT_STATUS_OK;
392 }
393
394 /* get a list of trusted domains */
395 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
396                                 TALLOC_CTX *mem_ctx,
397                                 uint32 *num_domains,
398                                 char ***names,
399                                 char ***alt_names,
400                                 DOM_SID **dom_sids)
401 {
402         NTSTATUS nt_status;
403         struct trustdom_info **domains;
404         int i;
405         TALLOC_CTX *tmp_ctx;
406
407         *num_domains = 0;
408         *names = NULL;
409         *alt_names = NULL;
410         *dom_sids = NULL;
411
412         if (!(tmp_ctx = talloc_init("trusted_domains"))) {
413                 return NT_STATUS_NO_MEMORY;
414         }
415
416         nt_status = pdb_enum_trusteddoms(tmp_ctx, num_domains, &domains);
417         if (!NT_STATUS_IS_OK(nt_status)) {
418                 TALLOC_FREE(tmp_ctx);
419                 return nt_status;
420         }
421
422         if (*num_domains) {
423                 *names = TALLOC_ARRAY(mem_ctx, char *, *num_domains);
424                 *alt_names = TALLOC_ARRAY(mem_ctx, char *, *num_domains);
425                 *dom_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_domains);
426
427                 if ((*alt_names == NULL) || (*names == NULL) || (*dom_sids == NULL)) {
428                         TALLOC_FREE(tmp_ctx);
429                         return NT_STATUS_NO_MEMORY;
430                 }
431         } else {
432                 *names = NULL;
433                 *alt_names = NULL;
434                 *dom_sids = NULL;
435         }
436
437         for (i=0; i<*num_domains; i++) {
438                 (*alt_names)[i] = NULL;
439                 if (!((*names)[i] = talloc_strdup((*names),
440                                                   domains[i]->name))) {
441                         TALLOC_FREE(tmp_ctx);
442                         return NT_STATUS_NO_MEMORY;
443                 }
444                 sid_copy(&(*dom_sids)[i], &domains[i]->sid);
445         }
446
447         TALLOC_FREE(tmp_ctx);
448         return NT_STATUS_OK;
449 }
450
451 /* the rpc backend methods are exposed via this structure */
452 struct winbindd_methods passdb_methods = {
453         False,
454         query_user_list,
455         enum_dom_groups,
456         enum_local_groups,
457         name_to_sid,
458         sid_to_name,
459         rids_to_names,
460         query_user,
461         lookup_usergroups,
462         lookup_useraliases,
463         lookup_groupmem,
464         sequence_number,
465         lockout_policy,
466         password_policy,
467         trusted_domains,
468 };