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

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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

617 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