Revert "s4:remove "util_ldb" submodule and integrate the three gendb_* calls in ...
[metze/samba/wip.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 "../lib/util/util_ldb.h"
33 #include "ldb_wrap.h"
34 #include "param/param.h"
35
36 /*
37   RID algorithm from pdb_ldap.c in source3/passdb/
38   (loosely based on Volkers code)
39  */
40 static int samba3sid_next_sid(struct ldb_module *module,
41                               TALLOC_CTX *mem_ctx, char **sid)
42 {
43         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
44         struct ldb_result *res;
45         const char *attrs[] = { "sambaNextRid", "sambaNextUserRid",
46                                 "sambaNextGroupRid", "sambaSID", NULL };
47         int ret;
48         struct ldb_context *ldb = ldb_module_get_ctx(module);
49         struct ldb_message *msg;
50         uint32_t sambaNextRid, sambaNextGroupRid, sambaNextUserRid, rid;
51         const char *sambaSID;
52
53         ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
54                                  attrs,
55                                  DSDB_FLAG_NEXT_MODULE |
56                                  DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
57                                  "(&(objectClass=sambaDomain)(sambaDomainName=%s))",
58                                  lpcfg_sam_name(ldb_get_opaque(ldb, "loadparm")));
59         if (ret != LDB_SUCCESS) {
60                 ldb_asprintf_errstring(ldb,
61                                        __location__
62                                        ": Failed to find domain object - %s",
63                                        ldb_errstring(ldb));
64                 talloc_free(tmp_ctx);
65                 return ret;
66         }
67         if (res->count != 1) {
68                 ldb_asprintf_errstring(ldb,
69                                        __location__
70                                        ": Expected exactly 1 domain object - got %u",
71                                        res->count);
72                 talloc_free(tmp_ctx);
73                 return LDB_ERR_OPERATIONS_ERROR;
74         }
75         msg = res->msgs[0];
76
77         sambaNextRid = ldb_msg_find_attr_as_uint(msg, "sambaNextRid",
78                                                  (uint32_t) -1);
79         sambaNextUserRid = ldb_msg_find_attr_as_uint(msg, "sambaNextUserRid",
80                                                      (uint32_t) -1);
81         sambaNextGroupRid = ldb_msg_find_attr_as_uint(msg, "sambaNextGroupRid",
82                                                       (uint32_t) -1);
83         sambaSID = ldb_msg_find_attr_as_string(msg, "sambaSID", NULL);
84
85         if (sambaSID == NULL) {
86                 ldb_asprintf_errstring(ldb,
87                                        __location__
88                                        ": No sambaSID in %s",
89                                        ldb_dn_get_linearized(msg->dn));
90                 talloc_free(tmp_ctx);
91                 return LDB_ERR_OPERATIONS_ERROR;
92         }
93
94         /* choose the highest of the 3 - see pdb_ldap.c for an
95          * explaination */
96         rid = sambaNextRid;
97         if ((sambaNextUserRid != (uint32_t) -1) && (sambaNextUserRid > rid)) {
98                 rid = sambaNextUserRid;
99         }
100         if ((sambaNextGroupRid != (uint32_t) -1) && (sambaNextGroupRid > rid)) {
101                 rid = sambaNextGroupRid;
102         }
103         if (rid == (uint32_t) -1) {
104                 ldb_asprintf_errstring(ldb,
105                                        __location__
106                                        ": No sambaNextRid in %s",
107                                        ldb_dn_get_linearized(msg->dn));
108                 talloc_free(tmp_ctx);
109                 return LDB_ERR_OPERATIONS_ERROR;
110         }
111
112         /* sambaNextRid is actually the previous RID .... */
113         rid += 1;
114
115         (*sid) = talloc_asprintf(tmp_ctx, "%s-%u", sambaSID, rid);
116         if (!*sid) {
117                 talloc_free(tmp_ctx);
118                 return ldb_module_oom(module);
119         }
120
121         ret = dsdb_module_constrainted_update_uint32(module, msg->dn,
122                                                      "sambaNextRid",
123                                                      &sambaNextRid, &rid);
124         if (ret != LDB_SUCCESS) {
125                 ldb_asprintf_errstring(ldb,
126                                        __location__
127                                        ": Failed to update sambaNextRid - %s",
128                                        ldb_errstring(ldb));
129                 talloc_free(tmp_ctx);
130                 return ret;
131         }
132
133         talloc_steal(mem_ctx, *sid);
134         talloc_free(tmp_ctx);
135         return LDB_SUCCESS;
136 }
137
138
139
140 /* add */
141 static int samba3sid_add(struct ldb_module *module, struct ldb_request *req)
142 {
143         struct ldb_context *ldb;
144         int ret;
145         const struct ldb_message *msg = req->op.add.message;
146         struct ldb_message *new_msg;
147         char *sid;
148         struct ldb_request *new_req;
149
150         ldb = ldb_module_get_ctx(module);
151
152         /* do not manipulate our control entries */
153         if (ldb_dn_is_special(req->op.add.message->dn)) {
154                 return ldb_next_request(module, req);
155         }
156
157         if (!samdb_find_attribute(ldb, msg, "objectclass", "posixAccount") &&
158             !samdb_find_attribute(ldb, msg, "objectclass", "posixGroup")) {
159                 /* its not a user or a group */
160                 return ldb_next_request(module, req);
161         }
162
163         if (ldb_msg_find_element(msg, "sambaSID")) {
164                 /* a SID was supplied */
165                 return ldb_next_request(module, req);
166         }
167
168         new_msg = ldb_msg_copy_shallow(req, req->op.add.message);
169         if (!new_msg) {
170                 return ldb_module_oom(module);
171         }
172
173         ret = samba3sid_next_sid(module, new_msg, &sid);
174         if (ret != LDB_SUCCESS) {
175                 return ret;
176         }
177
178         ret = ldb_msg_add_steal_string(new_msg, "sambaSID", sid);
179         if (ret != LDB_SUCCESS) {
180                 return ret;
181         }
182
183         ret = ldb_build_add_req(&new_req, ldb, req,
184                                 new_msg,
185                                 req->controls,
186                                 req, dsdb_next_callback,
187                                 req);
188         LDB_REQ_SET_LOCATION(new_req);
189         if (ret != LDB_SUCCESS) {
190                 return ret;
191         }
192
193         return ldb_next_request(module, new_req);
194 }
195
196 _PUBLIC_ const struct ldb_module_ops ldb_samba3sid_module_ops = {
197         .name          = "samba3sid",
198         .add           = samba3sid_add,
199 };
200