Project

General

Profile

Download (23.4 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
			foreach ($a_vip as $vipent) {
94
				$int = guess_interface_from_ip($vipent['subnet']);
95
				$intip = find_interface_ip($int);
96
				if($int == $dhcpif) {
97
					/* this is the interface! */
98
					if($vipent['advskew'] == "0")
99
						$skew = 0;
100
				}
101
			}
102
			if($skew == 1) {
103
				$type = "secondary";
104
			} else {
105
				$type = "primary";
106
			}
107
			$dhcpdconf .= <<<EOPP
108
failover peer "dhcp{$dhcpnum}" { 
109
  {$type};
110
  address {$intip};
111
  port 520;
112
  peer address {$dhcpifconf['failover_peerip']};
113
  peer port 519;
114
  max-response-delay 60;
115
  max-unacked-updates 10;
116
}
117
EOPP;
118
		$dhcpnum++;
119

    
120
			
121
		}
122
	}
123

    
124
	$dhcpnum = 0;
125

    
126
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
127

    
128
		$ifcfg = $config['interfaces'][$dhcpif];
129

    
130
		if (!isset($dhcpifconf['enable']) ||
131
			(($dhcpif != "lan") &&
132
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
133
			continue;
134

    
135
		$subnet = gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);
136
		$subnetmask = gen_subnet_mask($ifcfg['subnet']);
137

    
138
		$dnscfg = "";
139

    
140
		if ($dhcpifconf['domain']) {
141
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
142
		}
143

    
144
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
145
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
146
		} else if (isset($config['dnsmasq']['enable'])) {
147
			$dnscfg .= "	option domain-name-servers " . $ifcfg['ipaddr'] . ";";
148
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
149
			$dnscfg .= "	option domain-name-servers " . join(",", $syscfg['dnsserver']) . ";";
150
		}
151

    
152
		$dhcpdconf .= "subnet $subnet netmask $subnetmask {\n";
153
		$dhcpdconf .= "	pool {\n";
154
		if (isset($dhcpifconf['denyunknown']))
155
		   $dhcpdconf .= "		deny unknown clients;\n";
156

    
157
		if ($dhcpifconf['gateway'])
158
			$routers = $dhcpifconf['gateway'];
159
		else
160
			$routers = $ifcfg['ipaddr'];
161

    
162
		if($dhcpifconf['failover_peerip'] <> "") {
163
			$dhcpdconf .= "		failover peer \"dhcp{$dhcpnum}\";\n";
164
			$dhcpnum++;
165
		}
166

    
167
		$dhcpdconf .= <<<EOD
168
		range {$dhcpifconf['range']['from']} {$dhcpifconf['range']['to']};
169
	}
170
	option routers {$routers};
171
$dnscfg
172

    
173
EOD;
174

    
175
		if ($dhcpifconf['defaultleasetime'])
176
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
177
		if ($dhcpifconf['maxleasetime'])
178
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
179

    
180
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
181
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
182
			$dhcpdconf .= "	option netbios-node-type 8;\n";
183
		}
184

    
185
		if ($dhcpifconf['next-server'])
186
			$dhcpdconf .= "	next-server {$dhcpifconf['next-server']};\n";
187
		if ($dhcpifconf['filename'])
188
			$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
189

    
190
		$dhcpdconf .= <<<EOD
191
}
192

    
193
EOD;
194

    
195
		/* add static mappings */
196
		if (is_array($dhcpifconf['staticmap'])) {
197

    
198
			$i = 0;
199
			foreach ($dhcpifconf['staticmap'] as $sm) {
200
				$dhcpdconf .= <<<EOD
201
host s_{$dhcpif}_{$i} {
202
	hardware ethernet {$sm['mac']};
203

    
204
EOD;
205
				if ($sm['ipaddr'])
206
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
207

    
208
				$dhcpdconf .= "}\n";
209
				$i++;
210
			}
211
		}
212

    
213
		$dhcpdifs[] = $ifcfg['if'];
214
	}
215

    
216
	fwrite($fd, $dhcpdconf);
217
	fclose($fd);
218

    
219
	/* create an empty leases database */
220
	touch("{$g['vardb_path']}/dhcpd.leases");
221

    
222
	/* fire up dhcpd */
223
	mwexec("/usr/local/sbin/dhcpd -cf {$g['varetc_path']}/dhcpd.conf " .
224
		join(" ", $dhcpdifs));
225

    
226
	if ($g['booting']) {
227
                print "done.\n";
228
	}
229

    
230
	return 0;
231
}
232

    
233
function interfaces_staticarp_configure($if) {
234
        global $config, $g;
235
        
236
        $ifcfg = $config['interfaces'][$if];
237

    
238
        /* Enable staticarp, if enabled */
239
        if(isset($config['dhcpd'][$if]['staticarp'])) {
240
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " );
241
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
242
                if (is_array($config['dhcpd'][$if]['staticmap'])) {
243

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

    
247
                        }
248
                        
249
                }
250
        } else {
251
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " );
252
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
253
        }
254

    
255
        return 0;
256
}
257

    
258
function services_dhcrelay_configure() {
259
	global $config, $g;
260

    
261
	/* kill any running dhcrelay */
262
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
263

    
264
	$dhcrelaycfg = $config['dhcrelay'];
265

    
266
	/* DHCPRelay enabled on any interfaces? */
267
	$dhcrelayenable = false;
268
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
269
		if (isset($dhcrelayifconf['enable']) &&
270
			(($dhcrelayif == "lan") ||
271
			(isset($config['interfaces'][$dhcrelayif]['enable']) &&
272
			$config['interfaces'][$dhcrelayif]['if'] && (!$config['interfaces'][$dhcrelayif]['bridge']))))
273
			$dhcrelayenable = true;
274
	}
275

    
276
	if (!$dhcrelayenable)
277
		return 0;
278

    
279
	if ($g['booting'])
280
		echo "Starting DHCP relay service... ";
281
	else
282
		sleep(1);
283

    
284
	$dhcrelayifs = array();
285
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
286

    
287
		$ifcfg = $config['interfaces'][$dhcrelayif];
288

    
289
		if (!isset($dhcrelayifconf['enable']) ||
290
			(($dhcrelayif != "lan") &&
291
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
292
			continue;
293

    
294
		$dhcrelayifs[] = $ifcfg['if'];
295
	}
296

    
297
	/* In order for the relay to work, it needs to be active on the
298
	   interface in which the destination server sits */
299
	foreach ($config['interfaces'] as $ifname) {
300
		$subnet = $ifname['ipaddr'] . "/" . $ifname['subnet'];
301
		if (ip_in_subnet($dhcrelaycfg['server'],$subnet))
302
			$destif = $ifname['if'];
303
	}
304

    
305
	if (!isset($destif))
306
		$destif = $config['interfaces']['wan']['if'];
307

    
308
	$dhcrelayifs[] = $destif;
309
	$dhcrelayifs = array_unique($dhcrelayifs);
310

    
311
	/* fire up dhcrelay */
312
	$cmd = "/usr/local/sbin/dhcrelay -i " .  join(" -i ", $dhcrelayifs);
313

    
314
	if (isset($dhcrelaycfg['agentoption']))
315
		$cmd .=  " -a -m replace";
316

    
317
	$cmd .= " {$dhcrelaycfg['server']}";
318
	mwexec($cmd);
319

    
320
	if (!$g['booting']) {
321
		/* set the reload filter dity flag */
322
		touch("{$g['tmp_path']}/filter_dirty");
323
	}
324

    
325
	return 0;
326
}
327

    
328
function services_dyndns_reset() {
329
	global $config, $g;
330

    
331
	if (file_exists("{$g['vardb_path']}/ez-ipupdate.cache")) {
332
		unlink("{$g['vardb_path']}/ez-ipupdate.cache");
333
	}
334

    
335
	if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
336
		conf_mount_rw();
337
		unlink("{$g['conf_path']}/ez-ipupdate.cache");
338
		conf_mount_ro();
339
	}
340

    
341
	return 0;
342
}
343

    
344
function services_dyndns_configure() {
345
	global $config, $g;
346

    
347
	$dyndnscfg = $config['dyndns'];
348
	$wancfg = $config['interfaces']['wan'];
349

    
350
	if (isset($dyndnscfg['enable'])) {
351

    
352
		if ($g['booting'])
353
			echo "Starting DynDNS client... ";
354
		else
355
			sleep(1);
356

    
357
		$dns = new updatedns($dnsService = $config['dyndns']['type'],
358
							 $dnsHost = $config['dyndns']['host'],
359
							 $dnsUser = $config['dyndns']['username'],
360
							 $dnsPass = $config['dyndns']['password'],
361
							 $dnsWilcard = $config['dyndns']['wildcard'],
362
							 $dnsMX = $config['dyndns']['mx']);
363

    
364
		if ($g['booting'])
365
			echo "done.\n";
366
	}
367

    
368
	return 0;
369
}
370

    
371
function services_dnsmasq_configure() {
372
	global $config, $g;
373

    
374
	/* kill any running dnsmasq */
375
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
376

    
377
	if (isset($config['dnsmasq']['enable'])) {
378

    
379
		if ($g['booting'])
380
			echo "Starting DNS forwarder... ";
381
		else
382
			sleep(1);
383

    
384
		/* generate hosts file */
385
		system_hosts_generate();
386

    
387
		$args = "";
388

    
389
		if (isset($config['dnsmasq']['regdhcp'])) {
390

    
391
			$args .= " -l {$g['vardb_path']}/dhcpd.leases" .
392
				" -s {$config['system']['domain']}";
393
		}
394

    
395
                if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
396
                        foreach($config['dnsmasq']['domainoverrides'] as $override) {
397
                                $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
398
                        }
399
                }
400

    
401
		/* run dnsmasq */
402
		mwexec("/usr/local/sbin/dnsmasq {$args}");
403

    
404
		if ($g['booting'])
405
			echo "done.\n";
406
	}
407

    
408
	if (!$g['booting']) {
409
		services_dhcpd_configure();
410
	}
411

    
412
	return 0;
413
}
414

    
415
function services_snmpd_configure() {
416
	global $config, $g;
417

    
418
	/* kill any running snmpd */
419
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
420

    
421
	if (isset($config['snmpd']['enable'])) {
422

    
423
		if ($g['booting'])
424
			echo "Starting SNMP daemon... ";
425

    
426
		/* generate snmpd.conf */
427
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
428
		if (!$fd) {
429
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
430
			return 1;
431
		}
432

    
433

    
434
		$snmpdconf = <<<EOD
435
location := "{$config['snmpd']['syslocation']}"
436
contact := "{$config['snmpd']['syscontact']}"
437
read := "{$config['snmpd']['rocommunity']}"
438

    
439
EOD;
440

    
441
/* No docs on what write strings do there for disable for now.
442
		if(isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
443
		    $snmpdconf .= <<<EOD
444
# write string
445
write := "{$config['snmpd']['rwcommunity']}"
446

    
447
EOD;
448
		}
449
*/
450

    
451

    
452
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
453
		    $snmpdconf .= <<<EOD
454
# SNMP Trap support.
455
traphost := {$config['snmpd']['trapserver']}
456
trapport := {$config['snmpd']['trapserverport']}
457
trap := "{$config['snmpd']['trapstring']}"
458

    
459

    
460
EOD;
461
		}
462

    
463

    
464
		$snmpdconf .= <<<EOD
465
system := 1     # pfSense
466
%snmpd
467
begemotSnmpdDebugDumpPdus       = 2
468
begemotSnmpdDebugSyslogPri      = 7
469
begemotSnmpdCommunityString.0.1 = $(read)
470

    
471
EOD;
472

    
473
/* No docs on what write strings do there for disable for now.
474
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
475
		    $snmpdconf .= <<<EOD
476
begemotSnmpdCommunityString.0.2 = $(write)
477

    
478
EOD;
479
		}
480
*/
481

    
482
		
483
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
484
		    $snmpdconf .= <<<EOD
485
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
486
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
487
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
488

    
489
EOD;
490
		}
491

    
492

    
493
		$snmpdconf .= <<<EOD
494
begemotSnmpdCommunityDisable    = 1
495

    
496
EOD;
497

    
498
		if(is_port( $config['snmpd']['pollport'] )) {
499
		    $snmpdconf .= <<<EOD
500
begemotSnmpdPortStatus.0.0.0.0.{$config['snmpd']['pollport']} = 1
501

    
502
EOD;
503

    
504
		}
505

    
506
		$snmpdconf .= <<<EOD
507
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
508
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
509

    
510
# These are bsnmp macros not php vars.
511
sysContact      = $(contact)
512
sysLocation     = $(location)
513
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
514

    
515
snmpEnableAuthenTraps = 2
516

    
517
EOD;
518

    
519
		if (is_array( $config['snmpd']['modules'] )) {
520
		    if(isset($config['snmpd']['modules']['mibii'])) {
521
			$snmpdconf .= <<<EOD
522
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
523

    
524
EOD;
525
		    }
526

    
527
		    if(isset($config['snmpd']['modules']['netgraph'])) {
528
			$snmpdconf .= <<<EOD
529
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
530
%netgraph
531
begemotNgControlNodeName = "snmpd"
532

    
533
EOD;
534
		    }
535

    
536
		    if(isset($config['snmpd']['modules']['pf'])) {
537
			$snmpdconf .= <<<EOD
538
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
539
# config must end with blank line
540

    
541

    
542
EOD;
543
		    }
544
		}
545

    
546
		fwrite($fd, $snmpdconf);
547
		fclose($fd);
548

    
549
		/* run bsnmpd */
550
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
551
			" -p {$g['varrun_path']}/snmpd.pid");		  
552
//		mwexec("/usr/local/sbin/snmpd -c {$g['varetc_path']}/snmpd.conf" .
553
//			" -P {$g['varrun_path']}/snmpd.pid");
554

    
555
		if ($g['booting'])
556
			echo "done.\n";
557
	}
558

    
559
	return 0;
560
}
561

    
562
function services_proxyarp_configure() {
563
	global $config, $g;
564

    
565
	/* kill any running choparp */
566
	killbyname("choparp");
567

    
568
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
569
		$paa = array();
570

    
571
		/* group by interface */
572
		foreach ($config['virtualip']['vip'] as $vipent) {
573
			if ($vipent['mode'] === "proxyarp") {
574
				if ($vipent['interface'])
575
					$if = $vipent['interface'];
576
				else
577
					$if = "wan";
578

    
579
				if (!is_array($paa[$if]))
580
					$paa[$if] = array();
581

    
582
				$paa[$if][] = $vipent;
583
			}
584
		}
585

    
586
		if (count($paa))
587
		foreach ($paa as $paif => $paents) {
588
			if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) ||
589
                                       ($config['interfaces']['wan']['ipaddr'] == "dhcp") ||
590
                                       ($config['interfaces']['wan']['ipaddr'] == "bigpond")))
591
                               continue;
592

    
593
			$args = $config['interfaces'][$paif]['if'] . " auto";
594

    
595
			foreach ($paents as $paent) {
596

    
597
				if (isset($paent['subnet']))
598
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
599
				else if (isset($paent['range']))
600
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" .
601
						$paent['range']['to']);
602
			}
603

    
604
			mwexec_bg("/usr/local/sbin/choparp " . $args);
605
		}
606
	}
607
}
608

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

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

    
679
function setup_wireless_olsr($interface) {
680
	$fd = fopen("{$g['varetc_path']}/{$interface}_olsr.conf", "w");
681
	$olsr .= <<<EOD
682
#
683
# olsr.org OLSR daemon config file
684
#
685
# Lines starting with a # are discarded
686
#
687
# This file was shipped with olsrd 0.X.X
688
#
689

    
690
# This file is an example of a typical
691
# configuration for a mostly static
692
# network(regarding mobility) using
693
# the LQ extention
694

    
695
# Debug level(0-9)
696
# If set to 0 the daemon runs in the background
697

    
698
DebugLevel	2
699

    
700
# IP version to use (4 or 6)
701

    
702
IpVersion	4
703

    
704
# Clear the screen each time the internal state changes
705

    
706
ClearScreen     yes
707

    
708
# HNA IPv4 routes
709
# syntax: netaddr netmask
710
# Example Internet gateway:
711
# 0.0.0.0 0.0.0.0
712

    
713
Hna4
714
{
715
#   Internet gateway:
716
#   0.0.0.0      0.0.0.0
717
#   more entries can be added:
718
#   192.168.1.0  255.255.255.0
719
}
720

    
721
# HNA IPv6 routes
722
# syntax: netaddr prefix
723
# Example Internet gateway:
724
Hna6
725
{
726
#   Internet gateway:
727
#   ::              0
728
#   more entries can be added:
729
#   fec0:2200:106:: 48
730
}
731

    
732

    
733
# Should olsrd keep on running even if there are
734
# no interfaces available? This is a good idea
735
# for a PCMCIA/USB hotswap environment.
736
# "yes" OR "no"
737

    
738
AllowNoInt	yes
739

    
740
# TOS(type of service) value for
741
# the IP header of control traffic.
742
# If not set it will default to 16
743

    
744
#TosValue	16
745

    
746
# The fixed willingness to use(0-7)
747
# If not set willingness will be calculated
748
# dynamically based on battery/power status
749
# if such information is available
750

    
751
#Willingness    	4
752

    
753
# Allow processes like the GUI front-end
754
# to connect to the daemon.
755

    
756
IpcConnect
757
{
758
     # Determines how many simultaneously
759
     # IPC connections that will be allowed
760
     # Setting this to 0 disables IPC
761

    
762
     MaxConnections  0
763

    
764
     # By default only 127.0.0.1 is allowed
765
     # to connect. Here allowed hosts can
766
     # be added
767

    
768
     Host            127.0.0.1
769
     #Host            10.0.0.5
770

    
771
     # You can also specify entire net-ranges 
772
     # that are allowed to connect. Multiple
773
     # entries are allowed
774

    
775
     #Net             192.168.1.0 255.255.255.0     
776
}
777

    
778
# Wether to use hysteresis or not
779
# Hysteresis adds more robustness to the
780
# link sensing but delays neighbor registration.
781
# Used by default. 'yes' or 'no'
782

    
783
UseHysteresis	no
784

    
785
# Hysteresis parameters
786
# Do not alter these unless you know 
787
# what you are doing!
788
# Set to auto by default. Allowed
789
# values are floating point values
790
# in the interval 0,1
791
# THR_LOW must always be lower than
792
# THR_HIGH.
793

    
794
#HystScaling	0.50
795
#HystThrHigh	0.80
796
#HystThrLow	0.30
797

    
798

    
799
# Link quality level
800
# 0 = do not use link quality
801
# 1 = use link quality for MPR selection
802
# 2 = use link quality for MPR selection and routing
803
# Defaults to 0
804

    
805
LinkQualityLevel	2
806

    
807
# Link quality window size
808
# Defaults to 10
809

    
810
LinkQualityWinSize	10
811

    
812
# Polling rate in seconds(float). 
813
# Default value 0.05 sec
814

    
815
Pollrate	0.05
816

    
817

    
818
# TC redundancy
819
# Specifies how much neighbor info should
820
# be sent in TC messages
821
# Possible values are:
822
# 0 - only send MPR selectors
823
# 1 - send MPR selectors and MPRs
824
# 2 - send all neighbors
825
#
826
# defaults to 0
827

    
828
TcRedundancy	2
829

    
830

    
831
#
832
# MPR coverage
833
# Specifies how many MPRs a node should
834
# try select to reach every 2 hop neighbor
835
#
836
# Can be set to any integer >0
837
#
838
# defaults to 1
839

    
840
MprCoverage	3
841

    
842

    
843
# Olsrd plugins to load
844
# This must be the absolute path to the file
845
# or the loader will use the following scheme:
846
# - Try the paths in the LD_LIBRARY_PATH 
847
#   environment variable.
848
# - The list of libraries cached in /etc/ld.so.cache
849
# - /lib, followed by /usr/lib
850

    
851
# Example plugin entry with parameters:
852

    
853
#LoadPlugin "olsrd_dyn_gw.so.0.3"
854
#{
855
    # Here parameters are set to be sent to the
856
    # plugin. Theese are on the form "key" "value".
857
    # Parameters ofcause, differs from plugin to plugin.
858
    # Consult the documentation of your plugin for details.
859

    
860
    # Example: dyn_gw params
861

    
862
    # how often to check for Internet connectivity
863
    # defaults to 5 secs
864
#   PlParam     "Interval"   "40"
865
    
866
    # if one or more IPv4 addresses are given, do a ping on these in
867
    # descending order to validate that there is not only an entry in
868
    # routing table, but also a real internet connection. If any of
869
    # these addresses could be pinged successfully, the test was
870
    # succesful, i.e. if the ping on the 1st address was successful,the
871
    # 2nd won't be pinged
872
#   PlParam     "Ping"       "141.1.1.1"
873
#   PlParam     "Ping"       "194.25.2.129"
874
#}
875

    
876

    
877

    
878
# Interfaces and their rules
879
# Omitted options will be set to the
880
# default values. Multiple interfaces
881
# can be specified in the same block
882
# and multiple blocks can be set.
883

    
884
# !!CHANGE THE INTERFACE LABEL(s) TO MATCH YOUR INTERFACE(s)!!
885
# (eg. wlan0 or eth1):
886

    
887
Interface "{$interface}" 
888
{
889

    
890
    # IPv4 broadcast address to use. The
891
    # one usefull example would be 255.255.255.255
892
    # If not defined the broadcastaddress
893
    # every card is configured with is used
894

    
895
    # Ip4Broadcast		255.255.255.255
896

    
897
    # IPv6 address scope to use.
898
    # Must be 'site-local' or 'global'
899

    
900
    # Ip6AddrType		site-local
901

    
902
    # IPv6 multicast address to use when
903
    # using site-local addresses.
904
    # If not defined, ff05::15 is used
905

    
906
    # Ip6MulticastSite		ff05::11
907

    
908
    # IPv6 multicast address to use when
909
    # using global addresses
910
    # If not defined, ff0e::1 is used
911

    
912
    # Ip6MulticastGlobal	ff0e::1
913

    
914

    
915
    # Emission intervals.
916
    # If not defined, RFC proposed values will
917
    # be used in most cases.
918

    
919
    # Hello interval in seconds(float)
920
    HelloInterval    2.0
921

    
922
    # HELLO validity time
923
    HelloValidityTime	20.0
924

    
925
    # TC interval in seconds(float)
926
    TcInterval        5.0
927

    
928
    # TC validity time
929
    TcValidityTime	30.0
930

    
931
    # MID interval in seconds(float)
932
    MidInterval	5.0
933

    
934
    # MID validity time
935
    MidValidityTime	30.0
936

    
937
    # HNA interval in seconds(float)
938
    HnaInterval	5.0
939

    
940
    # HNA validity time
941
    HnaValidityTime 	30.0
942

    
943
    # When multiple links exist between hosts
944
    # the weight of interface is used to determine
945
    # the link to use. Normally the weight is
946
    # automatically calculated by olsrd based
947
    # on the characteristics of the interface,
948
    # but here you can specify a fixed value.
949
    # Olsrd will choose links with the lowest value.
950

    
951
    # Weight 0
952

    
953

    
954
}
955
EOD;
956
	fwrite($fd, $olsr);
957
	fclose($fd);
958
	mwexec_bg("/usr/local/sbin/olsrd -f {$g['varetc_path']}/{$interface}_olsr.conf");
959
}
960

    
961
?>
(15-15/23)