some more code
[Samba.git] / source3 / sam / idmap.c
blob96638b4723fc2e6611921137b1d3f8a0139460d3
1 /*
2 Unix SMB/CIFS implementation.
3 Winbind ID Mapping
4 Copyright (C) Tim Potter 2000
5 Copyright (C) Anthony Liguori <aliguor@us.ibm.com> 2003
6 Copyright (C) Simo Sorce 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "idmap.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
29 static struct {
31 const char *name;
32 /* Function to create a member of the idmap_methods list */
33 NTSTATUS (*reg_meth)(struct idmap_methods **methods);
34 struct idmap_methods *methods;
35 } builtin_idmap_functions[] = {
36 { "tdb", idmap_reg_tdb, NULL },
37 /* { "ldap", idmap_reg_ldap, NULL },*/
38 { NULL, NULL, NULL }
41 /* singleton pattern: uberlazy evaluation */
42 static struct idmap_methods *impl;
44 static struct idmap_methods *get_impl(const char *name)
46 int i = 0;
47 struct idmap_methods *ret = NULL;
49 while (builtin_idmap_functions[i].name && strcmp(builtin_idmap_functions[i].name, name)) {
50 i++;
53 if (builtin_idmap_functions[i].name) {
55 if (!builtin_idmap_functions[i].methods) {
56 builtin_idmap_functions[i].reg_meth(&builtin_idmap_functions[i].methods);
59 ret = builtin_idmap_functions[i].methods;
62 return ret;
65 /* Load idmap backend functions */
66 BOOL set_impl(void)
68 if (!impl) {
69 DEBUG(3, ("idmap_init: using '%s' as backend\n", lp_idmap_backend()));
71 impl = get_impl(lp_idmap_backend());
72 if (!impl) {
73 DEBUG(0, ("set_impl: could not load backend '%s'\n", lp_idmap_backend()));
74 return False;
77 return True;
80 /* Initialize backend */
81 NTSTATUS idmap_init(void)
83 NTSTATUS ret;
85 if (!set_impl()) return NT_STATUS_UNSUCCESSFUL;
87 ret = impl->init();
88 if (NT_STATUS_IS_ERR(ret)) {
89 DEBUG(3, ("idmap_init: init failed!\n"));
92 return ret;
95 /* Get ID from SID */
96 NTSTATUS idmap_get_id_from_sid(id_t *id, int *id_type, DOM_SID *sid)
98 NTSTATUS ret;
100 if (!set_impl()) return NT_STATUS_UNSUCCESSFUL;
102 ret = impl->get_id_from_sid(id, id_type, sid);
103 if (NT_STATUS_IS_ERR(ret)) {
104 DEBUG(3, ("idmap_get_id_from_sid: error fetching id!\n"));
107 return ret;
110 /* Get SID from ID */
111 NTSTATUS idmap_get_sid_from_id(DOM_SID *sid, id_t id, int id_type)
113 NTSTATUS ret;
115 if (!set_impl()) return NT_STATUS_UNSUCCESSFUL;
117 ret = impl->get_sid_from_id(sid, id, id_type);
118 if (NT_STATUS_IS_ERR(ret)) {
119 DEBUG(3, ("idmap_get_sid_from_id: error fetching sid!\n"));
122 return ret;
126 /* Close backend */
127 NTSTATUS idmap_close(void)
129 NTSTATUS ret;
131 if (!set_impl()) return NT_STATUS_UNSUCCESSFUL;
133 ret = impl->close();
134 if (NT_STATUS_IS_ERR(ret)) {
135 DEBUG(3, ("idmap_close: close failed!\n"));
138 return ret;
141 /* Dump backend status */
142 void idmap_status(void)
144 if (!set_impl()) return;
146 impl->status();