20664c3d61ba75cad92cf2d92e7e4d36311e73eb
bernd input-filtering

bernd authored 16 years ago

1) <?php
2) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

3) require_once('inc/error.php');
4) 
bernd input-filtering

bernd authored 16 years ago

5) 
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

6) function strong_password($password)
7) {
bernd detect if cracklib is insta...

bernd authored 14 years ago

8)   if (! function_exists("crack_opendict"))
9)   {
10)     DEBUG("cracklib not available!");
11)     return true;
12)   }
bernd Einige Dummheiten repariert...

bernd authored 14 years ago

13)   if (config('use_cracklib') === NULL or config('use_cracklib') === false) {
bernd cracklib ausschaltbar machen

bernd authored 16 years ago

14)     DEBUG('Cracklib deaktiviert');
15)     return true;
16)   }
bernd Einige Dummheiten repariert...

bernd authored 14 years ago

17)   DEBUG("Öffne Wörterbuch: ".config('cracklib_dict'));
bernd Mehr config-optionen und co...

bernd authored 14 years ago

18)   if (! ($dict = crack_opendict(config('cracklib_dict'))))
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

19)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

20)     logger(LOG_ERR, "inc/security", "cracklib", "could not open cracklib-dictionary »".config('cracklib_dict')."«");
bernd Einige Dummheiten repariert...

bernd authored 14 years ago

21)     system_failure("Kann Crack-Lib-Wörterbuch nicht öffnen: ".config('cracklib_dict'));
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

22)   }
23)   // Führe eine Überprüfung des Passworts durch
24)   $check = crack_check($dict, $password);
25) 
26)   $message = crack_getlastmessage();
27)   crack_closedict($dict);
28) 
29)   if ($check === True)
30)   {
31)     DEBUG("Passwort ok");
32)     return true;
33)   }
34)   else
35)   {
36)     DEBUG("Passwort nicht ok: {$message}");
37)     return $message;
38)   }
39) }
40) 
41) 
bernd input-filtering

bernd authored 16 years ago

42) function filter_input_general( $input )
43) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

44)   return htmlspecialchars(iconv('UTF-8', 'UTF-8', $input), ENT_QUOTES, 'UTF-8');
45) }
46) 
47) 
48) function verify_input_general( $input )
49) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

50)   if (filter_input_general($input) != $input) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

51)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Logger mit Logleveln

bernd authored 14 years ago

52)     logger(LOG_WARNING, 'inc/security', 'verify_input_general', 'Ungültige Daten: '.$input);
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

53)   }
bernd input-filtering

bernd authored 16 years ago

54) }
55) 
56) 
57) function filter_input_username( $input )
58) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

59)   return preg_replace("/[^[:alnum:]\_\.\+\-]/", "", $input );
hanno Hatte die Kompatibilität ge...

hanno authored 16 years ago

60) }
61) 
62) function verify_input_username( $input )
63) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

64)   if (filter_input_username( $input ) != $input) {
bernd Logger mit Logleveln

bernd authored 14 years ago

65)     logger(LOG_WARNING, 'inc/security', 'verify_input_username', 'Ungültige Daten: '.$input);
bernd Add IP-address patterns

bernd authored 15 years ago

66)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

67)   }
bernd input-filtering

bernd authored 16 years ago

68) }
69) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

70) 
71) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

72) function filter_input_hostname( $input, $wildcard=false )
bernd check auf hostname

bernd authored 16 years ago

73) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

74)   // FIXME: Eine "filter"-Funktion sollte keinen system_failure verursachen sondern einfach einen bereinigten String liefern.
75)   
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

76)   $input = str_replace(array('Ä', 'Ö', 'Ü'), array('ä', 'ö', 'ü'), strtolower($input));
bernd Hostnames sollten nicht mit...

bernd authored 16 years ago

77)   $input = rtrim($input, "\t\n\r\x00 .");
78)   $input = ltrim($input, "\t\n\r\x00 .");
bernd deprecation of ereg* functi...

bernd authored 14 years ago

79)   if (preg_replace("/[^[:alnum:]äöü*\.\-]/", "", $input ) != $input)
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

80)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Erlaube wildcards bei vhosts

bernd authored 13 years ago

81)   if (preg_match("/^.+\*/", $input ))
82)     system_failure("Ihre Daten enthielten ungültige Zeichen (Wildcard-Stern muss ganz vorne stehen)!");
83)   if (! $wildcard && preg_replace("/^\*/", "", $input ) != $input)
bernd Erlaube * im Hostname

bernd authored 14 years ago

84)     system_failure("Ihre Daten enthielten ungültige Zeichen (Keine Wildcards erlaubt)!");
bernd Hostname darf kein »..« ent...

bernd authored 16 years ago

85)   if (strstr($input, '..'))
86)     system_failure("Ungültiger Hostname");
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

87)   return $input;
bernd check auf hostname

bernd authored 16 years ago

88) }
89) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

90) function verify_input_hostname( $input, $wildcard=false )
bernd Add IP-address patterns

bernd authored 15 years ago

91) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

92)   if (filter_input_hostname( $input, $wildcard ) != $input) {
bernd Logger mit Logleveln

bernd authored 14 years ago

93)     logger(LOG_WARNING, 'inc/security', 'verify_input_hostname', 'Ungültige Daten: '.$input);
bernd Add IP-address patterns

bernd authored 15 years ago

94)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
95)   }
96) }
97) 
98) 
99) function verify_input_ipv4( $input )
100) {
101)   if (! preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $input))
102)     system_failure('Keine IP-Adresse');
103) }
104) 
105) 
106) function verify_input_ipv6( $input )
107) {
108)   // ripped from Perl module Net-IPv6Addr v0.2
109)   if (! preg_match("/^(([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|[0-9a-f]{0,4}::|:(?::[a-f0-9]{1,4}){1,6}|(?:[a-f0-9]{1,4}:){1,6}:|(?:[a-f0-9]{1,4}:)(?::[a-f0-9]{1,4}){1,6}|(?:[a-f0-9]{1,4}:){2}(?::[a-f0-9]{1,4}){1,5}|(?:[a-f0-9]{1,4}:){3}(?::[a-f0-9]{1,4}){1,4}|(?:[a-f0-9]{1,4}:){4}(?::[a-f0-9]{1,4}){1,3}|(?:[a-f0-9]{1,4}:){5}(?::[a-f0-9]{1,4}){1,2}|(?:[a-f0-9]{1,4}:){6}(?::[a-f0-9]{1,4}))$/i", $input))
110)     system_failure("Ungültige IPv6-Adresse");
111) }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

112) 
113) 
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

114) function filter_quotes( $input )
115) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

116)   return preg_replace('/["\'`]/', '', $input );
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

117) }
118) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

119) 
120) 
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

121) function filter_shell( $input )
122) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

123)   return preg_replace('/[^-[:alnum:]\_\.\+ßäöüÄÖÜ/%§=]/', '', $input );
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

124) }
125) 
126) function verify_shell( $input )
127) {
128)   if (filter_shell($input) != $input)
129)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

130) }
bernd input-filtering

bernd authored 16 years ago

131) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

132) 
133) 
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

134) function check_path( $input )
135) {
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

136)   DEBUG("checking {$input} for valid path name");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

137)   if ($input != filter_input_general($input))
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

138)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

139)     logger(LOG_WARNING, 'inc/security', 'check_path', 'HTML-Krams im Pfad: '.$input);
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

140)     DEBUG("HTML-Krams im Pfad");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

141)     return False;
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

142)   }
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

143)   $components = explode("/", $input);
144)   foreach ($components AS $item)
145)   {
146)     if ($item == '..')
147)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

148)       logger(LOG_WARNING, 'inc/security', 'check_path', '»..« im Pfad: '.$input);
bernd check auf hostname

bernd authored 16 years ago

149)       DEBUG("»..« im Pfad");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

150)       return False;
151)     }
152)   }
bernd Erlaube Leerzeichen im Pfad

bernd authored 13 years ago

153)   return (preg_match('/^[ A-Za-z0-9.@\/_-]*$/',$input) == 1);
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

154) }
155) 
156) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

157) function in_homedir($path)
158) {
159)   DEBUG("Prüfe »{$path}«");
160)   if (! check_path($path))
161)   {
162)     DEBUG('Kein Pfad');
163)     return False;
164)   }
165)   if (! isset($_SESSION['userinfo']['homedir']))
166)   {
167)     DEBUG("Kann homedir nicht ermitteln");
168)     return False;
169)   }
170)   return strncmp($_SESSION['userinfo']['homedir'], $path, count($_SESSION['userinfo']['homedir'])) == 0;
171) }
172) 
173) 
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

174) function check_emailaddr( $input )
175) {
bernd Syntax-fehler

bernd authored 16 years ago

176)   return (bool) filter_var($input, FILTER_VALIDATE_EMAIL) == $input;
bernd don't be too complicated. /...

bernd authored 16 years ago

177) }
178) 
179) function check_domain( $input )
180) {
181)   return (bool) preg_match("/[a-z0-9\.\-]+\.[a-z]{2,4}$/i", $input );