b088d2ed896cd94105999eec29a4ba5438a6f96d
[ira/wip.git] / source4 / dsdb / kcc / kcc_service.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    KCC service
5    
6    Copyright (C) Andrew Tridgell 2009
7    based on repl service code
8     
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.
13    
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.
18    
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/>.
21    
22 */
23
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"
37
38 /*
39   establish system creds
40  */
41 static WERROR kccsrv_init_creds(struct kccsrv_service *service)
42 {
43         service->system_session_info = system_session(service->task->lp_ctx);
44         if (!service->system_session_info) {
45                 return WERR_NOMEM;
46         }
47
48         return WERR_OK;
49 }
50
51 /*
52   connect to the local SAM
53  */
54 static WERROR kccsrv_connect_samdb(struct kccsrv_service *service, struct loadparm_context *lp_ctx)
55 {
56         const struct GUID *ntds_guid;
57
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;
61         }
62
63         ntds_guid = samdb_ntds_objectGUID(service->samdb);
64         if (!ntds_guid) {
65                 return WERR_DS_UNAVAILABLE;
66         }
67
68         service->ntds_guid = *ntds_guid;
69
70         return WERR_OK;
71 }
72
73
74 /*
75   load our local partition list
76  */
77 static WERROR kccsrv_load_partitions(struct kccsrv_service *s)
78 {
79         struct ldb_dn *basedn;
80         struct ldb_result *r;
81         struct ldb_message_element *el;
82         static const char *attrs[] = { "namingContexts", "configurationNamingContext", NULL };
83         uint32_t i;
84         int ret;
85
86         basedn = ldb_dn_new(s, s->samdb, NULL);
87         W_ERROR_HAVE_NO_MEMORY(basedn);
88
89         ret = ldb_search(s->samdb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
90                          "(objectClass=*)");
91         talloc_free(basedn);
92         if (ret != LDB_SUCCESS) {
93                 return WERR_FOOBAR;
94         } else if (r->count != 1) {
95                 talloc_free(r);
96                 return WERR_FOOBAR;
97         }
98
99         el = ldb_msg_find_element(r->msgs[0], "namingContexts");
100         if (!el) {
101                 return WERR_FOOBAR;
102         }
103
104         for (i=0; el && i < el->num_values; i++) {
105                 const char *v = (const char *)el->values[i].data;
106                 struct ldb_dn *pdn;
107                 struct kccsrv_partition *p;
108
109                 pdn = ldb_dn_new(s, s->samdb, v);
110                 if (!ldb_dn_validate(pdn)) {
111                         return WERR_FOOBAR;
112                 }
113
114                 p = talloc_zero(s, struct kccsrv_partition);
115                 W_ERROR_HAVE_NO_MEMORY(p);
116
117                 p->dn = talloc_steal(p, pdn);
118                 p->service = s;
119
120                 DLIST_ADD(s->partitions, p);
121
122                 DEBUG(2, ("kccsrv_partition[%s] loaded\n", v));
123         }
124
125         el = ldb_msg_find_element(r->msgs[0], "configurationNamingContext");
126         if (!el) {
127                 return WERR_FOOBAR;
128         }
129         s->config_dn = ldb_dn_new(s, s->samdb, (const char *)el->values[0].data);
130         if (!ldb_dn_validate(s->config_dn)) {
131                 return WERR_FOOBAR;
132         }
133
134         talloc_free(r);
135
136         return WERR_OK;
137 }
138
139
140 /*
141   startup the kcc service task
142 */
143 static void kccsrv_task_init(struct task_server *task)
144 {
145         WERROR status;
146         struct kccsrv_service *service;
147         uint32_t periodic_startup_interval;
148
149         switch (lp_server_role(task->lp_ctx)) {
150         case ROLE_STANDALONE:
151                 task_server_terminate(task, "kccsrv: no KCC required in standalone configuration", false);
152                 return;
153         case ROLE_DOMAIN_MEMBER:
154                 task_server_terminate(task, "kccsrv: no KCC required in domain member configuration", false);
155                 return;
156         case ROLE_DOMAIN_CONTROLLER:
157                 /* Yes, we want a KCC */
158                 break;
159         }
160
161         task_server_set_title(task, "task[kccsrv]");
162
163         service = talloc_zero(task, struct kccsrv_service);
164         if (!service) {
165                 task_server_terminate(task, "kccsrv_task_init: out of memory", true);
166                 return;
167         }
168         service->task           = task;
169         service->startup_time   = timeval_current();
170         task->private_data      = service;
171
172         status = kccsrv_init_creds(service);
173         if (!W_ERROR_IS_OK(status)) {
174                 task_server_terminate(task, 
175                                       talloc_asprintf(task,
176                                                       "kccsrv: Failed to obtain server credentials: %s\n",
177                                                       win_errstr(status)), true);
178                 return;
179         }
180
181         status = kccsrv_connect_samdb(service, task->lp_ctx);
182         if (!W_ERROR_IS_OK(status)) {
183                 task_server_terminate(task, talloc_asprintf(task,
184                                       "kccsrv: Failed to connect to local samdb: %s\n",
185                                                             win_errstr(status)), true);
186                 return;
187         }
188
189         status = kccsrv_load_partitions(service);
190         if (!W_ERROR_IS_OK(status)) {
191                 task_server_terminate(task, talloc_asprintf(task,
192                                       "kccsrv: Failed to load partitions: %s\n",
193                                                             win_errstr(status)), true);
194                 return;
195         }
196
197         periodic_startup_interval       = lp_parm_int(task->lp_ctx, NULL, "kccsrv", 
198                                                       "periodic_startup_interval", 15); /* in seconds */
199         service->periodic.interval      = lp_parm_int(task->lp_ctx, NULL, "kccsrv", 
200                                                       "periodic_interval", 300); /* in seconds */
201
202         status = kccsrv_periodic_schedule(service, periodic_startup_interval);
203         if (!W_ERROR_IS_OK(status)) {
204                 task_server_terminate(task, talloc_asprintf(task,
205                                       "kccsrv: Failed to periodic schedule: %s\n",
206                                                             win_errstr(status)), true);
207                 return;
208         }
209
210         irpc_add_name(task->msg_ctx, "kccsrv");
211 }
212
213 /*
214   register ourselves as a available server
215 */
216 NTSTATUS server_service_kcc_init(void)
217 {
218         return register_server_service("kcc", kccsrv_task_init);
219 }