1/*
2 * Part of Very Secure FTPd
3 * Licence: GPL v2
4 * Author: Chris Evans
5 * parseconf.c
6 *
7 * Routines and support to load in a file full of tunable variables and
8 * settings, populating corresponding runtime variables.
9 */
10
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"
18
19static const char* s_p_saved_filename;
20static int s_strings_copied;
21
22/* File local functions */
23static void handle_config_setting(struct mystr* p_setting_str,
24                                  struct mystr* p_value_str,
25                                  int errs_fatal);
26
27static void copy_string_settings(void);
28
29/* Tables mapping setting names to runtime variables */
30/* Boolean settings */
31static struct parseconf_bool_setting
32{
33  const char* p_setting_name;
34  int* p_variable;
35}
36parseconf_bool_array[] =
37{
38  { "anonymous_enable", &tunable_anonymous_enable },
39  { "local_enable", &tunable_local_enable },
40  { "pasv_enable", &tunable_pasv_enable },
41  { "port_enable", &tunable_port_enable },
42  { "chroot_local_user", &tunable_chroot_local_user },
43  { "write_enable", &tunable_write_enable },
44  { "anon_upload_enable", &tunable_anon_upload_enable },
45  { "anon_mkdir_write_enable", &tunable_anon_mkdir_write_enable },
46  { "anon_other_write_enable", &tunable_anon_other_write_enable },
47  { "chown_uploads", &tunable_chown_uploads },
48  { "connect_from_port_20", &tunable_connect_from_port_20 },
49  { "xferlog_enable", &tunable_xferlog_enable },
50  { "dirmessage_enable", &tunable_dirmessage_enable },
51  { "anon_world_readable_only", &tunable_anon_world_readable_only },
52  { "async_abor_enable", &tunable_async_abor_enable },
53  { "ascii_upload_enable", &tunable_ascii_upload_enable },
54  { "ascii_download_enable", &tunable_ascii_download_enable },
55  { "one_process_model", &tunable_one_process_model },
56  { "xferlog_std_format", &tunable_xferlog_std_format },
57  { "pasv_promiscuous", &tunable_pasv_promiscuous },
58  { "deny_email_enable", &tunable_deny_email_enable },
59  { "chroot_list_enable", &tunable_chroot_list_enable },
60  { "setproctitle_enable", &tunable_setproctitle_enable },
61  { "text_userdb_names", &tunable_text_userdb_names },
62  { "ls_recurse_enable", &tunable_ls_recurse_enable },
63  { "log_ftp_protocol", &tunable_log_ftp_protocol },
64  { "guest_enable", &tunable_guest_enable },
65  { "userlist_enable", &tunable_userlist_enable },
66  { "userlist_deny", &tunable_userlist_deny },
67  { "use_localtime", &tunable_use_localtime },
68  { "check_shell", &tunable_check_shell },
69  { "hide_ids", &tunable_hide_ids },
70  { "listen", &tunable_listen },
71  { "port_promiscuous", &tunable_port_promiscuous },
72  { "passwd_chroot_enable", &tunable_passwd_chroot_enable },
73  { "no_anon_password", &tunable_no_anon_password },
74  { "tcp_wrappers", &tunable_tcp_wrappers },
75  { "use_sendfile", &tunable_use_sendfile },
76  { "force_dot_files", &tunable_force_dot_files },
77  { "listen_ipv6", &tunable_listen_ipv6 },
78  { "dual_log_enable", &tunable_dual_log_enable },
79  { "syslog_enable", &tunable_syslog_enable },
80  { "background", &tunable_background },
81  { "virtual_use_local_privs", &tunable_virtual_use_local_privs },
82  { "session_support", &tunable_session_support },
83  { "download_enable", &tunable_download_enable },
84  { "dirlist_enable", &tunable_dirlist_enable },
85  { "chmod_enable", &tunable_chmod_enable },
86  { "secure_email_list_enable", &tunable_secure_email_list_enable },
87  { "run_as_launching_user", &tunable_run_as_launching_user },
88  { "no_log_lock", &tunable_no_log_lock },
89  { "ssl_enable", &tunable_ssl_enable },
90  { "allow_anon_ssl", &tunable_allow_anon_ssl },
91  { "force_local_logins_ssl", &tunable_force_local_logins_ssl },
92  { "force_local_data_ssl", &tunable_force_local_data_ssl },
93  { "ssl_sslv2", &tunable_sslv2 },
94  { "ssl_sslv3", &tunable_sslv3 },
95  { "ssl_tlsv1", &tunable_tlsv1 },
96  { "tilde_user_enable", &tunable_tilde_user_enable },
97  { "enable_iconv", &tunable_enable_iconv },	// Jiahao
98  { "force_anon_logins_ssl", &tunable_force_anon_logins_ssl },
99  { "force_anon_data_ssl", &tunable_force_anon_data_ssl },
100  { "mdtm_write", &tunable_mdtm_write },
101  { "lock_upload_files", &tunable_lock_upload_files },
102  { "pasv_addr_resolve", &tunable_pasv_addr_resolve },
103  { 0, 0 }
104};
105
106static struct parseconf_uint_setting
107{
108  const char* p_setting_name;
109  unsigned int* p_variable;
110}
111parseconf_uint_array[] =
112{
113  { "accept_timeout", &tunable_accept_timeout },
114  { "connect_timeout", &tunable_connect_timeout },
115  { "local_umask", &tunable_local_umask },
116  { "anon_umask", &tunable_anon_umask },
117  { "ftp_data_port", &tunable_ftp_data_port },
118  { "idle_session_timeout", &tunable_idle_session_timeout },
119  { "data_connection_timeout", &tunable_data_connection_timeout },
120  { "pasv_min_port", &tunable_pasv_min_port },
121  { "pasv_max_port", &tunable_pasv_max_port },
122  { "anon_max_rate", &tunable_anon_max_rate },
123  { "local_max_rate", &tunable_local_max_rate },
124  { "listen_port", &tunable_listen_port },
125  { "max_clients", &tunable_max_clients },
126  { "file_open_mode", &tunable_file_open_mode },
127  { "max_per_ip", &tunable_max_per_ip },
128  { "trans_chunk_size", &tunable_trans_chunk_size },
129  { 0, 0 }
130};
131
132static struct parseconf_str_setting
133{
134  const char* p_setting_name;
135  const char** p_variable;
136}
137parseconf_str_array[] =
138{
139  { "secure_chroot_dir", &tunable_secure_chroot_dir },
140  { "ftp_username", &tunable_ftp_username },
141  { "chown_username", &tunable_chown_username },
142  { "xferlog_file", &tunable_xferlog_file },
143  { "vsftpd_log_file", &tunable_vsftpd_log_file },
144  { "message_file", &tunable_message_file },
145  { "nopriv_user", &tunable_nopriv_user },
146  { "ftpd_banner", &tunable_ftpd_banner },
147  { "banned_email_file", &tunable_banned_email_file },
148  { "chroot_list_file", &tunable_chroot_list_file },
149  { "pam_service_name", &tunable_pam_service_name },
150  { "guest_username", &tunable_guest_username },
151  { "userlist_file", &tunable_userlist_file },
152  { "anon_root", &tunable_anon_root },
153  { "local_root", &tunable_local_root },
154  { "banner_file", &tunable_banner_file },
155  { "pasv_address", &tunable_pasv_address },
156  { "listen_address", &tunable_listen_address },
157  { "user_config_dir", &tunable_user_config_dir },
158  { "listen_address6", &tunable_listen_address6 },
159  { "cmds_allowed", &tunable_cmds_allowed },
160  { "hide_file", &tunable_hide_file },
161  { "deny_file", &tunable_deny_file },
162  { "user_sub_token", &tunable_user_sub_token },
163  { "email_password_file", &tunable_email_password_file },
164  { "rsa_cert_file", &tunable_rsa_cert_file },
165  { "dsa_cert_file", &tunable_dsa_cert_file },
166  { "ssl_ciphers", &tunable_ssl_ciphers },
167  { "local_charset", &tunable_local_charset },		// Jiahao
168  { "remote_charset", &tunable_remote_charset },	// Jiahao
169  { "rsa_private_key_file", &tunable_rsa_private_key_file },
170  { "dsa_private_key_file", &tunable_dsa_private_key_file },
171  { 0, 0 }
172};
173
174void
175vsf_parseconf_load_file(const char* p_filename, int errs_fatal)
176{
177  struct mystr config_file_str = INIT_MYSTR;
178  struct mystr config_setting_str = INIT_MYSTR;
179  struct mystr config_value_str = INIT_MYSTR;
180  unsigned int str_pos = 0;
181  int retval;
182  if (!p_filename)
183  {
184    p_filename = s_p_saved_filename;
185  }
186  else
187  {
188    if (s_p_saved_filename)
189    {
190      vsf_sysutil_free((char*)s_p_saved_filename);
191    }
192    s_p_saved_filename = vsf_sysutil_strdup(p_filename);
193  }
194  if (!p_filename)
195  {
196    bug("null filename in vsf_parseconf_load_file");
197  }
198  if (!s_strings_copied)
199  {
200    s_strings_copied = 1;
201    /* A minor hack to make sure all strings are malloc()'ed so we can free
202     * them at some later date. Specifically handles strings embedded in the
203     * binary.
204     */
205    copy_string_settings();
206  }
207  retval = str_fileread(&config_file_str, p_filename, VSFTP_CONF_FILE_MAX);
208  if (vsf_sysutil_retval_is_error(retval))
209  {
210    if (errs_fatal)
211    {
212      die2("cannot open config file:", p_filename);
213    }
214    else
215    {
216      return;
217    }
218  }
219  while (str_getline(&config_file_str, &config_setting_str, &str_pos))
220  {
221    if (str_isempty(&config_setting_str) ||
222        str_get_char_at(&config_setting_str, 0) == '#')
223    {
224      continue;
225    }
226    /* Split into name=value pair */
227    str_split_char(&config_setting_str, &config_value_str, '=');
228    handle_config_setting(&config_setting_str, &config_value_str, errs_fatal);
229  }
230  str_free(&config_file_str);
231  str_free(&config_setting_str);
232  str_free(&config_value_str);
233}
234
235static void
236handle_config_setting(struct mystr* p_setting_str, struct mystr* p_value_str,
237                      int errs_fatal)
238{
239  /* Is it a string setting? */
240  {
241    const struct parseconf_str_setting* p_str_setting = parseconf_str_array;
242    while (p_str_setting->p_setting_name != 0)
243    {
244      if (str_equal_text(p_setting_str, p_str_setting->p_setting_name))
245      {
246        /* Got it */
247        const char** p_curr_setting = p_str_setting->p_variable;
248        if (*p_curr_setting)
249        {
250          vsf_sysutil_free((char*)*p_curr_setting);
251        }
252        if (str_isempty(p_value_str))
253        {
254          *p_curr_setting = 0;
255        }
256        else
257        {
258          *p_curr_setting = str_strdup(p_value_str);
259        }
260        return;
261      }
262      p_str_setting++;
263    }
264  }
265  if (str_isempty(p_value_str))
266  {
267    if (errs_fatal)
268    {
269      die2("missing value in config file for: ", str_getbuf(p_setting_str));
270    }
271    else
272    {
273      return;
274    }
275  }
276  /* Is it a boolean value? */
277  {
278    const struct parseconf_bool_setting* p_bool_setting = parseconf_bool_array;
279    while (p_bool_setting->p_setting_name != 0)
280    {
281      if (str_equal_text(p_setting_str, p_bool_setting->p_setting_name))
282      {
283        /* Got it */
284        str_upper(p_value_str);
285        if (str_equal_text(p_value_str, "YES") ||
286            str_equal_text(p_value_str, "TRUE") ||
287            str_equal_text(p_value_str, "1"))
288        {
289          *(p_bool_setting->p_variable) = 1;
290        }
291        else if (str_equal_text(p_value_str, "NO") ||
292                 str_equal_text(p_value_str, "FALSE") ||
293                 str_equal_text(p_value_str, "0"))
294        {
295          *(p_bool_setting->p_variable) = 0;
296        }
297        else if (errs_fatal)
298        {
299          die2("bad bool value in config file for: ",
300               str_getbuf(p_setting_str));
301        }
302        return;
303      }
304      p_bool_setting++;
305    }
306  }
307  /* Is it an unsigned integer setting? */
308  {
309    const struct parseconf_uint_setting* p_uint_setting = parseconf_uint_array;
310    while (p_uint_setting->p_setting_name != 0)
311    {
312      if (str_equal_text(p_setting_str, p_uint_setting->p_setting_name))
313      {
314        /* Got it */
315        /* If the value starts with 0, assume it's an octal value */
316        if (!str_isempty(p_value_str) &&
317            str_get_char_at(p_value_str, 0) == '0')
318        {
319          *(p_uint_setting->p_variable) = str_octal_to_uint(p_value_str);
320        }
321        else
322        {
323          *(p_uint_setting->p_variable) = str_atoi(p_value_str);
324        }
325        return;
326      }
327      p_uint_setting++;
328    }
329  }
330  if (errs_fatal)
331  {
332    die2("unrecognised variable in config file: ", str_getbuf(p_setting_str));
333  }
334}
335
336static void
337copy_string_settings(void)
338{
339  const struct parseconf_str_setting* p_str_setting = parseconf_str_array;
340  while (p_str_setting->p_setting_name != 0)
341  {
342    if (*p_str_setting->p_variable != 0)
343    {
344      *p_str_setting->p_variable =
345          vsf_sysutil_strdup(*p_str_setting->p_variable);
346    }
347    p_str_setting++;
348  }
349}
350
351