home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / DebianNet.pm < prev    next >
Encoding:
Perl POD Document  |  2011-09-18  |  12.7 KB  |  359 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.6.1;
  16.  
  17. use Debconf::Client::ConfModule ':all';
  18. use File::Temp qw/ tempfile /;
  19. use File::Copy qw/ move /;
  20.  
  21. $inetdcf="/etc/inetd.conf";
  22. $sep = "#<off># ";
  23. $version = "1.12";
  24. $called_wakeup_inetd = 0;
  25.  
  26. sub add_service {
  27.     local($newentry, $group) = @_;
  28.     local($service, $searchentry, @inetd, $inetdconf, $found, $success);
  29.     unless (defined($newentry)) { return(-1) };
  30.     chomp($newentry);
  31.     if (defined $group) {
  32.         chomp($group);
  33.     } else {
  34.         $group = "OTHER";
  35.     }
  36.     $group =~ tr/a-z/A-Z/;
  37.     $newentry =~ s/\\t/\t/g;
  38.     ($service = $newentry) =~ s/(\W*\w+)\s+.*/$1/;
  39.     ($sservice = $service) =~ s/^#([A-Za-z].*)/$1/;
  40.     ($searchentry = $newentry) =~ s/^$sep//;
  41.     $searchentry =~ s/^#([A-Za-z].*)/$1/;
  42.  
  43.     # strip parameter from entry (e.g. -s /tftpboot)
  44.     # example:          service dgram udp     wait    root    /tcpd /prg   -s /tftpboot";
  45.     $searchentry =~ s/^(\w\S+\W+\w+\W+\w\S+\W+\w\S+\W+\w\S+\W+\S+\W+\S+).*/$1/;
  46.     $searchentry =~ s/[ \t]+/ /g;
  47.     $searchentry =~ s/ /\\s+/g;
  48.     $searchentry =~ s@\\s\+/\S+\\s\+/\S+@\\s\+\\S\+\\s\+\\S\+@g;
  49.  
  50.     if (open(INETDCONF,"$inetdcf")) {
  51.         @inetd=<INETDCONF>;
  52.         close(INETDCONF);
  53.         if (grep(m/^$sep$sservice\s+/,@inetd)) {
  54.             &enable_service($sservice);
  55.         } else {
  56.             if (grep(m/^$sservice\s+/,@inetd)) {
  57.                 if (grep(m/^$sservice\s+/,@inetd) > 1) {
  58.                     set("update-inetd/ask-several-entries", "true");
  59.                     fset("update-inetd/ask-several-entries", "seen", "false");
  60.                     settitle("update-inetd/title");
  61.                     subst("update-inetd/ask-several-entries", "service", "$sservice");
  62.                     subst("update-inetd/ask-several-entries", "sservice", "$sservice");
  63.                     subst("update-inetd/ask-several-entries", "inetdcf", "$inetdcf");
  64.                     input("high", "update-inetd/ask-several-entries");
  65.                     @ret = go();
  66.                     if ($ret[0] == 0) {
  67.                         @ret = get("update-inetd/ask-several-entries");
  68.                         exit(1) if ($ret[1] !~ m/true/i);
  69.                     }
  70.                 } elsif (!grep(m:^#?.*$searchentry.*:, @inetd)) {
  71.                     set("update-inetd/ask-entry-present", "true");
  72.                     fset("update-inetd/ask-entry-present", "seen", "false");
  73.                     settitle("update-inetd/title");
  74.                     subst("update-inetd/ask-entry-present", "service", "$sservice");
  75.                     subst("update-inetd/ask-entry-present", "newentry", "$newentry");
  76.                     subst("update-inetd/ask-entry-present", "sservice", "$sservice");
  77.                     subst("update-inetd/ask-entry-present", "inetdcf", "$inetdcf");
  78.                     my $lookslike = (grep(m/^$sservice\s+/,@inetd))[0];
  79.                     $lookslike =~ s/\n//g;
  80.                     subst("update-inetd/ask-entry-present", "lookslike", "$lookslike");
  81.                     input("high", "update-inetd/ask-entry-present");
  82.                     @ret = go();
  83.                     if ($ret[0] == 0) {
  84.                         @ret = get("update-inetd/ask-entry-present");
  85.                         exit(1) if ($ret[1] !~ m/true/i);
  86.                     }
  87.                 }
  88.             } elsif (grep(m/^#\s*$sservice\s+/, @inetd) >= 1 or
  89.               (($service =~ s/^#//) and grep(m/^$service\s+/, @inetd)>=1)) {
  90.                 print STDERR "Processing service \`$service' ... not enabled"
  91.                       . " (entry is commented out by user)\n";
  92.             } else {
  93.                 &printv("Processing service \`$sservice' ... added\n");
  94.                 $inetdconf=1;
  95.             }
  96.         }
  97.         if ($inetdconf) {
  98.             my $init_svc_count = &scan_entries();
  99.             &printv("Number of currently enabled services: $init_svc_count\n");
  100.             my ($ICWRITE, $new_inetdcf) = tempfile("/tmp/inetdcfXXXXX", UNLINK => 0);
  101.             unless (defined($ICWRITE)) { die "Error creating temporary file: $!\n" }
  102.             &printv("Using tempfile $new_inetdcf\n");
  103.             open(ICREAD, "$inetdcf");
  104.             while(<ICREAD>) {
  105.                 chomp;
  106.                 if (/^#:$group:/) {
  107.                     $found = 1;
  108.                 };
  109.                 if ($found and !(/[a-zA-Z#]/)) {
  110.                     print ($ICWRITE "$newentry\n")
  111.                         || die "Error writing to $new_inetdcf: $!\n";
  112.                     $found = 0;
  113.                     $success = 1;
  114.                 }
  115.                 print $ICWRITE "$_\n";
  116.             }
  117.             close(ICREAD);
  118.             unless ($success) {
  119.                 print ($ICWRITE "$newentry\n")
  120.                     || die "Error writing to $new_inetdcf: $!\n";
  121.                 $success = 1;
  122.             }
  123.             close($ICWRITE) || die "Error closing $new_inetdcf: $!\n";
  124.  
  125.             if ($success) {
  126.                 move("$new_inetdcf","$inetdcf") ||
  127.                     die "Error installing $new_inetdcf to $inetdcf: $!\n";
  128.                 chmod(0644, "$inetdcf");
  129.                 &wakeup_inetd(0,$init_svc_count);
  130.                 &printv("New service(s) added\n");
  131.             } else {
  132.                 &printv("No service(s) added\n");
  133.                 unlink("$new_inetdcf")
  134.                     || die "Error removing $new_inetdcf: $!\n";
  135.             }
  136.         } else {
  137.             &printv("No service(s) added\n");
  138.         }
  139.     }
  140.  
  141.     return(1);
  142. }
  143.  
  144. sub remove_service {
  145.     my($service) = @_;
  146.     unless(defined($service)) { return(-1) };
  147.     chomp($service);
  148.     my $nlines_removed = 0;
  149.     if($service eq "") {
  150.          print STDERR "DebianNet::remove_service called with empty argument\n";
  151.          return(-1);
  152.     }
  153.  
  154.     if (((&scan_entries("$service") > 1) or (&scan_entries("$sep$service") > 1))
  155.         and (not defined($multi))) {
  156.         set("update-inetd/ask-remove-entries", "false");
  157.         fset("update-inetd/ask-remove-entries", "seen", "false");
  158.             settitle("update-inetd/title");
  159.         subst("update-inetd/ask-remove-entries", "service", "$service");
  160.         subst("update-inetd/ask-remove-entries", "inetdcf", "$inetdcf");
  161.         input("high", "update-inetd/ask-remove-entries");
  162.         @ret = go();
  163.         if ($ret[0] == 0) {
  164.             @ret = get("update-inetd/ask-remove-entries");
  165.             return(1) if ($ret[1] =~ /false/i);
  166.         }
  167.     }
  168.  
  169.     my ($ICWRITE, $new_inetdcf) = tempfile("/tmp/inetdcfXXXXX", UNLINK => 0);
  170.     unless (defined($ICWRITE)) { die "Error creating temporary file: $!\n" }
  171.     &printv("Using tempfile $new_inetdcf\n");
  172.     open(ICREAD, "$inetdcf");
  173.     RLOOP: while(<ICREAD>) {
  174.         chomp;
  175.         unless (/^$service\s+/ or /^$sep$service\s+/) {
  176.             print $ICWRITE "$_\n";
  177.         } else {
  178.             &printv("Removing line: \`$_'\n");
  179.             $nlines_removed += 1;
  180.         }
  181.     }
  182.     close(ICREAD);
  183.     close($ICWRITE);
  184.  
  185.     if ($nlines_removed > 0) {
  186.         move("$new_inetdcf", "$inetdcf") ||
  187.             die "Error installing $new_inetdcf to $inetdcf: $!\n";
  188.         chmod(0644, "$inetdcf");
  189.         wakeup_inetd(1);
  190.         &printv("Number of service entries removed: $nlines_removed\n");
  191.     } else {
  192.         &printv("No service entries were removed\n");
  193.         unlink("$new_inetdcf") || die "Error removing $new_inetdcf: $!\n";
  194.     }
  195.  
  196.     return(1);
  197. }
  198.  
  199. sub disable_service {
  200.     my($service, $pattern) = @_;
  201.     unless (defined($service)) { return(-1) };
  202.     unless (defined($pattern)) { $pattern = ''; }
  203.     chomp($service);
  204.     my $nlines_disabled = 0;
  205.  
  206.     if ((&scan_entries("$service", $pattern) > 1) and (not defined($multi))) {
  207.         set("update-inetd/ask-disable-entries", "false");
  208.         fset("update-inetd/ask-disable-entries", "seen", "false");
  209.             settitle("update-inetd/title");
  210.         subst("update-inetd/ask-disable-entries", "service", "$service");
  211.         subst("update-inetd/ask-disable-entries", "inetdcf", "$inetdcf");
  212.         input("high", "update-inetd/ask-disable-entries");
  213.         @ret = go();
  214.         if ($ret[0] == 0) {
  215.             @ret = get("update-inetd/ask-disable-entries");
  216.             return(1) if ($ret[1] =~ /false/i);
  217.         }
  218.     }
  219.  
  220.     my ($ICWRITE, $new_inetdcf) = tempfile("/tmp/inetdcfXXXXX", UNLINK => 0);
  221.     unless (defined($ICWRITE)) { die "Error creating temporary file: $!\n" }
  222.     &printv("Using tempfile $new_inetdcf\n");
  223.     open(ICREAD, "$inetdcf");
  224.     DLOOP: while(<ICREAD>) {
  225.       chomp;
  226.       if (/^$service\s+\w+\s+/ and /$pattern/) {
  227.           &printv("Processing service \`$service' ... disabled\n");
  228.           $_ =~ s/^(.+)$/$sep$1/;
  229.           $nlines_disabled += 1;
  230.       }
  231.       print $ICWRITE "$_\n";
  232.     }
  233.     close(ICREAD);
  234.     close($ICWRITE) || die "Error closing $new_inetdcf: $!\n";
  235.  
  236.     if ($nlines_disabled > 0) {
  237.         move("$new_inetdcf","$inetdcf") ||
  238.             die "Error installing new $inetdcf: $!\n";
  239.         chmod(0644, "$inetdcf");
  240.         wakeup_inetd(1);
  241.         &printv("Number of service entries disabled: $nlines_disabled\n");
  242.     } else {
  243.         &printv("No service entries were disabled\n");
  244.         unlink("$new_inetdcf") || die "Error removing $new_inetdcf: $!\n";
  245.     }
  246.  
  247.     return(1);
  248. }
  249.  
  250. sub enable_service {
  251.     my($service, $pattern) = @_;
  252.     unless (defined($service)) { return(-1) };
  253.     unless (defined($pattern)) { $pattern = ''; }
  254.     my $init_svc_count = &scan_entries();
  255.     my $nlines_enabled = 0;
  256.     chomp($service);
  257.     my ($ICWRITE, $new_inetdcf) = tempfile("/tmp/inetdXXXXX", UNLINK => 0);
  258.     unless (defined($ICWRITE)) { die "Error creating temporary file: $!\n" }
  259.     &printv("Using tempfile $new_inetdcf\n");
  260.     open(ICREAD, "$inetdcf");
  261.     while(<ICREAD>) {
  262.       chomp;
  263.       if (/^$sep$service\s+\w+\s+/ and /$pattern/) {
  264.           &printv("Processing service \`$service' ... enabled\n");
  265.           $_ =~ s/^$sep//;
  266.           $nlines_enabled += 1;
  267.       }
  268.       print $ICWRITE "$_\n";
  269.     }
  270.     close(ICREAD);
  271.     close($ICWRITE) || die "Error closing $new_inetdcf: $!\n";
  272.  
  273.     if ($nlines_enabled > 0) {
  274.         move("$new_inetdcf","$inetdcf") ||
  275.             die "Error installing $new_inetdcf to $inetdcf: $!\n";
  276.         chmod(0644, "$inetdcf");
  277.         &wakeup_inetd(0,$init_svc_count);
  278.         &printv("Number of service entries enabled: $nlines_enabled\n");
  279.     } else {
  280.         &printv("No service entries were enabled\n");
  281.         unlink("$new_inetdcf") || die "Error removing $new_inetdcf: $!\n";
  282.     }
  283.  
  284.     return(1);
  285. }
  286.  
  287. sub wakeup_inetd {
  288.     my($removal,$init_svc_count) = @_;
  289.     my($pid);
  290.     my($action);
  291.  
  292.     $called_wakeup_inetd = 1;
  293.  
  294.     if ($removal) {
  295.         $action = 'force-reload';
  296.     } elsif ( defined($init_svc_count) and $init_svc_count == 0 ) {
  297.         $action = 'start';
  298.     } else {
  299.         $action = 'restart';
  300.     }
  301.  
  302.     $fake_invocation = defined($ENV{"UPDATE_INETD_FAKE_IT"});
  303.     if (open(P,"/var/run/inetd.pid")) {
  304.         $pid=<P>;
  305.         chomp($pid);
  306.         if (open(C,sprintf("/proc/%d/stat",$pid))) {
  307.             $_=<C>;
  308.             if (m/^\d+ \((rl|inetutils-)?inetd\)/) {
  309.                 &printv("About to send SIGHUP to inetd (pid: $pid)\n");
  310.                 unless ($fake_invocation) {
  311.                     kill(1,$pid);
  312.                 }
  313.             } else {
  314.                 print STDERR "/var/run/inetd.pid does not have a valid pid!";
  315.                 print STDERR "Please investigate and restart inetd manually.";
  316.             }
  317.             close(C);
  318.         }
  319.         close(P);
  320.     } else {
  321.         $_ = glob "/etc/init.d/*inetd";
  322.         if (m/\/etc\/init\.d\/(.*inetd)/ or $fake_invocation) {
  323.             &printv("About to $action inetd via invoke-rc.d\n");
  324.             my $service = $1;
  325.             unless ($fake_invocation) {
  326.                  # If we were called by a shell script that also uses
  327.                  # debconf, the pipe to the debconf frontend is fd 3 as
  328.                  # well as fd 1 (stdout).  Ensure that fd 3 is not
  329.                  # inherited by invoke-rc.d and inetd, as that will
  330.                  # cause debconf to hang (bug #589487).  Don't let them
  331.                  # confuse debconf via stdout either.
  332.                  system("invoke-rc.d $service $action >/dev/null 3>&-");
  333.             }
  334.         }
  335.     }
  336.     return(1);
  337. }
  338.  
  339. sub scan_entries {
  340.     my ($service, $pattern) = @_;
  341.     unless (defined($service)) { $service = '[^#\s]+'; }
  342.     unless (defined($pattern)) { $pattern = ''; }
  343.     my $counter = 0;
  344.  
  345.     open(ICREAD, "$inetdcf");
  346.     SLOOP: while (<ICREAD>) {
  347.         $counter++ if (/^$service\s+/ and /$pattern/);
  348.     }
  349.     close(ICREAD);
  350.     return($counter);
  351. }
  352.  
  353. sub printv {
  354.     print STDERR @_ if (defined($verbose));
  355. }
  356.  
  357. 1;
  358.  
  359.