<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w

# soucast userap modulu pro QoS na klfree debian
# ==============================================
# tento skript aktualizuje seznamy IP adres uzivatelu v /var/state/qos
# data ziskava z exportu z IS klfree


use UserapCommon;

#$DBSERVER="https://work.is.klfree.net/xtom/is/export.php?export=bans&amp;net=";

sub get_stamp ($) {
    my $fn = shift;
    my $stamp = '';
    if (open LF,"$fn") {
	$stamp = &lt;LF&gt;;
	(undef, $stamp) = split ("'",&lt;LF&gt;);
	close LF;
	return $stamp
    } else {
	return '';
    }
}

sub change_stamp ($$) {
    my $fn = shift;
    my $stamp = shift;
    open TF, "&gt;$fn.tmp";
    if (open LF, "$fn") {
	while (&lt;LF&gt;) {
	    if ($_ =~ /stamp/) {
		print TF "&lt;accounts stamp='$stamp'&gt;\n";
	    } else {
		print TF $_;
	    }
	}
    }
    close TF;
    close LF;
    `mv $fn.tmp $fn`;
}


sub update_face {
    my ($face,$fface) = @_;
    print "       $face\[$fface\] : ";
    if (system("ip link show dev $fface &amp;&gt;/dev/null")) {
        print "unknown device $fface\n";
        exit 0;
    }
    
    system("rm -f $DATADIR/qos-${face}.tmp 2&gt;/dev/null");

    my $act_stamp = get_stamp("$DATADIR/qos-${face}.act");

    if (-r "$QOSDIR/scripts/userap_files/manual/subnets-$face") {
	#manualne definovane subnety 
	my $ADDR = `cat ${QOSDIR}/scripts/userap_files/manual/subnets-${face}`;
	$ADDR =~ s/#.*//g;
	@ADDR = split("\n",$ADDR);
    } else {
	#automaticky podle subnetu na interfacech 
	my $ADDR =`ip r show dev $fface | cut -f1 -d ' '`;
	@ADDR = split ("\n",$ADDR);
    }
    
    #stazeni exportu ze serveru
    my $aadr = join(",", @ADDR);
    if (system("wget --timeout=3 --tries=5 --no-check-certificate -O $DATADIR/qos.tmp \"${DBSERVER}$aadr\&amp;stamp=$act_stamp\" &amp;&gt;/dev/null" )) {
        print "===== FAIL ==== \n";
        return;
    }
    system("cat $DATADIR/qos.tmp &gt;&gt;$DATADIR/qos-${face}.tmp");


    if ( ! (-r "$DATADIR/qos-$face.tmp") ) {
        print "NO DATA!\n";
        return;
    }

    my $size = `wc -l $DATADIR/qos-$face.tmp | cut -f1 -d ' '`;
    if ($size == 0) {
	print "NO LINES!\n";
	return;
    }

    my $new_stamp = get_stamp("$DATADIR/qos-${face}.tmp");
    #test stamps first
    if ($new_stamp eq $act_stamp) {
        #equal - no change
        print "same stamp\n";
    } elsif (system("diff $DATADIR/qos-$face.tmp $DATADIR/qos-$face.act --ignore-matching-lines=stamp &amp;&gt;/dev/null")) {
    	print "change!\n";
	`touch $DATADIR/faces/$face`;
	system("cp $DATADIR/qos-$face.tmp $DATADIR/qos-$face.act");
    } else {
        change_stamp("$DATADIR/qos-${face}.act",$new_stamp);	    
        print "only stamp change\n";
    }
    #nakopcit .tmp na .act
    system("rm -f $DATADIR/qos-$face.tmp &amp;&gt;/dev/null");
    system("rm -f $DATADIR/qos.tmp &amp;&gt;/dev/null");
}



# ==========================================================
# MAIN 
# ==========================================================

print "   is_updater.pl v:$UVERS updating:\n";

$tmpfc = `ls $DATADIR/faces/`;
@faces = split(" ", $tmpfc);
foreach (@faces) {
    my $face = $_;
    if ($face ne "") {
	my $fface = `cat $DATADIR/faces/$face`;
	chomp($fface);
	update_face($face,$fface);
    }
}
# ==========================================================
</pre></body></html>