TSIG.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /*
  3. * License Information:
  4. *
  5. * Net_DNS: A resolver library for PHP
  6. * Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. define('NET_DNS_DEFAULT_ALGORITHM', 'hmac-md5.sig-alg.reg.int');
  23. define('NET_DNS_DEFAULT_FUDGE', 300);
  24. /* Net_DNS_RR_TSIG definition {{{ */
  25. /**
  26. * A representation of a resource record of type <b>TSIG</b>
  27. *
  28. * @package Net_DNS
  29. */
  30. class Net_DNS_RR_TSIG extends Net_DNS_RR
  31. {
  32. /* class variable definitions {{{ */
  33. var $name;
  34. var $type;
  35. var $class;
  36. var $ttl;
  37. var $rdlength;
  38. var $rdata;
  39. var $time_signed;
  40. var $fudge;
  41. var $mac_size;
  42. var $mac;
  43. var $original_id;
  44. var $error;
  45. var $other_len;
  46. var $other_data;
  47. var $key;
  48. /* }}} */
  49. /* class constructor - RR(&$rro, $data, $offset = '') {{{ */
  50. function Net_DNS_RR_TSIG(&$rro, $data, $offset = '')
  51. {
  52. $this->name = $rro->name;
  53. $this->type = $rro->type;
  54. $this->class = $rro->class;
  55. $this->ttl = $rro->ttl;
  56. $this->rdlength = $rro->rdlength;
  57. $this->rdata = $rro->rdata;
  58. if ($offset) {
  59. if ($this->rdlength > 0) {
  60. list($alg, $offset) = Net_DNS_Packet::dn_expand($data, $offset);
  61. $this->algorithm = $alg;
  62. $d = unpack("\@$offset/nth/Ntl/nfudge/nmac_size", $data);
  63. $time_high = $d['th'];
  64. $time_low = $d['tl'];
  65. $this->time_signed = $time_low;
  66. $this->fudge = $d['fudge'];
  67. $this->mac_size = $d['mac_size'];
  68. $offset += 10;
  69. $this->mac = substr($data, $offset, $this->mac_size);
  70. $offset += $this->mac_size;
  71. $d = unpack("@$offset/noid/nerror/nolen", $data);
  72. $this->original_id = $d['oid'];
  73. $this->error = $d['error'];
  74. $this->other_len = $d['olen'];
  75. $offset += 6;
  76. $odata = substr($data, $offset, $this->other_len);
  77. $d = unpack('nodata_high/Nodata_low', $odata);
  78. $this->other_data = $d['odata_low'];
  79. }
  80. } else {
  81. if (strlen($data) && preg_match('/^(.*)$/', $data, $regs)) {
  82. $this->key = $regs[1];
  83. }
  84. $this->algorithm = NET_DNS_DEFAULT_ALGORITHM;
  85. $this->time_signed = time();
  86. $this->fudge = NET_DNS_DEFAULT_FUDGE;
  87. $this->mac_size = 0;
  88. $this->mac = '';
  89. $this->original_id = 0;
  90. $this->error = 0;
  91. $this->other_len = 0;
  92. $this->other_data = '';
  93. // RFC 2845 Section 2.3
  94. $this->class = 'ANY';
  95. }
  96. }
  97. /* }}} */
  98. /* Net_DNS_RR_TSIG::rdatastr() {{{ */
  99. function rdatastr()
  100. {
  101. $error = $this->error;
  102. if (! $error) {
  103. $error = 'UNDEFINED';
  104. }
  105. if (strlen($this->algorithm)) {
  106. $rdatastr = $this->algorithm . '. ' . $this->time_signed . ' ' .
  107. $this->fudge . ' ';
  108. if ($this->mac_size && strlen($this->mac)) {
  109. $rdatastr .= ' ' . $this->mac_size . ' ' . base64_encode($this->mac);
  110. } else {
  111. $rdatastr .= ' 0 ';
  112. }
  113. $rdatastr .= ' ' . $this->original_id . ' ' . $error;
  114. if ($this->other_len && strlen($this->other_data)) {
  115. $rdatastr .= ' ' . $this->other_data;
  116. } else {
  117. $rdatastr .= ' 0 ';
  118. }
  119. } else {
  120. $rdatastr = '; no data';
  121. }
  122. return($rdatastr);
  123. }
  124. /* }}} */
  125. /* Net_DNS_RR_TSIG::rr_rdata($packet, $offset) {{{ */
  126. function rr_rdata($packet, $offset)
  127. {
  128. $rdata = '';
  129. $sigdata = '';
  130. if (strlen($this->key)) {
  131. $key = $this->key;
  132. $key = ereg_replace(' ', '', $key);
  133. $key = base64_decode($key);
  134. $newpacket = $packet;
  135. $newoffset = $offset;
  136. array_pop($newpacket->additional);
  137. $newpacket->header->arcount--;
  138. $newpacket->compnames = array();
  139. /*
  140. * Add the request MAC if present (used to validate responses).
  141. */
  142. if (isset($this->request_mac)) {
  143. $sigdata .= pack('H*', $this->request_mac);
  144. }
  145. $sigdata .= $newpacket->data();
  146. /*
  147. * Don't compress the record (key) name.
  148. */
  149. $tmppacket = new Net_DNS_Packet;
  150. $sigdata .= $tmppacket->dn_comp(strtolower($this->name), 0);
  151. $sigdata .= pack('n', Net_DNS::classesbyname(strtoupper($this->class)));
  152. $sigdata .= pack('N', $this->ttl);
  153. /*
  154. * Don't compress the algorithm name.
  155. */
  156. $tmppacket->compnames = array();
  157. $sigdata .= $tmppacket->dn_comp(strtolower($this->algorithm), 0);
  158. $sigdata .= pack('nN', 0, $this->time_signed);
  159. $sigdata .= pack('n', $this->fudge);
  160. $sigdata .= pack('nn', $this->error, $this->other_len);
  161. if (strlen($this->other_data)) {
  162. $sigdata .= pack('nN', 0, $this->other_data);
  163. }
  164. $this->mac = mhash(MHASH_MD5, $sigdata, $key);
  165. $this->mac_size = strlen($this->mac);
  166. /*
  167. * Don't compress the algorithm name.
  168. */
  169. unset($tmppacket);
  170. $tmppacket = new Net_DNS_Packet;
  171. $rdata .= $tmppacket->dn_comp(strtolower($this->algorithm), 0);
  172. $rdata .= pack('nN', 0, $this->time_signed);
  173. $rdata .= pack('nn', $this->fudge, $this->mac_size);
  174. $rdata .= $this->mac;
  175. $rdata .= pack('nnn',$packet->header->id,
  176. $this->error,
  177. $this->other_len);
  178. if ($this->other_data) {
  179. $rdata .= pack('nN', 0, $this->other_data);
  180. }
  181. }
  182. return($rdata);
  183. }
  184. /* }}} */
  185. /* Net_DNS_RR_TSIG::error() {{{ */
  186. function error()
  187. {
  188. if ($this->error != 0) {
  189. $rcode = Net_DNS::rcodesbyval($error);
  190. }
  191. return $rcode;
  192. }
  193. /* }}} */
  194. }
  195. /* }}} */
  196. /* VIM settings {{{
  197. * Local variables:
  198. * tab-width: 4
  199. * c-basic-offset: 4
  200. * soft-stop-width: 4
  201. * c indent on
  202. * expandtab on
  203. * End:
  204. * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  205. * vim<600: sw=4 ts=4
  206. * }}} */
  207. ?>