ec73b1b8299e8c78437f1fb6b64d863747d5a78f
[cacert.git] / www / cats / .#cats_import.php.1.2
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', '72.21.48.90');
33 define ('CONFIG_FILEPATH', '/www/');
34
35 $remote_addr = (isset($_SERVER['REMOTE_ADDR']))?$_SERVER['REMOTE_ADDR']:UNDEFINED;
36 $server_name = (isset($_SERVER['SERVER_NAME']))?$_SERVER['SERVER_NAME']:UNDEFINED;
37 $https = (isset($_SERVER['HTTPS']))?$_SERVER['HTTPS']:UNDEFINED;
38 $ssl_client_s_dn = (isset($_SERVER['SSL_CLIENT_S_DN']))?$_SERVER['SSL_CLIENT_S_DN']:UNDEFINED;
39
40 $access = FALSE;
41
42 // Access only from CATS.cacert.org with a client certificate for cats@cacert.org
43 if (
44 $remote_addr == ALLOWED_IP &&
45 $https == 'on' &&
46 // 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?)
47 // Ted: Is this specified? I don't think so, therefore I'd keep stristr
48 strlen(stristr($ssl_client_s_dn, '/emailAddress=cats@cacert.org')) > 0
49 ) $access = TRUE;
50
51 if ($access !== TRUE) {
52 echo 'UNAUTHORIZED ACCESS<br>'."\r\n";
53 echo 'IP: '.sanitize_string($remote_addr).'<br>'."\r\n";
54 echo 'Server: '.sanitize_string($server_name).'<br>'."\r\n";
55 echo 'HTTPS: '.sanitize_string($https).'<br>'."\r\n";
56 echo 'Client cert: '.sanitize_string($ssl_client_s_dn).'<br>'."\r\n";
57 trigger_error('Unauthorized access: ip('.$remote_addr.') server('.$server_name.') https('.$https.') cert('.$ssl_client_s_dn.')', E_USER_ERROR);
58 exit();
59 }
60
61 // Comment (to be romeved): do you we session autostart in php.ini??
62 // Ted: Sessions are quite meaningless for me since the upload protocol is stateless. Should session_start be called nevertheless?
63 session_start();
64
65 require_once(CONFIG_FILEPATH.'includes/mysql.php');
66
67 // Comment (to be romeved): dunno the difference between stripslashes and stripcslashes
68 // manual is iunclear too, please make sure there are no decoding issues
69 // Ted: I just used it here because I saw it elsewhere and it seems to work. Would you prefer stripslashes?
70 if (get_magic_quotes_gpc()) {
71 $serial = stripcslashes($_POST['serial']);
72 $root = stripcslashes($_POST['root']);
73 $type = stripcslashes($_POST['type']);
74 $variant = stripcslashes($_POST['variant']);
75 $date = stripcslashes($_POST['date']);
76 } else {
77 $serial = $_POST['serial'];
78 $root = $_POST['root'];
79 $type = $_POST['type'];
80 $variant = $_POST['variant'];
81 $date = $_POST['date'];
82 }
83
84 // Explicitly select all those IDs so I can insert new rows if needed.
85 $query = mysql_query('SELECT `id` FROM `cats_type` WHERE `type_text` = \''.mysql_real_escape_string($type).'\';');
86 if (!$query) {
87 echo 'Invalid query'."\r\n";
88 trigger_error('Invalid query', E_USER_ERROR);
89 exit();
90 }
91
92 if (mysql_num_rows($query) > 0) {
93 $result = mysql_fetch_array($query);
94 $typeID = $result['0'];
95 } else {
96 $query = mysql_query('INSERT INTO `cats_type` (`type_text`) VALUES (\''.mysql_real_escape_string($type).'\');');
97 if (!$query) {
98 echo 'Invalid query'."\r\n";
99 trigger_error('Invalid query', E_USER_ERROR);
100 exit();
101 }
102
103 $typeID = mysql_insert_id();
104 }
105
106 $query = mysql_query('SELECT `id` FROM `cats_variant` WHERE `type_id` = \''.(int)intval($typeID).'\' AND `test_text` = \''.mysql_real_escape_string($variant).'\';');
107 if (!$query) {
108 echo 'Invalid query'."\r\n";
109 trigger_error('Invalid query', E_USER_ERROR);
110 exit();
111 }
112
113 if (mysql_num_rows($query) > 0) {
114 $result = mysql_fetch_array($query);
115 $variantID = $result['0'];
116 } else {
117 $query = mysql_query('INSERT INTO `cats_variant` (`type_id`, `test_text`) VALUES (\''.(int)intval($typeID).'\', \''.mysql_real_escape_string($variant).'\');');
118 if (!$query) {
119 echo 'Invalid query'."\r\n";
120 trigger_error('Invalid query', E_USER_ERROR);
121 exit();
122 }
123
124 $variantID = mysql_insert_id();
125 }
126
127 // Now find the userid from cert serial
128 $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).'\';');
129 if (!$query) {
130 echo 'Invalid query'."\r\n";
131 trigger_error('Invalid query', E_USER_ERROR);
132 exit();
133 }
134
135 if (mysql_num_rows($query) > 0) {
136 $result = mysql_fetch_array($query);
137 $userID = $result['0'];
138 } else {
139 echo 'Cannot find cert '.sanitize_string($serial).' / '.sanitize_string($root)."\r\n";
140 // Let's treat this as an error, since it should not happen.
141 trigger_error('Cannot find cert '.$serial.' / '.$root.'!'.mysql_error(), E_USER_ERROR);
142 exit();
143 }
144
145 // The unique constraint on cats_passed assures that records are not stored multiply
146 $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).'\');');
147 if (!$query) {
148 if (mysql_errno() != 1062) { // Duplicate Entry is considered success
149 echo 'Invalid query'."\r\n";
150 trigger_error('Invalid query', E_USER_ERROR);
151 exit();
152 }
153 }
154
155 // Update Assurer-Flag on users table if 100 points. Should the number of points be SUM(points) or SUM(awarded)?
156 $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
157 if (!$query) {
158 echo 'Invalid query'."\r\n";
159 trigger_error('Invalid query', E_USER_ERROR);
160 exit();
161 }
162
163 echo 'OK'."\r\n";
164
165 ?>