Rename set_sd() to set_sd_blob() - this describes what it does.
[Samba/gebeck_regimport.git] / source4 / dsdb / kcc / kcc_service.c
blob8b35d6f01a52a470de289da743101d26573a1c93
1 /*
2 Unix SMB/CIFS mplementation.
4 KCC service
6 Copyright (C) Andrew Tridgell 2009
7 based on repl service code
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "auth/auth.h"
27 #include "smbd/service.h"
28 #include "lib/events/events.h"
29 #include "lib/messaging/irpc.h"
30 #include "dsdb/kcc/kcc_service.h"
31 #include <ldb_errors.h>
32 #include "../lib/util/dlinklist.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "librpc/gen_ndr/ndr_drsuapi.h"
35 #include "librpc/gen_ndr/ndr_drsblobs.h"
36 #include "param/param.h"
39 establish system creds
41 static WERROR kccsrv_init_creds(struct kccsrv_service *service)
43 service->system_session_info = system_session(service->task->lp_ctx);
44 if (!service->system_session_info) {
45 return WERR_NOMEM;
48 return WERR_OK;
52 connect to the local SAM
54 static WERROR kccsrv_connect_samdb(struct kccsrv_service *service, struct loadparm_context *lp_ctx)
56 const struct GUID *ntds_guid;
58 service->samdb = samdb_connect(service, service->task->event_ctx, lp_ctx, service->system_session_info, 0);
59 if (!service->samdb) {
60 return WERR_DS_UNAVAILABLE;
63 ntds_guid = samdb_ntds_objectGUID(service->samdb);
64 if (!ntds_guid) {
65 return WERR_DS_UNAVAILABLE;
68 service->ntds_guid = *ntds_guid;
70 if (samdb_rodc(service->samdb, &service->am_rodc) != LDB_SUCCESS) {
71 DEBUG(0,(__location__ ": Failed to determine RODC status\n"));
72 return WERR_DS_UNAVAILABLE;
75 return WERR_OK;
80 load our local partition list
82 static WERROR kccsrv_load_partitions(struct kccsrv_service *s)
84 struct ldb_dn *basedn;
85 struct ldb_result *r;
86 struct ldb_message_element *el;
87 static const char *attrs[] = { "namingContexts", "configurationNamingContext", NULL };
88 unsigned int i;
89 int ret;
91 basedn = ldb_dn_new(s, s->samdb, NULL);
92 W_ERROR_HAVE_NO_MEMORY(basedn);
94 ret = ldb_search(s->samdb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
95 "(objectClass=*)");
96 talloc_free(basedn);
97 if (ret != LDB_SUCCESS) {
98 return WERR_FOOBAR;
99 } else if (r->count != 1) {
100 talloc_free(r);
101 return WERR_FOOBAR;
104 el = ldb_msg_find_element(r->msgs[0], "namingContexts");
105 if (!el) {
106 return WERR_FOOBAR;
109 for (i=0; i < el->num_values; i++) {
110 const char *v = (const char *)el->values[i].data;
111 struct ldb_dn *pdn;
112 struct kccsrv_partition *p;
114 pdn = ldb_dn_new(s, s->samdb, v);
115 if (!ldb_dn_validate(pdn)) {
116 return WERR_FOOBAR;
119 p = talloc_zero(s, struct kccsrv_partition);
120 W_ERROR_HAVE_NO_MEMORY(p);
122 p->dn = talloc_steal(p, pdn);
123 p->service = s;
125 DLIST_ADD(s->partitions, p);
127 DEBUG(2, ("kccsrv_partition[%s] loaded\n", v));
130 el = ldb_msg_find_element(r->msgs[0], "configurationNamingContext");
131 if (!el) {
132 return WERR_FOOBAR;
134 s->config_dn = ldb_dn_new(s, s->samdb, (const char *)el->values[0].data);
135 if (!ldb_dn_validate(s->config_dn)) {
136 return WERR_FOOBAR;
139 talloc_free(r);
141 return WERR_OK;
144 static NTSTATUS kccsrv_execute_kcc(struct irpc_message *msg, struct drsuapi_DsExecuteKCC *r)
146 TALLOC_CTX *mem_ctx;
147 NTSTATUS status = NT_STATUS_OK;
148 struct kccsrv_service *service = talloc_get_type(msg->private_data, struct kccsrv_service);
150 mem_ctx = talloc_new(service);
152 if (service->samba_kcc_code)
153 status = kccsrv_samba_kcc(service, mem_ctx);
154 else {
155 status = kccsrv_simple_update(service, mem_ctx);
156 if (!NT_STATUS_IS_OK(status))
157 DEBUG(0,("kccsrv_execute_kcc failed - %s\n",
158 nt_errstr(status)));
161 talloc_free(mem_ctx);
162 return NT_STATUS_OK;
165 static NTSTATUS kccsrv_replica_get_info(struct irpc_message *msg, struct drsuapi_DsReplicaGetInfo *r)
167 return kccdrs_replica_get_info(msg, r);
171 startup the kcc service task
173 static void kccsrv_task_init(struct task_server *task)
175 WERROR status;
176 struct kccsrv_service *service;
177 uint32_t periodic_startup_interval;
179 switch (lpcfg_server_role(task->lp_ctx)) {
180 case ROLE_STANDALONE:
181 task_server_terminate(task, "kccsrv: no KCC required in standalone configuration", false);
182 return;
183 case ROLE_DOMAIN_MEMBER:
184 task_server_terminate(task, "kccsrv: no KCC required in domain member configuration", false);
185 return;
186 case ROLE_ACTIVE_DIRECTORY_DC:
187 /* Yes, we want a KCC */
188 break;
191 task_server_set_title(task, "task[kccsrv]");
193 service = talloc_zero(task, struct kccsrv_service);
194 if (!service) {
195 task_server_terminate(task, "kccsrv_task_init: out of memory", true);
196 return;
198 service->task = task;
199 service->startup_time = timeval_current();
200 task->private_data = service;
202 status = kccsrv_init_creds(service);
203 if (!W_ERROR_IS_OK(status)) {
204 task_server_terminate(task,
205 talloc_asprintf(task,
206 "kccsrv: Failed to obtain server credentials: %s\n",
207 win_errstr(status)), true);
208 return;
211 status = kccsrv_connect_samdb(service, task->lp_ctx);
212 if (!W_ERROR_IS_OK(status)) {
213 task_server_terminate(task, talloc_asprintf(task,
214 "kccsrv: Failed to connect to local samdb: %s\n",
215 win_errstr(status)), true);
216 return;
219 status = kccsrv_load_partitions(service);
220 if (!W_ERROR_IS_OK(status)) {
221 task_server_terminate(task, talloc_asprintf(task,
222 "kccsrv: Failed to load partitions: %s\n",
223 win_errstr(status)), true);
224 return;
227 periodic_startup_interval =
228 lpcfg_parm_int(task->lp_ctx, NULL, "kccsrv",
229 "periodic_startup_interval", 15); /* in seconds */
230 service->periodic.interval =
231 lpcfg_parm_int(task->lp_ctx, NULL, "kccsrv",
232 "periodic_interval", 300); /* in seconds */
234 /* (kccsrv:samba_kcc=true) will run newer samba_kcc replication
235 * topology generation code.
237 service->samba_kcc_code = lpcfg_parm_bool(task->lp_ctx, NULL,
238 "kccsrv", "samba_kcc", false);
240 status = kccsrv_periodic_schedule(service, periodic_startup_interval);
241 if (!W_ERROR_IS_OK(status)) {
242 task_server_terminate(task, talloc_asprintf(task,
243 "kccsrv: Failed to periodic schedule: %s\n",
244 win_errstr(status)), true);
245 return;
248 irpc_add_name(task->msg_ctx, "kccsrv");
250 IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSEXECUTEKCC, kccsrv_execute_kcc, service);
251 IRPC_REGISTER(task->msg_ctx, drsuapi, DRSUAPI_DSREPLICAGETINFO, kccsrv_replica_get_info, service);
255 register ourselves as a available server
257 NTSTATUS server_service_kcc_init(void)
259 return register_server_service("kcc", kccsrv_task_init);