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 *tmp = cib__get_calldata(op_reply);
205  	
206  	        if (tmp == NULL) {
207  	            pcmk__trace("No output in reply to \"%s\" command %d", op,
208  	                        (cib->call_id - 1));
209  	        } else {
210  	            *output_data = pcmk__xml_copy(NULL, tmp);
211  	        }
212  	    }
213  	
214  	    pcmk__xml_free(op_reply);
215  	
216  	    return rc;
217  	}
218  	
219  	static int
220  	cib_remote_callback_dispatch(gpointer user_data)
221  	{
222  	    int rc;
223  	    cib_t *cib = user_data;
224  	    cib_remote_opaque_t *private = cib->variant_opaque;
225  	
226  	    xmlNode *msg = NULL;
227  	    const char *type = NULL;
228  	
229  	    /* If start time is 0, we've previously handled a complete message and this
230  	     * connection is being reused for a new message.  Reset the start_time,
231  	     * giving this new message timeout_sec from now to complete.
232  	     */
233  	    if (private->start_time == 0) {
234  	        private->start_time = time(NULL);
235  	    }
236  	
237  	    rc = pcmk__read_available_remote_data(&private->callback);
238  	    switch (rc) {
239  	        case pcmk_rc_ok:
240  	            /* We have the whole message so process it */
241  	            break;
242  	
243  	        case EAGAIN:
244  	            /* Have we timed out? */
245  	            if (time(NULL) >= private->start_time + private->timeout_sec) {
246  	                pcmk__info("Error reading from CIB manager connection: %s",
247  	                           pcmk_rc_str(ETIME));
248  	                return -1;
249  	            }
250  	
251  	            /* We haven't read the whole message yet */
252  	            return 0;
253  	
254  	        default:
255  	            /* Error */
256  	            pcmk__info("Error reading from CIB manager connection: %s",
257  	                       pcmk_rc_str(rc));
258  	            return -1;
259  	    }
260  	
261  	    // coverity[tainted_data] This can't easily be changed right now
262  	    msg = pcmk__remote_message_xml(&private->callback);
263  	    if (msg == NULL) {
264  	        private->start_time = 0;
265  	        return 0;
266  	    }
267  	
268  	    type = pcmk__xe_get(msg, PCMK__XA_T);
269  	
270  	    pcmk__trace("Activating %s callbacks...", type);
271  	
272  	    if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
273  	        cib_native_callback(cib, msg, 0, 0);
274  	    } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
275  	        g_list_foreach(cib->notify_list, cib_native_notify, msg);
276  	    } else {
277  	        pcmk__err("Unknown message type: %s", type);
278  	    }
279  	
280  	    pcmk__xml_free(msg);
281  	    private->start_time = 0;
282  	    return 0;
283  	}
284  	
285  	static int
286  	cib_remote_command_dispatch(gpointer user_data)
287  	{
288  	    int rc;
289  	    cib_t *cib = user_data;
290  	    cib_remote_opaque_t *private = cib->variant_opaque;
291  	
292  	    /* See cib_remote_callback_dispatch */
293  	    if (private->start_time == 0) {
294  	        private->start_time = time(NULL);
295  	    }
296  	
297  	    rc = pcmk__read_available_remote_data(&private->command);
298  	    if (rc == EAGAIN) {
299  	        /* Have we timed out? */
300  	        if (time(NULL) >= private->start_time + private->timeout_sec) {
301  	            pcmk__info("Error reading from CIB manager connection: %s",
302  	                       pcmk_rc_str(ETIME));
303  	            return -1;
304  	        }
305  	
306  	        /* We haven't read the whole message yet */
307  	        return 0;
308  	    }
309  	
310  	    g_clear_pointer(&private->command.buffer, free);
311  	    pcmk__err("Received late reply for remote cib connection, discarding");
312  	
313  	    if (rc != pcmk_rc_ok) {
314  	        pcmk__info("Error reading from CIB manager connection: %s",
315  	                   pcmk_rc_str(rc));
316  	        return -1;
317  	    }
318  	
319  	    private->start_time = 0;
320  	    return 0;
321  	}
322  	
323  	static int
324  	cib_tls_close(cib_t *cib)
325  	{
326  	    cib_remote_opaque_t *private = cib->variant_opaque;
327  	
328  	    if (private->encrypted) {
329  	        if (private->command.tls_session) {
330  	            gnutls_bye(private->command.tls_session, GNUTLS_SHUT_RDWR);
331  	            gnutls_deinit(private->command.tls_session);
332  	        }
333  	
334  	        if (private->callback.tls_session) {
335  	            gnutls_bye(private->callback.tls_session, GNUTLS_SHUT_RDWR);
336  	            gnutls_deinit(private->callback.tls_session);
337  	        }
338  	
339  	        private->command.tls_session = NULL;
340  	        private->callback.tls_session = NULL;
341  	        g_clear_pointer(&tls, pcmk__free_tls);
342  	    }
343  	
344  	    if (private->command.tcp_socket >= 0) {
345  	        shutdown(private->command.tcp_socket, SHUT_RDWR);       /* no more receptions */
346  	        close(private->command.tcp_socket);
347  	    }
348  	    if (private->callback.tcp_socket >= 0) {
349  	        shutdown(private->callback.tcp_socket, SHUT_RDWR);      /* no more receptions */
350  	        close(private->callback.tcp_socket);
351  	    }
352  	    private->command.tcp_socket = -1;
353  	    private->callback.tcp_socket = -1;
354  	
355  	    g_clear_pointer(&private->command.buffer, free);
356  	    g_clear_pointer(&private->callback.buffer, free);
357  	
358  	    return 0;
359  	}
360  	
361  	static void
362  	cib_remote_connection_destroy(gpointer user_data)
363  	{
364  	    pcmk__err("Connection destroyed");
365  	    cib_tls_close(user_data);
366  	}
367  	
368  	static int
369  	cib_setup_tls(pcmk__remote_t *connection, const char *server, int port,
370  	              const char *user)
371  	{
372  	    int rc = pcmk_rc_ok;
373  	    int tls_rc = GNUTLS_E_SUCCESS;
374  	    bool have_psk = false;
375  	    const char *key_location = getenv("CIB_key_file");
376  	
377  	    /* X509 certificates take precedence over PSK in pcmk__init_tls,
378  	     * so don't perform any of the following (potentially noisy) checks
379  	     * if we don't care about their results.
380  	     */
381  	    if (!pcmk__x509_enabled()) {
382  	        bool file_exists = false;
383  	
384  	        if (key_location != NULL) {
385  	            have_psk = pcmk__cred_file_useable(key_location, &file_exists);
386  	        }
387  	
388  	        if (!have_psk && file_exists) {
389  	            /* The credential file exists but doesn't have the right owner
390  	             * or permissions.  Don't fall back to anonymous on config
391  	             * errors.
392  	             */
393  	            pcmk__err("Remote CIB session creation for %s:%d failed",
394  	                      server, port);
395  	            rc = EACCES;
396  	            goto done;
397  	        }
398  	
399  	        if (!have_psk) {
400  	            /* The credential file does not exist at all, so fall back
401  	             * to anonymous auth.
402  	             *
403  	             * @COMPAT Remove fallback to anonymous authentication
404  	             */
405  	            pcmk__warn("Falling back to anonymous authentication for remote "
406  	                       "CIB connections");
407  	        }
408  	    }
409  	
410  	    rc = pcmk__init_tls(&tls, false, have_psk);
411  	    if (rc != pcmk_rc_ok) {
412  	        goto done;
413  	    }
414  	
415  	    if (tls->cred_type == GNUTLS_CRD_PSK) {
416  	        gnutls_datum_t psk_key = { NULL, 0 };
417  	
418  	        rc = pcmk__load_key(key_location, &psk_key, false);
419  	
420  	        if (rc != pcmk_rc_ok) {
421  	            pcmk__warn("Could not read remote CIB key from %s: %s",
422  	                       key_location, pcmk_rc_str(rc));
423  	            goto done;
424  	        }
425  	
426  	        pcmk__tls_client_add_psk_key(tls, user, &psk_key, false);
427  	        gnutls_free(psk_key.data);
428  	    }
429  	
430  	    connection->tls_session = pcmk__new_tls_session(tls, connection->tcp_socket);
431  	    if (connection->tls_session == NULL) {
432  	        rc = ENOTCONN;
433  	        goto done;
434  	    }
435  	
436  	    rc = pcmk__tls_client_handshake(connection, TLS_HANDSHAKE_TIMEOUT, &tls_rc);
437  	    if (rc != pcmk_rc_ok) {
438  	        const char *msg = NULL;
439  	
440  	        if (rc == EPROTO) {
441  	            msg = gnutls_strerror(tls_rc);
442  	        } else {
443  	            msg = pcmk_rc_str(rc);
444  	        }
445  	
446  	        pcmk__err("Remote CIB session creation for %s:%d failed: %s",
447  	                  server, port, msg);
448  	        g_clear_pointer(&connection->tls_session, gnutls_deinit);
449  	    }
450  	
451  	done:
452  	    return rc;
453  	}
454  	
455  	static int
456  	cib_tls_signon(cib_t *cib, pcmk__remote_t *connection, gboolean event_channel)
457  	{
458  	    cib_remote_opaque_t *private = cib->variant_opaque;
459  	    int rc;
460  	
461  	    xmlNode *answer = NULL;
462  	    xmlNode *login = NULL;
463  	    const char *msg_type = NULL;
464  	    const char *tmp_ticket = NULL;
465  	
466  	    static struct mainloop_fd_callbacks cib_fd_callbacks = { 0, };
467  	
468  	    cib_fd_callbacks.dispatch =
469  	        event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
470  	    cib_fd_callbacks.destroy = cib_remote_connection_destroy;
471  	
472  	    connection->tcp_socket = -1;
473  	    connection->tls_session = NULL;
474  	    rc = pcmk__connect_remote(private->server, private->port, 0, NULL,
475  	                              &(connection->tcp_socket), NULL, NULL);
476  	    if (rc != pcmk_rc_ok) {
477  	        pcmk__info("Remote connection to %s:%d failed: %s " QB_XS " rc=%d",
478  	                   private->server, private->port, pcmk_rc_str(rc), rc);
479  	        return -ENOTCONN;
480  	    }
481  	
482  	    if (private->encrypted) {
483  	        rc = cib_setup_tls(connection, private->server, private->port,
484  	                           private->user);
485  	
486  	        if (rc != pcmk_rc_ok) {
487  	            if (connection->tls_session == NULL) {
488  	                cib_tls_close(cib);
489  	            }
490  	
491  	            return -rc;
492  	        }
493  	
494  	    } else {
495  	        pcmk__warn("Connecting to remote CIB without encryption. This is "
496  	                   "insecure and will be removed in a future release. Use "
497  	                   "the CIB_encrypted=true environment variable instead.");
498  	    }
499  	
500  	    /* Now that the handshake is done, see if any client TLS certificate is
501  	     * close to its expiration date and log if so.  If a TLS certificate is not
502  	     * in use, this function will just return so we don't need to check for the
503  	     * session type here.
504  	     */
505  	    pcmk__tls_check_cert_expiration(connection->tls_session);
506  	
507  	    /* login to server */
508  	    login = pcmk__xe_create(NULL, PCMK__XE_CIB_COMMAND);
509  	    pcmk__xe_set_props(login,
510  	                       PCMK_XA_OP, "authenticate",
511  	                       PCMK_XA_USER, private->user,
512  	                       PCMK__XA_PASSWORD, private->passwd,
513  	                       PCMK__XA_HIDDEN, PCMK__VALUE_PASSWORD,
514  	                       NULL);
515  	
516  	    pcmk__remote_send_xml(connection, login);
517  	    pcmk__xml_free(login);
518  	
519  	    rc = pcmk_ok;
520  	    if (pcmk__read_remote_message(connection, -1) == ENOTCONN) {
521  	        rc = -ENOTCONN;
522  	    }
523  	
524  	    answer = pcmk__remote_message_xml(connection);
525  	
526  	    if (answer == NULL) {
527  	        rc = -EPROTO;
528  	        goto done;
529  	    }
530  	
531  	    /* The only reason we can receive an ACK here is if dispatch_common ->
532  	     * pcmk__client_data2xml processed something that's not valid XML.
533  	     * dispatch_common does not return ACK, unlike other daemons.
534  	     */
535  	    if (pcmk__xe_is(answer, PCMK__XE_ACK) && ack_is_failure(answer)) {
536  	        rc = -EPROTO;
537  	        goto done;
538  	    }
539  	
540  	    pcmk__log_xml_trace(answer, "reg-reply");
541  	
542  	    /* grab the token */
543  	    msg_type = pcmk__xe_get(answer, PCMK__XA_CIB_OP);
544  	    tmp_ticket = pcmk__xe_get(answer, PCMK__XA_CIB_CLIENTID);
545  	
546  	    if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
547  	        pcmk__err("Invalid registration message: %s", msg_type);
548  	        rc = -EPROTO;
549  	
550  	    } else if (tmp_ticket == NULL) {
551  	        rc = -EPROTO;
552  	
553  	    } else {
554  	        connection->token = strdup(tmp_ticket);
555  	    }
556  	
557  	done:
558  	    g_clear_pointer(&answer, pcmk__xml_free);
559  	
560  	    if (rc != 0) {
561  	        cib_tls_close(cib);
562  	        return rc;
563  	    }
564  	
565  	    pcmk__trace("remote client connection established");
566  	    private->timeout_sec = 60;
567  	    connection->source = mainloop_add_fd("cib-remote", G_PRIORITY_HIGH,
568  	                                         connection->tcp_socket, cib,
569  	                                         &cib_fd_callbacks);
570  	    return rc;
571  	}
572  	
573  	static int
574  	cib_remote_signon(cib_t *cib, const char *name, enum cib_conn_type type)
575  	{
576  	    int rc = pcmk_ok;
577  	    cib_remote_opaque_t *private = cib->variant_opaque;
578  	
579  	    if (name == NULL) {
580  	        name = pcmk__s(crm_system_name, "client");
581  	    }
582  	
583  	    if (private->passwd == NULL) {
584  	        if (private->out == NULL) {
585  	            /* If no pcmk__output_t is set, just assume that a text prompt
586  	             * is good enough.
587  	             */
588  	            pcmk__text_prompt("Password", false, &(private->passwd));
589  	        } else {
590  	            private->out->prompt("Password", false, &(private->passwd));
591  	        }
592  	    }
593  	
594  	    if (private->server == NULL || private->user == NULL) {
595  	        rc = -EINVAL;
596  	        goto done;
597  	    }
598  	
599  	    rc = cib_tls_signon(cib, &(private->command), FALSE);
600  	    if (rc != pcmk_ok) {
601  	        goto done;
602  	    }
603  	
604  	    rc = cib_tls_signon(cib, &(private->callback), TRUE);
605  	
606  	done:
607  	    if (rc == pcmk_ok) {
608  	        pcmk__info("Opened connection to %s:%d for %s", private->server,
609  	                   private->port, name);
610  	        cib->state = cib_connected_command;
611  	        cib->type = cib_command;
612  	
613  	    } else {
614  	        pcmk__info("Connection to %s:%d for %s failed: %s\n", private->server,
615  	                   private->port, name, pcmk_strerror(rc));
616  	    }
617  	
618  	    return rc;
619  	}
620  	
621  	static int
622  	cib_remote_signoff(cib_t *cib)
623  	{
624  	    int rc = pcmk_ok;
625  	
626  	    pcmk__debug("Disconnecting from the CIB manager");
627  	    cib_tls_close(cib);
628  	
629  	    cib->cmds->end_transaction(cib, false, cib_none);
630  	    cib->state = cib_disconnected;
631  	    cib->type = cib_no_connection;
632  	
633  	    return rc;
634  	}
635  	
636  	static int
637  	cib_remote_free(cib_t *cib)
638  	{
639  	    int rc = pcmk_ok;
640  	
641  	    pcmk__warn("Freeing CIB");
642  	    if (cib->state != cib_disconnected) {
643  	        rc = cib_remote_signoff(cib);
644  	        if (rc == pcmk_ok) {
645  	            cib_remote_opaque_t *private = cib->variant_opaque;
646  	
647  	            free(private->server);
648  	            free(private->user);
649  	            free(private->passwd);
650  	            free(cib->cmds);
651  	            free(cib->user);
652  	            free(private);
653  	            free(cib);
654  	        }
655  	    }
656  	
657  	    return rc;
658  	}
659  	
660  	static int
661  	cib_remote_register_notification(cib_t * cib, const char *callback, int enabled)
662  	{
663  	    xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_COMMAND);
664  	    cib_remote_opaque_t *private = cib->variant_opaque;
665  	
666  	    pcmk__xe_set(notify_msg, PCMK__XA_CIB_OP, PCMK__VALUE_CIB_NOTIFY);
667  	    pcmk__xe_set(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
668  	    pcmk__xe_set_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
669  	    pcmk__remote_send_xml(&private->callback, notify_msg);
670  	    pcmk__xml_free(notify_msg);
671  	    return pcmk_ok;
672  	}
673  	
674  	static int
675  	cib_remote_set_connection_dnotify(cib_t * cib, void (*dnotify) (gpointer user_data))
676  	{
677  	    return -EPROTONOSUPPORT;
678  	}
679  	
680  	/*!
681  	 * \internal
682  	 * \brief Get the given CIB connection's unique client identifiers
683  	 *
684  	 * These can be used to check whether this client requested the action that
685  	 * triggered a CIB notification.
686  	 *
687  	 * \param[in]  cib       CIB connection
688  	 * \param[out] async_id  If not \p NULL, where to store asynchronous client ID
689  	 * \param[out] sync_id   If not \p NULL, where to store synchronous client ID
690  	 *
691  	 * \return Legacy Pacemaker return code (specifically, \p pcmk_ok)
692  	 *
693  	 * \note This is the \p cib_remote variant implementation of
694  	 *       \p cib_api_operations_t:client_id().
695  	 * \note The client IDs are assigned during CIB sign-on.
696  	 */
697  	static int
698  	cib_remote_client_id(const cib_t *cib, const char **async_id,
699  	                     const char **sync_id)
700  	{
701  	    cib_remote_opaque_t *private = cib->variant_opaque;
702  	
703  	    if (async_id != NULL) {
704  	        // private->callback is the channel for async requests
705  	        *async_id = private->callback.token;
706  	    }
707  	    if (sync_id != NULL) {
708  	        // private->command is the channel for sync requests
709  	        *sync_id = private->command.token;
710  	    }
711  	    return pcmk_ok;
712  	}
713  	
714  	cib_t *
715  	cib_remote_new(const char *server, const char *user, const char *passwd, int port,
716  	               gboolean encrypted)
717  	{
718  	    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]
719  	    cib_t *cib = cib_new_variant();
720  	
(3) Event path: Condition "cib == NULL", taking false branch.
721  	    if (cib == NULL) {
722  	        return NULL;
723  	    }
724  	
725  	    private = calloc(1, sizeof(cib_remote_opaque_t));
726  	
(4) Event path: Condition "private == NULL", taking true branch.
727  	    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]
728  	        free(cib);
729  	        return NULL;
730  	    }
731  	
732  	    cib->variant = cib_remote;
733  	    cib->variant_opaque = private;
734  	
735  	    private->server = pcmk__str_copy(server);
736  	    private->user = pcmk__str_copy(user);
737  	    private->passwd = pcmk__str_copy(passwd);
738  	    private->port = port;
739  	    private->encrypted = encrypted;
740  	
741  	    /* assign variant specific ops */
742  	    cib->delegate_fn = cib_remote_perform_op;
743  	    cib->cmds->signon = cib_remote_signon;
744  	    cib->cmds->signoff = cib_remote_signoff;
745  	    cib->cmds->free = cib_remote_free;
746  	    cib->cmds->register_notification = cib_remote_register_notification;
747  	    cib->cmds->set_connection_dnotify = cib_remote_set_connection_dnotify;
748  	
749  	    cib->cmds->client_id = cib_remote_client_id;
750  	
751  	    return cib;
752  	}
753  	
754  	void
755  	cib__set_output(cib_t *cib, pcmk__output_t *out)
756  	{
757  	    cib_remote_opaque_t *private;
758  	
759  	    if (cib->variant != cib_remote) {
760  	        return;
761  	    }
762  	
763  	    private = cib->variant_opaque;
764  	    private->out = out;
765  	}
766