Project

General

Profile

Download (24.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	services.inc
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
/* include all configuration functions */
33
require_once("functions.inc");
34
require_once("dyndns.class");
35

    
36
function services_dhcpd_configure() {
37
	global $config, $g;
38

    
39
	/* kill any running dhcpd */
40
	killbypid("{$g['varrun_path']}/dhcpd.pid");
41

    
42
	$syscfg = $config['system'];
43
	$dhcpdcfg = $config['dhcpd'];
44

    
45
	/* DHCP enabled on any interfaces? */
46
	$dhcpdenable = false;
47
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
48
		if (isset($dhcpifconf['enable']) &&
49
			(($dhcpif == "lan") ||
50
			(isset($config['interfaces'][$dhcpif]['enable']) &&
51
			$config['interfaces'][$dhcpif]['if'] && (!$config['interfaces'][$dhcpif]['bridge']))))
52
			$dhcpdenable = true;
53
	}
54

    
55
	if (!$dhcpdenable)
56
		return 0;
57

    
58
	if ($g['booting'])
59
		echo "Starting DHCP service... ";
60
	else
61
		sleep(1);
62

    
63
	/* write dhcpd.conf */
64
	$fd = fopen("{$g['varetc_path']}/dhcpd.conf", "w");
65
	if (!$fd) {
66
		printf("Error: cannot open dhcpd.conf in services_dhcpd_configure().\n");
67
		return 1;
68
	}
69

    
70
	
71

    
72
	$dhcpdconf = <<<EOD
73
option domain-name "{$syscfg['domain']}";
74
default-lease-time 7200;
75
max-lease-time 86400;
76
authoritative;
77
log-facility local7;
78
ddns-update-style none;
79

    
80
EOD;
81

    
82
	$dhcpdifs = array();
83
	
84
	/*    loop through and deterimine if we need to setup
85
	 *    failover peer "bleh" entries
86
	 */
87
	$dhcpnum = 0;
88
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
89
		if($dhcpifconf['failover_peerip'] <> "") {
90
			/*
91
			 *    yep, failover peer is defined.
92
			 *    does it match up to a defined vip?
93
			 */
94
			$skew = 1;
95
			$a_vip = &$config['virtualip']['vip'];
96
			if(is_array($a_vip)) {
97
				foreach ($a_vip as $vipent) {
98
					$int = guess_interface_from_ip($dhcpifconf['failover_peerip']);
99
					$intip = find_interface_ip($int);
100
					$real_dhcpif = convert_friendly_interface_to_real_interface_name($dhcpif);
101
					if($int == $real_dhcpif) {
102
						/* this is the interface! */
103
						if($vipent['advskew'] == "0")
104
							$skew = 0;
105
					}
106
				}
107
			} else {
108
				log_error("Warning!  DHCP Failover setup and no CARP virtual IP's defined!");
109
			}
110
			if($skew > 0) {
111
				$type = "secondary";
112
				$dhcpdconf_pri  = "mclt 600;\n";
113
				$my_port = "520";
114
				$peer_port = "519";
115
			} else {
116
				$my_port = "519";
117
				$peer_port = "520";
118
				$type = "primary";
119
				$dhcpdconf_pri  = "split 128;\n";
120
				$dhcpdconf_pri .= "  mclt 600;\n";
121
			}
122
			$dhcpdconf .= <<<EOPP
123
failover peer "dhcp{$dhcpnum}" { 
124
  {$type};
125
  address {$intip};
126
  port {$my_port};
127
  peer address {$dhcpifconf['failover_peerip']};
128
  peer port {$peer_port};
129
  max-response-delay 60;
130
  max-unacked-updates 10;
131
  {$dhcpdconf_pri}
132
  load balance max seconds 3;
133
}
134

    
135
EOPP;
136
		$dhcpnum++;
137
		}
138
	}
139

    
140
	$dhcpnum = 0;
141

    
142
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
143

    
144
		$ifcfg = $config['interfaces'][$dhcpif];
145

    
146
		if (!isset($dhcpifconf['enable']) ||
147
			(($dhcpif != "lan") &&
148
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
149
			continue;
150

    
151
		$subnet = gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);
152
		$subnetmask = gen_subnet_mask($ifcfg['subnet']);
153

    
154
		$dnscfg = "";
155

    
156
		if ($dhcpifconf['domain']) {
157
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
158
		}
159

    
160
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
161
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
162
		} else if (isset($config['dnsmasq']['enable'])) {
163
			$dnscfg .= "	option domain-name-servers " . $ifcfg['ipaddr'] . ";";
164
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
165
			$dnscfg .= "	option domain-name-servers " . join(",", $syscfg['dnsserver']) . ";";
166
		}
167

    
168
		$dhcpdconf .= "subnet $subnet netmask $subnetmask {\n";
169
		$dhcpdconf .= "	pool {\n";
170

    
171
		/* is failover dns setup? */
172
		if (is_array($dhcpifconf['dnsserver']) && $dhcpifconf['dnsserver'][0] <> "") {
173
			$dhcpdconf .= "		option domain-name-servers {$dhcpifconf['dnsserver'][0]}";
174
			if($dhcpifconf['dnsserver'][1] <> "")
175
				$dhcpdconf .= ",{$dhcpifconf['dnsserver'][1]}";
176
			$dhcpdconf .= ";\n";
177
		}
178
		
179
		if($dhcpifconf['failover_peerip'] <> "") 
180
			$dhcpdconf .= "		deny dynamic bootp clients;\n";
181
		
182
		if (isset($dhcpifconf['denyunknown']))
183
		   $dhcpdconf .= "		deny unknown clients;\n";
184

    
185
		if ($dhcpifconf['gateway'])
186
			$routers = $dhcpifconf['gateway'];
187
		else
188
			$routers = $ifcfg['ipaddr'];
189

    
190
		if($dhcpifconf['failover_peerip'] <> "") {
191
			$dhcpdconf .= "		failover peer \"dhcp{$dhcpnum}\";\n";
192
			$dhcpnum++;
193
		}
194

    
195
		$dhcpdconf .= <<<EOD
196
		range {$dhcpifconf['range']['from']} {$dhcpifconf['range']['to']};
197
	}
198
	option routers {$routers};
199
$dnscfg
200

    
201
EOD;
202

    
203
		if ($dhcpifconf['defaultleasetime'])
204
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
205
		if ($dhcpifconf['maxleasetime'])
206
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
207

    
208
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
209
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
210
			$dhcpdconf .= "	option netbios-node-type 8;\n";
211
		}
212

    
213
		if ($dhcpifconf['next-server'])
214
			$dhcpdconf .= "	next-server {$dhcpifconf['next-server']};\n";
215
		if ($dhcpifconf['filename'])
216
			$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
217

    
218
		$dhcpdconf .= <<<EOD
219
}
220

    
221
EOD;
222

    
223
		/* add static mappings */
224
		if (is_array($dhcpifconf['staticmap'])) {
225

    
226
			$i = 0;
227
			foreach ($dhcpifconf['staticmap'] as $sm) {
228
				$dhcpdconf .= <<<EOD
229
host s_{$dhcpif}_{$i} {
230
	hardware ethernet {$sm['mac']};
231

    
232
EOD;
233
				if ($sm['ipaddr'])
234
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
235

    
236
				$dhcpdconf .= "}\n";
237
				$i++;
238
			}
239
		}
240

    
241
		$dhcpdifs[] = $ifcfg['if'];
242
	}
243

    
244
	fwrite($fd, $dhcpdconf);
245
	fclose($fd);
246

    
247
	/* create an empty leases database */
248
	touch("{$g['vardb_path']}/dhcpd.leases");
249

    
250
	/* fire up dhcpd */
251
	mwexec("/usr/local/sbin/dhcpd -cf {$g['varetc_path']}/dhcpd.conf " .
252
		join(" ", $dhcpdifs));
253

    
254
	if ($g['booting']) {
255
                print "done.\n";
256
	}
257

    
258
	return 0;
259
}
260

    
261
function interfaces_staticarp_configure($if) {
262
        global $config, $g;
263
        
264
        $ifcfg = $config['interfaces'][$if];
265

    
266
        /* Enable staticarp, if enabled */
267
        if(isset($config['dhcpd'][$if]['staticarp'])) {
268
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " );
269
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
270
                if (is_array($config['dhcpd'][$if]['staticmap'])) {
271

    
272
                        foreach ($config['dhcpd'][$if]['staticmap'] as $arpent) {
273
                                mwexec("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac']));
274

    
275
                        }
276
                        
277
                }
278
        } else {
279
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " );
280
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
281
        }
282

    
283
        return 0;
284
}
285

    
286
function services_dhcrelay_configure() {
287
	global $config, $g;
288

    
289
	/* kill any running dhcrelay */
290
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
291

    
292
	$dhcrelaycfg = $config['dhcrelay'];
293

    
294
	/* DHCPRelay enabled on any interfaces? */
295
	$dhcrelayenable = false;
296
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
297
		if (isset($dhcrelayifconf['enable']) &&
298
			(($dhcrelayif == "lan") ||
299
			(isset($config['interfaces'][$dhcrelayif]['enable']) &&
300
			$config['interfaces'][$dhcrelayif]['if'] && (!$config['interfaces'][$dhcrelayif]['bridge']))))
301
			$dhcrelayenable = true;
302
	}
303

    
304
	if (!$dhcrelayenable)
305
		return 0;
306

    
307
	if ($g['booting'])
308
		echo "Starting DHCP relay service... ";
309
	else
310
		sleep(1);
311

    
312
	$dhcrelayifs = array();
313
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
314

    
315
		$ifcfg = $config['interfaces'][$dhcrelayif];
316

    
317
		if (!isset($dhcrelayifconf['enable']) ||
318
			(($dhcrelayif != "lan") &&
319
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
320
			continue;
321

    
322
		$dhcrelayifs[] = $ifcfg['if'];
323
	}
324

    
325
	/* In order for the relay to work, it needs to be active on the
326
	   interface in which the destination server sits */
327
	foreach ($config['interfaces'] as $ifname) {
328
		$subnet = $ifname['ipaddr'] . "/" . $ifname['subnet'];
329
		if (ip_in_subnet($dhcrelaycfg['server'],$subnet))
330
			$destif = $ifname['if'];
331
	}
332

    
333
	if (!isset($destif))
334
		$destif = $config['interfaces']['wan']['if'];
335

    
336
	$dhcrelayifs[] = $destif;
337
	$dhcrelayifs = array_unique($dhcrelayifs);
338

    
339
	/* fire up dhcrelay */
340
	$cmd = "/usr/local/sbin/dhcrelay -i " .  join(" -i ", $dhcrelayifs);
341

    
342
	if (isset($dhcrelaycfg['agentoption']))
343
		$cmd .=  " -a -m replace";
344

    
345
	$cmd .= " {$dhcrelaycfg['server']}";
346
	mwexec($cmd);
347

    
348
	if (!$g['booting']) {
349
		/* set the reload filter dity flag */
350
		touch("{$g['tmp_path']}/filter_dirty");
351
	}
352

    
353
	return 0;
354
}
355

    
356
function services_dyndns_reset() {
357
	global $config, $g;
358

    
359
	if (file_exists("{$g['vardb_path']}/ez-ipupdate.cache")) {
360
		unlink("{$g['vardb_path']}/ez-ipupdate.cache");
361
	}
362

    
363
	if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
364
		conf_mount_rw();
365
		unlink("{$g['conf_path']}/ez-ipupdate.cache");
366
		conf_mount_ro();
367
	}
368

    
369
	return 0;
370
}
371

    
372
function services_dyndns_configure() {
373
	global $config, $g;
374

    
375
	$dyndnscfg = $config['dyndns'];
376
	$wancfg = $config['interfaces']['wan'];
377

    
378
	if (isset($dyndnscfg['enable'])) {
379

    
380
		if ($g['booting'])
381
			echo "Starting DynDNS client... ";
382
		else
383
			sleep(1);
384

    
385
		$dns = new updatedns($dnsService = $config['dyndns']['type'],
386
							 $dnsHost = $config['dyndns']['host'],
387
							 $dnsUser = $config['dyndns']['username'],
388
							 $dnsPass = $config['dyndns']['password'],
389
							 $dnsWilcard = $config['dyndns']['wildcard'],
390
							 $dnsMX = $config['dyndns']['mx']);
391

    
392
		if ($g['booting'])
393
			echo "done.\n";
394
	}
395

    
396
	return 0;
397
}
398

    
399
function services_dnsmasq_configure() {
400
	global $config, $g;
401

    
402
	/* kill any running dnsmasq */
403
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
404

    
405
	if (isset($config['dnsmasq']['enable'])) {
406

    
407
		if ($g['booting'])
408
			echo "Starting DNS forwarder... ";
409
		else
410
			sleep(1);
411

    
412
		/* generate hosts file */
413
		system_hosts_generate();
414

    
415
		$args = "";
416

    
417
		if (isset($config['dnsmasq']['regdhcp'])) {
418

    
419
			$args .= " -l {$g['vardb_path']}/dhcpd.leases" .
420
				" -s {$config['system']['domain']}";
421
		}
422

    
423
                if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
424
                        foreach($config['dnsmasq']['domainoverrides'] as $override) {
425
                                $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
426
                        }
427
                }
428

    
429
		/* run dnsmasq */
430
		mwexec("/usr/local/sbin/dnsmasq {$args}");
431

    
432
		if ($g['booting'])
433
			echo "done.\n";
434
	}
435

    
436
	if (!$g['booting']) {
437
		services_dhcpd_configure();
438
	}
439

    
440
	return 0;
441
}
442

    
443
function services_snmpd_configure() {
444
	global $config, $g;
445

    
446
	/* kill any running snmpd */
447
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
448

    
449
	if (isset($config['snmpd']['enable'])) {
450

    
451
		if ($g['booting'])
452
			echo "Starting SNMP daemon... ";
453

    
454
		/* generate snmpd.conf */
455
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
456
		if (!$fd) {
457
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
458
			return 1;
459
		}
460

    
461

    
462
		$snmpdconf = <<<EOD
463
location := "{$config['snmpd']['syslocation']}"
464
contact := "{$config['snmpd']['syscontact']}"
465
read := "{$config['snmpd']['rocommunity']}"
466

    
467
EOD;
468

    
469
/* No docs on what write strings do there for disable for now.
470
		if(isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
471
		    $snmpdconf .= <<<EOD
472
# write string
473
write := "{$config['snmpd']['rwcommunity']}"
474

    
475
EOD;
476
		}
477
*/
478

    
479

    
480
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
481
		    $snmpdconf .= <<<EOD
482
# SNMP Trap support.
483
traphost := {$config['snmpd']['trapserver']}
484
trapport := {$config['snmpd']['trapserverport']}
485
trap := "{$config['snmpd']['trapstring']}"
486

    
487

    
488
EOD;
489
		}
490

    
491

    
492
		$snmpdconf .= <<<EOD
493
system := 1     # pfSense
494
%snmpd
495
begemotSnmpdDebugDumpPdus       = 2
496
begemotSnmpdDebugSyslogPri      = 7
497
begemotSnmpdCommunityString.0.1 = $(read)
498

    
499
EOD;
500

    
501
/* No docs on what write strings do there for disable for now.
502
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
503
		    $snmpdconf .= <<<EOD
504
begemotSnmpdCommunityString.0.2 = $(write)
505

    
506
EOD;
507
		}
508
*/
509

    
510
		
511
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
512
		    $snmpdconf .= <<<EOD
513
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
514
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
515
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
516

    
517
EOD;
518
		}
519

    
520

    
521
		$snmpdconf .= <<<EOD
522
begemotSnmpdCommunityDisable    = 1
523

    
524
EOD;
525

    
526
		if(is_port( $config['snmpd']['pollport'] )) {
527
		    $snmpdconf .= <<<EOD
528
begemotSnmpdPortStatus.0.0.0.0.{$config['snmpd']['pollport']} = 1
529

    
530
EOD;
531

    
532
		}
533

    
534
		$snmpdconf .= <<<EOD
535
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
536
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
537

    
538
# These are bsnmp macros not php vars.
539
sysContact      = $(contact)
540
sysLocation     = $(location)
541
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
542

    
543
snmpEnableAuthenTraps = 2
544

    
545
EOD;
546

    
547
		if (is_array( $config['snmpd']['modules'] )) {
548
		    if(isset($config['snmpd']['modules']['mibii'])) {
549
			$snmpdconf .= <<<EOD
550
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
551

    
552
EOD;
553
		    }
554

    
555
		    if(isset($config['snmpd']['modules']['netgraph'])) {
556
			$snmpdconf .= <<<EOD
557
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
558
%netgraph
559
begemotNgControlNodeName = "snmpd"
560

    
561
EOD;
562
		    }
563

    
564
		    if(isset($config['snmpd']['modules']['pf'])) {
565
			$snmpdconf .= <<<EOD
566
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
567
# config must end with blank line
568

    
569

    
570
EOD;
571
		    }
572
		}
573

    
574
		fwrite($fd, $snmpdconf);
575
		fclose($fd);
576

    
577
		/* run bsnmpd */
578
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
579
			" -p {$g['varrun_path']}/snmpd.pid");
580

    
581
		if ($g['booting'])
582
			echo "done.\n";
583
	}
584

    
585
	return 0;
586
}
587

    
588
function services_proxyarp_configure() {
589
	global $config, $g;
590

    
591
	/* kill any running choparp */
592
	killbyname("choparp");
593

    
594
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
595
		$paa = array();
596

    
597
		/* group by interface */
598
		foreach ($config['virtualip']['vip'] as $vipent) {
599
			if ($vipent['mode'] === "proxyarp") {
600
				if ($vipent['interface'])
601
					$if = $vipent['interface'];
602
				else
603
					$if = "wan";
604

    
605
				if (!is_array($paa[$if]))
606
					$paa[$if] = array();
607

    
608
				$paa[$if][] = $vipent;
609
			}
610
		}
611

    
612
		if (count($paa))
613
		foreach ($paa as $paif => $paents) {
614
			if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) ||
615
                                       ($config['interfaces']['wan']['ipaddr'] == "dhcp") ||
616
                                       ($config['interfaces']['wan']['ipaddr'] == "bigpond")))
617
                               continue;
618

    
619
			$args = $config['interfaces'][$paif]['if'] . " auto";
620

    
621
			foreach ($paents as $paent) {
622

    
623
				if (isset($paent['subnet']))
624
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
625
				else if (isset($paent['range']))
626
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" .
627
						$paent['range']['to']);
628
			}
629

    
630
			mwexec_bg("/usr/local/sbin/choparp " . $args);
631
		}
632
	}
633
}
634

    
635
function services_dnsupdate_process() {
636
	global $config, $g;
637
	
638
	/* Dynamic DNS updating active? */
639
	if (isset($config['dnsupdate']['enable'])) {
640
		
641
		$wanip = get_current_wan_address();
642
		if ($wanip) {
643
			
644
			$keyname = $config['dnsupdate']['keyname'];
645
			/* trailing dot */
646
			if (substr($keyname, -1) != ".")
647
				$keyname .= ".";
648
			
649
			$hostname = $config['dnsupdate']['host'];
650
			/* trailing dot */
651
			if (substr($hostname, -1) != ".")
652
				$hostname .= ".";
653
			
654
			/* write private key file
655
			   this is dumb - public and private keys are the same for HMAC-MD5,
656
			   but nsupdate insists on having both */
657
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.private", "w");
658
			$privkey .= <<<EOD
659
Private-key-format: v1.2
660
Algorithm: 157 (HMAC)
661
Key: {$config['dnsupdate']['keydata']}
662

    
663
EOD;
664
			fwrite($fd, $privkey);
665
			fclose($fd);
666
			
667
			/* write public key file */
668
			if ($config['dnsupdate']['keytype'] == "zone") {
669
				$flags = 257;
670
				$proto = 3;
671
			} else if ($config['dnsupdate']['keytype'] == "host") {
672
				$flags = 513;
673
				$proto = 3;
674
			} else if ($config['dnsupdate']['keytype'] == "user") {
675
				$flags = 0;
676
				$proto = 2;
677
			}
678
			
679
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.key", "w");
680
			fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$config['dnsupdate']['keydata']}\n");
681
			fclose($fd);
682
			
683
			/* generate update instructions */
684
			$upinst =  "update delete {$config['dnsupdate']['host']} A\n";
685
			$upinst .= "update add {$config['dnsupdate']['host']} {$config['dnsupdate']['ttl']} A {$wanip}\n";
686
			$upinst .= "\n";	/* mind that trailing newline! */
687
			
688
			$fd = fopen("{$g['varetc_path']}/nsupdatecmds", "w");
689
			fwrite($fd, $upinst);
690
			fclose($fd);
691
			
692
			/* invoke nsupdate */
693
			$cmd = "/usr/sbin/nsupdate -k {$g['varetc_path']}:{$keyname}";
694
			if (isset($config['dnsupdate']['usetcp']))
695
				$cmd .= " -v";
696
			$cmd .= " {$g['varetc_path']}/nsupdatecmds";
697
			
698
			mwexec_bg($cmd);
699
		}
700
	}
701
	
702
	return 0;
703
}
704

    
705
function setup_wireless_olsr($interface) {
706
	$fd = fopen("{$g['varetc_path']}/{$interface}_olsr.conf", "w");
707
	$olsr .= <<<EOD
708
#
709
# olsr.org OLSR daemon config file
710
#
711
# Lines starting with a # are discarded
712
#
713
# This file was shipped with olsrd 0.X.X
714
#
715

    
716
# This file is an example of a typical
717
# configuration for a mostly static
718
# network(regarding mobility) using
719
# the LQ extention
720

    
721
# Debug level(0-9)
722
# If set to 0 the daemon runs in the background
723

    
724
DebugLevel	2
725

    
726
# IP version to use (4 or 6)
727

    
728
IpVersion	4
729

    
730
# Clear the screen each time the internal state changes
731

    
732
ClearScreen     yes
733

    
734
# HNA IPv4 routes
735
# syntax: netaddr netmask
736
# Example Internet gateway:
737
# 0.0.0.0 0.0.0.0
738

    
739
Hna4
740
{
741
#   Internet gateway:
742
#   0.0.0.0      0.0.0.0
743
#   more entries can be added:
744
#   192.168.1.0  255.255.255.0
745
}
746

    
747
# HNA IPv6 routes
748
# syntax: netaddr prefix
749
# Example Internet gateway:
750
Hna6
751
{
752
#   Internet gateway:
753
#   ::              0
754
#   more entries can be added:
755
#   fec0:2200:106:: 48
756
}
757

    
758

    
759
# Should olsrd keep on running even if there are
760
# no interfaces available? This is a good idea
761
# for a PCMCIA/USB hotswap environment.
762
# "yes" OR "no"
763

    
764
AllowNoInt	yes
765

    
766
# TOS(type of service) value for
767
# the IP header of control traffic.
768
# If not set it will default to 16
769

    
770
#TosValue	16
771

    
772
# The fixed willingness to use(0-7)
773
# If not set willingness will be calculated
774
# dynamically based on battery/power status
775
# if such information is available
776

    
777
#Willingness    	4
778

    
779
# Allow processes like the GUI front-end
780
# to connect to the daemon.
781

    
782
IpcConnect
783
{
784
     # Determines how many simultaneously
785
     # IPC connections that will be allowed
786
     # Setting this to 0 disables IPC
787

    
788
     MaxConnections  0
789

    
790
     # By default only 127.0.0.1 is allowed
791
     # to connect. Here allowed hosts can
792
     # be added
793

    
794
     Host            127.0.0.1
795
     #Host            10.0.0.5
796

    
797
     # You can also specify entire net-ranges 
798
     # that are allowed to connect. Multiple
799
     # entries are allowed
800

    
801
     #Net             192.168.1.0 255.255.255.0     
802
}
803

    
804
# Wether to use hysteresis or not
805
# Hysteresis adds more robustness to the
806
# link sensing but delays neighbor registration.
807
# Used by default. 'yes' or 'no'
808

    
809
UseHysteresis	no
810

    
811
# Hysteresis parameters
812
# Do not alter these unless you know 
813
# what you are doing!
814
# Set to auto by default. Allowed
815
# values are floating point values
816
# in the interval 0,1
817
# THR_LOW must always be lower than
818
# THR_HIGH.
819

    
820
#HystScaling	0.50
821
#HystThrHigh	0.80
822
#HystThrLow	0.30
823

    
824

    
825
# Link quality level
826
# 0 = do not use link quality
827
# 1 = use link quality for MPR selection
828
# 2 = use link quality for MPR selection and routing
829
# Defaults to 0
830

    
831
LinkQualityLevel	2
832

    
833
# Link quality window size
834
# Defaults to 10
835

    
836
LinkQualityWinSize	10
837

    
838
# Polling rate in seconds(float). 
839
# Default value 0.05 sec
840

    
841
Pollrate	0.05
842

    
843

    
844
# TC redundancy
845
# Specifies how much neighbor info should
846
# be sent in TC messages
847
# Possible values are:
848
# 0 - only send MPR selectors
849
# 1 - send MPR selectors and MPRs
850
# 2 - send all neighbors
851
#
852
# defaults to 0
853

    
854
TcRedundancy	2
855

    
856

    
857
#
858
# MPR coverage
859
# Specifies how many MPRs a node should
860
# try select to reach every 2 hop neighbor
861
#
862
# Can be set to any integer >0
863
#
864
# defaults to 1
865

    
866
MprCoverage	3
867

    
868

    
869
# Olsrd plugins to load
870
# This must be the absolute path to the file
871
# or the loader will use the following scheme:
872
# - Try the paths in the LD_LIBRARY_PATH 
873
#   environment variable.
874
# - The list of libraries cached in /etc/ld.so.cache
875
# - /lib, followed by /usr/lib
876

    
877
# Example plugin entry with parameters:
878

    
879
#LoadPlugin "olsrd_dyn_gw.so.0.3"
880
#{
881
    # Here parameters are set to be sent to the
882
    # plugin. Theese are on the form "key" "value".
883
    # Parameters ofcause, differs from plugin to plugin.
884
    # Consult the documentation of your plugin for details.
885

    
886
    # Example: dyn_gw params
887

    
888
    # how often to check for Internet connectivity
889
    # defaults to 5 secs
890
#   PlParam     "Interval"   "40"
891
    
892
    # if one or more IPv4 addresses are given, do a ping on these in
893
    # descending order to validate that there is not only an entry in
894
    # routing table, but also a real internet connection. If any of
895
    # these addresses could be pinged successfully, the test was
896
    # succesful, i.e. if the ping on the 1st address was successful,the
897
    # 2nd won't be pinged
898
#   PlParam     "Ping"       "141.1.1.1"
899
#   PlParam     "Ping"       "194.25.2.129"
900
#}
901

    
902

    
903

    
904
# Interfaces and their rules
905
# Omitted options will be set to the
906
# default values. Multiple interfaces
907
# can be specified in the same block
908
# and multiple blocks can be set.
909

    
910
# !!CHANGE THE INTERFACE LABEL(s) TO MATCH YOUR INTERFACE(s)!!
911
# (eg. wlan0 or eth1):
912

    
913
Interface "{$interface}" 
914
{
915

    
916
    # IPv4 broadcast address to use. The
917
    # one usefull example would be 255.255.255.255
918
    # If not defined the broadcastaddress
919
    # every card is configured with is used
920

    
921
    # Ip4Broadcast		255.255.255.255
922

    
923
    # IPv6 address scope to use.
924
    # Must be 'site-local' or 'global'
925

    
926
    # Ip6AddrType		site-local
927

    
928
    # IPv6 multicast address to use when
929
    # using site-local addresses.
930
    # If not defined, ff05::15 is used
931

    
932
    # Ip6MulticastSite		ff05::11
933

    
934
    # IPv6 multicast address to use when
935
    # using global addresses
936
    # If not defined, ff0e::1 is used
937

    
938
    # Ip6MulticastGlobal	ff0e::1
939

    
940

    
941
    # Emission intervals.
942
    # If not defined, RFC proposed values will
943
    # be used in most cases.
944

    
945
    # Hello interval in seconds(float)
946
    HelloInterval    2.0
947

    
948
    # HELLO validity time
949
    HelloValidityTime	20.0
950

    
951
    # TC interval in seconds(float)
952
    TcInterval        5.0
953

    
954
    # TC validity time
955
    TcValidityTime	30.0
956

    
957
    # MID interval in seconds(float)
958
    MidInterval	5.0
959

    
960
    # MID validity time
961
    MidValidityTime	30.0
962

    
963
    # HNA interval in seconds(float)
964
    HnaInterval	5.0
965

    
966
    # HNA validity time
967
    HnaValidityTime 	30.0
968

    
969
    # When multiple links exist between hosts
970
    # the weight of interface is used to determine
971
    # the link to use. Normally the weight is
972
    # automatically calculated by olsrd based
973
    # on the characteristics of the interface,
974
    # but here you can specify a fixed value.
975
    # Olsrd will choose links with the lowest value.
976

    
977
    # Weight 0
978

    
979

    
980
}
981
EOD;
982
	fwrite($fd, $olsr);
983
	fclose($fd);
984
	mwexec_bg("/usr/local/sbin/olsrd -f {$g['varetc_path']}/{$interface}_olsr.conf");
985
}
986

    
987
?>
(15-15/23)