Project

General

Profile

Download (28.5 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
	if(isset($config['system']['developerspew'])) {
39
		$mt = microtime();
40
		echo "services_dhcpd_configure($if) being called $mt\n";
41
	}
42

    
43
	/* kill any running dhcpd */
44
	killbypid("{$g['varrun_path']}/dhcpd.pid");
45

    
46
	$syscfg = $config['system'];
47
	$dhcpdcfg = $config['dhcpd'];
48

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

    
59
	if (!$dhcpdenable)
60
		return 0;
61

    
62
	if ($g['booting'])
63
		echo "Starting DHCP service... ";
64
	else
65
		sleep(1);
66

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

    
74
	
75

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

    
84
EOD;
85

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

    
139
EOPP;
140
		$dhcpnum++;
141
		}
142
	}
143

    
144
	$dhcpnum = 0;
145

    
146
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
147

    
148
		$ifcfg = $config['interfaces'][$dhcpif];
149

    
150
		if (!isset($dhcpifconf['enable']) ||
151
			(($dhcpif != "lan") &&
152
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
153
			continue;
154

    
155
		$subnet = gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);
156
		$subnetmask = gen_subnet_mask($ifcfg['subnet']);
157

    
158
		$dnscfg = "";
159

    
160
		if ($dhcpifconf['domain']) {
161
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
162
		}
163

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

    
172
		$dhcpdconf .= "subnet $subnet netmask $subnetmask {\n";
173
		$dhcpdconf .= "	pool {\n";
174

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

    
189
		if ($dhcpifconf['gateway'])
190
			$routers = $dhcpifconf['gateway'];
191
		else
192
			$routers = $ifcfg['ipaddr'];
193

    
194
		if($dhcpifconf['failover_peerip'] <> "") {
195
			$dhcpdconf .= "		failover peer \"dhcp{$dhcpnum}\";\n";
196
			$dhcpnum++;
197
		}
198

    
199
		$dhcpdconf .= <<<EOD
200
		range {$dhcpifconf['range']['from']} {$dhcpifconf['range']['to']};
201
	}
202
	option routers {$routers};
203
$dnscfg
204

    
205
EOD;
206

    
207
		if ($dhcpifconf['defaultleasetime'])
208
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
209
		if ($dhcpifconf['maxleasetime'])
210
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
211

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

    
217
		if ($dhcpifconf['next-server'])
218
			$dhcpdconf .= "	next-server {$dhcpifconf['next-server']};\n";
219
		if ($dhcpifconf['filename'])
220
			$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
221

    
222
		$dhcpdconf .= <<<EOD
223
}
224

    
225
EOD;
226

    
227
		/* add static mappings */
228
		if (is_array($dhcpifconf['staticmap'])) {
229

    
230
			$i = 0;
231
			foreach ($dhcpifconf['staticmap'] as $sm) {
232
				$dhcpdconf .= <<<EOD
233
host s_{$dhcpif}_{$i} {
234
	hardware ethernet {$sm['mac']};
235

    
236
EOD;
237
				if ($sm['ipaddr'])
238
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
239

    
240
				$dhcpdconf .= "}\n";
241
				$i++;
242
			}
243
		}
244

    
245
		$dhcpdifs[] = $ifcfg['if'];
246
	}
247

    
248
	fwrite($fd, $dhcpdconf);
249
	fclose($fd);
250

    
251
	/* create an empty leases database */
252
	touch("{$g['vardb_path']}/dhcpd.leases");
253

    
254
	/* fire up dhcpd */
255
	mwexec("/usr/local/sbin/dhcpd -cf {$g['varetc_path']}/dhcpd.conf " .
256
		join(" ", $dhcpdifs));
257

    
258
	if ($g['booting']) {
259
                print "done.\n";
260
	}
261

    
262
	return 0;
263
}
264

    
265
function interfaces_staticarp_configure($if) {
266
	global $config, $g;
267
	if(isset($config['system']['developerspew'])) {
268
		$mt = microtime();
269
		echo "interfaces_staticarp_configure($if) being called $mt\n";
270
	}
271
        
272
        $ifcfg = $config['interfaces'][$if];
273

    
274
        /* Enable staticarp, if enabled */
275
        if(isset($config['dhcpd'][$if]['staticarp'])) {
276
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " );
277
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
278
                if (is_array($config['dhcpd'][$if]['staticmap'])) {
279

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

    
283
                        }
284
                        
285
                }
286
        } else {
287
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " );
288
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
289
        }
290

    
291
        return 0;
292
}
293

    
294
function services_dhcrelay_configure() {
295
	global $config, $g;
296
	if(isset($config['system']['developerspew'])) {
297
		$mt = microtime();
298
		echo "services_dhcrelay_configure() being called $mt\n";
299
	}
300

    
301
	/* kill any running dhcrelay */
302
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
303

    
304
	$dhcrelaycfg = $config['dhcrelay'];
305

    
306
	/* DHCPRelay enabled on any interfaces? */
307
	$dhcrelayenable = false;
308
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
309
		if (isset($dhcrelayifconf['enable']) &&
310
			(($dhcrelayif == "lan") ||
311
			(isset($config['interfaces'][$dhcrelayif]['enable']) &&
312
			$config['interfaces'][$dhcrelayif]['if'] && (!$config['interfaces'][$dhcrelayif]['bridge']))))
313
			$dhcrelayenable = true;
314
	}
315

    
316
	if (!$dhcrelayenable)
317
		return 0;
318

    
319
	if ($g['booting'])
320
		echo "Starting DHCP relay service... ";
321
	else
322
		sleep(1);
323

    
324
	$dhcrelayifs = array();
325
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
326

    
327
		$ifcfg = $config['interfaces'][$dhcrelayif];
328

    
329
		if (!isset($dhcrelayifconf['enable']) ||
330
			(($dhcrelayif != "lan") &&
331
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
332
			continue;
333

    
334
		$dhcrelayifs[] = $ifcfg['if'];
335
	}
336

    
337
	/* In order for the relay to work, it needs to be active on the
338
	   interface in which the destination server sits */
339
	foreach ($config['interfaces'] as $ifname) {
340
		$subnet = $ifname['ipaddr'] . "/" . $ifname['subnet'];
341
		if (ip_in_subnet($dhcrelaycfg['server'],$subnet))
342
			$destif = $ifname['if'];
343
	}
344

    
345
	if (!isset($destif))
346
		$destif = $config['interfaces']['wan']['if'];
347

    
348
	$dhcrelayifs[] = $destif;
349
	$dhcrelayifs = array_unique($dhcrelayifs);
350

    
351
	/* fire up dhcrelay */
352
	$cmd = "/usr/local/sbin/dhcrelay -i " .  join(" -i ", $dhcrelayifs);
353

    
354
	if (isset($dhcrelaycfg['agentoption']))
355
		$cmd .=  " -a -m replace";
356

    
357
	$cmd .= " {$dhcrelaycfg['server']}";
358
	mwexec($cmd);
359

    
360
	if (!$g['booting']) {
361
		/* set the reload filter dity flag */
362
		touch("{$g['tmp_path']}/filter_dirty");
363
	}
364

    
365
	return 0;
366
}
367

    
368
function services_dyndns_reset() {
369
	global $config, $g;
370
	if(isset($config['system']['developerspew'])) {
371
		$mt = microtime();
372
		echo "services_dyndns_reset() being called $mt\n";
373
	}
374

    
375
	if (file_exists("{$g['vardb_path']}/ez-ipupdate.cache")) {
376
		unlink("{$g['vardb_path']}/ez-ipupdate.cache");
377
	}
378

    
379
	if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
380
		conf_mount_rw();
381
		unlink("{$g['conf_path']}/ez-ipupdate.cache");
382
		conf_mount_ro();
383
	}
384

    
385
	return 0;
386
}
387

    
388
function services_dyndns_configure() {
389
	global $config, $g;
390
	if(isset($config['system']['developerspew'])) {
391
		$mt = microtime();
392
		echo "services_dyndns_configure() being called $mt\n";
393
	}
394

    
395
	$dyndnscfg = $config['dyndns'];
396
	$wancfg = $config['interfaces']['wan'];
397

    
398
	if (isset($dyndnscfg['enable'])) {
399

    
400
		if ($g['booting']) {
401
			echo "Starting DynDNS client... ";
402
			if($config['system']['use_old_dyndns'] <> "") {
403
				echo " [Using ez-ipupdate] ";
404
				services_dyndns_configure_old();
405
				return;
406
			}	
407
		} else {
408
			sleep(1);
409
			if($config['system']['use_old_dyndns'] <> "") {
410
				services_dyndns_configure_old();
411
				return;
412
			}			
413
		}
414

    
415
		$dns = new updatedns($dnsService = $config['dyndns']['type'],
416
							 $dnsHost = $config['dyndns']['host'],
417
							 $dnsUser = $config['dyndns']['username'],
418
							 $dnsPass = $config['dyndns']['password'],
419
							 $dnsWilcard = $config['dyndns']['wildcard'],
420
							 $dnsMX = $config['dyndns']['mx']);
421

    
422
		if ($g['booting'])
423
			echo "done.\n";
424
	}
425

    
426
	return 0;
427
}
428

    
429
function services_dyndns_configure_old() {
430
	global $config, $g;
431
	if(isset($config['system']['developerspew'])) {
432
		$mt = microtime();
433
		echo "services_dyndns_configure_old() being called $mt\n";
434
	}
435

    
436
        /* kill any running ez-ipupdate */
437
        /* ez-ipupdate needs SIGQUIT instead of SIGTERM */
438
        sigkillbypid("{$g['varrun_path']}/ez-ipupdate.pid", "QUIT");
439

    
440
        $dyndnscfg = $config['dyndns'];
441
        $wancfg = $config['interfaces']['wan'];
442

    
443
        if (isset($dyndnscfg['enable'])) {
444

    
445
                if ($g['booting'])
446
                        echo "Starting DynDNS client... ";
447
                else
448
                        sleep(1);
449

    
450
                /* determine WAN interface name */
451
                $wanif = get_real_wan_interface();
452

    
453
                /* write ez-ipupdate.conf */
454
                $fd = fopen("{$g['varetc_path']}/ez-ipupdate.conf", "w");
455
                if (!$fd) {
456
                        printf("Error: cannot open ez-ipupdate.conf in services_dyndns_configure().\n");
457
                        return 1;
458
                }
459

    
460
                $ezipupdateconf = <<<EOD
461
service-type={$dyndnscfg['type']}
462
user={$dyndnscfg['username']}:{$dyndnscfg['password']}
463
host={$dyndnscfg['host']}
464
interface={$wanif}
465
max-interval=2073600
466
pid-file={$g['varrun_path']}/ez-ipupdate.pid
467
cache-file={$g['vardb_path']}/ez-ipupdate.cache
468
execute=/etc/rc.dyndns.storecache
469
daemon
470

    
471
EOD;
472

    
473
                /* enable server[:port]? */
474
                if ($dyndnscfg['server']) {
475
                        if ($dyndnscfg['port'])
476
                                $ezipupdateconf .= "server={$dyndnscfg['server']}:{$dyndnscfg['port']}\n";
477
                        else
478
                                $ezipupdateconf .= "server={$dyndnscfg['server']}\n";
479
                }
480

    
481
                /* enable MX? */
482
                if ($dyndnscfg['mx']) {
483
                        $ezipupdateconf .= "mx={$dyndnscfg['mx']}\n";
484
                }
485

    
486
                /* enable wildcards? */
487
                if (isset($dyndnscfg['wildcard'])) {
488
                        $ezipupdateconf .= "wildcard\n";
489
                }
490

    
491
                fwrite($fd, $ezipupdateconf);
492
                fclose($fd);
493

    
494
                /* if we're booting, copy the cache file from /conf */
495
                if ($g['booting']) {
496
                        if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
497
                                copy("{$g['conf_path']}/ez-ipupdate.cache", "{$g['vardb_path']}/ez-ipupdate.cache");
498
                       }
499
                }
500

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

    
504
                if ($g['booting'])
505
                        echo "done\n";
506
        }
507

    
508
        return 0;
509
}
510

    
511

    
512
function services_dnsmasq_configure() {
513
	global $config, $g;
514
	if(isset($config['system']['developerspew'])) {
515
		$mt = microtime();
516
		echo "services_dnsmasq_configure() being called $mt\n";
517
	}
518

    
519
	/* kill any running dnsmasq */
520
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
521

    
522
	if (isset($config['dnsmasq']['enable'])) {
523

    
524
		if ($g['booting'])
525
			echo "Starting DNS forwarder... ";
526
		else
527
			sleep(1);
528

    
529
		/* generate hosts file */
530
		system_hosts_generate();
531

    
532
		$args = "";
533

    
534
		if (isset($config['dnsmasq']['regdhcp'])) {
535

    
536
			$args .= " -l {$g['vardb_path']}/dhcpd.leases" .
537
				" -s {$config['system']['domain']}";
538
		}
539

    
540
                if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
541
                        foreach($config['dnsmasq']['domainoverrides'] as $override) {
542
                                $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
543
                        }
544
                }
545

    
546
		/* run dnsmasq */
547
		mwexec("/usr/local/sbin/dnsmasq {$args}");
548

    
549
		if ($g['booting'])
550
			echo "done.\n";
551
	}
552

    
553
	if (!$g['booting']) {
554
		services_dhcpd_configure();
555
	}
556

    
557
	return 0;
558
}
559

    
560
function services_snmpd_configure() {
561
	global $config, $g;
562
	if(isset($config['system']['developerspew'])) {
563
		$mt = microtime();
564
		echo "services_snmpd_configure() being called $mt\n";
565
	}
566

    
567
	/* kill any running snmpd */
568
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
569

    
570
	if (isset($config['snmpd']['enable'])) {
571

    
572
		if ($g['booting'])
573
			echo "Starting SNMP daemon... ";
574

    
575
		/* generate snmpd.conf */
576
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
577
		if (!$fd) {
578
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
579
			return 1;
580
		}
581

    
582

    
583
		$snmpdconf = <<<EOD
584
location := "{$config['snmpd']['syslocation']}"
585
contact := "{$config['snmpd']['syscontact']}"
586
read := "{$config['snmpd']['rocommunity']}"
587

    
588
EOD;
589

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

    
596
EOD;
597
		}
598
*/
599

    
600

    
601
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
602
		    $snmpdconf .= <<<EOD
603
# SNMP Trap support.
604
traphost := {$config['snmpd']['trapserver']}
605
trapport := {$config['snmpd']['trapserverport']}
606
trap := "{$config['snmpd']['trapstring']}"
607

    
608

    
609
EOD;
610
		}
611

    
612

    
613
		$snmpdconf .= <<<EOD
614
system := 1     # pfSense
615
%snmpd
616
begemotSnmpdDebugDumpPdus       = 2
617
begemotSnmpdDebugSyslogPri      = 7
618
begemotSnmpdCommunityString.0.1 = $(read)
619

    
620
EOD;
621

    
622
/* No docs on what write strings do there for disable for now.
623
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
624
		    $snmpdconf .= <<<EOD
625
begemotSnmpdCommunityString.0.2 = $(write)
626

    
627
EOD;
628
		}
629
*/
630

    
631
		
632
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
633
		    $snmpdconf .= <<<EOD
634
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
635
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
636
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
637

    
638
EOD;
639
		}
640

    
641

    
642
		$snmpdconf .= <<<EOD
643
begemotSnmpdCommunityDisable    = 1
644

    
645
EOD;
646

    
647
		if(is_port( $config['snmpd']['pollport'] )) {
648
		    $snmpdconf .= <<<EOD
649
begemotSnmpdPortStatus.0.0.0.0.{$config['snmpd']['pollport']} = 1
650

    
651
EOD;
652

    
653
		}
654

    
655
		$snmpdconf .= <<<EOD
656
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
657
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
658

    
659
# These are bsnmp macros not php vars.
660
sysContact      = $(contact)
661
sysLocation     = $(location)
662
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
663

    
664
snmpEnableAuthenTraps = 2
665

    
666
EOD;
667

    
668
		if (is_array( $config['snmpd']['modules'] )) {
669
		    if(isset($config['snmpd']['modules']['mibii'])) {
670
			$snmpdconf .= <<<EOD
671
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
672

    
673
EOD;
674
		    }
675

    
676
		    if(isset($config['snmpd']['modules']['netgraph'])) {
677
			$snmpdconf .= <<<EOD
678
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
679
%netgraph
680
begemotNgControlNodeName = "snmpd"
681

    
682
EOD;
683
		    }
684

    
685
		    if(isset($config['snmpd']['modules']['pf'])) {
686
			$snmpdconf .= <<<EOD
687
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
688
# config must end with blank line
689

    
690

    
691
EOD;
692
		    }
693
		}
694

    
695
		fwrite($fd, $snmpdconf);
696
		fclose($fd);
697

    
698
		/* run bsnmpd */
699
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
700
			" -p {$g['varrun_path']}/snmpd.pid");
701

    
702
		if ($g['booting'])
703
			echo "done.\n";
704
	}
705

    
706
	return 0;
707
}
708

    
709
function services_proxyarp_configure() {
710
	global $config, $g;
711
	if(isset($config['system']['developerspew'])) {
712
		$mt = microtime();
713
		echo "services_proxyarp_configure() being called $mt\n";
714
	}
715

    
716
	/* kill any running choparp */
717
	killbyname("choparp");
718

    
719
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
720
		$paa = array();
721

    
722
		/* group by interface */
723
		foreach ($config['virtualip']['vip'] as $vipent) {
724
			if ($vipent['mode'] === "proxyarp") {
725
				if ($vipent['interface'])
726
					$if = $vipent['interface'];
727
				else
728
					$if = "wan";
729

    
730
				if (!is_array($paa[$if]))
731
					$paa[$if] = array();
732

    
733
				$paa[$if][] = $vipent;
734
			}
735
		}
736

    
737
		if (count($paa))
738
		foreach ($paa as $paif => $paents) {
739
			if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) ||
740
                                       ($config['interfaces']['wan']['ipaddr'] == "dhcp") ||
741
                                       ($config['interfaces']['wan']['ipaddr'] == "bigpond")))
742
                               continue;
743

    
744
			$args = $config['interfaces'][$paif]['if'] . " auto";
745

    
746
			foreach ($paents as $paent) {
747

    
748
				if (isset($paent['subnet']))
749
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
750
				else if (isset($paent['range']))
751
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" .
752
						$paent['range']['to']);
753
			}
754

    
755
			mwexec_bg("/usr/local/sbin/choparp " . $args);
756
		}
757
	}
758
}
759

    
760
function services_dnsupdate_process() {
761
	global $config, $g;
762
	if(isset($config['system']['developerspew'])) {
763
		$mt = microtime();
764
		echo "services_dnsupdate_process() being called $mt\n";
765
	}
766

    
767
	/* Dynamic DNS updating active? */
768
	if (isset($config['dnsupdate']['enable'])) {
769
		
770
		$wanip = get_current_wan_address();
771
		if ($wanip) {
772
			
773
			$keyname = $config['dnsupdate']['keyname'];
774
			/* trailing dot */
775
			if (substr($keyname, -1) != ".")
776
				$keyname .= ".";
777
			
778
			$hostname = $config['dnsupdate']['host'];
779
			/* trailing dot */
780
			if (substr($hostname, -1) != ".")
781
				$hostname .= ".";
782
			
783
			/* write private key file
784
			   this is dumb - public and private keys are the same for HMAC-MD5,
785
			   but nsupdate insists on having both */
786
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.private", "w");
787
			$privkey .= <<<EOD
788
Private-key-format: v1.2
789
Algorithm: 157 (HMAC)
790
Key: {$config['dnsupdate']['keydata']}
791

    
792
EOD;
793
			fwrite($fd, $privkey);
794
			fclose($fd);
795
			
796
			/* write public key file */
797
			if ($config['dnsupdate']['keytype'] == "zone") {
798
				$flags = 257;
799
				$proto = 3;
800
			} else if ($config['dnsupdate']['keytype'] == "host") {
801
				$flags = 513;
802
				$proto = 3;
803
			} else if ($config['dnsupdate']['keytype'] == "user") {
804
				$flags = 0;
805
				$proto = 2;
806
			}
807
			
808
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.key", "w");
809
			fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$config['dnsupdate']['keydata']}\n");
810
			fclose($fd);
811
			
812
			/* generate update instructions */
813
			$upinst =  "update delete {$config['dnsupdate']['host']} A\n";
814
			$upinst .= "update add {$config['dnsupdate']['host']} {$config['dnsupdate']['ttl']} A {$wanip}\n";
815
			$upinst .= "\n";	/* mind that trailing newline! */
816
			
817
			$fd = fopen("{$g['varetc_path']}/nsupdatecmds", "w");
818
			fwrite($fd, $upinst);
819
			fclose($fd);
820
			
821
			/* invoke nsupdate */
822
			$cmd = "/usr/sbin/nsupdate -k {$g['varetc_path']}:{$keyname}";
823
			if (isset($config['dnsupdate']['usetcp']))
824
				$cmd .= " -v";
825
			$cmd .= " {$g['varetc_path']}/nsupdatecmds";
826
			
827
			mwexec_bg($cmd);
828
		}
829
	}
830
	
831
	return 0;
832
}
833

    
834
function setup_wireless_olsr($interface) {
835
	global $config, $g;
836
	if(isset($config['system']['developerspew'])) {
837
		$mt = microtime();
838
		echo "setup_wireless_olsr($interface) being called $mt\n";
839
	}
840
	$fd = fopen("{$g['varetc_path']}/{$interface}_olsr.conf", "w");
841
	$olsr .= <<<EOD
842
#
843
# olsr.org OLSR daemon config file
844
#
845
# Lines starting with a # are discarded
846
#
847
# This file was shipped with olsrd 0.X.X
848
#
849

    
850
# This file is an example of a typical
851
# configuration for a mostly static
852
# network(regarding mobility) using
853
# the LQ extention
854

    
855
# Debug level(0-9)
856
# If set to 0 the daemon runs in the background
857

    
858
DebugLevel	2
859

    
860
# IP version to use (4 or 6)
861

    
862
IpVersion	4
863

    
864
# Clear the screen each time the internal state changes
865

    
866
ClearScreen     yes
867

    
868
# HNA IPv4 routes
869
# syntax: netaddr netmask
870
# Example Internet gateway:
871
# 0.0.0.0 0.0.0.0
872

    
873
Hna4
874
{
875
#   Internet gateway:
876
#   0.0.0.0      0.0.0.0
877
#   more entries can be added:
878
#   192.168.1.0  255.255.255.0
879
}
880

    
881
# HNA IPv6 routes
882
# syntax: netaddr prefix
883
# Example Internet gateway:
884
Hna6
885
{
886
#   Internet gateway:
887
#   ::              0
888
#   more entries can be added:
889
#   fec0:2200:106:: 48
890
}
891

    
892

    
893
# Should olsrd keep on running even if there are
894
# no interfaces available? This is a good idea
895
# for a PCMCIA/USB hotswap environment.
896
# "yes" OR "no"
897

    
898
AllowNoInt	yes
899

    
900
# TOS(type of service) value for
901
# the IP header of control traffic.
902
# If not set it will default to 16
903

    
904
#TosValue	16
905

    
906
# The fixed willingness to use(0-7)
907
# If not set willingness will be calculated
908
# dynamically based on battery/power status
909
# if such information is available
910

    
911
#Willingness    	4
912

    
913
# Allow processes like the GUI front-end
914
# to connect to the daemon.
915

    
916
IpcConnect
917
{
918
     # Determines how many simultaneously
919
     # IPC connections that will be allowed
920
     # Setting this to 0 disables IPC
921

    
922
     MaxConnections  0
923

    
924
     # By default only 127.0.0.1 is allowed
925
     # to connect. Here allowed hosts can
926
     # be added
927

    
928
     Host            127.0.0.1
929
     #Host            10.0.0.5
930

    
931
     # You can also specify entire net-ranges 
932
     # that are allowed to connect. Multiple
933
     # entries are allowed
934

    
935
     #Net             192.168.1.0 255.255.255.0     
936
}
937

    
938
# Wether to use hysteresis or not
939
# Hysteresis adds more robustness to the
940
# link sensing but delays neighbor registration.
941
# Used by default. 'yes' or 'no'
942

    
943
UseHysteresis	no
944

    
945
# Hysteresis parameters
946
# Do not alter these unless you know 
947
# what you are doing!
948
# Set to auto by default. Allowed
949
# values are floating point values
950
# in the interval 0,1
951
# THR_LOW must always be lower than
952
# THR_HIGH.
953

    
954
#HystScaling	0.50
955
#HystThrHigh	0.80
956
#HystThrLow	0.30
957

    
958

    
959
# Link quality level
960
# 0 = do not use link quality
961
# 1 = use link quality for MPR selection
962
# 2 = use link quality for MPR selection and routing
963
# Defaults to 0
964

    
965
LinkQualityLevel	2
966

    
967
# Link quality window size
968
# Defaults to 10
969

    
970
LinkQualityWinSize	10
971

    
972
# Polling rate in seconds(float). 
973
# Default value 0.05 sec
974

    
975
Pollrate	0.05
976

    
977

    
978
# TC redundancy
979
# Specifies how much neighbor info should
980
# be sent in TC messages
981
# Possible values are:
982
# 0 - only send MPR selectors
983
# 1 - send MPR selectors and MPRs
984
# 2 - send all neighbors
985
#
986
# defaults to 0
987

    
988
TcRedundancy	2
989

    
990

    
991
#
992
# MPR coverage
993
# Specifies how many MPRs a node should
994
# try select to reach every 2 hop neighbor
995
#
996
# Can be set to any integer >0
997
#
998
# defaults to 1
999

    
1000
MprCoverage	3
1001

    
1002

    
1003
# Olsrd plugins to load
1004
# This must be the absolute path to the file
1005
# or the loader will use the following scheme:
1006
# - Try the paths in the LD_LIBRARY_PATH 
1007
#   environment variable.
1008
# - The list of libraries cached in /etc/ld.so.cache
1009
# - /lib, followed by /usr/lib
1010

    
1011
# Example plugin entry with parameters:
1012

    
1013
#LoadPlugin "olsrd_dyn_gw.so.0.3"
1014
#{
1015
    # Here parameters are set to be sent to the
1016
    # plugin. Theese are on the form "key" "value".
1017
    # Parameters ofcause, differs from plugin to plugin.
1018
    # Consult the documentation of your plugin for details.
1019

    
1020
    # Example: dyn_gw params
1021

    
1022
    # how often to check for Internet connectivity
1023
    # defaults to 5 secs
1024
#   PlParam     "Interval"   "40"
1025
    
1026
    # if one or more IPv4 addresses are given, do a ping on these in
1027
    # descending order to validate that there is not only an entry in
1028
    # routing table, but also a real internet connection. If any of
1029
    # these addresses could be pinged successfully, the test was
1030
    # succesful, i.e. if the ping on the 1st address was successful,the
1031
    # 2nd won't be pinged
1032
#   PlParam     "Ping"       "141.1.1.1"
1033
#   PlParam     "Ping"       "194.25.2.129"
1034
#}
1035

    
1036

    
1037

    
1038
# Interfaces and their rules
1039
# Omitted options will be set to the
1040
# default values. Multiple interfaces
1041
# can be specified in the same block
1042
# and multiple blocks can be set.
1043

    
1044
# !!CHANGE THE INTERFACE LABEL(s) TO MATCH YOUR INTERFACE(s)!!
1045
# (eg. wlan0 or eth1):
1046

    
1047
Interface "{$interface}" 
1048
{
1049

    
1050
    # IPv4 broadcast address to use. The
1051
    # one usefull example would be 255.255.255.255
1052
    # If not defined the broadcastaddress
1053
    # every card is configured with is used
1054

    
1055
    # Ip4Broadcast		255.255.255.255
1056

    
1057
    # IPv6 address scope to use.
1058
    # Must be 'site-local' or 'global'
1059

    
1060
    # Ip6AddrType		site-local
1061

    
1062
    # IPv6 multicast address to use when
1063
    # using site-local addresses.
1064
    # If not defined, ff05::15 is used
1065

    
1066
    # Ip6MulticastSite		ff05::11
1067

    
1068
    # IPv6 multicast address to use when
1069
    # using global addresses
1070
    # If not defined, ff0e::1 is used
1071

    
1072
    # Ip6MulticastGlobal	ff0e::1
1073

    
1074

    
1075
    # Emission intervals.
1076
    # If not defined, RFC proposed values will
1077
    # be used in most cases.
1078

    
1079
    # Hello interval in seconds(float)
1080
    HelloInterval    2.0
1081

    
1082
    # HELLO validity time
1083
    HelloValidityTime	20.0
1084

    
1085
    # TC interval in seconds(float)
1086
    TcInterval        5.0
1087

    
1088
    # TC validity time
1089
    TcValidityTime	30.0
1090

    
1091
    # MID interval in seconds(float)
1092
    MidInterval	5.0
1093

    
1094
    # MID validity time
1095
    MidValidityTime	30.0
1096

    
1097
    # HNA interval in seconds(float)
1098
    HnaInterval	5.0
1099

    
1100
    # HNA validity time
1101
    HnaValidityTime 	30.0
1102

    
1103
    # When multiple links exist between hosts
1104
    # the weight of interface is used to determine
1105
    # the link to use. Normally the weight is
1106
    # automatically calculated by olsrd based
1107
    # on the characteristics of the interface,
1108
    # but here you can specify a fixed value.
1109
    # Olsrd will choose links with the lowest value.
1110

    
1111
    # Weight 0
1112

    
1113

    
1114
}
1115
EOD;
1116
	fwrite($fd, $olsr);
1117
	fclose($fd);
1118
	mwexec_bg("/usr/local/sbin/olsrd -f {$g['varetc_path']}/{$interface}_olsr.conf");
1119
}
1120

    
1121
?>
(15-15/24)