1    	/*
2    	 * Copyright 2004 International Business Machines
3    	 * Later changes copyright 2004-2026 the Pacemaker project contributors
4    	 *
5    	 * The version control history for this file may have further details.
6    	 *
7    	 * This source code is licensed under the GNU Lesser General Public License
8    	 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
9    	 */
10   	
11   	#include <crm_internal.h>
12   	
13   	#include <errno.h>
14   	#include <crm_internal.h>
15   	#include <unistd.h>
16   	#include <stdbool.h>
17   	#include <stdlib.h>
18   	#include <stdio.h>
19   	#include <stdarg.h>
20   	#include <string.h>
21   	
22   	#include <glib.h>
23   	
24   	#include <crm/crm.h>
25   	#include <crm/cib/internal.h>
26   	
27   	#include <crm/common/mainloop.h>
28   	#include <crm/common/xml.h>
29   	
30   	typedef struct {
31   	    char *token;
32   	    crm_ipc_t *ipc;
33   	    void (*dnotify_fn)(void *user_data);
34   	    mainloop_io_t *source;
35   	} cib_native_opaque_t;
36   	
37   	static bool
38   	ack_is_failure(const xmlNode *reply)
39   	{
40   	    int status = 0;
41   	
42   	    pcmk__xe_get_int(reply, PCMK_XA_STATUS, &status);
43   	    if (status != CRM_EX_OK) {
44   	        pcmk__err("Received error response from based: %s", crm_exit_str(status));
45   	        return true;
46   	    }
47   	
48   	    return false;
49   	}
50   	
51   	static int
52   	cib_native_perform_op_delegate(cib_t *cib, const char *op, const char *host,
53   	                               const char *section, xmlNode *data,
54   	                               xmlNode **output_data, int call_options,
55   	                               const char *user_name)
56   	{
57   	    int rc = pcmk_ok;
58   	    int reply_id = 0;
59   	    enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
60   	
61   	    xmlNode *op_msg = NULL;
62   	    xmlNode *op_reply = NULL;
63   	
64   	    cib_native_opaque_t *native = cib->variant_opaque;
65   	
66   	    if (cib->state == cib_disconnected) {
67   	        return -ENOTCONN;
68   	    }
69   	
70   	    if (output_data != NULL) {
71   	        *output_data = NULL;
72   	    }
73   	
74   	    if (op == NULL) {
75   	        pcmk__err("No operation specified");
76   	        return -EINVAL;
77   	    }
78   	
79   	    if (call_options & cib_sync_call) {
80   	        pcmk__set_ipc_flags(ipc_flags, "client", crm_ipc_client_response);
81   	    }
82   	
83   	    rc = cib__create_op(cib, op, host, section, data, call_options, user_name,
84   	                        NULL, &op_msg);
85   	    rc = pcmk_rc2legacy(rc);
86   	    if (rc != pcmk_ok) {
87   	        return rc;
88   	    }
89   	
90   	    if (pcmk__is_set(call_options, cib_transaction)) {
91   	        rc = cib__extend_transaction(cib, op_msg);
92   	        rc = pcmk_rc2legacy(rc);
93   	        goto done;
94   	    }
95   	
96   	    pcmk__trace("Sending %s message to the CIB manager (timeout=%ds)", op,
97   	                cib->call_timeout);
98   	    rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, cib->call_timeout * 1000, &op_reply);
99   	
100  	    if (rc < 0) {
101  	        pcmk__err("Couldn't perform %s operation (timeout=%ds): %s (%d)", op,
102  	                  cib->call_timeout, pcmk_strerror(rc), rc);
103  	        rc = -ECOMM;
104  	        goto done;
105  	    }
106  	
107  	    /* The only reason we can receive an ACK here is if dispatch_common ->
108  	     * pcmk__client_data2xml processed something that's not valid XML.
109  	     * dispatch_common does not return ACK, unlike other daemons.
110  	     */
111  	    if (pcmk__xe_is(op_reply, PCMK__XE_ACK) && ack_is_failure(op_reply)) {
112  	        rc = -EPROTO;
113  	        goto done;
114  	    }
115  	
116  	    pcmk__log_xml_trace(op_reply, "Reply");
117  	
118  	    if (!(call_options & cib_sync_call)) {
119  	        pcmk__trace("Async call, returning %d", cib->call_id);
120  	        CRM_CHECK(cib->call_id != 0,
121  	                  rc = -ENOMSG; goto done);
122  	        rc = cib->call_id;
123  	        goto done;
124  	    }
125  	
126  	    rc = pcmk_ok;
127  	    pcmk__xe_get_int(op_reply, PCMK__XA_CIB_CALLID, &reply_id);
128  	    if (reply_id == cib->call_id) {
129  	        xmlNode *wrapper = pcmk__xe_first_child(op_reply, PCMK__XE_CIB_CALLDATA,
130  	                                                NULL, NULL);
131  	        xmlNode *tmp = pcmk__xe_first_child(wrapper, NULL, NULL, NULL);
132  	
133  	        pcmk__trace("Synchronous reply %d received", reply_id);
134  	        if (pcmk__xe_get_int(op_reply, PCMK__XA_CIB_RC, &rc) != pcmk_rc_ok) {
135  	            rc = -EPROTO;
136  	        }
137  	
138  	        if (output_data == NULL || (call_options & cib_discard_reply)) {
139  	            pcmk__trace("Discarding reply");
140  	        } else {
141  	            *output_data = pcmk__xml_copy(NULL, tmp);
142  	        }
143  	
144  	    } else if (reply_id <= 0) {
145  	        pcmk__err("Received bad reply: No id set");
146  	        pcmk__log_xml_err(op_reply, "Bad reply");
147  	        rc = -ENOMSG;
148  	        goto done;
149  	
150  	    } else {
151  	        pcmk__err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
152  	        pcmk__log_xml_err(op_reply, "Old reply");
153  	        rc = -ENOMSG;
154  	        goto done;
155  	    }
156  	
157  	    if (op_reply == NULL && cib->state == cib_disconnected) {
158  	        rc = -ENOTCONN;
159  	
160  	    } else if (rc == pcmk_ok && op_reply == NULL) {
161  	        rc = -ETIME;
162  	    }
163  	
164  	    switch (rc) {
165  	        case pcmk_ok:
166  	        case -EPERM:
167  	            break;
168  	
169  	            /* These indicate internal problems */
170  	        case -EPROTO:
171  	        case -ENOMSG:
172  	            pcmk__err("Call failed: %s", pcmk_strerror(rc));
173  	            if (op_reply) {
174  	                pcmk__log_xml_err(op_reply, "Invalid reply");
175  	            }
176  	            break;
177  	
178  	        default:
179  	            if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
180  	                pcmk__warn("Call failed: %s", pcmk_strerror(rc));
181  	            }
182  	    }
183  	
184  	  done:
185  	    if (!crm_ipc_connected(native->ipc)) {
186  	        pcmk__err("The CIB manager disconnected");
187  	        cib->state = cib_disconnected;
188  	    }
189  	
190  	    pcmk__xml_free(op_msg);
191  	    pcmk__xml_free(op_reply);
192  	    return rc;
193  	}
194  	
195  	static int
196  	cib_native_dispatch_internal(const char *buffer, ssize_t length, void *userdata)
197  	{
198  	    const char *type = NULL;
199  	    xmlNode *msg = NULL;
200  	
201  	    cib_t *cib = userdata;
202  	
203  	    pcmk__trace("dispatching %p", userdata);
204  	
205  	    if (cib == NULL) {
206  	        pcmk__err("No CIB!");
207  	        return 0;
208  	    }
209  	
210  	    msg = pcmk__xml_parse(buffer);
211  	
212  	    if (msg == NULL) {
213  	        pcmk__warn("Received a NULL message from the CIB manager");
214  	        return 0;
215  	    }
216  	
217  	    /* do callbacks */
218  	    type = pcmk__xe_get(msg, PCMK__XA_T);
219  	    pcmk__trace("Activating %s callbacks...", type);
220  	    crm_log_xml_explicit(msg, "cib-reply");
221  	
222  	    if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
223  	        cib_native_callback(cib, msg, 0, 0);
224  	
225  	    } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
226  	        g_list_foreach(cib->notify_list, cib_native_notify, msg);
227  	
228  	    } else {
229  	        pcmk__err("Unknown message type: %s", type);
230  	    }
231  	
232  	    pcmk__xml_free(msg);
233  	    return 0;
234  	}
235  	
236  	static void
237  	cib_native_destroy(void *userdata)
238  	{
239  	    cib_t *cib = userdata;
240  	    cib_native_opaque_t *native = cib->variant_opaque;
241  	
242  	    pcmk__trace("destroying %p", userdata);
243  	    cib->state = cib_disconnected;
244  	    native->source = NULL;
245  	    native->ipc = NULL;
246  	
247  	    if (native->dnotify_fn) {
248  	        native->dnotify_fn(userdata);
249  	    }
250  	}
251  	
252  	static int
253  	cib_native_signoff(cib_t *cib)
254  	{
255  	    cib_native_opaque_t *native = cib->variant_opaque;
256  	
257  	    pcmk__debug("Disconnecting from the CIB manager");
258  	
259  	    cib_free_notify(cib);
260  	    remove_cib_op_callback(0, TRUE);
261  	
262  	    if (native->source != NULL) {
263  	        /* Attached to mainloop */
264  	        g_clear_pointer(&native->source, mainloop_del_ipc_client);
265  	        native->ipc = NULL;
266  	
267  	    } else if (native->ipc) {
268  	        /* Not attached to mainloop */
269  	        crm_ipc_t *ipc = native->ipc;
270  	
271  	        native->ipc = NULL;
272  	        crm_ipc_close(ipc);
273  	        crm_ipc_destroy(ipc);
274  	    }
275  	
276  	    cib->cmds->end_transaction(cib, false, cib_none);
277  	    cib->state = cib_disconnected;
278  	    cib->type = cib_no_connection;
279  	
280  	    return pcmk_ok;
281  	}
282  	
283  	static int
284  	cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
285  	{
286  	    int rc = pcmk_ok;
287  	    const char *channel = NULL;
288  	    cib_native_opaque_t *native = cib->variant_opaque;
289  	    xmlNode *hello = NULL;
290  	
291  	    struct ipc_client_callbacks cib_callbacks = {
292  	        .dispatch = cib_native_dispatch_internal,
293  	        .destroy = cib_native_destroy
294  	    };
295  	
296  	    if (name == NULL) {
297  	        name = pcmk__s(crm_system_name, "client");
298  	    }
299  	
300  	    cib->call_timeout = PCMK__IPC_TIMEOUT;
301  	
302  	    if (type == cib_command) {
303  	        cib->state = cib_connected_command;
304  	        channel = PCMK__SERVER_BASED_RW;
305  	
306  	    } else if (type == cib_command_nonblocking) {
307  	        cib->state = cib_connected_command;
308  	        channel = PCMK__SERVER_BASED_SHM;
309  	
310  	    } else if (type == cib_query) {
311  	        cib->state = cib_connected_query;
312  	        channel = PCMK__SERVER_BASED_RO;
313  	
314  	    } else {
315  	        return -ENOTCONN;
316  	    }
317  	
318  	    pcmk__trace("Connecting %s channel", channel);
319  	
320  	    native->source = mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 0, cib,
321  	                                             &cib_callbacks);
322  	    native->ipc = mainloop_get_ipc_client(native->source);
323  	
324  	    if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
325  	        pcmk__info("Could not connect to CIB manager for %s", name);
326  	        rc = -ENOTCONN;
327  	    }
328  	
329  	    if (rc == pcmk_ok) {
330  	        rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL,
331  	                            cib_sync_call, NULL, name, &hello);
332  	        rc = pcmk_rc2legacy(rc);
333  	    }
334  	
335  	    if (rc == pcmk_ok) {
336  	        xmlNode *reply = NULL;
337  	        const char *msg_type = NULL;
338  	
339  	        if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
340  	                         &reply) <= 0) {
341  	            rc = -ECOMM;
342  	            goto done;
343  	        }
344  	
345  	        /* The only reason we can receive an ACK here is if dispatch_common ->
346  	         * pcmk__client_data2xml processed something that's not valid XML.
347  	         * dispatch_common does not return ACK, unlike other daemons.
348  	         */
349  	        if (pcmk__xe_is(reply, PCMK__XE_ACK) && ack_is_failure(reply)) {
350  	            rc = -EPROTO;
351  	            pcmk__xml_free(reply);
352  	            goto done;
353  	        }
354  	
355  	        msg_type = pcmk__xe_get(reply, PCMK__XA_CIB_OP);
356  	
357  	        pcmk__log_xml_trace(reply, "reg-reply");
358  	
359  	        if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
360  	            pcmk__info("Reply to CIB registration message has unknown type "
361  	                       "'%s'",
362  	                       msg_type);
363  	            rc = -EPROTO;
364  	
365  	        } else {
366  	            native->token = pcmk__xe_get_copy(reply, PCMK__XA_CIB_CLIENTID);
367  	            if (native->token == NULL) {
368  	                rc = -EPROTO;
369  	            }
370  	        }
371  	
372  	        pcmk__xml_free(reply);
373  	    }
374  	
375  	done:
376  	    pcmk__xml_free(hello);
377  	
378  	    if (rc == pcmk_ok) {
379  	        pcmk__info("Successfully connected to CIB manager for %s", name);
380  	        return pcmk_ok;
381  	    }
382  	
383  	    pcmk__info("Connection to CIB manager for %s failed: %s", name,
384  	               pcmk_strerror(rc));
385  	    cib_native_signoff(cib);
386  	    return rc;
387  	}
388  	
389  	static int
390  	cib_native_free(cib_t *cib)
391  	{
392  	    int rc = pcmk_ok;
393  	
394  	    if (cib->state != cib_disconnected) {
395  	        rc = cib_native_signoff(cib);
396  	    }
397  	
398  	    if (cib->state == cib_disconnected) {
399  	        cib_native_opaque_t *native = cib->variant_opaque;
400  	
401  	        free(native->token);
402  	        free(cib->variant_opaque);
403  	        free(cib->cmds);
404  	        free(cib->user);
405  	        free(cib);
406  	    }
407  	
408  	    return rc;
409  	}
410  	
411  	static int
412  	cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
413  	{
414  	    int rc = pcmk_ok;
415  	    xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_CALLBACK);
416  	    cib_native_opaque_t *native = cib->variant_opaque;
417  	
418  	    if (cib->state != cib_disconnected) {
419  	        pcmk__xe_set(notify_msg, PCMK__XA_CIB_OP, PCMK__VALUE_CIB_NOTIFY);
420  	        pcmk__xe_set(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
421  	        pcmk__xe_set_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
422  	
423  	        /* We don't care about the reply here, so there's no need to check
424  	         * if we got an ACK in response.
425  	         */
426  	        rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
427  	                          1000 * cib->call_timeout, NULL);
428  	        if (rc <= 0) {
429  	            pcmk__trace("Notification not registered: %d", rc);
430  	            rc = -ECOMM;
431  	        }
432  	    }
433  	
434  	    pcmk__xml_free(notify_msg);
435  	    return rc;
436  	}
437  	
438  	static int
439  	cib_native_set_connection_dnotify(cib_t *cib, void (*dnotify)(void *user_data))
440  	{
441  	    cib_native_opaque_t *native = NULL;
442  	
443  	    if (cib == NULL) {
444  	        pcmk__err("No CIB!");
445  	        return FALSE;
446  	    }
447  	
448  	    native = cib->variant_opaque;
449  	    native->dnotify_fn = dnotify;
450  	
451  	    return pcmk_ok;
452  	}
453  	
454  	/*!
455  	 * \internal
456  	 * \brief Get the given CIB connection's unique client identifier
457  	 *
458  	 * These can be used to check whether this client requested the action that
459  	 * triggered a CIB notification.
460  	 *
461  	 * \param[in]  cib       CIB connection
462  	 * \param[out] async_id  If not \p NULL, where to store asynchronous client ID
463  	 * \param[out] sync_id   If not \p NULL, where to store synchronous client ID
464  	 *
465  	 * \return Legacy Pacemaker return code (specifically, \p pcmk_ok)
466  	 *
467  	 * \note This is the \p cib_native variant implementation of
468  	 *       \p cib_api_operations_t:client_id().
469  	 * \note For \p cib_native objects, \p async_id and \p sync_id are the same.
470  	 * \note The client ID is assigned during CIB sign-on.
471  	 */
472  	static int
473  	cib_native_client_id(const cib_t *cib, const char **async_id,
474  	                     const char **sync_id)
475  	{
476  	    cib_native_opaque_t *native = cib->variant_opaque;
477  	
478  	    if (async_id != NULL) {
479  	        *async_id = native->token;
480  	    }
481  	    if (sync_id != NULL) {
482  	        *sync_id = native->token;
483  	    }
484  	    return pcmk_ok;
485  	}
486  	
487  	cib_t *
488  	cib_native_new(void)
489  	{
490  	    cib_native_opaque_t *native = NULL;
(1) Event alloc_arg: "cib_new_variant" allocates memory that is stored into "cib_new_variant()->cmds". [details]
(2) Event var_assign: Assigning: "cib->cmds" = "cib_new_variant()->cmds".
Also see events: [leaked_storage]
491  	    cib_t *cib = cib_new_variant();
492  	
(3) Event path: Condition "cib == NULL", taking false branch.
493  	    if (cib == NULL) {
494  	        return NULL;
495  	    }
496  	
497  	    native = calloc(1, sizeof(cib_native_opaque_t));
498  	
(4) Event path: Condition "native == NULL", taking true branch.
499  	    if (native == NULL) {
CID (unavailable; MK=5145b60158a6c3f90f7309d42b6744d8) (#1 of 1): Resource leak (RESOURCE_LEAK):
(5) Event leaked_storage: Freeing "cib" without freeing its pointer field "cmds" leaks the storage that "cmds" points to.
Also see events: [alloc_arg][var_assign]
500  	        free(cib);
501  	        return NULL;
502  	    }
503  	
504  	    cib->variant = cib_native;
505  	    cib->variant_opaque = native;
506  	
507  	    native->ipc = NULL;
508  	    native->source = NULL;
509  	    native->dnotify_fn = NULL;
510  	
511  	    /* assign variant specific ops */
512  	    cib->delegate_fn = cib_native_perform_op_delegate;
513  	    cib->cmds->signon = cib_native_signon;
514  	    cib->cmds->signoff = cib_native_signoff;
515  	    cib->cmds->free = cib_native_free;
516  	
517  	    cib->cmds->register_notification = cib_native_register_notification;
518  	    cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
519  	
520  	    cib->cmds->client_id = cib_native_client_id;
521  	
522  	    return cib;
523  	}
524