什么是用于安全地创建临时目录的POSIX功能? [英] What's a POSIX function for creating a temporary directory securely?

查看:165
本文介绍了什么是用于安全地创建临时目录的POSIX功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在 / tmp 中创建临时目录的任务,
如何在 mkdtemp mkstemp 等,用于便携式代码?

For the task of creating a temporary directory in /tmp, how would one choose between mkdtemp, mkstemp, etc., for portable code?

推荐答案

假设您需要在其他用户可能具有写入权限的目录中创建临时目录。

I presume you need to create a temporary directory inside a directory where other users may have write permission.

作为管理员,您应该设置好每个用户自己的 TMPDIR (例如,使用 pam-tmpdir - 或更好的<一个href =http://glandium.org/blog/?p=217>每个进程名称空间,但这需要更多的设置)。不过,作为一个应用程序的作者,你不能假设这一点,所以你需要处理一个世界可写的 / tmp

As an administrator, you should set things up so that each user has its own TMPDIR (e.g. with pam-tmpdir — or even better with per-process namespaces, but that takes more setup). As an application writer, however, you can't assume this, so you need to cope with a world-writable /tmp.

这里的正确功能是 mkdtemp ,因为 mkstemp 只能创建常规文件。 mkdtemp 仅在POSIX.1 2008中引入,因此原则上可能在所有POSIX平台上都不可用。然而,它已经在主要平台上长时间可用:

The right function here is mkdtemp, since mkstemp can only create regular files. mkdtemp was only introduced in POSIX.1 2008, so in principle it might not be available on all POSIX platforms yet. However, it has been available on major platforms for a long time:

  • on OpenBSD since 2.2 (1997)
  • on FreeBSD since 2.2.7 (1998)
  • on NetBSD since 1.4 (1998)
  • on OSX since… 10.0?
  • on Linux (with Glibc) since Glibc 2.2 (1999)
  • in dietlibc since at least 2001
  • in uClibc since its beginning, I think
  • in MINIX 3
  • on Solaris only since Solaris 10

所以在实践中, code> mkdtemp 。如果您需要一个后备,请在您的来源中包含OpenBSD实现。

So in practice, you can safely go with mkdtemp. If you need a fallback, include the OpenBSD implementation in your source.

这篇关于什么是用于安全地创建临时目录的POSIX功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆