Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Open Source > Development Lib > Common Toolkit &&
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (2)
master
ZCC0.01
some-old-version
master
Branches (1)
Tags (2)
master
ZCC0.01
some-old-version
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (2)
master
ZCC0.01
some-old-version
lib-zc
/
cpp_src
/
stdlib
/
chroot_user.cpp
lib-zc
/
cpp_src
/
stdlib
/
chroot_user.cpp
chroot_user.cpp 4.13 KB
Copy Edit Raw Blame History
linuxmail authored 2026年03月04日 20:11 +08:00 . 语法检查
/*
* ================================
* eli960@qq.com
* http:/linuxmail.cn/
* 2016年12月03日
* ================================
*/
/* postfix source, src/util/chroot_uid.c */
// 仅在 Linux 系统下编译此代码
#ifdef __linux__
// 包含自定义标准库头文件
#include "zcc/zcc_stdlib.h"
// 包含用于获取用户信息的头文件
#include <pwd.h>
// 包含用于获取组信息的头文件
#include <grp.h>
// 包含错误号相关的头文件
#include <errno.h>
// 包含系统类型定义的头文件
#include <sys/types.h>
// 包含 Unix 系统调用的头文件
#include <unistd.h>
zcc_namespace_begin;
/**
* @brief 定义一个宏,用于记录警告信息并保持 errno 不变
* @param fmt 格式化字符串,用于指定警告信息的格式
* @param ... 可变参数,用于填充格式化字符串
*/
#define ___zwarning(fmt, args...) \
{ \
errno2 = errno; \
zcc_warning("chroot_user: " fmt, ##args); \
errno = errno2; \
}
/**
* @brief 将当前进程切换到指定的根目录并切换到指定用户
*
* 该函数首先根据用户名获取用户的 UID 和 GID,然后设置组 ID 和附属组列表,
* 接着将进程的根目录切换到指定目录,最后设置用户 ID 以降低权限。
*
* @param root_dir 要切换到的根目录路径,如果为 NULL 则不进行根目录切换
* @param user_name 要切换到的用户名,如果为 NULL 则不进行用户切换
* @return int 成功返回 0,失败返回 -1,并设置相应的错误号
*/
int chroot_user(const char *root_dir, const char *user_name)
{
// 用于保存临时的 errno 值
int errno2;
// 指向用户密码文件条目的指针
struct passwd *pwd;
// 用户的 UID
uid_t uid;
// 用户的 GID
gid_t gid;
// 初始化 UID 为 0(通常是 root 用户)
uid = 0;
// 如果指定了用户名
if (user_name != 0)
{
// 根据用户名获取用户的密码文件条目
if ((pwd = getpwnam(user_name)) == 0)
{
// 记录未知用户的警告信息
___zwarning("unknown user: %s", user_name);
// 设置错误号为 ENONET
errno = ENONET;
// 返回 -1 表示失败
return -1;
}
// 获取用户的 UID
uid = pwd->pw_uid;
// 获取用户的 GID
gid = pwd->pw_gid;
// 设置当前进程的组 ID
if (setgid(gid) < 0)
{
// 记录设置组 ID 失败的警告信息
___zwarning("setgid %ld : %m", (long)gid);
// 返回 -1 表示失败
return -1;
}
// 初始化进程的附属组列表
if (initgroups(user_name, gid) < 0)
{
// 记录初始化附属组列表失败的警告信息
___zwarning("initgroups: %m");
// 返回 -1 表示失败
return -1;
}
}
/*
* Enter the jail.
*/
// 如果指定了根目录
if (root_dir)
{
// 将当前进程的根目录切换到指定目录
if (chroot(root_dir))
{
// 记录根目录切换失败的警告信息
___zwarning("chroot (%s) : %m", root_dir);
// 返回 -1 表示失败
return -1;
}
// 将当前进程的工作目录切换到新根目录的根路径
if (::chdir("/"))
{
// 记录工作目录切换失败的警告信息
___zwarning("chdir (/): %m");
// 返回 -1 表示失败
return -1;
}
}
/*
* Drop the user privileges.
*/
// 如果指定了用户名
if (user_name != 0)
{
// 设置当前进程的用户 ID
if (setuid(uid) < 0)
{
// 记录设置用户 ID 失败的警告信息
___zwarning("setuid %ld: %m", (long)uid);
// 返回 -1 表示失败
return -1;
}
}
// 成功返回 0
return 0;
}
// 退出自定义命名空间
zcc_namespace_end;
#endif // __linux__
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

LIB-ZC 是一个Linux平台通用C/C++扩展库。兼容 Windows/Macos。 支持 GCC/CLANG/MINGW/MSVC。 支持 X64/AMD64/ARM64。 基本的数据结构封装,master/server服务框架,异步IO,协程,JSON解析,邮件解析,字符集探测,redis、memcache客户端,imap、pop客户端,httpd,websocket,sqlite3
Cancel

Releases

No release

The Open Source Evaluation Index is derived from the OSS Compass evaluation system, which evaluates projects around the following three dimensions

1. Open source ecosystem

  • Productivity: To evaluate the ability of open-source projects to output software artifacts and open-source value.
  • Innovation: Used to evaluate the degree of diversity of open source software and its ecosystem.
  • Robustness: Used to evaluate the ability of open-source projects to resist internal and external interference and self recover in the face of changing development environments.

2. Collaboration, People, Software

  • Collaboration: represents the degree and depth of collaboration in open source development behavior.
  • Observe the influence of core personnel in open source projects, and examine the evaluations of users and developers on open source projects from a third-party perspective.
  • Software: Evaluate the value of products exported from open-source projects and their ultimate destination. It is also a concrete manifestation of "open source software", one of the oldest mainstream directions in open source evaluation.

3. Evaluation model

    Based on the dimensions of "open source ecosystem" and "collaboration, people, and software", identify quantifiable indicators directly or indirectly related to this goal, quantitatively evaluate the health and ecology of open source projects, and ultimately form an open source evaluation index.

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/linuxmail/lib-zc.git
git@gitee.com:linuxmail/lib-zc.git
linuxmail
lib-zc
lib-zc
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /