C++添加linux用户

发布于 2024-09-06 03:38:05 字数 334 浏览 3 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

段念尘 2024-09-13 03:38:05

我注意到大多数添加和更改用户的主要实用程序都直接这样做,通常以不同的方式。 它们是 glibc 的一部分

fgetpwentgetpwnam, 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 call putpwent. For adding the user's password in the shadow file, they simply call fprintf 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.

揪着可爱 2024-09-13 03:38:05

您可以尝试使用 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-platform useradd utility. At its core, luseradd uses libuser's lu_user_add function to physically create the user.

See the docs/html folder in the source distribution for documentation.

腻橙味 2024-09-13 03:38:05

添加用户的级别有点太高了,无法进行系统调用。据我所知,也没有任何广泛使用的库。您最好的选择可能是使用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 the useradd program with appropriate options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文