on commit { set clip = binary-to-ascii(10, 8, ".", leased-address); set clhw = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6)); execute("/root/bin/dhcpevent.php", "commit", clip, clhw, option vendor-class-identifier); }
/root/bin/dhcpevent.php commit 172.20.21.209 0:f:77:12:bc:aa "Cisco Systems, Inc. IP Phone CP-7945G"
#!/usr/local/bin/php <?php $rdn = 'uid=root,ou=Users,dc=labma,dc=ru'; // DN to auth against LDAP $pass = 'superpass'; // Password $cont = "telexNumber"; // Attribute to fill with Cisco phone ID $ds = ldap_connect("pilot.labma.ru"); // Exit if not connected if (!$ds) exit (128); // Modern LDAP do not work on v1/v2 if (!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) exit (128); // That means phone is not for us if (!preg_match ("/^Cisco/", $argv[4])) exit (1); $r = ldap_bind($ds, $rdn, $pass); $mac = ""; $macar = explode (":", $argv[3]); if (count($macar) != 6) exit (128); // PHP LDAP client get keys in low $contl = strtolower($cont); // DHCP server send not padded MAC foreach ($macar as $byte) $mac .= str_pad ($byte, 2, 0, STR_PAD_LEFT); $sr = ldap_search( $ds, "dc=labma, dc=ru", "macAddress=$mac", array ("dn", $cont) ); if (ldap_count_entries($ds, $sr) != 1) exit (4); $info = ldap_get_entries($ds, $sr)[0]; if ((array_key_exists($contl, $info)) && ($argv[4] == $info[$contl][0])) exit (0); $res = ldap_mod_replace ( $ds, $info["dn"], array ($cont => $argv[4]) ); if (!$res) exit (128); ldap_close ($ds); exit (0); ?>
<VirtualHost *:6970> ServerAdmin webmaster@pbx.labma.ru DocumentRoot "/export/tftp" </VirtualHost> <Directory "/export/tftp/"> Options Indexes FollowSymLinks AllowOverride None Require ip 172.20.21.0/24 </Directory>
RewriteEngine On RewriteRule ^(.*)\.xml$ index.php [L]
<?php if (preg_match ("/\SEP(\w+).cnf.xml/", $_SERVER["REQUEST_URI"], $m)) $mac = $m[1]; else { $file = getcwd ().$_SERVER["REQUEST_URI"]; if (!file_exists ($file)) _fail(); header ("Content-type: text/xml"); header ('Content-Length: ' . filesize($file)); readfile ($file); exit (0); } $user = _getUser($mac); if (!$user) _fail(); $tmpl = "template.".$user["cisco"].".xml"; if (!file_exists ($tmpl)) _fail (); $xml = file_get_contents ("template.".$user["cisco"].".xml"); // getLoadA hardcoded, loadB - search directory $user["load"] = _getLoadA($user["cisco"]); foreach ($user as $key => $value) { $xml = preg_replace ("/\#\#$key\#\#/m", $value, $xml); } header ("Content-type: text/xml"); header ('Content-Length: ' . strlen($xml)); echo $xml; exit; function _getUser ($mac) { $rdn = 'uid=root,ou=Users,dc=labma,dc=ru'; // DN to auth against LDAP $pass = 'superpassword'; // Password $cont = "telexNumber"; // Attribute to fill with Cisco phone ID $ds = ldap_connect("pilot.labma.ru"); // Exit if not connected if (!$ds) exit (128); // Modern LDAP do not work on v1/v2 if (!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) exit (128); $r = ldap_bind($ds, $rdn, $pass); $sr = ldap_search( $ds, "dc=labma, dc=ru", "macAddress=$mac" ); if (ldap_count_entries($ds, $sr) != 1) { return null; } $info = ldap_get_entries($ds, $sr)[0]; $user = array(); $user ["label"] = $info["sn"][0]; $user ["phone"] = $info["uidnumber"][0]; if (preg_match ("/CP-(\d+)/", $info["telexnumber"][0], $m)) $user ["cisco"] = $m[1]; else return null; return $user; } function _getLoadA($cisco) { $list = array ( 3951 => "SIP3951.8-1-4a", 7906 => "SIP11.9-4-2SR1-1S", 7911 => "SIP11.9-4-2SR1-1S", 7931 => "SIP31.9-4-2SR2-2S", 7941 => "SIP41.9-4-2SR2-2S", 7945 => "SIP45.9-4-2SR2-2S", 7961 => "SIP41.9-4-2SR2-2S", 7965 => "SIP45.9-4-2SR2-2S", 8941 => "SIP894x.9-4-2SR3-1", 8845 => "sip8845_65.11-5-1SR1-1", 8865 => "sip8845_65.11-5-1SR1-1", ); if (!array_key_exists ($cisco, $list)) return ""; if (!file_exists (getcwd()."/".$list["cisco"].".loads")) return ""; return $list[$cisco]; } function _getLoadB($cisco) { $list = array ( 3951 => "SIP3951", 7906 => "SIP11", 7911 => "SIP11", 7931 => "SIP31", 7941 => "SIP41", 7945 => "SIP45", 7961 => "SIP41", 7965 => "SIP45", 8941 => "SIP894x", 8845 => "sip8845_65", 8865 => "sip8845_65", ); if (!array_key_exists ($cisco, $list)) return ""; $files = glob ($list[$cisco].".*.loads"); if (count($files) != 1) return ""; else return str_replace (".loads", "", $files[0]); } function _fail () { header ("HTTP/1.0 404 Not Found"); exit (0); } ?>
Source: https://habr.com/ru/post/319450/
All Articles