This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[ira/wip.git] / source3 / 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 winbindd_idmap_methods **methods);
28   struct winbindd_idmap_methods *methods;
29 } builtin_winbindd_idmap_functions[] = {
30   { "tdb", winbind_idmap_reg_tdb, NULL },
31   { NULL, NULL, NULL }
32 };
33
34 /* singleton pattern: uberlazy evaluation */
35 static struct winbindd_idmap_methods *impl;
36
37 static struct winbindd_idmap_methods *get_impl(const char *name)
38 {
39   int i = 0;
40   struct winbindd_idmap_methods *ret = NULL;
41
42   while (builtin_winbindd_idmap_functions[i].name && 
43          strcmp(builtin_winbindd_idmap_functions[i].name, name)) {
44     i++;
45   }
46
47   if (builtin_winbindd_idmap_functions[i].name) {
48     if (!builtin_winbindd_idmap_functions[i].methods) {
49       builtin_winbindd_idmap_functions[i].reg_meth(&builtin_winbindd_idmap_functions[i].methods);
50     }
51
52     ret = builtin_winbindd_idmap_functions[i].methods;
53   }
54
55   return ret;
56 }
57
58 /* Initialize backend */
59 BOOL winbindd_idmap_init(void)
60 {
61   BOOL ret = False;
62
63   DEBUG(3, ("winbindd_idmap_init: using '%s' as backend\n", 
64             lp_winbind_backend()));
65
66   if (!impl) {
67     impl = get_impl(lp_winbind_backend());
68     if (!impl) {
69       DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
70                 lp_winbind_backend()));
71     }
72   }
73
74   if (impl) {
75     ret = impl->init();
76   }
77
78   DEBUG(3, ("winbind_idmap_init: returning %s\n", ret ? "true" : "false"));
79
80   return ret;
81 }
82
83 /* Get UID from SID */
84 BOOL winbindd_idmap_get_uid_from_sid(DOM_SID *sid, uid_t *uid)
85 {
86   BOOL ret = False;
87
88   if (!impl) {
89     impl = get_impl(lp_winbind_backend());
90     if (!impl) {
91       DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
92                 lp_winbind_backend()));
93     }
94   }
95
96   if (impl) {
97     ret = impl->get_uid_from_sid(sid, uid);
98   }
99
100   return ret;
101 }
102
103 /* Get GID from SID */
104 BOOL winbindd_idmap_get_gid_from_sid(DOM_SID *sid, gid_t *gid)
105 {
106   BOOL ret = False;
107
108   if (!impl) {
109     impl = get_impl(lp_winbind_backend());
110     if (!impl) {
111       DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
112                 lp_winbind_backend()));
113     }
114   }
115
116   if (impl) {
117     ret = impl->get_gid_from_sid(sid, gid);
118   }
119
120   return ret;
121 }
122
123 /* Get SID from UID */
124 BOOL winbindd_idmap_get_sid_from_uid(uid_t uid, DOM_SID *sid)
125 {
126   BOOL ret = False;
127
128   if (!impl) {
129     impl = get_impl(lp_winbind_backend());
130     if (!impl) {
131       DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
132                 lp_winbind_backend()));
133     }
134   }
135
136   if (impl) {
137     ret = impl->get_sid_from_uid(uid, sid);
138   }
139
140   return ret;
141 }
142
143 /* Get SID from GID */
144 BOOL winbindd_idmap_get_sid_from_gid(gid_t gid, DOM_SID *sid)
145 {
146   BOOL ret = False;
147
148   if (!impl) {
149     impl = get_impl(lp_winbind_backend());
150   }
151
152   if (impl) {
153     ret = impl->get_sid_from_gid(gid, sid);
154   } else {
155     DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
156               lp_winbind_backend()));
157   }
158
159   return ret;
160 }
161
162 /* Close backend */
163 BOOL winbindd_idmap_close(void)
164 {
165   BOOL ret = False;
166
167   if (!impl) {
168     impl = get_impl(lp_winbind_backend());
169   }
170
171   if (impl) {
172     ret = impl->close();
173   } else {
174     DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
175               lp_winbind_backend()));
176   }
177
178   return ret;
179 }
180
181 /* Dump backend status */
182 void winbindd_idmap_status(void)
183 {
184   if (!impl) {
185     impl = get_impl(lp_winbind_backend());
186   }
187
188   if (impl) {
189     impl->status();
190   } else {
191     DEBUG(0, ("winbindd_idmap_init: could not load backend '%s'\n",
192               lp_winbind_backend()));
193   }
194 }