s3:smbd_smb2_reauth_generic_return: make use of smb2req->xconn
[obnox/samba/samba-obnox.git] / ctdb / client / client_util.c
1 /*
2    CTDB client code
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 #include "system/filesys.h"
23
24 #include <talloc.h>
25 #include <tevent.h>
26 #include <tdb.h>
27
28 #include "common/logging.h"
29
30 #include "lib/util/debug.h"
31
32 #include "protocol/protocol.h"
33 #include "protocol/protocol_api.h"
34 #include "client/client_private.h"
35 #include "client/client.h"
36
37 int list_of_nodes(struct ctdb_node_map *nodemap,
38                   uint32_t flags_mask, uint32_t exclude_pnn,
39                   TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
40 {
41         int num_nodes = 0;
42         uint32_t *list;
43         int i;
44
45         /* Allocate the list of same number of nodes */
46         list = talloc_array(mem_ctx, uint32_t, nodemap->num);
47         if (list == NULL) {
48                 return -1;
49         }
50
51         for (i=0; i<nodemap->num; i++) {
52                 if (nodemap->node[i].flags & flags_mask) {
53                         continue;
54                 }
55                 if (nodemap->node[i].pnn == exclude_pnn) {
56                         continue;
57                 }
58                 list[num_nodes] = nodemap->node[i].pnn;
59                 num_nodes++;
60         }
61
62         *pnn_list = list;
63         return num_nodes;
64 }
65
66 int list_of_active_nodes(struct ctdb_node_map *nodemap, uint32_t exclude_pnn,
67                          TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
68 {
69         return list_of_nodes(nodemap, NODE_FLAGS_INACTIVE, exclude_pnn,
70                              mem_ctx, pnn_list);
71 }
72
73 int list_of_connected_nodes(struct ctdb_node_map *nodemap,
74                             uint32_t exclude_pnn,
75                             TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
76 {
77         return list_of_nodes(nodemap, NODE_FLAGS_DISCONNECTED, exclude_pnn,
78                              mem_ctx, pnn_list);
79 }
80
81 int ctdb_ctrl_modflags(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
82                        struct ctdb_client_context *client,
83                        uint32_t destnode, struct timeval timeout,
84                        uint32_t set, uint32_t clear)
85 {
86         struct ctdb_node_map *nodemap;
87         struct ctdb_node_flag_change flag_change;
88         struct ctdb_req_control request;
89         uint32_t *pnn_list;
90         int ret, count;
91
92         ret = ctdb_ctrl_get_nodemap(mem_ctx, ev, client, destnode,
93                                     tevent_timeval_zero(), &nodemap);
94         if (ret != 0) {
95                 return ret;
96         }
97
98         flag_change.pnn = destnode;
99         flag_change.old_flags = nodemap->node[destnode].flags;
100         flag_change.new_flags = flag_change.old_flags | set;
101         flag_change.new_flags &= ~clear;
102
103         count = list_of_connected_nodes(nodemap, -1, mem_ctx, &pnn_list);
104         if (count == -1) {
105                 return ENOMEM;
106         }
107
108         ctdb_req_control_modify_flags(&request, &flag_change);
109         ret = ctdb_client_control_multi(mem_ctx, ev, client, pnn_list, count,
110                                         tevent_timeval_zero(), &request,
111                                         NULL, NULL);
112         return ret;
113 }
114
115 bool ctdb_server_id_equal(struct ctdb_server_id *sid1,
116                           struct ctdb_server_id *sid2)
117 {
118         if (sid1->pid != sid2->pid) {
119                 return false;
120         }
121         if (sid1->task_id != sid2->task_id) {
122                 return false;
123         }
124         if (sid1->vnn != sid2->vnn) {
125                 return false;
126         }
127         if (sid1->unique_id != sid2->unique_id) {
128                 return false;
129         }
130
131         return true;
132 }
133
134 int ctdb_server_id_exists(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
135                           struct ctdb_client_context *client,
136                           struct ctdb_server_id *sid, bool *exists)
137 {
138         uint8_t *result;
139         int ret;
140
141         ret = ctdb_ctrl_check_srvids(mem_ctx, ev, client, sid->vnn,
142                                      tevent_timeval_zero(),
143                                      &sid->unique_id, 1, &result);
144         if (ret != 0) {
145                 return ret;
146         }
147
148         if (result[0] == 1) {
149                 *exists = true;
150         } else {
151                 *exists = false;
152         }
153
154         return 0;
155 }