1 /*
2 * Copyright 2018-2021 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 #ifndef CRMCOMMON_PRIVATE__H
11 # define CRMCOMMON_PRIVATE__H
12
13 /* This header is for the sole use of libcrmcommon, so that functions can be
14 * declared with G_GNUC_INTERNAL for efficiency.
15 */
16
17 #include <stdint.h> // uint8_t, uint32_t
18 #include <stdbool.h> // bool
19 #include <sys/types.h> // size_t
20 #include <glib.h> // GList
21 #include <libxml/tree.h> // xmlNode, xmlAttr
22 #include <qb/qbipcc.h> // struct qb_ipc_response_header
23
24 // Decent chunk size for processing large amounts of data
25 #define PCMK__BUFFER_SIZE 4096
26
27 /* When deleting portions of an XML tree, we keep a record so we can know later
28 * (e.g. when checking differences) that something was deleted.
29 */
30 typedef struct pcmk__deleted_xml_s {
31 char *path;
32 int position;
33 } pcmk__deleted_xml_t;
34
35 typedef struct xml_private_s {
36 long check;
37 uint32_t flags;
38 char *user;
39 GList *acls;
40 GList *deleted_objs; // List of pcmk__deleted_xml_t
41 } xml_private_t;
42
43 #define pcmk__set_xml_flags(xml_priv, flags_to_set) do { \
44 (xml_priv)->flags = pcmk__set_flags_as(__func__, __LINE__, \
45 LOG_NEVER, "XML", "XML node", (xml_priv)->flags, \
46 (flags_to_set), #flags_to_set); \
47 } while (0)
48
49 #define pcmk__clear_xml_flags(xml_priv, flags_to_clear) do { \
50 (xml_priv)->flags = pcmk__clear_flags_as(__func__, __LINE__, \
51 LOG_NEVER, "XML", "XML node", (xml_priv)->flags, \
52 (flags_to_clear), #flags_to_clear); \
53 } while (0)
54
55 G_GNUC_INTERNAL
56 void pcmk__xml2text(xmlNode *data, int options, char **buffer, int *offset,
57 int *max, int depth);
58
59 G_GNUC_INTERNAL
60 void pcmk__buffer_add_char(char **buffer, int *offset, int *max, char c);
61
62 G_GNUC_INTERNAL
63 bool pcmk__tracking_xml_changes(xmlNode *xml, bool lazy);
64
65 G_GNUC_INTERNAL
66 int pcmk__element_xpath(const char *prefix, xmlNode *xml, char *buffer,
67 int offset, size_t buffer_size);
68
69 G_GNUC_INTERNAL
70 void pcmk__mark_xml_created(xmlNode *xml);
71
72 G_GNUC_INTERNAL
73 int pcmk__xml_position(xmlNode *xml, enum xml_private_flags ignore_if_set);
74
75 G_GNUC_INTERNAL
76 xmlNode *pcmk__xml_match(xmlNode *haystack, xmlNode *needle, bool exact);
77
78 G_GNUC_INTERNAL
79 void pcmk__xe_log(int log_level, const char *file, const char *function,
80 int line, const char *prefix, xmlNode *data, int depth,
81 int options);
82
83 G_GNUC_INTERNAL
84 void pcmk__xml_update(xmlNode *parent, xmlNode *target, xmlNode *update,
85 bool as_diff);
86
87 G_GNUC_INTERNAL
88 xmlNode *pcmk__xc_match(xmlNode *root, xmlNode *search_comment, bool exact);
89
90 G_GNUC_INTERNAL
91 void pcmk__xc_update(xmlNode *parent, xmlNode *target, xmlNode *update);
92
93 G_GNUC_INTERNAL
94 void pcmk__free_acls(GList *acls);
95
96 G_GNUC_INTERNAL
97 void pcmk__unpack_acl(xmlNode *source, xmlNode *target, const char *user);
98
99 G_GNUC_INTERNAL
100 void pcmk__apply_acl(xmlNode *xml);
101
102 G_GNUC_INTERNAL
103 void pcmk__apply_creation_acl(xmlNode *xml, bool check_top);
104
105 G_GNUC_INTERNAL
106 void pcmk__mark_xml_attr_dirty(xmlAttr *a);
107
108 G_GNUC_INTERNAL
109 bool pcmk__xa_filterable(const char *name);
110
111 static inline const char *
112 pcmk__xml_attr_value(const xmlAttr *attr)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
113 {
114 return ((attr == NULL) || (attr->children == NULL))? NULL
115 : (const char *) attr->children->content;
116 }
117
118 /*
119 * IPC
120 */
121
122 #define PCMK__IPC_VERSION 1
123
124 #define PCMK__CONTROLD_API_MAJOR "1"
125 #define PCMK__CONTROLD_API_MINOR "0"
126
127 // IPC behavior that varies by daemon
128 typedef struct pcmk__ipc_methods_s {
129 /*!
130 * \internal
131 * \brief Allocate any private data needed by daemon IPC
132 *
133 * \param[in] api IPC API connection
134 *
135 * \return Standard Pacemaker return code
136 */
137 int (*new_data)(pcmk_ipc_api_t *api);
138
139 /*!
140 * \internal
141 * \brief Free any private data used by daemon IPC
142 *
143 * \param[in] api_data Data allocated by new_data() method
144 */
145 void (*free_data)(void *api_data);
146
147 /*!
148 * \internal
149 * \brief Perform daemon-specific handling after successful connection
150 *
151 * Some daemons require clients to register before sending any other
152 * commands. The controller requires a CRM_OP_HELLO (with no reply), and
153 * the CIB manager, executor, and fencer require a CRM_OP_REGISTER (with a
154 * reply). Ideally this would be consistent across all daemons, but for now
155 * this allows each to do its own authorization.
156 *
157 * \param[in] api IPC API connection
158 *
159 * \return Standard Pacemaker return code
160 */
161 int (*post_connect)(pcmk_ipc_api_t *api);
162
163 /*!
164 * \internal
165 * \brief Check whether an IPC request results in a reply
166 *
167 * \param[in] api IPC API connection
168 * \param[in] request IPC request XML
169 *
170 * \return true if request would result in an IPC reply, false otherwise
171 */
172 bool (*reply_expected)(pcmk_ipc_api_t *api, xmlNode *request);
173
174 /*!
175 * \internal
176 * \brief Perform daemon-specific handling of an IPC message
177 *
178 * \param[in] api IPC API connection
179 * \param[in] msg Message read from IPC connection
180 *
181 * \return true if more IPC reply messages should be expected
182 */
183 bool (*dispatch)(pcmk_ipc_api_t *api, xmlNode *msg);
184
185 /*!
186 * \internal
187 * \brief Perform daemon-specific handling of an IPC disconnect
188 *
189 * \param[in] api IPC API connection
190 */
191 void (*post_disconnect)(pcmk_ipc_api_t *api);
192 } pcmk__ipc_methods_t;
193
194 // Implementation of pcmk_ipc_api_t
195 struct pcmk_ipc_api_s {
196 enum pcmk_ipc_server server; // Daemon this IPC API instance is for
197 enum pcmk_ipc_dispatch dispatch_type; // How replies should be dispatched
198 size_t ipc_size_max; // maximum IPC buffer size
199 crm_ipc_t *ipc; // IPC connection
200 mainloop_io_t *mainloop_io; // If using mainloop, I/O source for IPC
201 bool free_on_disconnect; // Whether disconnect should free object
202 pcmk_ipc_callback_t cb; // Caller-registered callback (if any)
203 void *user_data; // Caller-registered data (if any)
204 void *api_data; // For daemon-specific use
205 pcmk__ipc_methods_t *cmds; // Behavior that varies by daemon
206 };
207
208 typedef struct pcmk__ipc_header_s {
209 struct qb_ipc_response_header qb;
210 uint32_t size_uncompressed;
211 uint32_t size_compressed;
212 uint32_t flags;
213 uint8_t version;
214 } pcmk__ipc_header_t;
215
216 G_GNUC_INTERNAL
217 int pcmk__send_ipc_request(pcmk_ipc_api_t *api, xmlNode *request);
218
219 G_GNUC_INTERNAL
220 void pcmk__call_ipc_callback(pcmk_ipc_api_t *api,
221 enum pcmk_ipc_event event_type,
222 crm_exit_t status, void *event_data);
223
224 G_GNUC_INTERNAL
225 unsigned int pcmk__ipc_buffer_size(unsigned int max);
226
227 G_GNUC_INTERNAL
228 bool pcmk__valid_ipc_header(const pcmk__ipc_header_t *header);
229
230 G_GNUC_INTERNAL
231 pcmk__ipc_methods_t *pcmk__controld_api_methods(void);
232
233 G_GNUC_INTERNAL
234 pcmk__ipc_methods_t *pcmk__pacemakerd_api_methods(void);
235
236 G_GNUC_INTERNAL
237 pcmk__ipc_methods_t *pcmk__schedulerd_api_methods(void);
238
239
240 /*
241 * Logging
242 */
243
244 /*!
245 * \brief Check the authenticity of the IPC socket peer process
246 *
247 * If everything goes well, peer's authenticity is verified by the means
248 * of comparing against provided referential UID and GID (either satisfies),
249 * and the result of this check can be deduced from the return value.
250 * As an exception, detected UID of 0 ("root") satisfies arbitrary
251 * provided referential daemon's credentials.
252 *
253 * \param[in] qb_ipc libqb client connection if available
254 * \param[in] sock IPC related, connected Unix socket to check peer of
255 * \param[in] refuid referential UID to check against
256 * \param[in] refgid referential GID to check against
257 * \param[out] gotpid to optionally store obtained PID of the peer
258 * (not available on FreeBSD, special value of 1
259 * used instead, and the caller is required to
260 * special case this value respectively)
261 * \param[out] gotuid to optionally store obtained UID of the peer
262 * \param[out] gotgid to optionally store obtained GID of the peer
263 *
264 * \return Standard Pacemaker return code
265 * ie: 0 if it the connection is authentic
266 * pcmk_rc_ipc_unauthorized if the connection is not authentic,
267 * standard errors.
268 *
269 * \note While this function is tolerant on what constitutes authorized
270 * IPC daemon process (its effective user matches UID=0 or \p refuid,
271 * or at least its group matches \p refgid), either or both (in case
272 * of UID=0) mismatches on the expected credentials of such peer
273 * process \e shall be investigated at the caller when value of 1
274 * gets returned there, since higher-than-expected privileges in
275 * respect to the expected/intended credentials possibly violate
276 * the least privilege principle and may pose an additional risk
277 * (i.e. such accidental inconsistency shall be eventually fixed).
278 */
279 int pcmk__crm_ipc_is_authentic_process(qb_ipcc_connection_t *qb_ipc, int sock, uid_t refuid, gid_t refgid,
280 pid_t *gotpid, uid_t *gotuid, gid_t *gotgid);
281
282
283 #endif // CRMCOMMON_PRIVATE__H