Gert Gottschalk
Gert Gottschalk

Reputation: 1716

Accessing /var/log files from php

I want to use PHP to show some syslog info on a web page to remote monitor my home linux box. I.e. some stuff filtered with grep out of /var/log/daemon.log

<?php
  $output = `grep ddclient /var/log/daemon.log`;
  echo "<pre>$output</pre>";
?> 

Now the file /var/log/daemon.log is owned by root and the PHP user (www-data) has no access. So obviously the above returns empty.

What's the solution?

Thanks, Gert

Upvotes: 2

Views: 1238

Answers (2)

Barmar
Barmar

Reputation: 780899

This is a variant of Puggan Se's setuid solution, but a bit better IMHO.

Create a grep_ddclient.sh shell script, containing:

#!/bin/sh
grep ddclient /var/log/daemon.log

Then add the following to /etc/sudoers:

apache ALL=NOPASSWD: /path/to/grep_ddclient.sh

Then run sudo /path/to/grep_ddclient.sh from PHP

Upvotes: 2

Puggan Se
Puggan Se

Reputation: 5846

Alt 1: change read access of the file /var/log/daemon.log so apache can read it.

Alt 2: put grep ddclient /var/log/daemon.log in a shell file, and then activate the SETUID flag on it, and give apache the right to execute it

chown root:apache grep_ddclient.sh
chmod 550 grep_ddclient.sh
chmod +s grep_ddclient.sh

and then run grep_ddclient.sh from php

Upvotes: 1

Related Questions