selftest: add a fake root user to nss_wrapper_passwd in s3.
[Samba/gbeck.git] / source3 / passdb / pdb_wbc_sam.c
blobe8116d0995210b969bcb3a27c3869ac15268b768
1 /*
2 Unix SMB/CIFS implementation.
4 Password and authentication handling by wbclient
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
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.
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.
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/>.
26 /* This passdb module retrieves full passdb information for local users and
27 * groups from a wbclient compatible daemon.
29 * The purpose of this module is to defer all SAM authorization information
30 * storage and retrieval to a wbc compatible daemon.
32 * This passdb backend is most useful when used in conjunction with auth_wbc.
34 * A few current limitations of this module are:
35 * - read only interface
36 * - no privileges
39 #include "includes.h"
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)
48 NTSTATUS result = NT_STATUS_OK;
50 if (pwd == NULL)
51 return NT_STATUS_NO_SUCH_USER;
53 memset(user, 0, sizeof(user));
55 /* Can we really get away with this little of information */
56 user->methods = methods;
57 result = samu_set_unix(user, pwd);
59 return result;
62 static NTSTATUS pdb_wbc_sam_getsampwnam(struct pdb_methods *methods, struct samu *user, const char *sname)
64 return _pdb_wbc_sam_getsampw(methods, user, winbind_getpwnam(sname));
67 static NTSTATUS pdb_wbc_sam_getsampwsid(struct pdb_methods *methods, struct samu *user, const DOM_SID *sid)
69 return _pdb_wbc_sam_getsampw(methods, user, winbind_getpwsid(sid));
72 static bool pdb_wbc_sam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
73 DOM_SID *sid)
75 return winbind_uid_to_sid(sid, uid);
78 static bool pdb_wbc_sam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
79 DOM_SID *sid)
81 return winbind_gid_to_sid(sid, gid);
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)
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;
97 return true;
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)
106 return NT_STATUS_NOT_IMPLEMENTED;
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)
116 size_t i;
117 const char *username = pdb_get_username(user);
118 uint32_t num_groups;
120 if (!winbind_get_groups(mem_ctx, username, &num_groups, pp_gids)) {
121 return NT_STATUS_NO_SUCH_USER;
123 *p_num_groups = num_groups;
125 if (*p_num_groups == 0) {
126 smb_panic("primary group missing");
129 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
131 if (*pp_sids == NULL) {
132 TALLOC_FREE(*pp_gids);
133 return NT_STATUS_NO_MEMORY;
136 for (i=0; i < *p_num_groups; i++) {
137 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
140 return NT_STATUS_OK;
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)
150 NTSTATUS result = NT_STATUS_OK;
151 char *domain = NULL;
152 char **account_names = NULL;
153 enum lsa_SidType *attr_list = NULL;
154 int i;
156 if (!winbind_lookup_rids(talloc_tos(), domain_sid, num_rids, rids,
157 (const char **)&domain,
158 (const char ***)&account_names, &attr_list))
160 result = NT_STATUS_NONE_MAPPED;
161 goto done;
164 memcpy(attrs, attr_list, num_rids * sizeof(enum lsa_SidType));
166 for (i=0; i<num_rids; i++) {
167 if (attrs[i] == SID_NAME_UNKNOWN) {
168 names[i] = NULL;
169 } else {
170 names[i] = talloc_strdup(names, account_names[i]);
171 if (names[i] == NULL) {
172 result = NT_STATUS_NO_MEMORY;
173 goto done;
179 done:
180 TALLOC_FREE(account_names);
181 TALLOC_FREE(domain);
182 TALLOC_FREE(attr_list);
183 return result;
186 static NTSTATUS pdb_wbc_sam_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
188 return NT_STATUS_UNSUCCESSFUL;
191 static NTSTATUS pdb_wbc_sam_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
193 return NT_STATUS_UNSUCCESSFUL;
196 static bool pdb_wbc_sam_search_groups(struct pdb_methods *methods,
197 struct pdb_search *search)
199 return false;
202 static bool pdb_wbc_sam_search_aliases(struct pdb_methods *methods,
203 struct pdb_search *search,
204 const DOM_SID *sid)
207 return false;
210 static bool pdb_wbc_sam_get_trusteddom_pw(struct pdb_methods *methods,
211 const char *domain,
212 char **pwd,
213 DOM_SID *sid,
214 time_t *pass_last_set_time)
216 return false;
220 static bool pdb_wbc_sam_set_trusteddom_pw(struct pdb_methods *methods,
221 const char *domain,
222 const char *pwd,
223 const DOM_SID *sid)
225 return false;
228 static bool pdb_wbc_sam_del_trusteddom_pw(struct pdb_methods *methods,
229 const char *domain)
231 return false;
234 static NTSTATUS pdb_wbc_sam_enum_trusteddoms(struct pdb_methods *methods,
235 TALLOC_CTX *mem_ctx,
236 uint32 *num_domains,
237 struct trustdom_info ***domains)
239 return NT_STATUS_NOT_IMPLEMENTED;
242 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)
244 snprintf(map->nt_name, sizeof(map->nt_name), "%s%c%s",
245 domain, *lp_winbind_separator(), name);
246 map->sid_name_use = name_type;
247 map->sid = *sid;
248 map->gid = gid;
249 return true;
252 static NTSTATUS pdb_wbc_sam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
253 DOM_SID sid)
255 NTSTATUS result = NT_STATUS_OK;
256 char *name = NULL;
257 char *domain = NULL;
258 enum lsa_SidType name_type;
259 gid_t gid;
261 if (!winbind_lookup_sid(talloc_tos(), &sid, (const char **)&domain,
262 (const char **) &name, &name_type)) {
263 result = NT_STATUS_NO_SUCH_GROUP;
264 goto done;
267 if ((name_type != SID_NAME_DOM_GRP) &&
268 (name_type != SID_NAME_DOMAIN) &&
269 (name_type != SID_NAME_ALIAS) &&
270 (name_type != SID_NAME_WKN_GRP)) {
271 result = NT_STATUS_NO_SUCH_GROUP;
272 goto done;
275 if (!winbind_sid_to_gid(&gid, &sid)) {
276 result = NT_STATUS_NO_SUCH_GROUP;
277 goto done;
280 if (!_make_group_map(methods, domain, name, name_type, gid, &sid, map)) {
281 result = NT_STATUS_NO_SUCH_GROUP;
282 goto done;
285 done:
286 TALLOC_FREE(name);
287 TALLOC_FREE(domain);
288 return result;
291 static NTSTATUS pdb_wbc_sam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
292 gid_t gid)
294 NTSTATUS result = NT_STATUS_OK;
295 char *name = NULL;
296 char *domain = NULL;
297 DOM_SID sid;
298 enum lsa_SidType name_type;
300 if (!winbind_gid_to_sid(&sid, gid)) {
301 result = NT_STATUS_NO_SUCH_GROUP;
302 goto done;
305 if (!winbind_lookup_sid(talloc_tos(), &sid, (const char **)&domain,
306 (const char **)&name, &name_type)) {
307 result = NT_STATUS_NO_SUCH_GROUP;
308 goto done;
311 if ((name_type != SID_NAME_DOM_GRP) &&
312 (name_type != SID_NAME_DOMAIN) &&
313 (name_type != SID_NAME_ALIAS) &&
314 (name_type != SID_NAME_WKN_GRP)) {
315 result = NT_STATUS_NO_SUCH_GROUP;
316 goto done;
319 if (!_make_group_map(methods, domain, name, name_type, gid, &sid, map)) {
320 result = NT_STATUS_NO_SUCH_GROUP;
321 goto done;
324 done:
325 TALLOC_FREE(name);
326 TALLOC_FREE(domain);
328 return result;
331 static NTSTATUS pdb_wbc_sam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
332 const char *name)
334 NTSTATUS result = NT_STATUS_OK;
335 char *user_name = NULL;
336 char *domain = NULL;
337 DOM_SID sid;
338 gid_t gid;
339 enum lsa_SidType name_type;
341 if (!winbind_lookup_name(domain, user_name, &sid, &name_type)) {
342 result = NT_STATUS_NO_SUCH_GROUP;
343 goto done;
346 if ((name_type != SID_NAME_DOM_GRP) &&
347 (name_type != SID_NAME_DOMAIN) &&
348 (name_type != SID_NAME_ALIAS) &&
349 (name_type != SID_NAME_WKN_GRP)) {
350 result = NT_STATUS_NO_SUCH_GROUP;
351 goto done;
354 if (!winbind_sid_to_gid(&gid, &sid)) {
355 result = NT_STATUS_NO_SUCH_GROUP;
356 goto done;
359 if (!_make_group_map(methods, domain, user_name, name_type, gid, &sid, map)) {
360 result = NT_STATUS_NO_SUCH_GROUP;
361 goto done;
364 done:
366 return result;
369 static NTSTATUS pdb_wbc_sam_enum_group_mapping(struct pdb_methods *methods,
370 const DOM_SID *sid, enum lsa_SidType sid_name_use,
371 GROUP_MAP **pp_rmap, size_t *p_num_entries,
372 bool unix_only)
374 return NT_STATUS_NOT_IMPLEMENTED;
377 static NTSTATUS pdb_wbc_sam_get_aliasinfo(struct pdb_methods *methods,
378 const DOM_SID *sid,
379 struct acct_info *info)
381 return NT_STATUS_NOT_IMPLEMENTED;
384 static NTSTATUS pdb_wbc_sam_enum_aliasmem(struct pdb_methods *methods,
385 const DOM_SID *alias, DOM_SID **pp_members,
386 size_t *p_num_members)
388 return NT_STATUS_NOT_IMPLEMENTED;
391 static NTSTATUS pdb_wbc_sam_alias_memberships(struct pdb_methods *methods,
392 TALLOC_CTX *mem_ctx,
393 const DOM_SID *domain_sid,
394 const DOM_SID *members,
395 size_t num_members,
396 uint32 **pp_alias_rids,
397 size_t *p_num_alias_rids)
399 if (!winbind_get_sid_aliases(mem_ctx, domain_sid,
400 members, num_members, pp_alias_rids, p_num_alias_rids))
401 return NT_STATUS_UNSUCCESSFUL;
403 return NT_STATUS_OK;
406 static NTSTATUS pdb_init_wbc_sam(struct pdb_methods **pdb_method, const char *location)
408 NTSTATUS result;
410 if (!NT_STATUS_IS_OK(result = make_pdb_method( pdb_method))) {
411 return result;
414 (*pdb_method)->name = "wbc_sam";
416 (*pdb_method)->getsampwnam = pdb_wbc_sam_getsampwnam;
417 (*pdb_method)->getsampwsid = pdb_wbc_sam_getsampwsid;
419 (*pdb_method)->getgrsid = pdb_wbc_sam_getgrsid;
420 (*pdb_method)->getgrgid = pdb_wbc_sam_getgrgid;
421 (*pdb_method)->getgrnam = pdb_wbc_sam_getgrnam;
422 (*pdb_method)->enum_group_mapping = pdb_wbc_sam_enum_group_mapping;
423 (*pdb_method)->enum_group_members = pdb_wbc_sam_enum_group_members;
424 (*pdb_method)->enum_group_memberships = pdb_wbc_sam_enum_group_memberships;
425 (*pdb_method)->get_aliasinfo = pdb_wbc_sam_get_aliasinfo;
426 (*pdb_method)->enum_aliasmem = pdb_wbc_sam_enum_aliasmem;
427 (*pdb_method)->enum_alias_memberships = pdb_wbc_sam_alias_memberships;
428 (*pdb_method)->lookup_rids = pdb_wbc_sam_lookup_rids;
429 (*pdb_method)->get_account_policy = pdb_wbc_sam_get_account_policy;
430 (*pdb_method)->set_account_policy = pdb_wbc_sam_set_account_policy;
431 (*pdb_method)->uid_to_sid = pdb_wbc_sam_uid_to_sid;
432 (*pdb_method)->gid_to_sid = pdb_wbc_sam_gid_to_sid;
433 (*pdb_method)->sid_to_id = pdb_wbc_sam_sid_to_id;
435 (*pdb_method)->search_groups = pdb_wbc_sam_search_groups;
436 (*pdb_method)->search_aliases = pdb_wbc_sam_search_aliases;
438 (*pdb_method)->get_trusteddom_pw = pdb_wbc_sam_get_trusteddom_pw;
439 (*pdb_method)->set_trusteddom_pw = pdb_wbc_sam_set_trusteddom_pw;
440 (*pdb_method)->del_trusteddom_pw = pdb_wbc_sam_del_trusteddom_pw;
441 (*pdb_method)->enum_trusteddoms = pdb_wbc_sam_enum_trusteddoms;
443 (*pdb_method)->private_data = NULL;
444 (*pdb_method)->free_private_data = NULL;
446 return NT_STATUS_OK;
449 NTSTATUS pdb_wbc_sam_init(void)
451 return smb_register_passdb(PASSDB_INTERFACE_VERSION, "wbc_sam", pdb_init_wbc_sam);