9cbd3c5115789e54a67595f9e599a91ac0fdb851
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) 
5) Written 2008-2012 by schokokeks.org Hosting, namely
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 Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

75)   }
bernd input-filtering

bernd authored 16 years ago

76) }
77) 
78) 
79) function filter_input_username( $input )
80) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

hanno authored 16 years ago

82) }
83) 
84) function verify_input_username( $input )
85) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 15 years ago

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

bernd authored 16 years ago

89)   }
bernd input-filtering

bernd authored 16 years ago

90) }
91) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

92) 
93) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 16 years ago

95) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 13 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

109)   return $input;
bernd check auf hostname

bernd authored 16 years ago

110) }
111) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 15 years ago

113) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 14 years ago

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

bernd authored 15 years ago

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

bernd authored 16 years ago

134) 
135) 
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

136) function filter_quotes( $input )
137) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

bernd authored 16 years ago

139) }
140) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

141) 
142) 
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

143) function filter_shell( $input )
144) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

bernd authored 16 years ago

146) }
147) 
148) function verify_shell( $input )
149) {
150)   if (filter_shell($input) != $input)
151)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

152) }
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

154) 
155) 
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

160)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

164)   }
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

165)   $components = explode("/", $input);
166)   foreach ($components AS $item)
167)   {
168)     if ($item == '..')
169)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

172)       return False;
173)     }
174)   }
bernd Erlaube Leerzeichen im Pfad

bernd authored 13 years ago

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

bernd authored 16 years ago

176) }
177) 
178) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

179) function in_homedir($path)
180) {
181)   DEBUG("Prüfe »{$path}«");
182)   if (! check_path($path))
183)   {
184)     DEBUG('Kein Pfad');
185)     return False;
186)   }
187)   if (! isset($_SESSION['userinfo']['homedir']))
188)   {
189)     DEBUG("Kann homedir nicht ermitteln");
190)     return False;
191)   }
192)   return strncmp($_SESSION['userinfo']['homedir'], $path, count($_SESSION['userinfo']['homedir'])) == 0;
193) }
194) 
195) 
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

196) function check_emailaddr( $input )
197) {
bernd Syntax-fehler

bernd authored 16 years ago

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

bernd authored 16 years ago

199) }
200) 
201) function check_domain( $input )
202) {
203)   return (bool) preg_match("/[a-z0-9\.\-]+\.[a-z]{2,4}$/i", $input );