Project

General

Profile

Download (23.7 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
	$dhcpdconf = <<<EOD
71
option domain-name "{$syscfg['domain']}";
72
default-lease-time 7200;
73
max-lease-time 86400;
74
authoritative;
75
log-facility local7;
76
ddns-update-style none;
77

    
78
EOD;
79

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

    
127
			
128
		}
129
	}
130

    
131
	$dhcpnum = 0;
132

    
133
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
134

    
135
		$ifcfg = $config['interfaces'][$dhcpif];
136

    
137
		if (!isset($dhcpifconf['enable']) ||
138
			(($dhcpif != "lan") &&
139
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
140
			continue;
141

    
142
		$subnet = gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);
143
		$subnetmask = gen_subnet_mask($ifcfg['subnet']);
144

    
145
		$dnscfg = "";
146

    
147
		if ($dhcpifconf['domain']) {
148
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
149
		}
150

    
151
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
152
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
153
		} else if (isset($config['dnsmasq']['enable'])) {
154
			$dnscfg .= "	option domain-name-servers " . $ifcfg['ipaddr'] . ";";
155
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
156
			$dnscfg .= "	option domain-name-servers " . join(",", $syscfg['dnsserver']) . ";";
157
		}
158

    
159
		$dhcpdconf .= "subnet $subnet netmask $subnetmask {\n";
160
		$dhcpdconf .= "	pool {\n";
161
		
162
		if($dhcpifconf['failover_peerip'] <> "") 
163
			$dhcpdconf .= "	deny dynamic bootp clients;\n";
164
		
165
		if (isset($dhcpifconf['denyunknown']))
166
		   $dhcpdconf .= "		deny unknown clients;\n";
167

    
168
		if ($dhcpifconf['gateway'])
169
			$routers = $dhcpifconf['gateway'];
170
		else
171
			$routers = $ifcfg['ipaddr'];
172

    
173
		if($dhcpifconf['failover_peerip'] <> "") {
174
			$dhcpdconf .= "		failover peer \"dhcp{$dhcpnum}\";\n";
175
			$dhcpnum++;
176
		}
177

    
178
		$dhcpdconf .= <<<EOD
179
		range {$dhcpifconf['range']['from']} {$dhcpifconf['range']['to']};
180
	}
181
	option routers {$routers};
182
$dnscfg
183

    
184
EOD;
185

    
186
		if ($dhcpifconf['defaultleasetime'])
187
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
188
		if ($dhcpifconf['maxleasetime'])
189
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
190

    
191
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
192
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
193
			$dhcpdconf .= "	option netbios-node-type 8;\n";
194
		}
195

    
196
		if ($dhcpifconf['next-server'])
197
			$dhcpdconf .= "	next-server {$dhcpifconf['next-server']};\n";
198
		if ($dhcpifconf['filename'])
199
			$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
200

    
201
		$dhcpdconf .= <<<EOD
202
}
203

    
204
EOD;
205

    
206
		/* add static mappings */
207
		if (is_array($dhcpifconf['staticmap'])) {
208

    
209
			$i = 0;
210
			foreach ($dhcpifconf['staticmap'] as $sm) {
211
				$dhcpdconf .= <<<EOD
212
host s_{$dhcpif}_{$i} {
213
	hardware ethernet {$sm['mac']};
214

    
215
EOD;
216
				if ($sm['ipaddr'])
217
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
218

    
219
				$dhcpdconf .= "}\n";
220
				$i++;
221
			}
222
		}
223

    
224
		$dhcpdifs[] = $ifcfg['if'];
225
	}
226

    
227
	fwrite($fd, $dhcpdconf);
228
	fclose($fd);
229

    
230
	/* create an empty leases database */
231
	touch("{$g['vardb_path']}/dhcpd.leases");
232

    
233
	/* fire up dhcpd */
234
	mwexec("/usr/local/sbin/dhcpd -cf {$g['varetc_path']}/dhcpd.conf " .
235
		join(" ", $dhcpdifs));
236

    
237
	if ($g['booting']) {
238
                print "done.\n";
239
	}
240

    
241
	return 0;
242
}
243

    
244
function interfaces_staticarp_configure($if) {
245
        global $config, $g;
246
        
247
        $ifcfg = $config['interfaces'][$if];
248

    
249
        /* Enable staticarp, if enabled */
250
        if(isset($config['dhcpd'][$if]['staticarp'])) {
251
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " );
252
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
253
                if (is_array($config['dhcpd'][$if]['staticmap'])) {
254

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

    
258
                        }
259
                        
260
                }
261
        } else {
262
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " );
263
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
264
        }
265

    
266
        return 0;
267
}
268

    
269
function services_dhcrelay_configure() {
270
	global $config, $g;
271

    
272
	/* kill any running dhcrelay */
273
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
274

    
275
	$dhcrelaycfg = $config['dhcrelay'];
276

    
277
	/* DHCPRelay enabled on any interfaces? */
278
	$dhcrelayenable = false;
279
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
280
		if (isset($dhcrelayifconf['enable']) &&
281
			(($dhcrelayif == "lan") ||
282
			(isset($config['interfaces'][$dhcrelayif]['enable']) &&
283
			$config['interfaces'][$dhcrelayif]['if'] && (!$config['interfaces'][$dhcrelayif]['bridge']))))
284
			$dhcrelayenable = true;
285
	}
286

    
287
	if (!$dhcrelayenable)
288
		return 0;
289

    
290
	if ($g['booting'])
291
		echo "Starting DHCP relay service... ";
292
	else
293
		sleep(1);
294

    
295
	$dhcrelayifs = array();
296
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
297

    
298
		$ifcfg = $config['interfaces'][$dhcrelayif];
299

    
300
		if (!isset($dhcrelayifconf['enable']) ||
301
			(($dhcrelayif != "lan") &&
302
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
303
			continue;
304

    
305
		$dhcrelayifs[] = $ifcfg['if'];
306
	}
307

    
308
	/* In order for the relay to work, it needs to be active on the
309
	   interface in which the destination server sits */
310
	foreach ($config['interfaces'] as $ifname) {
311
		$subnet = $ifname['ipaddr'] . "/" . $ifname['subnet'];
312
		if (ip_in_subnet($dhcrelaycfg['server'],$subnet))
313
			$destif = $ifname['if'];
314
	}
315

    
316
	if (!isset($destif))
317
		$destif = $config['interfaces']['wan']['if'];
318

    
319
	$dhcrelayifs[] = $destif;
320
	$dhcrelayifs = array_unique($dhcrelayifs);
321

    
322
	/* fire up dhcrelay */
323
	$cmd = "/usr/local/sbin/dhcrelay -i " .  join(" -i ", $dhcrelayifs);
324

    
325
	if (isset($dhcrelaycfg['agentoption']))
326
		$cmd .=  " -a -m replace";
327

    
328
	$cmd .= " {$dhcrelaycfg['server']}";
329
	mwexec($cmd);
330

    
331
	if (!$g['booting']) {
332
		/* set the reload filter dity flag */
333
		touch("{$g['tmp_path']}/filter_dirty");
334
	}
335

    
336
	return 0;
337
}
338

    
339
function services_dyndns_reset() {
340
	global $config, $g;
341

    
342
	if (file_exists("{$g['vardb_path']}/ez-ipupdate.cache")) {
343
		unlink("{$g['vardb_path']}/ez-ipupdate.cache");
344
	}
345

    
346
	if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
347
		conf_mount_rw();
348
		unlink("{$g['conf_path']}/ez-ipupdate.cache");
349
		conf_mount_ro();
350
	}
351

    
352
	return 0;
353
}
354

    
355
function services_dyndns_configure() {
356
	global $config, $g;
357

    
358
	$dyndnscfg = $config['dyndns'];
359
	$wancfg = $config['interfaces']['wan'];
360

    
361
	if (isset($dyndnscfg['enable'])) {
362

    
363
		if ($g['booting'])
364
			echo "Starting DynDNS client... ";
365
		else
366
			sleep(1);
367

    
368
		$dns = new updatedns($dnsService = $config['dyndns']['type'],
369
							 $dnsHost = $config['dyndns']['host'],
370
							 $dnsUser = $config['dyndns']['username'],
371
							 $dnsPass = $config['dyndns']['password'],
372
							 $dnsWilcard = $config['dyndns']['wildcard'],
373
							 $dnsMX = $config['dyndns']['mx']);
374

    
375
		if ($g['booting'])
376
			echo "done.\n";
377
	}
378

    
379
	return 0;
380
}
381

    
382
function services_dnsmasq_configure() {
383
	global $config, $g;
384

    
385
	/* kill any running dnsmasq */
386
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
387

    
388
	if (isset($config['dnsmasq']['enable'])) {
389

    
390
		if ($g['booting'])
391
			echo "Starting DNS forwarder... ";
392
		else
393
			sleep(1);
394

    
395
		/* generate hosts file */
396
		system_hosts_generate();
397

    
398
		$args = "";
399

    
400
		if (isset($config['dnsmasq']['regdhcp'])) {
401

    
402
			$args .= " -l {$g['vardb_path']}/dhcpd.leases" .
403
				" -s {$config['system']['domain']}";
404
		}
405

    
406
                if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
407
                        foreach($config['dnsmasq']['domainoverrides'] as $override) {
408
                                $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
409
                        }
410
                }
411

    
412
		/* run dnsmasq */
413
		mwexec("/usr/local/sbin/dnsmasq {$args}");
414

    
415
		if ($g['booting'])
416
			echo "done.\n";
417
	}
418

    
419
	if (!$g['booting']) {
420
		services_dhcpd_configure();
421
	}
422

    
423
	return 0;
424
}
425

    
426
function services_snmpd_configure() {
427
	global $config, $g;
428

    
429
	/* kill any running snmpd */
430
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
431

    
432
	if (isset($config['snmpd']['enable'])) {
433

    
434
		if ($g['booting'])
435
			echo "Starting SNMP daemon... ";
436

    
437
		/* generate snmpd.conf */
438
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
439
		if (!$fd) {
440
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
441
			return 1;
442
		}
443

    
444

    
445
		$snmpdconf = <<<EOD
446
location := "{$config['snmpd']['syslocation']}"
447
contact := "{$config['snmpd']['syscontact']}"
448
read := "{$config['snmpd']['rocommunity']}"
449

    
450
EOD;
451

    
452
/* No docs on what write strings do there for disable for now.
453
		if(isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
454
		    $snmpdconf .= <<<EOD
455
# write string
456
write := "{$config['snmpd']['rwcommunity']}"
457

    
458
EOD;
459
		}
460
*/
461

    
462

    
463
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
464
		    $snmpdconf .= <<<EOD
465
# SNMP Trap support.
466
traphost := {$config['snmpd']['trapserver']}
467
trapport := {$config['snmpd']['trapserverport']}
468
trap := "{$config['snmpd']['trapstring']}"
469

    
470

    
471
EOD;
472
		}
473

    
474

    
475
		$snmpdconf .= <<<EOD
476
system := 1     # pfSense
477
%snmpd
478
begemotSnmpdDebugDumpPdus       = 2
479
begemotSnmpdDebugSyslogPri      = 7
480
begemotSnmpdCommunityString.0.1 = $(read)
481

    
482
EOD;
483

    
484
/* No docs on what write strings do there for disable for now.
485
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
486
		    $snmpdconf .= <<<EOD
487
begemotSnmpdCommunityString.0.2 = $(write)
488

    
489
EOD;
490
		}
491
*/
492

    
493
		
494
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
495
		    $snmpdconf .= <<<EOD
496
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
497
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
498
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
499

    
500
EOD;
501
		}
502

    
503

    
504
		$snmpdconf .= <<<EOD
505
begemotSnmpdCommunityDisable    = 1
506

    
507
EOD;
508

    
509
		if(is_port( $config['snmpd']['pollport'] )) {
510
		    $snmpdconf .= <<<EOD
511
begemotSnmpdPortStatus.0.0.0.0.{$config['snmpd']['pollport']} = 1
512

    
513
EOD;
514

    
515
		}
516

    
517
		$snmpdconf .= <<<EOD
518
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
519
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
520

    
521
# These are bsnmp macros not php vars.
522
sysContact      = $(contact)
523
sysLocation     = $(location)
524
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
525

    
526
snmpEnableAuthenTraps = 2
527

    
528
EOD;
529

    
530
		if (is_array( $config['snmpd']['modules'] )) {
531
		    if(isset($config['snmpd']['modules']['mibii'])) {
532
			$snmpdconf .= <<<EOD
533
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
534

    
535
EOD;
536
		    }
537

    
538
		    if(isset($config['snmpd']['modules']['netgraph'])) {
539
			$snmpdconf .= <<<EOD
540
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
541
%netgraph
542
begemotNgControlNodeName = "snmpd"
543

    
544
EOD;
545
		    }
546

    
547
		    if(isset($config['snmpd']['modules']['pf'])) {
548
			$snmpdconf .= <<<EOD
549
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
550
# config must end with blank line
551

    
552

    
553
EOD;
554
		    }
555
		}
556

    
557
		fwrite($fd, $snmpdconf);
558
		fclose($fd);
559

    
560
		/* run bsnmpd */
561
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
562
			" -p {$g['varrun_path']}/snmpd.pid");		  
563
//		mwexec("/usr/local/sbin/snmpd -c {$g['varetc_path']}/snmpd.conf" .
564
//			" -P {$g['varrun_path']}/snmpd.pid");
565

    
566
		if ($g['booting'])
567
			echo "done.\n";
568
	}
569

    
570
	return 0;
571
}
572

    
573
function services_proxyarp_configure() {
574
	global $config, $g;
575

    
576
	/* kill any running choparp */
577
	killbyname("choparp");
578

    
579
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
580
		$paa = array();
581

    
582
		/* group by interface */
583
		foreach ($config['virtualip']['vip'] as $vipent) {
584
			if ($vipent['mode'] === "proxyarp") {
585
				if ($vipent['interface'])
586
					$if = $vipent['interface'];
587
				else
588
					$if = "wan";
589

    
590
				if (!is_array($paa[$if]))
591
					$paa[$if] = array();
592

    
593
				$paa[$if][] = $vipent;
594
			}
595
		}
596

    
597
		if (count($paa))
598
		foreach ($paa as $paif => $paents) {
599
			if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) ||
600
                                       ($config['interfaces']['wan']['ipaddr'] == "dhcp") ||
601
                                       ($config['interfaces']['wan']['ipaddr'] == "bigpond")))
602
                               continue;
603

    
604
			$args = $config['interfaces'][$paif]['if'] . " auto";
605

    
606
			foreach ($paents as $paent) {
607

    
608
				if (isset($paent['subnet']))
609
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
610
				else if (isset($paent['range']))
611
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" .
612
						$paent['range']['to']);
613
			}
614

    
615
			mwexec_bg("/usr/local/sbin/choparp " . $args);
616
		}
617
	}
618
}
619

    
620
function services_dnsupdate_process() {
621
	global $config, $g;
622
	
623
	/* Dynamic DNS updating active? */
624
	if (isset($config['dnsupdate']['enable'])) {
625
		
626
		$wanip = get_current_wan_address();
627
		if ($wanip) {
628
			
629
			$keyname = $config['dnsupdate']['keyname'];
630
			/* trailing dot */
631
			if (substr($keyname, -1) != ".")
632
				$keyname .= ".";
633
			
634
			$hostname = $config['dnsupdate']['host'];
635
			/* trailing dot */
636
			if (substr($hostname, -1) != ".")
637
				$hostname .= ".";
638
			
639
			/* write private key file
640
			   this is dumb - public and private keys are the same for HMAC-MD5,
641
			   but nsupdate insists on having both */
642
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.private", "w");
643
			$privkey .= <<<EOD
644
Private-key-format: v1.2
645
Algorithm: 157 (HMAC)
646
Key: {$config['dnsupdate']['keydata']}
647

    
648
EOD;
649
			fwrite($fd, $privkey);
650
			fclose($fd);
651
			
652
			/* write public key file */
653
			if ($config['dnsupdate']['keytype'] == "zone") {
654
				$flags = 257;
655
				$proto = 3;
656
			} else if ($config['dnsupdate']['keytype'] == "host") {
657
				$flags = 513;
658
				$proto = 3;
659
			} else if ($config['dnsupdate']['keytype'] == "user") {
660
				$flags = 0;
661
				$proto = 2;
662
			}
663
			
664
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.key", "w");
665
			fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$config['dnsupdate']['keydata']}\n");
666
			fclose($fd);
667
			
668
			/* generate update instructions */
669
			$upinst =  "update delete {$config['dnsupdate']['host']} A\n";
670
			$upinst .= "update add {$config['dnsupdate']['host']} {$config['dnsupdate']['ttl']} A {$wanip}\n";
671
			$upinst .= "\n";	/* mind that trailing newline! */
672
			
673
			$fd = fopen("{$g['varetc_path']}/nsupdatecmds", "w");
674
			fwrite($fd, $upinst);
675
			fclose($fd);
676
			
677
			/* invoke nsupdate */
678
			$cmd = "/usr/sbin/nsupdate -k {$g['varetc_path']}:{$keyname}";
679
			if (isset($config['dnsupdate']['usetcp']))
680
				$cmd .= " -v";
681
			$cmd .= " {$g['varetc_path']}/nsupdatecmds";
682
			
683
			mwexec_bg($cmd);
684
		}
685
	}
686
	
687
	return 0;
688
}
689

    
690
function setup_wireless_olsr($interface) {
691
	$fd = fopen("{$g['varetc_path']}/{$interface}_olsr.conf", "w");
692
	$olsr .= <<<EOD
693
#
694
# olsr.org OLSR daemon config file
695
#
696
# Lines starting with a # are discarded
697
#
698
# This file was shipped with olsrd 0.X.X
699
#
700

    
701
# This file is an example of a typical
702
# configuration for a mostly static
703
# network(regarding mobility) using
704
# the LQ extention
705

    
706
# Debug level(0-9)
707
# If set to 0 the daemon runs in the background
708

    
709
DebugLevel	2
710

    
711
# IP version to use (4 or 6)
712

    
713
IpVersion	4
714

    
715
# Clear the screen each time the internal state changes
716

    
717
ClearScreen     yes
718

    
719
# HNA IPv4 routes
720
# syntax: netaddr netmask
721
# Example Internet gateway:
722
# 0.0.0.0 0.0.0.0
723

    
724
Hna4
725
{
726
#   Internet gateway:
727
#   0.0.0.0      0.0.0.0
728
#   more entries can be added:
729
#   192.168.1.0  255.255.255.0
730
}
731

    
732
# HNA IPv6 routes
733
# syntax: netaddr prefix
734
# Example Internet gateway:
735
Hna6
736
{
737
#   Internet gateway:
738
#   ::              0
739
#   more entries can be added:
740
#   fec0:2200:106:: 48
741
}
742

    
743

    
744
# Should olsrd keep on running even if there are
745
# no interfaces available? This is a good idea
746
# for a PCMCIA/USB hotswap environment.
747
# "yes" OR "no"
748

    
749
AllowNoInt	yes
750

    
751
# TOS(type of service) value for
752
# the IP header of control traffic.
753
# If not set it will default to 16
754

    
755
#TosValue	16
756

    
757
# The fixed willingness to use(0-7)
758
# If not set willingness will be calculated
759
# dynamically based on battery/power status
760
# if such information is available
761

    
762
#Willingness    	4
763

    
764
# Allow processes like the GUI front-end
765
# to connect to the daemon.
766

    
767
IpcConnect
768
{
769
     # Determines how many simultaneously
770
     # IPC connections that will be allowed
771
     # Setting this to 0 disables IPC
772

    
773
     MaxConnections  0
774

    
775
     # By default only 127.0.0.1 is allowed
776
     # to connect. Here allowed hosts can
777
     # be added
778

    
779
     Host            127.0.0.1
780
     #Host            10.0.0.5
781

    
782
     # You can also specify entire net-ranges 
783
     # that are allowed to connect. Multiple
784
     # entries are allowed
785

    
786
     #Net             192.168.1.0 255.255.255.0     
787
}
788

    
789
# Wether to use hysteresis or not
790
# Hysteresis adds more robustness to the
791
# link sensing but delays neighbor registration.
792
# Used by default. 'yes' or 'no'
793

    
794
UseHysteresis	no
795

    
796
# Hysteresis parameters
797
# Do not alter these unless you know 
798
# what you are doing!
799
# Set to auto by default. Allowed
800
# values are floating point values
801
# in the interval 0,1
802
# THR_LOW must always be lower than
803
# THR_HIGH.
804

    
805
#HystScaling	0.50
806
#HystThrHigh	0.80
807
#HystThrLow	0.30
808

    
809

    
810
# Link quality level
811
# 0 = do not use link quality
812
# 1 = use link quality for MPR selection
813
# 2 = use link quality for MPR selection and routing
814
# Defaults to 0
815

    
816
LinkQualityLevel	2
817

    
818
# Link quality window size
819
# Defaults to 10
820

    
821
LinkQualityWinSize	10
822

    
823
# Polling rate in seconds(float). 
824
# Default value 0.05 sec
825

    
826
Pollrate	0.05
827

    
828

    
829
# TC redundancy
830
# Specifies how much neighbor info should
831
# be sent in TC messages
832
# Possible values are:
833
# 0 - only send MPR selectors
834
# 1 - send MPR selectors and MPRs
835
# 2 - send all neighbors
836
#
837
# defaults to 0
838

    
839
TcRedundancy	2
840

    
841

    
842
#
843
# MPR coverage
844
# Specifies how many MPRs a node should
845
# try select to reach every 2 hop neighbor
846
#
847
# Can be set to any integer >0
848
#
849
# defaults to 1
850

    
851
MprCoverage	3
852

    
853

    
854
# Olsrd plugins to load
855
# This must be the absolute path to the file
856
# or the loader will use the following scheme:
857
# - Try the paths in the LD_LIBRARY_PATH 
858
#   environment variable.
859
# - The list of libraries cached in /etc/ld.so.cache
860
# - /lib, followed by /usr/lib
861

    
862
# Example plugin entry with parameters:
863

    
864
#LoadPlugin "olsrd_dyn_gw.so.0.3"
865
#{
866
    # Here parameters are set to be sent to the
867
    # plugin. Theese are on the form "key" "value".
868
    # Parameters ofcause, differs from plugin to plugin.
869
    # Consult the documentation of your plugin for details.
870

    
871
    # Example: dyn_gw params
872

    
873
    # how often to check for Internet connectivity
874
    # defaults to 5 secs
875
#   PlParam     "Interval"   "40"
876
    
877
    # if one or more IPv4 addresses are given, do a ping on these in
878
    # descending order to validate that there is not only an entry in
879
    # routing table, but also a real internet connection. If any of
880
    # these addresses could be pinged successfully, the test was
881
    # succesful, i.e. if the ping on the 1st address was successful,the
882
    # 2nd won't be pinged
883
#   PlParam     "Ping"       "141.1.1.1"
884
#   PlParam     "Ping"       "194.25.2.129"
885
#}
886

    
887

    
888

    
889
# Interfaces and their rules
890
# Omitted options will be set to the
891
# default values. Multiple interfaces
892
# can be specified in the same block
893
# and multiple blocks can be set.
894

    
895
# !!CHANGE THE INTERFACE LABEL(s) TO MATCH YOUR INTERFACE(s)!!
896
# (eg. wlan0 or eth1):
897

    
898
Interface "{$interface}" 
899
{
900

    
901
    # IPv4 broadcast address to use. The
902
    # one usefull example would be 255.255.255.255
903
    # If not defined the broadcastaddress
904
    # every card is configured with is used
905

    
906
    # Ip4Broadcast		255.255.255.255
907

    
908
    # IPv6 address scope to use.
909
    # Must be 'site-local' or 'global'
910

    
911
    # Ip6AddrType		site-local
912

    
913
    # IPv6 multicast address to use when
914
    # using site-local addresses.
915
    # If not defined, ff05::15 is used
916

    
917
    # Ip6MulticastSite		ff05::11
918

    
919
    # IPv6 multicast address to use when
920
    # using global addresses
921
    # If not defined, ff0e::1 is used
922

    
923
    # Ip6MulticastGlobal	ff0e::1
924

    
925

    
926
    # Emission intervals.
927
    # If not defined, RFC proposed values will
928
    # be used in most cases.
929

    
930
    # Hello interval in seconds(float)
931
    HelloInterval    2.0
932

    
933
    # HELLO validity time
934
    HelloValidityTime	20.0
935

    
936
    # TC interval in seconds(float)
937
    TcInterval        5.0
938

    
939
    # TC validity time
940
    TcValidityTime	30.0
941

    
942
    # MID interval in seconds(float)
943
    MidInterval	5.0
944

    
945
    # MID validity time
946
    MidValidityTime	30.0
947

    
948
    # HNA interval in seconds(float)
949
    HnaInterval	5.0
950

    
951
    # HNA validity time
952
    HnaValidityTime 	30.0
953

    
954
    # When multiple links exist between hosts
955
    # the weight of interface is used to determine
956
    # the link to use. Normally the weight is
957
    # automatically calculated by olsrd based
958
    # on the characteristics of the interface,
959
    # but here you can specify a fixed value.
960
    # Olsrd will choose links with the lowest value.
961

    
962
    # Weight 0
963

    
964

    
965
}
966
EOD;
967
	fwrite($fd, $olsr);
968
	fclose($fd);
969
	mwexec_bg("/usr/local/sbin/olsrd -f {$g['varetc_path']}/{$interface}_olsr.conf");
970
}
971

    
972
?>
(15-15/23)