2 Unix SMB/CIFS implementation.
3 KCC service periodic handling
5 Copyright (C) CrÃstian Deives
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/events/events.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "auth/auth.h"
26 #include "smbd/service.h"
27 #include "lib/messaging/irpc.h"
28 #include "dsdb/kcc/kcc_service.h"
29 #include "dsdb/kcc/kcc_connection.h"
30 #include <ldb_errors.h>
31 #include "../lib/util/dlinklist.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "librpc/gen_ndr/ndr_drsuapi.h"
34 #include "librpc/gen_ndr/ndr_drsblobs.h"
35 #include "param/param.h"
37 static int kccsrv_add_connection(struct kccsrv_service
*s
,
38 struct kcc_connection
*conn
)
40 struct ldb_message
*msg
;
42 struct ldb_dn
*new_dn
, *server_dn
;
44 /* struct ldb_val schedule_val; */
48 tmp_ctx
= talloc_new(s
);
50 DEBUG(0, ("failed to talloc\n"));
51 ret
= LDB_ERR_OPERATIONS_ERROR
;
54 new_dn
= samdb_ntds_settings_dn(s
->samdb
, tmp_ctx
);
56 DEBUG(0, ("failed to find NTDS settings\n"));
57 ret
= LDB_ERR_OPERATIONS_ERROR
;
60 new_dn
= ldb_dn_copy(tmp_ctx
, new_dn
);
62 DEBUG(0, ("failed to copy NTDS settings\n"));
63 ret
= LDB_ERR_OPERATIONS_ERROR
;
67 ok
= ldb_dn_add_child_fmt(new_dn
, "CN=%s", GUID_string(tmp_ctx
, &guid
));
69 DEBUG(0, ("failed to create nTDSConnection DN\n"));
70 ret
= LDB_ERR_INVALID_DN_SYNTAX
;
73 ret
= dsdb_find_dn_by_guid(s
->samdb
, tmp_ctx
, &conn
->dsa_guid
, &server_dn
);
74 if (ret
!= LDB_SUCCESS
) {
75 DEBUG(0, ("failed to find fromServer DN '%s'\n",
76 GUID_string(tmp_ctx
, &conn
->dsa_guid
)));
79 /*schedule_val = data_blob_const(r1->schedule, sizeof(r1->schedule));*/
81 msg
= ldb_msg_new(tmp_ctx
);
83 ldb_msg_add_string(msg
, "objectClass", "nTDSConnection");
84 ldb_msg_add_string(msg
, "showInAdvancedViewOnly", "TRUE");
85 ldb_msg_add_string(msg
, "enabledConnection", "TRUE");
86 ldb_msg_add_linearized_dn(msg
, "fromServer", server_dn
);
87 /* ldb_msg_add_value(msg, "schedule", &schedule_val, NULL); */
89 samdb_msg_add_uint(s
->samdb
, msg
, msg
,
90 "options", NTDSCONN_OPT_IS_GENERATED
);
92 ret
= ldb_add(s
->samdb
, msg
);
93 if (ret
== LDB_SUCCESS
) {
94 DEBUG(2, ("added nTDSConnection object '%s'\n",
95 ldb_dn_get_linearized(new_dn
)));
97 DEBUG(0, ("failed to add an nTDSConnection object: %s\n",
102 talloc_free(tmp_ctx
);
106 static int kccsrv_delete_connection(struct kccsrv_service
*s
,
107 struct kcc_connection
*conn
)
113 tmp_ctx
= talloc_new(s
);
114 ret
= dsdb_find_dn_by_guid(s
->samdb
, tmp_ctx
, &conn
->obj_guid
, &dn
);
115 if (ret
!= LDB_SUCCESS
) {
116 DEBUG(0, ("failed to find nTDSConnection's DN: %s\n",
121 ret
= ldb_delete(s
->samdb
, dn
);
122 if (ret
== LDB_SUCCESS
) {
123 DEBUG(2, ("deleted nTDSConnection object '%s'\n",
124 ldb_dn_get_linearized(dn
)));
126 DEBUG(0, ("failed to delete an nTDSConnection object: %s\n",
131 talloc_free(tmp_ctx
);
135 void kccsrv_apply_connections(struct kccsrv_service
*s
,
136 struct kcc_connection_list
*ntds_list
,
137 struct kcc_connection_list
*dsa_list
)
139 unsigned int i
, j
, deleted
= 0, added
= 0;
144 * This routine is not respecting connections that the
145 * administrator can specifically create (NTDSCONN_OPT_IS_GENERATED
146 * bit will not be set)
148 for (i
= 0; ntds_list
&& i
< ntds_list
->count
; i
++) {
149 struct kcc_connection
*ntds
= &ntds_list
->servers
[i
];
150 for (j
= 0; j
< dsa_list
->count
; j
++) {
151 struct kcc_connection
*dsa
= &dsa_list
->servers
[j
];
152 if (GUID_equal(&ntds
->dsa_guid
, &dsa
->dsa_guid
)) {
156 if (j
== dsa_list
->count
) {
157 ret
= kccsrv_delete_connection(s
, ntds
);
158 if (ret
== LDB_SUCCESS
) {
163 DEBUG(4, ("%d connections have been deleted\n", deleted
));
165 for (i
= 0; i
< dsa_list
->count
; i
++) {
166 struct kcc_connection
*dsa
= &dsa_list
->servers
[i
];
167 for (j
= 0; ntds_list
&& j
< ntds_list
->count
; j
++) {
168 struct kcc_connection
*ntds
= &ntds_list
->servers
[j
];
169 if (GUID_equal(&dsa
->dsa_guid
, &ntds
->dsa_guid
)) {
173 if (ntds_list
== NULL
|| j
== ntds_list
->count
) {
174 ret
= kccsrv_add_connection(s
, dsa
);
175 if (ret
== LDB_SUCCESS
) {
180 DEBUG(4, ("%d connections have been added\n", added
));
183 struct kcc_connection_list
*kccsrv_find_connections(struct kccsrv_service
*s
,
188 struct ldb_dn
*base_dn
;
189 struct ldb_result
*res
;
190 const char *attrs
[] = { "objectGUID", "fromServer", NULL
};
191 struct kcc_connection_list
*list
;
195 tmp_ctx
= talloc_new(mem_ctx
);
197 DEBUG(0, ("failed to talloc\n"));
201 base_dn
= samdb_ntds_settings_dn(s
->samdb
, tmp_ctx
);
203 DEBUG(0, ("failed to find our own NTDS settings DN\n"));
204 talloc_free(tmp_ctx
);
208 ret
= ldb_search(s
->samdb
, tmp_ctx
, &res
, base_dn
, LDB_SCOPE_ONELEVEL
,
209 attrs
, "objectClass=nTDSConnection");
210 if (ret
!= LDB_SUCCESS
) {
211 DEBUG(0, ("failed nTDSConnection search: %s\n",
213 talloc_free(tmp_ctx
);
217 list
= talloc(tmp_ctx
, struct kcc_connection_list
);
219 DEBUG(0, ("out of memory"));
222 list
->servers
= talloc_array(list
, struct kcc_connection
,
224 if (!list
->servers
) {
225 DEBUG(0, ("out of memory"));
226 talloc_free(tmp_ctx
);
231 for (i
= 0; i
< res
->count
; i
++) {
232 struct ldb_dn
*server_dn
;
234 list
->servers
[i
].obj_guid
= samdb_result_guid(res
->msgs
[i
],
236 server_dn
= samdb_result_dn(s
->samdb
, mem_ctx
, res
->msgs
[i
],
238 ret
= dsdb_find_guid_by_dn(s
->samdb
, server_dn
,
239 &list
->servers
[i
].dsa_guid
);
240 if (ret
!= LDB_SUCCESS
) {
241 DEBUG(0, ("Failed to find connection server's GUID by "
243 ldb_dn_get_linearized(server_dn
),
249 DEBUG(4, ("found %d existing nTDSConnection objects\n", list
->count
));
250 talloc_steal(mem_ctx
, list
);
251 talloc_free(tmp_ctx
);