#!/usr/bin/perl # $Source: /srv/cvs/asterisk/cgi-bin/snom,v $ # $Id: snom,v 1.9 2009-06-18 18:51:39 cnw Exp $ # Copyright 2008 Conrad Wood # script to provision SNOM phones # put this into apaches cgi-bin # configure DHCP like so: #option subnet-mask 255.255.255.0; #default-lease-time 3600; #max-lease-time 7200; #next-server 192.168.110.1; #option ntp-servers 192.168.110.1; #subnet 192.168.110.0 netmask 255.255.255.0 { # range 192.168.110.10 192.168.110.200; # option broadcast-address 192.168.110.255; # option routers 192.168.110.4; # next-server 192.168.110.1; # option tftp-server-name "http://192.168.110.1"; # filename "cgi-bin/snom?event=boot&mac={mac}"; # option bootfile-name "cgi-bin/snom?event=boot&mac={mac}"; #} # put your main cfg file in /etc/snom/default.cfg # and one file for each phone in /etc/snom/[mac] with the phone-specific parameters # point your browser at http://[server]/cgi-bin/snom # to edit the snom phone entries use strict; use CGI; my $DEFAULTFILE="/etc/snom/default.cfg"; my $form = new CGI(); my $mac = $form->param("mac"); my $event = $form->param("event"); unless(defined($event)) { $event="boot"; } print "Content-Type: text/html\n\n"; if ($event eq "boot") { doBoot(); } elsif ($event eq "edit") { doEdit(); } elsif ($event eq "registerfailure") { my $ip = $ENV{REMOTE_ADDR}; system("mail","-s","Register failure on $ip","cnw\@conradwood.net"); } exit 0; sub doEdit { if ( (!defined($mac) ) || (length($mac) == 0)) { print "
"; askMac(); print "\n"; return; } my $cmac = cleanMac($mac); unless (defined($cmac)) { print "Invalid mac: $mac (12 characters, only a-f and 0-9 allowed)\n"; print ""; askMac(); print "\n"; return; } my $filename = "/etc/snom/$cmac.cfg"; my $edit_username=$form->param("edit_username"); my $edit_extension=$form->param("edit_extension"); if (defined($edit_username)) { my $fmac = findExtension($edit_extension); if ( (defined($fmac)) && ( $fmac ne $cmac) ) { print "Error. extension $edit_extension is already allocated to mac address $fmac| MAC | extension | username |
\n#comment\n";
if ( (!defined($mac) ) || (length($mac) == 0)) {
fatal_error("Mac undefined");
}
my $peer = mac_settings($mac);
unless ( defined($peer) ) {
fatal_error("no peer for mac $mac");
}
my $default = readHash($DEFAULTFILE);
foreach my $name (keys(%{$peer})) {
$default->{$name} = $peer->{$name};
}
foreach my $name (sort(keys(%{$default}))) {
print "$name: " . $default->{$name} . "\n";
}
print "\n\n";
}
# given a mac will return the specific settings
sub mac_settings {
my $mac = shift;
$mac = uc($mac);
my $filename = "/etc/snom/$mac.cfg";
return readHash("$filename");
}
sub readHash {
my $filename = shift;
open FH,"<$filename" or fatal_error("Missing file $filename: $!");
my @lines =