1

I'm running Bind and Apache on Debian and im doing dynamic updates on one of my zones with nsupdate via php script

The update Function:

function nsupdate($subdomain, $ip) {
 $domain = escapeshellcmd($subdomain . '.example.com');
 $ip = escapeshellcmd($ip);
 $data = "<<EOF
 server localhost
 zone example.com
 update delete $domain A
 update add $domain 10 A $ip
 send
 EOF";
 exec("/usr/bin/nsupdate -k /var/bind/keys/Kexample.com.+157+40387.private $data", $cmdout, $ret);
 return $ret;
}

I call this function (sub = 'test', ip = random valid IPv4 IP):

$ret = nsupdate($sub, $ip);
if ($ret != 0) {
    $msg = "Error! Code: $ret";
}else {
     $msg = "Success!";
}

The value of $ret is '1' and the $msg is 'Error! Code: 1', but the syslog shows

client ::1#9726/key example.com: signer "example.com" approved
client ::1#9726/key example.com: updating zone 'example.com/IN': deleting rrset at 'test.example.com' A
client ::1#9726/key example.com: updating zone 'example.com/IN': adding an RR at 'test.example.com' A

i can ping the subdomain and it returns the new IP. But i'd still like to know why the nsupdate call returns 1 and not the expected 0 on a succesful update.

update 1 here the $cmdout with debugging enabled for nsupdate

array(13) { 
[0]=> string(22) "Outgoing update query:" 
[1]=> string(59) ";; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 58402" 
[2]=> string(55) ";; flags:; ZONE: 1, PREREQ: 0, UPDATE: 2, ADDITIONAL: 1" 
[3]=> string(16) ";; ZONE SECTION:" 
[4]=> string(22) ";example.com. IN SOA" 
[5]=> string(0) "" 
[6]=> string(18) ";; UPDATE SECTION:" 
[7]=> string(33) "test.example.com. 0 ANY A" 
[8]=> string(45) "test.example.com. 10 IN A 22.22.22.22" 
[9]=> string(0) "" 
[10]=> string(22) ";; TSIG PSEUDOSECTION:"
[11]=> string(109) "example.com. 0 ANY TSIG hmac-md5.sig-alg.reg.int. 1439217988 300 16 kaHm1/GbMf0+cuIJO62lgw== 58402 NOERROR 0" 
[12]=> string(0) "" } 
Wipfelgilm
  • 11
  • 6

2 Answers2

2

I had the same "problem" bothering me for a while now. When I tried the above nsupdate commands on the command line everything was fine and the exit status (type in console directly after nsupdate -> echo $? returned 0) but called from php it was 1.

Until I realized that while being on the console I typed quit to end my nsupdate session. As soon as I added this line to the php script everything was fine aka exit code 0.

So change $data to $data = "<<EOF server localhost zone example.com update delete $domain A update add $domain 10 A $ip send quit EOF";

Arigion
  • 3,267
  • 31
  • 41
  • I have tested with quit, but still I got $ret = 2 – developer Oct 05 '17 at 22:13
  • The question is about $ret=1. Exit code 2 is about some other error. Try updating the record manually and/or check your log files. Probably the format or the access rights are incorrect. – Arigion Oct 12 '17 at 14:16
1

Try adding var_dump($cmdout);var_dump($ret); before return $ret. $msg is irrelevant.

Voro
  • 66
  • 2
  • $cmdout is just an empty Array, $ret is int(1) ( see Update 1 for $cmdout with debugging enabled ) – Wipfelgilm Aug 10 '15 at 14:57
  • Maybe the error is not related to the update operation itself but because of a "local" problem (writing to a local file like logs, locks or status). Check the user / permissions or try to run it as root. Also, attaching strace or gdb may be useful. – Voro Aug 10 '15 at 17:08