NTSTATUS strings are much more use than raw numbers...
[samba.git] / source3 / sam / 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    Copyright (C) Simo Sorce 2003
7
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.
12
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.
17
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.*/
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_IDMAP
26
27 static struct {
28
29         const char *name;
30         /* Function to create a member of the idmap_methods list */
31         NTSTATUS (*reg_meth)(struct idmap_methods **methods);
32         struct idmap_methods *methods;
33
34 } remote_idmap_functions[] = {
35
36         { "winbind", idmap_reg_winbind, NULL },
37         { NULL, NULL, NULL }
38
39 };
40
41 static struct idmap_methods *local_map;
42 static struct idmap_methods *remote_map;
43
44 static struct idmap_methods *get_methods(const char *name)
45 {
46         int i = 0;
47         struct idmap_methods *ret = NULL;
48
49         while (remote_idmap_functions[i].name && strcmp(remote_idmap_functions[i].name, name)) {
50                 i++;
51         }
52
53         if (remote_idmap_functions[i].name) {
54
55                 if (!remote_idmap_functions[i].methods) {
56                         remote_idmap_functions[i].reg_meth(&remote_idmap_functions[i].methods);
57                 }
58
59                 ret = remote_idmap_functions[i].methods;
60         }
61
62         return ret;
63 }
64
65 /* Initialize backend */
66 BOOL idmap_init(const char *remote_backend)
67 {
68         if (!local_map) {
69                 idmap_reg_tdb(&local_map);
70                 local_map->init();
71         }
72         
73         if (!remote_map && remote_backend && *remote_backend != 0) {
74                 DEBUG(3, ("load_methods: using '%s' as remote backend\n", remote_backend));
75                 
76                 remote_map = get_methods(remote_backend);
77                 if (!remote_map) {
78                         DEBUG(0, ("load_methods: could not load remote backend '%s'\n", remote_backend));
79                         return False;
80                 }
81                 remote_map->init();
82         }
83
84         return True;
85 }
86
87 NTSTATUS idmap_set_mapping(const DOM_SID *sid, unid_t id, int id_type)
88 {
89         NTSTATUS ret;
90
91         ret = local_map->set_mapping(sid, id, id_type);
92         if (NT_STATUS_IS_ERR(ret)) {
93                 DEBUG (0, ("idmap_set_mapping: Error, unable to modify local cache!\n"));
94                 DEBUGADD(0, ("Error: %s", nt_errstr(ret)));
95                 return ret;
96         }
97
98         /* Being able to update the remote cache is seldomly right.
99            Generally this is a forbidden operation. */
100         if (!(id_type & ID_CACHE) && (remote_map != NULL)) {
101                 remote_map->set_mapping(sid, id, id_type);
102                 if (NT_STATUS_IS_ERR(ret)) {
103                         DEBUG (0, ("idmap_set_mapping: Error, unable to modify remote cache!\n"));
104                         DEBUGADD(0, ("Error: %s", nt_errstr(ret)));
105                 }
106         }
107
108         return ret;
109 }
110
111 /* Get ID from SID */
112 NTSTATUS idmap_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
113 {
114         NTSTATUS ret;
115         int loc_type;
116
117         loc_type = *id_type;
118         if (remote_map) { /* We have a central remote idmap */
119                 loc_type |= ID_NOMAP;
120         }
121         ret = local_map->get_id_from_sid(id, &loc_type, sid);
122         if (NT_STATUS_IS_ERR(ret)) {
123                 if (remote_map) {
124                         ret = remote_map->get_id_from_sid(id, id_type, sid);
125                         if (NT_STATUS_IS_ERR(ret)) {
126                                 DEBUG(3, ("idmap_get_id_from_sid: error fetching id!\n"));
127                                 return ret;
128                         } else {
129                                 loc_type |= ID_CACHE;
130                                 idmap_set_mapping(sid, *id, loc_type);
131                         }
132                 }
133         } else {
134                 *id_type = loc_type & ID_TYPEMASK;
135         }
136
137         return ret;
138 }
139
140 /* Get SID from ID */
141 NTSTATUS idmap_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type)
142 {
143         NTSTATUS ret;
144         int loc_type;
145
146         loc_type = id_type;
147         if (remote_map) {
148                 loc_type = id_type | ID_NOMAP;
149         }
150         ret = local_map->get_sid_from_id(sid, id, loc_type);
151         if (NT_STATUS_IS_ERR(ret)) {
152                 if (remote_map) {
153                         ret = remote_map->get_sid_from_id(sid, id, id_type);
154                         if (NT_STATUS_IS_ERR(ret)) {
155                                 DEBUG(3, ("idmap_get_sid_from_id: unable to fetch sid!\n"));
156                                 return ret;
157                         } else {
158                                 loc_type |= ID_CACHE;
159                                 idmap_set_mapping(sid, id, loc_type);
160                         }
161                 }
162         }
163
164         return ret;
165 }
166
167 /* Close backend */
168 NTSTATUS idmap_close(void)
169 {
170         NTSTATUS ret;
171
172         ret = local_map->close();
173         if (NT_STATUS_IS_ERR(ret)) {
174                 DEBUG(3, ("idmap_close: failed to close local cache!\n"));
175         }
176
177         if (remote_map) {
178                 ret = remote_map->close();
179                 if (NT_STATUS_IS_ERR(ret)) {
180                         DEBUG(3, ("idmap_close: failed to close remote idmap repository!\n"));
181                 }
182         }
183
184         return ret;
185 }
186
187 /* Dump backend status */
188 void idmap_status(void)
189 {
190         local_map->status();
191         if (remote_map) remote_map->status();
192 }
193