Solved

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

Posted on 2008-10-08
11
734 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 137
Handle Exceptions during instantiation 28 411
Problem to scan all sheets 3 95
Beginner to Unreal Engine 4 5 61
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now