Project

General

Profile

Download (28.6 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

    
35
function services_dhcpd_configure() {
36
	global $config, $g;
37
	if(isset($config['system']['developerspew'])) {
38
		$mt = microtime();
39
		echo "services_dhcpd_configure($if) being called $mt\n";
40
	}
41

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

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

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

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

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

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

    
73
	
74

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

    
83
EOD;
84

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

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

    
143
	$dhcpnum = 0;
144

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

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

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

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

    
157
		$dnscfg = "";
158

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

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

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

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

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

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

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

    
204
EOD;
205

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

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

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

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

    
224
EOD;
225

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

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

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

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

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

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

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

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

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

    
261
	return 0;
262
}
263

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

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

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

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

    
290
        return 0;
291
}
292

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

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

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

    
305
	/* DHCPRelay enabled on any interfaces? */
306
	$dhcrelayenable = false;
307
	if(is_array($dhcrelaycfg)) {
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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
366
	return 0;
367
}
368

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

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

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

    
388
	return 0;
389
}
390

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

    
398
	$dyndnscfg = $config['dyndns'];
399
	$wancfg = $config['interfaces']['wan'];
400

    
401
	if (isset($dyndnscfg['enable'])) {
402

    
403
		if ($g['booting']) {
404
			echo "Starting DynDNS client... ";
405
			if(isset($config['system']['use_old_dyndns'])) {
406
				echo " [Using ez-ipupdate] ";
407
				services_dyndns_configure_old();
408
				return;
409
			}	
410
		} else {
411
			sleep(1);
412
			if(isset($config['system']['use_old_dyndns'])) {
413
				services_dyndns_configure_old();
414
				return;
415
			}			
416
		}
417
		
418
		/* load up the dyndns.class */
419
		require_once("dyndns.class");
420

    
421
		log_error("DynDns: Running updatedns()");
422

    
423
		$dns = new updatedns($dnsService = $config['dyndns']['type'],
424
							 $dnsHost = $config['dyndns']['host'],
425
							 $dnsUser = $config['dyndns']['username'],
426
							 $dnsPass = $config['dyndns']['password'],
427
							 $dnsWilcard = $config['dyndns']['wildcard'],
428
							 $dnsMX = $config['dyndns']['mx']);
429

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

    
434
	return 0;
435
}
436

    
437
function services_dyndns_configure_old() {
438
	global $config, $g;
439
	if(isset($config['system']['developerspew'])) {
440
		$mt = microtime();
441
		echo "services_dyndns_configure_old() being called $mt\n";
442
	}
443

    
444
        /* kill any running ez-ipupdate */
445
        /* ez-ipupdate needs SIGQUIT instead of SIGTERM */
446
        sigkillbypid("{$g['varrun_path']}/ez-ipupdate.pid", "QUIT");
447

    
448
        $dyndnscfg = $config['dyndns'];
449
        $wancfg = $config['interfaces']['wan'];
450

    
451
        if (isset($dyndnscfg['enable'])) {
452

    
453
                if ($g['booting'])
454
                        echo "Starting DynDNS client... ";
455
                else
456
                        sleep(1);
457

    
458
                /* determine WAN interface name */
459
                $wanif = get_real_wan_interface();
460

    
461
                /* write ez-ipupdate.conf */
462
                $fd = fopen("{$g['varetc_path']}/ez-ipupdate.conf", "w");
463
                if (!$fd) {
464
                        printf("Error: cannot open ez-ipupdate.conf in services_dyndns_configure().\n");
465
                        return 1;
466
                }
467

    
468
                $ezipupdateconf = <<<EOD
469
service-type={$dyndnscfg['type']}
470
user={$dyndnscfg['username']}:{$dyndnscfg['password']}
471
host={$dyndnscfg['host']}
472
interface={$wanif}
473
max-interval=2073600
474
pid-file={$g['varrun_path']}/ez-ipupdate.pid
475
cache-file={$g['vardb_path']}/ez-ipupdate.cache
476
execute=/etc/rc.dyndns.storecache
477
daemon
478

    
479
EOD;
480

    
481
                /* enable server[:port]? */
482
                if ($dyndnscfg['server']) {
483
                        if ($dyndnscfg['port'])
484
                                $ezipupdateconf .= "server={$dyndnscfg['server']}:{$dyndnscfg['port']}\n";
485
                        else
486
                                $ezipupdateconf .= "server={$dyndnscfg['server']}\n";
487
                }
488

    
489
                /* enable MX? */
490
                if ($dyndnscfg['mx']) {
491
                        $ezipupdateconf .= "mx={$dyndnscfg['mx']}\n";
492
                }
493

    
494
                /* enable wildcards? */
495
                if (isset($dyndnscfg['wildcard'])) {
496
                        $ezipupdateconf .= "wildcard\n";
497
                }
498

    
499
                fwrite($fd, $ezipupdateconf);
500
                fclose($fd);
501

    
502
                /* if we're booting, copy the cache file from /conf */
503
                if ($g['booting']) {
504
                        if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
505
                                copy("{$g['conf_path']}/ez-ipupdate.cache", "{$g['vardb_path']}/ez-ipupdate.cache");
506
                       }
507
                }
508

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

    
512
                if ($g['booting'])
513
                        echo "done\n";
514
        }
515

    
516
        return 0;
517
}
518

    
519
function services_dnsmasq_configure() {
520
	global $config, $g;
521
	if(isset($config['system']['developerspew'])) {
522
		$mt = microtime();
523
		echo "services_dnsmasq_configure() being called $mt\n";
524
	}
525

    
526
	/* kill any running dnsmasq */
527
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
528

    
529
	if (isset($config['dnsmasq']['enable'])) {
530

    
531
		if ($g['booting'])
532
			echo "Starting DNS forwarder... ";
533
		else
534
			sleep(1);
535

    
536
		/* generate hosts file */
537
		system_hosts_generate();
538

    
539
		$args = "";
540

    
541
		if (isset($config['dnsmasq']['regdhcp'])) {
542

    
543
			$args .= " -l {$g['vardb_path']}/dhcpd.leases" .
544
				" -s {$config['system']['domain']}";
545
		}
546

    
547
                if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
548
                        foreach($config['dnsmasq']['domainoverrides'] as $override) {
549
                                $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
550
                        }
551
                }
552

    
553
		/* run dnsmasq */
554
		mwexec("/usr/local/sbin/dnsmasq {$args}");
555

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

    
560
	if (!$g['booting']) {
561
		services_dhcpd_configure();
562
	}
563

    
564
	return 0;
565
}
566

    
567
function services_snmpd_configure() {
568
	global $config, $g;
569
	if(isset($config['system']['developerspew'])) {
570
		$mt = microtime();
571
		echo "services_snmpd_configure() being called $mt\n";
572
	}
573

    
574
	/* kill any running snmpd */
575
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
576

    
577
	if (isset($config['snmpd']['enable'])) {
578

    
579
		if ($g['booting'])
580
			echo "Starting SNMP daemon... ";
581

    
582
		/* generate snmpd.conf */
583
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
584
		if (!$fd) {
585
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
586
			return 1;
587
		}
588

    
589

    
590
		$snmpdconf = <<<EOD
591
location := "{$config['snmpd']['syslocation']}"
592
contact := "{$config['snmpd']['syscontact']}"
593
read := "{$config['snmpd']['rocommunity']}"
594

    
595
EOD;
596

    
597
/* No docs on what write strings do there for disable for now.
598
		if(isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
599
		    $snmpdconf .= <<<EOD
600
# write string
601
write := "{$config['snmpd']['rwcommunity']}"
602

    
603
EOD;
604
		}
605
*/
606

    
607

    
608
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
609
		    $snmpdconf .= <<<EOD
610
# SNMP Trap support.
611
traphost := {$config['snmpd']['trapserver']}
612
trapport := {$config['snmpd']['trapserverport']}
613
trap := "{$config['snmpd']['trapstring']}"
614

    
615

    
616
EOD;
617
		}
618

    
619

    
620
		$snmpdconf .= <<<EOD
621
system := 1     # pfSense
622
%snmpd
623
begemotSnmpdDebugDumpPdus       = 2
624
begemotSnmpdDebugSyslogPri      = 7
625
begemotSnmpdCommunityString.0.1 = $(read)
626

    
627
EOD;
628

    
629
/* No docs on what write strings do there for disable for now.
630
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
631
		    $snmpdconf .= <<<EOD
632
begemotSnmpdCommunityString.0.2 = $(write)
633

    
634
EOD;
635
		}
636
*/
637

    
638
		
639
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
640
		    $snmpdconf .= <<<EOD
641
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
642
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
643
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
644

    
645
EOD;
646
		}
647

    
648

    
649
		$snmpdconf .= <<<EOD
650
begemotSnmpdCommunityDisable    = 1
651

    
652
EOD;
653

    
654
		if(is_port( $config['snmpd']['pollport'] )) {
655
		    $snmpdconf .= <<<EOD
656
begemotSnmpdPortStatus.0.0.0.0.{$config['snmpd']['pollport']} = 1
657

    
658
EOD;
659

    
660
		}
661

    
662
		$snmpdconf .= <<<EOD
663
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
664
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
665

    
666
# These are bsnmp macros not php vars.
667
sysContact      = $(contact)
668
sysLocation     = $(location)
669
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
670

    
671
snmpEnableAuthenTraps = 2
672

    
673
EOD;
674

    
675
		if (is_array( $config['snmpd']['modules'] )) {
676
		    if(isset($config['snmpd']['modules']['mibii'])) {
677
			$snmpdconf .= <<<EOD
678
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
679

    
680
EOD;
681
		    }
682

    
683
		    if(isset($config['snmpd']['modules']['netgraph'])) {
684
			$snmpdconf .= <<<EOD
685
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
686
%netgraph
687
begemotNgControlNodeName = "snmpd"
688

    
689
EOD;
690
		    }
691

    
692
		    if(isset($config['snmpd']['modules']['pf'])) {
693
			$snmpdconf .= <<<EOD
694
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
695
# config must end with blank line
696

    
697

    
698
EOD;
699
		    }
700
		}
701

    
702
		fwrite($fd, $snmpdconf);
703
		fclose($fd);
704

    
705
		/* run bsnmpd */
706
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
707
			" -p {$g['varrun_path']}/snmpd.pid");
708

    
709
		if ($g['booting'])
710
			echo "done.\n";
711
	}
712

    
713
	return 0;
714
}
715

    
716
function services_proxyarp_configure() {
717
	global $config, $g;
718
	if(isset($config['system']['developerspew'])) {
719
		$mt = microtime();
720
		echo "services_proxyarp_configure() being called $mt\n";
721
	}
722

    
723
	/* kill any running choparp */
724
	killbyname("choparp");
725

    
726
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
727
		$paa = array();
728

    
729
		/* group by interface */
730
		foreach ($config['virtualip']['vip'] as $vipent) {
731
			if ($vipent['mode'] === "proxyarp") {
732
				if ($vipent['interface'])
733
					$if = $vipent['interface'];
734
				else
735
					$if = "wan";
736

    
737
				if (!is_array($paa[$if]))
738
					$paa[$if] = array();
739

    
740
				$paa[$if][] = $vipent;
741
			}
742
		}
743

    
744
		if (count($paa))
745
		foreach ($paa as $paif => $paents) {
746
			if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) ||
747
                                       ($config['interfaces']['wan']['ipaddr'] == "dhcp") ||
748
                                       ($config['interfaces']['wan']['ipaddr'] == "bigpond")))
749
                               continue;
750

    
751
			$args = $config['interfaces'][$paif]['if'] . " auto";
752

    
753
			foreach ($paents as $paent) {
754

    
755
				if (isset($paent['subnet']))
756
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
757
				else if (isset($paent['range']))
758
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" .
759
						$paent['range']['to']);
760
			}
761

    
762
			mwexec_bg("/usr/local/sbin/choparp " . $args);
763
		}
764
	}
765
}
766

    
767
function services_dnsupdate_process() {
768
	global $config, $g;
769
	if(isset($config['system']['developerspew'])) {
770
		$mt = microtime();
771
		echo "services_dnsupdate_process() being called $mt\n";
772
	}
773

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

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

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

    
857
# This file is an example of a typical
858
# configuration for a mostly static
859
# network(regarding mobility) using
860
# the LQ extention
861

    
862
# Debug level(0-9)
863
# If set to 0 the daemon runs in the background
864

    
865
DebugLevel	2
866

    
867
# IP version to use (4 or 6)
868

    
869
IpVersion	4
870

    
871
# Clear the screen each time the internal state changes
872

    
873
ClearScreen     yes
874

    
875
# HNA IPv4 routes
876
# syntax: netaddr netmask
877
# Example Internet gateway:
878
# 0.0.0.0 0.0.0.0
879

    
880
Hna4
881
{
882
#   Internet gateway:
883
#   0.0.0.0      0.0.0.0
884
#   more entries can be added:
885
#   192.168.1.0  255.255.255.0
886
}
887

    
888
# HNA IPv6 routes
889
# syntax: netaddr prefix
890
# Example Internet gateway:
891
Hna6
892
{
893
#   Internet gateway:
894
#   ::              0
895
#   more entries can be added:
896
#   fec0:2200:106:: 48
897
}
898

    
899

    
900
# Should olsrd keep on running even if there are
901
# no interfaces available? This is a good idea
902
# for a PCMCIA/USB hotswap environment.
903
# "yes" OR "no"
904

    
905
AllowNoInt	yes
906

    
907
# TOS(type of service) value for
908
# the IP header of control traffic.
909
# If not set it will default to 16
910

    
911
#TosValue	16
912

    
913
# The fixed willingness to use(0-7)
914
# If not set willingness will be calculated
915
# dynamically based on battery/power status
916
# if such information is available
917

    
918
#Willingness    	4
919

    
920
# Allow processes like the GUI front-end
921
# to connect to the daemon.
922

    
923
IpcConnect
924
{
925
     # Determines how many simultaneously
926
     # IPC connections that will be allowed
927
     # Setting this to 0 disables IPC
928

    
929
     MaxConnections  0
930

    
931
     # By default only 127.0.0.1 is allowed
932
     # to connect. Here allowed hosts can
933
     # be added
934

    
935
     Host            127.0.0.1
936
     #Host            10.0.0.5
937

    
938
     # You can also specify entire net-ranges 
939
     # that are allowed to connect. Multiple
940
     # entries are allowed
941

    
942
     #Net             192.168.1.0 255.255.255.0     
943
}
944

    
945
# Wether to use hysteresis or not
946
# Hysteresis adds more robustness to the
947
# link sensing but delays neighbor registration.
948
# Used by default. 'yes' or 'no'
949

    
950
UseHysteresis	no
951

    
952
# Hysteresis parameters
953
# Do not alter these unless you know 
954
# what you are doing!
955
# Set to auto by default. Allowed
956
# values are floating point values
957
# in the interval 0,1
958
# THR_LOW must always be lower than
959
# THR_HIGH.
960

    
961
#HystScaling	0.50
962
#HystThrHigh	0.80
963
#HystThrLow	0.30
964

    
965

    
966
# Link quality level
967
# 0 = do not use link quality
968
# 1 = use link quality for MPR selection
969
# 2 = use link quality for MPR selection and routing
970
# Defaults to 0
971

    
972
LinkQualityLevel	2
973

    
974
# Link quality window size
975
# Defaults to 10
976

    
977
LinkQualityWinSize	10
978

    
979
# Polling rate in seconds(float). 
980
# Default value 0.05 sec
981

    
982
Pollrate	0.05
983

    
984

    
985
# TC redundancy
986
# Specifies how much neighbor info should
987
# be sent in TC messages
988
# Possible values are:
989
# 0 - only send MPR selectors
990
# 1 - send MPR selectors and MPRs
991
# 2 - send all neighbors
992
#
993
# defaults to 0
994

    
995
TcRedundancy	2
996

    
997

    
998
#
999
# MPR coverage
1000
# Specifies how many MPRs a node should
1001
# try select to reach every 2 hop neighbor
1002
#
1003
# Can be set to any integer >0
1004
#
1005
# defaults to 1
1006

    
1007
MprCoverage	3
1008

    
1009

    
1010
# Olsrd plugins to load
1011
# This must be the absolute path to the file
1012
# or the loader will use the following scheme:
1013
# - Try the paths in the LD_LIBRARY_PATH 
1014
#   environment variable.
1015
# - The list of libraries cached in /etc/ld.so.cache
1016
# - /lib, followed by /usr/lib
1017

    
1018
# Example plugin entry with parameters:
1019

    
1020
#LoadPlugin "olsrd_dyn_gw.so.0.3"
1021
#{
1022
    # Here parameters are set to be sent to the
1023
    # plugin. Theese are on the form "key" "value".
1024
    # Parameters ofcause, differs from plugin to plugin.
1025
    # Consult the documentation of your plugin for details.
1026

    
1027
    # Example: dyn_gw params
1028

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

    
1043

    
1044

    
1045
# Interfaces and their rules
1046
# Omitted options will be set to the
1047
# default values. Multiple interfaces
1048
# can be specified in the same block
1049
# and multiple blocks can be set.
1050

    
1051
# !!CHANGE THE INTERFACE LABEL(s) TO MATCH YOUR INTERFACE(s)!!
1052
# (eg. wlan0 or eth1):
1053

    
1054
Interface "{$interface}" 
1055
{
1056

    
1057
    # IPv4 broadcast address to use. The
1058
    # one usefull example would be 255.255.255.255
1059
    # If not defined the broadcastaddress
1060
    # every card is configured with is used
1061

    
1062
    # Ip4Broadcast		255.255.255.255
1063

    
1064
    # IPv6 address scope to use.
1065
    # Must be 'site-local' or 'global'
1066

    
1067
    # Ip6AddrType		site-local
1068

    
1069
    # IPv6 multicast address to use when
1070
    # using site-local addresses.
1071
    # If not defined, ff05::15 is used
1072

    
1073
    # Ip6MulticastSite		ff05::11
1074

    
1075
    # IPv6 multicast address to use when
1076
    # using global addresses
1077
    # If not defined, ff0e::1 is used
1078

    
1079
    # Ip6MulticastGlobal	ff0e::1
1080

    
1081

    
1082
    # Emission intervals.
1083
    # If not defined, RFC proposed values will
1084
    # be used in most cases.
1085

    
1086
    # Hello interval in seconds(float)
1087
    HelloInterval    2.0
1088

    
1089
    # HELLO validity time
1090
    HelloValidityTime	20.0
1091

    
1092
    # TC interval in seconds(float)
1093
    TcInterval        5.0
1094

    
1095
    # TC validity time
1096
    TcValidityTime	30.0
1097

    
1098
    # MID interval in seconds(float)
1099
    MidInterval	5.0
1100

    
1101
    # MID validity time
1102
    MidValidityTime	30.0
1103

    
1104
    # HNA interval in seconds(float)
1105
    HnaInterval	5.0
1106

    
1107
    # HNA validity time
1108
    HnaValidityTime 	30.0
1109

    
1110
    # When multiple links exist between hosts
1111
    # the weight of interface is used to determine
1112
    # the link to use. Normally the weight is
1113
    # automatically calculated by olsrd based
1114
    # on the characteristics of the interface,
1115
    # but here you can specify a fixed value.
1116
    # Olsrd will choose links with the lowest value.
1117

    
1118
    # Weight 0
1119

    
1120

    
1121
}
1122
EOD;
1123
	fwrite($fd, $olsr);
1124
	fclose($fd);
1125
	mwexec_bg("/usr/local/sbin/olsrd -f {$g['varetc_path']}/{$interface}_olsr.conf");
1126
}
1127

    
1128
?>
(17-17/26)