1    	/*
2    	 * Copyright 2008-2026 the Pacemaker project contributors
3    	 *
4    	 * The version control history for this file may have further details.
5    	 *
6    	 * This source code is licensed under the GNU Lesser General Public License
7    	 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8    	 */
9    	
10   	#include <crm_internal.h>
11   	
12   	#include <errno.h>                  // ENOTCONN, EPROTO, EAGAIN
13   	#include <stdbool.h>                // false, bool, true
14   	#include <stdlib.h>                 // NULL, free, calloc
15   	#include <string.h>                 // strdup
16   	#include <sys/socket.h>             // shutdown, SHUT_RDWR
17   	#include <time.h>                   // time, time_t
18   	#include <unistd.h>                 // close
19   	
20   	#include <glib.h>                   // gpointer, gboolean, g_list_foreach
21   	#include <gnutls/gnutls.h>          // gnutls_deinit, gnutls_bye
22   	#include <libxml/tree.h>            // xmlNode
23   	#include <qb/qblog.h>               // QB_XS
24   	
25   	#include <crm/cib.h>                // cib_t, cib_remote_new
26   	#include <crm/cib/internal.h>       // cib__create_op, cib__extend_transaction
27   	#include <crm/common/internal.h>
28   	#include <crm/common/mainloop.h>    // mainloop_fd_callbacks
29   	#include <crm/common/results.h>     // pcmk_rc_str, pcmk_rc_*
30   	#include <crm/common/xml.h>         // PCMK_XA_*,
31   	#include <crm/crm.h>                // CRM_OP_REGISTER, crm_system_name
32   	
33   	// GnuTLS handshake timeout in seconds
34   	#define TLS_HANDSHAKE_TIMEOUT 5
35   	
36   	static pcmk__tls_t *tls = NULL;
37   	
38   	#include <arpa/inet.h>
39   	
40   	typedef struct {
41   	    int port;
42   	    char *server;
43   	    char *user;
44   	    char *passwd;
45   	    gboolean encrypted;
46   	    pcmk__remote_t command;
47   	    pcmk__remote_t callback;
48   	    pcmk__output_t *out;
49   	    time_t start_time;
50   	    int timeout_sec;
51   	} cib_remote_opaque_t;
52   	
53   	static bool
54   	ack_is_failure(const xmlNode *reply)
55   	{
56   	    int status = 0;
57   	
58   	    pcmk__xe_get_int(reply, PCMK_XA_STATUS, &status);
59   	    if (status != CRM_EX_OK) {
60   	        pcmk__err("Received error response from based: %s", crm_exit_str(status));
61   	        return true;
62   	    }
63   	
64   	    return false;
65   	}
66   	
67   	static int
68   	cib_remote_perform_op(cib_t *cib, const char *op, const char *host,
69   	                      const char *section, xmlNode *data,
70   	                      xmlNode **output_data, int call_options,
71   	                      const char *user_name)
72   	{
73   	    int rc;
74   	    int remaining_time = 0;
75   	    time_t start_time;
76   	
77   	    xmlNode *op_msg = NULL;
78   	    xmlNode *op_reply = NULL;
79   	
80   	    cib_remote_opaque_t *private = cib->variant_opaque;
81   	
82   	    if (cib->state == cib_disconnected) {
83   	        return -ENOTCONN;
84   	    }
85   	
86   	    if (output_data != NULL) {
87   	        *output_data = NULL;
88   	    }
89   	
90   	    if (op == NULL) {
91   	        pcmk__err("No operation specified");
92   	        return -EINVAL;
93   	    }
94   	
95   	    rc = cib__create_op(cib, op, host, section, data, call_options, user_name,
96   	                        NULL, &op_msg);
97   	    rc = pcmk_rc2legacy(rc);
98   	    if (rc != pcmk_ok) {
99   	        return rc;
100  	    }
101  	
102  	    if (pcmk__is_set(call_options, cib_transaction)) {
103  	        rc = cib__extend_transaction(cib, op_msg);
104  	        pcmk__xml_free(op_msg);
105  	        return pcmk_rc2legacy(rc);
106  	    }
107  	
108  	    pcmk__trace("Sending %s message to the CIB manager", op);
109  	    if (!(call_options & cib_sync_call)) {
110  	        pcmk__remote_send_xml(&private->callback, op_msg);
111  	    } else {
112  	        pcmk__remote_send_xml(&private->command, op_msg);
113  	    }
114  	    pcmk__xml_free(op_msg);
115  	
116  	    if ((call_options & cib_discard_reply)) {
117  	        pcmk__trace("Discarding reply");
118  	        return pcmk_ok;
119  	
120  	    } else if (!(call_options & cib_sync_call)) {
121  	        return cib->call_id;
122  	    }
123  	
124  	    pcmk__trace("Waiting for a synchronous reply");
125  	
126  	    start_time = time(NULL);
127  	    remaining_time = cib->call_timeout ? cib->call_timeout : 60;
128  	
129  	    rc = pcmk_rc_ok;
130  	    while (remaining_time > 0 && (rc != ENOTCONN)) {
131  	        int reply_id = -1;
132  	        int msg_id = cib->call_id;
133  	
134  	        rc = pcmk__read_remote_message(&private->command,
135  	                                       remaining_time * 1000);
136  	        op_reply = pcmk__remote_message_xml(&private->command);
137  	
138  	        if (!op_reply) {
139  	            break;
140  	        }
141  	
142  	        pcmk__xe_get_int(op_reply, PCMK__XA_CIB_CALLID, &reply_id);
143  	
144  	        if (reply_id == msg_id) {
145  	            break;
146  	
147  	        } else if (reply_id < msg_id) {
148  	            pcmk__debug("Received old reply: %d (wanted %d)", reply_id, msg_id);
149  	            pcmk__log_xml_trace(op_reply, "Old reply");
150  	
151  	        } else if ((reply_id - 10000) > msg_id) {
152  	            /* wrap-around case */
153  	            pcmk__debug("Received old reply: %d (wanted %d)", reply_id, msg_id);
154  	            pcmk__log_xml_trace(op_reply, "Old reply");
155  	        } else {
156  	            pcmk__err("Received a __future__ reply: %d (wanted %d)", reply_id,
157  	                      msg_id);
158  	        }
159  	
160  	        g_clear_pointer(&op_reply, pcmk__xml_free);
161  	
162  	        /* wasn't the right reply, try and read some more */
163  	        remaining_time = time(NULL) - start_time;
164  	    }
165  	
166  	    if (rc == ENOTCONN) {
167  	        pcmk__err("Disconnected while waiting for reply");
168  	        return -ENOTCONN;
169  	    }
170  	
171  	    if (op_reply == NULL) {
172  	        pcmk__err("No reply message - empty");
173  	        return -ENOMSG;
174  	    }
175  	
176  	    /* The only reason we can receive an ACK here is if dispatch_common ->
177  	     * pcmk__client_data2xml processed something that's not valid XML.
178  	     * dispatch_common does not return ACK, unlike other daemons.
179  	     */
180  	    if (pcmk__xe_is(op_reply, PCMK__XE_ACK) && ack_is_failure(op_reply)) {
181  	        pcmk__xml_free(op_reply);
182  	        return -EPROTO;
183  	    }
184  	
185  	    pcmk__trace("Synchronous reply received");
186  	
187  	    /* Start processing the reply... */
188  	    if (pcmk__xe_get_int(op_reply, PCMK__XA_CIB_RC, &rc) != pcmk_rc_ok) {
189  	        rc = -EPROTO;
190  	    }
191  	
192  	    if (rc == pcmk_ok || rc == -EPERM) {
193  	        pcmk__log_xml_debug(op_reply, "passed");
194  	
195  	    } else {
196  	        pcmk__err("Call failed: %s", pcmk_strerror(rc));
197  	        pcmk__log_xml_warn(op_reply, "failed");
198  	    }
199  	
200  	    if (output_data == NULL) {
201  	        /* do nothing more */
202  	
203  	    } else if (!(call_options & cib_discard_reply)) {
204  	        xmlNode *wrapper = pcmk__xe_first_child(op_reply, PCMK__XE_CIB_CALLDATA,
205  	                                                NULL, NULL);
206  	        xmlNode *tmp = pcmk__xe_first_child(wrapper, NULL, NULL, NULL);
207  	
208  	        if (tmp == NULL) {
209  	            pcmk__trace("No output in reply to \"%s\" command %d", op,
210  	                        (cib->call_id - 1));
211  	        } else {
212  	            *output_data = pcmk__xml_copy(NULL, tmp);
213  	        }
214  	    }
215  	
216  	    pcmk__xml_free(op_reply);
217  	
218  	    return rc;
219  	}
220  	
221  	static int
222  	cib_remote_callback_dispatch(gpointer user_data)
223  	{
224  	    int rc;
225  	    cib_t *cib = user_data;
226  	    cib_remote_opaque_t *private = cib->variant_opaque;
227  	
228  	    xmlNode *msg = NULL;
229  	    const char *type = NULL;
230  	
231  	    /* If start time is 0, we've previously handled a complete message and this
232  	     * connection is being reused for a new message.  Reset the start_time,
233  	     * giving this new message timeout_sec from now to complete.
234  	     */
235  	    if (private->start_time == 0) {
236  	        private->start_time = time(NULL);
237  	    }
238  	
239  	    rc = pcmk__read_available_remote_data(&private->callback);
240  	    switch (rc) {
241  	        case pcmk_rc_ok:
242  	            /* We have the whole message so process it */
243  	            break;
244  	
245  	        case EAGAIN:
246  	            /* Have we timed out? */
247  	            if (time(NULL) >= private->start_time + private->timeout_sec) {
248  	                pcmk__info("Error reading from CIB manager connection: %s",
249  	                           pcmk_rc_str(ETIME));
250  	                return -1;
251  	            }
252  	
253  	            /* We haven't read the whole message yet */
254  	            return 0;
255  	
256  	        default:
257  	            /* Error */
258  	            pcmk__info("Error reading from CIB manager connection: %s",
259  	                       pcmk_rc_str(rc));
260  	            return -1;
261  	    }
262  	
263  	    // coverity[tainted_data] This can't easily be changed right now
264  	    msg = pcmk__remote_message_xml(&private->callback);
265  	    if (msg == NULL) {
266  	        private->start_time = 0;
267  	        return 0;
268  	    }
269  	
270  	    type = pcmk__xe_get(msg, PCMK__XA_T);
271  	
272  	    pcmk__trace("Activating %s callbacks...", type);
273  	
274  	    if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
275  	        cib_native_callback(cib, msg, 0, 0);
276  	    } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
277  	        g_list_foreach(cib->notify_list, cib_native_notify, msg);
278  	    } else {
279  	        pcmk__err("Unknown message type: %s", type);
280  	    }
281  	
282  	    pcmk__xml_free(msg);
283  	    private->start_time = 0;
284  	    return 0;
285  	}
286  	
287  	static int
288  	cib_remote_command_dispatch(gpointer user_data)
289  	{
290  	    int rc;
291  	    cib_t *cib = user_data;
292  	    cib_remote_opaque_t *private = cib->variant_opaque;
293  	
294  	    /* See cib_remote_callback_dispatch */
295  	    if (private->start_time == 0) {
296  	        private->start_time = time(NULL);
297  	    }
298  	
299  	    rc = pcmk__read_available_remote_data(&private->command);
300  	    if (rc == EAGAIN) {
301  	        /* Have we timed out? */
302  	        if (time(NULL) >= private->start_time + private->timeout_sec) {
303  	            pcmk__info("Error reading from CIB manager connection: %s",
304  	                       pcmk_rc_str(ETIME));
305  	            return -1;
306  	        }
307  	
308  	        /* We haven't read the whole message yet */
309  	        return 0;
310  	    }
311  	
312  	    g_clear_pointer(&private->command.buffer, free);
313  	    pcmk__err("Received late reply for remote cib connection, discarding");
314  	
315  	    if (rc != pcmk_rc_ok) {
316  	        pcmk__info("Error reading from CIB manager connection: %s",
317  	                   pcmk_rc_str(rc));
318  	        return -1;
319  	    }
320  	
321  	    private->start_time = 0;
322  	    return 0;
323  	}
324  	
325  	static int
326  	cib_tls_close(cib_t *cib)
327  	{
328  	    cib_remote_opaque_t *private = cib->variant_opaque;
329  	
330  	    if (private->encrypted) {
331  	        if (private->command.tls_session) {
332  	            gnutls_bye(private->command.tls_session, GNUTLS_SHUT_RDWR);
333  	            gnutls_deinit(private->command.tls_session);
334  	        }
335  	
336  	        if (private->callback.tls_session) {
337  	            gnutls_bye(private->callback.tls_session, GNUTLS_SHUT_RDWR);
338  	            gnutls_deinit(private->callback.tls_session);
339  	        }
340  	
341  	        private->command.tls_session = NULL;
342  	        private->callback.tls_session = NULL;
343  	        g_clear_pointer(&tls, pcmk__free_tls);
344  	    }
345  	
346  	    if (private->command.tcp_socket >= 0) {
347  	        shutdown(private->command.tcp_socket, SHUT_RDWR);       /* no more receptions */
348  	        close(private->command.tcp_socket);
349  	    }
350  	    if (private->callback.tcp_socket >= 0) {
351  	        shutdown(private->callback.tcp_socket, SHUT_RDWR);      /* no more receptions */
352  	        close(private->callback.tcp_socket);
353  	    }
354  	    private->command.tcp_socket = -1;
355  	    private->callback.tcp_socket = -1;
356  	
357  	    g_clear_pointer(&private->command.buffer, free);
358  	    g_clear_pointer(&private->callback.buffer, free);
359  	
360  	    return 0;
361  	}
362  	
363  	static void
364  	cib_remote_connection_destroy(gpointer user_data)
365  	{
366  	    pcmk__err("Connection destroyed");
367  	    cib_tls_close(user_data);
368  	}
369  	
370  	static int
371  	cib_setup_tls(pcmk__remote_t *connection, const char *server, int port,
372  	              const char *user)
373  	{
374  	    int rc = pcmk_rc_ok;
375  	    int tls_rc = GNUTLS_E_SUCCESS;
376  	    bool have_psk = false;
377  	    const char *key_location = getenv("CIB_key_file");
378  	
379  	    /* X509 certificates take precedence over PSK in pcmk__init_tls,
380  	     * so don't perform any of the following (potentially noisy) checks
381  	     * if we don't care about their results.
382  	     */
383  	    if (!pcmk__x509_enabled()) {
384  	        bool file_exists = false;
385  	
386  	        if (key_location != NULL) {
387  	            have_psk = pcmk__cred_file_useable(key_location, &file_exists);
388  	        }
389  	
390  	        if (!have_psk && file_exists) {
391  	            /* The credential file exists but doesn't have the right owner
392  	             * or permissions.  Don't fall back to anonymous on config
393  	             * errors.
394  	             */
395  	            pcmk__err("Remote CIB session creation for %s:%d failed",
396  	                      server, port);
397  	            rc = EACCES;
398  	            goto done;
399  	        }
400  	
401  	        if (!have_psk) {
402  	            /* The credential file does not exist at all, so fall back
403  	             * to anonymous auth.
404  	             *
405  	             * @COMPAT Remove fallback to anonymous authentication
406  	             */
407  	            pcmk__warn("Falling back to anonymous authentication for remote "
408  	                       "CIB connections");
409  	        }
410  	    }
411  	
412  	    rc = pcmk__init_tls(&tls, false, have_psk);
413  	    if (rc != pcmk_rc_ok) {
414  	        goto done;
415  	    }
416  	
417  	    if (tls->cred_type == GNUTLS_CRD_PSK) {
418  	        gnutls_datum_t psk_key = { NULL, 0 };
419  	
420  	        rc = pcmk__load_key(key_location, &psk_key, false);
421  	
422  	        if (rc != pcmk_rc_ok) {
423  	            pcmk__warn("Could not read remote CIB key from %s: %s",
424  	                       key_location, pcmk_rc_str(rc));
425  	            goto done;
426  	        }
427  	
428  	        pcmk__tls_client_add_psk_key(tls, user, &psk_key, false);
429  	        gnutls_free(psk_key.data);
430  	    }
431  	
432  	    connection->tls_session = pcmk__new_tls_session(tls, connection->tcp_socket);
433  	    if (connection->tls_session == NULL) {
434  	        rc = ENOTCONN;
435  	        goto done;
436  	    }
437  	
438  	    rc = pcmk__tls_client_handshake(connection, TLS_HANDSHAKE_TIMEOUT, &tls_rc);
439  	    if (rc != pcmk_rc_ok) {
440  	        const char *msg = NULL;
441  	
442  	        if (rc == EPROTO) {
443  	            msg = gnutls_strerror(tls_rc);
444  	        } else {
445  	            msg = pcmk_rc_str(rc);
446  	        }
447  	
448  	        pcmk__err("Remote CIB session creation for %s:%d failed: %s",
449  	                  server, port, msg);
450  	        g_clear_pointer(&connection->tls_session, gnutls_deinit);
451  	    }
452  	
453  	done:
454  	    return rc;
455  	}
456  	
457  	static int
458  	cib_tls_signon(cib_t *cib, pcmk__remote_t *connection, gboolean event_channel)
459  	{
460  	    cib_remote_opaque_t *private = cib->variant_opaque;
461  	    int rc;
462  	
463  	    xmlNode *answer = NULL;
464  	    xmlNode *login = NULL;
465  	    const char *msg_type = NULL;
466  	    const char *tmp_ticket = NULL;
467  	
468  	    static struct mainloop_fd_callbacks cib_fd_callbacks = { 0, };
469  	
470  	    cib_fd_callbacks.dispatch =
471  	        event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
472  	    cib_fd_callbacks.destroy = cib_remote_connection_destroy;
473  	
474  	    connection->tcp_socket = -1;
475  	    connection->tls_session = NULL;
476  	    rc = pcmk__connect_remote(private->server, private->port, 0, NULL,
477  	                              &(connection->tcp_socket), NULL, NULL);
478  	    if (rc != pcmk_rc_ok) {
479  	        pcmk__info("Remote connection to %s:%d failed: %s " QB_XS " rc=%d",
480  	                   private->server, private->port, pcmk_rc_str(rc), rc);
481  	        return -ENOTCONN;
482  	    }
483  	
484  	    if (private->encrypted) {
485  	        rc = cib_setup_tls(connection, private->server, private->port,
486  	                           private->user);
487  	
488  	        if (rc != pcmk_rc_ok) {
489  	            if (connection->tls_session == NULL) {
490  	                cib_tls_close(cib);
491  	            }
492  	
493  	            return -rc;
494  	        }
495  	
496  	    } else {
497  	        pcmk__warn("Connecting to remote CIB without encryption. This is "
498  	                   "insecure and will be removed in a future release. Use "
499  	                   "the CIB_encrypted=true environment variable instead.");
500  	    }
501  	
502  	    /* Now that the handshake is done, see if any client TLS certificate is
503  	     * close to its expiration date and log if so.  If a TLS certificate is not
504  	     * in use, this function will just return so we don't need to check for the
505  	     * session type here.
506  	     */
507  	    pcmk__tls_check_cert_expiration(connection->tls_session);
508  	
509  	    /* login to server */
510  	    login = pcmk__xe_create(NULL, PCMK__XE_CIB_COMMAND);
511  	    pcmk__xe_set_props(login,
512  	                       PCMK_XA_OP, "authenticate",
513  	                       PCMK_XA_USER, private->user,
514  	                       PCMK__XA_PASSWORD, private->passwd,
515  	                       PCMK__XA_HIDDEN, PCMK__VALUE_PASSWORD,
516  	                       NULL);
517  	
518  	    pcmk__remote_send_xml(connection, login);
519  	    pcmk__xml_free(login);
520  	
521  	    rc = pcmk_ok;
522  	    if (pcmk__read_remote_message(connection, -1) == ENOTCONN) {
523  	        rc = -ENOTCONN;
524  	    }
525  	
526  	    answer = pcmk__remote_message_xml(connection);
527  	
528  	    if (answer == NULL) {
529  	        rc = -EPROTO;
530  	        goto done;
531  	    }
532  	
533  	    /* The only reason we can receive an ACK here is if dispatch_common ->
534  	     * pcmk__client_data2xml processed something that's not valid XML.
535  	     * dispatch_common does not return ACK, unlike other daemons.
536  	     */
537  	    if (pcmk__xe_is(answer, PCMK__XE_ACK) && ack_is_failure(answer)) {
538  	        rc = -EPROTO;
539  	        goto done;
540  	    }
541  	
542  	    pcmk__log_xml_trace(answer, "reg-reply");
543  	
544  	    /* grab the token */
545  	    msg_type = pcmk__xe_get(answer, PCMK__XA_CIB_OP);
546  	    tmp_ticket = pcmk__xe_get(answer, PCMK__XA_CIB_CLIENTID);
547  	
548  	    if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
549  	        pcmk__err("Invalid registration message: %s", msg_type);
550  	        rc = -EPROTO;
551  	
552  	    } else if (tmp_ticket == NULL) {
553  	        rc = -EPROTO;
554  	
555  	    } else {
556  	        connection->token = strdup(tmp_ticket);
557  	    }
558  	
559  	done:
560  	    g_clear_pointer(&answer, pcmk__xml_free);
561  	
562  	    if (rc != 0) {
563  	        cib_tls_close(cib);
564  	        return rc;
565  	    }
566  	
567  	    pcmk__trace("remote client connection established");
568  	    private->timeout_sec = 60;
569  	    connection->source = mainloop_add_fd("cib-remote", G_PRIORITY_HIGH,
570  	                                         connection->tcp_socket, cib,
571  	                                         &cib_fd_callbacks);
572  	    return rc;
573  	}
574  	
575  	static int
576  	cib_remote_signon(cib_t *cib, const char *name, enum cib_conn_type type)
577  	{
578  	    int rc = pcmk_ok;
579  	    cib_remote_opaque_t *private = cib->variant_opaque;
580  	
581  	    if (name == NULL) {
582  	        name = pcmk__s(crm_system_name, "client");
583  	    }
584  	
585  	    if (private->passwd == NULL) {
586  	        if (private->out == NULL) {
587  	            /* If no pcmk__output_t is set, just assume that a text prompt
588  	             * is good enough.
589  	             */
590  	            pcmk__text_prompt("Password", false, &(private->passwd));
591  	        } else {
592  	            private->out->prompt("Password", false, &(private->passwd));
593  	        }
594  	    }
595  	
596  	    if (private->server == NULL || private->user == NULL) {
597  	        rc = -EINVAL;
598  	        goto done;
599  	    }
600  	
601  	    rc = cib_tls_signon(cib, &(private->command), FALSE);
602  	    if (rc != pcmk_ok) {
603  	        goto done;
604  	    }
605  	
606  	    rc = cib_tls_signon(cib, &(private->callback), TRUE);
607  	
608  	done:
609  	    if (rc == pcmk_ok) {
610  	        pcmk__info("Opened connection to %s:%d for %s", private->server,
611  	                   private->port, name);
612  	        cib->state = cib_connected_command;
613  	        cib->type = cib_command;
614  	
615  	    } else {
616  	        pcmk__info("Connection to %s:%d for %s failed: %s\n", private->server,
617  	                   private->port, name, pcmk_strerror(rc));
618  	    }
619  	
620  	    return rc;
621  	}
622  	
623  	static int
624  	cib_remote_signoff(cib_t *cib)
625  	{
626  	    int rc = pcmk_ok;
627  	
628  	    pcmk__debug("Disconnecting from the CIB manager");
629  	    cib_tls_close(cib);
630  	
631  	    cib->cmds->end_transaction(cib, false, cib_none);
632  	    cib->state = cib_disconnected;
633  	    cib->type = cib_no_connection;
634  	
635  	    return rc;
636  	}
637  	
638  	static int
639  	cib_remote_free(cib_t *cib)
640  	{
641  	    int rc = pcmk_ok;
642  	
643  	    pcmk__warn("Freeing CIB");
644  	    if (cib->state != cib_disconnected) {
645  	        rc = cib_remote_signoff(cib);
646  	        if (rc == pcmk_ok) {
647  	            cib_remote_opaque_t *private = cib->variant_opaque;
648  	
649  	            free(private->server);
650  	            free(private->user);
651  	            free(private->passwd);
652  	            free(cib->cmds);
653  	            free(cib->user);
654  	            free(private);
655  	            free(cib);
656  	        }
657  	    }
658  	
659  	    return rc;
660  	}
661  	
662  	static int
663  	cib_remote_register_notification(cib_t * cib, const char *callback, int enabled)
664  	{
665  	    xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_COMMAND);
666  	    cib_remote_opaque_t *private = cib->variant_opaque;
667  	
668  	    pcmk__xe_set(notify_msg, PCMK__XA_CIB_OP, PCMK__VALUE_CIB_NOTIFY);
669  	    pcmk__xe_set(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
670  	    pcmk__xe_set_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
671  	    pcmk__remote_send_xml(&private->callback, notify_msg);
672  	    pcmk__xml_free(notify_msg);
673  	    return pcmk_ok;
674  	}
675  	
676  	static int
677  	cib_remote_set_connection_dnotify(cib_t * cib, void (*dnotify) (gpointer user_data))
678  	{
679  	    return -EPROTONOSUPPORT;
680  	}
681  	
682  	/*!
683  	 * \internal
684  	 * \brief Get the given CIB connection's unique client identifiers
685  	 *
686  	 * These can be used to check whether this client requested the action that
687  	 * triggered a CIB notification.
688  	 *
689  	 * \param[in]  cib       CIB connection
690  	 * \param[out] async_id  If not \p NULL, where to store asynchronous client ID
691  	 * \param[out] sync_id   If not \p NULL, where to store synchronous client ID
692  	 *
693  	 * \return Legacy Pacemaker return code (specifically, \p pcmk_ok)
694  	 *
695  	 * \note This is the \p cib_remote variant implementation of
696  	 *       \p cib_api_operations_t:client_id().
697  	 * \note The client IDs are assigned during CIB sign-on.
698  	 */
699  	static int
700  	cib_remote_client_id(const cib_t *cib, const char **async_id,
701  	                     const char **sync_id)
702  	{
703  	    cib_remote_opaque_t *private = cib->variant_opaque;
704  	
705  	    if (async_id != NULL) {
706  	        // private->callback is the channel for async requests
707  	        *async_id = private->callback.token;
708  	    }
709  	    if (sync_id != NULL) {
710  	        // private->command is the channel for sync requests
711  	        *sync_id = private->command.token;
712  	    }
713  	    return pcmk_ok;
714  	}
715  	
716  	cib_t *
717  	cib_remote_new(const char *server, const char *user, const char *passwd, int port,
718  	               gboolean encrypted)
719  	{
720  	    cib_remote_opaque_t *private = 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]
721  	    cib_t *cib = cib_new_variant();
722  	
(3) Event path: Condition "cib == NULL", taking false branch.
723  	    if (cib == NULL) {
724  	        return NULL;
725  	    }
726  	
727  	    private = calloc(1, sizeof(cib_remote_opaque_t));
728  	
(4) Event path: Condition "private == NULL", taking true branch.
729  	    if (private == NULL) {
CID (unavailable; MK=4a12853161e2d8c167707dd391f8150b) (#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]
730  	        free(cib);
731  	        return NULL;
732  	    }
733  	
734  	    cib->variant = cib_remote;
735  	    cib->variant_opaque = private;
736  	
737  	    private->server = pcmk__str_copy(server);
738  	    private->user = pcmk__str_copy(user);
739  	    private->passwd = pcmk__str_copy(passwd);
740  	    private->port = port;
741  	    private->encrypted = encrypted;
742  	
743  	    /* assign variant specific ops */
744  	    cib->delegate_fn = cib_remote_perform_op;
745  	    cib->cmds->signon = cib_remote_signon;
746  	    cib->cmds->signoff = cib_remote_signoff;
747  	    cib->cmds->free = cib_remote_free;
748  	    cib->cmds->register_notification = cib_remote_register_notification;
749  	    cib->cmds->set_connection_dnotify = cib_remote_set_connection_dnotify;
750  	
751  	    cib->cmds->client_id = cib_remote_client_id;
752  	
753  	    return cib;
754  	}
755  	
756  	void
757  	cib__set_output(cib_t *cib, pcmk__output_t *out)
758  	{
759  	    cib_remote_opaque_t *private;
760  	
761  	    if (cib->variant != cib_remote) {
762  	        return;
763  	    }
764  	
765  	    private = cib->variant_opaque;
766  	    private->out = out;
767  	}
768