[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-dhcp/sources/scripts/ -> makedhcpdconf (source)

   1  #!/usr/bin/perl
   2  # This script makedhcpdconf
   3  #
   4  # $Id: makedhcpdconf 8377 2015-01-10 23:17:08Z keyser $ 
   5  
   6  
   7  
   8  ################################
   9  # INIT                    #
  10  ################################
  11  
  12  use Sys::Syslog qw(:DEFAULT setlogsock);
  13  # use strict;
  14  use DBI;
  15  require '/etc/SeConfig.ph';
  16  
  17  my $connexion_db = DBI->connect("DBI:mysql:$connexionDb@$mysqlServerIp", $mysqlServerUsername, $mysqlServerPw);
  18  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_iface';");
  19  $requete->execute();
  20  my @row = $requete->fetchrow_array();
  21  my $IFACE=$row[0];
  22  $requete->finish;
  23  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_on_boot';");
  24  $requete->execute();
  25  my @row = $requete->fetchrow_array();
  26  my $ONBOOT=$row[0];
  27  $requete->finish;
  28  my $requete = $connexion_db->prepare("SELECT restrictions.valeur FROM corresp,restrictions where corresp.chemin like '%AutoConfigURL%' and corresp.cleID=restrictions.cleID and restrictions.groupe='base';");
  29  $requete->execute();
  30  my @row = $requete->fetchrow_array();
  31  my $WPAD=$row[0];
  32  
  33  my $action = $ARGV[0];
  34  ########################################
  35  ##### If ARG is "status" ###############
  36  if ($action eq "state") {
  37      system ("/etc/init.d/isc-dhcp-server status > /dev/null");
  38      if ( $? == 0  )
  39      {print "1";} else {print "0
  40    ";};
  41      exit 0;
  42  }
  43  ########################################
  44  ##### If ARG is "stop" ###############
  45  if ($action eq "stop") {
  46      `/etc/init.d/isc-dhcp-server stop`;
  47      exit 0;
  48  }
  49  
  50  ########################################
  51  ##### STATE on BOOT ###############
  52  if ($ONBOOT eq "1") {
  53      `insserv -d isc-dhcp-server`;
  54  }
  55  else {
  56      `insserv -r -d isc-dhcp-server`;
  57  }
  58  
  59  ######################################
  60  # # # Decimal to Binary   #######
  61  sub DecToBin {
  62  my $DEC= $_[0];
  63  my $BITS = $_[1];
  64  my $i;
  65  my @BITCHARS;
  66  my $BIN;
  67  for( $i = ($BITS-1) ; $i >= 0 ; $i -- ) {
  68  $BITCHARS[$i] = $DEC % 2;
  69  $DEC = $DEC / 2;
  70  }
  71  $BIN = join( "", @BITCHARS );
  72  $BIN;
  73  }
  74  ########################################
  75  # Subroutine: IpMaskToLan( Ip, Mask )return LAN IP ------
  76  # # # Caclul LAN IP with @IP and LAN Mask.................
  77  sub IpMaskToLan {
  78            my @ip = (split /\./ , $_[0]);
  79          my @mask = (split /\./ , $_[1]);
  80          my $i = 0;
  81          my $j = 0;
  82          my @IPSTREAM;
  83          my @MASKSTREAM;
  84          my @RESO;
  85          my @nouvip;
  86          my @nouvdecimal;
  87          for ($i=0; $i<=3; $i++)
  88            {
  89            @{$IPSTREAM[$i]}= split // ,DecToBin( $ip[$i], 8);
  90            @{$MASKSTREAM[$i]} = split // ,DecToBin( $mask[$i], 8);
  91            }
  92          for ($i=0; $i<=3; $i++)
  93                  {
  94                  for ($j=0; $j<=7; $j++)
  95                          {
  96                          $RESO[$i][$j]=$IPSTREAM[$i][$j] && $MASKSTREAM[$i][$j];
  97                          }
  98                  }
  99          for ($i=0; $i<=3; $i++)
 100                  {
 101                  $nouvip[$i]= join('',$RESO[$i][0],$RESO[$i][1],$RESO[$i][2],$RESO[$i][3],$RESO[$i][4],$RESO[$i][5],$RESO[$i][6],$RESO[$i][7]);
 102                  $nouvdecimal[$i]= ord(pack('B8', $nouvip[$i]));
 103                  }
 104  
 105          return join('.',$nouvdecimal[0],$nouvdecimal[1],$nouvdecimal[2],$nouvdecimal[3]);
 106          }
 107  ##        /  Sub function end           ##
 108  ###########################################
 109  
 110  if (! -e "/var/run/rsyslogd.pid") {
 111      system("/usr/sbin/service rsyslog start");
 112      }
 113  
 114  # Open syslog########################
 115  setlogsock('unix');
 116  openlog("SE3-DHCP",'pid');
 117  ####################################################
 118  ########  First : make /etc/default/isc-dhcp-server for interface listening for dhcpd 
 119  ########                                                         #######
 120  
 121  syslog('INFO','Erasing /etc/default/isc-dhcp-server with SE3 config :  listening '.$IFACE);
 122  open (FILE,">/etc/default/isc-dhcp-server");
 123  print FILE "INTERFACES=\"$IFACE\"\n";
 124  close FILE;
 125  
 126  
 127  
 128  #########################
 129  ####  LOAD OPTIONS FOR dhcpd.conf
 130  #####  GATEWAY  
 131  
 132  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_gateway';");
 133  $requete->execute();
 134  my @row = $requete->fetchrow_array();
 135  my $GATEWAY=$row[0];
 136  $requete->finish;
 137  
 138  
 139  #####   DNS server 
 140  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_dns_server_prim';");
 141  $requete->execute();
 142  my @row = $requete->fetchrow_array();
 143  my $SERVEUR_DNS_PRIMARY=$row[0];
 144  $requete->finish;
 145  
 146  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_dns_server_sec';");
 147  $requete->execute();
 148  my @row = $requete->fetchrow_array();
 149  my $SERVEUR_DNS_SECONDARY=$row[0];
 150  $requete->finish;
 151  
 152  #####   wins server
 153  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_wins';");
 154  $requete->execute();
 155  my @row = $requete->fetchrow_array();
 156  my $SERVEUR_WINS=$row[0];
 157  $requete->finish;
 158  
 159  
 160  #####   NTP server  
 161  
 162  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_ntp';");
 163  $requete->execute();
 164  my @row = $requete->fetchrow_array();
 165  my $SERVEUR_NTP=$row[0];
 166  $requete->finish;
 167  
 168  
 169  #####   NORMAL_BAIL 
 170  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_default_lease';");
 171  $requete->execute();
 172  my @row = $requete->fetchrow_array();
 173  my $NORMAL_BAIL=$row[0];
 174  $requete->finish;
 175  
 176  #####   MAX_BAIL 
 177  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_max_lease';");
 178  $requete->execute();
 179  my @row = $requete->fetchrow_array();
 180  my $MAX_BAIL=$row[0];
 181  $requete->finish;
 182  
 183  #####  BEGIN_RANGE  
 184  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_begin_range';");
 185  $requete->execute();
 186  my @row = $requete->fetchrow_array();
 187  my $BEGIN_RANGE=$row[0];
 188  $requete->finish;
 189  
 190  ##### END_RANGE 
 191  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_end_range';");
 192  $requete->execute();
 193  my @row = $requete->fetchrow_array();
 194  my $END_RANGE=$row[0];
 195  $requete->finish;
 196  
 197  ##### EXTRA_OPTION 
 198  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_extra_option';");
 199  $requete->execute();
 200  my @row = $requete->fetchrow_array();
 201  my $EXTRA_OPTION=$row[0];
 202  $requete->finish;
 203  
 204  # DOMAIN NAME from "se3_dhcp" table ########
 205  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_domain_name';");
 206  $requete->execute();
 207  my @row = $requete->fetchrow_array();
 208  my $DOMAIN_NAME=$row[0];
 209  $requete->finish;
 210  
 211  #########      RECUPERE IP / MASK / NETWORK on internal device #######################
 212  my $NETWORK_INTERNAL_IP = `/sbin/ifconfig $IFACE |/bin/grep inet' ' |/usr/bin/cut -d\: -f2 |/usr/bin/cut -d\ ' ' -f1`;
 213  my $NETMASK_INTERNAL = `/sbin/ifconfig $IFACE |/bin/grep inet' ' |/usr/bin/cut -d\: -f4 |/usr/bin/cut -d\ ' '  -f1`;
 214  chomp $NETWORK_INTERNAL_IP ;
 215  chomp $NETMASK_INTERNAL;
 216  my $INTERNAL_NETWORK=&IpMaskToLan ($NETWORK_INTERNAL_IP,$NETMASK_INTERNAL);
 217  
 218  # FTP SERVER  ########
 219  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_tftp_server';");
 220  $requete->execute();
 221  my @row = $requete->fetchrow_array();
 222  my $tftp_server=$row[0];
 223  $requete->finish;
 224  # UNATTENDED ########
 225  #my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_unatt_server';");
 226  #$requete->execute();
 227  #my @row = $requete->fetchrow_array();
 228  #my $unatt_server=$row[0];
 229  #$requete->finish;
 230  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_unatt_login';");
 231  $requete->execute();
 232  my @row = $requete->fetchrow_array();
 233  my $unatt_login=$row[0];
 234  $requete->finish;
 235  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_unatt_pass';");
 236  $requete->execute();
 237  my @row = $requete->fetchrow_array();
 238  my $unatt_pass=$row[0];
 239  $requete->finish;
 240  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_unatt_filename';");
 241  $requete->execute();
 242  my @row = $requete->fetchrow_array();
 243  my $unatt_filename=$row[0];
 244  $requete->finish;
 245  
 246  
 247  # VLAN  ########
 248  my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='dhcp_vlan';");
 249  $requete->execute();
 250  my @row = $requete->fetchrow_array();
 251  my $VLAN=$row[0];
 252  $requete->finish;
 253  
 254  
 255  ##################################################################
 256  ###########  Make dhcp.conf ############################################
 257  
 258  syslog('INFO','WAIT : Making /etc/dhcp3/dhcp.conf SE3 config  who  listening '.$IFACE);
 259  open (FILE,">/etc/dhcp/dhcpd.conf");
 260  print FILE "################################################################################\n";
 261  print FILE "# This File is automagically created by SE3 interface\n\n";
 262  print FILE "##       GENERAL OPTIONS          ##############################################\n";
 263  print FILE "allow booting;\n";
 264  print FILE "allow bootp;\n";
 265  print FILE "authoritative;\n";
 266  
 267  if (! $DOMAIN_NAME=="")
 268  {
 269      print FILE "option domain-name \"$DOMAIN_NAME\";  \n";
 270  }
 271  if (! $SERVEUR_DNS_PRIMARY=="")
 272  {
 273      if (! $SERVEUR_DNS_SECONDARY=="")
 274      {
 275      print FILE "option domain-name-servers $SERVEUR_DNS_PRIMARY,$SERVEUR_DNS_SECONDARY;\n";
 276      }
 277      else 
 278      {
 279      print FILE "option domain-name-servers $SERVEUR_DNS_PRIMARY;\n";
 280      }
 281  }
 282  
 283  
 284  if (! $MAX_BAIL =="")
 285  {
 286      print FILE "max-lease-time $MAX_BAIL;\n";
 287  }
 288  
 289  if (! $WPAD == "")
 290  {
 291      print FILE "option wpad-url code 252 = string;\n";
 292      #print FILE "option wpad-url \"$WPAD\\n\";\n";
 293      print FILE "option wpad-url \"$WPAD\";\n";
 294  }
 295  
 296  if (! $NORMAL_BAIL =="")
 297  {
 298      print FILE "default-lease-time $NORMAL_BAIL;\n";
 299  }
 300  
 301  if (! $SERVEUR_WINS =="")
 302  {
 303      print FILE "option netbios-name-servers $SERVEUR_WINS;\n";
 304  }
 305  
 306  if (! $tftp_server =="")
 307  {
 308      print FILE "next-server  $tftp_server;\n";
 309     
 310  }
 311  if (! $unatt_filename =="")
 312  {
 313      print FILE "filename \"$unatt_filename\";\n";
 314  }
 315  
 316  
 317  
 318  if (! $unatt_login =="")
 319  {
 320      
 321      print FILE "option unattended-options code 233 = string;\n";
 322      print FILE "option unattended-options \"z_user=$unatt_login z_pass=$unatt_pass z_path=//$tftp_server/install\";\n";
 323  }
 324  if (! $EXTRA_OPTION =="")
 325  {
 326      
 327      print FILE "include \"$EXTRA_OPTION\";\n";
 328  }
 329  
 330  if ($VLAN > "0")
 331  {
 332      my $i=1;
 333      print FILE "\n\n";
 334      while ($i <= $VLAN)
 335      {
 336          my $GATEWAY="";
 337          my $RESEAU="";
 338          my $MASQUE="";
 339          my $BEGIN_RANGE="";
 340          my $END_RANGE="";
 341          my $EXTRA_OPTION="";
 342          
 343          # Subnet vlan   ########
 344          my $dhcp_reseau="dhcp_reseau_".$i;
 345          my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='$dhcp_reseau';");
 346          $requete->execute();
 347          my @row = $requete->fetchrow_array();
 348          my $RESEAU=$row[0];
 349          $requete->finish;
 350  
 351  
 352          # Masque vlan   ########
 353          my $dhcp_masque="dhcp_masque_".$i;
 354          my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='$dhcp_masque';");
 355          $requete->execute();
 356          my @row = $requete->fetchrow_array();
 357          my $MASQUE=$row[0];
 358          $requete->finish;
 359  
 360          
 361          # Range begin  vlan   ########
 362          my $dhcp_begin_range="dhcp_begin_range_".$i;
 363          my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='$dhcp_begin_range';");
 364          $requete->execute();
 365          my @row = $requete->fetchrow_array();
 366          my $BEGIN_RANGE=$row[0];
 367          $requete->finish;
 368  
 369          
 370          # Range end  vlan   ########
 371          my $dhcp_end_range="dhcp_end_range_".$i;
 372          my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='$dhcp_end_range';");
 373          $requete->execute();
 374          my @row = $requete->fetchrow_array();
 375          my $END_RANGE=$row[0];
 376          $requete->finish;
 377  
 378  
 379          # Gateway  vlan   ########
 380          my $dhcp_gateway="dhcp_gateway_".$i;
 381          my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='$dhcp_gateway';");
 382          $requete->execute();
 383          my @row = $requete->fetchrow_array();
 384          my $GATEWAY=$row[0];
 385          $requete->finish;
 386  
 387          # Extra option  vlan   ########
 388          my $dhcp_extra_option="dhcp_extra_option_".$i;
 389          my $requete = $connexion_db->prepare("SELECT value FROM params WHERE name='$dhcp_extra_option';");
 390          $requete->execute();
 391          my @row = $requete->fetchrow_array();
 392          my $EXTRA_OPTION=$row[0];
 393          $requete->finish;
 394  
 395  
 396          if (! (($BEGIN_RANGE=="") || ($END_RANGE=="") || ($RESEAU=="") || ($MASQUE=="") || ($GATEWAY=="")))
 397          {
 398              print FILE "\n";
 399              print FILE "#####  SUBNETS DECLARATION #########\n";
 400              print FILE "subnet $RESEAU netmask $MASQUE {\n";
 401              print FILE "    range $BEGIN_RANGE $END_RANGE;\n";
 402              print FILE "    option routers $GATEWAY;\n";
 403              if (! ($EXTRA_OPTION=="")) {
 404                      print FILE "    include \"$EXTRA_OPTION\";\n";
 405              }
 406              print FILE "}\n";
 407          }
 408  
 409          $i++;
 410      }    
 411  } else { 
 412  
 413      if (! $GATEWAY=="")
 414      {
 415          print FILE "option routers $GATEWAY;\n";
 416      }
 417      
 418      print FILE "\n\n";
 419  
 420      if (! (($BEGIN_RANGE=="") || ($END_RANGE=="")))
 421      {
 422          print FILE "#####  SUBNETS DECLARATION #########\n";
 423          print FILE "subnet $INTERNAL_NETWORK netmask $NETMASK_INTERNAL {\n";
 424          print FILE "range $BEGIN_RANGE $END_RANGE;\n";
 425          print FILE "}\n";
 426      }
 427  }
 428  
 429  
 430  print FILE "\n\n";
 431  
 432  #####################################################################
 433  ###  HOSTS enregistrés dans la base  #############################################
 434  #
 435  print FILE "################################################################################\n";
 436  print FILE "# HOSTS recorded on the base\n\n";
 437  
 438  my $requete = $connexion_db->prepare("SELECT name,ip,mac FROM se3_dhcp ORDER BY ip;");
 439  $requete->execute();
 440  
 441  
 442  
 443  while (@result = $requete->fetchrow_array())
 444  {
 445      print FILE "\n";
 446      print FILE "\n";
 447      
 448      print FILE "host $result[0]\n";
 449      print FILE "{\n";
 450      print FILE "\thardware ethernet $result[2] ;\n";
 451      print FILE "\tfixed-address $result[1] ;\n";
 452      print FILE "}\n";
 453  }
 454  
 455  close FILE;
 456  
 457  print "restart dhcpd-server\n";
 458  system("/etc/init.d/isc-dhcp-server restart");
 459  
 460  
 461  $proxy = `grep \"http_proxy=\" /etc/profile | cut -d\\\" -f2 | cut -d/ -f3`;
 462  chomp($proxy);
 463  if ($proxy) {
 464      open (FILE,">/var/www/se3.pac");
 465      print FILE "function FindProxyForURL(url, host)\n";
 466      print FILE "{\n";
 467      print FILE "\tif (isPlainHostName(host) ||\n";
 468      print FILE "\tdnsDomainIs(host, \"\.$DOMAIN_NAME\") ||\n";
 469      print FILE "\tisInNet(host, \"$INTERNAL_NETWORK\", \"$NETMASK_INTERNAL\") ||\n";
 470      print FILE "\tisInNet(host, \"127.0.0.0\", \"255.0.0.0\") ) {\n";
 471      print FILE "\t\treturn  \"DIRECT\";\n";
 472      print FILE "\t}\n";
 473      print FILE "\telse {\n";
 474      print FILE "\t\treturn \"PROXY $proxy; DIRECT\";\n";
 475      print FILE "\t}\n";
 476      print FILE "}\n";
 477      close FILE;
 478      
 479      if (!-e "/var/www/wpad.dat") {
 480      system("ln -s /var/www/se3.pac /var/www/wpad.dat");
 481      }
 482  }


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1