vsftpd ver. 2.3.0 (08-06-2010)
[tomato.git] / release / src / router / vsftpd / parseconf.c
blobb28357600539b0bf6b214927f6facc2b9c24f165
1 /*
2 * Part of Very Secure FTPd
3 * Licence: GPL v2
4 * Author: Chris Evans
5 * parseconf.c
7 * Routines and support to load in a file full of tunable variables and
8 * settings, populating corresponding runtime variables.
9 */
11 #include "parseconf.h"
12 #include "tunables.h"
13 #include "str.h"
14 #include "filestr.h"
15 #include "defs.h"
16 #include "sysutil.h"
17 #include "utility.h"
19 static const char* s_p_saved_filename;
21 /* Tables mapping setting names to runtime variables */
22 /* Boolean settings */
23 static struct parseconf_bool_setting
25 const char* p_setting_name;
26 int* p_variable;
28 parseconf_bool_array[] =
30 { "anonymous_enable", &tunable_anonymous_enable },
31 { "local_enable", &tunable_local_enable },
32 { "pasv_enable", &tunable_pasv_enable },
33 { "port_enable", &tunable_port_enable },
34 { "chroot_local_user", &tunable_chroot_local_user },
35 { "write_enable", &tunable_write_enable },
36 { "anon_upload_enable", &tunable_anon_upload_enable },
37 { "anon_mkdir_write_enable", &tunable_anon_mkdir_write_enable },
38 { "anon_other_write_enable", &tunable_anon_other_write_enable },
39 { "chown_uploads", &tunable_chown_uploads },
40 { "connect_from_port_20", &tunable_connect_from_port_20 },
41 { "xferlog_enable", &tunable_xferlog_enable },
42 { "dirmessage_enable", &tunable_dirmessage_enable },
43 { "anon_world_readable_only", &tunable_anon_world_readable_only },
44 { "async_abor_enable", &tunable_async_abor_enable },
45 { "ascii_upload_enable", &tunable_ascii_upload_enable },
46 { "ascii_download_enable", &tunable_ascii_download_enable },
47 { "one_process_model", &tunable_one_process_model },
48 { "xferlog_std_format", &tunable_xferlog_std_format },
49 { "pasv_promiscuous", &tunable_pasv_promiscuous },
50 { "deny_email_enable", &tunable_deny_email_enable },
51 { "chroot_list_enable", &tunable_chroot_list_enable },
52 { "setproctitle_enable", &tunable_setproctitle_enable },
53 { "text_userdb_names", &tunable_text_userdb_names },
54 { "ls_recurse_enable", &tunable_ls_recurse_enable },
55 { "log_ftp_protocol", &tunable_log_ftp_protocol },
56 { "guest_enable", &tunable_guest_enable },
57 { "userlist_enable", &tunable_userlist_enable },
58 { "userlist_deny", &tunable_userlist_deny },
59 { "use_localtime", &tunable_use_localtime },
60 { "check_shell", &tunable_check_shell },
61 { "hide_ids", &tunable_hide_ids },
62 { "listen", &tunable_listen },
63 { "port_promiscuous", &tunable_port_promiscuous },
64 { "passwd_chroot_enable", &tunable_passwd_chroot_enable },
65 { "no_anon_password", &tunable_no_anon_password },
66 { "tcp_wrappers", &tunable_tcp_wrappers },
67 { "use_sendfile", &tunable_use_sendfile },
68 { "force_dot_files", &tunable_force_dot_files },
69 { "listen_ipv6", &tunable_listen_ipv6 },
70 { "dual_log_enable", &tunable_dual_log_enable },
71 { "syslog_enable", &tunable_syslog_enable },
72 { "background", &tunable_background },
73 { "virtual_use_local_privs", &tunable_virtual_use_local_privs },
74 { "session_support", &tunable_session_support },
75 { "download_enable", &tunable_download_enable },
76 { "dirlist_enable", &tunable_dirlist_enable },
77 { "chmod_enable", &tunable_chmod_enable },
78 { "secure_email_list_enable", &tunable_secure_email_list_enable },
79 { "run_as_launching_user", &tunable_run_as_launching_user },
80 { "no_log_lock", &tunable_no_log_lock },
81 { "ssl_enable", &tunable_ssl_enable },
82 { "allow_anon_ssl", &tunable_allow_anon_ssl },
83 { "force_local_logins_ssl", &tunable_force_local_logins_ssl },
84 { "force_local_data_ssl", &tunable_force_local_data_ssl },
85 { "ssl_sslv2", &tunable_sslv2 },
86 { "ssl_sslv3", &tunable_sslv3 },
87 { "ssl_tlsv1", &tunable_tlsv1 },
88 { "tilde_user_enable", &tunable_tilde_user_enable },
89 { "force_anon_logins_ssl", &tunable_force_anon_logins_ssl },
90 { "force_anon_data_ssl", &tunable_force_anon_data_ssl },
91 { "mdtm_write", &tunable_mdtm_write },
92 { "lock_upload_files", &tunable_lock_upload_files },
93 { "pasv_addr_resolve", &tunable_pasv_addr_resolve },
94 { "debug_ssl", &tunable_debug_ssl },
95 { "require_cert", &tunable_require_cert },
96 { "validate_cert", &tunable_validate_cert },
97 { "strict_ssl_read_eof", &tunable_strict_ssl_read_eof },
98 { "strict_ssl_write_shutdown", &tunable_strict_ssl_write_shutdown },
99 { "ssl_request_cert", &tunable_ssl_request_cert },
100 { "delete_failed_uploads", &tunable_delete_failed_uploads },
101 { "implicit_ssl", &tunable_implicit_ssl },
102 { "sandbox", &tunable_sandbox },
103 { "require_ssl_reuse", &tunable_require_ssl_reuse },
104 { "isolate", &tunable_isolate },
105 { "isolate_network", &tunable_isolate_network },
106 { "ftp_enable", &tunable_ftp_enable },
107 { "http_enable", &tunable_http_enable },
108 { 0, 0 }
111 static struct parseconf_uint_setting
113 const char* p_setting_name;
114 unsigned int* p_variable;
116 parseconf_uint_array[] =
118 { "accept_timeout", &tunable_accept_timeout },
119 { "connect_timeout", &tunable_connect_timeout },
120 { "local_umask", &tunable_local_umask },
121 { "anon_umask", &tunable_anon_umask },
122 { "ftp_data_port", &tunable_ftp_data_port },
123 { "idle_session_timeout", &tunable_idle_session_timeout },
124 { "data_connection_timeout", &tunable_data_connection_timeout },
125 { "pasv_min_port", &tunable_pasv_min_port },
126 { "pasv_max_port", &tunable_pasv_max_port },
127 { "anon_max_rate", &tunable_anon_max_rate },
128 { "local_max_rate", &tunable_local_max_rate },
129 { "listen_port", &tunable_listen_port },
130 { "max_clients", &tunable_max_clients },
131 { "file_open_mode", &tunable_file_open_mode },
132 { "max_per_ip", &tunable_max_per_ip },
133 { "trans_chunk_size", &tunable_trans_chunk_size },
134 { "delay_failed_login", &tunable_delay_failed_login },
135 { "delay_successful_login", &tunable_delay_successful_login },
136 { "max_login_fails", &tunable_max_login_fails },
137 { "chown_upload_mode", &tunable_chown_upload_mode },
138 { 0, 0 }
141 static struct parseconf_str_setting
143 const char* p_setting_name;
144 const char** p_variable;
146 parseconf_str_array[] =
148 { "secure_chroot_dir", &tunable_secure_chroot_dir },
149 { "ftp_username", &tunable_ftp_username },
150 { "chown_username", &tunable_chown_username },
151 { "xferlog_file", &tunable_xferlog_file },
152 { "vsftpd_log_file", &tunable_vsftpd_log_file },
153 { "message_file", &tunable_message_file },
154 { "nopriv_user", &tunable_nopriv_user },
155 { "ftpd_banner", &tunable_ftpd_banner },
156 { "banned_email_file", &tunable_banned_email_file },
157 { "chroot_list_file", &tunable_chroot_list_file },
158 { "pam_service_name", &tunable_pam_service_name },
159 { "guest_username", &tunable_guest_username },
160 { "userlist_file", &tunable_userlist_file },
161 { "anon_root", &tunable_anon_root },
162 { "local_root", &tunable_local_root },
163 { "banner_file", &tunable_banner_file },
164 { "pasv_address", &tunable_pasv_address },
165 { "listen_address", &tunable_listen_address },
166 { "user_config_dir", &tunable_user_config_dir },
167 { "listen_address6", &tunable_listen_address6 },
168 { "cmds_allowed", &tunable_cmds_allowed },
169 { "hide_file", &tunable_hide_file },
170 { "deny_file", &tunable_deny_file },
171 { "user_sub_token", &tunable_user_sub_token },
172 { "email_password_file", &tunable_email_password_file },
173 { "rsa_cert_file", &tunable_rsa_cert_file },
174 { "dsa_cert_file", &tunable_dsa_cert_file },
175 { "ssl_ciphers", &tunable_ssl_ciphers },
176 { "rsa_private_key_file", &tunable_rsa_private_key_file },
177 { "dsa_private_key_file", &tunable_dsa_private_key_file },
178 { "ca_certs_file", &tunable_ca_certs_file },
179 { "cmds_denied", &tunable_cmds_denied },
180 { 0, 0 }
183 void
184 vsf_parseconf_load_file(const char* p_filename, int errs_fatal)
186 struct mystr config_file_str = INIT_MYSTR;
187 struct mystr config_setting_str = INIT_MYSTR;
188 struct mystr config_value_str = INIT_MYSTR;
189 unsigned int str_pos = 0;
190 int retval;
191 if (!p_filename)
193 p_filename = s_p_saved_filename;
195 else
197 if (s_p_saved_filename)
199 vsf_sysutil_free((char*)s_p_saved_filename);
201 s_p_saved_filename = vsf_sysutil_strdup(p_filename);
203 if (!p_filename)
205 bug("null filename in vsf_parseconf_load_file");
207 retval = str_fileread(&config_file_str, p_filename, VSFTP_CONF_FILE_MAX);
208 if (vsf_sysutil_retval_is_error(retval))
210 if (errs_fatal)
212 die2("cannot read config file: ", p_filename);
214 else
216 str_free(&config_file_str);
217 return;
221 struct vsf_sysutil_statbuf* p_statbuf = 0;
222 retval = vsf_sysutil_stat(p_filename, &p_statbuf);
223 /* Security: check current user owns the config file. These are sanity
224 * checks for the admin, and are NOT designed to be checks safe from
225 * race conditions.
227 if (vsf_sysutil_retval_is_error(retval) ||
228 vsf_sysutil_statbuf_get_uid(p_statbuf) != vsf_sysutil_getuid() ||
229 !vsf_sysutil_statbuf_is_regfile(p_statbuf))
231 die("config file not owned by correct user, or not a file");
233 vsf_sysutil_free(p_statbuf);
235 while (str_getline(&config_file_str, &config_setting_str, &str_pos))
237 if (str_isempty(&config_setting_str) ||
238 str_get_char_at(&config_setting_str, 0) == '#' ||
239 str_all_space(&config_setting_str))
241 continue;
243 vsf_parseconf_load_setting(str_getbuf(&config_setting_str), errs_fatal);
245 str_free(&config_file_str);
246 str_free(&config_setting_str);
247 str_free(&config_value_str);
250 void
251 vsf_parseconf_load_setting(const char* p_setting, int errs_fatal)
253 static struct mystr s_setting_str;
254 static struct mystr s_value_str;
255 while (vsf_sysutil_isspace(*p_setting))
257 p_setting++;
259 str_alloc_text(&s_setting_str, p_setting);
260 str_split_char(&s_setting_str, &s_value_str, '=');
261 /* Is it a string setting? */
263 const struct parseconf_str_setting* p_str_setting = parseconf_str_array;
264 while (p_str_setting->p_setting_name != 0)
266 if (str_equal_text(&s_setting_str, p_str_setting->p_setting_name))
268 /* Got it */
269 const char** p_curr_setting = p_str_setting->p_variable;
270 if (*p_curr_setting)
272 vsf_sysutil_free((char*) *p_curr_setting);
274 if (str_isempty(&s_value_str))
276 *p_curr_setting = 0;
278 else
280 *p_curr_setting = str_strdup(&s_value_str);
282 return;
284 p_str_setting++;
287 if (str_isempty(&s_value_str))
289 if (errs_fatal)
291 die2("missing value in config file for: ", str_getbuf(&s_setting_str));
293 else
295 return;
298 /* Is it a boolean value? */
300 const struct parseconf_bool_setting* p_bool_setting = parseconf_bool_array;
301 while (p_bool_setting->p_setting_name != 0)
303 if (str_equal_text(&s_setting_str, p_bool_setting->p_setting_name))
305 /* Got it */
306 str_upper(&s_value_str);
307 if (str_equal_text(&s_value_str, "YES") ||
308 str_equal_text(&s_value_str, "TRUE") ||
309 str_equal_text(&s_value_str, "1"))
311 *(p_bool_setting->p_variable) = 1;
313 else if (str_equal_text(&s_value_str, "NO") ||
314 str_equal_text(&s_value_str, "FALSE") ||
315 str_equal_text(&s_value_str, "0"))
317 *(p_bool_setting->p_variable) = 0;
319 else if (errs_fatal)
321 die2("bad bool value in config file for: ",
322 str_getbuf(&s_setting_str));
324 return;
326 p_bool_setting++;
329 /* Is it an unsigned integer setting? */
331 const struct parseconf_uint_setting* p_uint_setting = parseconf_uint_array;
332 while (p_uint_setting->p_setting_name != 0)
334 if (str_equal_text(&s_setting_str, p_uint_setting->p_setting_name))
336 /* Got it */
337 /* If the value starts with 0, assume it's an octal value */
338 if (!str_isempty(&s_value_str) &&
339 str_get_char_at(&s_value_str, 0) == '0')
341 *(p_uint_setting->p_variable) = str_octal_to_uint(&s_value_str);
343 else
345 *(p_uint_setting->p_variable) = str_atoi(&s_value_str);
347 return;
349 p_uint_setting++;
352 if (errs_fatal)
354 die2("unrecognised variable in config file: ", str_getbuf(&s_setting_str));