76bcfce71388422042331af704b6c304d6ed77a3
[samba.git] / source4 / dsdb / samdb / ldb_modules / samba3sid.c
1 /*
2    samba3sid module
3
4    Copyright (C) Andrew Bartlett 2010
5    Copyright (C) Andrew Tridgell 2010
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22   add objectSid to users and groups using samba3 nextRid method
23  */
24
25 #include "includes.h"
26 #include "libcli/ldap/ldap_ndr.h"
27 #include "ldb_module.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/samdb/ldb_modules/util.h"
30 #include "libcli/security/security.h"
31 #include "librpc/gen_ndr/ndr_security.h"
32 #include "ldb_wrap.h"
33 #include "param/param.h"
34
35 /*
36   RID algorithm from pdb_ldap.c in source3/passdb/
37   (loosely based on Volkers code)
38  */
39 static int samba3sid_next_sid(struct ldb_module *module,
40                               TALLOC_CTX *mem_ctx, char **sid)
41 {
42         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
43         struct ldb_result *res;
44         const char *attrs[] = { "sambaNextRid", "sambaNextUserRid",
45                                 "sambaNextGroupRid", "sambaSID", NULL };
46         int ret;
47         struct ldb_context *ldb = ldb_module_get_ctx(module);
48         struct ldb_message *msg;
49         uint32_t sambaNextRid, sambaNextGroupRid, sambaNextUserRid, rid;
50         const char *sambaSID;
51
52         ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
53                                  attrs,
54                                  DSDB_FLAG_NEXT_MODULE |
55                                  DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
56                                  "(&(objectClass=sambaDomain)(sambaDomainName=%s))",
57                                  lpcfg_sam_name(ldb_get_opaque(ldb, "loadparm")));
58         if (ret != LDB_SUCCESS) {
59                 ldb_asprintf_errstring(ldb,
60                                        __location__
61                                        ": Failed to find domain object - %s",
62                                        ldb_errstring(ldb));
63                 talloc_free(tmp_ctx);
64                 return ret;
65         }
66         if (res->count != 1) {
67                 ldb_asprintf_errstring(ldb,
68                                        __location__
69                                        ": Expected exactly 1 domain object - got %u",
70                                        res->count);
71                 talloc_free(tmp_ctx);
72                 return LDB_ERR_OPERATIONS_ERROR;
73         }
74         msg = res->msgs[0];
75
76         sambaNextRid = ldb_msg_find_attr_as_uint(msg, "sambaNextRid",
77                                                  (uint32_t) -1);
78         sambaNextUserRid = ldb_msg_find_attr_as_uint(msg, "sambaNextUserRid",
79                                                      (uint32_t) -1);
80         sambaNextGroupRid = ldb_msg_find_attr_as_uint(msg, "sambaNextGroupRid",
81                                                       (uint32_t) -1);
82         sambaSID = ldb_msg_find_attr_as_string(msg, "sambaSID", NULL);
83
84         if (sambaSID == NULL) {
85                 ldb_asprintf_errstring(ldb,
86                                        __location__
87                                        ": No sambaSID in %s",
88                                        ldb_dn_get_linearized(msg->dn));
89                 talloc_free(tmp_ctx);
90                 return LDB_ERR_OPERATIONS_ERROR;
91         }
92
93         /* choose the highest of the 3 - see pdb_ldap.c for an
94          * explaination */
95         rid = sambaNextRid;
96         if ((sambaNextUserRid != (uint32_t) -1) && (sambaNextUserRid > rid)) {
97                 rid = sambaNextUserRid;
98         }
99         if ((sambaNextGroupRid != (uint32_t) -1) && (sambaNextGroupRid > rid)) {
100                 rid = sambaNextGroupRid;
101         }
102         if (rid == (uint32_t) -1) {
103                 ldb_asprintf_errstring(ldb,
104                                        __location__
105                                        ": No sambaNextRid in %s",
106                                        ldb_dn_get_linearized(msg->dn));
107                 talloc_free(tmp_ctx);
108                 return LDB_ERR_OPERATIONS_ERROR;
109         }
110
111         /* sambaNextRid is actually the previous RID .... */
112         rid += 1;
113
114         (*sid) = talloc_asprintf(tmp_ctx, "%s-%u", sambaSID, rid);
115         if (!*sid) {
116                 talloc_free(tmp_ctx);
117                 return ldb_module_oom(module);
118         }
119
120         ret = dsdb_module_constrainted_update_uint32(module, msg->dn,
121                                                      "sambaNextRid",
122                                                      &sambaNextRid, &rid);
123         if (ret != LDB_SUCCESS) {
124                 ldb_asprintf_errstring(ldb,
125                                        __location__
126                                        ": Failed to update sambaNextRid - %s",
127                                        ldb_errstring(ldb));
128                 talloc_free(tmp_ctx);
129                 return ret;
130         }
131
132         talloc_steal(mem_ctx, *sid);
133         talloc_free(tmp_ctx);
134         return LDB_SUCCESS;
135 }
136
137
138
139 /* add */
140 static int samba3sid_add(struct ldb_module *module, struct ldb_request *req)
141 {
142         struct ldb_context *ldb;
143         int ret;
144         const struct ldb_message *msg = req->op.add.message;
145         struct ldb_message *new_msg;
146         char *sid;
147         struct ldb_request *new_req;
148
149         ldb = ldb_module_get_ctx(module);
150
151         /* do not manipulate our control entries */
152         if (ldb_dn_is_special(req->op.add.message->dn)) {
153                 return ldb_next_request(module, req);
154         }
155
156         if (!samdb_find_attribute(ldb, msg, "objectclass", "posixAccount") &&
157             !samdb_find_attribute(ldb, msg, "objectclass", "posixGroup")) {
158                 /* its not a user or a group */
159                 return ldb_next_request(module, req);
160         }
161
162         if (ldb_msg_find_element(msg, "sambaSID")) {
163                 /* a SID was supplied */
164                 return ldb_next_request(module, req);
165         }
166
167         new_msg = ldb_msg_copy_shallow(req, req->op.add.message);
168         if (!new_msg) {
169                 return ldb_module_oom(module);
170         }
171
172         ret = samba3sid_next_sid(module, new_msg, &sid);
173         if (ret != LDB_SUCCESS) {
174                 return ret;
175         }
176
177         ret = ldb_msg_add_steal_string(new_msg, "sambaSID", sid);
178         if (ret != LDB_SUCCESS) {
179                 return ret;
180         }
181
182         ret = ldb_build_add_req(&new_req, ldb, req,
183                                 new_msg,
184                                 req->controls,
185                                 req, dsdb_next_callback,
186                                 req);
187         LDB_REQ_SET_LOCATION(new_req);
188         if (ret != LDB_SUCCESS) {
189                 return ret;
190         }
191
192         return ldb_next_request(module, new_req);
193 }
194
195 static const struct ldb_module_ops ldb_samba3sid_module_ops = {
196         .name          = "samba3sid",
197         .add           = samba3sid_add,
198 };
199
200
201 int ldb_samba3sid_module_init(const char *version)
202 {
203         LDB_MODULE_CHECK_VERSION(version);
204         return ldb_register_module(&ldb_samba3sid_module_ops);
205 }