Project

General

Profile

Download (27 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
			if($config['system']['use_old_dyndns'] <> "") {
383
				echo " [Using ez-ipupdate] ";
384
				services_dyndns_configure_old();
385
				exit;
386
			}	
387
		} else {
388
			sleep(1);
389
			if($config['system']['use_old_dyndns'] <> "") {
390
				services_dyndns_configure_old();
391
				exit;
392
			}			
393
		}
394

    
395
		$dns = new updatedns($dnsService = $config['dyndns']['type'],
396
							 $dnsHost = $config['dyndns']['host'],
397
							 $dnsUser = $config['dyndns']['username'],
398
							 $dnsPass = $config['dyndns']['password'],
399
							 $dnsWilcard = $config['dyndns']['wildcard'],
400
							 $dnsMX = $config['dyndns']['mx']);
401

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

    
406
	return 0;
407
}
408

    
409
function services_dyndns_configure_old() {
410
        global $config, $g;
411

    
412
        /* kill any running ez-ipupdate */
413
        /* ez-ipupdate needs SIGQUIT instead of SIGTERM */
414
        sigkillbypid("{$g['varrun_path']}/ez-ipupdate.pid", "QUIT");
415

    
416
        $dyndnscfg = $config['dyndns'];
417
        $wancfg = $config['interfaces']['wan'];
418

    
419
        if (isset($dyndnscfg['enable'])) {
420

    
421
                if ($g['booting'])
422
                        echo "Starting DynDNS client... ";
423
                else
424
                        sleep(1);
425

    
426
                /* determine WAN interface name */
427
                $wanif = get_real_wan_interface();
428

    
429
                /* write ez-ipupdate.conf */
430
                $fd = fopen("{$g['varetc_path']}/ez-ipupdate.conf", "w");
431
                if (!$fd) {
432
                        printf("Error: cannot open ez-ipupdate.conf in services_dyndns_configure().\n");
433
                        return 1;
434
                }
435

    
436
                $ezipupdateconf = <<<EOD
437
service-type={$dyndnscfg['type']}
438
user={$dyndnscfg['username']}:{$dyndnscfg['password']}
439
host={$dyndnscfg['host']}
440
interface={$wanif}
441
max-interval=2073600
442
pid-file={$g['varrun_path']}/ez-ipupdate.pid
443
cache-file={$g['vardb_path']}/ez-ipupdate.cache
444
execute=/etc/rc.dyndns.storecache
445
daemon
446

    
447
EOD;
448

    
449
                /* enable server[:port]? */
450
                if ($dyndnscfg['server']) {
451
                        if ($dyndnscfg['port'])
452
                                $ezipupdateconf .= "server={$dyndnscfg['server']}:{$dyndnscfg['port']}\n";
453
                        else
454
                                $ezipupdateconf .= "server={$dyndnscfg['server']}\n";
455
                }
456

    
457
                /* enable MX? */
458
                if ($dyndnscfg['mx']) {
459
                        $ezipupdateconf .= "mx={$dyndnscfg['mx']}\n";
460
                }
461

    
462
                /* enable wildcards? */
463
                if (isset($dyndnscfg['wildcard'])) {
464
                        $ezipupdateconf .= "wildcard\n";
465
                }
466

    
467
                fwrite($fd, $ezipupdateconf);
468
                fclose($fd);
469

    
470
                /* if we're booting, copy the cache file from /conf */
471
                if ($g['booting']) {
472
                        if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
473
                                copy("{$g['conf_path']}/ez-ipupdate.cache", "{$g['vardb_path']}/ez-ipupdate.cache");
474
                       }
475
                }
476

    
477
                /* run ez-ipupdate */
478
                mwexec("/usr/local/bin/ez-ipupdate -c {$g['varetc_path']}/ez-ipupdate.conf");
479

    
480
                if ($g['booting'])
481
                        echo "done\n";
482
        }
483

    
484
        return 0;
485
}
486

    
487

    
488
function services_dnsmasq_configure() {
489
	global $config, $g;
490

    
491
	/* kill any running dnsmasq */
492
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
493

    
494
	if (isset($config['dnsmasq']['enable'])) {
495

    
496
		if ($g['booting'])
497
			echo "Starting DNS forwarder... ";
498
		else
499
			sleep(1);
500

    
501
		/* generate hosts file */
502
		system_hosts_generate();
503

    
504
		$args = "";
505

    
506
		if (isset($config['dnsmasq']['regdhcp'])) {
507

    
508
			$args .= " -l {$g['vardb_path']}/dhcpd.leases" .
509
				" -s {$config['system']['domain']}";
510
		}
511

    
512
                if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
513
                        foreach($config['dnsmasq']['domainoverrides'] as $override) {
514
                                $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
515
                        }
516
                }
517

    
518
		/* run dnsmasq */
519
		mwexec("/usr/local/sbin/dnsmasq {$args}");
520

    
521
		if ($g['booting'])
522
			echo "done.\n";
523
	}
524

    
525
	if (!$g['booting']) {
526
		services_dhcpd_configure();
527
	}
528

    
529
	return 0;
530
}
531

    
532
function services_snmpd_configure() {
533
	global $config, $g;
534

    
535
	/* kill any running snmpd */
536
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
537

    
538
	if (isset($config['snmpd']['enable'])) {
539

    
540
		if ($g['booting'])
541
			echo "Starting SNMP daemon... ";
542

    
543
		/* generate snmpd.conf */
544
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
545
		if (!$fd) {
546
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
547
			return 1;
548
		}
549

    
550

    
551
		$snmpdconf = <<<EOD
552
location := "{$config['snmpd']['syslocation']}"
553
contact := "{$config['snmpd']['syscontact']}"
554
read := "{$config['snmpd']['rocommunity']}"
555

    
556
EOD;
557

    
558
/* No docs on what write strings do there for disable for now.
559
		if(isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
560
		    $snmpdconf .= <<<EOD
561
# write string
562
write := "{$config['snmpd']['rwcommunity']}"
563

    
564
EOD;
565
		}
566
*/
567

    
568

    
569
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
570
		    $snmpdconf .= <<<EOD
571
# SNMP Trap support.
572
traphost := {$config['snmpd']['trapserver']}
573
trapport := {$config['snmpd']['trapserverport']}
574
trap := "{$config['snmpd']['trapstring']}"
575

    
576

    
577
EOD;
578
		}
579

    
580

    
581
		$snmpdconf .= <<<EOD
582
system := 1     # pfSense
583
%snmpd
584
begemotSnmpdDebugDumpPdus       = 2
585
begemotSnmpdDebugSyslogPri      = 7
586
begemotSnmpdCommunityString.0.1 = $(read)
587

    
588
EOD;
589

    
590
/* No docs on what write strings do there for disable for now.
591
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
592
		    $snmpdconf .= <<<EOD
593
begemotSnmpdCommunityString.0.2 = $(write)
594

    
595
EOD;
596
		}
597
*/
598

    
599
		
600
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
601
		    $snmpdconf .= <<<EOD
602
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
603
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
604
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
605

    
606
EOD;
607
		}
608

    
609

    
610
		$snmpdconf .= <<<EOD
611
begemotSnmpdCommunityDisable    = 1
612

    
613
EOD;
614

    
615
		if(is_port( $config['snmpd']['pollport'] )) {
616
		    $snmpdconf .= <<<EOD
617
begemotSnmpdPortStatus.0.0.0.0.{$config['snmpd']['pollport']} = 1
618

    
619
EOD;
620

    
621
		}
622

    
623
		$snmpdconf .= <<<EOD
624
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
625
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
626

    
627
# These are bsnmp macros not php vars.
628
sysContact      = $(contact)
629
sysLocation     = $(location)
630
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
631

    
632
snmpEnableAuthenTraps = 2
633

    
634
EOD;
635

    
636
		if (is_array( $config['snmpd']['modules'] )) {
637
		    if(isset($config['snmpd']['modules']['mibii'])) {
638
			$snmpdconf .= <<<EOD
639
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
640

    
641
EOD;
642
		    }
643

    
644
		    if(isset($config['snmpd']['modules']['netgraph'])) {
645
			$snmpdconf .= <<<EOD
646
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
647
%netgraph
648
begemotNgControlNodeName = "snmpd"
649

    
650
EOD;
651
		    }
652

    
653
		    if(isset($config['snmpd']['modules']['pf'])) {
654
			$snmpdconf .= <<<EOD
655
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
656
# config must end with blank line
657

    
658

    
659
EOD;
660
		    }
661
		}
662

    
663
		fwrite($fd, $snmpdconf);
664
		fclose($fd);
665

    
666
		/* run bsnmpd */
667
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
668
			" -p {$g['varrun_path']}/snmpd.pid");
669

    
670
		if ($g['booting'])
671
			echo "done.\n";
672
	}
673

    
674
	return 0;
675
}
676

    
677
function services_proxyarp_configure() {
678
	global $config, $g;
679

    
680
	/* kill any running choparp */
681
	killbyname("choparp");
682

    
683
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
684
		$paa = array();
685

    
686
		/* group by interface */
687
		foreach ($config['virtualip']['vip'] as $vipent) {
688
			if ($vipent['mode'] === "proxyarp") {
689
				if ($vipent['interface'])
690
					$if = $vipent['interface'];
691
				else
692
					$if = "wan";
693

    
694
				if (!is_array($paa[$if]))
695
					$paa[$if] = array();
696

    
697
				$paa[$if][] = $vipent;
698
			}
699
		}
700

    
701
		if (count($paa))
702
		foreach ($paa as $paif => $paents) {
703
			if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) ||
704
                                       ($config['interfaces']['wan']['ipaddr'] == "dhcp") ||
705
                                       ($config['interfaces']['wan']['ipaddr'] == "bigpond")))
706
                               continue;
707

    
708
			$args = $config['interfaces'][$paif]['if'] . " auto";
709

    
710
			foreach ($paents as $paent) {
711

    
712
				if (isset($paent['subnet']))
713
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
714
				else if (isset($paent['range']))
715
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" .
716
						$paent['range']['to']);
717
			}
718

    
719
			mwexec_bg("/usr/local/sbin/choparp " . $args);
720
		}
721
	}
722
}
723

    
724
function services_dnsupdate_process() {
725
	global $config, $g;
726
	
727
	/* Dynamic DNS updating active? */
728
	if (isset($config['dnsupdate']['enable'])) {
729
		
730
		$wanip = get_current_wan_address();
731
		if ($wanip) {
732
			
733
			$keyname = $config['dnsupdate']['keyname'];
734
			/* trailing dot */
735
			if (substr($keyname, -1) != ".")
736
				$keyname .= ".";
737
			
738
			$hostname = $config['dnsupdate']['host'];
739
			/* trailing dot */
740
			if (substr($hostname, -1) != ".")
741
				$hostname .= ".";
742
			
743
			/* write private key file
744
			   this is dumb - public and private keys are the same for HMAC-MD5,
745
			   but nsupdate insists on having both */
746
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.private", "w");
747
			$privkey .= <<<EOD
748
Private-key-format: v1.2
749
Algorithm: 157 (HMAC)
750
Key: {$config['dnsupdate']['keydata']}
751

    
752
EOD;
753
			fwrite($fd, $privkey);
754
			fclose($fd);
755
			
756
			/* write public key file */
757
			if ($config['dnsupdate']['keytype'] == "zone") {
758
				$flags = 257;
759
				$proto = 3;
760
			} else if ($config['dnsupdate']['keytype'] == "host") {
761
				$flags = 513;
762
				$proto = 3;
763
			} else if ($config['dnsupdate']['keytype'] == "user") {
764
				$flags = 0;
765
				$proto = 2;
766
			}
767
			
768
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.key", "w");
769
			fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$config['dnsupdate']['keydata']}\n");
770
			fclose($fd);
771
			
772
			/* generate update instructions */
773
			$upinst =  "update delete {$config['dnsupdate']['host']} A\n";
774
			$upinst .= "update add {$config['dnsupdate']['host']} {$config['dnsupdate']['ttl']} A {$wanip}\n";
775
			$upinst .= "\n";	/* mind that trailing newline! */
776
			
777
			$fd = fopen("{$g['varetc_path']}/nsupdatecmds", "w");
778
			fwrite($fd, $upinst);
779
			fclose($fd);
780
			
781
			/* invoke nsupdate */
782
			$cmd = "/usr/sbin/nsupdate -k {$g['varetc_path']}:{$keyname}";
783
			if (isset($config['dnsupdate']['usetcp']))
784
				$cmd .= " -v";
785
			$cmd .= " {$g['varetc_path']}/nsupdatecmds";
786
			
787
			mwexec_bg($cmd);
788
		}
789
	}
790
	
791
	return 0;
792
}
793

    
794
function setup_wireless_olsr($interface) {
795
	$fd = fopen("{$g['varetc_path']}/{$interface}_olsr.conf", "w");
796
	$olsr .= <<<EOD
797
#
798
# olsr.org OLSR daemon config file
799
#
800
# Lines starting with a # are discarded
801
#
802
# This file was shipped with olsrd 0.X.X
803
#
804

    
805
# This file is an example of a typical
806
# configuration for a mostly static
807
# network(regarding mobility) using
808
# the LQ extention
809

    
810
# Debug level(0-9)
811
# If set to 0 the daemon runs in the background
812

    
813
DebugLevel	2
814

    
815
# IP version to use (4 or 6)
816

    
817
IpVersion	4
818

    
819
# Clear the screen each time the internal state changes
820

    
821
ClearScreen     yes
822

    
823
# HNA IPv4 routes
824
# syntax: netaddr netmask
825
# Example Internet gateway:
826
# 0.0.0.0 0.0.0.0
827

    
828
Hna4
829
{
830
#   Internet gateway:
831
#   0.0.0.0      0.0.0.0
832
#   more entries can be added:
833
#   192.168.1.0  255.255.255.0
834
}
835

    
836
# HNA IPv6 routes
837
# syntax: netaddr prefix
838
# Example Internet gateway:
839
Hna6
840
{
841
#   Internet gateway:
842
#   ::              0
843
#   more entries can be added:
844
#   fec0:2200:106:: 48
845
}
846

    
847

    
848
# Should olsrd keep on running even if there are
849
# no interfaces available? This is a good idea
850
# for a PCMCIA/USB hotswap environment.
851
# "yes" OR "no"
852

    
853
AllowNoInt	yes
854

    
855
# TOS(type of service) value for
856
# the IP header of control traffic.
857
# If not set it will default to 16
858

    
859
#TosValue	16
860

    
861
# The fixed willingness to use(0-7)
862
# If not set willingness will be calculated
863
# dynamically based on battery/power status
864
# if such information is available
865

    
866
#Willingness    	4
867

    
868
# Allow processes like the GUI front-end
869
# to connect to the daemon.
870

    
871
IpcConnect
872
{
873
     # Determines how many simultaneously
874
     # IPC connections that will be allowed
875
     # Setting this to 0 disables IPC
876

    
877
     MaxConnections  0
878

    
879
     # By default only 127.0.0.1 is allowed
880
     # to connect. Here allowed hosts can
881
     # be added
882

    
883
     Host            127.0.0.1
884
     #Host            10.0.0.5
885

    
886
     # You can also specify entire net-ranges 
887
     # that are allowed to connect. Multiple
888
     # entries are allowed
889

    
890
     #Net             192.168.1.0 255.255.255.0     
891
}
892

    
893
# Wether to use hysteresis or not
894
# Hysteresis adds more robustness to the
895
# link sensing but delays neighbor registration.
896
# Used by default. 'yes' or 'no'
897

    
898
UseHysteresis	no
899

    
900
# Hysteresis parameters
901
# Do not alter these unless you know 
902
# what you are doing!
903
# Set to auto by default. Allowed
904
# values are floating point values
905
# in the interval 0,1
906
# THR_LOW must always be lower than
907
# THR_HIGH.
908

    
909
#HystScaling	0.50
910
#HystThrHigh	0.80
911
#HystThrLow	0.30
912

    
913

    
914
# Link quality level
915
# 0 = do not use link quality
916
# 1 = use link quality for MPR selection
917
# 2 = use link quality for MPR selection and routing
918
# Defaults to 0
919

    
920
LinkQualityLevel	2
921

    
922
# Link quality window size
923
# Defaults to 10
924

    
925
LinkQualityWinSize	10
926

    
927
# Polling rate in seconds(float). 
928
# Default value 0.05 sec
929

    
930
Pollrate	0.05
931

    
932

    
933
# TC redundancy
934
# Specifies how much neighbor info should
935
# be sent in TC messages
936
# Possible values are:
937
# 0 - only send MPR selectors
938
# 1 - send MPR selectors and MPRs
939
# 2 - send all neighbors
940
#
941
# defaults to 0
942

    
943
TcRedundancy	2
944

    
945

    
946
#
947
# MPR coverage
948
# Specifies how many MPRs a node should
949
# try select to reach every 2 hop neighbor
950
#
951
# Can be set to any integer >0
952
#
953
# defaults to 1
954

    
955
MprCoverage	3
956

    
957

    
958
# Olsrd plugins to load
959
# This must be the absolute path to the file
960
# or the loader will use the following scheme:
961
# - Try the paths in the LD_LIBRARY_PATH 
962
#   environment variable.
963
# - The list of libraries cached in /etc/ld.so.cache
964
# - /lib, followed by /usr/lib
965

    
966
# Example plugin entry with parameters:
967

    
968
#LoadPlugin "olsrd_dyn_gw.so.0.3"
969
#{
970
    # Here parameters are set to be sent to the
971
    # plugin. Theese are on the form "key" "value".
972
    # Parameters ofcause, differs from plugin to plugin.
973
    # Consult the documentation of your plugin for details.
974

    
975
    # Example: dyn_gw params
976

    
977
    # how often to check for Internet connectivity
978
    # defaults to 5 secs
979
#   PlParam     "Interval"   "40"
980
    
981
    # if one or more IPv4 addresses are given, do a ping on these in
982
    # descending order to validate that there is not only an entry in
983
    # routing table, but also a real internet connection. If any of
984
    # these addresses could be pinged successfully, the test was
985
    # succesful, i.e. if the ping on the 1st address was successful,the
986
    # 2nd won't be pinged
987
#   PlParam     "Ping"       "141.1.1.1"
988
#   PlParam     "Ping"       "194.25.2.129"
989
#}
990

    
991

    
992

    
993
# Interfaces and their rules
994
# Omitted options will be set to the
995
# default values. Multiple interfaces
996
# can be specified in the same block
997
# and multiple blocks can be set.
998

    
999
# !!CHANGE THE INTERFACE LABEL(s) TO MATCH YOUR INTERFACE(s)!!
1000
# (eg. wlan0 or eth1):
1001

    
1002
Interface "{$interface}" 
1003
{
1004

    
1005
    # IPv4 broadcast address to use. The
1006
    # one usefull example would be 255.255.255.255
1007
    # If not defined the broadcastaddress
1008
    # every card is configured with is used
1009

    
1010
    # Ip4Broadcast		255.255.255.255
1011

    
1012
    # IPv6 address scope to use.
1013
    # Must be 'site-local' or 'global'
1014

    
1015
    # Ip6AddrType		site-local
1016

    
1017
    # IPv6 multicast address to use when
1018
    # using site-local addresses.
1019
    # If not defined, ff05::15 is used
1020

    
1021
    # Ip6MulticastSite		ff05::11
1022

    
1023
    # IPv6 multicast address to use when
1024
    # using global addresses
1025
    # If not defined, ff0e::1 is used
1026

    
1027
    # Ip6MulticastGlobal	ff0e::1
1028

    
1029

    
1030
    # Emission intervals.
1031
    # If not defined, RFC proposed values will
1032
    # be used in most cases.
1033

    
1034
    # Hello interval in seconds(float)
1035
    HelloInterval    2.0
1036

    
1037
    # HELLO validity time
1038
    HelloValidityTime	20.0
1039

    
1040
    # TC interval in seconds(float)
1041
    TcInterval        5.0
1042

    
1043
    # TC validity time
1044
    TcValidityTime	30.0
1045

    
1046
    # MID interval in seconds(float)
1047
    MidInterval	5.0
1048

    
1049
    # MID validity time
1050
    MidValidityTime	30.0
1051

    
1052
    # HNA interval in seconds(float)
1053
    HnaInterval	5.0
1054

    
1055
    # HNA validity time
1056
    HnaValidityTime 	30.0
1057

    
1058
    # When multiple links exist between hosts
1059
    # the weight of interface is used to determine
1060
    # the link to use. Normally the weight is
1061
    # automatically calculated by olsrd based
1062
    # on the characteristics of the interface,
1063
    # but here you can specify a fixed value.
1064
    # Olsrd will choose links with the lowest value.
1065

    
1066
    # Weight 0
1067

    
1068

    
1069
}
1070
EOD;
1071
	fwrite($fd, $olsr);
1072
	fclose($fd);
1073
	mwexec_bg("/usr/local/sbin/olsrd -f {$g['varetc_path']}/{$interface}_olsr.conf");
1074
}
1075

    
1076
?>
(15-15/24)