我正在尝试看看passwd 和 gpasswd 程序的源代码。这些工具会检查设置为零的有效 UID 以成功完成执行。但我是 C 菜鸟,找不到检查进程是否由 UID == 0 运行的代码行。我将不胜感激任何提示或解决方案。
答案1
如果您指的是 的“shadow-utils”实现passwd
,您会发现测试这里:
/*
* The program behaves differently when executed by root than when
* executed by a normal user.
*/
amroot = (getuid () == 0);
在 中gpasswd
,测试被定义为两个阶段;第一的UID 已存储:
/*
* Make a note of whether or not this command was invoked by root.
* This will be used to bypass certain checks later on. Also, set
* the real user ID to match the effective user ID. This will
* prevent the invoker from issuing signals which would interfere
* with this command.
*/
bywho = getuid ();
然后用在宏观amroot
:
/* Indicate if gpasswd was called by root */
#define amroot (0 == bywho)