s3: Rename auth_onefs_wb and pdb_onefs_sam
[ira/wip.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
119         if (!winbind_get_groups(mem_ctx, username, p_num_groups, pp_gids)) {
120                 return NT_STATUS_NO_SUCH_USER;
121         }
122
123         if (*p_num_groups == 0) {
124                 smb_panic("primary group missing");
125         }
126
127         *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
128
129         if (*pp_sids == NULL) {
130                 TALLOC_FREE(*pp_gids);
131                 return NT_STATUS_NO_MEMORY;
132         }
133
134         for (i=0; i < *p_num_groups; i++) {
135                 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
136         }
137
138         return NT_STATUS_OK;
139 }
140
141 static NTSTATUS pdb_wbc_sam_lookup_rids(struct pdb_methods *methods,
142                                         const DOM_SID *domain_sid,
143                                         int num_rids,
144                                         uint32 *rids,
145                                         const char **names,
146                                         enum lsa_SidType *attrs)
147 {
148         NTSTATUS result = NT_STATUS_OK;
149         char *domain = NULL;
150         char **account_names = NULL;
151         char name[256];
152         enum lsa_SidType *attr_list = NULL;
153         int i;
154
155         if (!winbind_lookup_rids(talloc_tos(), domain_sid, num_rids, rids,
156                                  (const char **)&domain,
157                                  (const char ***)&account_names, &attr_list))
158         {
159                 result = NT_STATUS_NONE_MAPPED;
160                 goto done;
161         }
162
163         memcpy(attrs, attr_list, num_rids * sizeof(enum lsa_SidType));
164
165         for (i=0; i<num_rids; i++) {
166                 if (attrs[i] == SID_NAME_UNKNOWN) {
167                         names[i] = NULL;
168                 } else {
169                         snprintf(name, sizeof(name), "%s%c%s", domain,
170                                  *lp_winbind_separator(), account_names[i]);
171                         names[i] = talloc_strdup(names, name);
172                 }
173         }
174
175 done:
176         TALLOC_FREE(account_names);
177         TALLOC_FREE(domain);
178         TALLOC_FREE(attrs);
179         return result;
180 }
181
182 static NTSTATUS pdb_wbc_sam_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
183 {
184         return NT_STATUS_UNSUCCESSFUL;
185 }
186
187 static NTSTATUS pdb_wbc_sam_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
188 {
189         return NT_STATUS_UNSUCCESSFUL;
190 }
191
192 static bool pdb_wbc_sam_search_groups(struct pdb_methods *methods,
193                                       struct pdb_search *search)
194 {
195         return false;
196 }
197
198 static bool pdb_wbc_sam_search_aliases(struct pdb_methods *methods,
199                                        struct pdb_search *search,
200                                        const DOM_SID *sid)
201 {
202
203         return false;
204 }
205
206 static bool pdb_wbc_sam_get_trusteddom_pw(struct pdb_methods *methods,
207                                           const char *domain,
208                                           char **pwd,
209                                           DOM_SID *sid,
210                                           time_t *pass_last_set_time)
211 {
212         return false;
213
214 }
215
216 static bool pdb_wbc_sam_set_trusteddom_pw(struct pdb_methods *methods,
217                                           const char *domain,
218                                           const char *pwd,
219                                           const DOM_SID *sid)
220 {
221         return false;
222 }
223
224 static bool pdb_wbc_sam_del_trusteddom_pw(struct pdb_methods *methods,
225                                           const char *domain)
226 {
227         return false;
228 }
229
230 static NTSTATUS pdb_wbc_sam_enum_trusteddoms(struct pdb_methods *methods,
231                                              TALLOC_CTX *mem_ctx,
232                                              uint32 *num_domains,
233                                              struct trustdom_info ***domains)
234 {
235         return NT_STATUS_NOT_IMPLEMENTED;
236 }
237
238 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)
239 {
240         snprintf(map->nt_name, sizeof(map->nt_name), "%s%c%s",
241                 domain, *lp_winbind_separator(), name);
242         map->sid_name_use = name_type;
243         map->sid = *sid;
244         map->gid = gid;
245         return true;
246 }
247
248 static NTSTATUS pdb_wbc_sam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
249                                  DOM_SID sid)
250 {
251         NTSTATUS result = NT_STATUS_OK;
252         char *name = NULL;
253         char *domain = NULL;
254         enum lsa_SidType name_type;
255         gid_t gid;
256
257         if (!winbind_lookup_sid(talloc_tos(), &sid, (const char **)&domain,
258                                 (const char **) &name, &name_type)) {
259                 result = NT_STATUS_NO_SUCH_GROUP;
260                 goto done;
261         }
262
263         if ((name_type != SID_NAME_DOM_GRP) &&
264             (name_type != SID_NAME_DOMAIN) &&
265             (name_type != SID_NAME_ALIAS) &&
266             (name_type != SID_NAME_WKN_GRP)) {
267                 result = NT_STATUS_NO_SUCH_GROUP;
268                 goto done;
269         }
270
271         if (!winbind_sid_to_gid(&gid, &sid)) {
272                 result = NT_STATUS_NO_SUCH_GROUP;
273                 goto done;
274         }
275
276         if (!_make_group_map(methods, domain, name, name_type, gid, &sid, map)) {
277                 result = NT_STATUS_NO_SUCH_GROUP;
278                 goto done;
279         }
280
281 done:
282         TALLOC_FREE(name);
283         TALLOC_FREE(domain);
284         return result;
285 }
286
287 static NTSTATUS pdb_wbc_sam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
288                                  gid_t gid)
289 {
290         NTSTATUS result = NT_STATUS_OK;
291         char *name = NULL;
292         char *domain = NULL;
293         DOM_SID sid;
294         enum lsa_SidType name_type;
295
296         if (!winbind_gid_to_sid(&sid, gid)) {
297                 result = NT_STATUS_NO_SUCH_GROUP;
298                 goto done;
299         }
300
301         if (!winbind_lookup_sid(talloc_tos(), &sid, (const char **)&domain,
302                                 (const char **)&name, &name_type)) {
303                 result = NT_STATUS_NO_SUCH_GROUP;
304                 goto done;
305         }
306
307         if ((name_type != SID_NAME_DOM_GRP) &&
308             (name_type != SID_NAME_DOMAIN) &&
309             (name_type != SID_NAME_ALIAS) &&
310             (name_type != SID_NAME_WKN_GRP)) {
311                 result = NT_STATUS_NO_SUCH_GROUP;
312                 goto done;
313         }
314
315         if (!_make_group_map(methods, domain, name, name_type, gid, &sid, map)) {
316                 result = NT_STATUS_NO_SUCH_GROUP;
317                 goto done;
318         }
319
320 done:
321         TALLOC_FREE(name);
322         TALLOC_FREE(domain);
323
324         return result;
325 }
326
327 static NTSTATUS pdb_wbc_sam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
328                                  const char *name)
329 {
330         NTSTATUS result = NT_STATUS_OK;
331         char *user_name = NULL;
332         char *domain = NULL;
333         DOM_SID sid;
334         gid_t gid;
335         enum lsa_SidType name_type;
336
337         if (!winbind_lookup_name(domain, user_name, &sid, &name_type)) {
338                 result = NT_STATUS_NO_SUCH_GROUP;
339                 goto done;
340         }
341
342         if ((name_type != SID_NAME_DOM_GRP) &&
343             (name_type != SID_NAME_DOMAIN) &&
344             (name_type != SID_NAME_ALIAS) &&
345             (name_type != SID_NAME_WKN_GRP)) {
346                 result = NT_STATUS_NO_SUCH_GROUP;
347                 goto done;
348         }
349
350         if (!winbind_sid_to_gid(&gid, &sid)) {
351                 result = NT_STATUS_NO_SUCH_GROUP;
352                 goto done;
353         }
354
355         if (!_make_group_map(methods, domain, user_name, name_type, gid, &sid, map)) {
356                 result = NT_STATUS_NO_SUCH_GROUP;
357                 goto done;
358         }
359
360 done:
361
362         return result;
363 }
364
365 static NTSTATUS pdb_wbc_sam_enum_group_mapping(struct pdb_methods *methods,
366                                            const DOM_SID *sid, enum lsa_SidType sid_name_use,
367                                            GROUP_MAP **pp_rmap, size_t *p_num_entries,
368                                            bool unix_only)
369 {
370         return NT_STATUS_NOT_IMPLEMENTED;
371 }
372
373 static NTSTATUS pdb_wbc_sam_get_aliasinfo(struct pdb_methods *methods,
374                                    const DOM_SID *sid,
375                                    struct acct_info *info)
376 {
377         return NT_STATUS_NOT_IMPLEMENTED;
378 }
379
380 static NTSTATUS pdb_wbc_sam_enum_aliasmem(struct pdb_methods *methods,
381                                    const DOM_SID *alias, DOM_SID **pp_members,
382                                    size_t *p_num_members)
383 {
384         return NT_STATUS_NOT_IMPLEMENTED;
385 }
386
387 static NTSTATUS pdb_wbc_sam_alias_memberships(struct pdb_methods *methods,
388                                        TALLOC_CTX *mem_ctx,
389                                        const DOM_SID *domain_sid,
390                                        const DOM_SID *members,
391                                        size_t num_members,
392                                        uint32 **pp_alias_rids,
393                                        size_t *p_num_alias_rids)
394 {
395         if (!winbind_get_sid_aliases(mem_ctx, domain_sid,
396                     members, num_members, pp_alias_rids, p_num_alias_rids))
397                 return NT_STATUS_UNSUCCESSFUL;
398
399         return NT_STATUS_OK;
400 }
401
402 static NTSTATUS pdb_init_wbc_sam(struct pdb_methods **pdb_method, const char *location)
403 {
404         NTSTATUS result;
405
406         if (!NT_STATUS_IS_OK(result = make_pdb_method( pdb_method))) {
407                 return result;
408         }
409
410         (*pdb_method)->name = "wbc_sam";
411
412         (*pdb_method)->getsampwnam = pdb_wbc_sam_getsampwnam;
413         (*pdb_method)->getsampwsid = pdb_wbc_sam_getsampwsid;
414
415         (*pdb_method)->getgrsid = pdb_wbc_sam_getgrsid;
416         (*pdb_method)->getgrgid = pdb_wbc_sam_getgrgid;
417         (*pdb_method)->getgrnam = pdb_wbc_sam_getgrnam;
418         (*pdb_method)->enum_group_mapping = pdb_wbc_sam_enum_group_mapping;
419         (*pdb_method)->enum_group_members = pdb_wbc_sam_enum_group_members;
420         (*pdb_method)->enum_group_memberships = pdb_wbc_sam_enum_group_memberships;
421         (*pdb_method)->get_aliasinfo = pdb_wbc_sam_get_aliasinfo;
422         (*pdb_method)->enum_aliasmem = pdb_wbc_sam_enum_aliasmem;
423         (*pdb_method)->enum_alias_memberships = pdb_wbc_sam_alias_memberships;
424         (*pdb_method)->lookup_rids = pdb_wbc_sam_lookup_rids;
425         (*pdb_method)->get_account_policy = pdb_wbc_sam_get_account_policy;
426         (*pdb_method)->set_account_policy = pdb_wbc_sam_set_account_policy;
427         (*pdb_method)->uid_to_sid = pdb_wbc_sam_uid_to_sid;
428         (*pdb_method)->gid_to_sid = pdb_wbc_sam_gid_to_sid;
429         (*pdb_method)->sid_to_id = pdb_wbc_sam_sid_to_id;
430
431         (*pdb_method)->search_groups = pdb_wbc_sam_search_groups;
432         (*pdb_method)->search_aliases = pdb_wbc_sam_search_aliases;
433
434         (*pdb_method)->get_trusteddom_pw = pdb_wbc_sam_get_trusteddom_pw;
435         (*pdb_method)->set_trusteddom_pw = pdb_wbc_sam_set_trusteddom_pw;
436         (*pdb_method)->del_trusteddom_pw = pdb_wbc_sam_del_trusteddom_pw;
437         (*pdb_method)->enum_trusteddoms  = pdb_wbc_sam_enum_trusteddoms;
438
439         (*pdb_method)->private_data = NULL;
440         (*pdb_method)->free_private_data = NULL;
441
442         return NT_STATUS_OK;
443 }
444
445 NTSTATUS pdb_wbc_sam_init(void)
446 {
447         return smb_register_passdb(PASSDB_INTERFACE_VERSION, "wbc_sam", pdb_init_wbc_sam);
448 }