Debian Codegraph @

runtime/timezones.c

raw
1 /* timezones.c
2 * Support for timezones in RainerScript.
3 *
4 * Copyright 2022 Attila Lakatos and Adiscon GmbH.
5 *
6 * This file is part of the rsyslog runtime library.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * -or-
14 * see COPYING.ASL20 in the source distribution
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23 #include "config.h"
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <ctype.h>
28 #include <stdint.h>
29 #include <errno.h>
30
31 #include "rsyslog.h"
32 #include "unicode-helper.h"
33 #include "errmsg.h"
34 #include "parserif.h"
35 #include "rainerscript.h"
36 #include "srUtils.h"
37 #include "rsconf.h"
38
39
40 static struct cnfparamdescr timezonecnfparamdescr[] = {{"id", eCmdHdlrString, CNFPARAM_REQUIRED},
41 {"offset", eCmdHdlrGetWord, CNFPARAM_REQUIRED}};
42 static struct cnfparamblk timezonepblk = {
43 CNFPARAMBLK_VERSION, sizeof(timezonecnfparamdescr) / sizeof(struct cnfparamdescr), timezonecnfparamdescr};
44
45 /* Note: this function is NOT thread-safe!
46 * This is currently not needed as used only during
47 * initialization.
48 */
49 static rsRetVal addTimezoneInfo(rsconf_t *cnf, uchar *tzid, char offsMode, int8_t offsHour, int8_t offsMin) {
50 DEFiRet;
51 tzinfo_t *newti;
52 CHKmalloc(newti = realloc(cnf->timezones.tzinfos, (cnf->timezones.ntzinfos + 1) * sizeof(tzinfo_t)));
53 if ((newti[cnf->timezones.ntzinfos].id = strdup((char *)tzid)) == NULL) {
54 free(newti);
55 DBGPRINTF("addTimezoneInfo: strdup failed with OOM\n");
56 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
57 }
58 newti[cnf->timezones.ntzinfos].offsMode = offsMode;
59 newti[cnf->timezones.ntzinfos].offsHour = offsHour;
60 newti[cnf->timezones.ntzinfos].offsMin = offsMin;
61 ++cnf->timezones.ntzinfos, cnf->timezones.tzinfos = newti;
62 finalize_it:
63 RETiRet;
64 }
65
66 void glblProcessTimezone(struct cnfobj *o) {
67 struct cnfparamvals *pvals;
68 uchar *id = NULL;
69 uchar *offset = NULL;
70 char offsMode;
71 int8_t offsHour;
72 int8_t offsMin;
73 int i;
74
75 pvals = nvlstGetParams(o->nvlst, &timezonepblk, NULL);
76 if (pvals == NULL) {
77 LogError(0, RS_RET_MISSING_CNFPARAMS,
78 "error processing timezone "
79 "config parameters");
80 goto done;
81 }
82 if (Debug) {
83 dbgprintf("timezone param blk after glblProcessTimezone:\n");
84 cnfparamsPrint(&timezonepblk, pvals);
85 }
86
87 for (i = 0; i < timezonepblk.nParams; ++i) {
88 if (!pvals[i].bUsed) continue;
89 if (!strcmp(timezonepblk.descr[i].name, "id")) {
90 id = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL);
91 } else if (!strcmp(timezonepblk.descr[i].name, "offset")) {
92 offset = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL);
93 } else {
94 dbgprintf(
95 "glblProcessTimezone: program error, non-handled "
96 "param '%s'\n",
97 timezonepblk.descr[i].name);
98 }
99 }
100
101 /* note: the following two checks for NULL are not strictly necessary
102 * as these are required parameters for the config block. But we keep
103 * them to make the clang static analyzer happy, which also helps
104 * guard against logic errors.
105 */
106 if (offset == NULL) {
107 parser_errmsg("offset parameter missing (logic error?), timezone config ignored");
108 goto done;
109 }
110 if (id == NULL) {
111 parser_errmsg("id parameter missing (logic error?), timezone config ignored");
112 goto done;
113 }
114
115 if (strlen((char *)offset) != 6 || !(offset[0] == '-' || offset[0] == '+') ||
116 !(isdigit(offset[1]) && isdigit(offset[2])) || offset[3] != ':' ||
117 !(isdigit(offset[4]) && isdigit(offset[5]))) {
118 parser_errmsg("timezone offset has invalid format. Must be +/-hh:mm, e.g. \"-07:00\".");
119 goto done;
120 }
121
122 offsHour = (offset[1] - '0') * 10 + offset[2] - '0';
123 offsMin = (offset[4] - '0') * 10 + offset[5] - '0';
124 offsMode = offset[0];
125
126 if (offsHour > 12 || offsMin > 59) {
127 parser_errmsg("timezone offset outside of supported range (hours 0..12, minutes 0..59)");
128 goto done;
129 }
130
131 addTimezoneInfo(loadConf, id, offsMode, offsHour, offsMin);
132
133 done:
134 cnfparamvalsDestruct(pvals, &timezonepblk);
135 free(id);
136 free(offset);
137 }
138
139 /* comparison function for qsort() and string array compare
140 * this is for the string lookup table type
141 */
142 static int qs_arrcmp_tzinfo(const void *s1, const void *s2) {
143 return strcmp(((tzinfo_t *)s1)->id, ((tzinfo_t *)s2)->id);
144 }
145
146 void sortTimezones(rsconf_t *cnf) {
147 if (cnf->timezones.ntzinfos > 0) {
148 qsort(cnf->timezones.tzinfos, cnf->timezones.ntzinfos, sizeof(tzinfo_t), qs_arrcmp_tzinfo);
149 }
150 }
151
152 void displayTimezones(rsconf_t *cnf) {
153 if (!Debug) return;
154 for (int i = 0; i < cnf->timezones.ntzinfos; ++i)
155 dbgprintf("tzinfo: '%s':%c%2.2d:%2.2d\n", cnf->timezones.tzinfos[i].id, cnf->timezones.tzinfos[i].offsMode,
156 cnf->timezones.tzinfos[i].offsHour, cnf->timezones.tzinfos[i].offsMin);
157 }
158
159 static int bs_arrcmp_tzinfo(const void *s1, const void *s2) {
160 return strcmp((char *)s1, (char *)((tzinfo_t *)s2)->id);
161 }
162
163 /* returns matching timezone info or NULL if no entry exists */
164 tzinfo_t *glblFindTimezone(rsconf_t *cnf, char *id) {
165 return (tzinfo_t *)bsearch(id, cnf->timezones.tzinfos, cnf->timezones.ntzinfos, sizeof(tzinfo_t), bs_arrcmp_tzinfo);
166 }
167
168 static void freeTimezone(tzinfo_t *tzinfo) {
169 free(tzinfo->id);
170 }
171
172 void freeTimezones(rsconf_t *cnf) {
173 for (int i = 0; i < cnf->timezones.ntzinfos; ++i) freeTimezone(&cnf->timezones.tzinfos[i]);
174 if (cnf->timezones.ntzinfos > 0) free(cnf->timezones.tzinfos);
175 cnf->timezones.tzinfos = NULL;
176 }