summaryrefslogtreecommitdiff
path: root/source4/dsdb/kcc/kcc_connection.c
blob: 762972b993420d3c6a90b9b34f45b168d7ea41c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
   Unix SMB/CIFS implementation.
   KCC service periodic handling

   Copyright (C) Crístian Deives

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "includes.h"
#include "lib/events/events.h"
#include "dsdb/samdb/samdb.h"
#include "auth/auth.h"
#include "smbd/service.h"
#include "lib/messaging/irpc.h"
#include "dsdb/kcc/kcc_service.h"
#include "lib/ldb/include/ldb_errors.h"
#include "../lib/util/dlinklist.h"
#include "librpc/gen_ndr/ndr_misc.h"
#include "librpc/gen_ndr/ndr_drsuapi.h"
#include "librpc/gen_ndr/ndr_drsblobs.h"
#include "param/param.h"

void kccsrv_apply_connections(struct ldb_dn **connections)
{
}

void kccsrv_create_connection(struct kccsrv_service *s, struct repsFromTo1 *r1)
{
	struct ldb_message *msg;
	TALLOC_CTX *tmp_ctx;
	struct ldb_dn *new_dn, *server_dn;
	struct GUID guid;
	const struct GUID *invocation_id;
	struct ldb_val schedule_val;
	int ret;
	bool ok;

	tmp_ctx = talloc_new(s);
	new_dn = samdb_ntds_settings_dn(s->samdb);
	if (!new_dn) {
		DEBUG(0, ("failed to find NTDS settings\n"));
		goto done;
	}
	invocation_id = samdb_ntds_invocation_id(s->samdb);
	guid = GUID_random();
	ok = ldb_dn_add_child_fmt(new_dn, "CN=%s", GUID_string(tmp_ctx, &guid));
	if (!ok) {
		DEBUG(0, ("failed to create nTDSConnection DN\n"));
		goto done;
	}
	ret = dsdb_find_dn_by_guid(s->samdb, tmp_ctx, GUID_string(tmp_ctx,
				   &r1->source_dsa_obj_guid), &server_dn);
	if (ret != LDB_SUCCESS) {
		DEBUG(0, ("failed to find fromServer DN '%s'\n",
			  GUID_string(tmp_ctx, &r1->source_dsa_obj_guid)));
		goto done;
	}
	schedule_val = data_blob_const(r1->schedule, sizeof(r1->schedule));

	msg = ldb_msg_new(tmp_ctx);
	msg->dn = new_dn;
	ldb_msg_add_string(msg, "invocationID",
			   GUID_string(tmp_ctx, invocation_id));
	ldb_msg_add_string(msg, "objectClass", "nTDSConnection");
	ldb_msg_add_string(msg, "showInAdvancedViewOnly", "TRUE");
	ldb_msg_add_string(msg, "enabledConnection", "TRUE");
	/* ldb_msg_add_dn(msg, "fromServer", server_dn); */
	ldb_msg_add_string(msg, "fromServer", ldb_dn_get_linearized(server_dn));
	ldb_msg_add_value(msg, "schedule", &schedule_val, NULL);
	ldb_msg_add_string(msg, "options", "1");

	ret = ldb_add(s->samdb, msg);
	if (ret == LDB_SUCCESS) {
		DEBUG(2, ("added nTDSConnection object '%s'\n",
			  ldb_dn_get_linearized(new_dn)));
	} else {
		DEBUG(0, ("failed to add an nTDSConnection object: %s\n",
			  ldb_strerror(ret)));
	}

done:
	talloc_free(tmp_ctx);
}

struct ldb_dn **kccsrv_find_connections(struct kccsrv_service *s,
					TALLOC_CTX *mem_ctx)
{
	struct ldb_result *res;
	int ret, i;
	const char *attrs[] = { "distinguishedName", NULL };
	struct ldb_dn **connections;

	ret = ldb_search(s->samdb, mem_ctx, &res, s->config_dn,
			 LDB_SCOPE_ONELEVEL, attrs, "objectClass=nTDSDSA");
	if (ret != LDB_SUCCESS) {
		DEBUG(0, ("failed nTDSDSA search: %s\n", ldb_strerror(ret)));
		return NULL;
	}

	for (i = 0; i < res->count; i++) {
		connections = talloc_realloc(mem_ctx, connections,
					     struct ldb_dn *, i + 1);
		connections[i] = samdb_result_dn(s->samdb, mem_ctx,
						 res->msgs[i],
						 "distinguishedName", NULL);
	}
	connections = talloc_realloc(mem_ctx, connections, struct ldb_dn *,
				     i + 1);
	connections[i] = NULL;
	return connections;
}