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