C++添加linux用户
使用 C++ 在 Linux 中添加用户/组的最佳方法是什么?有一个我可以调用的库吗?我不想开始做这样的事情:
fopen("/etc/passwd", "a");
fprintf(tmp, "%s:x:%d:1:%s:/%s/%s:/bin/ksh\n", username, usernumber, commentfield, userdir, username);
fclose(tmp);
fopen("/etc/shadow", "a");
fprintf(stmp, "%s:*LK*:::::::\n", username);
fclose(stmp);
谢谢!
Whats the best way to add a user/group in linux using C++ is there a library I can call on? I dont want to start doing things like:
fopen("/etc/passwd", "a");
fprintf(tmp, "%s:x:%d:1:%s:/%s/%s:/bin/ksh\n", username, usernumber, commentfield, userdir, username);
fclose(tmp);
fopen("/etc/shadow", "a");
fprintf(stmp, "%s:*LK*:::::::\n", username);
fclose(stmp);
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我注意到大多数添加和更改用户的主要实用程序都直接这样做,通常以不同的方式。
和
和 它们是 glibc 的一部分。fgetpwent,getpwnam, getpw, < a href="http://linux.die.net/man/3/getpwent_r" rel="noreferrer">getpwent_r, putpwent, setpwent
我们可以看看以 busybox(通过 TinyLogin)为例。在
busybox/loginutils/adduser.c
,他们通过构建passwd结构将用户放入passwd文件中,然后调用putpwent
。为了将用户的密码添加到影子文件中,他们只需调用fprintf
并直接写入字符串即可。为了以现代方式对用户进行身份验证,有 Linux -PAM。但就添加用户而言,您可以在 pam_unix_passwd.c 他们称之为unix_update_db(),它调用 libpwdb,如果您使用它,则必须将其添加为项目的依赖项。
话虽如此,我还是为编写了几个实用程序来解析 passwd 和影子数据库并直接修改它们而感到内疚。它工作得很好,但这是在一个嵌入式系统上,我可以严格控制一切。我不必担心其他程序修改 passwd 数据库的竞争条件等问题。
如果您需要添加组,群组数据库。
I've noticed that most major utilities that add and change users do so directly, often in different ways. The functions you can use to modify the passwd and shadow files are exposed in
<pwd.h>
and in<sys/types.h>
, and they're part of glibc.fgetpwent, getpwnam, getpw, getpwent_r, putpwent, setpwent
We can look into how busybox (via TinyLogin) does it, as an example. In
busybox/loginutils/adduser.c
, they put a user in the passwd file by building the passwd structure and then callputpwent
. For adding the user's password in the shadow file, they simply callfprintf
and write the string directly.For authenticating users the modern way, there's Linux-PAM. But as far as adding users, you can see in pam_unix_passwd.c that they call unix_update_db(), which calls various functions in libpwdb, which you'd have to add as a dependency to your project if you use it.
That being said, I'm guilty of having written a couple utilities to parse the passwd and shadow databases and modify them directly. It worked fine, but this was on an embedded system where I could tightly control everything. I didn't have to worry about things like race conditions with other programs modifying the passwd db.
If you need to add a group, same goes for the group database.
您可以尝试使用 libuser 库。
与 libuser 一起分发的应用程序之一是
luseradd
,它是一个跨平台useradd
实用程序。luseradd
的核心是使用 libuser 的lu_user_add
函数来物理创建用户。请参阅源代码分发中的
docs/html
文件夹以获取文档。You might try using the libuser library.
One of the applications that are distributed with libuser is
luseradd
, a program that appears to be a cross-platformuseradd
utility. At its core,luseradd
uses libuser'slu_user_add
function to physically create the user.See the
docs/html
folder in the source distribution for documentation.添加用户的级别有点太高了,无法进行系统调用。据我所知,也没有任何广泛使用的库。您最好的选择可能是使用
system
调用来调用具有适当选项的useradd
程序。Adding a user is a bit too high-level for there to be a system call for it. As far as I'm aware, there aren't any widely used libraries for this either. Your best bet will probably be to use the
system
call to invoke theuseradd
program with appropriate options.