简体   繁体   English

在java中编辑只读文件

[英]Edit read-only file in java

I have a protect, read-only, text file on my Linux operating system that I have to make edits to.我的 Linux 操作系统上有一个受保护的只读文本文件,我必须对其进行编辑。 Of course I can goto the terminal and type:当然,我可以转到终端并输入:

sudo gedit /etc/ppp/options

I would then have to type my sudo password.然后我必须输入我的 sudo 密码。 My question is how do I give my java program "sudo" privileges to be able to write to this file and save those edits?我的问题是如何赋予我的 java 程序“sudo”权限才能写入该文件并保存这些编辑? I figured once I have given the program these privileges I could then use a BufferedWriter or PrintWriter to make edits.我想一旦我给了程序这些权限,我就可以使用 BufferedWriter 或 PrintWriter 进行编辑。

To clarify, my program prompts the user for their administrative (sudo) password.为了澄清,我的程序提示用户输入他们的管理(sudo)密码。 How could I use this to grant access to this file for writing purposes?我如何使用它来授予对该文件的访问权限以用于写入目的?

My question is how do I give my java program "sudo" privileges to be able to write to this file and save those edits.我的问题是如何赋予我的 java 程序“sudo”权限,以便能够写入该文件并保存这些编辑。

I think that the simple answer is that you can't.我认为简单的答案是你不能。

Sudo works by checking a file to see if the user allowed to run a command, and optionally confirming his / her identity by asking for his / her password. Sudo 的工作原理是检查文件以查看用户是否允许运行命令,并可选择通过询问他/她的密码来确认他/她的身份。 If both of these check out, then the sudo command executes the requested command as a different user;如果这两个都检出,则sudo命令以不同的用户身份执行请求的命令; typically root .通常是root

However, in order to do this, the "sudo" program itself is implemented as a "setuid" program;然而,为了做到这一点,“sudo”程序本身被实现为“setuid”程序; ie a program that itself that runs with elevated privileges.本身以提升的权限运行的程序。 Without this, it cannot run the requested program "as root", and it can't even read the "/etc/sudoers" file.没有这个,它不能以“root”身份运行所请求的程序,甚至不能读取“/etc/sudoers”文件。

So can we emulate "sudo" in Java?那么我们可以在 Java 中模拟“sudo”吗?

Basically, no.基本上,没有。 To do this, you would need to make the java command a "setuid" program.为此,您需要将java命令设置为“setuid”程序。 But the java command can execute ANY java program written by anyone.但是java命令可以执行任何人编写的任何 java 程序。 So a "setuid" java would allow anyone to run any java program with administrator privileges.因此,“setuid” java将允许任何人以管理员权限运行任何 java 程序。 That would be insanely insecure.那将是非常不安全的。


So what are the alternatives?那么有哪些替代方案呢?

  1. You could use ProcessBuilder to "exec" the sudo command as an external command.您可以使用ProcessBuildersudo命令作为外部命令“执行”。 However, passing a password to sudo via its command line is not possible.但是,通过命令行将密码传递给sudo是不可能的。 So you would need to either:因此,您需要:

    • configure the sudoers file to allow the (specific) command to be executed without a password (potentially dangerous), or配置sudoers文件以允许在没有密码的情况下执行(特定)命令(可能很危险),或者

    • figure out a way to use the --askPass hook to request the user's password securely from Java, or找出一种使用--askPass钩子从 Java 安全地请求用户密码的方法,或者

    • use the --stdin option to read the user's password from standard input instead of the user's terminal.使用--stdin选项从标准输入而不是用户终端读取用户密码。

  2. Write a custom "setuid" (or "setgid") wrapper to execute the admin command.编写自定义“setuid”(或“setgid”)包装器来执行管理命令。 Note: "setuid" shell scripts are fundamentally insecure.注意:“setuid”shell 脚本根本上是不安全的。 AC / C++ wrapper is potentially the safest approach ... but you need to be sure that the wrapper can't be tricked into doing something dangerous / nasty by supplying clever arguments or environment variables. AC / C++ 包装器可能是最安全的方法......但您需要确保包装器不会通过提供聪明的参数或环境变量而被欺骗做一些危险/讨厌的事情。

  3. Don't do it in Java.不要在Java中这样做。 Get the user to run the privileged command from the command line;让用户从命令行运行特权命令; eg using sudo .例如使用sudo You could potentially make it easier for the user by providing a script (shell, perl, whatever) to handle anything complicated, do sanity checking, etcetera.您可以通过提供一个脚本(shell、perl 等)来处理任何复杂的事情、进行完整性检查等,从而使用户更容易使用。

My recommendation is approach #3 (in general).我的建议是方法#3(一般而言)。 It is the most secure and transparent approach.这是最安全和最透明的方法。 (As an administrator / power-user, I would be very uncomfortable with some complicated / opaque Java application asking for my password to run sudo commands on my system.) (作为管理员/高级用户,我会对一些复杂/不透明的 Java 应用程序要求我提供密码以在我的系统上运行sudo命令感到非常不舒服。)

WARNING警告

Doing this kind of thing safely and securely requires a good understanding of how UNIx / Linux security works.安全可靠地做这种事情需要很好地理解 UNIx / Linux 安全性是如何工作的。 There are numerous mistakes you could make (too many to list) that could compromise security, and allow users to run arbitrary programs as root, or other dangerous / destructive things.您可能会犯许多错误(太多无法列出),这些错误可能会危及安全性,并允许用户以 root 身份运行任意程序或其他危险/破坏性的东西。 Be careful, and get an real UNIX / Linux expert to review your implemented mechanism.小心,请一位真正的 UNIX/Linux 专家来审查您实现的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM