📄 atatftp.pl
字号:
#!/usr/bin/perl -wrequire 5.002;# This script takes a request from a TFTP server to provision an ATA.# It gets the provisioning data and converts it to a form usable by# the ATA, then returns the data to the TFTP server.use strict;use sigtrap;use IO::Socket;use CGI;use XML::Parser;use Socket;# =======================================================================# The Vovida Software License, Version 1.0 # Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.# # Redistribution and use in source and binary forms, with or without# modification, are permitted provided that the following conditions# are met:# # 1. Redistributions of source code must retain the above copyright# notice, this list of conditions and the following disclaimer.# # 2. Redistributions in binary form must reproduce the above copyright# notice, this list of conditions and the following disclaimer in# the documentation and/or other materials provided with the# distribution.# # 3. The names "VOCAL", "Vovida Open Communication Application Library",# and "Vovida Open Communication Application Library (VOCAL)" must# not be used to endorse or promote products derived from this# software without prior written permission. For written# permission, please contact vocal@vovida.org.# # 4. Products derived from this software may not be called "VOCAL", nor# may "VOCAL" appear in their name, without prior written# permission of Vovida Networks, Inc.# # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND# NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA# NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES# IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH# DAMAGE.# # ====================================================================# # This software consists of voluntary contributions made by Vovida# Networks, Inc. and many individuals on behalf of Vovida Networks,# Inc. For more information on Vovida Networks, Inc., please see# <http://www.vovida.org/>.# # # All third party licenses and copyright notices and other required# legends also need to be complied with as well.# # ====================================================================# uncomment for debugging my $debug_is_on = 1;#my $debug_is_on = 0;# logfile my $templog = "/tmp/.atalog";# tempfile for writing text provisioning datamy $temptext = "/tmp/.atatxt";# tempfile for writing binary provisioning datamy $tempbin = "/tmp/.atabin";# template for changing OpFlags in first passmy $template1 = "../../etc/ataOpFlags.txt";# template for setting provisioning data in second passmy $template2 = "../../etc/ataParams.txt";# lock filemy $lockfile = "/tmp/.atatftp.lock";# conversion program for ATAmy $cfgfmt = "../../bin/cfgfmt";# data file used by cfgfmtmy $ptag = "../../bin/ptag.dat";# string to set up new usermy $default_provisioning;# name of MAC address tablemy $mac_table_file = "../../var/mac_addresses";# low end of range of MAC addresses (get from pserver)my $low_range = 1000;;# high end of range of MAC addresses (get from pserver)my $high_range = 2000;;# MAC address coming from tftp server querymy $path_info;# userid assigned to above MAC addressmy $userid;# CGI objectmy $tftp_query = new CGI;# Array of 2 values for returning last pserver result.# First value is the status (ie 200)# Second value is content, if there is any.my @pserver_result;# global provisioning data (from pserver)my @global_provisioning;# hash of values to write out in tftp responsemy %tftp_hash;# name of the User Agent Marshal groupmy $marshalgroup = "UserAgentGroup";# address of marshal --> Outbound Proxy# defaults to the current host ip.my $marshal_address = `hostname -i`;# make chomp get trailing spaces and newlines$/ = " \n";chomp $marshal_address;# set variable back so socket read will work$/ = "\n";# used for parsing ListOfMarshalServers xmlmy $inside_server_group = 0;#################################################### BEGIN#################################################### Only one process may use this script at a time.# We use a lock file to enforce this.&lock();close STDERR;if (-f ${templog}){ open STDERR, ">>${templog}";}else{ open STDERR, ">${templog}";}if ($#ARGV > -1){ $path_info = $ARGV[0];}else{ $path_info = $tftp_query->path_info();}#remove initial /$path_info =~ s/^\/*//;die "blank MAC address given" unless $path_info;# decide whether the path_info is a zup requestif($path_info =~ /.zup/){ print STDERR "got a zup request\n"; open ZUP, "../../etc/${path_info}" or die "failed to open ${path_info}"; print "Content-type: tftp\n\n"; print while <ZUP>; close ZUP; exit(0);}# decide whether the path_info is a mac addresselsif (is_mac_address($path_info)){# continue}# this must be some sort of bootstrap request# try to get the ATA to send something elseelse{ print STDERR "got a bootstrap request: ${path_info}\n"; open ATATEXT, ">$temptext" or die "failed to open ${temptext} for writing"; open CHANGE_OP_FLAGS, $template1 or die "failed to open ${template1} for reading"; my $hostname = `hostname -i`; chomp $hostname; while(<CHANGE_OP_FLAGS>) { print ATATEXT; } close ATATEXT; close CHANGE_OP_FLAGS; &send_text_cfg(); &unlock(); exit(0);}print STDERR "MAC address is ${path_info}\n";# we will respond with the provisioning data, once we have it.# Connect to pserver&get_pserver_connection();# get Global Provisioning Datasend_to_pserver("GET", "SystemConfiguration", "GlobalConfigData");die "Pserver failed to return global provisioning" if $pserver_result[0] == 400;die "Pserver returned empty global provisioning result" if (scalar @pserver_result) < 2;my $global_provisioning = $pserver_result[1];# use the XML Parser to parse the response from pservermy $p = new XML::Parser(ErrorContext => 2, Handlers => {Start => \&start_global_element } );$p->parse($global_provisioning);# open the data file and search for MAC address.# if the MAC address exists, get provisioning data from pserver# otherwise, create a new user and write it to pserver# convert the data to ATA format and return in to tftp servermy %mac_hash;if (-f $mac_table_file){ open MACTABLE, $mac_table_file; while (<MACTABLE>) { my ($mac, $id) = split; if ($mac && $id) { $mac_hash{$mac} = $id; } } close MACTABLE;}# get the list of userids currently in Provisioning# the list will be put into pserver_result[1]send_to_pserver("NLIST", "Accounts", "Accounts");chomp $pserver_result[1];# sort alphabetically into an arraymy @nlist = split(/,/, $pserver_result[1]);$userid = $mac_hash{$path_info};if ($userid){ # this user already exists -- make sure it is still in Provisioning. if (&is_in_provisioning($userid)) { # parse the Account XML to get the Group name for User Agent Marshal send_to_pserver("GET", "Accounts", $userid); die "Pserver failed to return user provisioning for ${userid}" if $pserver_result[0] == 400; die "Pserver returned empty user provisioning for ${userid}" if (scalar @pserver_result) < 2; my $user_provisioning = $pserver_result[1]; my $p = new XML::Parser(ErrorContext => 2, Handlers => {Char => \&account_char_handler } ); $p->parse($user_provisioning); } else { &create_new_user($userid); }}else{ # this user does not exist -- make a new one. # use one greater than the last one seen. my @mac_values = values %mac_hash; #check to see if there are any mac addresses if (scalar @mac_values > 0) { #sort them in reverse order my @userids = sort {$b cmp $a} (@mac_values); my $highest_userid = $userids[0]; $userid = $highest_userid + 1; } else { $userid = $low_range; } # if userid is out of range but it already exists in the file # then use it anyway. # make sure this user does not already exist in Provisioning # if it does, increment userid and try again # we only use $attempts if we need to loop back to low end of range. my $attempts = -1; while(&is_in_provisioning($userid)) { if ($userid++ >= $high_range) { #start from the beginning in case there are any holes to fill. $userid = $low_range; $attempts = 0; } if ($attempts > -1) { if ($attempts++ >= ($high_range - $low_range)) { die "No userid available in given range"; } } } &create_new_user($userid);# Append the new MAC address to the table of MAC addressesif (-f $mac_table_file){ # append to existing file open MACTABLE, ">>$mac_table_file" or die "failed to open $mac_table_file for writing";}else{ # create new file open MACTABLE, ">$mac_table_file" or die "failed to open $mac_table_file for writing";} print MACTABLE "${path_info} ${userid}\n"; close MACTABLE;}&set_up_tftp_params();&send_text_cfg();&unlock();################ END OF MAIN ################sub send_text_cfg{ die "ptag.dat is missing" unless -f ${ptag}; my $cfg_result = system("${cfgfmt} -t${ptag} ${temptext} ${tempbin}"); die "failed to format configuration data for ata" if $cfg_result < 0; open ATABIN, "$tempbin" or die "failed to open ${tempbin} for reading"; print "Content-type: tftp\n\n"; print while <ATABIN>; close ATABIN; if (!$debug_is_on) { unlink ${temptext}; unlink ${templog}; } unlink ${tempbin};}sub lock(){ # LOCK_EX is 2 open LOCKFILE, ">$lockfile" or die "unable to get the lock"; flock LOCKFILE, 2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -