2 * Part of Very Secure FTPd
7 * Code implementing the standard, secure two process security model.
10 #include "twoprocess.h"
13 #include "postlogin.h"
14 #include "postprivparent.h"
24 #include "parseconf.h"
26 #include "readwrite.h"
28 #include "sysdeputil.h"
31 static void drop_all_privs(void);
32 static void handle_sigchld(void* duff
);
33 static void handle_sigterm(void* duff
);
34 static void process_login_req(struct vsf_session
* p_sess
);
35 static void common_do_login(struct vsf_session
* p_sess
,
36 const struct mystr
* p_user_str
, int do_chroot
,
38 static void handle_per_user_config(const struct mystr
* p_user_str
);
39 static void calculate_chdir_dir(int anon
, struct mystr
* p_userdir_str
,
40 struct mystr
* p_chroot_str
,
41 struct mystr
* p_chdir_str
,
42 const struct mystr
* p_user_str
,
43 const struct mystr
* p_orig_user_str
);
46 handle_sigchld(void* duff
)
49 struct vsf_sysutil_wait_retval wait_retval
= vsf_sysutil_wait();
51 /* Child died, so we'll do the same! Report it as an error unless the child
52 * exited normally with zero exit code
54 if (vsf_sysutil_retval_is_error(vsf_sysutil_wait_get_retval(&wait_retval
)) ||
55 !vsf_sysutil_wait_exited_normally(&wait_retval
) ||
56 vsf_sysutil_wait_get_exitcode(&wait_retval
) != 0)
67 handle_sigterm(void* duff
)
70 /* Blow away the connection to make sure no process lingers. */
71 vsf_sysutil_shutdown_failok(VSFTP_COMMAND_FD
);
72 /* Will call the registered exit function to clean up u/wtmp if needed. */
77 vsf_two_process_start(struct vsf_session
* p_sess
)
79 vsf_sysutil_install_sighandler(kVSFSysUtilSigTERM
, handle_sigterm
, 0, 1);
80 /* Overrides the SIGKILL setting set by the standalone listener. */
81 vsf_set_term_if_parent_dies();
82 /* Create the comms channel between privileged parent and no-priv child */
83 priv_sock_init(p_sess
);
84 if (tunable_ssl_enable
)
86 /* Create the comms channel between the no-priv SSL child and the low-priv
87 * protocol handling child.
89 ssl_comm_channel_init(p_sess
);
91 vsf_sysutil_install_sighandler(kVSFSysUtilSigCHLD
, handle_sigchld
, 0, 1);
94 if (tunable_isolate_network
)
96 newpid
= vsf_sysutil_fork_newnet();
100 newpid
= vsf_sysutil_fork();
104 priv_sock_set_parent_context(p_sess
);
105 if (tunable_ssl_enable
)
107 ssl_comm_channel_set_consumer_context(p_sess
);
109 /* Parent - go into pre-login parent process mode */
112 process_login_req(p_sess
);
116 /* Child process - time to lose as much privilege as possible and do the
119 vsf_set_die_if_parent_dies();
120 priv_sock_set_child_context(p_sess
);
121 if (tunable_ssl_enable
)
123 ssl_comm_channel_set_producer_context(p_sess
);
125 if (tunable_local_enable
&& tunable_userlist_enable
)
127 int retval
= str_fileread(&p_sess
->userlist_str
, tunable_userlist_file
,
128 VSFTP_CONF_FILE_MAX
);
129 if (vsf_sysutil_retval_is_error(retval
))
131 die2("cannot read user list file:", tunable_userlist_file
);
135 init_connection(p_sess
);
142 struct mystr user_str
= INIT_MYSTR
;
143 struct mystr dir_str
= INIT_MYSTR
;
144 int option
= VSF_SECUTIL_OPTION_CHROOT
| VSF_SECUTIL_OPTION_NO_PROCS
;
145 if (!tunable_ssl_enable
)
147 /* Unfortunately, can only enable this if we can be sure of not using SSL.
148 * In the SSL case, we'll need to receive data transfer file descriptors.
150 option
|= VSF_SECUTIL_OPTION_NO_FDS
;
152 str_alloc_text(&user_str
, tunable_nopriv_user
);
153 str_alloc_text(&dir_str
, tunable_secure_chroot_dir
);
154 /* Be kind: give good error message if the secure dir is missing */
156 struct vsf_sysutil_statbuf
* p_statbuf
= 0;
157 if (vsf_sysutil_retval_is_error(str_lstat(&dir_str
, &p_statbuf
)))
159 die2("vsftpd: not found: directory given in 'secure_chroot_dir':",
160 tunable_secure_chroot_dir
);
162 vsf_sysutil_free(p_statbuf
);
164 vsf_secutil_change_credentials(&user_str
, &dir_str
, 0, 0, option
);
170 vsf_two_process_login(struct vsf_session
* p_sess
,
171 const struct mystr
* p_pass_str
)
174 priv_sock_send_cmd(p_sess
->child_fd
, PRIV_SOCK_LOGIN
);
175 priv_sock_send_str(p_sess
->child_fd
, &p_sess
->user_str
);
176 priv_sock_send_str(p_sess
->child_fd
, p_pass_str
);
177 priv_sock_send_int(p_sess
->child_fd
, p_sess
->control_use_ssl
);
178 priv_sock_send_int(p_sess
->child_fd
, p_sess
->data_use_ssl
);
179 result
= priv_sock_get_result(p_sess
->child_fd
);
180 if (result
== PRIV_SOCK_RESULT_OK
)
182 /* Miracle. We don't emit the success message here. That is left to
183 * process_post_login().
184 * Exit normally, unless we are remaining as the SSL read / write child.
186 if (!p_sess
->control_use_ssl
)
196 else if (result
== PRIV_SOCK_RESULT_BAD
)
198 /* Continue the processing loop.. */
203 die("priv_sock_get_result");
208 vsf_two_process_get_priv_data_sock(struct vsf_session
* p_sess
)
211 unsigned short port
= vsf_sysutil_sockaddr_get_port(p_sess
->p_port_sockaddr
);
212 priv_sock_send_cmd(p_sess
->child_fd
, PRIV_SOCK_GET_DATA_SOCK
);
213 priv_sock_send_int(p_sess
->child_fd
, port
);
214 res
= priv_sock_get_result(p_sess
->child_fd
);
215 if (res
== PRIV_SOCK_RESULT_BAD
)
219 else if (res
!= PRIV_SOCK_RESULT_OK
)
221 die("could not get privileged socket");
223 return priv_sock_recv_fd(p_sess
->child_fd
);
227 vsf_two_process_pasv_cleanup(struct vsf_session
* p_sess
)
230 priv_sock_send_cmd(p_sess
->child_fd
, PRIV_SOCK_PASV_CLEANUP
);
231 res
= priv_sock_get_result(p_sess
->child_fd
);
232 if (res
!= PRIV_SOCK_RESULT_OK
)
234 die("could not clean up socket");
239 vsf_two_process_pasv_active(struct vsf_session
* p_sess
)
241 priv_sock_send_cmd(p_sess
->child_fd
, PRIV_SOCK_PASV_ACTIVE
);
242 return priv_sock_get_int(p_sess
->child_fd
);
246 vsf_two_process_listen(struct vsf_session
* p_sess
)
248 priv_sock_send_cmd(p_sess
->child_fd
, PRIV_SOCK_PASV_LISTEN
);
249 return (unsigned short) priv_sock_get_int(p_sess
->child_fd
);
253 vsf_two_process_get_pasv_fd(struct vsf_session
* p_sess
)
256 priv_sock_send_cmd(p_sess
->child_fd
, PRIV_SOCK_PASV_ACCEPT
);
257 res
= priv_sock_get_result(p_sess
->child_fd
);
258 if (res
== PRIV_SOCK_RESULT_BAD
)
260 return priv_sock_get_int(p_sess
->child_fd
);
262 else if (res
!= PRIV_SOCK_RESULT_OK
)
264 die("could not accept on listening socket");
266 return priv_sock_recv_fd(p_sess
->child_fd
);
270 vsf_two_process_chown_upload(struct vsf_session
* p_sess
, int fd
)
273 priv_sock_send_cmd(p_sess
->child_fd
, PRIV_SOCK_CHOWN
);
274 priv_sock_send_fd(p_sess
->child_fd
, fd
);
275 res
= priv_sock_get_result(p_sess
->child_fd
);
276 if (res
!= PRIV_SOCK_RESULT_OK
)
278 die("unexpected failure in vsf_two_process_chown_upload");
283 process_login_req(struct vsf_session
* p_sess
)
285 enum EVSFPrivopLoginResult e_login_result
= kVSFLoginNull
;
288 cmd
= priv_sock_get_cmd(p_sess
->parent_fd
);
289 if (cmd
!= PRIV_SOCK_LOGIN
)
293 /* Get username and password - we must distrust these */
295 struct mystr password_str
= INIT_MYSTR
;
296 priv_sock_get_str(p_sess
->parent_fd
, &p_sess
->user_str
);
297 priv_sock_get_str(p_sess
->parent_fd
, &password_str
);
298 p_sess
->control_use_ssl
= priv_sock_get_int(p_sess
->parent_fd
);
299 p_sess
->data_use_ssl
= priv_sock_get_int(p_sess
->parent_fd
);
300 if (!tunable_ssl_enable
)
302 p_sess
->control_use_ssl
= 0;
303 p_sess
->data_use_ssl
= 0;
305 e_login_result
= vsf_privop_do_login(p_sess
, &password_str
);
306 str_free(&password_str
);
308 switch (e_login_result
)
311 priv_sock_send_result(p_sess
->parent_fd
, PRIV_SOCK_RESULT_BAD
);
315 str_alloc_text(&p_sess
->user_str
, tunable_ftp_username
);
316 common_do_login(p_sess
, &p_sess
->user_str
, 1, 1);
321 if (tunable_chroot_local_user
)
325 if (tunable_chroot_list_enable
)
327 struct mystr chroot_list_file
= INIT_MYSTR
;
328 int retval
= str_fileread(&chroot_list_file
,
329 tunable_chroot_list_file
,
330 VSFTP_CONF_FILE_MAX
);
331 if (vsf_sysutil_retval_is_error(retval
))
333 die2("could not read chroot() list file:",
334 tunable_chroot_list_file
);
336 if (str_contains_line(&chroot_list_file
, &p_sess
->user_str
))
347 str_free(&chroot_list_file
);
349 common_do_login(p_sess
, &p_sess
->user_str
, do_chroot
, 0);
355 bug("weird state in process_login_request");
362 common_do_login(struct vsf_session
* p_sess
, const struct mystr
* p_user_str
,
363 int do_chroot
, int anon
)
366 const struct mystr
* p_orig_user_str
= p_user_str
;
368 vsf_sysutil_install_null_sighandler(kVSFSysUtilSigCHLD
);
369 /* Tells the pre-login child all is OK (it may exit in response) */
370 priv_sock_send_result(p_sess
->parent_fd
, PRIV_SOCK_RESULT_OK
);
371 if (!p_sess
->control_use_ssl
)
373 (void) vsf_sysutil_wait();
377 p_sess
->ssl_slave_active
= 1;
379 /* Handle loading per-user config options */
380 handle_per_user_config(p_user_str
);
381 /* Set this before we fork */
382 p_sess
->is_anonymous
= anon
;
383 priv_sock_close(p_sess
);
384 priv_sock_init(p_sess
);
385 vsf_sysutil_install_sighandler(kVSFSysUtilSigCHLD
, handle_sigchld
, 0, 1);
386 if (tunable_isolate_network
&& !tunable_port_promiscuous
)
388 newpid
= vsf_sysutil_fork_newnet();
392 newpid
= vsf_sysutil_fork();
396 struct mystr guest_user_str
= INIT_MYSTR
;
397 struct mystr chroot_str
= INIT_MYSTR
;
398 struct mystr chdir_str
= INIT_MYSTR
;
399 struct mystr userdir_str
= INIT_MYSTR
;
400 unsigned int secutil_option
= VSF_SECUTIL_OPTION_USE_GROUPS
|
401 VSF_SECUTIL_OPTION_NO_PROCS
;
402 /* Child - drop privs and start proper FTP! */
403 /* This PR_SET_PDEATHSIG doesn't work for all possible process tree setups.
404 * The other cases are taken care of by a shutdown() of the command
405 * connection in our SIGTERM handler.
407 vsf_set_die_if_parent_dies();
408 priv_sock_set_child_context(p_sess
);
409 if (tunable_guest_enable
&& !anon
)
411 p_sess
->is_guest
= 1;
412 /* Remap to the guest user */
413 str_alloc_text(&guest_user_str
, tunable_guest_username
);
414 p_user_str
= &guest_user_str
;
415 if (!tunable_virtual_use_local_privs
)
423 secutil_option
|= VSF_SECUTIL_OPTION_CHROOT
;
427 secutil_option
|= VSF_SECUTIL_OPTION_CHANGE_EUID
;
429 calculate_chdir_dir(was_anon
, &userdir_str
, &chroot_str
, &chdir_str
,
430 p_user_str
, p_orig_user_str
);
431 vsf_secutil_change_credentials(p_user_str
, &userdir_str
, &chroot_str
,
433 if (!str_isempty(&chdir_str
))
435 (void) str_chdir(&chdir_str
);
437 str_free(&guest_user_str
);
438 str_free(&chroot_str
);
439 str_free(&chdir_str
);
440 str_free(&userdir_str
);
441 /* Guard against the config error of having the anonymous ftp tree owned
442 * by the user we are running as
444 if (!tunable_anon_allow_writable_root
&& was_anon
&& vsf_sysutil_write_access("/"))
446 die("vsftpd: refusing to run with writable anonymous root");
448 p_sess
->is_anonymous
= anon
;
449 process_post_login(p_sess
);
450 bug("should not get here: common_do_login");
453 priv_sock_set_parent_context(p_sess
);
454 if (tunable_ssl_enable
)
456 ssl_comm_channel_set_producer_context(p_sess
);
458 vsf_priv_parent_postlogin(p_sess
);
459 bug("should not get here in common_do_login");
463 handle_per_user_config(const struct mystr
* p_user_str
)
465 struct mystr filename_str
= INIT_MYSTR
;
466 struct vsf_sysutil_statbuf
* p_statbuf
= 0;
467 struct str_locate_result loc_result
;
469 if (!tunable_user_config_dir
)
473 /* Security paranoia - ignore if user has a / in it. */
474 loc_result
= str_locate_char(p_user_str
, '/');
475 if (loc_result
.found
)
479 str_alloc_text(&filename_str
, tunable_user_config_dir
);
480 str_append_char(&filename_str
, '/');
481 str_append_str(&filename_str
, p_user_str
);
482 retval
= str_stat(&filename_str
, &p_statbuf
);
483 if (!vsf_sysutil_retval_is_error(retval
))
485 /* Security - file ownership check now in vsf_parseconf_load_file() */
486 vsf_parseconf_load_file(str_getbuf(&filename_str
), 1);
488 else if (vsf_sysutil_get_error() != kVSFSysUtilErrNOENT
)
490 die("error opening per-user config file");
492 str_free(&filename_str
);
493 vsf_sysutil_free(p_statbuf
);
497 calculate_chdir_dir(int anon_login
, struct mystr
* p_userdir_str
,
498 struct mystr
* p_chroot_str
,
499 struct mystr
* p_chdir_str
,
500 const struct mystr
* p_user_str
,
501 const struct mystr
* p_orig_user_str
)
505 const struct vsf_sysutil_user
* p_user
= str_getpwnam(p_user_str
);
508 die2("cannot locate user entry:", str_getbuf(p_user_str
));
510 str_alloc_text(p_userdir_str
, vsf_sysutil_user_get_homedir(p_user
));
511 if (tunable_user_sub_token
)
513 str_replace_text(p_userdir_str
, tunable_user_sub_token
,
514 str_getbuf(p_orig_user_str
));
517 if (anon_login
&& tunable_anon_root
)
519 str_alloc_text(p_chroot_str
, tunable_anon_root
);
521 else if (!anon_login
&& tunable_local_root
)
523 str_alloc_text(p_chroot_str
, tunable_local_root
);
524 if (tunable_user_sub_token
)
526 str_replace_text(p_chroot_str
, tunable_user_sub_token
,
527 str_getbuf(p_orig_user_str
));
530 /* If enabled, the chroot() location embedded in the HOMEDIR takes
533 if (!anon_login
&& tunable_passwd_chroot_enable
)
535 struct str_locate_result loc_result
;
536 loc_result
= str_locate_text(p_userdir_str
, "/./");
537 if (loc_result
.found
)
539 str_split_text(p_userdir_str
, p_chdir_str
, "/./");
540 str_copy(p_chroot_str
, p_userdir_str
);