6d77a75de9cedd45eec7416e726237ed0efc6f0c
[cacert.git] / www / cats / cats_import.php
1 <? /*
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2008 CAcert Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 // Comment (to be romeved): better to disable shot open tags in php.ini
20
21 /*
22 cats_import.php
23
24 API for CATS to import passed tests into main CAcert database.
25 */
26
27 function sanitize_string($buffer) {
28 return htmlentities(utf8_decode($buffer), (int)ENQ_QUOTES);
29 }
30
31 define ('UNDEFINED', 'nd');
32 define ('ALLOWED_IP', '213.154.225.228');
33 define ('ALLOWED_IP2', '193.238.157.112');
34 define ('CONFIG_FILEPATH', '/www/');
35
36 $remote_addr = (isset($_SERVER['REMOTE_ADDR']))?$_SERVER['REMOTE_ADDR']:UNDEFINED;
37 $server_name = (isset($_SERVER['SERVER_NAME']))?$_SERVER['SERVER_NAME']:UNDEFINED;
38 $https = (isset($_SERVER['HTTPS']))?$_SERVER['HTTPS']:UNDEFINED;
39 $ssl_client_s_dn = (isset($_SERVER['SSL_CLIENT_S_DN']))?$_SERVER['SSL_CLIENT_S_DN']:UNDEFINED;
40
41 $access = FALSE;
42
43 // Access only from CATS.cacert.org with a client certificate for cats@cacert.org
44 if (
45 ($remote_addr == ALLOWED_IP || $remote_addr == ALLOWED_IP2) &&
46 $https == 'on' &&
47 // Comment (to be romeved): better to use preg_match matching the end of the line (since this is on the end of the line right?)
48 // Ted: Is this specified? I don't think so, therefore I'd keep stristr
49 strlen(stristr($ssl_client_s_dn, '/emailAddress=cats@cacert.org')) > 0
50 ) $access = TRUE;
51
52 if ($access !== TRUE) {
53 echo 'UNAUTHORIZED ACCESS<br>'."\r\n";
54 echo 'IP: '.sanitize_string($remote_addr).'<br>'."\r\n";
55 echo 'Server: '.sanitize_string($server_name).'<br>'."\r\n";
56 echo 'HTTPS: '.sanitize_string($https).'<br>'."\r\n";
57 echo 'Client cert: '.sanitize_string($ssl_client_s_dn).'<br>'."\r\n";
58 trigger_error('Unauthorized access: ip('.$remote_addr.') server('.$server_name.') https('.$https.') cert('.$ssl_client_s_dn.')', E_USER_ERROR);
59 exit();
60 }
61
62 // Comment (to be romeved): do you we session autostart in php.ini??
63 // Ted: Sessions are quite meaningless for me since the upload protocol is stateless. Should session_start be called nevertheless?
64 session_start();
65
66 require_once(CONFIG_FILEPATH.'includes/mysql.php');
67
68 // Comment (to be romeved): dunno the difference between stripslashes and stripcslashes
69 // manual is iunclear too, please make sure there are no decoding issues
70 // Ted: I just used it here because I saw it elsewhere and it seems to work. Would you prefer stripslashes?
71 if (get_magic_quotes_gpc()) {
72 $serial = stripcslashes($_POST['serial']);
73 $root = stripcslashes($_POST['root']);
74 $type = stripcslashes($_POST['type']);
75 $variant = stripcslashes($_POST['variant']);
76 $date = stripcslashes($_POST['date']);
77 } else {
78 $serial = $_POST['serial'];
79 $root = $_POST['root'];
80 $type = $_POST['type'];
81 $variant = $_POST['variant'];
82 $date = $_POST['date'];
83 }
84
85 // Explicitly select all those IDs so I can insert new rows if needed.
86 $query = mysql_query('SELECT `id` FROM `cats_type` WHERE `type_text` = \''.mysql_real_escape_string($type).'\';');
87 if (!$query) {
88 echo 'Invalid query'."\r\n";
89 trigger_error('Invalid query', E_USER_ERROR);
90 exit();
91 }
92
93 if (mysql_num_rows($query) > 0) {
94 $result = mysql_fetch_array($query);
95 $typeID = $result['0'];
96 } else {
97 $query = mysql_query('INSERT INTO `cats_type` (`type_text`) VALUES (\''.mysql_real_escape_string($type).'\');');
98 if (!$query) {
99 echo 'Invalid query'."\r\n";
100 trigger_error('Invalid query', E_USER_ERROR);
101 exit();
102 }
103
104 $typeID = mysql_insert_id();
105 }
106
107 $query = mysql_query('SELECT `id` FROM `cats_variant` WHERE `type_id` = \''.(int)intval($typeID).'\' AND `test_text` = \''.mysql_real_escape_string($variant).'\';');
108 if (!$query) {
109 echo 'Invalid query'."\r\n";
110 trigger_error('Invalid query', E_USER_ERROR);
111 exit();
112 }
113
114 if (mysql_num_rows($query) > 0) {
115 $result = mysql_fetch_array($query);
116 $variantID = $result['0'];
117 } else {
118 $query = mysql_query('INSERT INTO `cats_variant` (`type_id`, `test_text`) VALUES (\''.(int)intval($typeID).'\', \''.mysql_real_escape_string($variant).'\');');
119 if (!$query) {
120 echo 'Invalid query'."\r\n";
121 trigger_error('Invalid query', E_USER_ERROR);
122 exit();
123 }
124
125 $variantID = mysql_insert_id();
126 }
127
128 // Now find the userid from cert serial
129 $query = mysql_query('SELECT `ec`.`memid` FROM `emailcerts` AS `ec`, `root_certs` AS `rc` WHERE `ec`.`rootcert` = `rc`.`id` AND `ec`.`serial` = \''.mysql_real_escape_string($serial).'\' AND `rc`.`cert_text` = \''.mysql_real_escape_string($root).'\';');
130 if (!$query) {
131 echo 'Invalid query'."\r\n";
132 trigger_error('Invalid query', E_USER_ERROR);
133 exit();
134 }
135
136 if (mysql_num_rows($query) > 0) {
137 $result = mysql_fetch_array($query);
138 $userID = $result['0'];
139 } else {
140 echo 'Cannot find cert '.sanitize_string($serial).' / '.sanitize_string($root)."\r\n";
141 // Let's treat this as an error, since it should not happen.
142 trigger_error('Cannot find cert '.$serial.' / '.$root.'!'.mysql_error(), E_USER_ERROR);
143 exit();
144 }
145
146 // The unique constraint on cats_passed assures that records are not stored multiply
147 $query = mysql_query('INSERT INTO `cats_passed` (`user_id`, `variant_id`, `pass_date`) VALUES (\''.(int)intval($userID).'\', \''.(int)intval($variantID).'\', \''.mysql_real_escape_string($date).'\');');
148 if (!$query) {
149 if (mysql_errno() != 1062) { // Duplicate Entry is considered success
150 echo 'Invalid query'."\r\n";
151 trigger_error('Invalid query', E_USER_ERROR);
152 exit();
153 }
154 }
155
156 // Update Assurer-Flag on users table if 100 points. Should the number of points be SUM(points) or SUM(awarded)?
157 $query = mysql_query('UPDATE `users` AS `u` SET `assurer` = 1 WHERE `u`.`id` = \''.(int)intval($userID).'\' AND EXISTS(SELECT 1 FROM `cats_passed` AS `tp` WHERE `tp`.`user_id` = `u`.`id`) AND (SELECT SUM(`points`) FROM `notary` AS `n` WHERE `n`.`to` = `u`.`id` AND `expire` < now()) >= 100;'); // Challenge has been passed and non-expired points >= 100
158 if (!$query) {
159 echo 'Invalid query'."\r\n";
160 trigger_error('Invalid query', E_USER_ERROR);
161 exit();
162 }
163
164 echo 'OK'."\r\n";
165
166 ?>