From 8bb4c91b58e429981457d9b5bd71d8b31be831f0 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Fri, 11 Aug 2017 21:41:26 +0800 Subject: [PATCH] add return value to show the result of acl_chroot_uid than just broken when error happened. --- lib_acl/changes.txt | 3 ++ lib_acl/include/stdlib/unix/acl_chroot_uid.h | 2 +- lib_acl/src/stdlib/sys/unix/acl_chroot_uid.c | 56 ++++++++++++-------- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/lib_acl/changes.txt b/lib_acl/changes.txt index 28ab69613..388b7836e 100644 --- a/lib_acl/changes.txt +++ b/lib_acl/changes.txt @@ -1,6 +1,9 @@ 修改历史列表: ------------------------------------------------------------------------ +603) 2017.8.11 +603.1) stable: acl_chroot_uid 内部如果出错则不会自动崩溃,给是返回 -1 通知上层 + 602) 2017.8.10 602.1) rename: acl_sane_bind 改名为 acl_inet_bind 602.2) feature: acl_inet_listen/acl_inet_bind/acl_vstream_listen_ex 增 flag 标志位 diff --git a/lib_acl/include/stdlib/unix/acl_chroot_uid.h b/lib_acl/include/stdlib/unix/acl_chroot_uid.h index eebadef3f..a46c5aaff 100644 --- a/lib_acl/include/stdlib/unix/acl_chroot_uid.h +++ b/lib_acl/include/stdlib/unix/acl_chroot_uid.h @@ -10,7 +10,7 @@ extern "C" { /* External interface. */ -extern void acl_chroot_uid(const char *, const char *); +extern int acl_chroot_uid(const char *, const char *); #endif /* ACL_UNIX */ diff --git a/lib_acl/src/stdlib/sys/unix/acl_chroot_uid.c b/lib_acl/src/stdlib/sys/unix/acl_chroot_uid.c index f8312f2bb..f598407fe 100644 --- a/lib_acl/src/stdlib/sys/unix/acl_chroot_uid.c +++ b/lib_acl/src/stdlib/sys/unix/acl_chroot_uid.c @@ -24,51 +24,60 @@ /* chroot_uid - restrict the damage that this program can do */ -void acl_chroot_uid(const char *root_dir, const char *user_name) +int acl_chroot_uid(const char *root_dir, const char *user_name) { struct passwd *pwd; - uid_t uid = 0; - gid_t gid; - char tbuf[256]; + uid_t uid = 0; + gid_t gid; + int err = 0; /* * Look up the uid/gid before entering the jail, and save them so they * can't be clobbered. Set up the primary and secondary groups. */ if (user_name != 0) { - if ((pwd = getpwnam(user_name)) == 0) - acl_msg_fatal("unknown user: %s", user_name); + if ((pwd = getpwnam(user_name)) == 0) { + acl_msg_error("unknown user: %s", user_name); + return -1; + } + uid = pwd->pw_uid; gid = pwd->pw_gid; - if (setgid(gid) < 0) - acl_msg_fatal("setgid(%ld): %s", (long) gid, - acl_last_strerror(tbuf, sizeof(tbuf))); - if (initgroups(user_name, gid) < 0) - acl_msg_fatal("initgroups: %s", - acl_last_strerror(tbuf, sizeof(tbuf))); + if (setgid(gid) < 0) { + acl_msg_error("setgid(%ld): %s", (long) gid, + acl_last_serror()); + err++; + } + if (initgroups(user_name, gid) < 0) { + acl_msg_error("initgroups: %s", acl_last_serror()); + err++; + } } /* * Enter the jail. */ if (root_dir) { - if (chroot(root_dir)) - acl_msg_fatal("chroot(%s): %s", root_dir, - acl_last_strerror(tbuf, sizeof(tbuf))); - if (chdir("/")) - acl_msg_fatal("chdir(/): %s", - acl_last_strerror(tbuf, sizeof(tbuf))); + if (chroot(root_dir)) { + acl_msg_error("chroot(%s): %s", + root_dir, acl_last_serror()); + err++; + } else if (chdir("/")) { + acl_msg_error("chdir(/): %s", acl_last_serror()); + err++; + } } /* * Drop the user privileges. */ if (user_name != 0) { - if (setuid(uid) < 0) - acl_msg_fatal("setuid(%ld): %s", (long) uid, - acl_last_strerror(tbuf, sizeof(tbuf))); + if (setuid(uid) < 0) { + acl_msg_error("setuid(%ld): %s", + (long) uid, acl_last_serror()); + err++; + } } - /* * Give the desperate developer a clue of what is happening. @@ -77,6 +86,7 @@ void acl_chroot_uid(const char *root_dir, const char *user_name) acl_msg_info("chroot %s user %s", root_dir ? root_dir : "(none)", user_name ? user_name : "(none)"); + + return err ? -1 : 0; } #endif /* ACL_UNIX */ -