1

I have deployed a .NET Core webapplication on a Ubuntu 16.04 LTS server using nginx.

I'd like to find this directory:

# Location source code
/home/user-0123456/webapp

From within this location:

// Location compiled code and source to outside world
/var/webapp

In this situation, user-0123456 is fixed, but the fix should be generic. So i.e., it could be different in the future - but you can assume there will always be one user.

I did came across this post, but both these lines:

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

return /var/www instead of /home/user-0123456/webapp.

Question

So how can I find the root directory of my 'webapp'-dir in the home folder from inside another path?

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
M. Douglas
  • 65
  • 2
  • 10
  • They return /var/www because the server is running as a user whose home is /var/www. There are maybe a dozen ways to proceed, using symlinks, another uid, a config file entry or code... Pick one and if you can't get it to work, ask a concrete question. – arnt Jun 05 '18 at 07:27

1 Answers1

1

Simple, you can get the HOME directory with:

System.Environment.GetEnvironmentVariable("HOME");

However, if your application runs under the account www-data, "home" will be the home-directory of the user www-data, and not the user you are currently logged in...

And if you want to get the root-directory of the web-application, that would be

System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);

But if you want to map the directory in /var/xyz to /home/user-123/xyz, then you do:

string user = System.Environment.GetEnvironmentVariable("USER");
string app = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);
app = app.Substring("/var/".Length);
string sourceDir = System.IO.Path.Combine("/home", user, app);

If you actually need the home directory of another user, this is how you get it by user-name:

string username = "user-123";
Mono.Unix.Native.Passwd pwd = Mono.Unix.Native.Syscall.getpwnam(username);
// pwd.pw_uid
string dir = pwd.pw_dir;
System.Console.WriteLine((dir));

The last one requires Mono.Posix compiled for .NET-Core/NetStandard2.0 and the native library that mono.posix wraps its Linux-syscalls around.

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
  • @M. Douglas: It seems to me you want the root-directory of the application, not the home-directory... – Stefan Steiger Jun 04 '18 at 18:15
  • I am almost there. Your last piece of code is what I am looking for. However, this returns `/home/www-data/webapp` instead of `/home/user-123/webapp` – M. Douglas Jun 04 '18 at 18:25
  • @M. Douglas: then your application is run as user www-data, and you somehow need to retrieve the original user. Then you can get the home-directory with the linux-syscall getpwnam. Added code for that. Basically, if you're lucky, there is a file with the original permissions, so you can get the user-uid by getting the primary group-uid of such a file (the primary-group-uid is the user-uid - by default - default can be changed). – Stefan Steiger Jun 04 '18 at 18:34
  • I do think this complecates things. Wouldn't there be a simpler way to retrieve the single user from the home-dir without using the library? – M. Douglas Jun 04 '18 at 18:45
  • The problem is, you don't have the home-dir of the original user, you only have the home-dir of the current user, which is www-data (in other words, not your original user). So you first need to figure out who the original user was, after that, it's easy. Problem is, you don't have that information anywhere, AFAIK. The home variable is just the home directory of the current user. Of course, if you have the user-name, you can just assume the home directory is /home/ (which doesn't necessarely need to be this way) and skip the syscall. – Stefan Steiger Jun 04 '18 at 18:52
  • Although I have to think about another solution, this would still be the correct solution. Marked green, thanks! – M. Douglas Jun 04 '18 at 19:30