Merge branch 'master' of git://git.samba.org/samba into teventfix
[samba.git] / source3 / passdb / pdb_wbc_sam.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Password and authentication handling by wbclient
5
6    Copyright (C) Andrew Bartlett                        2002
7    Copyright (C) Jelmer Vernooij                        2002
8    Copyright (C) Simo Sorce                             2003
9    Copyright (C) Volker Lendecke                        2006
10    Copyright (C) Dan Sledz                              2009
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 /* This passdb module retrieves full passdb information for local users and
27  * groups from a wbclient compatible daemon.
28  *
29  * The purpose of this module is to defer all SAM authorization information
30  * storage and retrieval to a wbc compatible daemon.
31  *
32  * This passdb backend is most useful when used in conjunction with auth_wbc.
33  *
34  * A few current limitations of this module are:
35  *   - read only interface
36  *   - no privileges
37  */
38
39 #include "includes.h"
40
41 /***************************************************************************
42   Default implementations of some functions.
43  ****************************************************************************/
44 static NTSTATUS _pdb_wbc_sam_getsampw(struct pdb_methods *methods,
45                                        struct samu *user,
46                                        const struct passwd *pwd)
47 {
48         NTSTATUS result = NT_STATUS_OK;
49
50         if (pwd == NULL)
51                 return NT_STATUS_NO_SUCH_USER;
52
53         memset(user, 0, sizeof(user));
54
55         /* Can we really get away with this little of information */
56         user->methods = methods;
57         result = samu_set_unix(user, pwd);
58
59         return result;
60 }
61
62 static NTSTATUS pdb_wbc_sam_getsampwnam(struct pdb_methods *methods, struct samu *user, const char *sname)
63 {
64         return _pdb_wbc_sam_getsampw(methods, user, winbind_getpwnam(sname));
65 }
66
67 static NTSTATUS pdb_wbc_sam_getsampwsid(struct pdb_methods *methods, struct samu *user, const DOM_SID *sid)
68 {
69         return _pdb_wbc_sam_getsampw(methods, user, winbind_getpwsid(sid));
70 }
71
72 static bool pdb_wbc_sam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
73                                    DOM_SID *sid)
74 {
75         return winbind_uid_to_sid(sid, uid);
76 }
77
78 static bool pdb_wbc_sam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
79                                    DOM_SID *sid)
80 {
81         return winbind_gid_to_sid(sid, gid);
82 }
83
84 static bool pdb_wbc_sam_sid_to_id(struct pdb_methods *methods,
85                                   const DOM_SID *sid,
86                                   union unid_t *id, enum lsa_SidType *type)
87 {
88         if (winbind_sid_to_uid(&id->uid, sid)) {
89                 *type = SID_NAME_USER;
90         } else if (winbind_sid_to_gid(&id->gid, sid)) {
91                 /* We assume all gids are groups, not aliases */
92                 *type = SID_NAME_DOM_GRP;
93         } else {
94                 return false;
95         }
96
97         return true;
98 }
99
100 static NTSTATUS pdb_wbc_sam_enum_group_members(struct pdb_methods *methods,
101                                                TALLOC_CTX *mem_ctx,
102                                                const DOM_SID *group,
103                                                uint32 **pp_member_rids,
104                                                size_t *p_num_members)
105 {
106         return NT_STATUS_NOT_IMPLEMENTED;
107 }
108
109 static NTSTATUS pdb_wbc_sam_enum_group_memberships(struct pdb_methods *methods,
110                                                    TALLOC_CTX *mem_ctx,
111                                                    struct samu *user,
112                                                    DOM_SID **pp_sids,
113                                                    gid_t **pp_gids,
114                                                    size_t *p_num_groups)
115 {
116         size_t i;
117         const char *username = pdb_get_username(user);
118         uint32_t num_groups;
119
120         if (!winbind_get_groups(mem_ctx, username, &num_groups, pp_gids)) {
121                 return NT_STATUS_NO_SUCH_USER;
122         }
123         *p_num_groups = num_groups;
124
125         if (*p_num_groups == 0) {
126                 smb_panic("primary group missing");
127         }
128
129         *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
130
131         if (*pp_sids == NULL) {
132                 TALLOC_FREE(*pp_gids);
133                 return NT_STATUS_NO_MEMORY;
134         }
135
136         for (i=0; i < *p_num_groups; i++) {
137                 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
138         }
139
140         return NT_STATUS_OK;
141 }
142
143 static NTSTATUS pdb_wbc_sam_lookup_rids(struct pdb_methods *methods,
144                                         const DOM_SID *domain_sid,
145                                         int num_rids,
146                                         uint32 *rids,
147                                         const char **names,
148                                         enum lsa_SidType *attrs)
149 {
150         NTSTATUS result = NT_STATUS_OK;
151         char *domain = NULL;
152         char **account_names = NULL;
153         char name[256];
154         enum lsa_SidType *attr_list = NULL;
155         int i;
156
157         if (!winbind_lookup_rids(talloc_tos(), domain_sid, num_rids, rids,
158                                  (const char **)&domain,
159                                  (const char ***)&account_names, &attr_list))
160         {
161                 result = NT_STATUS_NONE_MAPPED;
162                 goto done;
163         }
164
165         memcpy(attrs, attr_list, num_rids * sizeof(enum lsa_SidType));
166
167         for (i=0; i<num_rids; i++) {
168                 if (attrs[i] == SID_NAME_UNKNOWN) {
169                         names[i] = NULL;
170                 } else {
171                         snprintf(name, sizeof(name), "%s%c%s", domain,
172                                  *lp_winbind_separator(), account_names[i]);
173                         names[i] = talloc_strdup(names, name);
174                 }
175         }
176
177 done:
178         TALLOC_FREE(account_names);
179         TALLOC_FREE(domain);
180         TALLOC_FREE(attrs);
181         return result;
182 }
183
184 static NTSTATUS pdb_wbc_sam_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
185 {
186         return NT_STATUS_UNSUCCESSFUL;
187 }
188
189 static NTSTATUS pdb_wbc_sam_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
190 {
191         return NT_STATUS_UNSUCCESSFUL;
192 }
193
194 static bool pdb_wbc_sam_search_groups(struct pdb_methods *methods,
195                                       struct pdb_search *search)
196 {
197         return false;
198 }
199
200 static bool pdb_wbc_sam_search_aliases(struct pdb_methods *methods,
201                                        struct pdb_search *search,
202                                        const DOM_SID *sid)
203 {
204
205         return false;
206 }
207
208 static bool pdb_wbc_sam_get_trusteddom_pw(struct pdb_methods *methods,
209                                           const char *domain,
210                                           char **pwd,
211                                           DOM_SID *sid,
212                                           time_t *pass_last_set_time)
213 {
214         return false;
215
216 }
217
218 static bool pdb_wbc_sam_set_trusteddom_pw(struct pdb_methods *methods,
219                                           const char *domain,
220                                           const char *pwd,
221                                           const DOM_SID *sid)
222 {
223         return false;
224 }
225
226 static bool pdb_wbc_sam_del_trusteddom_pw(struct pdb_methods *methods,
227                                           const char *domain)
228 {
229         return false;
230 }
231
232 static NTSTATUS pdb_wbc_sam_enum_trusteddoms(struct pdb_methods *methods,
233                                              TALLOC_CTX *mem_ctx,
234                                              uint32 *num_domains,
235                                              struct trustdom_info ***domains)
236 {
237         return NT_STATUS_NOT_IMPLEMENTED;
238 }
239
240 static bool _make_group_map(struct pdb_methods *methods, const char *domain, const char *name, enum lsa_SidType name_type, gid_t gid, DOM_SID *sid, GROUP_MAP *map)
241 {
242         snprintf(map->nt_name, sizeof(map->nt_name), "%s%c%s",
243                 domain, *lp_winbind_separator(), name);
244         map->sid_name_use = name_type;
245         map->sid = *sid;
246         map->gid = gid;
247         return true;
248 }
249
250 static NTSTATUS pdb_wbc_sam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
251                                  DOM_SID sid)
252 {
253         NTSTATUS result = NT_STATUS_OK;
254         char *name = NULL;
255         char *domain = NULL;
256         enum lsa_SidType name_type;
257         gid_t gid;
258
259         if (!winbind_lookup_sid(talloc_tos(), &sid, (const char **)&domain,
260                                 (const char **) &name, &name_type)) {
261                 result = NT_STATUS_NO_SUCH_GROUP;
262                 goto done;
263         }
264
265         if ((name_type != SID_NAME_DOM_GRP) &&
266             (name_type != SID_NAME_DOMAIN) &&
267             (name_type != SID_NAME_ALIAS) &&
268             (name_type != SID_NAME_WKN_GRP)) {
269                 result = NT_STATUS_NO_SUCH_GROUP;
270                 goto done;
271         }
272
273         if (!winbind_sid_to_gid(&gid, &sid)) {
274                 result = NT_STATUS_NO_SUCH_GROUP;
275                 goto done;
276         }
277
278         if (!_make_group_map(methods, domain, name, name_type, gid, &sid, map)) {
279                 result = NT_STATUS_NO_SUCH_GROUP;
280                 goto done;
281         }
282
283 done:
284         TALLOC_FREE(name);
285         TALLOC_FREE(domain);
286         return result;
287 }
288
289 static NTSTATUS pdb_wbc_sam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
290                                  gid_t gid)
291 {
292         NTSTATUS result = NT_STATUS_OK;
293         char *name = NULL;
294         char *domain = NULL;
295         DOM_SID sid;
296         enum lsa_SidType name_type;
297
298         if (!winbind_gid_to_sid(&sid, gid)) {
299                 result = NT_STATUS_NO_SUCH_GROUP;
300                 goto done;
301         }
302
303         if (!winbind_lookup_sid(talloc_tos(), &sid, (const char **)&domain,
304                                 (const char **)&name, &name_type)) {
305                 result = NT_STATUS_NO_SUCH_GROUP;
306                 goto done;
307         }
308
309         if ((name_type != SID_NAME_DOM_GRP) &&
310             (name_type != SID_NAME_DOMAIN) &&
311             (name_type != SID_NAME_ALIAS) &&
312             (name_type != SID_NAME_WKN_GRP)) {
313                 result = NT_STATUS_NO_SUCH_GROUP;
314                 goto done;
315         }
316
317         if (!_make_group_map(methods, domain, name, name_type, gid, &sid, map)) {
318                 result = NT_STATUS_NO_SUCH_GROUP;
319                 goto done;
320         }
321
322 done:
323         TALLOC_FREE(name);
324         TALLOC_FREE(domain);
325
326         return result;
327 }
328
329 static NTSTATUS pdb_wbc_sam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
330                                  const char *name)
331 {
332         NTSTATUS result = NT_STATUS_OK;
333         char *user_name = NULL;
334         char *domain = NULL;
335         DOM_SID sid;
336         gid_t gid;
337         enum lsa_SidType name_type;
338
339         if (!winbind_lookup_name(domain, user_name, &sid, &name_type)) {
340                 result = NT_STATUS_NO_SUCH_GROUP;
341                 goto done;
342         }
343
344         if ((name_type != SID_NAME_DOM_GRP) &&
345             (name_type != SID_NAME_DOMAIN) &&
346             (name_type != SID_NAME_ALIAS) &&
347             (name_type != SID_NAME_WKN_GRP)) {
348                 result = NT_STATUS_NO_SUCH_GROUP;
349                 goto done;
350         }
351
352         if (!winbind_sid_to_gid(&gid, &sid)) {
353                 result = NT_STATUS_NO_SUCH_GROUP;
354                 goto done;
355         }
356
357         if (!_make_group_map(methods, domain, user_name, name_type, gid, &sid, map)) {
358                 result = NT_STATUS_NO_SUCH_GROUP;
359                 goto done;
360         }
361
362 done:
363
364         return result;
365 }
366
367 static NTSTATUS pdb_wbc_sam_enum_group_mapping(struct pdb_methods *methods,
368                                            const DOM_SID *sid, enum lsa_SidType sid_name_use,
369                                            GROUP_MAP **pp_rmap, size_t *p_num_entries,
370                                            bool unix_only)
371 {
372         return NT_STATUS_NOT_IMPLEMENTED;
373 }
374
375 static NTSTATUS pdb_wbc_sam_get_aliasinfo(struct pdb_methods *methods,
376                                    const DOM_SID *sid,
377                                    struct acct_info *info)
378 {
379         return NT_STATUS_NOT_IMPLEMENTED;
380 }
381
382 static NTSTATUS pdb_wbc_sam_enum_aliasmem(struct pdb_methods *methods,
383                                    const DOM_SID *alias, DOM_SID **pp_members,
384                                    size_t *p_num_members)
385 {
386         return NT_STATUS_NOT_IMPLEMENTED;
387 }
388
389 static NTSTATUS pdb_wbc_sam_alias_memberships(struct pdb_methods *methods,
390                                        TALLOC_CTX *mem_ctx,
391                                        const DOM_SID *domain_sid,
392                                        const DOM_SID *members,
393                                        size_t num_members,
394                                        uint32 **pp_alias_rids,
395                                        size_t *p_num_alias_rids)
396 {
397         if (!winbind_get_sid_aliases(mem_ctx, domain_sid,
398                     members, num_members, pp_alias_rids, p_num_alias_rids))
399                 return NT_STATUS_UNSUCCESSFUL;
400
401         return NT_STATUS_OK;
402 }
403
404 static NTSTATUS pdb_init_wbc_sam(struct pdb_methods **pdb_method, const char *location)
405 {
406         NTSTATUS result;
407
408         if (!NT_STATUS_IS_OK(result = make_pdb_method( pdb_method))) {
409                 return result;
410         }
411
412         (*pdb_method)->name = "wbc_sam";
413
414         (*pdb_method)->getsampwnam = pdb_wbc_sam_getsampwnam;
415         (*pdb_method)->getsampwsid = pdb_wbc_sam_getsampwsid;
416
417         (*pdb_method)->getgrsid = pdb_wbc_sam_getgrsid;
418         (*pdb_method)->getgrgid = pdb_wbc_sam_getgrgid;
419         (*pdb_method)->getgrnam = pdb_wbc_sam_getgrnam;
420         (*pdb_method)->enum_group_mapping = pdb_wbc_sam_enum_group_mapping;
421         (*pdb_method)->enum_group_members = pdb_wbc_sam_enum_group_members;
422         (*pdb_method)->enum_group_memberships = pdb_wbc_sam_enum_group_memberships;
423         (*pdb_method)->get_aliasinfo = pdb_wbc_sam_get_aliasinfo;
424         (*pdb_method)->enum_aliasmem = pdb_wbc_sam_enum_aliasmem;
425         (*pdb_method)->enum_alias_memberships = pdb_wbc_sam_alias_memberships;
426         (*pdb_method)->lookup_rids = pdb_wbc_sam_lookup_rids;
427         (*pdb_method)->get_account_policy = pdb_wbc_sam_get_account_policy;
428         (*pdb_method)->set_account_policy = pdb_wbc_sam_set_account_policy;
429         (*pdb_method)->uid_to_sid = pdb_wbc_sam_uid_to_sid;
430         (*pdb_method)->gid_to_sid = pdb_wbc_sam_gid_to_sid;
431         (*pdb_method)->sid_to_id = pdb_wbc_sam_sid_to_id;
432
433         (*pdb_method)->search_groups = pdb_wbc_sam_search_groups;
434         (*pdb_method)->search_aliases = pdb_wbc_sam_search_aliases;
435
436         (*pdb_method)->get_trusteddom_pw = pdb_wbc_sam_get_trusteddom_pw;
437         (*pdb_method)->set_trusteddom_pw = pdb_wbc_sam_set_trusteddom_pw;
438         (*pdb_method)->del_trusteddom_pw = pdb_wbc_sam_del_trusteddom_pw;
439         (*pdb_method)->enum_trusteddoms  = pdb_wbc_sam_enum_trusteddoms;
440
441         (*pdb_method)->private_data = NULL;
442         (*pdb_method)->free_private_data = NULL;
443
444         return NT_STATUS_OK;
445 }
446
447 NTSTATUS pdb_wbc_sam_init(void)
448 {
449         return smb_register_passdb(PASSDB_INTERFACE_VERSION, "wbc_sam", pdb_init_wbc_sam);
450 }