home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / perl5 / DebianNet.pm next >
Encoding:
Perl POD Document  |  2006-11-17  |  7.9 KB  |  256 lines

  1. # DebianNet.pm: a perl module to add entries to the /etc/inetd.conf file
  2. #
  3. # Copyright (C) 1995, 1996 Peter Tobias <tobias@et-inf.fho-emden.de>
  4. #                          Ian Jackson <iwj10@cus.cam.ac.uk>
  5. #
  6. #
  7. # DebianNet::add_service($newentry, $group);
  8. # DebianNet::disable_service($service, $pattern);
  9. # DebianNet::enable_service($service, $pattern);
  10. # DebianNet::remove_service($entry);
  11. #
  12.  
  13. package DebianNet;
  14.  
  15. require 5.000;
  16.  
  17. use Debconf::Client::ConfModule ':all';
  18.  
  19. $inetdcf="/etc/inetd.conf";
  20. $sep = "#<off># ";
  21. $version = "1.11";
  22.  
  23. sub add_service {
  24.     local($newentry, $group) = @_;
  25.     local($service, $searchentry, @inetd, $inetdconf, $found, $success);
  26.     unless (defined($newentry)) { return(-1) };
  27.     chomp($newentry); chomp($group);
  28.     $group = "OTHER" unless (defined($group));
  29.     $group =~ tr/a-z/A-Z/;
  30.     $newentry =~ s/\\t/\t/g;
  31.     ($service = $newentry) =~ s/(\W*\w+)\s+.*/$1/;
  32.     ($sservice = $service) =~ s/^#([A-Za-z].*)/$1/;
  33.     ($searchentry = $newentry) =~ s/^$sep//;
  34.     $searchentry =~ s/^#([A-Za-z].*)/$1/;
  35.  
  36.     # strip parameter from entry (e.g. -s /tftpboot)
  37.     # example:          service dgram udp     wait    root    /tcpd /prg   -s /tftpboot";
  38.     $searchentry =~ s/^(\w\S+\W+\w+\W+\w\S+\W+\w\S+\W+\w\S+\W+\S+\W+\S+).*/$1/;
  39.     $searchentry =~ s/[ \t]+/ /g;
  40.     $searchentry =~ s/ /\\s+/g;
  41.     $searchentry =~ s@\\s\+/\S+\\s\+/\S+@\\s\+\\S\+\\s\+\\S\+@g;
  42.  
  43.     if (open(INETDCONF,"$inetdcf")) {
  44.         @inetd=<INETDCONF>;
  45.         close(INETDCONF);
  46.         if (grep(m/^$sep$sservice\s+/,@inetd)) {
  47.             &enable_service($sservice);
  48.         } else {
  49.             if (grep(m/^$sservice\s+/,@inetd)) {
  50.                 if (grep(m/^$sservice\s+/,@inetd) > 1) {
  51.             set("update-inetd/ask-several-entries", "yes");
  52.             fset("update-inetd/ask-several-entries", "seen", "false");
  53.             subst("update-inetd/ask-several-entries", "sservice", "$sservice");
  54.             subst("update-inetd/ask-several-entries", "inetdcf", "$inetdcf");
  55.             input("high", "update-inetd/ask-several-entries");
  56.             @ret = go();
  57.             if ($ret[0] == 0) {
  58.                 @ret = get("update-inetd/ask-several-entries");
  59.             exit(1) if ($ret[1] !~ m/y/i);
  60.             }
  61.                 } elsif (!grep(m:^#?.*$searchentry.*:, @inetd)) {
  62.             set("update-inetd/ask-entry-present", "yes");
  63.             fset("update-inetd/ask-entry-present", "seen", "false");
  64.             subst("update-inetd/ask-entry-present", "newentry", "$newentry");
  65.             subst("update-inetd/ask-entry-present", "sservice", "$sservice");
  66.             subst("update-inetd/ask-entry-present", "inetdcf", "$inetdcf");
  67.             my $lookslike = (grep(m/^$sservice\s+/,@inetd))[0];
  68.             $lookslike =~ s/\n//g;
  69.             subst("update-inetd/ask-entry-present", "lookslike", "$lookslike");
  70.             input("high", "update-inetd/ask-entry-present");
  71.             @ret = go();
  72.             if ($ret[0] == 0) {
  73.                 @ret = get("update-inetd/ask-entry-present");
  74.             exit(1) if ($ret[1] !~ m/y/i);
  75.             }
  76.                 }
  77.             } elsif (grep(m/^#\s*$sservice\s+/, @inetd) >= 1 or
  78.               (($service =~ s/^#//) and grep(m/^$service\s+/, @inetd)>=1)) {
  79.                 &printv("Processing service \`$service' ... not changed\n");
  80.             } else {
  81.                 &printv("Processing service \`$sservice' ... added\n");
  82.                 $inetdconf=1;
  83.             }
  84.         }
  85.         if ($inetdconf) {
  86.             open(ICWRITE, ">$inetdcf.new") || die "Error creating new $inetdcf: $!\n";
  87.             open(ICREAD, "$inetdcf");
  88.             while(<ICREAD>) {
  89.                 chomp;
  90.                 if (/^#:$group:/) {
  91.                     $found = 1;
  92.                 };
  93.                 if ($found and !(/[a-zA-Z#]/)) {
  94.                     print (ICWRITE "$newentry\n") || die "Error writing new $inetdcf: $!\n";
  95.                     $found = 0;
  96.                     $success = 1;
  97.                 }
  98.                 print ICWRITE "$_\n";
  99.             }
  100.             close(ICREAD);
  101.             unless ($success) {
  102.                 print (ICWRITE "$newentry\n") || die "Error writing new $inetdcf: $!\n";
  103.             }
  104.             close(ICWRITE) || die "Error closing new inetd.conf: $!\n";
  105.  
  106.             rename("$inetdcf.new","$inetdcf") ||
  107.                 die "Error installing new $inetdcf: $!\n";
  108.             chmod(0644, "$inetdcf");
  109.  
  110.             &wakeup_inetd;
  111.         }
  112.     }
  113.  
  114.     return(1);
  115. }
  116.  
  117. sub remove_service {
  118.     my($service) = @_;
  119.     unless(defined($service)) { return(-1) };
  120.     chomp($service);
  121.     if($service eq "") {
  122.          print STDERR "DebianNet::remove_service called with empty argument\n";
  123.          return(-1);
  124.     }
  125.  
  126.     if ((&scan_entries("$service") > 1) and (not defined($multi))) {
  127.     set("update-inetd/ask-remove-entries", "no");
  128.     fset("update-inetd/ask-remove-entries", "seen", "false");
  129.         subst("update-inetd/ask-remove-entries", "service", "$service");
  130.     subst("update-inetd/ask-remove-entries", "inetdcf", "$inetdcf");
  131.     input("high", "update-inetd/ask-remove-entries");
  132.     @ret = go();
  133.     if ($ret[0] == 0) {
  134.         @ret = get("update-inetd/ask-remove-entries");
  135.         return(1) if ($ret[1] =~ /^[^y]/i);
  136.         }
  137.     }
  138.  
  139.     open(ICWRITE, ">$inetdcf.new") || die "Error creating $inetdcf.new";
  140.     open(ICREAD, "$inetdcf");
  141.     RLOOP: while(<ICREAD>) {
  142.         chomp;
  143.         unless (/^$service\b/) {
  144.             print ICWRITE "$_\n";
  145.         } else {
  146.             &printv("Removing line: \`$_'\n");
  147.         }
  148.     }
  149.     close(ICREAD);
  150.     close(ICWRITE);
  151.  
  152.     rename("$inetdcf.new", "$inetdcf") ||
  153.         die "Error installing new $inetdcf: $!\n";
  154.     chmod(0644, "$inetdcf");
  155.  
  156.     &wakeup_inetd;
  157.     return(1);
  158. }
  159.  
  160. sub disable_service {
  161.     my($service, $pattern) = @_;
  162.     unless (defined($service)) { return(-1) };
  163.     chomp($service);
  164.  
  165.     if ((&scan_entries("$service", $pattern) > 1) and (not defined($multi))) {
  166.     set("update-inetd/ask-disable-entries", "no");
  167.     fset("update-inetd/ask-disable-entries", "seen", "false");
  168.         subst("update-inetd/ask-disable-entries", "service", "$service");
  169.     subst("update-inetd/ask-disable-entries", "inetdcf", "$inetdcf");
  170.     input("high", "update-inetd/ask-disable-entries");
  171.     @ret = go();
  172.     if ($ret[0] == 0) {
  173.         @ret = get("update-inetd/ask-disable-entries");
  174.         return(1) if ($ret[1] =~ /^[^y]/i);
  175.         }
  176.     }
  177.  
  178.     open(ICWRITE, ">$inetdcf.new") || die "Error creating new $inetdcf: $!\n";
  179.     open(ICREAD, "$inetdcf");
  180.     DLOOP: while(<ICREAD>) {
  181.       chomp;
  182.       if (/^$service\s+\w+\s+/ and /$pattern/) {
  183.           &printv("Processing service \`$service' ... disabled\n");
  184.           $_ =~ s/^(.+)$/$sep$1/;
  185.       }
  186.       print ICWRITE "$_\n";
  187.     }
  188.     close(ICREAD);
  189.     close(ICWRITE) || die "Error closing new inetd.conf: $!\n";
  190.  
  191.     rename("$inetdcf.new","$inetdcf") ||
  192.         die "Error installing new $inetdcf: $!\n";
  193.     chmod(0644, "$inetdcf");
  194.  
  195.     &wakeup_inetd;
  196.     return(1);
  197. }
  198.  
  199. sub enable_service {
  200.     my($service, $pattern) = @_;
  201.     unless (defined($service)) { return(-1) };
  202.     chomp($service);
  203.     open(ICWRITE, ">$inetdcf.new") || die "Error creating new $inetdcf: $!\n";
  204.     open(ICREAD, "$inetdcf");
  205.     while(<ICREAD>) {
  206.       chomp;
  207.       if (/^$sep$service\s+\w+\s+/ and /$pattern/) {
  208.           &printv("Processing service \`$service' ... enabled\n");
  209.           $_ =~ s/^$sep//;
  210.       }
  211.       print ICWRITE "$_\n";
  212.     }
  213.     close(ICREAD);
  214.     close(ICWRITE) || die "Error closing new inetd.conf: $!\n";
  215.  
  216.     rename("$inetdcf.new","$inetdcf") ||
  217.         die "Error installing new $inetdcf: $!\n";
  218.     chmod(0644, "$inetdcf");
  219.  
  220.     &wakeup_inetd;
  221.     return(1);
  222. }
  223.  
  224. sub wakeup_inetd {
  225.     my($pid);
  226.     if (open(P,"/var/run/inetd.pid")) {
  227.         $pid=<P>;
  228.         if (open(C,sprintf("/proc/%d/stat",$pid))) {
  229.             $_=<C>;
  230.             if (m/^\d+ \(inetd\)/) { kill(1,$pid); }
  231.             close(C);
  232.         }
  233.         close(P);
  234.     }
  235.     return(1);
  236. }
  237.  
  238. sub scan_entries {
  239.     my ($service, $pattern) = @_;
  240.     my $counter = 0;
  241.  
  242.     open(ICREAD, "$inetdcf");
  243.     SLOOP: while (<ICREAD>) {
  244.         $counter++ if (/^$service\b/ and /$pattern/);
  245.     }
  246.     close(ICREAD);
  247.     return($counter);
  248. }
  249.  
  250. sub printv {
  251.     print STDERR @_ if (defined($verbose));
  252. }
  253.  
  254. 1;
  255.  
  256.