Udo G
Udo G

Reputation: 13141

How can I access files of different users and retain permission restrictions in Linux using Node.JS?

I'm trying to reimplement an existing server service in Node.JS. That service can be compared to a classic FTP server: authenticated users can read/create/modify files, but restricted to the permissions given to the matching system user name.

I'm pretty sure I can't have Node.JS run as root and switch users using seteuid() or alike since that would break concurrency.

Instead, can I let my Node.JS process run as ROOT and manually check permissions when accessing files? I'm thinking about some system call like "could user X create a file in directory Y?"

Otherwise, could I solve this by using user groups? Note that the service must be able to delete/modify a file created by the real system user, which may not set a special group just so that the service can access the file.

Upvotes: 2

Views: 257

Answers (1)

alandarev
alandarev

Reputation: 8635

Running node as root sounds dangerous, but I assume there aren't many options left for you. Most FTP servers run as root too, for the same reason. Though, it means you need to pay a severe attention to the security of the code you are going to run.

Now to the question:

You are asking whether you can reimplement the UNIX permissions control in node.js. Yes you can, but Should Not! It is almost 100% chance you will leave holes or miss edge cases Unix core has already taken care of.

Instead use the process.setuid(id) as you mentioned. It will not defeat concurrency, but you need to think of parallel concurrency rather than async now. That is an extra work, but will release you of an headache of reinventing the Unix security.

Alternatively, if all of the operations you want to carry on filesystem involve shell commands, then you can simply modify them to the following pattern:

runuser -l  userNameHere -c 'command'

Upvotes: 1

Related Questions