check for duplicate host name

This commit is contained in:
Roland Gruber 2018-11-23 20:05:41 +01:00
parent 2d3f584bb4
commit bb9a1b1719
1 changed files with 296 additions and 224 deletions

View File

@ -7,7 +7,7 @@ $Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2008 Thomas Manninger Copyright (C) 2008 Thomas Manninger
2008 - 2017 Roland Gruber 2008 - 2018 Roland Gruber
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -61,6 +61,9 @@ class fixed_ip extends baseModule {
/** cached host entries (list of array('cn' => ..., 'iphostnumber' => ..., 'macaddress' => ...)) */ /** cached host entries (list of array('cn' => ..., 'iphostnumber' => ..., 'macaddress' => ...)) */
private $hostCache = null; private $hostCache = null;
/** cache for existing host entries */
private $existingDhcpHostsCache = null;
/** /**
* Returns true if this module can manage accounts of the current type, otherwise false. * Returns true if this module can manage accounts of the current type, otherwise false.
* *
@ -287,7 +290,9 @@ class fixed_ip extends baseModule {
*/ */
public function process_attributes() { public function process_attributes() {
$errors = array(); $errors = array();
if (!$this->isRootNode()) { if ($this->isRootNode()) {
return $errors;
}
$this->processed = true; $this->processed = true;
$this->reset_overlapd_ip(); $this->reset_overlapd_ip();
@ -350,7 +355,7 @@ class fixed_ip extends baseModule {
if (!empty($_POST['pc_'.$id])) $_POST['pc_'.$id] = trim($_POST['pc_'.$id]); if (!empty($_POST['pc_'.$id])) $_POST['pc_'.$id] = trim($_POST['pc_'.$id]);
if (!empty($_POST['pc_'.$id])) { if (!empty($_POST['pc_'.$id])) {
// Already use? // name already in use
if (in_array($_POST['pc_'.$id], $pcs) ) { if (in_array($_POST['pc_'.$id], $pcs) ) {
$error = true; $error = true;
} }
@ -391,7 +396,6 @@ class fixed_ip extends baseModule {
); );
$this->orderByIP(); $this->orderByIP();
} }
}
return $errors; return $errors;
} }
@ -407,7 +411,6 @@ class fixed_ip extends baseModule {
$return->addElement(new htmlStatusMessage('ERROR', _("Please fill out the DHCP settings first.")), true); $return->addElement(new htmlStatusMessage('ERROR', _("Please fill out the DHCP settings first.")), true);
return $return; return $return;
} }
else {
$this->initCache(); $this->initCache();
// auto-completion for host names // auto-completion for host names
$autoNames = array(); $autoNames = array();
@ -452,6 +455,7 @@ class fixed_ip extends baseModule {
foreach($this->fixed_ip AS $id=>$arr) { foreach($this->fixed_ip AS $id=>$arr) {
// pc name // pc name
$pcError = ""; $pcError = "";
$existsInDifferentDn = $this->hostNameExists($_POST['pc_'.$id]);
if (!$this->processed) { if (!$this->processed) {
$pcError = ""; $pcError = "";
} }
@ -467,6 +471,9 @@ class fixed_ip extends baseModule {
elseif (isset($_POST['pc_'.$id]) && !preg_match("/^[A-Za-z0-9\\._-]*$/", $_POST['pc_'.$id])) { elseif (isset($_POST['pc_'.$id]) && !preg_match("/^[A-Za-z0-9\\._-]*$/", $_POST['pc_'.$id])) {
$pcError = _("The PC name may only contain A-Z, a-z and 0-9."); $pcError = _("The PC name may only contain A-Z, a-z and 0-9.");
} }
elseif ($existsInDifferentDn !== false) {
$pcError = sprintf(_('This PC name already exists in %s. Use e.g. %s.'), $existsInDifferentDn[0], $existsInDifferentDn[1]);
}
$pcs[] = $this->fixed_ip[$id]['cn']; $pcs[] = $this->fixed_ip[$id]['cn'];
// MAC address // MAC address
@ -548,10 +555,58 @@ class fixed_ip extends baseModule {
$addHostButton->setIconClass('createButton'); $addHostButton->setIconClass('createButton');
$return->addElement($addHostButton , true); $return->addElement($addHostButton , true);
} }
}
return $return; return $return;
} }
/**
* Checks if the given host name already exists.
*
* @param string $name host name
* @return boolean|array false if not exists, DN + new name suggestion if exists
*/
private function hostNameExists($name) {
$this->fillExistingDhcpHosts();
foreach ($this->existingDhcpHostsCache as &$host) {
if ($name === $host['name']) {
$dn = $host['dn'];
if ($this->getAccountContainer()->isNewAccount || strpos($dn, $this->getAccountContainer()->dn_orig) === false) {
return array($dn, $this->getHostNameSuggestion($name));
}
}
}
return false;
}
/**
* Returns a suggestion for a free host name.
*
* @param string $name host name
* @return string suggested new name
*/
private function getHostNameSuggestion($name) {
$matches = array();
$number = 0;
$namePrefix = $name;
if (preg_match('/(.*[^0-9])([0-9]+)/', $name, $matches)) {
$namePrefix = $matches[1];
$number = $matches[2];
}
while (true) {
$number++;
$newName = $namePrefix . $number;
$found = false;
foreach ($this->existingDhcpHostsCache as &$host) {
if ($host['name'] === $newName) {
$found = true;
break;
}
}
if (!$found) {
return $newName;
}
}
}
/** /**
* Returns the HTML meta data for the add host page. * Returns the HTML meta data for the add host page.
* *
@ -869,6 +924,23 @@ class fixed_ip extends baseModule {
return $this->getAccountContainer()->dn_orig == $rootSuffix; return $this->getAccountContainer()->dn_orig == $rootSuffix;
} }
/**
* Fills the list of existing DHCP hosts.
*/
private function fillExistingDhcpHosts() {
if ($this->existingDhcpHostsCache != null) {
return $this->existingDhcpHostsCache;
}
$entries = searchLDAPByAttribute('cn', '*', 'dhcpHost', array('cn'), array('dhcp'));
$this->existingDhcpHostsCache = array();
foreach ($entries as $entry) {
$this->existingDhcpHostsCache[] = array(
'dn' => $entry['dn'],
'name' => $entry['cn'][0]
);
}
}
} }
?> ?>