Debian Codegraph @

contrib/mod_sftp/utf8.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp UTF8 encoding
3 * Copyright (c) 2008-2026 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 #include "mod_sftp.h"
26 #include "utf8.h"
27
28 #ifdef HAVE_ICONV_H
29 # include <iconv.h>
30 #endif
31
32 #ifdef HAVE_LANGINFO_H
33 # include <langinfo.h>
34 #endif
35
36 static const char *local_charset = NULL;
37
38 #if defined(PR_USE_NLS) && defined(HAVE_ICONV_H)
39 static iconv_t decode_conv = (iconv_t) -1;
40 static iconv_t encode_conv = (iconv_t) -1;
41
42 static int utf8_convert(iconv_t conv, const char *inbuf, size_t *inbuflen,
43 char *outbuf, size_t *outbuflen) {
44 # if defined(HAVE_ICONV)
45
46 /* Reset the state machine before each conversion. */
47 (void) iconv(conv, NULL, NULL, NULL, NULL);
48
49 while (*inbuflen > 0) {
50 size_t nconv;
51
52 pr_signals_handle();
53
54 /* Solaris/FreeBSD's iconv(3) takes a const char ** for the input buffer
55 * (depending on version), whereas Linux/Mac OSX/GNU iconv(3) use char **
56 * for the input buffer.
57 */
58 #if defined(LINUX) || defined(DARWIN6) || defined(DARWIN7) || \
59 defined(DARWIN8) || defined(DARWIN9) || defined(DARWIN10) || \
60 defined(DARWIN11) || defined(DARWIN12) || defined(FREEBSD14) || \
61 defined(GNU)
62
63 nconv = iconv(conv, (char **) &inbuf, inbuflen, &outbuf, outbuflen);
64 #else
65 nconv = iconv(conv, &inbuf, inbuflen, &outbuf, outbuflen);
66 #endif
67
68 if (nconv == (size_t) -1) {
69
70 /* Note: an errno of EILSEQ here can indicate badly encoded strings OR
71 * (more likely) that the source character set used in the iconv_open(3)
72 * call for this iconv_t descriptor does not accurately describe the
73 * character encoding of the given string. E.g. a filename may use
74 * the ISO8859-1 character set, but iconv_open(3) was called using
75 * US-ASCII.
76 */
77
78 return -1;
79 }
80
81 /* XXX We should let the loop condition work, rather than breaking out
82 * of the loop here.
83 */
84 break;
85 }
86
87 return 0;
88
89 # else
90 errno = ENOSYS;
91 return -1;
92 # endif /* HAVE_ICONV */
93 }
94 #endif /* !PR_USE_NLS && !HAVE_ICONV_H */
95
96 int sftp_utf8_set_charset(const char *charset) {
97 int res;
98
99 if (charset == NULL) {
100 errno = EINVAL;
101 return -1;
102 }
103
104 if (local_charset) {
105 pr_trace_msg("sftp", 5,
106 "attempting to switch local charset from %s to %s", local_charset,
107 charset);
108
109 } else {
110 pr_trace_msg("sftp", 5, "attempting to use %s as local charset", charset);
111 }
112
113 (void) sftp_utf8_free();
114
115 local_charset = pstrdup(permanent_pool, charset);
116
117 res = sftp_utf8_init();
118 if (res < 0) {
119 pr_trace_msg("sftp", 1,
120 "failed to initialize encoding for local charset %s", charset);
121 local_charset = NULL;
122 return -1;
123 }
124
125 return res;
126 }
127
128 int sftp_utf8_free(void) {
129 # if defined(PR_USE_NLS) && defined(HAVE_ICONV)
130 int res = 0;
131
132 /* Close the iconv handles. */
133 if (encode_conv != (iconv_t) -1) {
134 res = iconv_close(encode_conv);
135 if (res < 0) {
136 pr_trace_msg("sftp", 1,
137 "error closing encoding conversion handle from '%s' to '%s': %s",
138 local_charset, "UTF-8", strerror(errno));
139 res = -1;
140 }
141
142 encode_conv = (iconv_t) -1;
143 }
144
145 if (decode_conv != (iconv_t) -1) {
146 res = iconv_close(decode_conv);
147 if (res < 0) {
148 pr_trace_msg("sftp", 1,
149 "error closing decoding conversion handle from '%s' to '%s': %s",
150 "UTF-8", local_charset, strerror(errno));
151 res = -1;
152 }
153
154 decode_conv = (iconv_t) -1;
155 }
156
157 return res;
158 # else
159 errno = ENOSYS;
160 return -1;
161 # endif
162 }
163
164 int sftp_utf8_init(void) {
165 #if defined(PR_USE_NLS) && defined(HAVE_ICONV)
166
167 if (local_charset == NULL) {
168 local_charset = pr_encode_get_local_charset();
169
170 } else {
171 pr_trace_msg("sftp", 3,
172 "using '%s' as local charset for UTF8 conversion", local_charset);
173 }
174
175 /* Get the iconv handles. */
176 encode_conv = iconv_open("UTF-8", local_charset);
177 if (encode_conv == (iconv_t) -1) {
178 pr_trace_msg("sftp", 1, "error opening conversion handle from '%s' "
179 "to '%s': %s", local_charset, "UTF-8", strerror(errno));
180 return -1;
181 }
182
183 decode_conv = iconv_open(local_charset, "UTF-8");
184 if (decode_conv == (iconv_t) -1) {
185 int xerrno = errno;
186
187 pr_trace_msg("sftp", 1, "error opening conversion handle from '%s' "
188 "to '%s': %s", "UTF-8", local_charset, strerror(errno));
189
190 (void) iconv_close(encode_conv);
191 encode_conv = (iconv_t) -1;
192
193 errno = xerrno;
194 return -1;
195 }
196
197 return 0;
198 # else
199 errno = ENOSYS;
200 return -1;
201 #endif /* HAVE_ICONV */
202 }
203
204 char *sftp_utf8_decode_str(pool *p, const char *str) {
205 #if defined(PR_USE_NLS) && defined(HAVE_ICONV_H)
206 size_t inlen, inbuflen, outlen, outbuflen;
207 char *inbuf, outbuf[PR_TUNABLE_PATH_MAX*2], *res = NULL;
208
209 if (p == NULL ||
210 str == NULL) {
211 errno = EINVAL;
212 return NULL;
213 }
214
215 if (decode_conv == (iconv_t) -1) {
216 pr_trace_msg("sftp", 1, "decoding conversion handle is invalid, unable to "
217 "decode UTF8 string");
218 return (char *) str;
219 }
220
221 /* If the local charset matches the remote charset (i.e. local_charset is
222 * "UTF-8"), then there's no point in converting; the charsets are the
223 * same. Indeed, on some libiconv implementations, attempting to
224 * convert between the same charsets results in a tightly spinning CPU
225 * (see Bug#3272).
226 */
227 if (strncasecmp(local_charset, "UTF-8", 6) == 0) {
228 return (char *) str;
229 }
230
231 inlen = strlen(str) + 1;
232 inbuf = pcalloc(p, inlen);
233 memcpy(inbuf, str, inlen);
234 inbuflen = inlen;
235
236 outbuflen = sizeof(outbuf);
237
238 if (utf8_convert(decode_conv, inbuf, &inbuflen, outbuf, &outbuflen) < 0) {
239 pr_trace_msg("sftp", 1, "error decoding string: %s", strerror(errno));
240
241 if (pr_trace_get_level("sftp") >= 14) {
242 /* Write out the string we tried (and failed) to decode, in hex. */
243 register unsigned int i;
244 unsigned char *raw_str;
245 size_t len, raw_len;
246
247 len = strlen(str);
248 raw_len = (len * 5) + 1;
249 raw_str = pcalloc(p, raw_len + 1);
250
251 for (i = 0; i < len; i++) {
252 pr_snprintf((char *) (raw_str + (i * 5)), (raw_len - 1) - (i * 5),
253 "0x%02x ", (unsigned char) str[i]);
254 }
255
256 pr_trace_msg("sftp", 14, "unable to decode string (raw bytes): %s",
257 raw_str);
258 }
259
260 return (char *) str;
261 }
262
263 outlen = sizeof(outbuf) - outbuflen;
264 res = pcalloc(p, outlen);
265 memcpy(res, outbuf, outlen);
266
267 return res;
268 #else
269 return pstrdup(p, str);
270 #endif /* !PR_USE_NLS && !HAVE_ICONV_H */
271 }
272
273 char *sftp_utf8_encode_str(pool *p, const char *str) {
274 #if defined(PR_USE_NLS) && defined(HAVE_ICONV_H)
275 size_t inlen, inbuflen, outlen, outbuflen;
276 char *inbuf, outbuf[PR_TUNABLE_PATH_MAX*2], *res;
277
278 if (p == NULL ||
279 str == NULL) {
280 errno = EINVAL;
281 return NULL;
282 }
283
284 if (encode_conv == (iconv_t) -1) {
285 pr_trace_msg("sftp", 1, "encoding conversion handle is invalid, unable to "
286 "encode UTF8 string");
287 return (char *) str;
288 }
289
290 inlen = strlen(str) + 1;
291 inbuf = pcalloc(p, inlen);
292 memcpy(inbuf, str, inlen);
293 inbuflen = inlen;
294
295 outbuflen = sizeof(outbuf);
296
297 if (utf8_convert(encode_conv, inbuf, &inbuflen, outbuf, &outbuflen) < 0) {
298 pr_trace_msg("sftp", 1, "error encoding string: %s", strerror(errno));
299
300 if (pr_trace_get_level("sftp") >= 14) {
301 /* Write out the string we tried (and failed) to encode, in hex. */
302 register unsigned int i;
303 unsigned char *raw_str;
304 size_t len, raw_len;
305
306 len = strlen(str);
307 raw_len = (len * 5) + 1;
308 raw_str = pcalloc(p, raw_len + 1);
309
310 for (i = 0; i < len; i++) {
311 pr_snprintf((char *) (raw_str + (i * 5)), (raw_len - 1) - (i * 5),
312 "0x%02x ", (unsigned char) str[i]);
313 }
314
315 pr_trace_msg("sftp", 14, "unable to encode string (raw bytes): %s",
316 raw_str);
317 }
318
319 return (char *) str;
320 }
321
322 outlen = sizeof(outbuf) - outbuflen;
323 res = pcalloc(p, outlen);
324 memcpy(res, outbuf, outlen);
325
326 return res;
327 #else
328 return pstrdup(p, str);
329 #endif /* !PR_USE_NLS && !HAVE_ICONV_H */
330 }