1

Keeping it brief;

PHP script needs to run /etc/init.d/nagios reload, added apache ALL = (nagios) NOPASSWD: /etc/init.d/nagios to sudo-ers file, but to use SUDO PHP will need a TTY (I don't want to remove Defaults requiretty so I want to 'fake' or create a TTY for the PHP script so it can reload Nagios.

RedHat 6, PHP 5.3

Unfortunate
  • 21
  • 1
  • 4
  • You should invoke it over `screen` or `expect`. Wouldn't even know how to programmatically set up a pseudo-tty, but it's most certainly impossible from within PHP. – mario Nov 03 '11 at 13:01
  • Take a look at this: [Piping data to Linux program which expects a TTY (terminal)](http://stackoverflow.com/questions/4233808/piping-data-to-linux-program-which-expects-a-tty-terminal) – jcubic May 03 '17 at 11:14

1 Answers1

2

This can be done—sudo's requiretty option is relatively easy to defeat, so it doesn't really add much security by itself. If you are really sure that you want to leave it on (maybe you need to support running your script on users' systems which have requiretty on by default?) then making a tty may really be the right solution.

This would probably be an enormous pain in straight PHP, though. I've searched the reference docs at php.net, and I don't see any indication that it has any builtin utility functions for allocating or using ptys. If you really want to do it in pure PHP, you'll probably need to refer to the glibc source code for the forkpty() call (including the other dependencies it has, like openpty(), getpt(), grantpt(), unlockpt(), etc). At its core, this will involve open()'ing the pseudoterminal master device at /dev/ptmx to get a master PT file descriptor, doing a chown and a TIOCSPTLCK ioctl, then determining which device in /dev/pts is the corresponding slave fd and opening that.

But I'd highly recommend doing the tty faking part outside of PHP, probably by popen-ing some sudo wrapper script in Python (which has some nice convenient functions in the pty module) or even bash + something like socat.

the paul
  • 8,972
  • 1
  • 36
  • 53