winbindd: fix LSA connections via DCERPC_AUTH_SCHANNEL
[Samba.git] / ctdb / client / client_connect.c
blob89a602d4030b43e74f3fe35234719cc79659541b
1 /*
2 CTDB client code
4 Copyright (C) Amitay Isaacs 2015
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
21 #include "system/network.h"
22 #include "system/filesys.h"
24 #include <talloc.h>
25 #include <tevent.h>
26 #include <tdb.h>
28 #include "common/reqid.h"
29 #include "common/srvid.h"
30 #include "common/comm.h"
31 #include "common/logging.h"
33 #include "lib/util/tevent_unix.h"
34 #include "lib/util/debug.h"
36 #include "protocol/protocol.h"
37 #include "protocol/protocol_api.h"
39 #include "client/client_private.h"
40 #include "client/client.h"
41 #include "client/client_sync.h"
43 static void client_read_handler(uint8_t *buf, size_t buflen,
44 void *private_data);
45 static void client_dead_handler(void *private_data);
47 struct ctdb_client_init_state {
48 struct ctdb_client_context *client;
51 static int ctdb_client_context_destructor(struct ctdb_client_context *client);
52 static void ctdb_client_init_done(struct tevent_req *subreq);
54 struct tevent_req *ctdb_client_init_send(TALLOC_CTX *mem_ctx,
55 struct tevent_context *ev,
56 const char *sockpath)
58 struct tevent_req *req, *subreq;
59 struct ctdb_client_init_state *state;
60 struct ctdb_client_context *client;
61 struct ctdb_req_control request;
62 struct sockaddr_un addr;
63 size_t len;
64 int ret;
66 req = tevent_req_create(mem_ctx, &state,
67 struct ctdb_client_init_state);
68 if (req == NULL) {
69 return NULL;
72 if (sockpath == NULL) {
73 D_ERR("socket path cannot be NULL\n");
74 tevent_req_error(req, EINVAL);
75 return tevent_req_post(req, ev);
78 client = talloc_zero(state, struct ctdb_client_context);
79 if (tevent_req_nomem(client, req)) {
80 return tevent_req_post(req, ev);
83 ret = reqid_init(client, INT_MAX-200, &client->idr);
84 if (ret != 0) {
85 D_ERR("reqid_init() failed, ret=%d\n", ret);
86 talloc_free(client);
87 tevent_req_error(req, ret);
88 return tevent_req_post(req, ev);
91 ret = srvid_init(client, &client->srv);
92 if (ret != 0) {
93 DEBUG(DEBUG_ERR, ("srvid_init() failed, ret=%d\n", ret));
94 talloc_free(client);
95 tevent_req_error(req, ret);
96 return tevent_req_post(req, ev);
99 ret = srvid_init(client, &client->tunnels);
100 if (ret != 0) {
101 DEBUG(DEBUG_ERR, ("srvid_init() failed, ret=%d\n", ret));
102 talloc_free(client);
103 tevent_req_error(req, ret);
104 return tevent_req_post(req, ev);
107 memset(&addr, 0, sizeof(addr));
108 addr.sun_family = AF_UNIX;
109 len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
110 if (len != strlen(sockpath)) {
111 D_ERR("socket path too long, len=%zu\n", strlen(sockpath));
112 talloc_free(client);
113 tevent_req_error(req, ENAMETOOLONG);
114 return tevent_req_post(req, ev);
117 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
118 if (client->fd == -1) {
119 ret = errno;
120 D_ERR("socket() failed, errno=%d\n", ret);
121 talloc_free(client);
122 tevent_req_error(req, ret);
123 return tevent_req_post(req, ev);
126 ret = connect(client->fd, (struct sockaddr *)&addr, sizeof(addr));
127 if (ret == -1) {
128 ret = errno;
129 DEBUG(DEBUG_ERR, ("connect() failed, errno=%d\n", ret));
130 close(client->fd);
131 talloc_free(client);
132 tevent_req_error(req, ret);
133 return tevent_req_post(req, ev);
136 ret = comm_setup(client, ev, client->fd, client_read_handler, client,
137 client_dead_handler, client, &client->comm);
138 if (ret != 0) {
139 DEBUG(DEBUG_ERR, ("comm_setup() failed, ret=%d\n", ret));
140 close(client->fd);
141 talloc_free(client);
142 tevent_req_error(req, ret);
143 return tevent_req_post(req, ev);
146 client->pnn = CTDB_UNKNOWN_PNN;
148 talloc_set_destructor(client, ctdb_client_context_destructor);
150 state->client = client;
152 ctdb_req_control_get_pnn(&request);
153 subreq = ctdb_client_control_send(state, ev, client,
154 CTDB_CURRENT_NODE,
155 tevent_timeval_zero(),
156 &request);
157 if (tevent_req_nomem(subreq, req)) {
158 TALLOC_FREE(state->client);
159 return tevent_req_post(req, ev);
161 tevent_req_set_callback(subreq, ctdb_client_init_done, req);
163 return req;
166 static int ctdb_client_context_destructor(struct ctdb_client_context *client)
168 if (client->fd != -1) {
169 close(client->fd);
170 client->fd = -1;
172 return 0;
175 static void ctdb_client_init_done(struct tevent_req *subreq)
177 struct tevent_req *req = tevent_req_callback_data(
178 subreq, struct tevent_req);
179 struct ctdb_client_init_state *state = tevent_req_data(
180 req, struct ctdb_client_init_state);
181 struct ctdb_reply_control *reply;
182 int ret;
183 bool status;
185 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
186 TALLOC_FREE(subreq);
187 if (! status) {
188 tevent_req_error(req, ret);
189 return;
192 ret = ctdb_reply_control_get_pnn(reply, &state->client->pnn);
193 if (ret != 0) {
194 tevent_req_error(req, ret);
195 return;
198 tevent_req_done(req);
201 bool ctdb_client_init_recv(struct tevent_req *req, int *perr,
202 TALLOC_CTX *mem_ctx,
203 struct ctdb_client_context **result)
205 struct ctdb_client_init_state *state = tevent_req_data(
206 req, struct ctdb_client_init_state);
207 int ret;
209 if (tevent_req_is_unix_error(req, &ret)) {
210 if (perr != NULL) {
211 *perr = ret;
213 return false;
216 *result = talloc_steal(mem_ctx, state->client);
217 return true;
221 int ctdb_client_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
222 const char *sockpath, struct ctdb_client_context **out)
224 struct tevent_req *req;
225 int ret;
226 bool status;
228 req = ctdb_client_init_send(mem_ctx, ev, sockpath);
229 if (req == NULL) {
230 return ENOMEM;
233 tevent_req_poll(req, ev);
235 status = ctdb_client_init_recv(req, &ret, mem_ctx, out);
236 TALLOC_FREE(req);
237 if (! status) {
238 return ret;
241 return 0;
244 static void client_read_handler(uint8_t *buf, size_t buflen,
245 void *private_data)
247 struct ctdb_client_context *client = talloc_get_type_abort(
248 private_data, struct ctdb_client_context);
249 struct ctdb_req_header hdr;
250 size_t np;
251 int ret;
253 ret = ctdb_req_header_pull(buf, buflen, &hdr, &np);
254 if (ret != 0) {
255 DEBUG(DEBUG_WARNING, ("invalid header, ret=%d\n", ret));
256 return;
259 if (buflen != hdr.length) {
260 DEBUG(DEBUG_WARNING, ("packet size mismatch %zu != %d\n",
261 buflen, hdr.length));
262 return;
265 ret = ctdb_req_header_verify(&hdr, 0);
266 if (ret != 0) {
267 DEBUG(DEBUG_WARNING, ("invalid header, ret=%d\n", ret));
268 return;
271 switch (hdr.operation) {
272 case CTDB_REPLY_CALL:
273 ctdb_client_reply_call(client, buf, buflen, hdr.reqid);
274 break;
276 case CTDB_REQ_MESSAGE:
277 ctdb_client_req_message(client, buf, buflen, hdr.reqid);
278 break;
280 case CTDB_REPLY_CONTROL:
281 ctdb_client_reply_control(client, buf, buflen, hdr.reqid);
282 break;
284 case CTDB_REQ_TUNNEL:
285 ctdb_client_req_tunnel(client, buf, buflen, hdr.reqid);
286 break;
288 default:
289 break;
293 static void client_dead_handler(void *private_data)
295 struct ctdb_client_context *client = talloc_get_type_abort(
296 private_data, struct ctdb_client_context);
297 ctdb_client_callback_func_t callback = client->callback;
298 void *callback_data = client->private_data;
300 talloc_free(client);
301 if (callback != NULL) {
302 callback(callback_data);
303 return;
306 DEBUG(DEBUG_NOTICE, ("connection to daemon closed, exiting\n"));
307 exit(1);
310 void ctdb_client_set_disconnect_callback(struct ctdb_client_context *client,
311 ctdb_client_callback_func_t callback,
312 void *private_data)
314 client->callback = callback;
315 client->private_data = private_data;
318 uint32_t ctdb_client_pnn(struct ctdb_client_context *client)
320 return client->pnn;
323 void ctdb_client_wait(struct tevent_context *ev, bool *done)
325 while (! (*done)) {
326 tevent_loop_once(ev);
330 static void ctdb_client_wait_timeout_handler(struct tevent_context *ev,
331 struct tevent_timer *te,
332 struct timeval t,
333 void *private_data)
335 bool *timed_out = (bool *)private_data;
337 *timed_out = true;
340 int ctdb_client_wait_timeout(struct tevent_context *ev, bool *done,
341 struct timeval timeout)
343 TALLOC_CTX *mem_ctx;
344 struct tevent_timer *timer;
345 bool timed_out = false;
347 mem_ctx = talloc_new(ev);
348 if (mem_ctx == NULL) {
349 return ENOMEM;
352 timer = tevent_add_timer(ev, mem_ctx, timeout,
353 ctdb_client_wait_timeout_handler,
354 &timed_out);
355 if (timer == NULL) {
356 talloc_free(mem_ctx);
357 return ENOMEM;
360 while (! (*done) && ! timed_out) {
361 tevent_loop_once(ev);
364 talloc_free(mem_ctx);
366 if (timed_out) {
367 return ETIME;
370 return 0;
373 struct ctdb_recovery_wait_state {
374 struct tevent_context *ev;
375 struct ctdb_client_context *client;
378 static void ctdb_recovery_wait_recmode(struct tevent_req *subreq);
379 static void ctdb_recovery_wait_retry(struct tevent_req *subreq);
381 struct tevent_req *ctdb_recovery_wait_send(TALLOC_CTX *mem_ctx,
382 struct tevent_context *ev,
383 struct ctdb_client_context *client)
385 struct tevent_req *req, *subreq;
386 struct ctdb_recovery_wait_state *state;
387 struct ctdb_req_control request;
389 req = tevent_req_create(mem_ctx, &state,
390 struct ctdb_recovery_wait_state);
391 if (req == NULL) {
392 return NULL;
395 state->ev = ev;
396 state->client = client;
398 ctdb_req_control_get_recmode(&request);
399 subreq = ctdb_client_control_send(state, ev, client, client->pnn,
400 tevent_timeval_zero(), &request);
401 if (tevent_req_nomem(subreq, req)) {
402 return tevent_req_post(req, ev);
404 tevent_req_set_callback(subreq, ctdb_recovery_wait_recmode, req);
406 return req;
409 static void ctdb_recovery_wait_recmode(struct tevent_req *subreq)
411 struct tevent_req *req = tevent_req_callback_data(
412 subreq, struct tevent_req);
413 struct ctdb_recovery_wait_state *state = tevent_req_data(
414 req, struct ctdb_recovery_wait_state);
415 struct ctdb_reply_control *reply;
416 int recmode;
417 int ret;
418 bool status;
420 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
421 TALLOC_FREE(subreq);
422 if (! status) {
423 tevent_req_error(req, ret);
424 return;
427 ret = ctdb_reply_control_get_recmode(reply, &recmode);
428 if (ret != 0) {
429 tevent_req_error(req, ret);
430 return;
433 if (recmode == CTDB_RECOVERY_NORMAL) {
434 tevent_req_done(req);
435 return;
438 subreq = tevent_wakeup_send(state, state->ev,
439 tevent_timeval_current_ofs(1, 0));
440 if (tevent_req_nomem(subreq, req)) {
441 return;
443 tevent_req_set_callback(subreq, ctdb_recovery_wait_retry, req);
446 static void ctdb_recovery_wait_retry(struct tevent_req *subreq)
448 struct tevent_req *req = tevent_req_callback_data(
449 subreq, struct tevent_req);
450 struct ctdb_recovery_wait_state *state = tevent_req_data(
451 req, struct ctdb_recovery_wait_state);
452 struct ctdb_req_control request;
453 bool status;
455 status = tevent_wakeup_recv(subreq);
456 TALLOC_FREE(subreq);
457 if (! status) {
458 tevent_req_error(req, ENOMEM);
459 return;
462 ctdb_req_control_get_recmode(&request);
463 subreq = ctdb_client_control_send(state, state->ev, state->client,
464 state->client->pnn,
465 tevent_timeval_zero(), &request);
466 if (tevent_req_nomem(subreq, req)) {
467 return;
469 tevent_req_set_callback(subreq, ctdb_recovery_wait_recmode, req);
472 bool ctdb_recovery_wait_recv(struct tevent_req *req, int *perr)
474 int err;
476 if (tevent_req_is_unix_error(req, &err)) {
477 if (perr != NULL) {
478 *perr = err;
480 return false;
483 return true;
486 bool ctdb_recovery_wait(struct tevent_context *ev,
487 struct ctdb_client_context *client)
489 TALLOC_CTX *mem_ctx;
490 struct tevent_req *req;
491 bool status;
493 mem_ctx = talloc_new(client);
494 if (mem_ctx == NULL) {
495 return false;
498 req = ctdb_recovery_wait_send(mem_ctx, ev, client);
499 if (req == NULL) {
500 return false;
503 tevent_req_poll(req, ev);
505 status = ctdb_recovery_wait_recv(req, NULL);
507 talloc_free(mem_ctx);
508 return status;