Unix Programming - Several questions about uid

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2004 > Several questions about uid





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Several questions about uid
music4

2004-02-25, 9:39 am

Greetings,

In my program, I want to do this:

char *username; // suppose this var. has value.

if ((pid = fork() == 0)
{
get uid from username // Question 1: how to do?

setuid(uid);

get the uid's home directory // Question 2: how to do?

execute the uid's .profile //Question 3: how to do?

setenv("MY_VAR", "aa"); // Question 4: how to handle memory?

exec("my_program");
}
else if (pid > 0)
{
waitpid(pid);
}
else
{
perror("fork error");
}

So there are totally four questions. Please help. Thanks in advance!

Evan


Maurizio Loreti

2004-02-25, 9:39 am

"music4" <music4@163.net> writes:

> get uid from username // Question 1: how to do?
> ...
> get the uid's home directory // Question 2: how to do?


If your environment is POSIX-compliant, you may check getpwnam(); that
function returns a pointer to a structure containing the broken out
fields of the /etc/passwd entry for a given user name.

> execute the uid's .profile //Question 3: how to do?
>
> setenv("MY_VAR", "aa"); // Question 4: how to handle memory?
>
> exec("my_program");


Build a shell command file containing

source /path_to_user_home/.profile
MY_VAR=aa
export MY_VAR
my_program

and exec "/bin/sh that_command_file".

--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: ybergv@cq.vasa.vg
Fletcher Glenn

2004-02-25, 1:34 pm



music4 wrote:
> Greetings,
>
> In my program, I want to do this:
>
> char *username; // suppose this var. has value.
>
> if ((pid = fork() == 0)
> {
> get uid from username // Question 1: how to do?
>
> setuid(uid);
>
> get the uid's home directory // Question 2: how to do?
>
> execute the uid's .profile //Question 3: how to do?
>
> setenv("MY_VAR", "aa"); // Question 4: how to handle memory?
>
> exec("my_program");
> }
> else if (pid > 0)
> {
> waitpid(pid);
> }
> else
> {
> perror("fork error");
> }
>
> So there are totally four questions. Please help. Thanks in advance!
>
> Evan
>
>


All of this is pointless unless you are running as root. Only root can
successfully execute setuid(). However, as has been pointed out,
getpwnam() will the the passwd entry from the name.

You also may have some difficulty executing the user's .profile file as
this is a shell script source document.

Perhaps the easiest way to accomplish your goal is (while running as
root), execute "su - username -c my_program" in a system() call.

--

Fletcher Glenn

Martin Blume

2004-02-27, 10:34 am

"music4" <music4@163.net> schrieb
> Greetings,
>
> get uid from username // Question 1: how to do?

man -k uid | grep "(3)"
....
getpwuid (3) - get password file entry
....

> get the uid's home directory // Question 2: how to do?

man -k uid | grep "(3)"
....
getpwuid (3) - get password file entry
....

>
> execute the uid's .profile //Question 3: how to do?

man -k execute | grep "(3)"
....
system (3) - execute a shell command
....

>
> setenv("MY_VAR", "aa"); // Question 4: how to handle memory?
>

Huh?
man setenv

Your man pages are broken. Install the man pages
(or better yet, info pages).

HTH
Martin



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com