#!/usr/bin/perl -w

use strict;
use subs qw(setup_files show_ip save_ip get_user get_ip);
use vars qw($IP $IP_FILE $HOMEDIR_BASE $cmd);

use CGI qw(:standard);

$HOMEDIR_BASE = "/home/members";
$IP_FILE = "$HOMEDIR_BASE/_HOMEDIR_/.dyndns/.dyndns";

# Wurde etwas in der Kommandozeile uebergeben ?
$cmd=$ARGV[0];
if ( defined $cmd ) {
  if ($cmd eq "create") { setup_files; }
  }

else {
  print header, start_html(" ");
  get_user;

  if (defined($ENV{REMOTE_USER}) && $ENV{REMOTE_USER} ne "read") {
    get_ip;
    if ($IP ne "") { save_ip($IP); }
    }
  else { show_ip; }
  print end_html, "\n";
  }



##################################################
#   Subroutines
##################################################

sub show_ip {

    my $current_ip;

    if (!open(IN, "<$IP_FILE")) { print "Error opening: $!"; return; }
    if (!chomp($current_ip = <IN>)) { print "Error reading: $!"; return; }
    if (!close(IN)) { print "Error closing: $!"; return; }
    print "Current IP: $current_ip<br>\n";

}

sub save_ip {
    my $new_ip = shift;
    
    if (!($new_ip =~ /^\d+\.\d+\.\d+\.\d+$/)) { print "Cant parse new value"; return; }

    print "Changing IP to $new_ip in $IP_FILE<br>\n";

    if (!open(OUT, ">$IP_FILE")) { print "Error creating: $!"; return; }
    if (!chmod 0604, "$IP_FILE") { print "Error chmoding: $!"; return; }
    if (!print OUT "$new_ip\n") { print "Error writing: $!"; return; }
    if (!close(OUT)) { print "Error closing: $!"; return; }

    print "new IP saved";
}



### Setup  (nur von der Console moeglich)

sub setup_files {

    print "Creating dyndns files...\n";

    my $username = getlogin || getpwuid($<);
    my $name;
    
    open (FILE,">.htaccess");

    print FILE "<Files update.pl>\nAuthType Basic\nAuthName \"update\"\nAuthUserFile \"$HOMEDIR_BASE/$username/.dyndns/passwd\"\nrequire valid-user\n</Files>\n"; 
    close FILE;

    print "Creating ~/.dyndns/\n";
    umask 0;
    mkdir "$HOMEDIR_BASE/$username/.dyndns", 0;
    chmod 01707, "$HOMEDIR_BASE/$username/.dyndns";

    open FILE,">$HOMEDIR_BASE/$username/.dyndns/passwd";
    print FILE "read:".crypt("", $username."sT")."\n";
    close FILE;

    chmod 0604, "$HOMEDIR_BASE/$username/.dyndns/passwd";
    print "Enter username for IP update: ";
    $name = <STDIN>;
    chomp $name;
    system("htpasswd ~/.dyndns/passwd $name");

    print "\n
Your IP can be updated by accessing the following URL:

 http://www.$username.prima.de/cgi-bin/dynip/update.pl

You will then be asked for your password and username.
To avoid the dialogue you may enter as well:

 http://$name:<YOUR_PASSWORD>\@www.$username.prima.de/cgi-bin/dynip/update.pl

or using linux:

 wget -nv --proxy=off --http-user=$name --http-passwd=<YOUR_PASSWORD> --spider 'http://www.$username.prima.de/cgi-bin/dynip/update.pl'

You can put this line into your ip-up script.
\n";
    }



### aus URL des angesprochenen servers username ermitteln

sub get_user {

    if (defined($ENV{SERVER_NAME})) {
        my ($www,$username)=split(/\./,$ENV{SERVER_NAME}) ;

        $IP_FILE =~ s/_HOMEDIR_/$username/g ;

        print "User: $username<br>\n";
        print "Directory: $IP_FILE<br>\n";
        }
    else {
        print "FATAL ERROR: Could not determine username.<br>\n";
        exit 1;
        }
    }


### IP aus client-adresse der Anfrage ermitteln

sub get_ip {

  $IP = param("IP");

  if (!$IP) {
    print "No IP sent by client - trying to guess IP ...<br>\n";
    if (defined($ENV{REMOTE_ADDR})) {
      $IP=$ENV{REMOTE_ADDR};
      print "Guessed IP: $IP<br>\n";
      }
    }
  }
