s4-dsdb: Improve memory handling in kccsrv_find_connections() by adding a tmp_ctx
[mat/samba.git] / source4 / dsdb / kcc / kcc_connection.c
1 /*
2    Unix SMB/CIFS implementation.
3    KCC service periodic handling
4
5    Copyright (C) Crístian Deives
6
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.
11
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.
16
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/>.
19
20 */
21
22 #include "includes.h"
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"
36
37 static int kccsrv_add_connection(struct kccsrv_service *s,
38                                  struct kcc_connection *conn)
39 {
40         struct ldb_message *msg;
41         TALLOC_CTX *tmp_ctx;
42         struct ldb_dn *new_dn, *server_dn;
43         struct GUID guid;
44         /* struct ldb_val schedule_val; */
45         int ret;
46         bool ok;
47
48         tmp_ctx = talloc_new(s);
49         new_dn = samdb_ntds_settings_dn(s->samdb);
50         if (!new_dn) {
51                 DEBUG(0, ("failed to find NTDS settings\n"));
52                 ret = LDB_ERR_OPERATIONS_ERROR;
53                 goto done;
54         }
55         new_dn = ldb_dn_copy(tmp_ctx, new_dn);
56         if (!new_dn) {
57                 DEBUG(0, ("failed to copy NTDS settings\n"));
58                 ret = LDB_ERR_OPERATIONS_ERROR;
59                 goto done;
60         }
61         guid = GUID_random();
62         ok = ldb_dn_add_child_fmt(new_dn, "CN=%s", GUID_string(tmp_ctx, &guid));
63         if (!ok) {
64                 DEBUG(0, ("failed to create nTDSConnection DN\n"));
65                 ret = LDB_ERR_INVALID_DN_SYNTAX;
66                 goto done;
67         }
68         ret = dsdb_find_dn_by_guid(s->samdb, tmp_ctx, &conn->dsa_guid, &server_dn);
69         if (ret != LDB_SUCCESS) {
70                 DEBUG(0, ("failed to find fromServer DN '%s'\n",
71                           GUID_string(tmp_ctx, &conn->dsa_guid)));
72                 goto done;
73         }
74         /*schedule_val = data_blob_const(r1->schedule, sizeof(r1->schedule));*/
75
76         msg = ldb_msg_new(tmp_ctx);
77         msg->dn = new_dn;
78         ldb_msg_add_string(msg, "objectClass", "nTDSConnection");
79         ldb_msg_add_string(msg, "showInAdvancedViewOnly", "TRUE");
80         ldb_msg_add_string(msg, "enabledConnection", "TRUE");
81         ldb_msg_add_linearized_dn(msg, "fromServer", server_dn);
82         /* ldb_msg_add_value(msg, "schedule", &schedule_val, NULL); */
83
84         samdb_msg_add_uint(s->samdb, msg, msg,
85                                 "options", NTDSCONN_OPT_IS_GENERATED);
86
87         ret = ldb_add(s->samdb, msg);
88         if (ret == LDB_SUCCESS) {
89                 DEBUG(2, ("added nTDSConnection object '%s'\n",
90                           ldb_dn_get_linearized(new_dn)));
91         } else {
92                 DEBUG(0, ("failed to add an nTDSConnection object: %s\n",
93                           ldb_strerror(ret)));
94         }
95
96 done:
97         talloc_free(tmp_ctx);
98         return ret;
99 }
100
101 static int kccsrv_delete_connection(struct kccsrv_service *s,
102                                     struct kcc_connection *conn)
103 {
104         TALLOC_CTX *tmp_ctx;
105         struct ldb_dn *dn;
106         int ret;
107
108         tmp_ctx = talloc_new(s);
109         ret = dsdb_find_dn_by_guid(s->samdb, tmp_ctx, &conn->obj_guid, &dn);
110         if (ret != LDB_SUCCESS) {
111                 DEBUG(0, ("failed to find nTDSConnection's DN: %s\n",
112                           ldb_strerror(ret)));
113                 goto done;
114         }
115
116         ret = ldb_delete(s->samdb, dn);
117         if (ret == LDB_SUCCESS) {
118                 DEBUG(2, ("deleted nTDSConnection object '%s'\n",
119                           ldb_dn_get_linearized(dn)));
120         } else {
121                 DEBUG(0, ("failed to delete an nTDSConnection object: %s\n",
122                           ldb_strerror(ret)));
123         }
124
125 done:
126         talloc_free(tmp_ctx);
127         return ret;
128 }
129
130 void kccsrv_apply_connections(struct kccsrv_service *s,
131                               struct kcc_connection_list *ntds_list,
132                               struct kcc_connection_list *dsa_list)
133 {
134         unsigned int i, j, deleted = 0, added = 0;
135         int ret;
136
137         /* XXX
138          *
139          * This routine is not respecting connections that the
140          * administrator can specifically create (NTDSCONN_OPT_IS_GENERATED
141          * bit will not be set)
142          */
143         for (i = 0; ntds_list && i < ntds_list->count; i++) {
144                 struct kcc_connection *ntds = &ntds_list->servers[i];
145                 for (j = 0; j < dsa_list->count; j++) {
146                         struct kcc_connection *dsa = &dsa_list->servers[j];
147                         if (GUID_equal(&ntds->dsa_guid, &dsa->dsa_guid)) {
148                                 break;
149                         }
150                 }
151                 if (j == dsa_list->count) {
152                         ret = kccsrv_delete_connection(s, ntds);
153                         if (ret == LDB_SUCCESS) {
154                                 deleted++;
155                         }
156                 }
157         }
158         DEBUG(4, ("%d connections have been deleted\n", deleted));
159
160         for (i = 0; i < dsa_list->count; i++) {
161                 struct kcc_connection *dsa = &dsa_list->servers[i];
162                 for (j = 0; ntds_list && j < ntds_list->count; j++) {
163                         struct kcc_connection *ntds = &ntds_list->servers[j];
164                         if (GUID_equal(&dsa->dsa_guid, &ntds->dsa_guid)) {
165                                 break;
166                         }
167                 }
168                 if (ntds_list == NULL || j == ntds_list->count) {
169                         ret = kccsrv_add_connection(s, dsa);
170                         if (ret == LDB_SUCCESS) {
171                                 added++;
172                         }
173                 }
174         }
175         DEBUG(4, ("%d connections have been added\n", added));
176 }
177
178 struct kcc_connection_list *kccsrv_find_connections(struct kccsrv_service *s,
179                                                     TALLOC_CTX *mem_ctx)
180 {
181         unsigned int i;
182         int ret;
183         struct ldb_dn *base_dn;
184         struct ldb_result *res;
185         const char *attrs[] = { "objectGUID", "fromServer", NULL };
186         struct kcc_connection_list *list;
187         TALLOC_CTX *tmp_ctx;
188         kcctpl_test(s);
189
190         tmp_ctx = talloc_new(mem_ctx);
191         if (!tmp_ctx) {
192                 DEBUG(0, ("failed to talloc\n"));
193                 return NULL;
194         }
195
196         base_dn = samdb_ntds_settings_dn(s->samdb);
197         if (!base_dn) {
198                 DEBUG(0, ("failed to find our own NTDS settings DN\n"));
199                 talloc_free(tmp_ctx);
200                 return NULL;
201         }
202
203         ret = ldb_search(s->samdb, tmp_ctx, &res, base_dn, LDB_SCOPE_ONELEVEL,
204                          attrs, "objectClass=nTDSConnection");
205         if (ret != LDB_SUCCESS) {
206                 DEBUG(0, ("failed nTDSConnection search: %s\n",
207                           ldb_strerror(ret)));
208                 talloc_free(tmp_ctx);
209                 return NULL;
210         }
211
212         list = talloc(tmp_ctx, struct kcc_connection_list);
213         if (!list) {
214                 DEBUG(0, ("out of memory"));
215                 return NULL;
216         }
217         list->servers = talloc_array(list, struct kcc_connection,
218                                      res->count);
219         if (!list->servers) {
220                 DEBUG(0, ("out of memory"));
221                 talloc_free(tmp_ctx);
222                 return NULL;
223         }
224         list->count = 0;
225
226         for (i = 0; i < res->count; i++) {
227                 struct ldb_dn *server_dn;
228
229                 list->servers[i].obj_guid = samdb_result_guid(res->msgs[i],
230                                                               "objectGUID");
231                 server_dn = samdb_result_dn(s->samdb, mem_ctx, res->msgs[i],
232                                             "fromServer", NULL);
233                 ret = dsdb_find_guid_by_dn(s->samdb, server_dn,
234                                            &list->servers[i].dsa_guid);
235                 if (ret != LDB_SUCCESS) {
236                         DEBUG(0, ("Failed to find connection server's GUID by "
237                                   "DN=%s: %s\n",
238                                   ldb_dn_get_linearized(server_dn),
239                                   ldb_strerror(ret)));
240                         continue;
241                 }
242                 list->count++;
243         }
244         DEBUG(4, ("found %d existing nTDSConnection objects\n", list->count));
245         talloc_steal(mem_ctx, list);
246         talloc_free(tmp_ctx);
247         return list;
248 }