1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #if HAVE_LIBIDN2
4 #  include <idn2.h>
5 #elif HAVE_LIBIDN
6 #  include <idna.h>
7 #  include <stringprep.h>
8 #endif
9 
10 #include "alloc-util.h"
11 #include "dlfcn-util.h"
12 #include "idn-util.h"
13 
14 #if HAVE_LIBIDN || HAVE_LIBIDN2
15 static void* idn_dl = NULL;
16 #endif
17 
18 #if HAVE_LIBIDN2
19 int (*sym_idn2_lookup_u8)(const uint8_t* src, uint8_t** lookupname, int flags) = NULL;
20 const char *(*sym_idn2_strerror)(int rc) = NULL;
21 int (*sym_idn2_to_unicode_8z8z)(const char * input, char ** output, int flags) = NULL;
22 
dlopen_idn(void)23 int dlopen_idn(void) {
24         return dlopen_many_sym_or_warn(
25                         &idn_dl, "libidn2.so.0", LOG_DEBUG,
26                         DLSYM_ARG(idn2_lookup_u8),
27                         DLSYM_ARG(idn2_strerror),
28                         DLSYM_ARG(idn2_to_unicode_8z8z));
29 }
30 #endif
31 
32 #if HAVE_LIBIDN
33 int (*sym_idna_to_ascii_4i)(const uint32_t * in, size_t inlen, char *out, int flags);
34 int (*sym_idna_to_unicode_44i)(const uint32_t * in, size_t inlen,uint32_t * out, size_t * outlen, int flags);
35 char* (*sym_stringprep_ucs4_to_utf8)(const uint32_t * str, ssize_t len, size_t * items_read, size_t * items_written);
36 uint32_t* (*sym_stringprep_utf8_to_ucs4)(const char *str, ssize_t len, size_t *items_written);
37 
dlopen_idn(void)38 int dlopen_idn(void) {
39         _cleanup_(dlclosep) void *dl = NULL;
40         int r;
41 
42         if (idn_dl)
43                 return 0; /* Already loaded */
44 
45         dl = dlopen("libidn.so.12", RTLD_LAZY);
46         if (!dl) {
47                 /* libidn broke ABI in 1.34, but not in a way we care about (a new field got added to an
48                  * open-coded struct we do not use), hence support both versions. */
49                 dl = dlopen("libidn.so.11", RTLD_LAZY);
50                 if (!dl)
51                         return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
52                                                "libidn support is not installed: %s", dlerror());
53         }
54 
55         r = dlsym_many_or_warn(
56                         dl,
57                         LOG_DEBUG,
58                         DLSYM_ARG(idna_to_ascii_4i),
59                         DLSYM_ARG(idna_to_unicode_44i),
60                         DLSYM_ARG(stringprep_ucs4_to_utf8),
61                         DLSYM_ARG(stringprep_utf8_to_ucs4));
62         if (r < 0)
63                 return r;
64 
65         idn_dl = TAKE_PTR(dl);
66 
67         return 1;
68 }
69 #endif
70