Line 0
Link Here
|
|
|
1 |
/* |
2 |
* ProFTPD: mod_codeconv -- local <-> remote charset conversion |
3 |
* |
4 |
* Copyright (c) 2004 by TSUJIKAWA Tohru <tsujikawa@tsg.ne.jp> / All rights reserved. |
5 |
* Fixed segfault bug by Sergey Belyashov <Sergey.Belyashov@gmail.com> |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
20 |
* |
21 |
*/ |
22 |
|
23 |
|
24 |
#include "conf.h" |
25 |
#include <iconv.h> |
26 |
#include <string.h> |
27 |
|
28 |
// |
29 |
// directive |
30 |
// |
31 |
#define DIRECTIVE_CHARSETLOCAL "CharsetLocal" |
32 |
#define DIRECTIVE_CHARSETREMOTE "CharsetRemote" |
33 |
|
34 |
static char charset_local[256] = ""; |
35 |
static char charset_remote[256] = ""; |
36 |
|
37 |
|
38 |
// |
39 |
// initialization |
40 |
// |
41 |
static int codeconv_init(void) |
42 |
{ |
43 |
return 0; |
44 |
} |
45 |
|
46 |
static int codeconv_sess_init(void) |
47 |
{ |
48 |
return 0; |
49 |
} |
50 |
|
51 |
|
52 |
char* remote2local(struct pool* pool, char* remote) |
53 |
{ |
54 |
iconv_t ic = (iconv_t)(-1); |
55 |
char* local; |
56 |
char* in_ptr; |
57 |
char* out_ptr; |
58 |
size_t inbytesleft, outbytesleft; |
59 |
|
60 |
if (strlen(charset_local)&&strlen(charset_remote)) |
61 |
ic = iconv_open(charset_local, charset_remote); |
62 |
if (ic == (iconv_t)(-1)) return NULL; |
63 |
|
64 |
iconv(ic, NULL, NULL, NULL, NULL); |
65 |
|
66 |
inbytesleft = strlen(remote); |
67 |
outbytesleft = inbytesleft*3; |
68 |
local = palloc(pool, outbytesleft+1); |
69 |
|
70 |
in_ptr = remote; out_ptr = local; |
71 |
while (inbytesleft) { |
72 |
if (iconv(ic, &in_ptr, &inbytesleft, &out_ptr, &outbytesleft) == -1) { |
73 |
*out_ptr = '?'; out_ptr++; outbytesleft--; |
74 |
in_ptr++; inbytesleft--; |
75 |
break; |
76 |
} |
77 |
} |
78 |
*out_ptr = 0; |
79 |
|
80 |
iconv_close(ic); |
81 |
|
82 |
return local; |
83 |
} |
84 |
|
85 |
|
86 |
char* local2remote(char* local) |
87 |
{ |
88 |
iconv_t ic = (iconv_t)(-1); |
89 |
char* remote; |
90 |
char* in_ptr; |
91 |
char* out_ptr; |
92 |
size_t inbytesleft, outbytesleft; |
93 |
|
94 |
if (strlen(charset_local)&&strlen(charset_remote)) |
95 |
ic = iconv_open(charset_remote, charset_local); |
96 |
if (ic == (iconv_t)(-1)) return NULL; |
97 |
|
98 |
iconv(ic, NULL, NULL, NULL, NULL); |
99 |
|
100 |
inbytesleft = strlen(local); |
101 |
outbytesleft = inbytesleft*3; |
102 |
remote = malloc(outbytesleft+1); |
103 |
|
104 |
in_ptr = local; out_ptr = remote; |
105 |
while (inbytesleft) { |
106 |
if (iconv(ic, &in_ptr, &inbytesleft, &out_ptr, &outbytesleft) == -1) { |
107 |
*out_ptr = '?'; out_ptr++; outbytesleft--; |
108 |
in_ptr++; inbytesleft--; |
109 |
break; |
110 |
} |
111 |
} |
112 |
*out_ptr = 0; |
113 |
|
114 |
iconv_close(ic); |
115 |
|
116 |
return remote; |
117 |
} |
118 |
|
119 |
|
120 |
// |
121 |
// module handler |
122 |
// |
123 |
MODRET codeconv_pre_any(cmd_rec* cmd) |
124 |
{ |
125 |
char* p; |
126 |
int i; |
127 |
|
128 |
p = remote2local(cmd->pool, cmd->arg); |
129 |
if (p) cmd->arg = p; |
130 |
|
131 |
for (i = 0; i < cmd->argc; i++) { |
132 |
p = remote2local(cmd->pool, cmd->argv[i]); |
133 |
if (p) cmd->argv[i] = p; |
134 |
} |
135 |
|
136 |
return DECLINED(cmd); |
137 |
} |
138 |
|
139 |
|
140 |
// |
141 |
// local charset directive "CharsetLocal" |
142 |
// |
143 |
MODRET set_charsetlocal(cmd_rec *cmd) { |
144 |
config_rec *c = NULL; |
145 |
|
146 |
/* Syntax: CharsetLocal iconv-charset-name */ |
147 |
|
148 |
CHECK_ARGS(cmd, 1); |
149 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
150 |
|
151 |
c = add_config_param_str(DIRECTIVE_CHARSETLOCAL, 1, cmd->argv[1]); |
152 |
strncpy(charset_local, cmd->argv[1], sizeof(charset_local)); |
153 |
|
154 |
return HANDLED(cmd); |
155 |
} |
156 |
|
157 |
// |
158 |
// remote charset directive "CharsetRemote" |
159 |
// |
160 |
MODRET set_charsetremote(cmd_rec *cmd) { |
161 |
config_rec *c = NULL; |
162 |
|
163 |
/* Syntax: CharsetRemote iconv-charset-name */ |
164 |
|
165 |
CHECK_ARGS(cmd, 1); |
166 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
167 |
|
168 |
c = add_config_param_str(DIRECTIVE_CHARSETREMOTE, 1, cmd->argv[1]); |
169 |
strncpy(charset_remote, cmd->argv[1], sizeof(charset_remote)); |
170 |
|
171 |
return HANDLED(cmd); |
172 |
} |
173 |
|
174 |
|
175 |
// |
176 |
// module ÍÑ directive |
177 |
// |
178 |
static conftable codeconv_conftab[] = { |
179 |
{ DIRECTIVE_CHARSETLOCAL, set_charsetlocal, NULL }, |
180 |
{ DIRECTIVE_CHARSETREMOTE, set_charsetremote, NULL }, |
181 |
{ NULL, NULL, NULL } |
182 |
}; |
183 |
|
184 |
|
185 |
// |
186 |
// trap ¤¹¤ë¥³¥Þ¥ó¥É°ìÍ÷ |
187 |
// |
188 |
static cmdtable codeconv_cmdtab[] = { |
189 |
{ PRE_CMD, C_ANY, G_NONE, codeconv_pre_any, FALSE, FALSE }, |
190 |
{ 0, NULL } |
191 |
}; |
192 |
|
193 |
|
194 |
// |
195 |
// module ¾ðÊó |
196 |
// |
197 |
module codeconv_module = { |
198 |
|
199 |
/* Always NULL */ |
200 |
NULL, NULL, |
201 |
|
202 |
/* Module API version (2.0) */ |
203 |
0x20, |
204 |
|
205 |
/* Module name */ |
206 |
"codeconv", |
207 |
|
208 |
/* Module configuration directive handlers */ |
209 |
codeconv_conftab, |
210 |
|
211 |
/* Module command handlers */ |
212 |
codeconv_cmdtab, |
213 |
|
214 |
/* Module authentication handlers (none in this case) */ |
215 |
NULL, |
216 |
|
217 |
/* Module initialization */ |
218 |
codeconv_init, |
219 |
|
220 |
/* Session initialization */ |
221 |
codeconv_sess_init |
222 |
|
223 |
}; |