2 Unix SMB/CIFS mplementation.
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/>.
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 service
->system_session_info
= system_session(service
->task
->lp_ctx
);
44 if (!service
->system_session_info
) {
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
);
59 if (!service
->samdb
) {
60 return WERR_DS_UNAVAILABLE
;
63 ntds_guid
= samdb_ntds_objectGUID(service
->samdb
);
65 return WERR_DS_UNAVAILABLE
;
68 service
->ntds_guid
= *ntds_guid
;
75 load our local partition list
77 static WERROR
kccsrv_load_partitions(struct kccsrv_service
*s
)
79 struct ldb_dn
*basedn
;
81 struct ldb_message_element
*el
;
82 static const char *attrs
[] = { "namingContexts", "configurationNamingContext", NULL
};
86 basedn
= ldb_dn_new(s
, s
->samdb
, NULL
);
87 W_ERROR_HAVE_NO_MEMORY(basedn
);
89 ret
= ldb_search(s
->samdb
, s
, &r
, basedn
, LDB_SCOPE_BASE
, attrs
,
92 if (ret
!= LDB_SUCCESS
) {
94 } else if (r
->count
!= 1) {
99 el
= ldb_msg_find_element(r
->msgs
[0], "namingContexts");
104 for (i
=0; el
&& i
< el
->num_values
; i
++) {
105 const char *v
= (const char *)el
->values
[i
].data
;
107 struct kccsrv_partition
*p
;
109 pdn
= ldb_dn_new(s
, s
->samdb
, v
);
110 if (!ldb_dn_validate(pdn
)) {
114 p
= talloc_zero(s
, struct kccsrv_partition
);
115 W_ERROR_HAVE_NO_MEMORY(p
);
117 p
->dn
= talloc_steal(p
, pdn
);
120 DLIST_ADD(s
->partitions
, p
);
122 DEBUG(2, ("kccsrv_partition[%s] loaded\n", v
));
125 el
= ldb_msg_find_element(r
->msgs
[0], "configurationNamingContext");
129 s
->config_dn
= ldb_dn_new(s
, s
->samdb
, (const char *)el
->values
[0].data
);
130 if (!ldb_dn_validate(s
->config_dn
)) {
139 static NTSTATUS
kccsrv_execute_kcc(struct irpc_message
*msg
, struct drsuapi_DsExecuteKCC
*r
)
143 struct kccsrv_service
*service
= talloc_get_type(msg
->private_data
, struct kccsrv_service
);
145 mem_ctx
= talloc_new(service
);
146 status
= kccsrv_simple_update(service
, mem_ctx
);
148 if (!NT_STATUS_IS_OK(status
)) {
149 DEBUG(0,("kccsrv_simple_update failed - %s\n", nt_errstr(status
)));
150 talloc_free(mem_ctx
);
154 talloc_free(mem_ctx
);
158 static NTSTATUS
kccsrv_replica_get_info(struct irpc_message
*msg
, struct drsuapi_DsReplicaGetInfo
*r
)
160 return kccdrs_replica_get_info(msg
, r
);
164 startup the kcc service task
166 static void kccsrv_task_init(struct task_server
*task
)
169 struct kccsrv_service
*service
;
170 uint32_t periodic_startup_interval
;
172 switch (lpcfg_server_role(task
->lp_ctx
)) {
173 case ROLE_STANDALONE
:
174 task_server_terminate(task
, "kccsrv: no KCC required in standalone configuration", false);
176 case ROLE_DOMAIN_MEMBER
:
177 task_server_terminate(task
, "kccsrv: no KCC required in domain member configuration", false);
179 case ROLE_DOMAIN_CONTROLLER
:
180 /* Yes, we want a KCC */
184 task_server_set_title(task
, "task[kccsrv]");
186 service
= talloc_zero(task
, struct kccsrv_service
);
188 task_server_terminate(task
, "kccsrv_task_init: out of memory", true);
191 service
->task
= task
;
192 service
->startup_time
= timeval_current();
193 task
->private_data
= service
;
195 status
= kccsrv_init_creds(service
);
196 if (!W_ERROR_IS_OK(status
)) {
197 task_server_terminate(task
,
198 talloc_asprintf(task
,
199 "kccsrv: Failed to obtain server credentials: %s\n",
200 win_errstr(status
)), true);
204 status
= kccsrv_connect_samdb(service
, task
->lp_ctx
);
205 if (!W_ERROR_IS_OK(status
)) {
206 task_server_terminate(task
, talloc_asprintf(task
,
207 "kccsrv: Failed to connect to local samdb: %s\n",
208 win_errstr(status
)), true);
212 status
= kccsrv_load_partitions(service
);
213 if (!W_ERROR_IS_OK(status
)) {
214 task_server_terminate(task
, talloc_asprintf(task
,
215 "kccsrv: Failed to load partitions: %s\n",
216 win_errstr(status
)), true);
220 periodic_startup_interval
= lpcfg_parm_int(task
->lp_ctx
, NULL
, "kccsrv",
221 "periodic_startup_interval", 15); /* in seconds */
222 service
->periodic
.interval
= lpcfg_parm_int(task
->lp_ctx
, NULL
, "kccsrv",
223 "periodic_interval", 300); /* in seconds */
225 status
= kccsrv_periodic_schedule(service
, periodic_startup_interval
);
226 if (!W_ERROR_IS_OK(status
)) {
227 task_server_terminate(task
, talloc_asprintf(task
,
228 "kccsrv: Failed to periodic schedule: %s\n",
229 win_errstr(status
)), true);
233 irpc_add_name(task
->msg_ctx
, "kccsrv");
235 IRPC_REGISTER(task
->msg_ctx
, drsuapi
, DRSUAPI_DSEXECUTEKCC
, kccsrv_execute_kcc
, service
);
236 IRPC_REGISTER(task
->msg_ctx
, drsuapi
, DRSUAPI_DSREPLICAGETINFO
, kccsrv_replica_get_info
, service
);
240 register ourselves as a available server
242 NTSTATUS
server_service_kcc_init(void)
244 return register_server_service("kcc", kccsrv_task_init
);