Solved

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

Posted on 2008-10-08
11
731 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 nested-loops in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now