1837976 Members
2846 Online
110124 Solutions
New Discussion

Re: C Scripting question

 
SOLVED
Go to solution
Ridzuan Zakaria
Frequent Advisor

C Scripting question

Hi,

We have an exixting C program that use getlogin() fucntion to validate user who run the executable. The program run without any problem until we try to execute it from cron. It seems that getlogin() funtion will not return userid information if it run from cron.

The C program was design to allow only certain ids can execute it.

Is there a way to make this program work in cron.

Any helps is appreciated.

Thanks.

quest for perfections
8 REPLIES 8
Steven E. Protter
Exalted Contributor

Re: C Scripting question

Seeing the code would be useful.

cron has no environment. You need to set up PATH and sometimes SHLIB_PATH to make certain programs work right.

If you just want to know who the user is, you can check the environment variable $LOGNAME

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Pete Randall
Outstanding Contributor

Re: C Scripting question

When run by cron, it will take the userid of the crontab entry's owner. Whose crontab is this?


Pete

Pete
Sundar_7
Honored Contributor

Re: C Scripting question

hmm...you are not using the right system call :-)

===========================================
The getlogin() function retrieves the name of the user currently
logged in on a terminal associated with the calling process, as found
in /etc/utmp.

At least one of the standard input, standard output, or standard error
must be a terminal. For the first of these found that is a terminal,
a user must have logged in on that terminal, and that terminal must be
the controlling terminal of the session leader process of the calling
process's session.
============================================

Remember there is no terminal attached to the cron jobs. That is the very reason why your getlogin() is not working.

Use getuid() to get the userid and pass it on to the getpw() to get the username and then compare with the allowed list of usernames.
Learn What to do ,How to do and more importantly When to do ?
Ridzuan Zakaria
Frequent Advisor

Re: C Scripting question

Hi,

Try this below code:

#include
#include

main ()
{

int user;

user=getlogin();
printf("Hello %s\n",user);

}

If I the above code as oracle id from command prompt, it will return "Hello oracle". But if run it from cron, it will return "Hello".

Thanks.


quest for perfections
Sundar_7
Honored Contributor
Solution

Re: C Scripting question

As I said above, getlogin() will not work when the process is not attached to a terminal.

Try this

#include
#include
#include
main()
{
struct passwd *test;
test=getpwuid(getuid());
printf("User : %s\n",test->pw_name);
}

I am not good at C, so test it before you use :-).
Learn What to do ,How to do and more importantly When to do ?

Re: C Scripting question


Hi,

I'm not sure, but you can try it. Rather directly using the Program in the cron, add the entry in cron as following:

su -

Hope this helps.

regards,
Ravi B Hiremath
Muthukumar_5
Honored Contributor

Re: C Scripting question

We can getlogin and cuserid call for this too. Zakaria you tried wrongly with int there.

See this program as,
# cat test.c
#include
#include

main()
{

char *user;
char *euser;

user=getlogin();
euser=cuserid(user);

printf ("USER=%s\tEUSER=%s\n",user,euser);
}

It will do it your requirement.
Easy to suggest when don't know about the problem!
Ridzuan Zakaria
Frequent Advisor

Re: C Scripting question

Thanks to those you have response to my questions.

I am using solution provided by Sundar to resolve my problem.
quest for perfections