5327f05c7506558931cb3e0952e0cccb922f40b3
[samba.git] / source3 / nsswitch / idmap_rid.c
1 /*
2  *  idmap_rid: static map between Active Directory/NT RIDs and RFC 2307 accounts
3  *  Copyright (C) Guenther Deschner, 2004
4  *  Copyright (C) Sumit Bose, 2004
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include "includes.h"
23 #include "winbindd.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_IDMAP
27
28 struct idmap_rid_context {
29         const char *domain_name;
30         uint32_t low_id;
31         uint32_t high_id;
32         uint32_t base_rid;
33 };
34
35 /******************************************************************************
36   compat params can't be used because of the completely different way
37   we support multiple domains in the new idmap
38  *****************************************************************************/
39
40 static NTSTATUS idmap_rid_initialize(struct idmap_domain *dom)
41 {
42         NTSTATUS ret;
43         struct idmap_rid_context *ctx;
44         char *config_option = NULL;
45         const char *range;
46         uid_t low_uid = 0;
47         uid_t high_uid = 0;
48         gid_t low_gid = 0;
49         gid_t high_gid = 0;
50
51         if ( (ctx = TALLOC_ZERO_P(dom, struct idmap_rid_context)) == NULL ) {
52                 DEBUG(0, ("Out of memory!\n"));
53                 return NT_STATUS_NO_MEMORY;
54         }
55
56         config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
57         if ( ! config_option) {
58                 DEBUG(0, ("Out of memory!\n"));
59                 ret = NT_STATUS_NO_MEMORY;
60                 goto failed;
61         }
62
63         range = lp_parm_const_string(-1, config_option, "range", NULL);
64         if ( !range ||
65             (sscanf(range, "%u - %u", &ctx->low_id, &ctx->high_id) != 2) ||
66             (ctx->low_id > ctx->high_id)) 
67         {
68                 ctx->low_id = 0;
69                 ctx->high_id = 0;
70         }
71
72         /* lets see if the range is defined by the old idmap uid/idmap gid */
73         if (!ctx->low_id && !ctx->high_id) {
74                 if (lp_idmap_uid(&low_uid, &high_uid)) {
75                         ctx->low_id = low_uid;
76                         ctx->high_id = high_uid;
77                 }
78
79                 if (lp_idmap_gid(&low_gid, &high_gid)) {
80                         if ((ctx->low_id != low_gid) ||
81                             (ctx->high_id != high_uid)) {
82                                 DEBUG(1, ("ERROR: idmap uid range must match idmap gid range\n"));
83                                 ret = NT_STATUS_UNSUCCESSFUL;
84                                 goto failed;
85                         }
86                 }
87         }
88
89         if (!ctx->low_id || !ctx->high_id) {
90                 DEBUG(1, ("ERROR: Invalid configuration, ID range missing or invalid\n"));
91                 ret = NT_STATUS_UNSUCCESSFUL;
92                 goto failed;
93         }
94
95         ctx->base_rid = lp_parm_int(-1, config_option, "base_rid", 0);
96         ctx->domain_name = talloc_strdup( ctx, dom->name );
97         
98         dom->private_data = ctx;
99         dom->initialized = True;
100
101         talloc_free(config_option);
102         return NT_STATUS_OK;
103
104 failed:
105         talloc_free(ctx);
106         return ret;
107 }
108
109 static NTSTATUS idmap_rid_id_to_sid(TALLOC_CTX *memctx, struct idmap_rid_context *ctx, struct id_map *map)
110 {
111         struct winbindd_domain *domain; 
112
113         /* apply filters before checking */
114         if ((map->xid.id < ctx->low_id) || (map->xid.id > ctx->high_id)) {
115                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
116                                 map->xid.id, ctx->low_id, ctx->high_id));
117                 return NT_STATUS_NONE_MAPPED;
118         }
119
120         if ( (domain = find_domain_from_name_noinit(ctx->domain_name)) == NULL ) {
121                 return NT_STATUS_NO_SUCH_DOMAIN;
122         }
123         
124         sid_compose(map->sid, &domain->sid, map->xid.id - ctx->low_id + ctx->base_rid);
125
126         /* We **really** should have some way of validating 
127            the SID exists and is the correct type here.  But 
128            that is a deficiency in the idmap_rid design. */
129
130         map->status = ID_MAPPED;
131
132         return NT_STATUS_OK;
133 }
134
135 /**********************************
136  Single sid to id lookup function. 
137 **********************************/
138
139 static NTSTATUS idmap_rid_sid_to_id(TALLOC_CTX *memctx, struct idmap_rid_context *ctx, struct id_map *map)
140 {
141         uint32_t rid;
142
143         sid_peek_rid(map->sid, &rid);
144         map->xid.id = rid - ctx->base_rid + ctx->low_id;
145
146         /* apply filters before returning result */
147
148         if ((map->xid.id < ctx->low_id) || (map->xid.id > ctx->high_id)) {
149                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
150                                 map->xid.id, ctx->low_id, ctx->high_id));
151                 map->status = ID_UNMAPPED;
152                 return NT_STATUS_NONE_MAPPED;
153         }
154
155         /* We **really** should have some way of validating 
156            the SID exists and is the correct type here.  But 
157            that is a deficiency in the idmap_rid design. */
158
159         map->status = ID_MAPPED;
160
161         return NT_STATUS_OK;
162 }
163
164 /**********************************
165  lookup a set of unix ids. 
166 **********************************/
167
168 static NTSTATUS idmap_rid_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
169 {
170         struct idmap_rid_context *ridctx;
171         TALLOC_CTX *ctx;
172         NTSTATUS ret;
173         int i;
174
175         /* Initilization my have been deferred because of an error, retry or fail */
176         if ( ! dom->initialized) {
177                 ret = idmap_rid_initialize(dom);
178                 if ( ! NT_STATUS_IS_OK(ret)) {
179                         return ret;
180                 }
181         }
182
183         ridctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
184
185         ctx = talloc_new(dom);
186         if ( ! ctx) {
187                 DEBUG(0, ("Out of memory!\n"));
188                 return NT_STATUS_NO_MEMORY;
189         }
190
191         for (i = 0; ids[i]; i++) {
192
193                 ret = idmap_rid_id_to_sid(ctx, ridctx, ids[i]);
194
195                 if (( ! NT_STATUS_IS_OK(ret)) &&
196                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
197                         /* some fatal error occurred, log it */
198                         DEBUG(3, ("Unexpected error resolving an ID (%d)\n", ids[i]->xid.id));
199                 }
200         }
201
202         talloc_free(ctx);
203         return NT_STATUS_OK;
204 }
205
206 /**********************************
207  lookup a set of sids. 
208 **********************************/
209
210 static NTSTATUS idmap_rid_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
211 {
212         struct idmap_rid_context *ridctx;
213         TALLOC_CTX *ctx;
214         NTSTATUS ret;
215         int i;
216
217         /* Initilization my have been deferred because of an error, retry or fail */
218         if ( ! dom->initialized) {
219                 ret = idmap_rid_initialize(dom);
220                 if ( ! NT_STATUS_IS_OK(ret)) {
221                         return ret;
222                 }
223         }
224
225         ridctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
226
227         ctx = talloc_new(dom);
228         if ( ! ctx) {
229                 DEBUG(0, ("Out of memory!\n"));
230                 return NT_STATUS_NO_MEMORY;
231         }
232
233         for (i = 0; ids[i]; i++) {
234
235                 ret = idmap_rid_sid_to_id(ctx, ridctx, ids[i]);
236
237                 if (( ! NT_STATUS_IS_OK(ret)) &&
238                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
239                         /* some fatal error occurred, log it */
240                         DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
241                                         sid_string_static(ids[i]->sid)));
242                 }
243         }
244
245         talloc_free(ctx);
246         return NT_STATUS_OK;
247 }
248
249 static NTSTATUS idmap_rid_close(struct idmap_domain *dom)
250 {
251         if (dom->private_data) {
252                 TALLOC_FREE(dom->private_data);
253         }
254         return NT_STATUS_OK;
255 }
256
257 static struct idmap_methods rid_methods = {
258         .init = idmap_rid_initialize,
259         .unixids_to_sids = idmap_rid_unixids_to_sids,
260         .sids_to_unixids = idmap_rid_sids_to_unixids,
261         .close_fn = idmap_rid_close
262 };
263
264 NTSTATUS idmap_rid_init(void)
265 {
266         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rid", &rid_methods);
267 }
268