Hauptmenü

Linux Firewall

Linux Server

Dokumente
Checks Unit
{***************************************************************************}
{*                               SysQuadrat                                *}
{*                                                                         *}
{* ProgramId: checks                                                       *}
{* Date     : 03.06.2002                                                   *}
{* Time     : 22:00                                                        *}
{* (c) 2002 by M. Weinert                                                  *}
{***************************************************************************}
{* Diese Unit dient dazu verschiedene Parameter zu Ã?berprÃ?fen, als da      *}
{* wÃ?ren fÃ?r den Start:                                                    *}
{* CheckDomain, CheckIP, CheckTTL                                          *}
{*                                                                         *}
{* Wenn es sinnvoll reparierbar ist, wird dies auch getan, und er bringt   *}
{* keine Fehlermeldung raus. Vielleicht eine SysLog-Warnung                *}
{***************************************************************************}


unit checks;
{$H+}
                         INTERFACE
uses stwort,strings,sysutils,resolv;

function CheckDomain(tz: ShortString):Boolean;
function CheckDomainSyntax(tz: ShortString):Boolean;
function CheckIP(var tz: ShortString):Boolean;
function CheckTTL(var tz: ShortString):Boolean;
function CheckIN(tz: ShortString):Boolean;
function CheckEmail(tz: ShortString):Boolean;
function CheckMask(tz: ShortString):byte;      // 255.255.255.0 => 24
Function GetNet(tz,ty : ShortString):ShortString;   // 192.168.1.17 255.255.255.0 => 192.168.1.0
Function GetBC(tz,ty : ShortString):ShortString;    // 192.168.1.17 255.255.255.0 => 192.168.1.255
Function GetWC(tz : ShortString):ShortString;       // 
Function GetCidr(tz,ty:ShortString):ShortString;    // 192.168.1.17 255.255.255.0 => 192.168.1.0/24

                         IMPLEMENTATION

function CheckEmail(tz: ShortString):Boolean;


begin
   CheckEmail:=True;
   if Pos('@',tz)=0 then CheckEmail:=False;
   if Pos('.',tz)=0 then CheckEmail:=False;
//   if Sys2_GetMXAddr(wort(translate(tz,'@',' '),2))='' then CheckEmail:=False;
end;


function CheckTTL(var tz: ShortString):Boolean;

var
   Wert,xc : LongInt;

begin
    val(tz,Wert,xc);
    if Wert<300 then tz:='86400';
    CheckTTL:=TRUE;  // Wird immer repariert.
end;



function CheckIN(tz: ShortString):Boolean;

begin
   tz:=lowercase(tz);
   tz:=wort(translate(tz,':',' '),1);
   if (tz<>'eth0') AND
      (tz<>'eth1') AND
      (tz<>'eth2') AND
      (tz<>'eth3') AND
      (tz<>'tr0') AND
      (tz<>'tr1') AND
      (tz<>'ppp0') AND
      (tz<>'ppp1') AND
      (tz<>'ippp0') AND
      (tz<>'ippp1') AND 
      (tz<>'lo') then CheckIN:=False else CheckIn:=TRUE;
end;

// Funktion zum Überpr�fen eines Domainnamens.
function CheckDomain(tz: ShortString):Boolean;
begin
  CheckDomain:=TRUE;
  if (Sys2_GetHostAddr(tz+'.')='') OR (Sys2_GetHostAddr(tz+'.')='0.0.0.0') then begin
      if (Sys2_GetHostAddr('www.'+tz+'.')='') OR (Sys2_GetHostAddr('www.'+tz+'.')='0.0.0.0') then CheckDomain:=False;
  end;
  if POS('.',tz)=0 then begin
     CheckDomain:=FALSE;
     exit;
  end;
  if worte(tz)>1 then CheckDomain:=False;

  tz:=translate(tz,'.',' ');
  if worte(tz)<2 then begin
     CheckDomain:=False;
     exit;
  end;
  // UngÃ?ltige Zeichen:
  if Pos('#',tz)<>0 then CheckDomain:=False;
  if Pos('_',tz)<>0 then CheckDomain:=False;
  // if Pos('#',tz)<>0 then CheckDomain:=False;

  // Noch was ?
end;

// Funktion zum Überpr�fen eines Domainnamens.
function CheckDomainSyntax(tz: ShortString):Boolean;
begin
  CheckDomainSyntax:=TRUE;
  if POS('.',tz)=0 then begin
     CheckDomainSyntax:=FALSE;
     exit;
  end;
  if worte(tz)>1 then CheckDomainSyntax:=False;

  tz:=translate(tz,'.',' ');
  if worte(tz)<2 then begin
     CheckDomainSyntax:=False;
     exit;
  end;
  // UngÃ?ltige Zeichen:
  if Pos('#',tz)<>0 then CheckDomainSyntax:=False;
  if Pos('_',tz)<>0 then CheckDomainSyntax:=False;
end;




function CheckIP(var tz: ShortString):Boolean;

var
  Zahl : Integer;
  z1,z2,z3,z4 : Byte;
  xt      : LongInt;

begin
  CheckIP:=True;
  tz:=translate(tz,'.',' ');
  if worte(tz)<>4 then begin
     CheckIP:=False;
     exit;
  end;
  Val(wort(tz,1),Zahl,xt);
  if (Zahl=0) or (Zahl>255) then CheckIP:=False;
  z1:=Zahl;
  Zahl:=StrToINt(wort(tz,2));
  if (Zahl<0) or (Zahl>255) then CheckIP:=False;
  z2:=Zahl;
  Zahl:=StrToINt(wort(tz,3));
  if (Zahl<0) or (Zahl>255) then CheckIP:=False;
  z3:=Zahl;
  Zahl:=StrToINt(wort(tz,4));
  if (Zahl<0) or (Zahl>255) then CheckIP:=False;
  z4:=Zahl;
  tz:=IntToStr(z1)+'.'+IntToStr(z2)+'.'+IntToStr(z3)+'.'+IntToStr(z4);
end;

// Überpr�ft die Netzmaske => 255.255.255.128 => 29
function CheckMask(tz: ShortString):byte;

var
  Zahl : Integer;
  Ausgabe: Byte;
  i    : Byte;
  xt      : LongInt;

begin
  Ausgabe:=0;
  tz:=translate(tz,'.',' ');
  if worte(tz)<>4 then begin
     CheckMask:=0;
     exit;
  end;
  for i:=1 to 4 do begin
     Val(wort(tz,i),Zahl,xt);
     if Zahl=255 then Ausgabe:=Ausgabe+8 // 7 macht keinen Sinn !
     else begin
       i:=4;
       case Zahl of
          252 : Ausgabe:=Ausgabe+6;
          248 : Ausgabe:=Ausgabe+5;
          240 : Ausgabe:=Ausgabe+4;
          224 : Ausgabe:=Ausgabe+3;
          192 : Ausgabe:=Ausgabe+2;
          128 : Ausgabe:=Ausgabe+1;
	    0 : Ausgabe:=Ausgabe;
          else Ausgabe:=0;
       end;
     end; // else begin
  end; // for i
  CheckMask:=Ausgabe;
end;

// Diese Function liefert das Netzwerk zurÃ?ck.
// tz=IP-Adresse, ty=Netmask
Function GetNet(tz,ty : ShortString):ShortString;

var
  i    : Byte;
  a,b  : Byte;
  s,tx : String;
//   x    : LongInt;

begin
  s:='';
  if CheckIP(tz)=false then begin
     GetNet:='';
     exit;
  end;
  if CheckMask(ty)=0 then begin
     GetNet:='';
     exit;
  end;
  tz:=translate(tz,'.',' ');
  ty:=translate(ty,'.',' ');
  for i:=1 to 4 do begin
    Val(wort(tz,i),a);
    Val(wort(ty,i),b);
    a:=a AND b;
    Str(a,tx);
    if i=1 then s:=tx else s:=s+'.'+tx;
  end;
  GetNet:=s;
end;

// Diese Function liefert die Broadcast-Adresse:
// tz=IP ty=Netmask
Function GetBC(tz,ty : ShortString):ShortString;

var
  i    : Byte;
  a,b  : Byte;
  s,tx : String;
//   x    : LongInt;

begin
  s:='';
  if CheckIP(tz)=false then begin
     GetBC:='';
     exit;
  end;
  if CheckMask(ty)=0 then begin
     GetBC:='';
     exit;
  end;
  tz:=translate(tz,'.',' ');
  ty:=translate(ty,'.',' ');
  for i:=1 to 4 do begin
    Val(wort(tz,i),a);
    Val(wort(ty,i),b);
    b:=NOT b;
    a:=a OR b;
    Str(a,tx);
    if i=1 then s:=tx else s:=s+'.'+tx;
  end;
  GetBC:=s;
end;

Function GetWC(tz : ShortString):ShortString;

var
  i    : Byte;
  a    : Byte;
  s,tx : String;
//   x    : LongInt;

begin
  s:='';
  if CheckMask(tz)=0 then begin
     GetWC:='';
     exit;
  end;
  tz:=translate(tz,'.',' ');
  for i:=1 to 4 do begin
    Val(wort(tz,i),a);
    a:=NOT a;
    Str(a,tx);
    if i=1 then s:=tx else s:=s+'.'+tx;
  end;
  GetWC:=s;
end;

Function GetCidr(tz,ty:ShortString):ShortString;    // 192.168.1.17 255.255.255.0 => 192.168.1.0/24
var
   i : Byte;
   a : String;
begin
  i:=CheckMask(ty);
  a:=tz+'/'+IntToStr(i);
  GetCidr:=a;
end;

end.



Druckversion

zuletzt geändert am 16.07.2003


(c) 2003,2015 by M. Weinert       
SysQuadrat Portal Linux Firewall / Security und mehr
Diese Seite ist Bestandteil von www.linux-firewall.de