Blame locale/findlocale.c

Packit 6c4009
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#ifdef _POSIX_MAPPED_FILES
Packit 6c4009
# include <sys/mman.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include "localeinfo.h"
Packit 6c4009
#include "../iconv/gconv_charset.h"
Packit 6c4009
#include "../iconv/gconv_int.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef NL_CURRENT_INDIRECT
Packit 6c4009
# define DEFINE_CATEGORY(category, category_name, items, a) \
Packit 6c4009
extern struct __locale_data _nl_C_##category; \
Packit 6c4009
weak_extern (_nl_C_##category)
Packit 6c4009
# include "categories.def"
Packit 6c4009
# undef	DEFINE_CATEGORY
Packit 6c4009
Packit 6c4009
/* Array indexed by category of pointers to _nl_C_CATEGORY slots.
Packit 6c4009
   Elements are zero for categories whose data is never used.  */
Packit 6c4009
struct __locale_data *const _nl_C[] attribute_hidden =
Packit 6c4009
  {
Packit 6c4009
# define DEFINE_CATEGORY(category, category_name, items, a) \
Packit 6c4009
    [category] = &_nl_C_##category,
Packit 6c4009
# include "categories.def"
Packit 6c4009
# undef	DEFINE_CATEGORY
Packit 6c4009
  };
Packit 6c4009
#else
Packit 6c4009
# define _nl_C		(_nl_C_locobj.__locales)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* For each category we keep a list of records for the locale files
Packit 6c4009
   which are somehow addressed.  */
Packit 6c4009
struct loaded_l10nfile *_nl_locale_file_list[__LC_LAST];
Packit 6c4009
Packit 6c4009
const char _nl_default_locale_path[] attribute_hidden = COMPLOCALEDIR;
Packit 6c4009
Packit 6c4009
/* Checks if the name is actually present, that is, not NULL and not
Packit 6c4009
   empty.  */
Packit 6c4009
static inline int
Packit 6c4009
name_present (const char *name)
Packit 6c4009
{
Packit 6c4009
  return name != NULL && name[0] != '\0';
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Checks that the locale name neither extremely long, nor contains a
Packit 6c4009
   ".." path component (to prevent directory traversal).  */
Packit 6c4009
static inline int
Packit 6c4009
valid_locale_name (const char *name)
Packit 6c4009
{
Packit 6c4009
  /* Not set.  */
Packit 6c4009
  size_t namelen = strlen (name);
Packit 6c4009
  /* Name too long.  The limit is arbitrary and prevents stack overflow
Packit 6c4009
     issues later.  */
Packit 6c4009
  if (__glibc_unlikely (namelen > 255))
Packit 6c4009
    return 0;
Packit 6c4009
  /* Directory traversal attempt.  */
Packit 6c4009
  static const char slashdot[4] = {'/', '.', '.', '/'};
Packit 6c4009
  if (__glibc_unlikely (__memmem (name, namelen,
Packit 6c4009
				  slashdot, sizeof (slashdot)) != NULL))
Packit 6c4009
    return 0;
Packit 6c4009
  if (namelen == 2 && __glibc_unlikely (name[0] == '.' && name [1] == '.'))
Packit 6c4009
    return 0;
Packit 6c4009
  if (namelen >= 3
Packit 6c4009
      && __glibc_unlikely (((name[0] == '.'
Packit 6c4009
			     && name[1] == '.'
Packit 6c4009
			     && name[2] == '/')
Packit 6c4009
			    || (name[namelen - 3] == '/'
Packit 6c4009
				&& name[namelen - 2] == '.'
Packit 6c4009
				&& name[namelen - 1] == '.'))))
Packit 6c4009
    return 0;
Packit 6c4009
  /* If there is a slash in the name, it must start with one.  */
Packit 6c4009
  if (__glibc_unlikely (memchr (name, '/', namelen) != NULL) && name[0] != '/')
Packit 6c4009
    return 0;
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
struct __locale_data *
Packit 6c4009
_nl_find_locale (const char *locale_path, size_t locale_path_len,
Packit 6c4009
		 int category, const char **name)
Packit 6c4009
{
Packit 6c4009
  int mask;
Packit 6c4009
  /* Name of the locale for this category.  */
Packit 6c4009
  const char *cloc_name = *name;
Packit 6c4009
  const char *language;
Packit 6c4009
  const char *modifier;
Packit 6c4009
  const char *territory;
Packit 6c4009
  const char *codeset;
Packit 6c4009
  const char *normalized_codeset;
Packit 6c4009
  struct loaded_l10nfile *locale_file;
Packit 6c4009
Packit 6c4009
  if (cloc_name[0] == '\0')
Packit 6c4009
    {
Packit 6c4009
      /* The user decides which locale to use by setting environment
Packit 6c4009
	 variables.  */
Packit 6c4009
      cloc_name = getenv ("LC_ALL");
Packit 6c4009
      if (!name_present (cloc_name))
Packit 6c4009
	cloc_name = getenv (_nl_category_names.str
Packit 6c4009
			    + _nl_category_name_idxs[category]);
Packit 6c4009
      if (!name_present (cloc_name))
Packit 6c4009
	cloc_name = getenv ("LANG");
Packit 6c4009
      if (!name_present (cloc_name))
Packit 6c4009
	cloc_name = _nl_C_name;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We used to fall back to the C locale if the name contains a slash
Packit 6c4009
     character '/', but we now check for directory traversal in
Packit 6c4009
     valid_locale_name, so this is no longer necessary.  */
Packit 6c4009
Packit 6c4009
  if (__builtin_expect (strcmp (cloc_name, _nl_C_name), 1) == 0
Packit 6c4009
      || __builtin_expect (strcmp (cloc_name, _nl_POSIX_name), 1) == 0)
Packit 6c4009
    {
Packit 6c4009
      /* We need not load anything.  The needed data is contained in
Packit 6c4009
	 the library itself.  */
Packit 6c4009
      *name = _nl_C_name;
Packit 6c4009
      return _nl_C[category];
Packit 6c4009
    }
Packit 6c4009
  else if (!valid_locale_name (cloc_name))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  *name = cloc_name;
Packit 6c4009
Packit 6c4009
  /* We really have to load some data.  First we try the archive,
Packit 6c4009
     but only if there was no LOCPATH environment variable specified.  */
Packit 6c4009
  if (__glibc_likely (locale_path == NULL))
Packit 6c4009
    {
Packit 6c4009
      struct __locale_data *data
Packit 6c4009
	= _nl_load_locale_from_archive (category, name);
Packit 6c4009
      if (__glibc_likely (data != NULL))
Packit 6c4009
	return data;
Packit 6c4009
Packit 6c4009
      /* Nothing in the archive with the given name.  Expanding it as
Packit 6c4009
	 an alias and retry.  */
Packit 6c4009
      cloc_name = _nl_expand_alias (*name);
Packit 6c4009
      if (cloc_name != NULL)
Packit 6c4009
	{
Packit 6c4009
	  data = _nl_load_locale_from_archive (category, &cloc_name);
Packit 6c4009
	  if (__builtin_expect (data != NULL, 1))
Packit 6c4009
	    return data;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Nothing in the archive.  Set the default path to search below.  */
Packit 6c4009
      locale_path = _nl_default_locale_path;
Packit 6c4009
      locale_path_len = sizeof _nl_default_locale_path;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    /* We really have to load some data.  First see whether the name is
Packit 6c4009
       an alias.  Please note that this makes it impossible to have "C"
Packit 6c4009
       or "POSIX" as aliases.  */
Packit 6c4009
    cloc_name = _nl_expand_alias (*name);
Packit 6c4009
Packit 6c4009
  if (cloc_name == NULL)
Packit 6c4009
    /* It is no alias.  */
Packit 6c4009
    cloc_name = *name;
Packit 6c4009
Packit 6c4009
  /* Make a writable copy of the locale name.  */
Packit 6c4009
  char *loc_name = strdupa (cloc_name);
Packit 6c4009
Packit 6c4009
  /* LOCALE can consist of up to four recognized parts for the XPG syntax:
Packit 6c4009
Packit 6c4009
		language[_territory[.codeset]][@modifier]
Packit 6c4009
Packit 6c4009
     Beside the first all of them are allowed to be missing.  If the
Packit 6c4009
     full specified locale is not found, the less specific one are
Packit 6c4009
     looked for.  The various part will be stripped off according to
Packit 6c4009
     the following order:
Packit 6c4009
		(1) codeset
Packit 6c4009
		(2) normalized codeset
Packit 6c4009
		(3) territory
Packit 6c4009
		(4) modifier
Packit 6c4009
   */
Packit 6c4009
  mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
Packit 6c4009
			   &codeset, &normalized_codeset);
Packit 6c4009
  if (mask == -1)
Packit 6c4009
    /* Memory allocate problem.  */
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  /* If exactly this locale was already asked for we have an entry with
Packit 6c4009
     the complete name.  */
Packit 6c4009
  locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
Packit 6c4009
				    locale_path, locale_path_len, mask,
Packit 6c4009
				    language, territory, codeset,
Packit 6c4009
				    normalized_codeset, modifier,
Packit 6c4009
				    _nl_category_names.str
Packit 6c4009
				    + _nl_category_name_idxs[category], 0);
Packit 6c4009
Packit 6c4009
  if (locale_file == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Find status record for addressed locale file.  We have to search
Packit 6c4009
	 through all directories in the locale path.  */
Packit 6c4009
      locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
Packit 6c4009
					locale_path, locale_path_len, mask,
Packit 6c4009
					language, territory, codeset,
Packit 6c4009
					normalized_codeset, modifier,
Packit 6c4009
					_nl_category_names.str
Packit 6c4009
					+ _nl_category_name_idxs[category], 1);
Packit 6c4009
      if (locale_file == NULL)
Packit 6c4009
	/* This means we are out of core.  */
Packit 6c4009
	return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The space for normalized_codeset is dynamically allocated.  Free it.  */
Packit 6c4009
  if (mask & XPG_NORM_CODESET)
Packit 6c4009
    free ((void *) normalized_codeset);
Packit 6c4009
Packit 6c4009
  if (locale_file->decided == 0)
Packit 6c4009
    _nl_load_locale (locale_file, category);
Packit 6c4009
Packit 6c4009
  if (locale_file->data == NULL)
Packit 6c4009
    {
Packit 6c4009
      int cnt;
Packit 6c4009
      for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
Packit 6c4009
	{
Packit 6c4009
	  if (locale_file->successor[cnt]->decided == 0)
Packit 6c4009
	    _nl_load_locale (locale_file->successor[cnt], category);
Packit 6c4009
	  if (locale_file->successor[cnt]->data != NULL)
Packit 6c4009
	    break;
Packit 6c4009
	}
Packit 6c4009
      /* Move the entry we found (or NULL) to the first place of
Packit 6c4009
	 successors.  */
Packit 6c4009
      locale_file->successor[0] = locale_file->successor[cnt];
Packit 6c4009
      locale_file = locale_file->successor[cnt];
Packit 6c4009
Packit 6c4009
      if (locale_file == NULL)
Packit 6c4009
	return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The LC_CTYPE category allows to check whether a locale is really
Packit 6c4009
     usable.  If the locale name contains a charset name and the
Packit 6c4009
     charset name used in the locale (present in the LC_CTYPE data) is
Packit 6c4009
     not the same (after resolving aliases etc) we reject the locale
Packit 6c4009
     since using it would irritate users expecting the charset named
Packit 6c4009
     in the locale name.  */
Packit 6c4009
  if (codeset != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Get the codeset information from the locale file.  */
Packit 6c4009
      static const int codeset_idx[] =
Packit 6c4009
	{
Packit 6c4009
	  [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
Packit 6c4009
	  [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
Packit 6c4009
	  [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
Packit 6c4009
	  [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
Packit 6c4009
	  [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
Packit 6c4009
	  [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
Packit 6c4009
	  [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
Packit 6c4009
	  [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
Packit 6c4009
	  [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
Packit 6c4009
	  [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
Packit 6c4009
	  [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
Packit 6c4009
	  [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
Packit 6c4009
	};
Packit 6c4009
      const struct __locale_data *data;
Packit 6c4009
      const char *locale_codeset;
Packit 6c4009
      char *clocale_codeset;
Packit 6c4009
      char *ccodeset;
Packit 6c4009
Packit 6c4009
      data = (const struct __locale_data *) locale_file->data;
Packit 6c4009
      locale_codeset =
Packit 6c4009
	(const char *) data->values[codeset_idx[category]].string;
Packit 6c4009
      assert (locale_codeset != NULL);
Packit 6c4009
      /* Note the length of the allocated memory: +3 for up to two slashes
Packit 6c4009
	 and the NUL byte.  */
Packit 6c4009
      clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
Packit 6c4009
      strip (clocale_codeset, locale_codeset);
Packit 6c4009
Packit 6c4009
      ccodeset = (char *) alloca (strlen (codeset) + 3);
Packit 6c4009
      strip (ccodeset, codeset);
Packit 6c4009
Packit 6c4009
      if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
Packit 6c4009
				 upstr (clocale_codeset,
Packit 6c4009
					clocale_codeset)) != 0)
Packit 6c4009
	/* The codesets are not identical, don't use the locale.  */
Packit 6c4009
	return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Determine the locale name for which loading succeeded.  This
Packit 6c4009
     information comes from the file name.  The form is
Packit 6c4009
     <path>/<locale>/LC_foo.  We must extract the <locale> part.  */
Packit 6c4009
  if (((const struct __locale_data *) locale_file->data)->name == NULL)
Packit 6c4009
    {
Packit 6c4009
      char *cp, *endp;
Packit 6c4009
Packit 6c4009
      endp = strrchr (locale_file->filename, '/');
Packit 6c4009
      cp = endp - 1;
Packit 6c4009
      while (cp[-1] != '/')
Packit 6c4009
	--cp;
Packit 6c4009
      ((struct __locale_data *) locale_file->data)->name
Packit 6c4009
	= __strndup (cp, endp - cp);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Determine whether the user wants transliteration or not.  */
Packit 6c4009
  if (modifier != NULL
Packit 6c4009
      && __strcasecmp_l (modifier, "TRANSLIT", _nl_C_locobj_ptr) == 0)
Packit 6c4009
    ((struct __locale_data *) locale_file->data)->use_translit = 1;
Packit 6c4009
Packit 6c4009
  /* Increment the usage count.  */
Packit 6c4009
  if (((const struct __locale_data *) locale_file->data)->usage_count
Packit 6c4009
      < MAX_USAGE_COUNT)
Packit 6c4009
    ++((struct __locale_data *) locale_file->data)->usage_count;
Packit 6c4009
Packit 6c4009
  return (struct __locale_data *) locale_file->data;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Calling this function assumes the lock for handling global locale data
Packit 6c4009
   is acquired.  */
Packit 6c4009
void
Packit 6c4009
_nl_remove_locale (int locale, struct __locale_data *data)
Packit 6c4009
{
Packit 6c4009
  if (--data->usage_count == 0)
Packit 6c4009
    {
Packit 6c4009
      if (data->alloc != ld_archive)
Packit 6c4009
	{
Packit 6c4009
	  /* First search the entry in the list of loaded files.  */
Packit 6c4009
	  struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
Packit 6c4009
Packit 6c4009
	  /* Search for the entry.  It must be in the list.  Otherwise it
Packit 6c4009
	     is a bug and we crash badly.  */
Packit 6c4009
	  while ((struct __locale_data *) ptr->data != data)
Packit 6c4009
	    ptr = ptr->next;
Packit 6c4009
Packit 6c4009
	  /* Mark the data as not available anymore.  So when the data has
Packit 6c4009
	     to be used again it is reloaded.  */
Packit 6c4009
	  ptr->decided = 0;
Packit 6c4009
	  ptr->data = NULL;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* This does the real work.  */
Packit 6c4009
      _nl_unload_locale (data);
Packit 6c4009
    }
Packit 6c4009
}