s3-passdb: split out passdb/pdb_wbc_sam.h.
[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 #include "passdb.h"
41 #include "lib/winbind_util.h"
42 #include "passdb/pdb_wbc_sam.h"
43
44 /***************************************************************************
45   Default implementations of some functions.
46  ****************************************************************************/
47 static NTSTATUS _pdb_wbc_sam_getsampw(struct pdb_methods *methods,
48                                        struct samu *user,
49                                        const struct passwd *pwd)
50 {
51         NTSTATUS result = NT_STATUS_OK;
52
53         if (pwd == NULL)
54                 return NT_STATUS_NO_SUCH_USER;
55
56         ZERO_STRUCTP(user);
57
58         /* Can we really get away with this little of information */
59         user->methods = methods;
60         result = samu_set_unix(user, pwd);
61
62         return result;
63 }
64
65 static NTSTATUS pdb_wbc_sam_getsampwnam(struct pdb_methods *methods, struct samu *user, const char *sname)
66 {
67         return _pdb_wbc_sam_getsampw(methods, user, winbind_getpwnam(sname));
68 }
69
70 static NTSTATUS pdb_wbc_sam_getsampwsid(struct pdb_methods *methods, struct samu *user, const struct dom_sid *sid)
71 {
72         return _pdb_wbc_sam_getsampw(methods, user, winbind_getpwsid(sid));
73 }
74
75 static bool pdb_wbc_sam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
76                                    struct dom_sid *sid)
77 {
78         return winbind_uid_to_sid(sid, uid);
79 }
80
81 static bool pdb_wbc_sam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
82                                    struct dom_sid *sid)
83 {
84         return winbind_gid_to_sid(sid, gid);
85 }
86
87 static NTSTATUS pdb_wbc_sam_enum_group_members(struct pdb_methods *methods,
88                                                TALLOC_CTX *mem_ctx,
89                                                const struct dom_sid *group,
90                                                uint32 **pp_member_rids,
91                                                size_t *p_num_members)
92 {
93         return NT_STATUS_NOT_IMPLEMENTED;
94 }
95
96 static NTSTATUS pdb_wbc_sam_enum_group_memberships(struct pdb_methods *methods,
97                                                    TALLOC_CTX *mem_ctx,
98                                                    struct samu *user,
99                                                    struct dom_sid **pp_sids,
100                                                    gid_t **pp_gids,
101                                                    uint32_t *p_num_groups)
102 {
103         size_t i;
104         const char *username = pdb_get_username(user);
105         uint32_t num_groups;
106
107         if (!winbind_get_groups(mem_ctx, username, &num_groups, pp_gids)) {
108                 return NT_STATUS_NO_SUCH_USER;
109         }
110         *p_num_groups = num_groups;
111
112         if (*p_num_groups == 0) {
113                 smb_panic("primary group missing");
114         }
115
116         *pp_sids = talloc_array(mem_ctx, struct dom_sid, *p_num_groups);
117
118         if (*pp_sids == NULL) {
119                 TALLOC_FREE(*pp_gids);
120                 return NT_STATUS_NO_MEMORY;
121         }
122
123         for (i=0; i < *p_num_groups; i++) {
124                 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
125         }
126
127         return NT_STATUS_OK;
128 }
129
130 static NTSTATUS pdb_wbc_sam_lookup_rids(struct pdb_methods *methods,
131                                         const struct dom_sid *domain_sid,
132                                         int num_rids,
133                                         uint32 *rids,
134                                         const char **names,
135                                         enum lsa_SidType *attrs)
136 {
137         NTSTATUS result = NT_STATUS_OK;
138         char *domain = NULL;
139         char **account_names = NULL;
140         enum lsa_SidType *attr_list = NULL;
141         int i;
142
143         if (!winbind_lookup_rids(talloc_tos(), domain_sid, num_rids, rids,
144                                  (const char **)&domain,
145                                  (const char ***)&account_names, &attr_list))
146         {
147                 result = NT_STATUS_NONE_MAPPED;
148                 goto done;
149         }
150
151         memcpy(attrs, attr_list, num_rids * sizeof(enum lsa_SidType));
152
153         for (i=0; i<num_rids; i++) {
154                 if (attrs[i] == SID_NAME_UNKNOWN) {
155                         names[i] = NULL;
156                 } else {
157                         names[i] = talloc_strdup(names, account_names[i]);
158                         if (names[i] == NULL) {
159                                 result = NT_STATUS_NO_MEMORY;
160                                 goto done;
161                         }
162
163                 }
164         }
165
166 done:
167         TALLOC_FREE(account_names);
168         TALLOC_FREE(domain);
169         TALLOC_FREE(attr_list);
170         return result;
171 }
172
173 static NTSTATUS pdb_wbc_sam_get_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t *value)
174 {
175         return NT_STATUS_UNSUCCESSFUL;
176 }
177
178 static NTSTATUS pdb_wbc_sam_set_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t value)
179 {
180         return NT_STATUS_UNSUCCESSFUL;
181 }
182
183 static bool pdb_wbc_sam_search_groups(struct pdb_methods *methods,
184                                       struct pdb_search *search)
185 {
186         return false;
187 }
188
189 static bool pdb_wbc_sam_search_aliases(struct pdb_methods *methods,
190                                        struct pdb_search *search,
191                                        const struct dom_sid *sid)
192 {
193
194         return false;
195 }
196
197 static bool pdb_wbc_sam_get_trusteddom_pw(struct pdb_methods *methods,
198                                           const char *domain,
199                                           char **pwd,
200                                           struct dom_sid *sid,
201                                           time_t *pass_last_set_time)
202 {
203         return false;
204
205 }
206
207 static bool pdb_wbc_sam_set_trusteddom_pw(struct pdb_methods *methods,
208                                           const char *domain,
209                                           const char *pwd,
210                                           const struct dom_sid *sid)
211 {
212         return false;
213 }
214
215 static bool pdb_wbc_sam_del_trusteddom_pw(struct pdb_methods *methods,
216                                           const char *domain)
217 {
218         return false;
219 }
220
221 static NTSTATUS pdb_wbc_sam_enum_trusteddoms(struct pdb_methods *methods,
222                                              TALLOC_CTX *mem_ctx,
223                                              uint32 *num_domains,
224                                              struct trustdom_info ***domains)
225 {
226         return NT_STATUS_NOT_IMPLEMENTED;
227 }
228
229 static bool _make_group_map(struct pdb_methods *methods, const char *domain, const char *name, enum lsa_SidType name_type, gid_t gid, struct dom_sid *sid, GROUP_MAP *map)
230 {
231         snprintf(map->nt_name, sizeof(map->nt_name), "%s%c%s",
232                 domain, *lp_winbind_separator(), name);
233         map->sid_name_use = name_type;
234         map->sid = *sid;
235         map->gid = gid;
236         return true;
237 }
238
239 static NTSTATUS pdb_wbc_sam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
240                                  struct dom_sid sid)
241 {
242         NTSTATUS result = NT_STATUS_OK;
243         char *name = NULL;
244         char *domain = NULL;
245         enum lsa_SidType name_type;
246         gid_t gid;
247
248         if (!winbind_lookup_sid(talloc_tos(), &sid, (const char **)&domain,
249                                 (const char **) &name, &name_type)) {
250                 result = NT_STATUS_NO_SUCH_GROUP;
251                 goto done;
252         }
253
254         if ((name_type != SID_NAME_DOM_GRP) &&
255             (name_type != SID_NAME_DOMAIN) &&
256             (name_type != SID_NAME_ALIAS) &&
257             (name_type != SID_NAME_WKN_GRP)) {
258                 result = NT_STATUS_NO_SUCH_GROUP;
259                 goto done;
260         }
261
262         if (!winbind_sid_to_gid(&gid, &sid)) {
263                 result = NT_STATUS_NO_SUCH_GROUP;
264                 goto done;
265         }
266
267         if (!_make_group_map(methods, domain, name, name_type, gid, &sid, map)) {
268                 result = NT_STATUS_NO_SUCH_GROUP;
269                 goto done;
270         }
271
272 done:
273         TALLOC_FREE(name);
274         TALLOC_FREE(domain);
275         return result;
276 }
277
278 static NTSTATUS pdb_wbc_sam_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
279                                  gid_t gid)
280 {
281         NTSTATUS result = NT_STATUS_OK;
282         char *name = NULL;
283         char *domain = NULL;
284         struct dom_sid sid;
285         enum lsa_SidType name_type;
286
287         if (!winbind_gid_to_sid(&sid, gid)) {
288                 result = NT_STATUS_NO_SUCH_GROUP;
289                 goto done;
290         }
291
292         if (!winbind_lookup_sid(talloc_tos(), &sid, (const char **)&domain,
293                                 (const char **)&name, &name_type)) {
294                 result = NT_STATUS_NO_SUCH_GROUP;
295                 goto done;
296         }
297
298         if ((name_type != SID_NAME_DOM_GRP) &&
299             (name_type != SID_NAME_DOMAIN) &&
300             (name_type != SID_NAME_ALIAS) &&
301             (name_type != SID_NAME_WKN_GRP)) {
302                 result = NT_STATUS_NO_SUCH_GROUP;
303                 goto done;
304         }
305
306         if (!_make_group_map(methods, domain, name, name_type, gid, &sid, map)) {
307                 result = NT_STATUS_NO_SUCH_GROUP;
308                 goto done;
309         }
310
311 done:
312         TALLOC_FREE(name);
313         TALLOC_FREE(domain);
314
315         return result;
316 }
317
318 static NTSTATUS pdb_wbc_sam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
319                                  const char *name)
320 {
321         NTSTATUS result = NT_STATUS_OK;
322         const char *domain = "";
323         struct dom_sid sid;
324         gid_t gid;
325         enum lsa_SidType name_type;
326
327         if (!winbind_lookup_name(domain, name, &sid, &name_type)) {
328                 result = NT_STATUS_NO_SUCH_GROUP;
329                 goto done;
330         }
331
332         if ((name_type != SID_NAME_DOM_GRP) &&
333             (name_type != SID_NAME_DOMAIN) &&
334             (name_type != SID_NAME_ALIAS) &&
335             (name_type != SID_NAME_WKN_GRP)) {
336                 result = NT_STATUS_NO_SUCH_GROUP;
337                 goto done;
338         }
339
340         if (!winbind_sid_to_gid(&gid, &sid)) {
341                 result = NT_STATUS_NO_SUCH_GROUP;
342                 goto done;
343         }
344
345         if (!_make_group_map(methods, domain, name, name_type, gid, &sid, map)) {
346                 result = NT_STATUS_NO_SUCH_GROUP;
347                 goto done;
348         }
349
350 done:
351
352         return result;
353 }
354
355 static NTSTATUS pdb_wbc_sam_enum_group_mapping(struct pdb_methods *methods,
356                                            const struct dom_sid *sid, enum lsa_SidType sid_name_use,
357                                            GROUP_MAP **pp_rmap, size_t *p_num_entries,
358                                            bool unix_only)
359 {
360         return NT_STATUS_NOT_IMPLEMENTED;
361 }
362
363 static NTSTATUS pdb_wbc_sam_get_aliasinfo(struct pdb_methods *methods,
364                                    const struct dom_sid *sid,
365                                    struct acct_info *info)
366 {
367         return NT_STATUS_NOT_IMPLEMENTED;
368 }
369
370 static NTSTATUS pdb_wbc_sam_enum_aliasmem(struct pdb_methods *methods,
371                                           const struct dom_sid *alias,
372                                           TALLOC_CTX *mem_ctx,
373                                           struct dom_sid **pp_members,
374                                           size_t *p_num_members)
375 {
376         return NT_STATUS_NOT_IMPLEMENTED;
377 }
378
379 static NTSTATUS pdb_wbc_sam_alias_memberships(struct pdb_methods *methods,
380                                        TALLOC_CTX *mem_ctx,
381                                        const struct dom_sid *domain_sid,
382                                        const struct dom_sid *members,
383                                        size_t num_members,
384                                        uint32 **pp_alias_rids,
385                                        size_t *p_num_alias_rids)
386 {
387         if (!winbind_get_sid_aliases(mem_ctx, domain_sid,
388                     members, num_members, pp_alias_rids, p_num_alias_rids))
389                 return NT_STATUS_UNSUCCESSFUL;
390
391         return NT_STATUS_OK;
392 }
393
394 static NTSTATUS pdb_init_wbc_sam(struct pdb_methods **pdb_method, const char *location)
395 {
396         NTSTATUS result;
397
398         if (!NT_STATUS_IS_OK(result = make_pdb_method( pdb_method))) {
399                 return result;
400         }
401
402         (*pdb_method)->name = "wbc_sam";
403
404         (*pdb_method)->getsampwnam = pdb_wbc_sam_getsampwnam;
405         (*pdb_method)->getsampwsid = pdb_wbc_sam_getsampwsid;
406
407         (*pdb_method)->getgrsid = pdb_wbc_sam_getgrsid;
408         (*pdb_method)->getgrgid = pdb_wbc_sam_getgrgid;
409         (*pdb_method)->getgrnam = pdb_wbc_sam_getgrnam;
410         (*pdb_method)->enum_group_mapping = pdb_wbc_sam_enum_group_mapping;
411         (*pdb_method)->enum_group_members = pdb_wbc_sam_enum_group_members;
412         (*pdb_method)->enum_group_memberships = pdb_wbc_sam_enum_group_memberships;
413         (*pdb_method)->get_aliasinfo = pdb_wbc_sam_get_aliasinfo;
414         (*pdb_method)->enum_aliasmem = pdb_wbc_sam_enum_aliasmem;
415         (*pdb_method)->enum_alias_memberships = pdb_wbc_sam_alias_memberships;
416         (*pdb_method)->lookup_rids = pdb_wbc_sam_lookup_rids;
417         (*pdb_method)->get_account_policy = pdb_wbc_sam_get_account_policy;
418         (*pdb_method)->set_account_policy = pdb_wbc_sam_set_account_policy;
419         (*pdb_method)->uid_to_sid = pdb_wbc_sam_uid_to_sid;
420         (*pdb_method)->gid_to_sid = pdb_wbc_sam_gid_to_sid;
421
422         (*pdb_method)->search_groups = pdb_wbc_sam_search_groups;
423         (*pdb_method)->search_aliases = pdb_wbc_sam_search_aliases;
424
425         (*pdb_method)->get_trusteddom_pw = pdb_wbc_sam_get_trusteddom_pw;
426         (*pdb_method)->set_trusteddom_pw = pdb_wbc_sam_set_trusteddom_pw;
427         (*pdb_method)->del_trusteddom_pw = pdb_wbc_sam_del_trusteddom_pw;
428         (*pdb_method)->enum_trusteddoms  = pdb_wbc_sam_enum_trusteddoms;
429
430         (*pdb_method)->private_data = NULL;
431         (*pdb_method)->free_private_data = NULL;
432
433         return NT_STATUS_OK;
434 }
435
436 NTSTATUS pdb_wbc_sam_init(void)
437 {
438         return smb_register_passdb(PASSDB_INTERFACE_VERSION, "wbc_sam", pdb_init_wbc_sam);
439 }