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