summaryrefslogtreecommitdiff
path: root/roles/install_dhcp
diff options
context:
space:
mode:
Diffstat (limited to 'roles/install_dhcp')
-rw-r--r--roles/install_dhcp/files/fastd-services-api.php44
-rw-r--r--roles/install_dhcp/tasks/main.yml18
2 files changed, 62 insertions, 0 deletions
diff --git a/roles/install_dhcp/files/fastd-services-api.php b/roles/install_dhcp/files/fastd-services-api.php
new file mode 100644
index 0000000..4eedd73
--- /dev/null
+++ b/roles/install_dhcp/files/fastd-services-api.php
@@ -0,0 +1,44 @@
+#!/usr/bin/php -f
+<?php
+// add include "/etc/dhcpd.hosts.conf"; to your dhcp config
+$url = 'https://www.freifunk-myk.de/services/ips';
+$out = '/etc/dhcpd.hosts.conf';
+
+if(!is_writable($out)) die('Output file perms');
+
+if( ($data = file_get_contents($url)) === FALSE ) die('Error getting ips');
+$data = unserialize($data);
+
+$active=array();
+
+foreach($data as $host) {
+ if(!preg_match('/^[a-f0-9]{12}$/', $host['mac'])) {
+ trigger_error('Host mit falscher MAC?!', E_USER_WARNING);
+ }elseif(!preg_match('/^10\.222\.\(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)\.\(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)$/', $host['staticip'])) {
+ trigger_error('Host mit falscher IP?!'.$host['mac'], E_USER_WARNING);
+ }else{
+ $active[] = $host;
+ }
+}
+if(count($active) < 5) die('Less than 5 hosts? Database broken?');
+$oldhash = hash_file("sha256", $out);
+$fp=fopen($out, 'w');
+foreach($active as $host) {
+ fwrite($fp, "host host".$host['mac']." {"."\n");
+ fwrite($fp, "\thardware ethernet ".
+ substr($host['mac'],0,2).":".
+ substr($host['mac'],2,2).":".
+ substr($host['mac'],4,2).":".
+ substr($host['mac'],6,2).":".
+ substr($host['mac'],8,2).":".
+ substr($host['mac'],10,2).
+ ';'."\n");
+ fwrite($fp, "\tfixed-address ".$host['staticip'].';'."\n");
+ fwrite($fp, "}\n\n");
+}
+fclose($fp);
+$newhash = hash_file("sha256", $out);
+if($oldhash != $newhash) {
+ exec('systemctl restart dhcpd4.service');
+}
+?>
diff --git a/roles/install_dhcp/tasks/main.yml b/roles/install_dhcp/tasks/main.yml
index 60cac06..c8912be 100644
--- a/roles/install_dhcp/tasks/main.yml
+++ b/roles/install_dhcp/tasks/main.yml
@@ -9,6 +9,24 @@
path: /etc/dhcpd.hosts.conf
state: touch
+- name: install php
+ pacman:
+ name: php
+ state: present
+
+- name: copy fastd-services-api.php
+ copy:
+ src: fastd-services-api.php
+ dest: /etc/fastd-services-api.php
+
+- name: setup cronjob for fastd-services-api
+ cron:
+ name: fastd-services-api
+ minute: '*/10'
+ user: root
+ cron_file: fastd-api
+ job: '/usr/bin/php /etc/fastd-services-api.php'
+
- name: dhcpd.conf
template:
src: dhcpd.conf.j2