s4-kcc: add a very simple KCC
[Samba/aatanasov.git] / source4 / dsdb / kcc / kcc_service.c
blob22798790000ca3f545fefa020254b23f45378c54
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 "lib/ldb/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 NTSTATUS status;
45 status = auth_system_session_info(service, service->task->lp_ctx,
46 &service->system_session_info);
47 if (!NT_STATUS_IS_OK(status)) {
48 return ntstatus_to_werror(status);
51 return WERR_OK;
55 connect to the local SAM
57 static WERROR kccsrv_connect_samdb(struct kccsrv_service *service, struct loadparm_context *lp_ctx)
59 const struct GUID *ntds_guid;
61 service->samdb = samdb_connect(service, service->task->event_ctx, lp_ctx, service->system_session_info);
62 if (!service->samdb) {
63 return WERR_DS_UNAVAILABLE;
66 ntds_guid = samdb_ntds_objectGUID(service->samdb);
67 if (!ntds_guid) {
68 return WERR_DS_UNAVAILABLE;
71 service->ntds_guid = *ntds_guid;
73 return WERR_OK;
78 load our local partition list
80 static WERROR kccsrv_load_partitions(struct kccsrv_service *s)
82 struct ldb_dn *basedn;
83 struct ldb_result *r;
84 struct ldb_message_element *el;
85 static const char *attrs[] = { "namingContexts", "configurationNamingContext", NULL };
86 uint32_t i;
87 int ret;
89 basedn = ldb_dn_new(s, s->samdb, NULL);
90 W_ERROR_HAVE_NO_MEMORY(basedn);
92 ret = ldb_search(s->samdb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
93 "(objectClass=*)");
94 talloc_free(basedn);
95 if (ret != LDB_SUCCESS) {
96 return WERR_FOOBAR;
97 } else if (r->count != 1) {
98 talloc_free(r);
99 return WERR_FOOBAR;
102 el = ldb_msg_find_element(r->msgs[0], "namingContexts");
103 if (!el) {
104 return WERR_FOOBAR;
107 for (i=0; el && i < el->num_values; i++) {
108 const char *v = (const char *)el->values[i].data;
109 struct ldb_dn *pdn;
110 struct kccsrv_partition *p;
112 pdn = ldb_dn_new(s, s->samdb, v);
113 if (!ldb_dn_validate(pdn)) {
114 return WERR_FOOBAR;
117 p = talloc_zero(s, struct kccsrv_partition);
118 W_ERROR_HAVE_NO_MEMORY(p);
120 p->dn = talloc_steal(p, pdn);
121 p->service = s;
123 DLIST_ADD(s->partitions, p);
125 DEBUG(2, ("kccsrv_partition[%s] loaded\n", v));
128 el = ldb_msg_find_element(r->msgs[0], "configurationNamingContext");
129 if (!el) {
130 return WERR_FOOBAR;
132 s->config_dn = ldb_dn_new(s, s->samdb, (const char *)el->values[0].data);
133 if (!ldb_dn_validate(s->config_dn)) {
134 return WERR_FOOBAR;
137 talloc_free(r);
139 return WERR_OK;
144 startup the kcc service task
146 static void kccsrv_task_init(struct task_server *task)
148 WERROR status;
149 struct kccsrv_service *service;
150 uint32_t periodic_startup_interval;
152 switch (lp_server_role(task->lp_ctx)) {
153 case ROLE_STANDALONE:
154 task_server_terminate(task, "kccsrv: no KCC required in standalone configuration");
155 return;
156 case ROLE_DOMAIN_MEMBER:
157 task_server_terminate(task, "kccsrv: no KCC required in domain member configuration");
158 return;
159 case ROLE_DOMAIN_CONTROLLER:
160 /* Yes, we want a KCC */
161 break;
164 task_server_set_title(task, "task[kccsrv]");
166 service = talloc_zero(task, struct kccsrv_service);
167 if (!service) {
168 task_server_terminate(task, "kccsrv_task_init: out of memory");
169 return;
171 service->task = task;
172 service->startup_time = timeval_current();
173 task->private_data = service;
175 status = kccsrv_init_creds(service);
176 if (!W_ERROR_IS_OK(status)) {
177 task_server_terminate(task, talloc_asprintf(task,
178 "kccsrv: Failed to obtain server credentials: %s\n",
179 win_errstr(status)));
180 return;
183 status = kccsrv_connect_samdb(service, task->lp_ctx);
184 if (!W_ERROR_IS_OK(status)) {
185 task_server_terminate(task, talloc_asprintf(task,
186 "kccsrv: Failed to connect to local samdb: %s\n",
187 win_errstr(status)));
188 return;
191 status = kccsrv_load_partitions(service);
192 if (!W_ERROR_IS_OK(status)) {
193 task_server_terminate(task, talloc_asprintf(task,
194 "kccsrv: Failed to load partitions: %s\n",
195 win_errstr(status)));
196 return;
199 periodic_startup_interval = lp_parm_int(task->lp_ctx, NULL, "kccsrv",
200 "periodic_startup_interval", 15); /* in seconds */
201 service->periodic.interval = lp_parm_int(task->lp_ctx, NULL, "kccsrv",
202 "periodic_interval", 300); /* in seconds */
204 status = kccsrv_periodic_schedule(service, periodic_startup_interval);
205 if (!W_ERROR_IS_OK(status)) {
206 task_server_terminate(task, talloc_asprintf(task,
207 "kccsrv: Failed to periodic schedule: %s\n",
208 win_errstr(status)));
209 return;
212 irpc_add_name(task->msg_ctx, "kccsrv");
216 register ourselves as a available server
218 NTSTATUS server_service_kcc_init(void)
220 return register_server_service("kcc", kccsrv_task_init);