Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2008-10-08
Medium Priority
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  

Question by:hernan007
  • 4
  • 3
  • 2
  • +1
LVL 17

Expert Comment

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

Expert Comment

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.
LVL 17

Expert Comment

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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 45

Expert Comment

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.

Author Comment

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


Author Comment

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

Expert Comment

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

Author Comment

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

LVL 23

Accepted Solution

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);

LVL 23

Expert Comment

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)


Author Comment

ID: 22692850
thanks,  sweetfa3, sunnycoder  and  Mysidia,

Mysidia your suggestionn was exaclty that I need...

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 and use switch statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

572 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