c208bd906b3991555db11b9229846c4601ca408c
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 Einige Dummheiten repariert...

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

61) function filter_input_general( $input )
62) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

72)   }
bernd input-filtering

bernd authored 16 years ago

73) }
74) 
75) 
76) function filter_input_username( $input )
77) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

hanno authored 16 years ago

79) }
80) 
81) function verify_input_username( $input )
82) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 15 years ago

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

bernd authored 16 years ago

86)   }
bernd input-filtering

bernd authored 16 years ago

87) }
88) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

89) 
90) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 16 years ago

92) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 13 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

106)   return $input;
bernd check auf hostname

bernd authored 16 years ago

107) }
108) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 15 years ago

110) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 14 years ago

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

bernd authored 15 years ago

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

bernd authored 16 years ago

131) 
132) 
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

133) function filter_quotes( $input )
134) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

bernd authored 16 years ago

136) }
137) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

138) 
139) 
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

149) }
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

151) 
152) 
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

157)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

161)   }
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

169)       return False;
170)     }
171)   }
bernd Erlaube Leerzeichen im Pfad

bernd authored 13 years ago

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

bernd authored 16 years ago

173) }
174) 
175) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

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

bernd authored 16 years ago

193) function check_emailaddr( $input )
194) {
bernd Syntax-fehler

bernd authored 16 years ago

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

bernd authored 16 years ago

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