s3:idmap_rid: use ranges from idmap_domain struct in idmap_rid_id_to_sid()
[metze/samba/wip.git] / source3 / winbindd / 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, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "includes.h"
22 #include "winbindd.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_IDMAP
26
27 struct idmap_rid_context {
28         const char *domain_name;
29         uint32_t low_id;
30         uint32_t high_id;
31         uint32_t base_rid;
32 };
33
34 /******************************************************************************
35   compat params can't be used because of the completely different way
36   we support multiple domains in the new idmap
37  *****************************************************************************/
38
39 static NTSTATUS idmap_rid_initialize(struct idmap_domain *dom,
40                                      const char *params)
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         ctx = TALLOC_ZERO_P(dom, struct idmap_rid_context);
52         if (ctx == NULL) {
53                 DEBUG(0, ("Out of memory!\n"));
54                 return NT_STATUS_NO_MEMORY;
55         }
56
57         config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
58         if ( ! config_option) {
59                 DEBUG(0, ("Out of memory!\n"));
60                 ret = NT_STATUS_NO_MEMORY;
61                 goto failed;
62         }
63
64         range = lp_parm_const_string(-1, config_option, "range", NULL);
65         if ( !range ||
66             (sscanf(range, "%u - %u", &ctx->low_id, &ctx->high_id) != 2) ||
67             (ctx->low_id > ctx->high_id)) 
68         {
69                 ctx->low_id = 0;
70                 ctx->high_id = 0;
71         }
72
73         /* lets see if the range is defined by the old idmap uid/idmap gid */
74         if (!ctx->low_id && !ctx->high_id) {
75                 if (lp_idmap_uid(&low_uid, &high_uid)) {
76                         ctx->low_id = low_uid;
77                         ctx->high_id = high_uid;
78                 }
79
80                 if (lp_idmap_gid(&low_gid, &high_gid)) {
81                         if ((ctx->low_id != low_gid) ||
82                             (ctx->high_id != high_uid)) {
83                                 DEBUG(1, ("ERROR: idmap uid range must match idmap gid range\n"));
84                                 ret = NT_STATUS_UNSUCCESSFUL;
85                                 goto failed;
86                         }
87                 }
88         }
89
90         if (!ctx->low_id || !ctx->high_id) {
91                 DEBUG(1, ("ERROR: Invalid configuration, ID range missing or invalid\n"));
92                 ret = NT_STATUS_UNSUCCESSFUL;
93                 goto failed;
94         }
95
96         ctx->base_rid = lp_parm_int(-1, config_option, "base_rid", 0);
97         ctx->domain_name = talloc_strdup( ctx, dom->name );
98         
99         dom->private_data = ctx;
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(struct idmap_domain *dom, struct id_map *map)
110 {
111         struct winbindd_domain *domain;
112         struct idmap_rid_context *ctx;
113
114         ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
115
116         /* apply filters before checking */
117         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
118                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
119                                 map->xid.id, dom->low_id, dom->high_id));
120                 return NT_STATUS_NONE_MAPPED;
121         }
122
123         domain = find_domain_from_name_noinit(dom->name);
124         if (domain == NULL ) {
125                 return NT_STATUS_NO_SUCH_DOMAIN;
126         }
127
128         sid_compose(map->sid, &domain->sid, map->xid.id - dom->low_id + ctx->base_rid);
129
130         /* We **really** should have some way of validating 
131            the SID exists and is the correct type here.  But 
132            that is a deficiency in the idmap_rid design. */
133
134         map->status = ID_MAPPED;
135
136         return NT_STATUS_OK;
137 }
138
139 /**********************************
140  Single sid to id lookup function. 
141 **********************************/
142
143 static NTSTATUS idmap_rid_sid_to_id(struct idmap_rid_context *ctx, struct id_map *map)
144 {
145         uint32_t rid;
146
147         sid_peek_rid(map->sid, &rid);
148         map->xid.id = rid - ctx->base_rid + ctx->low_id;
149
150         /* apply filters before returning result */
151
152         if ((map->xid.id < ctx->low_id) || (map->xid.id > ctx->high_id)) {
153                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
154                                 map->xid.id, ctx->low_id, ctx->high_id));
155                 map->status = ID_UNMAPPED;
156                 return NT_STATUS_NONE_MAPPED;
157         }
158
159         /* We **really** should have some way of validating 
160            the SID exists and is the correct type here.  But 
161            that is a deficiency in the idmap_rid design. */
162
163         map->status = ID_MAPPED;
164
165         return NT_STATUS_OK;
166 }
167
168 /**********************************
169  lookup a set of unix ids. 
170 **********************************/
171
172 static NTSTATUS idmap_rid_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
173 {
174         NTSTATUS ret;
175         int i;
176
177         /* initialize the status to avoid suprise */
178         for (i = 0; ids[i]; i++) {
179                 ids[i]->status = ID_UNKNOWN;
180         }
181
182         for (i = 0; ids[i]; i++) {
183
184                 ret = idmap_rid_id_to_sid(dom, ids[i]);
185
186                 if (( ! NT_STATUS_IS_OK(ret)) &&
187                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
188                         /* some fatal error occurred, log it */
189                         DEBUG(3, ("Unexpected error resolving an ID (%d)\n", ids[i]->xid.id));
190                 }
191         }
192
193         return NT_STATUS_OK;
194 }
195
196 /**********************************
197  lookup a set of sids. 
198 **********************************/
199
200 static NTSTATUS idmap_rid_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
201 {
202         struct idmap_rid_context *ridctx;
203         NTSTATUS ret;
204         int i;
205
206         /* initialize the status to avoid suprise */
207         for (i = 0; ids[i]; i++) {
208                 ids[i]->status = ID_UNKNOWN;
209         }
210         
211         ridctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
212
213         for (i = 0; ids[i]; i++) {
214
215                 ret = idmap_rid_sid_to_id(ridctx, ids[i]);
216
217                 if (( ! NT_STATUS_IS_OK(ret)) &&
218                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
219                         /* some fatal error occurred, log it */
220                         DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
221                                   sid_string_dbg(ids[i]->sid)));
222                 }
223         }
224
225         return NT_STATUS_OK;
226 }
227
228 static NTSTATUS idmap_rid_close(struct idmap_domain *dom)
229 {
230         if (dom->private_data) {
231                 TALLOC_FREE(dom->private_data);
232         }
233         return NT_STATUS_OK;
234 }
235
236 static struct idmap_methods rid_methods = {
237         .init = idmap_rid_initialize,
238         .unixids_to_sids = idmap_rid_unixids_to_sids,
239         .sids_to_unixids = idmap_rid_sids_to_unixids,
240         .close_fn = idmap_rid_close
241 };
242
243 NTSTATUS idmap_rid_init(void)
244 {
245         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rid", &rid_methods);
246 }
247