4 Copyright (C) Andrew Bartlett 2010
5 Copyright (C) Andrew Tridgell 2010
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.
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.
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/>.
22 add objectSid to users and groups using samba3 nextRid method
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"
33 #include "param/param.h"
36 RID algorithm from pdb_ldap.c in source3/passdb/
37 (loosely based on Volkers code)
39 static int samba3sid_next_sid(struct ldb_module
*module
,
40 TALLOC_CTX
*mem_ctx
, char **sid
,
41 struct ldb_request
*parent
)
43 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
44 struct ldb_result
*res
;
45 const char *attrs
[] = { "sambaNextRid", "sambaNextUserRid",
46 "sambaNextGroupRid", "sambaSID", NULL
};
48 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
49 struct ldb_message
*msg
;
50 uint32_t sambaNextRid
, sambaNextGroupRid
, sambaNextUserRid
, rid
;
53 ret
= dsdb_module_search(module
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
55 DSDB_FLAG_NEXT_MODULE
|
56 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
,
58 "(&(objectClass=sambaDomain)(sambaDomainName=%s))",
59 lpcfg_sam_name(ldb_get_opaque(ldb
, "loadparm")));
60 if (ret
!= LDB_SUCCESS
) {
61 ldb_asprintf_errstring(ldb
,
63 ": Failed to find domain object - %s",
68 if (res
->count
!= 1) {
69 ldb_asprintf_errstring(ldb
,
71 ": Expected exactly 1 domain object - got %u",
74 return LDB_ERR_OPERATIONS_ERROR
;
78 sambaNextRid
= ldb_msg_find_attr_as_uint(msg
, "sambaNextRid",
80 sambaNextUserRid
= ldb_msg_find_attr_as_uint(msg
, "sambaNextUserRid",
82 sambaNextGroupRid
= ldb_msg_find_attr_as_uint(msg
, "sambaNextGroupRid",
84 sambaSID
= ldb_msg_find_attr_as_string(msg
, "sambaSID", NULL
);
86 if (sambaSID
== NULL
) {
87 ldb_asprintf_errstring(ldb
,
89 ": No sambaSID in %s",
90 ldb_dn_get_linearized(msg
->dn
));
92 return LDB_ERR_OPERATIONS_ERROR
;
95 /* choose the highest of the 3 - see pdb_ldap.c for an
98 if ((sambaNextUserRid
!= (uint32_t) -1) && (sambaNextUserRid
> rid
)) {
99 rid
= sambaNextUserRid
;
101 if ((sambaNextGroupRid
!= (uint32_t) -1) && (sambaNextGroupRid
> rid
)) {
102 rid
= sambaNextGroupRid
;
104 if (rid
== (uint32_t) -1) {
105 ldb_asprintf_errstring(ldb
,
107 ": No sambaNextRid in %s",
108 ldb_dn_get_linearized(msg
->dn
));
109 talloc_free(tmp_ctx
);
110 return LDB_ERR_OPERATIONS_ERROR
;
113 /* sambaNextRid is actually the previous RID .... */
116 (*sid
) = talloc_asprintf(tmp_ctx
, "%s-%u", sambaSID
, rid
);
118 talloc_free(tmp_ctx
);
119 return ldb_module_oom(module
);
122 ret
= dsdb_module_constrainted_update_uint32(module
, msg
->dn
,
124 &sambaNextRid
, &rid
, parent
);
125 if (ret
!= LDB_SUCCESS
) {
126 ldb_asprintf_errstring(ldb
,
128 ": Failed to update sambaNextRid - %s",
130 talloc_free(tmp_ctx
);
134 talloc_steal(mem_ctx
, *sid
);
135 talloc_free(tmp_ctx
);
142 static int samba3sid_add(struct ldb_module
*module
, struct ldb_request
*req
)
144 struct ldb_context
*ldb
;
146 const struct ldb_message
*msg
= req
->op
.add
.message
;
147 struct ldb_message
*new_msg
;
149 struct ldb_request
*new_req
;
151 ldb
= ldb_module_get_ctx(module
);
153 /* do not manipulate our control entries */
154 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
155 return ldb_next_request(module
, req
);
158 if (!samdb_find_attribute(ldb
, msg
, "objectclass", "posixAccount") &&
159 !samdb_find_attribute(ldb
, msg
, "objectclass", "posixGroup")) {
160 /* its not a user or a group */
161 return ldb_next_request(module
, req
);
164 if (ldb_msg_find_element(msg
, "sambaSID")) {
165 /* a SID was supplied */
166 return ldb_next_request(module
, req
);
169 new_msg
= ldb_msg_copy_shallow(req
, req
->op
.add
.message
);
171 return ldb_module_oom(module
);
174 ret
= samba3sid_next_sid(module
, new_msg
, &sid
, req
);
175 if (ret
!= LDB_SUCCESS
) {
179 ret
= ldb_msg_add_steal_string(new_msg
, "sambaSID", sid
);
180 if (ret
!= LDB_SUCCESS
) {
184 ret
= ldb_build_add_req(&new_req
, ldb
, req
,
187 req
, dsdb_next_callback
,
189 LDB_REQ_SET_LOCATION(new_req
);
190 if (ret
!= LDB_SUCCESS
) {
194 return ldb_next_request(module
, new_req
);
197 static const struct ldb_module_ops ldb_samba3sid_module_ops
= {
199 .add
= samba3sid_add
,
203 int ldb_samba3sid_module_init(const char *version
)
205 LDB_MODULE_CHECK_VERSION(version
);
206 return ldb_register_module(&ldb_samba3sid_module_ops
);