1 /*-------------------------------------------------------------------------
4 * Functions for parsing timezone offset files
6 * Note: this code is invoked from the check_hook for the GUC variable
7 * timezone_abbreviations. Therefore, it should report problems using
8 * GUC_check_errmsg() and related functions, and try to avoid throwing
9 * elog(ERROR). This is not completely bulletproof at present --- in
10 * particular out-of-memory will throw an error. Could probably fix with
11 * PG_TRY if necessary.
14 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
15 * Portions Copyright (c) 1994, Regents of the University of California
18 * src/backend/utils/misc/tzparser.c
20 *-------------------------------------------------------------------------
27 #include "miscadmin.h"
28 #include "storage/fd.h"
29 #include "utils/guc.h"
30 #include "utils/memutils.h"
31 #include "utils/tzparser.h"
34 #define WHITESPACE " \t\n\r"
36 static bool validateTzEntry(tzEntry *tzentry);
37 static bool splitTzLine(const char *filename, int lineno,
38 char *line, tzEntry *tzentry);
39 static int addToArray(tzEntry **base, int *arraysize, int n,
40 tzEntry *entry, bool override);
41 static int ParseTzFile(const char *filename, int depth,
42 tzEntry **base, int *arraysize, int n);
46 * Apply additional validation checks to a tzEntry
48 * Returns TRUE if OK, else false
51 validateTzEntry(tzEntry *tzentry)
56 * Check restrictions imposed by datetkntbl storage format (see
59 if (strlen(tzentry->abbrev) > TOKMAXLEN)
61 GUC_check_errmsg("time zone abbreviation \"%s\" is too long (maximum %d characters) in time zone file \"%s\", line %d",
62 tzentry->abbrev, TOKMAXLEN,
63 tzentry->filename, tzentry->lineno);
68 * Sanity-check the offset: shouldn't exceed 14 hours
70 if (tzentry->offset > 14 * 60 * 60 ||
71 tzentry->offset < -14 * 60 * 60)
73 GUC_check_errmsg("time zone offset %d is out of range in time zone file \"%s\", line %d",
75 tzentry->filename, tzentry->lineno);
80 * Convert abbrev to lowercase (must match datetime.c's conversion)
82 for (p = (unsigned char *) tzentry->abbrev; *p; p++)
89 * Attempt to parse the line as a timezone abbrev spec
95 * Returns TRUE if OK, else false; data is stored in *tzentry
98 splitTzLine(const char *filename, int lineno, char *line, tzEntry *tzentry)
106 tzentry->lineno = lineno;
107 tzentry->filename = filename;
109 abbrev = strtok(line, WHITESPACE);
112 GUC_check_errmsg("missing time zone abbreviation in time zone file \"%s\", line %d",
116 tzentry->abbrev = pstrdup(abbrev);
118 offset = strtok(NULL, WHITESPACE);
121 GUC_check_errmsg("missing time zone offset in time zone file \"%s\", line %d",
126 /* We assume zone names don't begin with a digit or sign */
127 if (isdigit((unsigned char) *offset) || *offset == '+' || *offset == '-')
129 tzentry->zone = NULL;
130 tzentry->offset = strtol(offset, &offset_endptr, 10);
131 if (offset_endptr == offset || *offset_endptr != '\0')
133 GUC_check_errmsg("invalid number for time zone offset in time zone file \"%s\", line %d",
138 is_dst = strtok(NULL, WHITESPACE);
139 if (is_dst && pg_strcasecmp(is_dst, "D") == 0)
141 tzentry->is_dst = true;
142 remain = strtok(NULL, WHITESPACE);
146 /* there was no 'D' dst specifier */
147 tzentry->is_dst = false;
154 * Assume entry is a zone name. We do not try to validate it by
155 * looking up the zone, because that would force loading of a lot of
156 * zones that probably will never be used in the current session.
158 tzentry->zone = pstrdup(offset);
160 tzentry->is_dst = false;
161 remain = strtok(NULL, WHITESPACE);
164 if (!remain) /* no more non-whitespace chars */
167 if (remain[0] != '#') /* must be a comment */
169 GUC_check_errmsg("invalid syntax in time zone file \"%s\", line %d",
177 * Insert entry into sorted array
179 * *base: base address of array (changeable if must enlarge array)
180 * *arraysize: allocated length of array (changeable if must enlarge array)
181 * n: current number of valid elements in array
182 * entry: new data to insert
183 * override: TRUE if OK to override
185 * Returns the new array length (new value for n), or -1 if error
188 addToArray(tzEntry **base, int *arraysize, int n,
189 tzEntry *entry, bool override)
196 * Search the array for a duplicate; as a useful side effect, the array is
197 * maintained in sorted order. We use strcmp() to ensure we match the
198 * sort order datetime.c expects.
205 int mid = (low + high) >> 1;
206 tzEntry *midptr = arrayptr + mid;
209 cmp = strcmp(entry->abbrev, midptr->abbrev);
217 * Found a duplicate entry; complain unless it's the same.
219 if ((midptr->zone == NULL && entry->zone == NULL &&
220 midptr->offset == entry->offset &&
221 midptr->is_dst == entry->is_dst) ||
222 (midptr->zone != NULL && entry->zone != NULL &&
223 strcmp(midptr->zone, entry->zone) == 0))
225 /* return unchanged array */
230 /* same abbrev but something is different, override */
231 midptr->zone = entry->zone;
232 midptr->offset = entry->offset;
233 midptr->is_dst = entry->is_dst;
236 /* same abbrev but something is different, complain */
237 GUC_check_errmsg("time zone abbreviation \"%s\" is multiply defined",
239 GUC_check_errdetail("Entry in time zone file \"%s\", line %d, conflicts with entry in file \"%s\", line %d.",
240 midptr->filename, midptr->lineno,
241 entry->filename, entry->lineno);
247 * No match, insert at position "low".
252 *base = (tzEntry *) repalloc(*base, *arraysize * sizeof(tzEntry));
255 arrayptr = *base + low;
257 memmove(arrayptr + 1, arrayptr, (n - low) * sizeof(tzEntry));
259 memcpy(arrayptr, entry, sizeof(tzEntry));
265 * Parse a single timezone abbrev file --- can recurse to handle @INCLUDE
267 * filename: user-specified file name (does not include path)
268 * depth: current recursion depth
269 * *base: array for results (changeable if must enlarge array)
270 * *arraysize: allocated length of array (changeable if must enlarge array)
271 * n: current number of valid elements in array
273 * Returns the new array length (new value for n), or -1 if error
276 ParseTzFile(const char *filename, int depth,
277 tzEntry **base, int *arraysize, int n)
279 char share_path[MAXPGPATH];
280 char file_path[MAXPGPATH];
286 bool override = false;
290 * We enforce that the filename is all alpha characters. This may be
291 * overly restrictive, but we don't want to allow access to anything
292 * outside the timezonesets directory, so for instance '/' *must* be
295 for (p = filename; *p; p++)
297 if (!isalpha((unsigned char) *p))
299 /* at level 0, just use guc.c's regular "invalid value" message */
301 GUC_check_errmsg("invalid time zone file name \"%s\"",
308 * The maximal recursion depth is a pretty arbitrary setting. It is hard
309 * to imagine that someone needs more than 3 levels so stick with this
310 * conservative setting until someone complains.
314 GUC_check_errmsg("time zone file recursion limit exceeded in file \"%s\"",
319 get_share_path(my_exec_path, share_path);
320 snprintf(file_path, sizeof(file_path), "%s/timezonesets/%s",
321 share_path, filename);
322 tzFile = AllocateFile(file_path, "r");
326 * Check to see if the problem is not the filename but the directory.
327 * This is worth troubling over because if the installation share/
328 * directory is missing or unreadable, this is likely to be the first
329 * place we notice a problem during postmaster startup.
331 int save_errno = errno;
334 snprintf(file_path, sizeof(file_path), "%s/timezonesets",
336 tzdir = AllocateDir(file_path);
339 GUC_check_errmsg("could not open directory \"%s\": %m",
341 GUC_check_errhint("This may indicate an incomplete PostgreSQL installation, or that the file \"%s\" has been moved away from its proper location.",
349 * otherwise, if file doesn't exist and it's level 0, guc.c's
350 * complaint is enough
352 if (errno != ENOENT || depth > 0)
353 GUC_check_errmsg("could not read time zone file \"%s\": %m",
359 while (!feof(tzFile))
362 if (fgets(tzbuf, sizeof(tzbuf), tzFile) == NULL)
366 GUC_check_errmsg("could not read time zone file \"%s\": %m",
370 /* else we're at EOF after all */
373 if (strlen(tzbuf) == sizeof(tzbuf) - 1)
375 /* the line is too long for tzbuf */
376 GUC_check_errmsg("line is too long in time zone file \"%s\", line %d",
381 /* skip over whitespace */
383 while (*line && isspace((unsigned char) *line))
386 if (*line == '\0') /* empty line */
388 if (*line == '#') /* comment line */
391 if (pg_strncasecmp(line, "@INCLUDE", strlen("@INCLUDE")) == 0)
393 /* pstrdup so we can use filename in result data structure */
394 char *includeFile = pstrdup(line + strlen("@INCLUDE"));
396 includeFile = strtok(includeFile, WHITESPACE);
397 if (!includeFile || !*includeFile)
399 GUC_check_errmsg("@INCLUDE without file name in time zone file \"%s\", line %d",
403 n = ParseTzFile(includeFile, depth + 1,
410 if (pg_strncasecmp(line, "@OVERRIDE", strlen("@OVERRIDE")) == 0)
416 if (!splitTzLine(filename, lineno, line, &tzentry))
418 if (!validateTzEntry(&tzentry))
420 n = addToArray(base, arraysize, n, &tzentry, override);
431 * load_tzoffsets --- read and parse the specified timezone offset file
433 * On success, return a filled-in TimeZoneAbbrevTable, which must have been
434 * malloc'd not palloc'd. On failure, return NULL, using GUC_check_errmsg
435 * and friends to give details of the problem.
437 TimeZoneAbbrevTable *
438 load_tzoffsets(const char *filename)
440 TimeZoneAbbrevTable *result = NULL;
441 MemoryContext tmpContext;
442 MemoryContext oldContext;
448 * Create a temp memory context to work in. This makes it easy to clean
451 tmpContext = AllocSetContextCreate(CurrentMemoryContext,
453 ALLOCSET_SMALL_MINSIZE,
454 ALLOCSET_SMALL_INITSIZE,
455 ALLOCSET_SMALL_MAXSIZE);
456 oldContext = MemoryContextSwitchTo(tmpContext);
458 /* Initialize array at a reasonable size */
460 array = (tzEntry *) palloc(arraysize * sizeof(tzEntry));
462 /* Parse the file(s) */
463 n = ParseTzFile(filename, 0, &array, &arraysize, 0);
465 /* If no errors so far, let datetime.c allocate memory & convert format */
468 result = ConvertTimeZoneAbbrevs(array, n);
470 GUC_check_errmsg("out of memory");
474 MemoryContextSwitchTo(oldContext);
475 MemoryContextDelete(tmpContext);