Skip to content

Commit

Permalink
fix(net.admin): added dhclient hook files (#4645)
Browse files Browse the repository at this point in the history
* Added dhclient hook scripts

Signed-off-by: pierantoniomerlino <pierantonio.merlino@eurotech.com>

* Improved dhclient hook scripts managemenet

Signed-off-by: pierantoniomerlino <pierantonio.merlino@eurotech.com>

* Updated hook script; improved installation

Signed-off-by: pierantoniomerlino <pierantonio.merlino@eurotech.com>

---------

Signed-off-by: pierantoniomerlino <pierantonio.merlino@eurotech.com>
  • Loading branch information
pierantoniomerlino committed May 12, 2023
1 parent 39d8fee commit 4f66bbc
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 5 deletions.
2 changes: 2 additions & 0 deletions kura/distrib/src/main/ant/build_equinox_distrib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ fi]]>
prefix="${build.output.name}/${install.folder}" />
<zipfileset file="src/main/resources/common/ifup-local.raspbian"
prefix="${build.output.name}/${install.folder}" />
<zipfileset file="src/main/resources/common/kura-dhclient-enter-hook"
prefix="${build.output.name}/${install.folder}" />

<zipfileset
file="${project.build.directory}/${build.output.name}/selinuxKura.pp"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# Don't delete the following entry. Add custom actions after it.
. /etc/kura-dhclient-resolv-hook
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ fi
chown bind:bind /etc/bind/rndc.key
chmod 600 /etc/bind/rndc.key

#set up dhclient hooks
cp ${INSTALL_DIR}/kura/install/kura-dhclient-enter-hook /etc/dhcp/dhclient-enter-hooks.d/zz-kura-dhclient-enter-hook

#set up logrotate - no need to restart as it is a cronjob
cp ${INSTALL_DIR}/kura/install/kura.logrotate /etc/logrotate-kura.conf

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ fi
chown bind:bind /etc/bind/rndc.key
chmod 600 /etc/bind/rndc.key

#set up dhclient hooks
cp ${INSTALL_DIR}/kura/install/kura-dhclient-enter-hook /etc/dhcp/dhclient-enter-hooks.d/zz-kura-dhclient-enter-hook

#set up logrotate - no need to restart as it is a cronjob
cp ${INSTALL_DIR}/kura/install/kura.logrotate /etc/logrotate-kura.conf

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ fi
chown bind:bind /etc/bind/rndc.key
chmod 600 /etc/bind/rndc.key

#set up dhclient hooks
cp ${INSTALL_DIR}/kura/install/kura-dhclient-enter-hook /etc/dhcp/dhclient-enter-hooks.d/zz-kura-dhclient-enter-hook

#set up logrotate - no need to restart as it is a cronjob
cp ${INSTALL_DIR}/kura/install/kura.logrotate /etc/logrotate-kura.conf

Expand Down
3 changes: 3 additions & 0 deletions kura/distrib/src/main/resources/raspberry-pi/kura_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ fi
chown bind:bind /etc/bind/rndc.key
chmod 600 /etc/bind/rndc.key

#set up dhclient hooks
cp ${INSTALL_DIR}/kura/install/kura-dhclient-enter-hook /etc/dhcp/dhclient-enter-hooks.d/zz-kura-dhclient-enter-hook

#set up logrotate - no need to restart as it is a cronjob
cp ${INSTALL_DIR}/kura/install/kura.logrotate /etc/logrotate-kura.conf

Expand Down
2 changes: 1 addition & 1 deletion kura/org.eclipse.kura.linux.net/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Export-Package: org.eclipse.kura.internal.board;version="1.0.0";x-internal:=true
org.eclipse.kura.net,
org.eclipse.kura.net.modem,
org.eclipse.kura.usb",
org.eclipse.kura.linux.net.dhcp;version="2.0.0";uses:="org.eclipse.kura.net.dhcp",
org.eclipse.kura.linux.net.dhcp;version="2.1.0";uses:="org.eclipse.kura.net.dhcp",
org.eclipse.kura.linux.net.dns;version="1.1.0";uses:="org.eclipse.kura.net,org.eclipse.kura.net.dns",
org.eclipse.kura.linux.net.iptables;version="1.1.0";uses:="org.eclipse.kura.net",
org.eclipse.kura.linux.net.modem;version="1.2.0";uses:="org.osgi.service.event,org.eclipse.kura.usb",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2020 Eurotech and/or its affiliates and others
* Copyright (c) 2011, 2023 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -30,8 +30,9 @@ public class DhcpClientManager {

private static final Logger logger = LoggerFactory.getLogger(DhcpClientManager.class);

private static DhcpClientTool dhcpClientTool = DhcpClientTool.NONE;
private static final String PID_FILE_DIR = "/var/run";
private static final String DHCLIENT_HOOK_SCRIPT_FILE = "/etc/kura-dhclient-resolv-hook";
private static DhcpClientTool dhcpClientTool = DhcpClientTool.NONE;
private final CommandExecutorService executorService;

static {
Expand Down Expand Up @@ -89,6 +90,14 @@ public void releaseCurrentLease(String interfaceName) throws KuraProcessExecutio
}
}

public static String getHookScriptFileName() {
if (dhcpClientTool == DhcpClientTool.DHCLIENT) {
return DHCLIENT_HOOK_SCRIPT_FILE;
} else {
return "";
}
}

private static boolean removePidFile(String interfaceName) {
boolean ret = true;
File pidFile = new File(getPidFilename(interfaceName));
Expand Down
2 changes: 1 addition & 1 deletion kura/org.eclipse.kura.net.admin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Import-Package: javax.microedition.io,
org.eclipse.kura.internal.linux.net.dns;version="[1.0,2.0)",
org.eclipse.kura.internal.linux.net.wifi;version="[1.0,2.0)",
org.eclipse.kura.linux.net;version="[1.0,2.0)",
org.eclipse.kura.linux.net.dhcp;version="[2.0,3.0)",
org.eclipse.kura.linux.net.dhcp;version="[2.1,3.0)",
org.eclipse.kura.linux.net.dns;version="[1.0,2.0)",
org.eclipse.kura.linux.net.iptables;version="[1.0,2.0)",
org.eclipse.kura.linux.net.modem;version="[1.1,2.0)",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*******************************************************************************
* Copyright (c) 2023 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech
*******************************************************************************/
package org.eclipse.kura.net.admin.visitor.linux;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.kura.KuraException;
import org.eclipse.kura.KuraIOException;
import org.eclipse.kura.core.net.AbstractNetInterface;
import org.eclipse.kura.core.net.NetworkConfiguration;
import org.eclipse.kura.core.net.NetworkConfigurationVisitor;
import org.eclipse.kura.executor.CommandExecutorService;
import org.eclipse.kura.linux.net.dhcp.DhcpClientManager;
import org.eclipse.kura.linux.net.dhcp.DhcpClientTool;
import org.eclipse.kura.net.NetInterfaceAddressConfig;
import org.eclipse.kura.net.NetInterfaceConfig;
import org.eclipse.kura.net.NetInterfaceStatus;
import org.eclipse.kura.net.NetInterfaceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DhcpClientConfigWriter implements NetworkConfigurationVisitor {

private static final Logger logger = LoggerFactory.getLogger(DhcpClientConfigWriter.class);
private static final String DEFAULT_HOOK_SCRIPT = "#!/bin/sh\n"
+ "\n"
+ "interfaces=\"\"\n"
+ "\n"
+ "for item in $interfaces; do \n"
+ " if [ \"$item\" = \"$interface\" ]; then\n"
+ " make_resolv_conf(){\n"
+ " logger \"Don't set DNS address for $interface\"\n"
+ " :\n"
+ " }\n"
+ " fi\n"
+ "done";

public DhcpClientConfigWriter() {
// Do nothing...
}

@Override
public void setExecutorService(CommandExecutorService executorService) {
// Not needed
}

@Override
public void visit(NetworkConfiguration config) throws KuraException {
if (DhcpClientManager.getTool() != DhcpClientTool.DHCLIENT) {
logger.debug("Hook scripts are supported only by dhclient. Do nothing.");
return;
}

List<String> lanInterfaceNames = new ArrayList<>();
List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = config
.getModifiedNetInterfaceConfigs();
for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
boolean isLan = ((AbstractNetInterface<?>) netInterfaceConfig)
.getInterfaceStatus() == NetInterfaceStatus.netIPv4StatusEnabledLAN;
if (isLan && (netInterfaceConfig.getType() == NetInterfaceType.ETHERNET
|| netInterfaceConfig.getType() == NetInterfaceType.WIFI)) {
lanInterfaceNames.add(netInterfaceConfig.getName());
}
}

lanInterfaceNames.sort(null);
writeDhcpClientConfig(lanInterfaceNames);
}

private void writeDhcpClientConfig(List<String> interfaceNames) {
String hookScriptFileName = DhcpClientManager.getHookScriptFileName();
if (hookScriptFileName == null || hookScriptFileName.isEmpty()) {
logger.debug("Hook script file name not defined. Do nothing.");
return;
}

try {
writeDhclientHookScript(interfaceNames, hookScriptFileName);
} catch (KuraIOException e) {
logger.error("Failed to write dhclient hook script", e);
}
}

private void writeDhclientHookScript(List<String> interfaceNames, String hookScriptFileName)
throws KuraIOException {
StringBuilder interfacesLine = new StringBuilder("interfaces=\"");
interfacesLine.append(interfaceNames.stream().collect(Collectors.joining(" ")));
interfacesLine.append("\"\n");
String hookScriptContent = DEFAULT_HOOK_SCRIPT.replace("interfaces=\"\"\n", interfacesLine);

Path hookScriptFilePath = Paths.get(hookScriptFileName);
try {
if (Files.exists(hookScriptFilePath) && Files.isReadable(hookScriptFilePath)) {
String currentHookScriptContent = new String(Files.readAllBytes(hookScriptFilePath),
StandardCharsets.UTF_8);
if (!hookScriptContent.equals(currentHookScriptContent)) {
writeFile(hookScriptFilePath, hookScriptContent);
}
} else {
writeFile(hookScriptFilePath, hookScriptContent);
}
} catch (IOException e) {
throw new KuraIOException(e, "Failed to update dhclient hook script");
}
}

private void writeFile(Path path, String content) throws IOException {
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private LinuxWriteVisitor() {
this.visitors.add(new WifiConfigWriter());
this.visitors.add(new PppConfigWriter());
this.visitors.add(new DhcpConfigWriter());
this.visitors.add(new DhcpClientConfigWriter());
this.visitors.add(new FirewallAutoNatConfigWriter());
}

Expand All @@ -51,7 +52,8 @@ public void visit(NetworkConfiguration config) throws KuraException {
visitor.visit(config);
}

// After every visit, unset the executorService. This must be set before every call.
// After every visit, unset the executorService. This must be set before every
// call.
this.executorService = null;
}

Expand Down

0 comments on commit 4f66bbc

Please sign in to comment.