first public release of samba4 code
[samba.git] / source / nsswitch / winbindd_idmap.c
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
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "winbindd.h"
23
24 static struct {
25   const char *name;
26   /* Function to create a member of the idmap_methods list */
27   BOOL (*reg_meth)(struct idmap_methods **methods);
28   struct idmap_methods *methods;
29 } builtin_idmap_functions[] = {
30   { "tdb", winbind_idmap_reg_tdb, NULL },
31   /*  { "ldap", winbind_idmap_reg_ldap, NULL },*/
32   { NULL, NULL, NULL }
33 };
34
35 /* singleton pattern: uberlazy evaluation */
36 static struct idmap_methods *impl;
37
38 static struct idmap_methods *get_impl(const char *name)
39 {
40   int i = 0;
41   struct idmap_methods *ret = NULL;
42
43   while (builtin_idmap_functions[i].name && 
44          strcmp(builtin_idmap_functions[i].name, name)) {
45     i++;
46   }
47
48   if (builtin_idmap_functions[i].name) {
49     if (!builtin_idmap_functions[i].methods) {
50       builtin_idmap_functions[i].reg_meth(&builtin_idmap_functions[i].methods);
51     }
52
53     ret = builtin_idmap_functions[i].methods;
54   }
55
56   return ret;
57 }
58
59 /* Initialize backend */
60 BOOL winbindd_idmap_init(void)
61 {
62   BOOL ret = False;
63
64   DEBUG(3, ("winbindd_idmap_init: using '%s' as backend\n", 
65             lp_idmap_backend()));
66
67   if (!impl) {
68     impl = get_impl(lp_idmap_backend());
69     if (!impl) {
70       DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
71                 lp_idmap_backend()));
72     }
73   }
74
75   if (impl) {
76     ret = impl->init();
77   }
78
79   DEBUG(3, ("winbind_idmap_init: returning %s\n", ret ? "true" : "false"));
80
81   return ret;
82 }
83
84 /* Get UID from SID */
85 BOOL winbindd_idmap_get_uid_from_sid(DOM_SID *sid, uid_t *uid)
86 {
87   BOOL ret = False;
88
89   if (!impl) {
90     impl = get_impl(lp_idmap_backend());
91     if (!impl) {
92       DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
93                 lp_idmap_backend()));
94     }
95   }
96
97   if (impl) {
98     ret = impl->get_uid_from_sid(sid, uid);
99   }
100
101   return ret;
102 }
103
104 /* Get GID from SID */
105 BOOL winbindd_idmap_get_gid_from_sid(DOM_SID *sid, gid_t *gid)
106 {
107   BOOL ret = False;
108
109   if (!impl) {
110     impl = get_impl(lp_idmap_backend());
111     if (!impl) {
112       DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
113                 lp_idmap_backend()));
114     }
115   }
116
117   if (impl) {
118     ret = impl->get_gid_from_sid(sid, gid);
119   }
120
121   return ret;
122 }
123
124 /* Get SID from UID */
125 BOOL winbindd_idmap_get_sid_from_uid(uid_t uid, DOM_SID *sid)
126 {
127   BOOL ret = False;
128
129   if (!impl) {
130     impl = get_impl(lp_idmap_backend());
131     if (!impl) {
132       DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
133                 lp_idmap_backend()));
134     }
135   }
136
137   if (impl) {
138     ret = impl->get_sid_from_uid(uid, sid);
139   }
140
141   return ret;
142 }
143
144 /* Get SID from GID */
145 BOOL winbindd_idmap_get_sid_from_gid(gid_t gid, DOM_SID *sid)
146 {
147   BOOL ret = False;
148
149   if (!impl) {
150     impl = get_impl(lp_idmap_backend());
151   }
152
153   if (impl) {
154     ret = impl->get_sid_from_gid(gid, sid);
155   } else {
156     DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
157               lp_idmap_backend()));
158   }
159
160   return ret;
161 }
162
163 /* Close backend */
164 BOOL winbindd_idmap_close(void)
165 {
166   BOOL ret = False;
167
168   if (!impl) {
169     impl = get_impl(lp_idmap_backend());
170   }
171
172   if (impl) {
173     ret = impl->close();
174   } else {
175     DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
176               lp_idmap_backend()));
177   }
178
179   return ret;
180 }
181
182 /* Dump backend status */
183 void winbindd_idmap_status(void)
184 {
185   if (!impl) {
186     impl = get_impl(lp_idmap_backend());
187   }
188
189   if (impl) {
190     impl->status();
191   } else {
192     DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
193               lp_idmap_backend()));
194   }
195 }
196