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 (pcmk__is_set(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 based_ipc_dispatch ->
108  	     * pcmk__client_data2xml processed something that's not valid XML.
109  	     * based_ipc_dispatch 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 (!pcmk__is_set(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 *tmp = cib__get_calldata(op_reply);
130  	
131  	        pcmk__trace("Synchronous reply %d received", reply_id);
132  	        if (pcmk__xe_get_int(op_reply, PCMK__XA_CIB_RC, &rc) != pcmk_rc_ok) {
133  	            rc = -EPROTO;
134  	        }
135  	
136  	        if ((output_data == NULL)
137  	            || pcmk__is_set(call_options, cib_discard_reply)) {
138  	
139  	            pcmk__trace("Discarding reply");
140  	
141  	        } else {
142  	            *output_data = pcmk__xml_copy(NULL, tmp);
143  	        }
144  	
145  	    } else if (reply_id <= 0) {
146  	        pcmk__err("Received bad reply: No id set");
147  	        pcmk__log_xml_err(op_reply, "Bad reply");
148  	        rc = -ENOMSG;
149  	        goto done;
150  	
151  	    } else {
152  	        pcmk__err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
153  	        pcmk__log_xml_err(op_reply, "Old reply");
154  	        rc = -ENOMSG;
155  	        goto done;
156  	    }
157  	
158  	    if (op_reply == NULL && cib->state == cib_disconnected) {
159  	        rc = -ENOTCONN;
160  	
161  	    } else if (rc == pcmk_ok && op_reply == NULL) {
162  	        rc = -ETIME;
163  	    }
164  	
165  	    switch (rc) {
166  	        case pcmk_ok:
167  	        case -EPERM:
168  	            break;
169  	
170  	            /* These indicate internal problems */
171  	        case -EPROTO:
172  	        case -ENOMSG:
173  	            pcmk__err("Call failed: %s", pcmk_strerror(rc));
174  	            if (op_reply) {
175  	                pcmk__log_xml_err(op_reply, "Invalid reply");
176  	            }
177  	            break;
178  	
179  	        default:
180  	            if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
181  	                pcmk__warn("Call failed: %s", pcmk_strerror(rc));
182  	            }
183  	    }
184  	
185  	  done:
186  	    if (!crm_ipc_connected(native->ipc)) {
187  	        pcmk__err("The CIB manager disconnected");
188  	        cib->state = cib_disconnected;
189  	    }
190  	
191  	    pcmk__xml_free(op_msg);
192  	    pcmk__xml_free(op_reply);
193  	    return rc;
194  	}
195  	
196  	static int
197  	cib_native_dispatch_internal(const char *buffer, ssize_t length, void *userdata)
198  	{
199  	    const char *type = NULL;
200  	    xmlNode *msg = NULL;
201  	
202  	    cib_t *cib = userdata;
203  	
204  	    pcmk__trace("dispatching %p", userdata);
205  	
206  	    if (cib == NULL) {
207  	        pcmk__err("No CIB!");
208  	        return 0;
209  	    }
210  	
211  	    msg = pcmk__xml_parse(buffer);
212  	
213  	    if (msg == NULL) {
214  	        pcmk__warn("Received a NULL message from the CIB manager");
215  	        return 0;
216  	    }
217  	
218  	    /* do callbacks */
219  	    type = pcmk__xe_get(msg, PCMK__XA_T);
220  	    pcmk__trace("Activating %s callbacks...", type);
221  	    crm_log_xml_explicit(msg, "cib-reply");
222  	
223  	    if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
224  	        cib_native_callback(cib, msg, 0, 0);
225  	
226  	    } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
227  	        g_list_foreach(cib->notify_list, cib_native_notify, msg);
228  	
229  	    } else {
230  	        pcmk__err("Unknown message type: %s", type);
231  	    }
232  	
233  	    pcmk__xml_free(msg);
234  	    return 0;
235  	}
236  	
237  	static void
238  	cib_native_destroy(void *userdata)
239  	{
240  	    cib_t *cib = userdata;
241  	    cib_native_opaque_t *native = cib->variant_opaque;
242  	
243  	    pcmk__trace("destroying %p", userdata);
244  	    cib->state = cib_disconnected;
245  	    native->source = NULL;
246  	    native->ipc = NULL;
247  	
248  	    if (native->dnotify_fn) {
249  	        native->dnotify_fn(userdata);
250  	    }
251  	}
252  	
253  	static int
254  	cib_native_signoff(cib_t *cib)
255  	{
256  	    cib_native_opaque_t *native = cib->variant_opaque;
257  	
258  	    pcmk__debug("Disconnecting from the CIB manager");
259  	
260  	    cib_free_notify(cib);
261  	    remove_cib_op_callback(0, TRUE);
262  	
263  	    if (native->source != NULL) {
264  	        /* Attached to mainloop */
265  	        g_clear_pointer(&native->source, mainloop_del_ipc_client);
266  	        native->ipc = NULL;
267  	
268  	    } else if (native->ipc) {
269  	        /* Not attached to mainloop */
270  	        crm_ipc_t *ipc = native->ipc;
271  	
272  	        native->ipc = NULL;
273  	        crm_ipc_close(ipc);
274  	        crm_ipc_destroy(ipc);
275  	    }
276  	
277  	    cib->cmds->end_transaction(cib, false, cib_none);
278  	    cib->state = cib_disconnected;
279  	
280  	    return pcmk_ok;
281  	}
282  	
283  	/*!
284  	 * \internal
285  	 * \brief Sign on a native client to the CIB API
286  	 *
287  	 * \param[in,out] cib   CIB connection (client)
288  	 * \param[in]     name  Ignored
289  	 * \param[in]     type  Ignored
290  	 */
291  	static int
292  	cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
293  	{
294  	    int rc = pcmk_ok;
295  	    cib_native_opaque_t *native = cib->variant_opaque;
296  	    xmlNode *hello = NULL;
297  	    xmlNode *reply = NULL;
298  	    const char *msg_type = NULL;
299  	
300  	    struct ipc_client_callbacks cib_callbacks = {
301  	        .dispatch = cib_native_dispatch_internal,
302  	        .destroy = cib_native_destroy,
303  	    };
304  	
305  	    name = pcmk__s(crm_system_name, "client");
306  	
307  	    cib->call_timeout = PCMK__IPC_TIMEOUT;
308  	
309  	    native->source = mainloop_add_ipc_client(PCMK__SERVER_BASED_RW,
310  	                                             G_PRIORITY_HIGH, 0, cib,
311  	                                             &cib_callbacks);
312  	    native->ipc = mainloop_get_ipc_client(native->source);
313  	
314  	    if ((native->ipc == NULL) || !crm_ipc_connected(native->ipc)) {
315  	        pcmk__info("Could not connect to CIB manager for %s", name);
316  	        rc = -ENOTCONN;
317  	        goto done;
318  	    }
319  	
320  	    rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL, cib_sync_call,
321  	                        NULL, name, &hello);
322  	    rc = pcmk_rc2legacy(rc);
323  	    if (rc != pcmk_ok) {
324  	        goto done;
325  	    }
326  	
327  	    if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
328  	                     &reply) <= 0) {
329  	        rc = -ECOMM;
330  	        goto done;
331  	    }
332  	
333  	    /* The only reason we can receive an ACK here is if based_ipc_dispatch ->
334  	     * pcmk__client_data2xml processed something that's not valid XML.
335  	     * based_ipc_dispatch does not return ACK, unlike other daemons.
336  	     */
337  	    if (pcmk__xe_is(reply, PCMK__XE_ACK) && ack_is_failure(reply)) {
338  	        rc = -EPROTO;
339  	        goto done;
340  	    }
341  	
342  	    pcmk__log_xml_trace(reply, "reg-reply");
343  	    msg_type = pcmk__xe_get(reply, PCMK__XA_CIB_OP);
344  	
345  	    if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_none)) {
346  	        pcmk__info("Reply to CIB registration message has unknown type '%s'",
347  	                   msg_type);
348  	        rc = -EPROTO;
349  	        goto done;
350  	    }
351  	
352  	    native->token = pcmk__xe_get_copy(reply, PCMK__XA_CIB_CLIENTID);
353  	    if (native->token == NULL) {
354  	        rc = -EPROTO;
355  	        goto done;
356  	    }
357  	
358  	    pcmk__info("Successfully connected to CIB manager for %s", name);
359  	    cib->state = cib_connected_command;
360  	
361  	done:
362  	    pcmk__xml_free(hello);
363  	    pcmk__xml_free(reply);
364  	
365  	    if (rc != pcmk_ok) {
366  	        pcmk__info("Connection to CIB manager for %s failed: %s", name,
367  	                   pcmk_strerror(rc));
368  	        cib_native_signoff(cib);
369  	    }
370  	
371  	    return rc;
372  	}
373  	
374  	static int
375  	cib_native_free(cib_t *cib)
376  	{
377  	    int rc = pcmk_ok;
378  	
379  	    if (cib->state != cib_disconnected) {
380  	        rc = cib_native_signoff(cib);
381  	    }
382  	
383  	    if (cib->state == cib_disconnected) {
384  	        cib_native_opaque_t *native = cib->variant_opaque;
385  	
386  	        free(native->token);
387  	        free(cib->variant_opaque);
388  	        free(cib->cmds);
389  	        free(cib->user);
390  	        free(cib);
391  	    }
392  	
393  	    return rc;
394  	}
395  	
396  	static int
397  	cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
398  	{
399  	    int rc = pcmk_ok;
400  	    xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_CALLBACK);
401  	    cib_native_opaque_t *native = cib->variant_opaque;
402  	
403  	    if (cib->state != cib_disconnected) {
404  	        pcmk__xe_set(notify_msg, PCMK__XA_CIB_OP, PCMK__VALUE_CIB_NOTIFY);
405  	        pcmk__xe_set(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
406  	        pcmk__xe_set_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
407  	
408  	        /* We don't care about the reply here, so there's no need to check
409  	         * if we got an ACK in response.
410  	         */
411  	        rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
412  	                          1000 * cib->call_timeout, NULL);
413  	        if (rc <= 0) {
414  	            pcmk__trace("Notification not registered: %d", rc);
415  	            rc = -ECOMM;
416  	        }
417  	    }
418  	
419  	    pcmk__xml_free(notify_msg);
420  	    return rc;
421  	}
422  	
423  	static int
424  	cib_native_set_connection_dnotify(cib_t *cib, void (*dnotify)(void *user_data))
425  	{
426  	    cib_native_opaque_t *native = NULL;
427  	
428  	    if (cib == NULL) {
429  	        pcmk__err("No CIB!");
430  	        return FALSE;
431  	    }
432  	
433  	    native = cib->variant_opaque;
434  	    native->dnotify_fn = dnotify;
435  	
436  	    return pcmk_ok;
437  	}
438  	
439  	/*!
440  	 * \internal
441  	 * \brief Get the given CIB connection's unique client identifier
442  	 *
443  	 * These can be used to check whether this client requested the action that
444  	 * triggered a CIB notification.
445  	 *
446  	 * \param[in]  cib       CIB connection
447  	 * \param[out] async_id  If not \p NULL, where to store asynchronous client ID
448  	 * \param[out] sync_id   If not \p NULL, where to store synchronous client ID
449  	 *
450  	 * \return Legacy Pacemaker return code (specifically, \p pcmk_ok)
451  	 *
452  	 * \note This is the \p cib_native variant implementation of
453  	 *       \p cib_api_operations_t:client_id().
454  	 * \note For \p cib_native objects, \p async_id and \p sync_id are the same.
455  	 * \note The client ID is assigned during CIB sign-on.
456  	 */
457  	static int
458  	cib_native_client_id(const cib_t *cib, const char **async_id,
459  	                     const char **sync_id)
460  	{
461  	    cib_native_opaque_t *native = cib->variant_opaque;
462  	
463  	    if (async_id != NULL) {
464  	        *async_id = native->token;
465  	    }
466  	    if (sync_id != NULL) {
467  	        *sync_id = native->token;
468  	    }
469  	    return pcmk_ok;
470  	}
471  	
472  	cib_t *
473  	cib_native_new(void)
474  	{
475  	    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]
476  	    cib_t *cib = cib_new_variant();
477  	
(3) Event path: Condition "cib == NULL", taking false branch.
478  	    if (cib == NULL) {
479  	        return NULL;
480  	    }
481  	
482  	    native = calloc(1, sizeof(cib_native_opaque_t));
483  	
(4) Event path: Condition "native == NULL", taking true branch.
484  	    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]
485  	        free(cib);
486  	        return NULL;
487  	    }
488  	
489  	    cib->variant = cib_native;
490  	    cib->variant_opaque = native;
491  	
492  	    native->ipc = NULL;
493  	    native->source = NULL;
494  	    native->dnotify_fn = NULL;
495  	
496  	    /* assign variant specific ops */
497  	    cib->delegate_fn = cib_native_perform_op_delegate;
498  	    cib->cmds->signon = cib_native_signon;
499  	    cib->cmds->signoff = cib_native_signoff;
500  	    cib->cmds->free = cib_native_free;
501  	
502  	    cib->cmds->register_notification = cib_native_register_notification;
503  	    cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
504  	
505  	    cib->cmds->client_id = cib_native_client_id;
506  	
507  	    return cib;
508  	}
509