1 /*
2 CTDB protocol marshalling
3
4 Copyright (C) Amitay Isaacs 2015
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include "system/network.h"
22
23 #include <talloc.h>
24
25 #include "common/line.h"
26
27 #include "protocol.h"
28 #include "protocol_util.h"
29 #include "lib/util/util.h"
30
31 static struct {
32 enum ctdb_runstate runstate;
33 const char * label;
34 } runstate_map[] = {
35 { CTDB_RUNSTATE_UNKNOWN, "UNKNOWN" },
36 { CTDB_RUNSTATE_INIT, "INIT" },
37 { CTDB_RUNSTATE_SETUP, "SETUP" },
38 { CTDB_RUNSTATE_FIRST_RECOVERY, "FIRST_RECOVERY" },
39 { CTDB_RUNSTATE_STARTUP, "STARTUP" },
40 { CTDB_RUNSTATE_RUNNING, "RUNNING" },
41 { CTDB_RUNSTATE_SHUTDOWN, "SHUTDOWN" },
42 { -1, NULL },
43 };
44
ctdb_runstate_to_string(enum ctdb_runstate runstate)45 const char *ctdb_runstate_to_string(enum ctdb_runstate runstate)
46 {
47 int i;
48
49 for (i=0; runstate_map[i].label != NULL; i++) {
50 if (runstate_map[i].runstate == runstate) {
51 return runstate_map[i].label;
52 }
53 }
54
55 return runstate_map[0].label;
56 }
57
ctdb_runstate_from_string(const char * runstate_str)58 enum ctdb_runstate ctdb_runstate_from_string(const char *runstate_str)
59 {
60 int i;
61
62 for (i=0; runstate_map[i].label != NULL; i++) {
63 if (strcasecmp(runstate_map[i].label,
64 runstate_str) == 0) {
65 return runstate_map[i].runstate;
66 }
67 }
68
69 return CTDB_RUNSTATE_UNKNOWN;
70 }
71
72 static struct {
73 enum ctdb_event event;
74 const char *label;
75 } event_map[] = {
76 { CTDB_EVENT_INIT, "init" },
77 { CTDB_EVENT_SETUP, "setup" },
78 { CTDB_EVENT_STARTUP, "startup" },
79 { CTDB_EVENT_START_RECOVERY, "startrecovery" },
80 { CTDB_EVENT_RECOVERED, "recovered" },
81 { CTDB_EVENT_TAKE_IP, "takeip" },
82 { CTDB_EVENT_RELEASE_IP, "releaseip" },
83 { CTDB_EVENT_MONITOR, "monitor" },
84 { CTDB_EVENT_SHUTDOWN, "shutdown" },
85 { CTDB_EVENT_UPDATE_IP, "updateip" },
86 { CTDB_EVENT_IPREALLOCATED, "ipreallocated" },
87 { CTDB_EVENT_MAX, "all" },
88 { -1, NULL },
89 };
90
ctdb_event_to_string(enum ctdb_event event)91 const char *ctdb_event_to_string(enum ctdb_event event)
92 {
93 int i;
94
95 for (i=0; event_map[i].label != NULL; i++) {
96 if (event_map[i].event == event) {
97 return event_map[i].label;
98 }
99 }
100
101 return "unknown";
102 }
103
ctdb_event_from_string(const char * event_str)104 enum ctdb_event ctdb_event_from_string(const char *event_str)
105 {
106 int i;
107
108 for (i=0; event_map[i].label != NULL; i++) {
109 if (strcmp(event_map[i].label, event_str) == 0) {
110 return event_map[i].event;
111 }
112 }
113
114 return CTDB_EVENT_MAX;
115 }
116
ctdb_sock_addr_to_buf(char * buf,socklen_t buflen,ctdb_sock_addr * addr,bool with_port)117 int ctdb_sock_addr_to_buf(char *buf, socklen_t buflen,
118 ctdb_sock_addr *addr, bool with_port)
119 {
120 const char *t;
121
122 switch (addr->sa.sa_family) {
123 case AF_INET:
124 t = inet_ntop(addr->ip.sin_family, &addr->ip.sin_addr,
125 buf, buflen);
126 if (t == NULL) {
127 return errno;
128 }
129 break;
130
131 case AF_INET6:
132 t = inet_ntop(addr->ip6.sin6_family, &addr->ip6.sin6_addr,
133 buf, buflen);
134 if (t == NULL) {
135 return errno;
136 }
137 break;
138
139 default:
140 return EAFNOSUPPORT;
141 break;
142 }
143
144 if (with_port) {
145 size_t len = strlen(buf);
146 int ret;
147
148 ret = snprintf(buf+len, buflen-len,
149 ":%u", ctdb_sock_addr_port(addr));
150 if (ret < 0 || (size_t)ret >= buflen-len) {
151 return ENOSPC;
152 }
153 }
154
155 return 0;
156 }
157
ctdb_sock_addr_to_string(TALLOC_CTX * mem_ctx,ctdb_sock_addr * addr,bool with_port)158 char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx,
159 ctdb_sock_addr *addr,
160 bool with_port)
161 {
162 size_t len = 64;
163 char *cip;
164 int ret;
165
166 cip = talloc_size(mem_ctx, len);
167
168 if (cip == NULL) {
169 return NULL;
170 }
171
172 ret = ctdb_sock_addr_to_buf(cip, len, addr, with_port);
173 if (ret != 0) {
174 talloc_free(cip);
175 return NULL;
176 }
177
178 return cip;
179 }
180
ipv4_from_string(const char * str,struct sockaddr_in * ip)181 static int ipv4_from_string(const char *str, struct sockaddr_in *ip)
182 {
183 int ret;
184
185 *ip = (struct sockaddr_in) {
186 .sin_family = AF_INET,
187 };
188
189 ret = inet_pton(AF_INET, str, &ip->sin_addr);
190 if (ret != 1) {
191 return EINVAL;
192 }
193
194 #ifdef HAVE_SOCK_SIN_LEN
195 ip->sin_len = sizeof(*ip);
196 #endif
197 return 0;
198 }
199
ipv6_from_string(const char * str,struct sockaddr_in6 * ip6)200 static int ipv6_from_string(const char *str, struct sockaddr_in6 *ip6)
201 {
202 int ret;
203
204 *ip6 = (struct sockaddr_in6) {
205 .sin6_family = AF_INET6,
206 };
207
208 ret = inet_pton(AF_INET6, str, &ip6->sin6_addr);
209 if (ret != 1) {
210 return EINVAL;
211 }
212
213 #ifdef HAVE_SOCK_SIN6_LEN
214 ip6->sin6_len = sizeof(*ip6);
215 #endif
216 return 0;
217 }
218
ip_from_string(const char * str,ctdb_sock_addr * addr)219 static int ip_from_string(const char *str, ctdb_sock_addr *addr)
220 {
221 char *p;
222 int ret;
223
224 if (addr == NULL) {
225 return EINVAL;
226 }
227
228 ZERO_STRUCTP(addr); /* valgrind :-) */
229
230 /* IPv4 or IPv6 address?
231 *
232 * Use rindex() because we need the right-most ':' below for
233 * IPv4-mapped IPv6 addresses anyway...
234 */
235 p = rindex(str, ':');
236 if (p == NULL) {
237 ret = ipv4_from_string(str, &addr->ip);
238 } else {
239 uint8_t ipv4_mapped_prefix[12] = {
240 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
241 };
242
243 ret = ipv6_from_string(str, &addr->ip6);
244 if (ret != 0) {
245 return ret;
246 }
247
248 /*
249 * Check for IPv4-mapped IPv6 address
250 * (e.g. ::ffff:192.0.2.128) - reparse as IPv4 if
251 * necessary
252 */
253 if (memcmp(&addr->ip6.sin6_addr.s6_addr[0],
254 ipv4_mapped_prefix,
255 sizeof(ipv4_mapped_prefix)) == 0) {
256 /* Initialize addr struct to zero before reparsing as IPV4 */
257 ZERO_STRUCTP(addr);
258
259 /* Reparse as IPv4 */
260 ret = ipv4_from_string(p+1, &addr->ip);
261 }
262 }
263
264 return ret;
265 }
266
ctdb_sock_addr_from_string(const char * str,ctdb_sock_addr * addr,bool with_port)267 int ctdb_sock_addr_from_string(const char *str,
268 ctdb_sock_addr *addr, bool with_port)
269 {
270 char *p;
271 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
272 unsigned port;
273 size_t len;
274 int ret;
275
276 if (! with_port) {
277 ret = ip_from_string(str, addr);
278 return ret;
279 }
280
281 /* Parse out port number and then IP address */
282
283 len = strlcpy(s, str, sizeof(s));
284 if (len >= sizeof(s)) {
285 return EINVAL;
286 }
287
288 p = rindex(s, ':');
289 if (p == NULL) {
290 return EINVAL;
291 }
292
293 port = smb_strtoul(p+1, NULL, 10, &ret, SMB_STR_FULL_STR_CONV);
294 if (ret != 0) {
295 /* Empty string or trailing garbage */
296 return EINVAL;
297 }
298
299 *p = '\0';
300 ret = ip_from_string(s, addr);
301
302 ctdb_sock_addr_set_port(addr, port);
303
304 return ret;
305 }
306
ctdb_sock_addr_mask_from_string(const char * str,ctdb_sock_addr * addr,unsigned int * mask)307 int ctdb_sock_addr_mask_from_string(const char *str,
308 ctdb_sock_addr *addr,
309 unsigned int *mask)
310 {
311 char *p;
312 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
313 unsigned int m;
314 size_t len;
315 int ret = 0;
316
317 if (addr == NULL || mask == NULL) {
318 return EINVAL;
319 }
320
321 len = strlcpy(s, str, sizeof(s));
322 if (len >= sizeof(s)) {
323 return EINVAL;
324 }
325
326 p = rindex(s, '/');
327 if (p == NULL) {
328 return EINVAL;
329 }
330
331 m = smb_strtoul(p+1, NULL, 10, &ret, SMB_STR_FULL_STR_CONV);
332 if (ret != 0) {
333 /* Empty string or trailing garbage */
334 return EINVAL;
335 }
336
337 *p = '\0';
338 ret = ip_from_string(s, addr);
339
340 if (ret == 0) {
341 *mask = m;
342 }
343
344 return ret;
345 }
346
ctdb_sock_addr_port(ctdb_sock_addr * addr)347 unsigned int ctdb_sock_addr_port(ctdb_sock_addr *addr)
348 {
349 switch (addr->sa.sa_family) {
350 case AF_INET:
351 return ntohs(addr->ip.sin_port);
352 break;
353 case AF_INET6:
354 return ntohs(addr->ip6.sin6_port);
355 break;
356 default:
357 return 0;
358 }
359 }
360
ctdb_sock_addr_set_port(ctdb_sock_addr * addr,unsigned int port)361 void ctdb_sock_addr_set_port(ctdb_sock_addr *addr, unsigned int port)
362 {
363 switch (addr->sa.sa_family) {
364 case AF_INET:
365 addr->ip.sin_port = htons(port);
366 break;
367 case AF_INET6:
368 addr->ip6.sin6_port = htons(port);
369 break;
370 default:
371 break;
372 }
373 }
374
ctdb_sock_addr_cmp_family(const ctdb_sock_addr * addr1,const ctdb_sock_addr * addr2)375 static int ctdb_sock_addr_cmp_family(const ctdb_sock_addr *addr1,
376 const ctdb_sock_addr *addr2)
377 {
378 /* This is somewhat arbitrary. However, when used for sorting
379 * it just needs to be consistent.
380 */
381 if (addr1->sa.sa_family < addr2->sa.sa_family) {
382 return -1;
383 }
384 if (addr1->sa.sa_family > addr2->sa.sa_family) {
385 return 1;
386 }
387
388 return 0;
389 }
390
ctdb_sock_addr_cmp_ip(const ctdb_sock_addr * addr1,const ctdb_sock_addr * addr2)391 int ctdb_sock_addr_cmp_ip(const ctdb_sock_addr *addr1,
392 const ctdb_sock_addr *addr2)
393 {
394 int ret;
395
396 ret = ctdb_sock_addr_cmp_family(addr1, addr2);
397 if (ret != 0) {
398 return ret;
399 }
400
401 switch (addr1->sa.sa_family) {
402 case AF_INET:
403 ret = memcmp(&addr1->ip.sin_addr.s_addr,
404 &addr2->ip.sin_addr.s_addr, 4);
405 break;
406
407 case AF_INET6:
408 ret = memcmp(addr1->ip6.sin6_addr.s6_addr,
409 addr2->ip6.sin6_addr.s6_addr, 16);
410 break;
411
412 default:
413 ret = -1;
414 }
415
416 return ret;
417 }
418
ctdb_sock_addr_cmp(const ctdb_sock_addr * addr1,const ctdb_sock_addr * addr2)419 int ctdb_sock_addr_cmp(const ctdb_sock_addr *addr1,
420 const ctdb_sock_addr *addr2)
421 {
422 int ret = 0;
423
424 ret = ctdb_sock_addr_cmp_ip(addr1, addr2);
425 if (ret != 0) {
426 return ret;
427 }
428
429 switch (addr1->sa.sa_family) {
430 case AF_INET:
431 if (addr1->ip.sin_port < addr2->ip.sin_port) {
432 ret = -1;
433 } else if (addr1->ip.sin_port > addr2->ip.sin_port) {
434 ret = 1;
435 }
436 break;
437
438 case AF_INET6:
439 if (addr1->ip6.sin6_port < addr2->ip6.sin6_port) {
440 ret = -1;
441 } else if (addr1->ip6.sin6_port > addr2->ip6.sin6_port) {
442 ret = 1;
443 }
444 break;
445
446 default:
447 ret = -1;
448 }
449
450 return ret;
451 }
452
ctdb_sock_addr_same_ip(const ctdb_sock_addr * addr1,const ctdb_sock_addr * addr2)453 bool ctdb_sock_addr_same_ip(const ctdb_sock_addr *addr1,
454 const ctdb_sock_addr *addr2)
455 {
456 return (ctdb_sock_addr_cmp_ip(addr1, addr2) == 0);
457 }
458
ctdb_sock_addr_same(const ctdb_sock_addr * addr1,const ctdb_sock_addr * addr2)459 bool ctdb_sock_addr_same(const ctdb_sock_addr *addr1,
460 const ctdb_sock_addr *addr2)
461 {
462 return (ctdb_sock_addr_cmp(addr1, addr2) == 0);
463 }
464
ctdb_connection_to_buf(char * buf,size_t buflen,struct ctdb_connection * conn,bool client_first)465 int ctdb_connection_to_buf(char *buf, size_t buflen,
466 struct ctdb_connection *conn, bool client_first)
467 {
468 char server[64], client[64];
469 int ret;
470
471 ret = ctdb_sock_addr_to_buf(server, sizeof(server),
472 &conn->server, true);
473 if (ret != 0) {
474 return ret;
475 }
476
477 ret = ctdb_sock_addr_to_buf(client, sizeof(client),
478 &conn->client, true);
479 if (ret != 0) {
480 return ret;
481 }
482
483 if (! client_first) {
484 ret = snprintf(buf, buflen, "%s %s", server, client);
485 } else {
486 ret = snprintf(buf, buflen, "%s %s", client, server);
487 }
488 if (ret < 0 || (size_t)ret >= buflen) {
489 return ENOSPC;
490 }
491
492 return 0;
493 }
494
ctdb_connection_to_string(TALLOC_CTX * mem_ctx,struct ctdb_connection * conn,bool client_first)495 char *ctdb_connection_to_string(TALLOC_CTX *mem_ctx,
496 struct ctdb_connection *conn,
497 bool client_first)
498 {
499 const size_t len = 128;
500 char *out;
501 int ret;
502
503 out = talloc_size(mem_ctx, len);
504 if (out == NULL) {
505 return NULL;
506 }
507
508 ret = ctdb_connection_to_buf(out, len, conn, client_first);
509 if (ret != 0) {
510 talloc_free(out);
511 return NULL;
512 }
513
514 return out;
515 }
516
ctdb_connection_from_string(const char * str,bool client_first,struct ctdb_connection * conn)517 int ctdb_connection_from_string(const char *str, bool client_first,
518 struct ctdb_connection *conn)
519 {
520 char s[128];
521 char *t1 = NULL, *t2 = NULL;
522 size_t len;
523 ctdb_sock_addr *first = (client_first ? &conn->client : &conn->server);
524 ctdb_sock_addr *second = (client_first ? &conn->server : &conn->client);
525 int ret;
526
527 len = strlcpy(s, str, sizeof(s));
528 if (len >= sizeof(s)) {
529 return EINVAL;
530 }
531
532 t1 = strtok(s, " \t\n");
533 if (t1 == NULL) {
534 return EINVAL;
535 }
536
537 t2 = strtok(NULL, " \t\n\0");
538 if (t2 == NULL) {
539 return EINVAL;
540 }
541
542 ret = ctdb_sock_addr_from_string(t1, first, true);
543 if (ret != 0) {
544 return ret;
545 }
546
547 ret = ctdb_sock_addr_from_string(t2, second, true);
548 if (ret != 0) {
549 return ret;
550 }
551
552 ret = ctdb_sock_addr_cmp_family(first, second);
553 if (ret != 0) {
554 return EINVAL;
555 }
556
557 return 0;
558 }
559
ctdb_connection_list_add(struct ctdb_connection_list * conn_list,struct ctdb_connection * conn)560 int ctdb_connection_list_add(struct ctdb_connection_list *conn_list,
561 struct ctdb_connection *conn)
562 {
563 uint32_t len;
564
565 if (conn_list == NULL) {
566 return EINVAL;
567 }
568
569 /* Ensure array is big enough */
570 len = talloc_array_length(conn_list->conn);
571 if (conn_list->num == len) {
572 conn_list->conn = talloc_realloc(conn_list, conn_list->conn,
573 struct ctdb_connection,
574 len+128);
575 if (conn_list->conn == NULL) {
576 return ENOMEM;
577 }
578 }
579
580 conn_list->conn[conn_list->num] = *conn;
581 conn_list->num++;
582
583 return 0;
584 }
585
connection_cmp(const void * a,const void * b)586 static int connection_cmp(const void *a, const void *b)
587 {
588 const struct ctdb_connection *conn_a = a;
589 const struct ctdb_connection *conn_b = b;
590 int ret;
591
592 ret = ctdb_sock_addr_cmp(&conn_a->server, &conn_b->server);
593 if (ret == 0) {
594 ret = ctdb_sock_addr_cmp(&conn_a->client, &conn_b->client);
595 }
596
597 return ret;
598 }
599
ctdb_connection_list_sort(struct ctdb_connection_list * conn_list)600 int ctdb_connection_list_sort(struct ctdb_connection_list *conn_list)
601 {
602 if (conn_list == NULL) {
603 return EINVAL;
604 }
605
606 if (conn_list->num > 0) {
607 qsort(conn_list->conn, conn_list->num,
608 sizeof(struct ctdb_connection), connection_cmp);
609 }
610
611 return 0;
612 }
613
ctdb_connection_list_to_string(TALLOC_CTX * mem_ctx,struct ctdb_connection_list * conn_list,bool client_first)614 char *ctdb_connection_list_to_string(
615 TALLOC_CTX *mem_ctx,
616 struct ctdb_connection_list *conn_list, bool client_first)
617 {
618 uint32_t i;
619 char *out;
620
621 out = talloc_strdup(mem_ctx, "");
622 if (out == NULL) {
623 return NULL;
624 }
625
626 if (conn_list == NULL || conn_list->num == 0) {
627 return out;
628 }
629
630 for (i = 0; i < conn_list->num; i++) {
631 char buf[128];
632 int ret;
633
634 ret = ctdb_connection_to_buf(buf, sizeof(buf),
635 &conn_list->conn[i], client_first);
636 if (ret != 0) {
637 talloc_free(out);
638 return NULL;
639 }
640
641 out = talloc_asprintf_append(out, "%s\n", buf);
642 if (out == NULL) {
643 return NULL;
644 }
645 }
646
647 return out;
648 }
649
650 struct ctdb_connection_list_read_state {
651 struct ctdb_connection_list *list;
652 bool client_first;
653 };
654
ctdb_connection_list_read_line(char * line,void * private_data)655 static int ctdb_connection_list_read_line(char *line, void *private_data)
656 {
657 struct ctdb_connection_list_read_state *state =
658 (struct ctdb_connection_list_read_state *)private_data;
659 struct ctdb_connection conn;
660 int ret;
661
662 /* Skip empty lines */
663 if (line[0] == '\0') {
664 return 0;
665 }
666
667 /* Comment */
668 if (line[0] == '#') {
669 return 0;
670 }
671
672 ret = ctdb_connection_from_string(line, state->client_first, &conn);
673 if (ret != 0) {
674 return ret;
675 }
676
677 ret = ctdb_connection_list_add(state->list, &conn);
678 if (ret != 0) {
679 return ret;
680 }
681
682 return 0;
683 }
684
ctdb_connection_list_read(TALLOC_CTX * mem_ctx,int fd,bool client_first,struct ctdb_connection_list ** conn_list)685 int ctdb_connection_list_read(TALLOC_CTX *mem_ctx,
686 int fd,
687 bool client_first,
688 struct ctdb_connection_list **conn_list)
689 {
690 struct ctdb_connection_list_read_state state;
691 int ret;
692
693 if (conn_list == NULL) {
694 return EINVAL;
695 }
696
697 state.list = talloc_zero(mem_ctx, struct ctdb_connection_list);
698 if (state.list == NULL) {
699 return ENOMEM;
700 }
701
702 state.client_first = client_first;
703
704 ret = line_read(fd,
705 128,
706 mem_ctx,
707 ctdb_connection_list_read_line,
708 &state,
709 NULL);
710
711 *conn_list = state.list;
712
713 return ret;
714 }
715