Original Post
Comments for the article Linux Game Development Part 3
Quote:
On a Linux system, you should write saved game files, configuration files, high score lists, etc. to the player’s home directory. You can determine the player’s home directory in your C/C++ program by reading the HOME environment variable, like this:
char* home_dir = getenv(“HOME”);
if (home_dir != NULL)
{
// build the path to your game files
}
#include <unistd.h>#include <sys/types.h>#include <pwd.h>#include <stdlib.h>struct passwd *passwd;if (NULL != (passwd = getpwuid(getuid())) { char *home_dir = passwd->pw_dir; /* ... */}Quote:
Original post by Codexus
@let_bound: Actually, I realize that maybe you meant to type "pw_dir" instead "pw_name" in which case your solution is somewhat OK but still it's recommended to use the "HOME" variable to allow people to change it.
Quote:
Original post by let_bound
Please don't do that. Changing an environment variable is dead easy and it'll cause your program to fail (at best) or to write in the wrong location.
Quote:
This will return the correct home directory (using the process ID) regardless of the environment variable.
Quote:
Original post by let_bound
As for allowing the user to change it, I disagree with it, if only because you actually must obtain a value (in order to read/write the files) and the environment variable can be unset, or incorrectly set.
Quote:
I personally find that environment variables are great for tweaking behaviour, but I don't think one should depend on them being correctly set, or set a all. I guess I wrote too much server-side code in my life to trust them. YMMV.
Quote:
Original post by Nathan Baum Quote:
Original post by let_bound
As for allowing the user to change it, I disagree with it, if only because you actually must obtain a value (in order to read/write the files) and the environment variable can be unset, or incorrectly set.
And? The user would have to go out of their way to unset HOME, or set it to something that isn't a directory the program can write to. If they have, the program can simply display an error message. If HOME really is bad, the user has a serious problem with their system which will probably effect many other programs they run, so it needs fixing anyway.
Quote:
Quote:
I personally find that environment variables are great for tweaking behaviour, but I don't think one should depend on them being correctly set, or set a all. I guess I wrote too much server-side code in my life to trust them. YMMV.
Client-side code is completely different. If something is wrong, the user knows immediately and can fix it right there. Besides, if you can't depend upon HOME being set, you have bigger problems.
Quote:
Original post by let_bound
You're probably right, although I still feel uncomfortable with the idea of allowing potentially anyone to write to/read from someone else's home directory by mistake if the permissions aren't correct.
Quote:
I'm quite aware that client-side is completely different. I mentioned my writing server-side code to explain my view on the subject of software engineering. Sometimes I tend to go overkill with stability and security, at the expense of simplicity. It's in my nature to think of users as misbehaving bastards. [grin]
Quote:
Original post by Nathan Baum
Nobody sets their HOME to somebody else's home directory by mistake. Somebody doing that is doing it on purpose, and will already know if they can read from or write to files in somebody's home directory. They wouldn't need to use the game to do that.
Quote:
The only time using HOME would be a problem is if the game executable was setuid, since I could have HOME refer to a directory I wouldn't normally be able to write to. But relying on /etc/passwd to keep a setuid program secure would also be a problem, since I could make ~/.game a symlink to another user's directory.
Quote:
Quote:
I'm quite aware that client-side is completely different. I mentioned my writing server-side code to explain my view on the subject of software engineering. Sometimes I tend to go overkill with stability and security, at the expense of simplicity. It's in my nature to think of users as misbehaving bastards. [grin]
But this isn't the way to solve it. If overriding HOME enables a user to bypass security, then you have security problems which can't be reliably resolved simply by using /etc/passwd instead.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more