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