Click to See Complete Forum and Search --> : C Question (Maybe too complex)
OK. Perhaps I'm in the wrong place. But I'll ask anyway. What I want to do is check authorization inside a C program, i.e. who is th person executing it. It is VERY VERY important that noone is able to spoof who they are to the program. What I have right now is:
valid1 = "root";
valid2 = "andy";
user = (char *)getenv("USER");
if ( (*user != *valid1) && (*user != *valid2) ) {
(void) fprintf(stderr, "Sorry, user '");
(void) fprintf(stderr, user);
(void) fprintf(stderr, "' is not authorized to use this program.\n");
exit(1);
}
Now that's all and good, but all the person has to do is change their $user env variable to 'root' or 'andy' and the security gets broken. Anything you can think of that will allow the program to look at the OFFICIAL executor of the program? I have a few ideas, but they are all very 'hack' type ideas (read: take a lot of code and time to implement, inefficient). Let me know.
Andy
w_bar@hotmail.com
dotzourb
04-25-1999, 04:46 PM
would it be easier to just code a password identifier so that users have to supply the correct password to use the program? I think there are just inherent problems in trying to use environment variables for security purposes. But that's just me. Anyone else have ideas?
X_console
04-25-1999, 05:07 PM
I agree with [bdotzourb[/b]. Use password protection for physical security as well. This way, someone walking up to the user's terminal and attempting to use the program would still require him to provide a password. You'll want to use a strong encryption algorithm for your password database of course.
The point is that I won't have to enter a password.
OK, ok. Here's the whole point of the program. I can do 'become 0' and get a root shell. Or 'become oracle' to get the oracle shell. I hate using su (have to enter password), and I don't want to work as root all the time (for obvious reasons), but the times that I need to do something as root, I want to do it quickly. I just need a little more security (by validating that it is me, and not a regular user).
To bo honest, I know this is not the most secure method. But if you can help, let me know :-)
dotzourb
04-25-1999, 05:21 PM
That's understandable. And if there is one thing that people hate about security, it's having to enter a password. But if it's something that is really important and needs to be guarded, i think it would be unwise to neglect this kind of precaution.
X_console
04-25-1999, 09:32 PM
Hm... so what you want, is to be able to run program foo, without having to type a password, yet others will not be able to run program foo? Well how about adding a group to it? Edit /etc/group and add a new group, say bar, and add yourself and root as members of bar. Next, change the group foo belongs to:
root# chgrp foo bar
Now only members of group bar will be able to run program foo. You might also want to chmod it to permissions 750 so the user has full read/write/execute access, group has read/execute, and everyone else has none.
I didn't think of that.... Maybe it was too fundamental :-)
Sounds like a good idea, I'll give it a shot.
Andy
Definitely a smart idea. Tried it out, all I did was chgrp it to adm, add myself to that group, and chmod 4110 the executable. Thanks for helping me see what should have been obvious :-)
-- Andy
X_console
04-26-1999, 12:22 AM
You're welcome. http://www.linuxnewbie.org/ubb/smile.gif
Gargan
04-26-1999, 03:28 AM
If this thread is still alive, I think a friend of mine had a similar situation. Use getlogin instead of user.
What do you mean getlogin? Even though I solved it, I'm still curious to know:
Perhaps post a code example paralleling what I have up top?
Thanks.
Forget it, I acted too quickly.
<man getlogin>
:-)
X_console
04-26-1999, 12:01 PM
getuid would also work and might actually be more secure. Since root is the only uid with 0, any other uid's can be blocked out.
------------------
"I am Oz! The great and powerful! Who are you? Who are you?"
-- The Wizard Of Oz
LloydM
05-07-1999, 07:44 AM
use getuid() or geteuid() to get uid or effective uid, then use getpwent() to find the name
mastersibn
01-31-2001, 07:24 AM
my favorite solution (so far) is:
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
int main( int argc, char **argv )
{
struct passwd pw_entry;
...
pw_entry = getpwuid( geteuid( ) );
printf( pw_entry->pw_name );
...
return 0;
}
You can replace ... with whatever code you want, obviously. I like this approach because it returns the name of the process owner, no matter what $USER is set to. Unfortunately, it seems to take a little bit more code, but that's no biggie (imo)... http://www.linuxnewbie.org/ubb/smile.gif
------------------
grab my gnupg key (http://jove.prohosting.com/~msibn/sibn-p.asc) if you feel so inclined.
cAPS lOCK? wHAT cAPS lOCK?
I cna ytpe 300 wrods pre mniuet!!!
an operating system has not just advantages...