s3:idmap_rid: remove unused domain_name from the idmap_rid_context.
[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         uint32_t base_rid;
29 };
30
31 /******************************************************************************
32   compat params can't be used because of the completely different way
33   we support multiple domains in the new idmap
34  *****************************************************************************/
35
36 static NTSTATUS idmap_rid_initialize(struct idmap_domain *dom,
37                                      const char *params)
38 {
39         NTSTATUS ret;
40         struct idmap_rid_context *ctx;
41         char *config_option = NULL;
42
43         ctx = TALLOC_ZERO_P(dom, struct idmap_rid_context);
44         if (ctx == NULL) {
45                 DEBUG(0, ("Out of memory!\n"));
46                 return NT_STATUS_NO_MEMORY;
47         }
48
49         config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
50         if ( ! config_option) {
51                 DEBUG(0, ("Out of memory!\n"));
52                 ret = NT_STATUS_NO_MEMORY;
53                 goto failed;
54         }
55
56         ctx->base_rid = lp_parm_int(-1, config_option, "base_rid", 0);
57
58         dom->private_data = ctx;
59
60         talloc_free(config_option);
61         return NT_STATUS_OK;
62
63 failed:
64         talloc_free(ctx);
65         return ret;
66 }
67
68 static NTSTATUS idmap_rid_id_to_sid(struct idmap_domain *dom, struct id_map *map)
69 {
70         struct winbindd_domain *domain;
71         struct idmap_rid_context *ctx;
72
73         ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
74
75         /* apply filters before checking */
76         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
77                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
78                                 map->xid.id, dom->low_id, dom->high_id));
79                 return NT_STATUS_NONE_MAPPED;
80         }
81
82         domain = find_domain_from_name_noinit(dom->name);
83         if (domain == NULL ) {
84                 return NT_STATUS_NO_SUCH_DOMAIN;
85         }
86
87         sid_compose(map->sid, &domain->sid, map->xid.id - dom->low_id + ctx->base_rid);
88
89         /* We **really** should have some way of validating 
90            the SID exists and is the correct type here.  But 
91            that is a deficiency in the idmap_rid design. */
92
93         map->status = ID_MAPPED;
94
95         return NT_STATUS_OK;
96 }
97
98 /**********************************
99  Single sid to id lookup function. 
100 **********************************/
101
102 static NTSTATUS idmap_rid_sid_to_id(struct idmap_domain *dom, struct id_map *map)
103 {
104         uint32_t rid;
105         struct idmap_rid_context *ctx;
106
107         ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
108
109         sid_peek_rid(map->sid, &rid);
110         map->xid.id = rid - ctx->base_rid + dom->low_id;
111
112         /* apply filters before returning result */
113
114         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
115                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
116                                 map->xid.id, dom->low_id, dom->high_id));
117                 map->status = ID_UNMAPPED;
118                 return NT_STATUS_NONE_MAPPED;
119         }
120
121         /* We **really** should have some way of validating 
122            the SID exists and is the correct type here.  But 
123            that is a deficiency in the idmap_rid design. */
124
125         map->status = ID_MAPPED;
126
127         return NT_STATUS_OK;
128 }
129
130 /**********************************
131  lookup a set of unix ids. 
132 **********************************/
133
134 static NTSTATUS idmap_rid_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
135 {
136         NTSTATUS ret;
137         int i;
138
139         /* initialize the status to avoid suprise */
140         for (i = 0; ids[i]; i++) {
141                 ids[i]->status = ID_UNKNOWN;
142         }
143
144         for (i = 0; ids[i]; i++) {
145
146                 ret = idmap_rid_id_to_sid(dom, ids[i]);
147
148                 if (( ! NT_STATUS_IS_OK(ret)) &&
149                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
150                         /* some fatal error occurred, log it */
151                         DEBUG(3, ("Unexpected error resolving an ID (%d)\n", ids[i]->xid.id));
152                 }
153         }
154
155         return NT_STATUS_OK;
156 }
157
158 /**********************************
159  lookup a set of sids. 
160 **********************************/
161
162 static NTSTATUS idmap_rid_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
163 {
164         NTSTATUS ret;
165         int i;
166
167         /* initialize the status to avoid suprise */
168         for (i = 0; ids[i]; i++) {
169                 ids[i]->status = ID_UNKNOWN;
170         }
171
172         for (i = 0; ids[i]; i++) {
173
174                 ret = idmap_rid_sid_to_id(dom, ids[i]);
175
176                 if (( ! NT_STATUS_IS_OK(ret)) &&
177                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
178                         /* some fatal error occurred, log it */
179                         DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
180                                   sid_string_dbg(ids[i]->sid)));
181                 }
182         }
183
184         return NT_STATUS_OK;
185 }
186
187 static NTSTATUS idmap_rid_close(struct idmap_domain *dom)
188 {
189         if (dom->private_data) {
190                 TALLOC_FREE(dom->private_data);
191         }
192         return NT_STATUS_OK;
193 }
194
195 static struct idmap_methods rid_methods = {
196         .init = idmap_rid_initialize,
197         .unixids_to_sids = idmap_rid_unixids_to_sids,
198         .sids_to_unixids = idmap_rid_sids_to_unixids,
199         .close_fn = idmap_rid_close
200 };
201
202 NTSTATUS idmap_rid_init(void)
203 {
204         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rid", &rid_methods);
205 }
206