r20116: Start merging in the work done to create the new idmap subsystem.
[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 2 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
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_IDMAP
26
27 struct idmap_rid_context {
28         DOM_SID dom_sid;
29         uint32_t low_id;
30         uint32_t high_id;
31         uint32_t base_rid;
32 };
33
34 /* compat params can't be used because of the completely different way we support multiple domains in the new idmap */
35 static NTSTATUS idmap_rid_initialize(struct idmap_domain *dom, const char *compat_params)
36 {
37         NTSTATUS ret;
38         struct idmap_rid_context *ctx;
39         char *config_option = NULL;
40         const char *range;
41
42         ctx = talloc_zero(dom, struct idmap_rid_context);
43         if ( ! ctx) {
44                 DEBUG(0, ("Out of memory!\n"));
45                 return NT_STATUS_NO_MEMORY;
46         }
47
48         config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
49         if ( ! config_option) {
50                 DEBUG(0, ("Out of memory!\n"));
51                 ret = NT_STATUS_NO_MEMORY;
52                 goto failed;
53         }
54
55         range = lp_parm_const_string(-1, config_option, "range", NULL);
56         if (( ! range) ||
57             (sscanf(range, "%u - %u", &ctx->low_id, &ctx->high_id) != 2) ||
58             (ctx->low_id > ctx->high_id)) {
59                 ctx->low_id = 0;
60                 ctx->high_id = 0;
61         }
62
63         if (( ! ctx->low_id) || ( ! ctx->high_id)) {
64                 DEBUG(1, ("ERROR: Invalid configuration, ID range missing\n"));
65                 ret = NT_STATUS_UNSUCCESSFUL;
66                 goto failed;
67         }
68
69         ctx->base_rid = lp_parm_int(-1, config_option, "base_rid", 0);
70
71         sid_copy(&ctx->dom_sid, dom->sid);
72
73         dom->private_data = ctx;
74
75         talloc_free(config_option);
76         return NT_STATUS_OK;
77
78 failed:
79         talloc_free(ctx);
80         return ret;
81 }
82
83 static NTSTATUS idmap_rid_id_to_sid(struct idmap_rid_context *ctx, struct id_map *map)
84 {
85         char *domname, *name;
86         enum lsa_SidType sid_type;
87
88         if (!ctx || !map) {
89                 return NT_STATUS_INVALID_PARAMETER;
90         }
91
92         /* apply filters before checking */
93         if ((map->xid.id < ctx->low_id) || (map->xid.id > ctx->high_id)) {
94                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
95                                 map->xid.id, ctx->low_id, ctx->high_id));
96                 return NT_STATUS_NONE_MAPPED;
97         }
98
99         sid_compose(map->sid, &ctx->dom_sid, map->xid.id - ctx->low_id + ctx->base_rid);
100
101         if (winbindd_lookup_name_by_sid(ctx, map->sid, &domname, &name, &sid_type)) {
102                 switch (sid_type) {
103                 case SID_NAME_USER:
104                         if (map->xid.type != ID_TYPE_UID) {
105                                 /* wrong type */
106                                 DEBUG(5, ("Resulting SID is of wrong ID type\n"));
107                                 return NT_STATUS_NONE_MAPPED;
108                         }
109                         break;
110                 case SID_NAME_DOM_GRP:
111                 case SID_NAME_ALIAS:
112                 case SID_NAME_WKN_GRP:
113                         if (map->xid.type != ID_TYPE_GID) {
114                                 /* wrong type */
115                                 DEBUG(5, ("Resulting SID is of wrong ID type\n"));
116                                 return NT_STATUS_NONE_MAPPED;
117                         }
118                         break;
119                 default:
120                         /* invalid sid, let's just leave it unmapped */
121                         DEBUG(10, ("SID %s is UNKNOWN, skip mapping\n", sid_string_static(map->sid)));
122                         return NT_STATUS_NONE_MAPPED;
123                 }
124         } else {
125                 DEBUG(2, ("Failed: to resolve SID\n"));
126                 return NT_STATUS_UNSUCCESSFUL;
127         }
128
129         map->mapped = True;
130
131         return NT_STATUS_OK;
132 }
133
134 /**********************************
135  Single sid to id lookup function. 
136 **********************************/
137
138 static NTSTATUS idmap_rid_sid_to_id(struct idmap_rid_context *ctx, struct id_map *map)
139 {
140         char *domname, *name;
141         enum lsa_SidType sid_type;
142         uint32_t rid;
143
144         if (!ctx || !map) {
145                 return NT_STATUS_INVALID_PARAMETER;
146         }
147
148         sid_peek_rid(map->sid, &rid);
149         map->xid.id = rid - ctx->base_rid + ctx->low_id;
150
151         /* check if this is a valid SID and set the type */
152         if (winbindd_lookup_name_by_sid(ctx, map->sid, &domname, &name, &sid_type)) {
153                 switch (sid_type) {
154                 case SID_NAME_USER:
155                         map->xid.type = ID_TYPE_UID;
156                         break;
157                 case SID_NAME_DOM_GRP:
158                 case SID_NAME_ALIAS:
159                 case SID_NAME_WKN_GRP:
160                         map->xid.type = ID_TYPE_GID;
161                         break;
162                 default:
163                         /* invalid sid, let's just leave it unmapped */
164                         DEBUG(10, ("SID %s is UNKNOWN, skip mapping\n", sid_string_static(map->sid)));
165                         return NT_STATUS_NONE_MAPPED;
166                 }
167         } else {
168                 DEBUG(2, ("Failed: to resolve SID\n"));
169                 return NT_STATUS_UNSUCCESSFUL;
170         }
171
172         /* apply filters before returning result */
173         if ((map->xid.id < ctx->low_id) || (map->xid.id > ctx->high_id)) {
174                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
175                                 map->xid.id, ctx->low_id, ctx->high_id));
176                 return NT_STATUS_NONE_MAPPED;
177         }
178
179         map->mapped = True;
180
181         return NT_STATUS_OK;
182 }
183
184 /**********************************
185  lookup a set of unix ids. 
186 **********************************/
187
188 static NTSTATUS idmap_rid_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
189 {
190         struct idmap_rid_context *ctx;
191         NTSTATUS ret;
192         int i;
193
194         ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
195
196         for (i = 0; ids[i]; i++) {
197                 /* make sure it is marked as unmapped before resolveing */
198                 ids[i]->mapped = False;
199
200                 ret = idmap_rid_id_to_sid(ctx, ids[i]);
201
202                 if (( ! NT_STATUS_IS_OK(ret)) &&
203                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
204                         /* some fatal error occurred, log it */
205                         DEBUG(3, ("Unexpected error resolving an ID (%d)\n", ids[i]->xid.id));
206                 }
207         }
208
209         return NT_STATUS_OK;
210 }
211
212 /**********************************
213  lookup a set of sids. 
214 **********************************/
215
216 static NTSTATUS idmap_rid_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
217 {
218         struct idmap_rid_context *ctx;
219         NTSTATUS ret;
220         int i;
221
222         ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
223
224         for (i = 0; ids[i]; i++) {
225                 /* make sure it is marked as unmapped before resolveing */
226                 ids[i]->mapped = False;
227
228                 ret = idmap_rid_sid_to_id(ctx, ids[i]);
229
230                 if (( ! NT_STATUS_IS_OK(ret)) &&
231                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
232                         /* some fatal error occurred, log it */
233                         DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
234                                         sid_string_static(ids[i]->sid)));
235                 }
236         }
237
238         return NT_STATUS_OK;
239 }
240
241 static NTSTATUS idmap_rid_close(struct idmap_domain *dom)
242 {
243         struct idmap_tdb_context *ctx;
244
245         if (dom->private_data) {
246                 TALLOC_FREE(dom->private_data);
247         }
248         return NT_STATUS_OK;
249 }
250
251 static struct idmap_methods rid_methods = {
252         .init = idmap_rid_initialize,
253         .unixids_to_sids = idmap_rid_unixids_to_sids,
254         .sids_to_unixids = idmap_rid_sids_to_unixids,
255         .close_fn = idmap_rid_close
256 };
257
258 NTSTATUS idmap_rid_init(void)
259 {
260         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rid", &rid_methods);
261 }
262