f4a5520490462b1b9dea5494aa8785d737b1cf02
bernd input-filtering

bernd authored 16 years ago

1) <?php
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
Bernd Wurst Updated copyright notice (2...

Bernd Wurst authored 11 years ago

5) Written 2008-2013 by schokokeks.org Hosting, namely
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
9) To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10) 
11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15) */
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

17) require_once('inc/error.php');
18) 
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

20) function strong_password($password)
21) {
Bernd Wurst Subusers-Modul in einer ers...

Bernd Wurst authored 12 years ago

22)   if ($password == '' || strlen($password) < 4) {
23)     DEBUG("Passwort zu kurz!");
24)     return "Passwort ist zu kurz!";
25)   }
26) 
bernd detect if cracklib is insta...

bernd authored 14 years ago

27)   if (! function_exists("crack_opendict"))
28)   {
29)     DEBUG("cracklib not available!");
30)     return true;
31)   }
bernd Einige Dummheiten repariert...

bernd authored 14 years ago

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

bernd authored 16 years ago

33)     DEBUG('Cracklib deaktiviert');
34)     return true;
35)   }
bernd Einige Dummheiten repariert...

bernd authored 14 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

38)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

39)     logger(LOG_ERR, "inc/security", "cracklib", "could not open cracklib-dictionary »".config('cracklib_dict')."«");
Bernd Wurst Wenn cracklib nicht funtkio...

Bernd Wurst authored 11 years ago

40)     #system_failure("Kann Crack-Lib-Wörterbuch nicht öffnen: ".config('cracklib_dict'));
41)     DEBUG('cracklib tut nicht, dann wird das PW akzeptiert');
42)     warning('Das Passwort konnte aufgrund eines internen Fehlers nicht auf die Passwortstärke geprüft werden.');
43)     return true;
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

44)   }
45)   // Führe eine Überprüfung des Passworts durch
46)   $check = crack_check($dict, $password);
47) 
48)   $message = crack_getlastmessage();
49)   crack_closedict($dict);
50) 
51)   if ($check === True)
52)   {
53)     DEBUG("Passwort ok");
54)     return true;
55)   }
56)   else
57)   {
58)     DEBUG("Passwort nicht ok: {$message}");
59)     return $message;
60)   }
61) }
62) 
63) 
bernd input-filtering

bernd authored 16 years ago

64) function filter_input_general( $input )
65) {
Bernd Wurst Weitere Prepared-Statements

Bernd Wurst authored 10 years ago

66)   if ($input === NULL) {
67)     return NULL;
68)   }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

69)   return htmlspecialchars(iconv('UTF-8', 'UTF-8', $input), ENT_QUOTES, 'UTF-8');
70) }
71) 
72) 
73) function verify_input_general( $input )
74) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

78)   }
bernd input-filtering

bernd authored 16 years ago

79) }
80) 
81) 
82) function filter_input_username( $input )
83) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

hanno authored 16 years ago

85) }
86) 
87) function verify_input_username( $input )
88) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 15 years ago

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

bernd authored 16 years ago

92)   }
bernd input-filtering

bernd authored 16 years ago

93) }
94) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

95) 
96) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 16 years ago

98) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

99)   // FIXME: Eine "filter"-Funktion sollte keinen system_failure verursachen sondern einfach einen bereinigten String liefern.
100)   
Bernd Wurst Wildcard-CSRs können jetzt...

Bernd Wurst authored 11 years ago

101)   DEBUG('filter_input_hostname("'.$input.'", $wildcard='.$wildcard.')');
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 13 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

113)   return $input;
bernd check auf hostname

bernd authored 16 years ago

114) }
115) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 15 years ago

117) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 14 years ago

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

bernd authored 15 years ago

120)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
121)   }
122) }
123) 
124) 
125) function verify_input_ipv4( $input )
126) {
127)   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))
128)     system_failure('Keine IP-Adresse');
129) }
130) 
131) 
132) function verify_input_ipv6( $input )
133) {
134)   // ripped from Perl module Net-IPv6Addr v0.2
135)   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))
136)     system_failure("Ungültige IPv6-Adresse");
137) }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

138) 
139) 
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

140) function filter_quotes( $input )
141) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

bernd authored 16 years ago

143) }
144) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

145) 
146) 
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

147) function filter_shell( $input )
148) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

bernd authored 16 years ago

150) }
151) 
152) function verify_shell( $input )
153) {
154)   if (filter_shell($input) != $input)
155)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

156) }
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

158) 
159) 
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

164)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

168)   }
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

169)   $components = explode("/", $input);
170)   foreach ($components AS $item)
171)   {
172)     if ($item == '..')
173)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

176)       return False;
177)     }
178)   }
bernd Erlaube Leerzeichen im Pfad

bernd authored 13 years ago

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

bernd authored 16 years ago

180) }
181) 
182) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

183) function in_homedir($path)
184) {
185)   DEBUG("Prüfe »{$path}«");
186)   if (! check_path($path))
187)   {
188)     DEBUG('Kein Pfad');
189)     return False;
190)   }
191)   if (! isset($_SESSION['userinfo']['homedir']))
192)   {
193)     DEBUG("Kann homedir nicht ermitteln");
194)     return False;
195)   }
196)   return strncmp($_SESSION['userinfo']['homedir'], $path, count($_SESSION['userinfo']['homedir'])) == 0;
197) }
198) 
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

199) function check_date( $input )
200) {
201)   return (bool) preg_match("/[0-9]{4}-(0?[1-9]|11|12)-([012]?[0-9]|30|31)/", $input);
202) }
203) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

204) 
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

205) function check_emailaddr( $input )
206) {
bernd Syntax-fehler

bernd authored 16 years ago

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

bernd authored 16 years ago

208) }
209) 
210) function check_domain( $input )
211) {
212)   return (bool) preg_match("/[a-z0-9\.\-]+\.[a-z]{2,4}$/i", $input );