1/* Copyright (C) 1996-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <assert.h>
20#include <errno.h>
21#include <locale.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#ifdef _POSIX_MAPPED_FILES
26# include <sys/mman.h>
27#endif
28
29#include "localeinfo.h"
30#include "../iconv/gconv_charset.h"
31#include "../iconv/gconv_int.h"
32
33
34#ifdef NL_CURRENT_INDIRECT
35# define DEFINE_CATEGORY(category, category_name, items, a) \
36extern struct __locale_data _nl_C_##category; \
37weak_extern (_nl_C_##category)
38# include "categories.def"
39# undef DEFINE_CATEGORY
40
41/* Array indexed by category of pointers to _nl_C_CATEGORY slots.
42 Elements are zero for categories whose data is never used. */
43struct __locale_data *const _nl_C[] attribute_hidden =
44 {
45# define DEFINE_CATEGORY(category, category_name, items, a) \
46 [category] = &_nl_C_##category,
47# include "categories.def"
48# undef DEFINE_CATEGORY
49 };
50#else
51# define _nl_C (_nl_C_locobj.__locales)
52#endif
53
54
55/* For each category we keep a list of records for the locale files
56 which are somehow addressed. */
57struct loaded_l10nfile *_nl_locale_file_list[__LC_LAST];
58
59const char _nl_default_locale_path[] attribute_hidden = COMPLOCALEDIR;
60
61/* Checks if the name is actually present, that is, not NULL and not
62 empty. */
63static inline int
64name_present (const char *name)
65{
66 return name != NULL && name[0] != '\0';
67}
68
69/* Checks that the locale name neither extremely long, nor contains a
70 ".." path component (to prevent directory traversal). */
71static inline int
72valid_locale_name (const char *name)
73{
74 /* Not set. */
75 size_t namelen = strlen (name);
76 /* Name too long. The limit is arbitrary and prevents stack overflow
77 issues later. */
78 if (__glibc_unlikely (namelen > 255))
79 return 0;
80 /* Directory traversal attempt. */
81 static const char slashdot[4] = {'/', '.', '.', '/'};
82 if (__glibc_unlikely (__memmem (name, namelen,
83 slashdot, sizeof (slashdot)) != NULL))
84 return 0;
85 if (namelen == 2 && __glibc_unlikely (name[0] == '.' && name [1] == '.'))
86 return 0;
87 if (namelen >= 3
88 && __glibc_unlikely (((name[0] == '.'
89 && name[1] == '.'
90 && name[2] == '/')
91 || (name[namelen - 3] == '/'
92 && name[namelen - 2] == '.'
93 && name[namelen - 1] == '.'))))
94 return 0;
95 /* If there is a slash in the name, it must start with one. */
96 if (__glibc_unlikely (memchr (name, '/', namelen) != NULL) && name[0] != '/')
97 return 0;
98 return 1;
99}
100
101struct __locale_data *
102_nl_find_locale (const char *locale_path, size_t locale_path_len,
103 int category, const char **name)
104{
105 int mask;
106 /* Name of the locale for this category. */
107 const char *cloc_name = *name;
108 const char *language;
109 const char *modifier;
110 const char *territory;
111 const char *codeset;
112 const char *normalized_codeset;
113 struct loaded_l10nfile *locale_file;
114
115 if (cloc_name[0] == '\0')
116 {
117 /* The user decides which locale to use by setting environment
118 variables. */
119 cloc_name = getenv ("LC_ALL");
120 if (!name_present (cloc_name))
121 cloc_name = getenv (_nl_category_names_get (category));
122 if (!name_present (cloc_name))
123 cloc_name = getenv ("LANG");
124 if (!name_present (cloc_name))
125 cloc_name = _nl_C_name;
126 }
127
128 /* We used to fall back to the C locale if the name contains a slash
129 character '/', but we now check for directory traversal in
130 valid_locale_name, so this is no longer necessary. */
131
132 if (__builtin_expect (strcmp (cloc_name, _nl_C_name), 1) == 0
133 || __builtin_expect (strcmp (cloc_name, _nl_POSIX_name), 1) == 0)
134 {
135 /* We need not load anything. The needed data is contained in
136 the library itself. */
137 *name = _nl_C_name;
138 return _nl_C[category];
139 }
140 else if (!valid_locale_name (cloc_name))
141 {
142 __set_errno (EINVAL);
143 return NULL;
144 }
145
146 *name = cloc_name;
147
148 /* We really have to load some data. First we try the archive,
149 but only if there was no LOCPATH environment variable specified. */
150 if (__glibc_likely (locale_path == NULL))
151 {
152 struct __locale_data *data
153 = _nl_load_locale_from_archive (category, name);
154 if (__glibc_likely (data != NULL))
155 return data;
156
157 /* Nothing in the archive with the given name. Expanding it as
158 an alias and retry. */
159 cloc_name = _nl_expand_alias (*name);
160 if (cloc_name != NULL)
161 {
162 data = _nl_load_locale_from_archive (category, &cloc_name);
163 if (__builtin_expect (data != NULL, 1))
164 return data;
165 }
166
167 /* Nothing in the archive. Set the default path to search below. */
168 locale_path = _nl_default_locale_path;
169 locale_path_len = sizeof _nl_default_locale_path;
170 }
171 else
172 /* We really have to load some data. First see whether the name is
173 an alias. Please note that this makes it impossible to have "C"
174 or "POSIX" as aliases. */
175 cloc_name = _nl_expand_alias (*name);
176
177 if (cloc_name == NULL)
178 /* It is no alias. */
179 cloc_name = *name;
180
181 /* Make a writable copy of the locale name. */
182 char *loc_name = strdupa (cloc_name);
183
184 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
185
186 language[_territory[.codeset]][@modifier]
187
188 Beside the first all of them are allowed to be missing. If the
189 full specified locale is not found, the less specific one are
190 looked for. The various part will be stripped off according to
191 the following order:
192 (1) codeset
193 (2) normalized codeset
194 (3) territory
195 (4) modifier
196 */
197 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
198 &codeset, &normalized_codeset);
199 if (mask == -1)
200 /* Memory allocate problem. */
201 return NULL;
202
203 /* If exactly this locale was already asked for we have an entry with
204 the complete name. */
205 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
206 locale_path, locale_path_len, mask,
207 language, territory, codeset,
208 normalized_codeset, modifier,
209 _nl_category_names_get (category), 0);
210
211 if (locale_file == NULL)
212 {
213 /* Find status record for addressed locale file. We have to search
214 through all directories in the locale path. */
215 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
216 locale_path, locale_path_len, mask,
217 language, territory, codeset,
218 normalized_codeset, modifier,
219 _nl_category_names_get (category), 1);
220 if (locale_file == NULL)
221 /* This means we are out of core. */
222 return NULL;
223 }
224
225 /* The space for normalized_codeset is dynamically allocated. Free it. */
226 if (mask & XPG_NORM_CODESET)
227 free ((void *) normalized_codeset);
228
229 if (locale_file->decided == 0)
230 _nl_load_locale (locale_file, category);
231
232 if (locale_file->data == NULL)
233 {
234 int cnt;
235 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
236 {
237 if (locale_file->successor[cnt]->decided == 0)
238 _nl_load_locale (locale_file->successor[cnt], category);
239 if (locale_file->successor[cnt]->data != NULL)
240 break;
241 }
242 /* Move the entry we found (or NULL) to the first place of
243 successors. */
244 locale_file->successor[0] = locale_file->successor[cnt];
245 locale_file = locale_file->successor[cnt];
246
247 if (locale_file == NULL)
248 return NULL;
249 }
250
251 /* The LC_CTYPE category allows to check whether a locale is really
252 usable. If the locale name contains a charset name and the
253 charset name used in the locale (present in the LC_CTYPE data) is
254 not the same (after resolving aliases etc) we reject the locale
255 since using it would irritate users expecting the charset named
256 in the locale name. */
257 if (codeset != NULL)
258 {
259 /* Get the codeset information from the locale file. */
260 static const int codeset_idx[] =
261 {
262 [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
263 [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
264 [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
265 [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
266 [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
267 [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
268 [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
269 [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
270 [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
271 [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
272 [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
273 [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
274 };
275 const struct __locale_data *data;
276 const char *locale_codeset;
277 char *clocale_codeset;
278 char *ccodeset;
279
280 data = (const struct __locale_data *) locale_file->data;
281 locale_codeset =
282 (const char *) data->values[codeset_idx[category]].string;
283 assert (locale_codeset != NULL);
284 /* Note the length of the allocated memory: +3 for up to two slashes
285 and the NUL byte. */
286 clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
287 strip (clocale_codeset, locale_codeset);
288
289 ccodeset = (char *) alloca (strlen (codeset) + 3);
290 strip (ccodeset, codeset);
291
292 if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
293 upstr (clocale_codeset,
294 clocale_codeset)) != 0)
295 /* The codesets are not identical, don't use the locale. */
296 return NULL;
297 }
298
299 /* Determine the locale name for which loading succeeded. This
300 information comes from the file name. The form is
301 <path>/<locale>/LC_foo. We must extract the <locale> part. */
302 if (((const struct __locale_data *) locale_file->data)->name == NULL)
303 {
304 char *cp, *endp;
305
306 endp = strrchr (locale_file->filename, '/');
307 cp = endp - 1;
308 while (cp[-1] != '/')
309 --cp;
310 ((struct __locale_data *) locale_file->data)->name
311 = __strndup (cp, endp - cp);
312 }
313
314 /* Determine whether the user wants transliteration or not. */
315 if (modifier != NULL
316 && __strcasecmp_l (modifier, "TRANSLIT", _nl_C_locobj_ptr) == 0)
317 ((struct __locale_data *) locale_file->data)->use_translit = 1;
318
319 /* Increment the usage count. */
320 if (((const struct __locale_data *) locale_file->data)->usage_count
321 < MAX_USAGE_COUNT)
322 ++((struct __locale_data *) locale_file->data)->usage_count;
323
324 return (struct __locale_data *) locale_file->data;
325}
326
327
328/* Calling this function assumes the lock for handling global locale data
329 is acquired. */
330void
331_nl_remove_locale (int locale, struct __locale_data *data)
332{
333 if (--data->usage_count == 0)
334 {
335 if (data->alloc != ld_archive)
336 {
337 /* First search the entry in the list of loaded files. */
338 struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
339
340 /* Search for the entry. It must be in the list. Otherwise it
341 is a bug and we crash badly. */
342 while ((struct __locale_data *) ptr->data != data)
343 ptr = ptr->next;
344
345 /* Mark the data as not available anymore. So when the data has
346 to be used again it is reloaded. */
347 ptr->decided = 0;
348 ptr->data = NULL;
349 }
350
351 /* This does the real work. */
352 _nl_unload_locale (data);
353 }
354}
355