/* Return the mmap()ed tzfile if found, else NULL. On success, the * length of the mapped data is placed in *length. */ static char *map_tzfile(const char *timezone, size_t *length) { char fname[PATH_MAX]; struct stat st; char *p; int fd; if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) { return NULL; } if (system_location_table) { const struct location_info *li; if ((li = find_zone_info(system_location_table, timezone)) != NULL) { /* Use the stored name to avoid case issue */ timezone = li->name; } } snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone); fd = open(fname, O_RDONLY); if (fd == -1) { return NULL; } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) { close(fd); return NULL; } *length = st.st_size; p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); close(fd); return p != MAP_FAILED ? p : NULL; }
int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb) { const unsigned char *tzf; #ifdef HAVE_SYSTEM_TZDATA if (tzdb == timezonedb_system) { char fname[PATH_MAX]; struct stat st; if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) { return 0; } if (system_location_table) { if (find_zone_info(system_location_table, timezone) != NULL) { /* found in cache */ return 1; } } snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone); return stat(fname, &st) == 0 && is_valid_tzfile(&st); } #endif return (inmem_seek_to_tz_position(&tzf, timezone, tzdb)); }
/* Return the mmap()ed tzfile if found, else NULL. On success, the * length of the mapped data is placed in *length. */ static char *map_tzfile(const char *timezone, size_t *length) { char fname[PATH_MAX]; struct stat st; char *p; int fd; if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) { return NULL; } snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone); fd = open(fname, O_RDONLY); if (fd == -1) { return NULL; } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) { close(fd); return NULL; } *length = st.st_size; p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); close(fd); return p != MAP_FAILED ? p : NULL; }