📄 atatftp.pl
字号:
my $pid = getppid(); print LOCKFILE "$pid";}sub unlock(){ # LOCK_UN is 8 flock LOCKFILE, 8; unlink $lockfile;}# checks a string to see if it is an ATA mac addresssub is_mac_address{ my $candidate = shift; # check that the first 3 characters are "ata" my $ata_identifier = substr $candidate, 0, 3; if ($ata_identifier ne "ata") { print STDERR "wrong ata identifier: ${ata_identifier}\n"; return 0; } # at this point I have confidence that it is an ATA mac address # strip off the first 3 characters my $mac_address_string = substr $candidate, 3; my @chars = split //, $mac_address_string; # there should be 6 octets (12 digits) in a mac address my $num_chars = scalar(@chars); if ($num_chars != 12) { print STDERR "wrong number of characters: $num_chars\n"; return 0; } return 0 unless $mac_address_string =~ /[0-9|a-f|A-F]{12}/; # everything checked out return 1;}# checks nlist to see if a given number is in itsub is_in_provisioning{ my $userid = shift; return 0 unless $userid; my $item; # If the nlist is null, then userid is not in it return 0 unless @nlist; foreach $item (@nlist) { if ($item eq $userid) { return 1; } } return 0;}#argument should be a string like "Content-Length: 200"sub get_content_length{ my $line = shift; my ($content_length_string, $content_length) = split(/\s+/, $line); return $content_length;}# Note that connection to pserver must already# be established.sub send_to_pserver{ my ($line, $content_length, $status, $content, $junk); my $pserver_command = shift; my $domain = shift; my $filename = shift; my $output = ${pserver_command} . " " . ${domain} . " " . ${filename} . "\n"; if ($pserver_command eq "PUT") { my $data = shift; my $content_length = length($data) + 1; $output .= "Content-Length: ${content_length}\n"; $output .= "${data}\n"; } print SOCK "${output}";# should be 200 OK or 400 FAILED $line = <SOCK>; die "no response from pserver" unless $line; ($status) = split(/\s+/, $line); $pserver_result[0] = $status; $line = <SOCK>; ($junk, $content_length) = split(/\s+/, $line); if ($content_length == 0) { # set array-length to 1 $#pserver_result=0; return; } my $bytes_read = read(SOCK, $content, $content_length); die "read wrong number of bytes: ${bytes_read}\n" unless $bytes_read == $content_length; $pserver_result[1] = $content; return;}# used for parsing XML from Global Configurationsub start_global_element{ my $xp = shift; my $elem = shift; # the autoProvisionATA element looks like # <autoProvisionATA startRange="1000" endRange="2000"/> if ($elem eq "autoProvisionATA") { my $attr1 = shift; my $val1 = shift; my $attr2 = shift; my $val2 = shift; $low_range = $val1 if $val1; $high_range = $val2 if $val2; die "Bad userid range: ${high_range} must be greater than ${low_range}" unless ${high_range} > ${low_range}; }}# used for parsing XML from ListOfMarshalServerssub start_marshals_element{ my $xp = shift; my $elem = shift; # the serverGroup element looks like # <serverGroup value="UserAgentGroup"> # <server host="ipaddress" port="5060"/> # </serverGroup> # but it is theoretically possible for the user's provisioning # to specify a different group, so check for that. if ($elem eq "serverGroup") { my $attr1 = shift; my $val1 = shift; if ($val1 eq $marshalgroup) { $inside_server_group = 1; } else { $inside_server_group = 0; } } elsif (($elem eq "server") && $inside_server_group) { # we always take the first server in the group. my $hostAttr = shift; my $hostValue = shift; my $portAttr = shift; my $portValue = shift; $marshal_address = $hostValue if $hostValue; $inside_server_group = 0; }}sub account_char_handler{ my $p = shift; my $data = shift; if ($p->current_element eq "marshalgroup") { $marshalgroup = $data; }}# Establish initial connection to Pserver# Requires password authenticationsub get_pserver_connection{ my ($port, $iaddr, $paddr, $proto, $line, $result); my ($status, $content_length); $port = 6005; $iaddr = inet_aton("localhost"); $paddr = pack_sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCK, $paddr) or die "connect: $!"; select (SOCK); $|=1; select STDOUT;# need to get the password for this. my $password; if (-f "../../etc/vocal.secret.in") { $password = `cat ../../etc/vocal.secret.in`; } else { $password = "hello"; } print SOCK "AUTH vocal:${password} VPP/1.1\n"; # should be 200 OK $line = <SOCK>; ($status) = split(/\s+/, $line); die "Connection not authorized\n" unless $status == 200;# should be Content_Length: 0 $line = <SOCK>; $content_length = &get_content_length($line); if ($content_length) { die "Unexpected Content-Length from Auth: ${content_length}\n"; }}# Send the XML strings to Pserver that make up user datasub create_new_user{ my $userid = shift; die "attempting to create blank user" unless $userid;######################################################### Calling and Called Contact list######################################################## send_to_pserver ("PUT", "Contact_Lists_Calling", ${userid}, "<callingContactList/\>"); die "Pserver failed to PUT ${userid} Calling Contact List" unless $pserver_result[0] == 200; send_to_pserver ("PUT", "Contact_Lists_Called", ${userid}, "<calledContactList/\>"); die "Pserver failed to PUT ${userid} Called Contact List" unless $pserver_result[0] == 200;######################################################### default xml string to create new user######################################################## $default_provisioning = <<EOF;<user><name>${userid}</name><password>6f7d5634f65192a3b9c8479cee3b655</password><ipaddr>0.0.0.0</ipaddr><marshalgroup>${marshalgroup}</marshalgroup><staticRegistration value="false"><terminatingContactList><contact value="UserAgentGroup"></contact><contact value="000.000.000.000:0000"></contact></terminatingContactList></staticRegistration><authenticationType value="None"><authenticationPassword value="password"></authenticationPassword></authenticationType><failurecase>unknown</failurecase><cfa enabled="false"><setfeat>OFF</setfeat><featuregroup>ForwardAllCallsGroup</featuregroup><forwardto>none</forwardto></cfa><fnab enabled="false"><setfeat>OFF</setfeat><featuregroup>ForwardNoAnswerBusyGroup</featuregroup><fna set="false"><forwardto>none</forwardto></fna><cfb set="false"><forwardto>none</forwardto></cfb></fnab><cs enabled="false"><setfeat>OFF</setfeat><featuregroup>CallScreeningGroup</featuregroup></cs><clbl enabled="false"><setfeat>OFF</setfeat><featuregroup>CallBlockingGroup</featuregroup><nineHundredBlocked adminSet="false" userSet="false"></nineHundredBlocked><longDistanceBlocked adminSet="false" userSet="false"></longDistanceBlocked></clbl><callReturn enabled="false"><setfeat>OFF</setfeat><featuregroup>CallReturnGroup</featuregroup></callReturn><callerIdBlocking enabled="false"><setfeat>OFF</setfeat><featuregroup>CallerIdBlockingGroup</featuregroup></callerIdBlocking><voicemail enabled="false"><setfeat>OFF</setfeat><featuregroup>VoiceMailGroup</featuregroup></voicemail><userGroup>ATA</userGroup><aliases></aliases></user>EOF######################################################## send_to_pserver ("PUT", "Accounts", ${userid}, $default_provisioning); die "Pserver failed to PUT ${userid} Account" unless $pserver_result[0] == 200;}sub set_up_tftp_params{ open ATATEXT, ">$temptext" or die "failed to open ${temptext} for writing"; open PARAMS, $template2 or die "failed to open ${template2} for reading"; #set up marshal_address send_to_pserver("GET", "SystemConfiguration", "ListOfMarshalServers"); die "Pserver failed to return list of marshal servers" if $pserver_result[0] == 400; die "Pserver returned empty list of marshal servers result" if (scalar @pserver_result) < 2; my $list_of_marshal_servers = $pserver_result[1]; my $p = new XML::Parser(ErrorContext => 2, Handlers => {Start => \&start_marshals_element } ); $p->parse($list_of_marshal_servers); my $hostname = `hostname -i`; chomp $hostname; while(<PARAMS>) { if (/^upgradecode:/) { s/{TFTP_HOST}/$hostname/; print ATATEXT; } elsif (/^cfgInterval:/) { # Note that random interval does not work because the # ATA will schedule an immediate update every time # cfgInterval changes, so if cfgInterval is randomized # it will loop endlessly. # One solution would be to assign a random number once # and store it in Provisioning. # make cfgInterval be a random number between 3000 and 4000 # my $random_interval = int(rand 1000) + 3000; # print ATATEXT "cfgInterval:${random_interval}\n"; print ATATEXT "cfgInterval:3600\n"; } elsif (/^UID0:/) { print STDERR "setting UID0 to ${userid}\n"; print ATATEXT "UID0:${userid}\n"; } elsif (/^SipOutBoundProxy:/) { print STDERR "setting SipOutBoundProxy to ${marshal_address}\n"; print ATATEXT "SipOutBoundProxy:${marshal_address}\n"; } elsif (/^GkOrProxy:/) { print STDERR "setting GkOrProxy to ${marshal_address}\n"; print ATATEXT "GkOrProxy:${marshal_address}\n"; } else { print ATATEXT; } } close ATATEXT;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -