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-2014, 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);
66 if (tzentry->offset % 900 != 0)
68 GUC_check_errmsg("time zone offset %d is not a multiple of 900 sec (15 min) in time zone file \"%s\", line %d",
70 tzentry->filename, tzentry->lineno);
75 * Sanity-check the offset: shouldn't exceed 14 hours
77 if (tzentry->offset > 14 * 60 * 60 ||
78 tzentry->offset < -14 * 60 * 60)
80 GUC_check_errmsg("time zone offset %d is out of range in time zone file \"%s\", line %d",
82 tzentry->filename, tzentry->lineno);
87 * Convert abbrev to lowercase (must match datetime.c's conversion)
89 for (p = (unsigned char *) tzentry->abbrev; *p; p++)
96 * Attempt to parse the line as a timezone abbrev spec (name, offset, dst)
98 * Returns TRUE if OK, else false; data is stored in *tzentry
101 splitTzLine(const char *filename, int lineno, char *line, tzEntry *tzentry)
109 tzentry->lineno = lineno;
110 tzentry->filename = filename;
112 abbrev = strtok(line, WHITESPACE);
115 GUC_check_errmsg("missing time zone abbreviation in time zone file \"%s\", line %d",
119 tzentry->abbrev = abbrev;
121 offset = strtok(NULL, WHITESPACE);
124 GUC_check_errmsg("missing time zone offset in time zone file \"%s\", line %d",
128 tzentry->offset = strtol(offset, &offset_endptr, 10);
129 if (offset_endptr == offset || *offset_endptr != '\0')
131 GUC_check_errmsg("invalid number for time zone offset in time zone file \"%s\", line %d",
136 is_dst = strtok(NULL, WHITESPACE);
137 if (is_dst && pg_strcasecmp(is_dst, "D") == 0)
139 tzentry->is_dst = true;
140 remain = strtok(NULL, WHITESPACE);
144 /* there was no 'D' dst specifier */
145 tzentry->is_dst = false;
149 if (!remain) /* no more non-whitespace chars */
152 if (remain[0] != '#') /* must be a comment */
154 GUC_check_errmsg("invalid syntax in time zone file \"%s\", line %d",
162 * Insert entry into sorted array
164 * *base: base address of array (changeable if must enlarge array)
165 * *arraysize: allocated length of array (changeable if must enlarge array)
166 * n: current number of valid elements in array
167 * entry: new data to insert
168 * override: TRUE if OK to override
170 * Returns the new array length (new value for n), or -1 if error
173 addToArray(tzEntry **base, int *arraysize, int n,
174 tzEntry *entry, bool override)
181 * Search the array for a duplicate; as a useful side effect, the array is
182 * maintained in sorted order. We use strcmp() to ensure we match the
183 * sort order datetime.c expects.
190 int mid = (low + high) >> 1;
191 tzEntry *midptr = arrayptr + mid;
194 cmp = strcmp(entry->abbrev, midptr->abbrev);
202 * Found a duplicate entry; complain unless it's the same.
204 if (midptr->offset == entry->offset &&
205 midptr->is_dst == entry->is_dst)
207 /* return unchanged array */
212 /* same abbrev but something is different, override */
213 midptr->offset = entry->offset;
214 midptr->is_dst = entry->is_dst;
217 /* same abbrev but something is different, complain */
218 GUC_check_errmsg("time zone abbreviation \"%s\" is multiply defined",
220 GUC_check_errdetail("Entry in time zone file \"%s\", line %d, conflicts with entry in file \"%s\", line %d.",
221 midptr->filename, midptr->lineno,
222 entry->filename, entry->lineno);
228 * No match, insert at position "low".
233 *base = (tzEntry *) repalloc(*base, *arraysize * sizeof(tzEntry));
236 arrayptr = *base + low;
238 memmove(arrayptr + 1, arrayptr, (n - low) * sizeof(tzEntry));
240 memcpy(arrayptr, entry, sizeof(tzEntry));
242 /* Must dup the abbrev to ensure it survives */
243 arrayptr->abbrev = pstrdup(entry->abbrev);
249 * Parse a single timezone abbrev file --- can recurse to handle @INCLUDE
251 * filename: user-specified file name (does not include path)
252 * depth: current recursion depth
253 * *base: array for results (changeable if must enlarge array)
254 * *arraysize: allocated length of array (changeable if must enlarge array)
255 * n: current number of valid elements in array
257 * Returns the new array length (new value for n), or -1 if error
260 ParseTzFile(const char *filename, int depth,
261 tzEntry **base, int *arraysize, int n)
263 char share_path[MAXPGPATH];
264 char file_path[MAXPGPATH];
270 bool override = false;
274 * We enforce that the filename is all alpha characters. This may be
275 * overly restrictive, but we don't want to allow access to anything
276 * outside the timezonesets directory, so for instance '/' *must* be
279 for (p = filename; *p; p++)
281 if (!isalpha((unsigned char) *p))
283 /* at level 0, just use guc.c's regular "invalid value" message */
285 GUC_check_errmsg("invalid time zone file name \"%s\"",
292 * The maximal recursion depth is a pretty arbitrary setting. It is hard
293 * to imagine that someone needs more than 3 levels so stick with this
294 * conservative setting until someone complains.
298 GUC_check_errmsg("time zone file recursion limit exceeded in file \"%s\"",
303 get_share_path(my_exec_path, share_path);
304 snprintf(file_path, sizeof(file_path), "%s/timezonesets/%s",
305 share_path, filename);
306 tzFile = AllocateFile(file_path, "r");
310 * Check to see if the problem is not the filename but the directory.
311 * This is worth troubling over because if the installation share/
312 * directory is missing or unreadable, this is likely to be the first
313 * place we notice a problem during postmaster startup.
315 int save_errno = errno;
318 snprintf(file_path, sizeof(file_path), "%s/timezonesets",
320 tzdir = AllocateDir(file_path);
323 GUC_check_errmsg("could not open directory \"%s\": %m",
325 GUC_check_errhint("This may indicate an incomplete PostgreSQL installation, or that the file \"%s\" has been moved away from its proper location.",
333 * otherwise, if file doesn't exist and it's level 0, guc.c's
334 * complaint is enough
336 if (errno != ENOENT || depth > 0)
337 GUC_check_errmsg("could not read time zone file \"%s\": %m",
343 while (!feof(tzFile))
346 if (fgets(tzbuf, sizeof(tzbuf), tzFile) == NULL)
350 GUC_check_errmsg("could not read time zone file \"%s\": %m",
354 /* else we're at EOF after all */
357 if (strlen(tzbuf) == sizeof(tzbuf) - 1)
359 /* the line is too long for tzbuf */
360 GUC_check_errmsg("line is too long in time zone file \"%s\", line %d",
365 /* skip over whitespace */
367 while (*line && isspace((unsigned char) *line))
370 if (*line == '\0') /* empty line */
372 if (*line == '#') /* comment line */
375 if (pg_strncasecmp(line, "@INCLUDE", strlen("@INCLUDE")) == 0)
377 /* pstrdup so we can use filename in result data structure */
378 char *includeFile = pstrdup(line + strlen("@INCLUDE"));
380 includeFile = strtok(includeFile, WHITESPACE);
381 if (!includeFile || !*includeFile)
383 GUC_check_errmsg("@INCLUDE without file name in time zone file \"%s\", line %d",
387 n = ParseTzFile(includeFile, depth + 1,
394 if (pg_strncasecmp(line, "@OVERRIDE", strlen("@OVERRIDE")) == 0)
400 if (!splitTzLine(filename, lineno, line, &tzentry))
402 if (!validateTzEntry(&tzentry))
404 n = addToArray(base, arraysize, n, &tzentry, override);
415 * load_tzoffsets --- read and parse the specified timezone offset file
417 * On success, return a filled-in TimeZoneAbbrevTable, which must have been
418 * malloc'd not palloc'd. On failure, return NULL, using GUC_check_errmsg
419 * and friends to give details of the problem.
421 TimeZoneAbbrevTable *
422 load_tzoffsets(const char *filename)
424 TimeZoneAbbrevTable *result = NULL;
425 MemoryContext tmpContext;
426 MemoryContext oldContext;
432 * Create a temp memory context to work in. This makes it easy to clean
435 tmpContext = AllocSetContextCreate(CurrentMemoryContext,
437 ALLOCSET_SMALL_MINSIZE,
438 ALLOCSET_SMALL_INITSIZE,
439 ALLOCSET_SMALL_MAXSIZE);
440 oldContext = MemoryContextSwitchTo(tmpContext);
442 /* Initialize array at a reasonable size */
444 array = (tzEntry *) palloc(arraysize * sizeof(tzEntry));
446 /* Parse the file(s) */
447 n = ParseTzFile(filename, 0, &array, &arraysize, 0);
449 /* If no errors so far, allocate result and let datetime.c convert data */
452 result = malloc(offsetof(TimeZoneAbbrevTable, abbrevs) +
453 n * sizeof(datetkn));
455 GUC_check_errmsg("out of memory");
457 ConvertTimeZoneAbbrevs(result, array, n);
461 MemoryContextSwitchTo(oldContext);
462 MemoryContextDelete(tmpContext);