Debian Codegraph @

components/keyrings/common/component_helpers/src/keyring_log_builtins_definition.cc

raw
1 /* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <time.h>
25 #include <cstdio>
26 #include <cstring>
27 #include <iostream>
28 #include <locale>
29 #include <memory>
30 #include <sstream>
31
32 #include <m_string.h>
33
34 #include <components/keyrings/common/component_helpers/include/keyring_log_builtins_definition.h>
35 /** Error structure */
36 typedef struct {
37 const char *name;
38 int errnr;
39 const char *text;
40
41 /* SQLSTATE */
42 const char *odbc_state;
43 const char *jdbc_state;
44 int error_index;
45 } st_error;
46
47 /** Error info - generated from error message file */
48 static st_error global_error_names[] = {
49 #ifndef IN_DOXYGEN
50 #include "mysqld_ername.h"
51 #endif /* IN_DOXYGEN */
52 {nullptr, 0, nullptr, nullptr, nullptr, 0}};
53
54 /**
55 Maximum number of key/value pairs in a log event.
56 May be changed or abolished later.
57 */
58 #define LOG_ITEM_MAX 64
59
60 /**
61 Iterator over the key/value pairs of a log_line.
62 At present, only one iter may exist per log_line.
63 */
64 typedef struct _log_item_iter {
65 struct _log_line *ll; ///< log_line this is the iter for
66 int index; ///< index of current key/value pair
67 } log_item_iter;
68
69 /**
70 log_line ("log event")
71 */
72 typedef struct _log_line {
73 log_item_type_mask seen; ///< bit field flagging item-types contained
74 log_item_iter iter; ///< iterator over key/value pairs
75 log_item output_buffer; ///< buffer a service can return its output in
76 int count; ///< number of key/value pairs ("log items")
77 log_item item[LOG_ITEM_MAX]; ///< log items
78 } log_line;
79
80 /**
81 Pre-defined "well-known" keys, as opposed to ad hoc ones,
82 for key/value pairs in logging.
83 */
84 typedef struct _log_item_wellknown_key {
85 const char *name; ///< key name
86 size_t name_len; ///< length of key's name
87 log_item_class item_class; ///< item class (float/int/string)
88 log_item_type item_type; ///< exact type, 1:1 relationship with name
89 } log_item_wellknown_key;
90
91 /** Required items and their type - See LogComponentErr */
92 static const log_item_wellknown_key log_item_wellknown_keys[] = {
93 {STRING_WITH_LEN("prio"), LOG_INTEGER, LOG_ITEM_LOG_PRIO},
94 {STRING_WITH_LEN("err_code"), LOG_INTEGER, LOG_ITEM_SQL_ERRCODE},
95 {STRING_WITH_LEN("subsystem"), LOG_CSTRING, LOG_ITEM_SRV_SUBSYS},
96 {STRING_WITH_LEN("component"), LOG_CSTRING, LOG_ITEM_SRV_COMPONENT},
97 {STRING_WITH_LEN("source_line"), LOG_INTEGER, LOG_ITEM_SRC_LINE},
98 {STRING_WITH_LEN("source_file"), LOG_CSTRING, LOG_ITEM_SRC_FILE},
99 {STRING_WITH_LEN("function"), LOG_CSTRING, LOG_ITEM_SRC_FUNC},
100 {STRING_WITH_LEN("msg"), LOG_CSTRING, LOG_ITEM_LOG_MESSAGE}};
101
102 static uint log_item_wellknown_keys_count =
103 (sizeof(log_item_wellknown_keys) / sizeof(log_item_wellknown_key));
104
105 /** Check if we know about the item type */
106 static int log_item_wellknown_by_type(log_item_type t) {
107 uint c;
108 // optimize and safeify lookup
109 for (c = 0; (c < log_item_wellknown_keys_count); c++) {
110 if (log_item_wellknown_keys[c].item_type == t) return c;
111 }
112 return LOG_ITEM_TYPE_NOT_FOUND;
113 }
114
115 /**
116 Convenience function: Derive a log label ("error", "warning",
117 "information") from a severity.
118
119 @param prio the severity/prio in question
120
121 @return a label corresponding to that priority.
122 @retval "System" for prio of SYSTEM_LEVEL
123 @retval "Error" for prio of ERROR_LEVEL
124 @retval "Warning" for prio of WARNING_LEVEL
125 @retval "Note" for prio of INFORMATION_LEVEL
126 */
127 static const char *log_label_from_prio(int prio) {
128 switch (prio) {
129 case SYSTEM_LEVEL:
130 return "System";
131 case ERROR_LEVEL:
132 return "Error";
133 case WARNING_LEVEL:
134 return "Warning";
135 case INFORMATION_LEVEL:
136 return "Note";
137 default:
138 return "Error";
139 }
140 }
141
142 /**
143 Base for line_item_set[_with_key]. Stripped down version of that in
144 log_builtins.cc.
145 */
146 static log_item_data *kr_line_item_set_with_key(log_line *ll, log_item_type t,
147 const char *k [[maybe_unused]],
148 uint32 alloc) {
149 if ((ll == nullptr) || (ll->count >= LOG_ITEM_MAX)) return nullptr;
150
151 log_item *li = &(ll->item[ll->count++]);
152 int c = log_item_wellknown_by_type(t);
153
154 li->alloc = alloc;
155 /*
156 keyring does not currently support generic types, so we always take
157 the key from the well-known array to be sure to use the canonical form.
158 */
159 li->key = log_item_wellknown_keys[c].name;
160 if ((li->item_class = log_item_wellknown_keys[c].item_class) == LOG_CSTRING)
161 li->item_class = LOG_LEX_STRING;
162 ll->seen |= (li->type = t);
163 return &li->data;
164 }
165
166 /**
167 Release any of key and value on a log-item that were dynamically allocated.
168
169 @param li log-item to release the payload of
170 */
171 static void kr_log_item_free(log_item *li) {
172 char *fa = nullptr;
173 if ((li->alloc & LOG_ITEM_FREE_VALUE) && (li->item_class == LOG_LEX_STRING) &&
174 ((fa = const_cast<char *>(li->data.data_string.str)) != nullptr)) {
175 delete[] fa;
176 li->alloc &= ~LOG_ITEM_FREE_VALUE;
177 }
178 }
179
180 /**
181 Release all log line items (key/value pairs) in log line ll.
182 This frees whichever keys and values were dynamically allocated.
183
184 @param ll log_line
185 */
186 static void kr_log_line_item_free_all(log_line *ll) {
187 while (ll->count > 0) kr_log_item_free(&(ll->item[--ll->count]));
188 ll->seen = LOG_ITEM_END;
189 }
190
191 namespace keyring_common {
192 namespace service_definition {
193
194 /* log_builtins */
195 DEFINE_METHOD(log_item_data *, Log_builtins_keyring::line_item_set_with_key,
196 (log_line * ll, log_item_type t, const char *key, uint32 alloc)) {
197 return kr_line_item_set_with_key(ll, t, key, alloc);
198 }
199
200 DEFINE_METHOD(log_item_data *, Log_builtins_keyring::line_item_set,
201 (log_line * ll, log_item_type t)) {
202 return kr_line_item_set_with_key(ll, t, nullptr, LOG_ITEM_FREE_NONE);
203 }
204
205 DEFINE_METHOD(log_line *, Log_builtins_keyring::line_init, ()) {
206 log_line *ll = new log_line();
207 if (ll != nullptr) memset(ll, 0, sizeof(log_line));
208 return ll;
209 }
210
211 DEFINE_METHOD(void, Log_builtins_keyring::line_exit, (log_line * ll)) {
212 if (ll != nullptr) delete ll;
213 }
214
215 DEFINE_METHOD(log_item_type_mask, Log_builtins_keyring::line_item_types_seen,
216 (log_line * ll, log_item_type_mask m)) {
217 return (ll != nullptr) ? (ll->seen & m) : 0;
218 }
219
220 DEFINE_METHOD(bool, Log_builtins_keyring::item_set_int,
221 (log_item_data * lid, longlong i)) {
222 if (lid != nullptr) {
223 lid->data_integer = i;
224 return false;
225 }
226 return true;
227 }
228
229 DEFINE_METHOD(bool, Log_builtins_keyring::item_set_lexstring,
230 (log_item_data * lid, const char *s, size_t s_len)) {
231 if (lid != nullptr) {
232 lid->data_string.str = (s == nullptr) ? "" : s;
233 lid->data_string.length = s_len;
234 return false;
235 }
236 return true;
237 }
238
239 DEFINE_METHOD(bool, Log_builtins_keyring::item_set_cstring,
240 (log_item_data * lid, const char *s)) {
241 if (lid != nullptr) {
242 lid->data_string.str = (s == nullptr) ? "" : s;
243 lid->data_string.length = strlen(lid->data_string.str);
244 return false;
245 }
246 return true;
247 }
248
249 DEFINE_METHOD(int, Log_builtins_keyring::line_submit, (log_line * ll)) {
250 if (ll->count > 0) {
251 log_item_type item_type;
252 int out_fields = 0;
253 const char *label = "Error";
254 size_t label_len = strlen(label);
255 enum loglevel prio = ERROR_LEVEL;
256 unsigned int errcode = 0;
257 const char *msg = "";
258 size_t msg_len = 0;
259 char *line_buffer = nullptr;
260 bool have_message = false;
261 for (int c = 0; c < ll->count; ++c) {
262 item_type = ll->item[c].type;
263 ++out_fields;
264 switch (item_type) {
265 case LOG_ITEM_LOG_PRIO:
266 prio = (enum loglevel)ll->item[c].data.data_integer;
267 label = log_label_from_prio(prio);
268 label_len = strlen(label);
269 break;
270 case LOG_ITEM_SQL_ERRCODE:
271 errcode = (unsigned int)ll->item[c].data.data_integer;
272 break;
273 case LOG_ITEM_LOG_MESSAGE: {
274 have_message = true;
275 const char *nl;
276 msg = ll->item[c].data.data_string.str;
277 msg_len = ll->item[c].data.data_string.length;
278 if ((nl = (const char *)memchr(msg, '\n', msg_len)) != nullptr) {
279 if (line_buffer != nullptr) delete[] line_buffer;
280 line_buffer = new char[msg_len + 1]();
281 if (line_buffer == nullptr) {
282 msg =
283 "The submitted error message contains a newline, "
284 "and a buffer to sanitize it for the traditional "
285 "log could not be allocated.";
286 msg_len = strlen(msg);
287 } else {
288 memcpy(line_buffer, msg, msg_len);
289 line_buffer[msg_len] = '\0';
290 char *nl2 = line_buffer;
291 while ((nl2 = strchr(nl2, '\n')) != nullptr) *(nl2++) = ' ';
292 msg = line_buffer;
293 }
294 }
295 break;
296 }
297 default:
298 --out_fields;
299 }
300 }
301
302 if (have_message) {
303 char internal_buff[LOG_BUFF_MAX];
304 size_t buff_size = sizeof(internal_buff);
305 char *buff_line = internal_buff;
306
307 const char format[] = "%Y-%m-%d %X";
308 time_t t(time(nullptr));
309 tm tm(*localtime(&t));
310
311 const size_t date_length{50};
312 std::unique_ptr<char[]> date{new char[date_length]};
313 strftime(date.get(), date_length, format, &tm);
314
315 std::string time_string = date.get();
316
317 (void)snprintf(buff_line, buff_size, "%s [%.*s] [MY-%06u] [Keyring] %.*s",
318 time_string.c_str(), (int)label_len, label, errcode,
319 (int)msg_len, msg);
320 std::cout << buff_line << std::endl;
321 if (line_buffer) delete[] line_buffer;
322 kr_log_line_item_free_all(ll);
323 return out_fields;
324 }
325 kr_log_line_item_free_all(ll);
326 return 0;
327 }
328 return 0;
329 }
330
331 DEFINE_METHOD(const char *, Log_builtins_keyring::errmsg_by_errcode,
332 (int mysql_errcode)) {
333 st_error *tmp_error;
334 const char *errtxt{"Unknown error"};
335
336 tmp_error = &global_error_names[0];
337
338 while (tmp_error->name != nullptr) {
339 if (tmp_error->errnr == mysql_errcode) {
340 errtxt = tmp_error->text;
341 break;
342 }
343 tmp_error++;
344 }
345 return errtxt;
346 }
347
348 /* log_builtins_string */
349 DEFINE_METHOD(void *, Log_builtins_keyring::malloc, (size_t len)) {
350 return new char[len + 1]();
351 }
352
353 DEFINE_METHOD(char *, Log_builtins_keyring::strndup,
354 (const char *fm, size_t len)) {
355 char *ptr = new char[len + 1]();
356 if (ptr != nullptr) {
357 memcpy(ptr, fm, len);
358 ptr[len] = 0;
359 }
360 return ptr;
361 }
362
363 DEFINE_METHOD(void, Log_builtins_keyring::free, (void *ptr)) {
364 if (ptr != nullptr) {
365 char *mem = (char *)ptr;
366 delete[] mem;
367 }
368 }
369
370 DEFINE_METHOD(size_t, Log_builtins_keyring::length, (const char *s)) {
371 return strlen(s);
372 }
373
374 DEFINE_METHOD(size_t, Log_builtins_keyring::substitutev,
375 (char *to, size_t n, const char *fmt, va_list ap)) {
376 return vsnprintf(to, n, fmt, ap);
377 }
378 } // namespace service_definition
379 } // namespace keyring_common