I am programming a small webinterface to control samba on Debian (kernel 2.6.32-5-686). I started by writing simple scripts to add users, create folders, etc. They worked fine when run as root. However, when run via web (apache + php) by using php: shell_exec() some of them didn't work. They look like this:
#!/bin/sh
#archive the userprofile
cp /home/samba/profile/$1 /home/samba/archive/$1
smbpasswd -LU $1 -x
userdel $1
exit 0
I tried using sudo to run the scripts, which doesn't seem to work. I added this line: (jupiter is the servers name)
%sudo jupiter=(ALL)NOPASSWD:/bin/<script>
to the /etc/sudoersfile, gave my www-data user the sudo group, nothing.
Then I read somewhere I should chmod 4755 and chown the scripts. It still didn't work.
At this point I was curious what the problem was and I redirected the STDERR to a file. I ran one of the small scripts, which would call cp, smbpasswdand userdel to delete the user and archive his share. All calls returned the exitcode 1, which AFAIK indicates that something went wrong. STDERR gave me a little clue:
userdel: cannot lock /etc/passwd; try again later.
This seems to be caused by some *.lock files, but there are none in my /etc/ folder.
At this point I got kinda frustrated and tried giving the www-data more root rights by adding it to the root group. Things still didn't work.
So far for my progress, I have no idea what to do next. My question is: How do I get simple scripts which seem to need root priveliges to run via the www-data user? I think it might be a problem with the /etc/passwd file, as I can imagine only the root-user has the right to alter it.
/bin. Custom scripts should be going into/usr/local/bininstead. This is to prevent possible conflicts with the package manager and also because it's the standard path as defined by the FHS. The world won't explode if you don't, but it's a good practice. – Patrick Sep 11 '12 at 12:14