| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <langinfo.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <sys/mman.h> |
| 7 |
#include <sys/stat.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#ifndef __GLIBC__ |
| 11 |
#include "sd-dlopen.h" |
| 12 |
#endif |
| 13 |
|
| 14 |
#include "dirent-util.h" |
| 15 |
#include "dlfcn-util.h" |
| 16 |
#include "env-util.h" |
| 17 |
#include "fd-util.h" |
| 18 |
#include "fileio.h" |
| 19 |
#include "locale-util.h" |
| 20 |
#include "log.h" |
| 21 |
#include "path-util.h" |
| 22 |
#include "process-util.h" |
| 23 |
#include "set.h" |
| 24 |
#include "stat-util.h" |
| 25 |
#include "string-table.h" |
| 26 |
#include "string-util.h" |
| 27 |
#include "strv.h" |
| 28 |
#include "utf8.h" |
| 29 |
|
| 30 |
#ifdef __GLIBC__ |
| 31 |
DLSYM_PROTOTYPE(dgettext) = dgettext; |
| 32 |
#else |
| 33 |
DLSYM_PROTOTYPE(dgettext) = NULL; |
| 34 |
#endif |
| 35 |
|
| 36 |
int dlopen_libintl(int log_level) { |
| 37 |
#ifdef __GLIBC__ |
| 38 |
return 1; |
| 39 |
#else |
| 40 |
static void *libintl_dl = NULL; |
| 41 |
|
| 42 |
LIBINTL_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED); |
| 43 |
|
| 44 |
return dlopen_many_sym_or_warn( |
| 45 |
&libintl_dl, |
| 46 |
"libintl.so.8", |
| 47 |
log_level, |
| 48 |
DLSYM_ARG(dgettext)); |
| 49 |
#endif |
| 50 |
} |
| 51 |
|
| 52 |
static char* normalize_locale(const char *name) { |
| 53 |
const char *e; |
| 54 |
|
| 55 |
/* Locale names are weird: glibc has some magic rules when looking for the charset name on disk: it |
| 56 |
* lowercases everything, and removes most special chars. This means the official .UTF-8 suffix |
| 57 |
* becomes .utf8 when looking things up on disk. When enumerating locales, let's do the reverse |
| 58 |
* operation, and go back to ".UTF-8" which appears to be the more commonly accepted name. We only do |
| 59 |
* that for UTF-8 however, since it's kinda the only charset that matters. */ |
| 60 |
|
| 61 |
e = endswith(name, ".utf8"); |
| 62 |
if (e) { |
| 63 |
_cleanup_free_ char *prefix = NULL; |
| 64 |
|
| 65 |
prefix = strndup(name, e - name); |
| 66 |
if (!prefix) |
| 67 |
return NULL; |
| 68 |
|
| 69 |
return strjoin(prefix, ".UTF-8"); |
| 70 |
} |
| 71 |
|
| 72 |
e = strstr(name, ".utf8@"); |
| 73 |
if (e) { |
| 74 |
_cleanup_free_ char *prefix = NULL; |
| 75 |
|
| 76 |
prefix = strndup(name, e - name); |
| 77 |
if (!prefix) |
| 78 |
return NULL; |
| 79 |
|
| 80 |
return strjoin(prefix, ".UTF-8@", e + 6); |
| 81 |
} |
| 82 |
|
| 83 |
return strdup(name); |
| 84 |
} |
| 85 |
|
| 86 |
static const char* get_locale_dir(void) { |
| 87 |
return secure_getenv("SYSTEMD_LOCALE_DIRECTORY") ?: |
| 88 |
#ifdef __GLIBC__ |
| 89 |
"/usr/lib/locale/"; |
| 90 |
#else |
| 91 |
"/usr/share/i18n/locales/musl/"; |
| 92 |
#endif |
| 93 |
} |
| 94 |
|
| 95 |
#ifdef __GLIBC__ |
| 96 |
static int add_locales_from_archive(Set *locales) { |
| 97 |
/* Stolen from glibc... */ |
| 98 |
|
| 99 |
struct locarhead { |
| 100 |
uint32_t magic; |
| 101 |
/* Serial number. */ |
| 102 |
uint32_t serial; |
| 103 |
/* Name hash table. */ |
| 104 |
uint32_t namehash_offset; |
| 105 |
uint32_t namehash_used; |
| 106 |
uint32_t namehash_size; |
| 107 |
/* String table. */ |
| 108 |
uint32_t string_offset; |
| 109 |
uint32_t string_used; |
| 110 |
uint32_t string_size; |
| 111 |
/* Table with locale records. */ |
| 112 |
uint32_t locrectab_offset; |
| 113 |
uint32_t locrectab_used; |
| 114 |
uint32_t locrectab_size; |
| 115 |
/* MD5 sum hash table. */ |
| 116 |
uint32_t sumhash_offset; |
| 117 |
uint32_t sumhash_used; |
| 118 |
uint32_t sumhash_size; |
| 119 |
}; |
| 120 |
|
| 121 |
struct namehashent { |
| 122 |
/* Hash value of the name. */ |
| 123 |
uint32_t hashval; |
| 124 |
/* Offset of the name in the string table. */ |
| 125 |
uint32_t name_offset; |
| 126 |
/* Offset of the locale record. */ |
| 127 |
uint32_t locrec_offset; |
| 128 |
}; |
| 129 |
|
| 130 |
int r; |
| 131 |
|
| 132 |
assert(locales); |
| 133 |
|
| 134 |
_cleanup_free_ char *locale_archive_file = path_join(get_locale_dir(), "locale-archive"); |
| 135 |
if (!locale_archive_file) |
| 136 |
return -ENOMEM; |
| 137 |
|
| 138 |
_cleanup_close_ int fd = open(locale_archive_file, O_RDONLY|O_NOCTTY|O_CLOEXEC); |
| 139 |
if (fd < 0) |
| 140 |
return errno == ENOENT ? 0 : -errno; |
| 141 |
|
| 142 |
struct stat st; |
| 143 |
if (fstat(fd, &st) < 0) |
| 144 |
return -errno; |
| 145 |
|
| 146 |
r = stat_verify_regular(&st); |
| 147 |
if (r < 0) |
| 148 |
return r; |
| 149 |
|
| 150 |
if (st.st_size < (off_t) sizeof(struct locarhead)) |
| 151 |
return -EBADMSG; |
| 152 |
|
| 153 |
if (file_offset_beyond_memory_size(st.st_size)) |
| 154 |
return -EFBIG; |
| 155 |
|
| 156 |
void *p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 157 |
if (p == MAP_FAILED) |
| 158 |
return -errno; |
| 159 |
|
| 160 |
const struct namehashent *e; |
| 161 |
const struct locarhead *h = p; |
| 162 |
if (h->magic != 0xde020109 || |
| 163 |
h->namehash_offset + h->namehash_size > st.st_size || |
| 164 |
h->string_offset + h->string_size > st.st_size || |
| 165 |
h->locrectab_offset + h->locrectab_size > st.st_size || |
| 166 |
h->sumhash_offset + h->sumhash_size > st.st_size) { |
| 167 |
r = -EBADMSG; |
| 168 |
goto finish; |
| 169 |
} |
| 170 |
|
| 171 |
e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset); |
| 172 |
for (size_t i = 0; i < h->namehash_size; i++) { |
| 173 |
char *z; |
| 174 |
|
| 175 |
if (e[i].locrec_offset == 0) |
| 176 |
continue; |
| 177 |
|
| 178 |
if (!utf8_is_valid((char*) p + e[i].name_offset)) |
| 179 |
continue; |
| 180 |
|
| 181 |
z = normalize_locale((char*) p + e[i].name_offset); |
| 182 |
if (!z) { |
| 183 |
r = -ENOMEM; |
| 184 |
goto finish; |
| 185 |
} |
| 186 |
|
| 187 |
r = set_consume(locales, z); |
| 188 |
if (r < 0) |
| 189 |
goto finish; |
| 190 |
} |
| 191 |
|
| 192 |
r = 0; |
| 193 |
|
| 194 |
finish: |
| 195 |
if (p != MAP_FAILED) |
| 196 |
munmap(p, st.st_size); |
| 197 |
|
| 198 |
return r; |
| 199 |
} |
| 200 |
|
| 201 |
static int add_locales_from_libdir(Set *locales) { |
| 202 |
_cleanup_closedir_ DIR *dir = NULL; |
| 203 |
int r; |
| 204 |
|
| 205 |
assert(locales); |
| 206 |
|
| 207 |
dir = opendir(get_locale_dir()); |
| 208 |
if (!dir) |
| 209 |
return errno == ENOENT ? 0 : -errno; |
| 210 |
|
| 211 |
FOREACH_DIRENT(de, dir, return -errno) { |
| 212 |
char *z; |
| 213 |
|
| 214 |
if (de->d_type != DT_DIR) |
| 215 |
continue; |
| 216 |
|
| 217 |
z = normalize_locale(de->d_name); |
| 218 |
if (!z) |
| 219 |
return -ENOMEM; |
| 220 |
|
| 221 |
r = set_consume(locales, z); |
| 222 |
if (r < 0) |
| 223 |
return r; |
| 224 |
} |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
#else |
| 230 |
|
| 231 |
static int add_locales_for_musl(Set *locales) { |
| 232 |
int r; |
| 233 |
|
| 234 |
assert(locales); |
| 235 |
|
| 236 |
_cleanup_closedir_ DIR *dir = opendir(get_locale_dir()); |
| 237 |
if (!dir) |
| 238 |
return errno == ENOENT ? 0 : -errno; |
| 239 |
|
| 240 |
FOREACH_DIRENT(de, dir, return -errno) { |
| 241 |
if (de->d_type != DT_REG) |
| 242 |
continue; |
| 243 |
|
| 244 |
char *z = normalize_locale(de->d_name); |
| 245 |
if (!z) |
| 246 |
return -ENOMEM; |
| 247 |
|
| 248 |
r = set_consume(locales, z); |
| 249 |
if (r < 0) |
| 250 |
return r; |
| 251 |
} |
| 252 |
|
| 253 |
return 0; |
| 254 |
} |
| 255 |
#endif |
| 256 |
|
| 257 |
int get_locales(char ***ret) { |
| 258 |
_cleanup_set_free_ Set *locales = NULL; |
| 259 |
int r; |
| 260 |
|
| 261 |
locales = set_new(&string_hash_ops_free); |
| 262 |
if (!locales) |
| 263 |
return -ENOMEM; |
| 264 |
|
| 265 |
#ifdef __GLIBC__ |
| 266 |
r = add_locales_from_archive(locales); |
| 267 |
if (r < 0 && r != -ENOENT) |
| 268 |
return r; |
| 269 |
|
| 270 |
r = add_locales_from_libdir(locales); |
| 271 |
if (r < 0) |
| 272 |
return r; |
| 273 |
#else |
| 274 |
r = add_locales_for_musl(locales); |
| 275 |
if (r < 0) |
| 276 |
return r; |
| 277 |
#endif |
| 278 |
|
| 279 |
char *locale; |
| 280 |
SET_FOREACH(locale, locales) { |
| 281 |
r = locale_is_installed(locale); |
| 282 |
if (r < 0) |
| 283 |
return r; |
| 284 |
if (r == 0) |
| 285 |
free(set_remove(locales, locale)); |
| 286 |
} |
| 287 |
|
| 288 |
_cleanup_strv_free_ char **l = set_to_strv(&locales); |
| 289 |
if (!l) |
| 290 |
return -ENOMEM; |
| 291 |
|
| 292 |
r = getenv_bool("SYSTEMD_LIST_NON_UTF8_LOCALES"); |
| 293 |
if (r <= 0) { |
| 294 |
if (!IN_SET(r, -ENXIO, 0)) |
| 295 |
log_debug_errno(r, "Failed to parse $SYSTEMD_LIST_NON_UTF8_LOCALES as boolean, ignoring: %m"); |
| 296 |
|
| 297 |
/* Filter out non-UTF-8 locales, because it's 2019, by default */ |
| 298 |
char **b = l; |
| 299 |
STRV_FOREACH(a, l) |
| 300 |
if (endswith(*a, "UTF-8") || strstr(*a, ".UTF-8@")) |
| 301 |
*(b++) = *a; |
| 302 |
else |
| 303 |
free(*a); |
| 304 |
|
| 305 |
*b = NULL; |
| 306 |
} |
| 307 |
|
| 308 |
strv_sort(l); |
| 309 |
|
| 310 |
*ret = TAKE_PTR(l); |
| 311 |
|
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
bool locale_is_valid(const char *name) { |
| 316 |
|
| 317 |
if (isempty(name)) |
| 318 |
return false; |
| 319 |
|
| 320 |
if (strlen(name) >= 128) |
| 321 |
return false; |
| 322 |
|
| 323 |
if (!utf8_is_valid(name)) |
| 324 |
return false; |
| 325 |
|
| 326 |
if (!filename_is_valid(name)) |
| 327 |
return false; |
| 328 |
|
| 329 |
/* Locales look like: ll_CC.ENC@variant, where ll and CC are alphabetic, ENC is alphanumeric with |
| 330 |
* dashes, and variant seems to be alphabetic. |
| 331 |
* See: https://www.gnu.org/software/gettext/manual/html_node/Locale-Names.html */ |
| 332 |
if (!in_charset(name, ALPHANUMERICAL "_.-@")) |
| 333 |
return false; |
| 334 |
|
| 335 |
return true; |
| 336 |
} |
| 337 |
|
| 338 |
int locale_is_installed(const char *name) { |
| 339 |
if (!locale_is_valid(name)) |
| 340 |
return false; |
| 341 |
|
| 342 |
if (STR_IN_SET(name, "C", "POSIX")) /* These ones are always OK */ |
| 343 |
return true; |
| 344 |
|
| 345 |
#ifdef __GLIBC__ |
| 346 |
_cleanup_(freelocalep) locale_t loc = newlocale(LC_ALL_MASK, name, (locale_t) 0); |
| 347 |
if (loc == (locale_t) 0) |
| 348 |
return errno == ENOMEM ? -ENOMEM : false; |
| 349 |
|
| 350 |
return true; |
| 351 |
#else |
| 352 |
/* musl also has C.UTF-8 as builtin */ |
| 353 |
if (streq(name, "C.UTF-8")) |
| 354 |
return true; |
| 355 |
|
| 356 |
/* musl's newlocale() always succeeds and provides a fake locale object even when the locale does |
| 357 |
* not exist. Hence, we need to explicitly check if the locale file exists. */ |
| 358 |
_cleanup_free_ char *p = path_join(get_locale_dir(), name); |
| 359 |
if (!p) |
| 360 |
return -ENOMEM; |
| 361 |
|
| 362 |
return access(p, F_OK) >= 0; |
| 363 |
#endif |
| 364 |
} |
| 365 |
|
| 366 |
static bool is_locale_utf8_impl(void) { |
| 367 |
const char *set; |
| 368 |
int r; |
| 369 |
|
| 370 |
/* Note that we default to 'true' here, since today UTF8 is pretty much supported everywhere. */ |
| 371 |
|
| 372 |
r = secure_getenv_bool("SYSTEMD_UTF8"); |
| 373 |
if (r >= 0) |
| 374 |
return r; |
| 375 |
if (r != -ENXIO) |
| 376 |
log_debug_errno(r, "Failed to parse $SYSTEMD_UTF8, ignoring: %m"); |
| 377 |
|
| 378 |
/* This function may be called from libsystemd, and setlocale() is not thread safe. Assuming yes. */ |
| 379 |
if (!is_main_thread()) |
| 380 |
return true; |
| 381 |
|
| 382 |
if (!setlocale(LC_ALL, "")) |
| 383 |
return true; |
| 384 |
|
| 385 |
set = nl_langinfo(CODESET); |
| 386 |
if (!set || streq(set, "UTF-8")) |
| 387 |
return true; |
| 388 |
|
| 389 |
set = setlocale(LC_CTYPE, NULL); |
| 390 |
if (!set) |
| 391 |
return true; |
| 392 |
|
| 393 |
/* Unless LC_CTYPE is explicitly overridden, return true. Because here CTYPE is effectively unset |
| 394 |
* and everything can do to UTF-8 nowadays. */ |
| 395 |
return STR_IN_SET(set, "C", "POSIX") && |
| 396 |
!getenv("LC_ALL") && |
| 397 |
!getenv("LC_CTYPE") && |
| 398 |
!getenv("LANG"); |
| 399 |
} |
| 400 |
|
| 401 |
bool is_locale_utf8(void) { |
| 402 |
static int cached = -1; |
| 403 |
|
| 404 |
if (cached < 0) |
| 405 |
cached = is_locale_utf8_impl(); |
| 406 |
|
| 407 |
return cached; |
| 408 |
} |
| 409 |
|
| 410 |
void locale_variables_free(char *l[_VARIABLE_LC_MAX]) { |
| 411 |
free_many_charp(l, _VARIABLE_LC_MAX); |
| 412 |
} |
| 413 |
|
| 414 |
void locale_variables_simplify(char *l[_VARIABLE_LC_MAX]) { |
| 415 |
assert(l); |
| 416 |
|
| 417 |
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) { |
| 418 |
if (p == VARIABLE_LANG) |
| 419 |
continue; |
| 420 |
if (isempty(l[p]) || streq_ptr(l[VARIABLE_LANG], l[p])) |
| 421 |
l[p] = mfree(l[p]); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
static const char * const locale_variable_table[_VARIABLE_LC_MAX] = { |
| 426 |
[VARIABLE_LANG] = "LANG", |
| 427 |
[VARIABLE_LANGUAGE] = "LANGUAGE", |
| 428 |
[VARIABLE_LC_CTYPE] = "LC_CTYPE", |
| 429 |
[VARIABLE_LC_NUMERIC] = "LC_NUMERIC", |
| 430 |
[VARIABLE_LC_TIME] = "LC_TIME", |
| 431 |
[VARIABLE_LC_COLLATE] = "LC_COLLATE", |
| 432 |
[VARIABLE_LC_MONETARY] = "LC_MONETARY", |
| 433 |
[VARIABLE_LC_MESSAGES] = "LC_MESSAGES", |
| 434 |
[VARIABLE_LC_PAPER] = "LC_PAPER", |
| 435 |
[VARIABLE_LC_NAME] = "LC_NAME", |
| 436 |
[VARIABLE_LC_ADDRESS] = "LC_ADDRESS", |
| 437 |
[VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE", |
| 438 |
[VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT", |
| 439 |
[VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION" |
| 440 |
}; |
| 441 |
|
| 442 |
DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable); |