s3:idmap_rid: remove a comment that does not apply in that place.
[amitay/samba.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         map->status = ID_MAPPED;
122
123         return NT_STATUS_OK;
124 }
125
126 /**********************************
127  lookup a set of unix ids. 
128 **********************************/
129
130 static NTSTATUS idmap_rid_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
131 {
132         NTSTATUS ret;
133         int i;
134
135         /* initialize the status to avoid suprise */
136         for (i = 0; ids[i]; i++) {
137                 ids[i]->status = ID_UNKNOWN;
138         }
139
140         for (i = 0; ids[i]; i++) {
141
142                 ret = idmap_rid_id_to_sid(dom, ids[i]);
143
144                 if (( ! NT_STATUS_IS_OK(ret)) &&
145                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
146                         /* some fatal error occurred, log it */
147                         DEBUG(3, ("Unexpected error resolving an ID (%d)\n", ids[i]->xid.id));
148                 }
149         }
150
151         return NT_STATUS_OK;
152 }
153
154 /**********************************
155  lookup a set of sids. 
156 **********************************/
157
158 static NTSTATUS idmap_rid_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
159 {
160         NTSTATUS ret;
161         int i;
162
163         /* initialize the status to avoid suprise */
164         for (i = 0; ids[i]; i++) {
165                 ids[i]->status = ID_UNKNOWN;
166         }
167
168         for (i = 0; ids[i]; i++) {
169
170                 ret = idmap_rid_sid_to_id(dom, ids[i]);
171
172                 if (( ! NT_STATUS_IS_OK(ret)) &&
173                     ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
174                         /* some fatal error occurred, log it */
175                         DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
176                                   sid_string_dbg(ids[i]->sid)));
177                 }
178         }
179
180         return NT_STATUS_OK;
181 }
182
183 static NTSTATUS idmap_rid_close(struct idmap_domain *dom)
184 {
185         if (dom->private_data) {
186                 TALLOC_FREE(dom->private_data);
187         }
188         return NT_STATUS_OK;
189 }
190
191 static struct idmap_methods rid_methods = {
192         .init = idmap_rid_initialize,
193         .unixids_to_sids = idmap_rid_unixids_to_sids,
194         .sids_to_unixids = idmap_rid_sids_to_unixids,
195         .close_fn = idmap_rid_close
196 };
197
198 NTSTATUS idmap_rid_init(void)
199 {
200         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rid", &rid_methods);
201 }
202