C 和文件权限

发布于 2024-12-02 18:58:46 字数 231 浏览 1 评论 0原文

我正在使用 Ubuntu Lucid Lynx

是否可以通过具有只读权限的 C 程序在文件中写入数据。如果不可能,那么有什么方法可以让 sudo 访问 C 程序。

对于没有权限的文件,我会将数据保存在字符串中。然后我会使用写入选项打开文件:

FILE *fp = fopen(file_path,"w")
fputs(string,fp);
fclose(fp);

I am using Ubuntu Lucid Lynx

is it possible to write data in a file through a C program which has read only permission. If it's not possible, then is there any way to give sudo access to the C program.

For files without permissions, I would save the data in a string. Then i would open the file with write option:

FILE *fp = fopen(file_path,"w")
fputs(string,fp);
fclose(fp);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

清风无影 2024-12-09 18:58:46

不,您不能写入只读文件,这会破坏只读的全部意义。至于给予 ac 程序 root 访问权限,您始终可以以 root 身份或有权修改文件的人身份运行它。

No you cannot write to a read-only file, that would undermine the whole point of read-only. As for giving root access to a c program, you could always run it as root or as someone who has permissions to modify the file.

巡山小妖精 2024-12-09 18:58:46

如果您是文件的所有者,则可以使用 chmod 更改权限。如果您不是所有者,您可以使用 setuid 位来访问该文件,但这确实应该是避免了。

If you are the owner of the file you can change the permissions with chmod. If you are not the owner you can use setuid bit to acces the file, but this really should be avoided.

半寸时光 2024-12-09 18:58:46

程序在运行时无法将自身提升到 sudo 状态。
这真是一件好事。
如果可能的话,每种病毒都将有能力接管任何系统。

该程序需要从一开始就以适当的访问权限启动。

It is not possible to have the program elevate itself to sudo status while its running.
And that is a bloody good thing.
If that was possible every virus would have the ability to take over any system.

The program needs to be started with the proper access-rights from the start.

乖乖公主 2024-12-09 18:58:46

在 Unix 中,通常你想要做的就是使程序的可执行文件由 root 拥有,世界可执行文件,然后在其上设置 SUID 位。

chown root filename
chmod 4775 filename

请参阅 chomd 文档 了解这些位的确切含义,但总而言之,这意味着每当有人运行此可执行文件时,他们将可执行进程的用户 ID 设置为所有者(root)。

In Unix generally what you'd want to do is make the program's executable owned by root, world executable, then set the SUID bit on it.

chown root filename
chmod 4775 filename

See chomd docs for exactly what the bits mean, but all together this means that whenever someone runs this executable, they get the executable process' user ID set to the owner's (root).

帝王念 2024-12-09 18:58:46

您可以,但不是以标准的跨平台方式。

在 Unix 系统上,使用 chmod。它应该位于 中,

请参阅此链接< /a>.

如果您需要 root 权限,请尝试:

if(fp == NULL)
    execvp("sudo", argv);

You can, but not in a standard cross-platform way.

On Unix systems, use chmod. It should be in <sys/stat.h>

See this link.

If you need root privilage, try:

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