-2

i am supposed to create a conf file in /etc/rsyslog.d ,restart rsyslog and when pgm exits it should remove the conf file.But in c i cannot create file in /etc/rsyslog.d/ it is not prompting for password like using popen() which is used execute a command through shell.I also cannot run the program as sudo since this is a part of the bigger module which runs like a normal application.So could someone please help me find a solution

Thanks

Nitish P
  • 43
  • 9

1 Answers1

0

Split out the bit that needs to run as root into a separate binary (or pack in it the same binary with an argument like -create-conffile).

The call system("sudo myapp -create-conffile") from within your application. If you need a graphical prompt, launch xterm to run sudo.

Nicholas Wilson
  • 9,435
  • 1
  • 41
  • 80
  • I am sorry i was not able to understand that. but here is what i tried In c program i created the ex.conf file in home directory and launched a popen("sudo ln -s /home/user/ex.conf /etc/rsyslog.d/example.conf") i created this way and while it exits i remove both the symbolic link using peopen("sudo rm /etc/rsyslog.d/example.conf") and the conf in home directory... but this method has overhead so i wanted to find a better and faster way of doing it. – Nitish P Apr 26 '13 at 15:40
  • That's pretty nasty: you don't want to be symlinking from /etc to your home directory. What exactly is the overhead you're worried about? Processes are cheap as chips, so forking one off to elevate with sudo and copy a file or remove it is perfectly fine. You should be able to just use `system()` rather than `popen()` for those calls. – Nicholas Wilson Apr 26 '13 at 15:44
  • Thanks but could u give me a small example of how to create file in /etc/rsyslog.d/ using system because i am new to programming and also new to linux so i am still learning both. – Nitish P Apr 26 '13 at 15:51
  • How about you make your file in /tmp instead of /home/user/ex.conf, then execute "sudo cp /tmp/ex.conf /etc/..." instead of "sudo ln -s ..." as you're doing now. I think you'll also find it helpful to get used to using documentation: "man system" should help you. Calling `system()` should be simpler for you than `popen()`. – Nicholas Wilson Apr 26 '13 at 16:04
  • ok but we need password access everytime to create files in / directories except for home is there a way of going around this.I searched the web and came across changing setuid() but did not work for me.. but i will try what you said Thanks for the advice – Nitish P Apr 26 '13 at 16:12
  • No, that's absolutely right. Please don't make setuid-root binaries until you've learned some more. The best you can do is to only prompt for a password once to launch a helper the first time you need it. sudo should remember your password for you for a while, so it's OK to ask the user to authenticate each time they do something as root because they won't get prompted all that often. – Nicholas Wilson Apr 26 '13 at 16:14