summaryrefslogtreecommitdiff
path: root/source4/dsdb/samdb/ldb_modules/samba3sid.c
blob: f38ab407bb4a586ccc45df6974a10b3c2b923cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
   samba3sid module

   Copyright (C) Andrew Bartlett 2010
   Copyright (C) Andrew Tridgell 2010

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
  add objectSid to users and groups using samba3 nextRid method
 */

#include "includes.h"
#include "libcli/ldap/ldap_ndr.h"
#include "ldb_module.h"
#include "dsdb/samdb/samdb.h"
#include "dsdb/samdb/ldb_modules/util.h"
#include "libcli/security/security.h"
#include "librpc/gen_ndr/ndr_security.h"
#include "ldb_wrap.h"
#include "param/param.h"

/*
  RID algorithm from pdb_ldap.c in source3/passdb/
  (loosely based on Volkers code)
 */
static int samba3sid_next_sid(struct ldb_module *module,
			      TALLOC_CTX *mem_ctx, char **sid,
			      struct ldb_request *parent)
{
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	struct ldb_result *res;
	const char *attrs[] = { "sambaNextRid", "sambaNextUserRid",
				"sambaNextGroupRid", "sambaSID", NULL };
	int ret;
	struct ldb_context *ldb = ldb_module_get_ctx(module);
	struct ldb_message *msg;
	uint32_t sambaNextRid, sambaNextGroupRid, sambaNextUserRid, rid;
	const char *sambaSID;

	ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
				 attrs,
				 DSDB_FLAG_NEXT_MODULE |
				 DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
				 parent,
				 "(&(objectClass=sambaDomain)(sambaDomainName=%s))",
				 lpcfg_sam_name(ldb_get_opaque(ldb, "loadparm")));
	if (ret != LDB_SUCCESS) {
		ldb_asprintf_errstring(ldb,
				       __location__
				       ": Failed to find domain object - %s",
				       ldb_errstring(ldb));
		talloc_free(tmp_ctx);
		return ret;
	}
	if (res->count != 1) {
		ldb_asprintf_errstring(ldb,
				       __location__
				       ": Expected exactly 1 domain object - got %u",
				       res->count);
		talloc_free(tmp_ctx);
		return LDB_ERR_OPERATIONS_ERROR;
	}
	msg = res->msgs[0];

	sambaNextRid = ldb_msg_find_attr_as_uint(msg, "sambaNextRid",
						 (uint32_t) -1);
	sambaNextUserRid = ldb_msg_find_attr_as_uint(msg, "sambaNextUserRid",
						     (uint32_t) -1);
	sambaNextGroupRid = ldb_msg_find_attr_as_uint(msg, "sambaNextGroupRid",
						      (uint32_t) -1);
	sambaSID = ldb_msg_find_attr_as_string(msg, "sambaSID", NULL);

	if (sambaSID == NULL) {
		ldb_asprintf_errstring(ldb,
				       __location__
				       ": No sambaSID in %s",
				       ldb_dn_get_linearized(msg->dn));
		talloc_free(tmp_ctx);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	/* choose the highest of the 3 - see pdb_ldap.c for an
	 * explaination */
	rid = sambaNextRid;
	if ((sambaNextUserRid != (uint32_t) -1) && (sambaNextUserRid > rid)) {
		rid = sambaNextUserRid;
	}
	if ((sambaNextGroupRid != (uint32_t) -1) && (sambaNextGroupRid > rid)) {
		rid = sambaNextGroupRid;
	}
	if (rid == (uint32_t) -1) {
		ldb_asprintf_errstring(ldb,
				       __location__
				       ": No sambaNextRid in %s",
				       ldb_dn_get_linearized(msg->dn));
		talloc_free(tmp_ctx);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	/* sambaNextRid is actually the previous RID .... */
	rid += 1;

	(*sid) = talloc_asprintf(tmp_ctx, "%s-%u", sambaSID, rid);
	if (!*sid) {
		talloc_free(tmp_ctx);
		return ldb_module_oom(module);
	}

	ret = dsdb_module_constrainted_update_uint32(module, msg->dn,
						     "sambaNextRid",
						     &sambaNextRid, &rid, parent);
	if (ret != LDB_SUCCESS) {
		ldb_asprintf_errstring(ldb,
				       __location__
				       ": Failed to update sambaNextRid - %s",
				       ldb_errstring(ldb));
		talloc_free(tmp_ctx);
		return ret;
	}

	talloc_steal(mem_ctx, *sid);
	talloc_free(tmp_ctx);
	return LDB_SUCCESS;
}



/* add */
static int samba3sid_add(struct ldb_module *module, struct ldb_request *req)
{
	struct ldb_context *ldb;
	int ret;
	const struct ldb_message *msg = req->op.add.message;
	struct ldb_message *new_msg;
	char *sid;
	struct ldb_request *new_req;

	ldb = ldb_module_get_ctx(module);

	/* do not manipulate our control entries */
	if (ldb_dn_is_special(req->op.add.message->dn)) {
		return ldb_next_request(module, req);
	}

	if (!samdb_find_attribute(ldb, msg, "objectclass", "posixAccount") &&
	    !samdb_find_attribute(ldb, msg, "objectclass", "posixGroup")) {
		/* its not a user or a group */
		return ldb_next_request(module, req);
	}

	if (ldb_msg_find_element(msg, "sambaSID")) {
		/* a SID was supplied */
		return ldb_next_request(module, req);
	}

	new_msg = ldb_msg_copy_shallow(req, req->op.add.message);
	if (!new_msg) {
		return ldb_module_oom(module);
	}

	ret = samba3sid_next_sid(module, new_msg, &sid, req);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	ret = ldb_msg_add_steal_string(new_msg, "sambaSID", sid);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	ret = ldb_build_add_req(&new_req, ldb, req,
				new_msg,
				req->controls,
				req, dsdb_next_callback,
				req);
	LDB_REQ_SET_LOCATION(new_req);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	return ldb_next_request(module, new_req);
}

static const struct ldb_module_ops ldb_samba3sid_module_ops = {
	.name          = "samba3sid",
	.add           = samba3sid_add,
};


int ldb_samba3sid_module_init(const char *version)
{
	LDB_MODULE_CHECK_VERSION(version);
	return ldb_register_module(&ldb_samba3sid_module_ops);
}