Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

run  administrative services  with c++ without of sudo passwords.

Posted on 2008-10-08
11
Medium Priority
?
763 Views
Last Modified: 2012-05-05
Dear Experts,

I have Debian Lenny running in an AMD Geode processor and  I need run  administrative services  with c++ for sample I need use the date function but I have problems because I need use "su" or "sudo"  password , I dont know how I can use it

I use :
system ("su mypassword");  
system ("date mydate");

but isn't work ...any idea?

thanks thanks  

cheers,
0
Comment
Question by:hernan007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:sweetfa2
ID: 22674721
As root user "chmod +s executable"
0
 
LVL 17

Expert Comment

by:sweetfa2
ID: 22674728
also try system("su -c date datetime");

The problem is the SU session is only for the first attempt at system call.  The subsequent attempt the authorisation is no longer current.  Therefore, you need to execute it all in one line.
0
 
LVL 17

Expert Comment

by:sweetfa2
ID: 22674923
If you want a program to change the date, everytime the user password changes you have to pull it out of somewhere, or recompile the application.  Therefore it is better to not do a sudo within the application if it can be helped and simply use the chmod +s approach to and change the ownership of the file to root with global executable, or executable within a group.

This way you are not tied in to password management, etc.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 22675000
Wouldnt it be better if your program gets launched with sudo permissions rather than your program trying to get sudo permissions mid-execution. What if you need to spend some time in a loop and then execute another command?

sudo ./myprogram

This would launch your program with sufficient permissions to execute some privileged commands. I am not a fan chmod +s for obvious reasons but if system("su -c date datetime"); works then it would be a good alternative.
0
 

Author Comment

by:hernan007
ID: 22678816
Thanks Sunnycoder and sweetfa2

the  system("su -c date datetime");   not works ...

I Use

char myDate[50]=" sudo -c date 041223002008";

system(myDate) ;

and I have the msg :   Uknown id:  041223002008

and the date  hasn't any change ...

I want only change the date with my date automatically without the su or sudo password.

any idea ? thanks

my best regards

0
 

Author Comment

by:hernan007
ID: 22679093
Also I  have changed, as root,  the date file with  chmod +s date  and nothing I have the same problem.   :(
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22679228
The chmod +s was meant to be on your executable not on date executable.

Does the date command that you are trying work fine from the command line? If yes, then did you try

sudo /path/to/my/program
0
 

Author Comment

by:hernan007
ID: 22680982
Dear Sunnycoder,

In my program Im using the serial ports and with the chmod +s  my program not run fine .. I don't have connection with my equipment (a datalogger) ,  
I send an instrucction to my datalogger and the answer is a date,  with this date I want update my PC.

 what can't I do?, I need update the date without the sudo or su  and via c/++ code

thanks
0
 
LVL 23

Accepted Solution

by:
Mysidia earned 2000 total points
ID: 22692701
Place the following in sudoers instead of whatever you have for username:
username    ALL=(ALL) NOPASSWD:  /bin/date


Then you can use sudo, and it won't prompt for a password while using the allowed command;  full path is required both here, and when running the
command with sudo, for security reasons.

This is better than 'su'  in that  you are not having to give the program a blank check to do anything as root, and no passwords.  Only the commands you list are available,  although in this example any options may be given to the 'date' command,  sudo also allows you to have some control over command line arguments.

Then use

char buffer[512];
char newdate[30] = "041223002008";
/* ^^^^^^^^^^^^^^^^^^^^^^  populate this with new date string */

sprintf(buffer, "sudo /bin/date +%.30s", newdate);
system(buffer);

0
 
LVL 23

Expert Comment

by:Mysidia
ID: 22692717
* To use sudo in a scripted method on some Linux distributions, you may also need to comment out a line in sudoers that says

Defaults    requiretty

By placing a '#'  symbol in front of it.

*The line you are commenting out essentially prevents sudo from working if a TTY has not been allocated;
i.e. stops sudo from being used by non-interactive scripts.

The reason 'requiretty' has sometimes been added by default is to do with the fact sudo can't stop display of user password while you're typing it in on a non-TTY.
 

or add  options using a user-specific defaults line such as

Defaults:username   env_reset, stay_setuid, !requiretty, !authenticate, mail_no_perms, mailto=(EMAIL ADDRESS TO SEND TO IF permissions are violated)

0
 

Author Comment

by:hernan007
ID: 22692850
thanks,  sweetfa3, sunnycoder  and  Mysidia,

Mysidia your suggestionn was exaclty that I need...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question