PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/smime.php

https://github.com/Excito/imp
PHP | 366 lines | 305 code | 43 blank | 18 comment | 54 complexity | 2c241377d99233b109d70989c08432b9 MD5 | raw file
  1. <?php
  2. /**
  3. * $Horde: imp/smime.php,v 2.48.4.16 2009/02/10 18:47:40 slusarz Exp $
  4. *
  5. * Copyright 2002-2009 The Horde Project (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (GPL). If you
  8. * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  9. *
  10. * @author Mike Cochrane <mike@graftonhall.co.nz>
  11. * @author Michael Slusarz <slusarz@horde.org>
  12. */
  13. function _importKeyDialog($target)
  14. {
  15. $title = _("Import S/MIME Key");
  16. require IMP_TEMPLATES . '/common-header.inc';
  17. IMP::status();
  18. $t = new IMP_Template();
  19. $t->setOption('gettext', true);
  20. $t->set('selfurl', Horde::applicationUrl('smime.php'));
  21. $t->set('broken_mp_form', $GLOBALS['browser']->hasQuirk('broken_multipart_form'));
  22. $t->set('reload', htmlspecialchars(Util::getFormData('reload')));
  23. $t->set('target', $target);
  24. $t->set('forminput', Util::formInput());
  25. $t->set('import_public_key', $target == 'process_import_public_key');
  26. $t->set('import_personal_certs', $target == 'process_import_personal_certs');
  27. echo $t->fetch(IMP_TEMPLATES . '/smime/import_key.html');
  28. }
  29. function _getImportKey()
  30. {
  31. $key = Util::getFormData('import_key');
  32. if (!empty($key)) {
  33. return $key;
  34. }
  35. $res = Browser::wasFileUploaded('upload_key', _("key"));
  36. if (!is_a($res, 'PEAR_Error')) {
  37. return file_get_contents($_FILES['upload_key']['tmp_name']);
  38. } else {
  39. $GLOBALS['notification']->push($res, 'horde.error');
  40. return;
  41. }
  42. }
  43. function _outputPassphraseDialog($secure_check)
  44. {
  45. if (is_a($secure_check, 'PEAR_Error')) {
  46. $GLOBALS['notification']->push($secure_check, 'horde.warning');
  47. }
  48. $title = _("S/MIME Passphrase Input");
  49. require IMP_TEMPLATES . '/common-header.inc';
  50. IMP::status();
  51. if (is_a($secure_check, 'PEAR_Error')) {
  52. return;
  53. }
  54. $t = new IMP_Template();
  55. $t->setOption('gettext', true);
  56. $t->set('submit_url', Util::addParameter(Horde::applicationUrl('smime.php'), 'actionID', 'process_passphrase_dialog'));
  57. $t->set('reload', htmlspecialchars(Util::getFormData('reload')));
  58. $t->set('action', htmlspecialchars(Util::getFormData('passphrase_action')));
  59. $t->set('locked_img', Horde::img('locked.png', _("S/MIME"), null, $GLOBALS['registry']->getImageDir('horde')));
  60. echo $t->fetch(IMP_TEMPLATES . '/smime/passphrase.html');
  61. }
  62. function _actionWindow()
  63. {
  64. require_once 'Horde/SessionObjects.php';
  65. $oid = Util::getFormData('passphrase_action');
  66. $cacheSess = &Horde_SessionObjects::singleton();
  67. $cacheSess->setPruneFlag($oid, true);
  68. Util::closeWindowJS($cacheSess->query($oid));
  69. }
  70. function _reloadWindow()
  71. {
  72. require_once 'Horde/SessionObjects.php';
  73. $cacheSess = &Horde_SessionObjects::singleton();
  74. $reload = Util::getFormData('reload');
  75. $url = $cacheSess->query($reload);
  76. $cacheSess->setPruneFlag($reload, true);
  77. Util::closeWindowJS('opener.focus();opener.location.href="' . $url . '";');
  78. }
  79. function _textWindowOutput($filename, $msg, $html = false)
  80. {
  81. $type = ($html ? 'text/html' : 'text/plain') . '; charset=' . NLS::getCharset();
  82. $GLOBALS['browser']->downloadHeaders($filename, $type, true, strlen($msg));
  83. echo $msg;
  84. }
  85. function _printKeyInfo($cert)
  86. {
  87. $key_info = $GLOBALS['imp_smime']->certToHTML($cert);
  88. if (empty($key_info)) {
  89. _textWindowOutput('S/MIME Key Information', _("Invalid key"));
  90. } else {
  91. _textWindowOutput('S/MIME Key Information', $key_info, true);
  92. }
  93. }
  94. @define('IMP_BASE', dirname(__FILE__));
  95. require_once IMP_BASE . '/lib/base.php';
  96. require_once IMP_BASE . '/lib/Crypt/SMIME.php';
  97. require_once IMP_BASE . '/lib/Template.php';
  98. $imp_smime = new IMP_SMIME();
  99. $secure_check = $imp_smime->requireSecureConnection();
  100. /* Run through the action handlers */
  101. $actionID = Util::getFormData('actionID');
  102. switch ($actionID) {
  103. case 'open_passphrase_dialog':
  104. if ($imp_smime->getPassphrase() !== false) {
  105. Util::closeWindowJS();
  106. } else {
  107. _outputPassphraseDialog($secure_check);
  108. }
  109. exit;
  110. case 'process_passphrase_dialog':
  111. if (is_a($secure_check, 'PEAR_Error')) {
  112. _outputPassphraseDialog($secure_check);
  113. } elseif (Util::getFormData('passphrase')) {
  114. if ($imp_smime->storePassphrase(Util::getFormData('passphrase'))) {
  115. if (Util::getFormData('passphrase_action')) {
  116. _actionWindow();
  117. } elseif (Util::getFormData('reload')) {
  118. _reloadWindow();
  119. } else {
  120. Util::closeWindowJS();
  121. }
  122. } else {
  123. $notification->push("Invalid passphrase entered.", 'horde.error');
  124. _outputPassphraseDialog($secure_check);
  125. }
  126. } else {
  127. $notification->push("No passphrase entered.", 'horde.error');
  128. _outputPassphraseDialog($secure_check);
  129. }
  130. exit;
  131. case 'delete_key':
  132. $imp_smime->deletePersonalKeys();
  133. $notification->push(_("Personal S/MIME keys deleted successfully."), 'horde.success');
  134. break;
  135. case 'delete_public_key':
  136. $result = $imp_smime->deletePublicKey(Util::getFormData('email'));
  137. if (is_a($result, 'PEAR_Error')) {
  138. $notification->push($result, $result->getCode());
  139. } else {
  140. $notification->push(sprintf(_("S/MIME Public Key for \"%s\" was successfully deleted."), Util::getFormData('email')), 'horde.success');
  141. }
  142. break;
  143. case 'import_public_key':
  144. _importKeyDialog('process_import_public_key');
  145. exit;
  146. case 'process_import_public_key':
  147. $publicKey = _getImportKey();
  148. if (empty($publicKey)) {
  149. $notification->push(_("No S/MIME public key imported."), 'horde.error');
  150. $actionID = 'import_public_key';
  151. _importKeyDialog('process_import_public_key');
  152. } else {
  153. /* Add the public key to the storage system. */
  154. $key_info = $imp_smime->addPublicKey($publicKey);
  155. if (is_a($key_info, 'PEAR_Error')) {
  156. $notification->push($key_info, 'horde.error');
  157. $actionID = 'import_public_key';
  158. _importKeyDialog('process_import_public_key');
  159. } else {
  160. $notification->push(_("S/MIME Public Key successfully added."), 'horde.success');
  161. _reloadWindow();
  162. }
  163. }
  164. exit;
  165. case 'view_public_key':
  166. $key = $imp_smime->getPublicKey(Util::getFormData('email'));
  167. if (is_a($key, 'PEAR_Error')) {
  168. $key = $key->getMessage();
  169. }
  170. _textWindowOutput('S/MIME Public Key', $key);
  171. exit;
  172. case 'info_public_key':
  173. $key = $imp_smime->getPublicKey(Util::getFormData('email'));
  174. if (is_a($key, 'PEAR_Error')) {
  175. $key = $key->getMessage();
  176. }
  177. _printKeyInfo($key);
  178. exit;
  179. case 'view_personal_public_key':
  180. _textWindowOutput('S/MIME Personal Public Key', $imp_smime->getPersonalPublicKey());
  181. exit;
  182. case 'info_personal_public_key':
  183. _printKeyInfo($imp_smime->getPersonalPublicKey());
  184. exit;
  185. case 'view_personal_private_key':
  186. _textWindowOutput('S/MIME Personal Private Key', $imp_smime->getPersonalPrivateKey());
  187. exit;
  188. case 'import_personal_certs':
  189. _importKeyDialog('process_import_personal_certs');
  190. exit;
  191. case 'process_import_personal_certs':
  192. if (!($pkcs12 = _getImportKey())) {
  193. $notification->push(_("No personal S/MIME certificates imported."), 'horde.error');
  194. $actionID = 'import_personal_certs';
  195. _importKeyDialog('process_import_personal_certs');
  196. } else {
  197. $res = $imp_smime->addFromPKCS12($pkcs12, Util::getFormData('upload_key_pass'), Util::getFormData('upload_key_pk_pass'));
  198. if (is_a($res, 'PEAR_Error')) {
  199. $notification->push(_("Personal S/MIME certificates NOT imported: ") . $res->getMessage(), 'horde.error');
  200. $actionID = 'import_personal_certs';
  201. _importKeyDialog('process_import_personal_certs');
  202. } else {
  203. $notification->push(_("S/MIME Public/Private Keypair successfully added."), 'horde.success');
  204. _reloadWindow();
  205. }
  206. }
  207. exit;
  208. case 'save_attachment_public_key':
  209. require_once 'Horde/SessionObjects.php';
  210. $cacheSess = &Horde_SessionObjects::singleton();
  211. $cert = $cacheSess->query(Util::getFormData('cert'));
  212. /* Add the public key to the storage system. */
  213. $cert = $imp_smime->addPublicKey($cert);
  214. if ($cert == false) {
  215. $notification->push(_("No Certificate found"), 'horde.error');
  216. } else {
  217. Util::closeWindowJS();
  218. }
  219. exit;
  220. case 'unset_passphrase':
  221. if ($imp_smime->getPassphrase() !== false) {
  222. $imp_smime->unsetPassphrase();
  223. $notification->push(_("Passphrase successfully unloaded."), 'horde.success');
  224. }
  225. break;
  226. case 'save_options':
  227. $prefs->setValue('use_smime', Util::getFormData('use_smime') ? 1 : 0);
  228. $prefs->setValue('smime_verify', Util::getFormData('smime_verify') ? 1 : 0);
  229. $notification->push(_("Preferences successfully updated."), 'horde.success');
  230. break;
  231. }
  232. /* Get list of Public Keys. */
  233. $pubkey_list = $imp_smime->listPublicKeys();
  234. if (is_a($pubkey_list, 'PEAR_Error')) {
  235. $notification->push($pubkey_list, $pubkey_list->getCode());
  236. }
  237. if (is_callable(array('Horde', 'loadConfiguration'))) {
  238. $result = Horde::loadConfiguration('prefs.php', array('prefGroups', '_prefs'), 'imp');
  239. if (!is_a($result, 'PEAR_Error')) {
  240. extract($result);
  241. }
  242. } else {
  243. require IMP_BASE . '/config/prefs.php';
  244. }
  245. require_once 'Horde/Help.php';
  246. require_once 'Horde/Prefs/UI.php';
  247. $app = 'imp';
  248. $chunk = Util::nonInputVar('chunk');
  249. Prefs_UI::generateHeader('smime', $chunk);
  250. $selfURL = Horde::applicationUrl('smime.php');
  251. /* If S/MIME preference not active, or openssl PHP extension not available, do
  252. * NOT show S/MIME Admin screen. */
  253. $openssl_check = $imp_smime->checkForOpenSSL();
  254. /* If S/MIME preference not active, do NOT show S/MIME Admin screen. */
  255. $t = new IMP_Template();
  256. $t->setOption('gettext', true);
  257. $t->set('use_smime_help', Help::link('imp', 'smime-overview'));
  258. if (!is_a($openssl_check, 'PEAR_Error') && $prefs->getValue('use_smime')) {
  259. Horde::addScriptFile('popup.js', 'imp', true);
  260. $t->set('smimeactive', true);
  261. $opensmimewin = $imp_smime->getJSOpenWinCode('open_passphrase_dialog');
  262. $t->set('manage_pubkey-help', Help::link('imp', 'smime-manage-pubkey'));
  263. $t->set('verify_notlocked', !$prefs->isLocked('smime_verify'));
  264. if ($t->get('verify_notlocked')) {
  265. $t->set('smime_verify', $prefs->getValue('smime_verify'));
  266. $t->set('smime_verify-help', Help::link('imp', 'smime-option-verify'));
  267. }
  268. $t->set('empty_pubkey_list', empty($pubkey_list));
  269. if (!$t->get('empty_pubkey_list')) {
  270. $t->set('pubkey_error', is_a($pubkey_list, 'PEAR_Error') ? $pubkey_list->getMessage() : false);
  271. if (!$t->get('pubkey_error')) {
  272. $plist = array();
  273. foreach ($pubkey_list as $val) {
  274. $linkurl = Util::addParameter($selfURL, 'email', $val['email']);
  275. $plist[] = array(
  276. 'name' => $val['name'],
  277. 'email' => $val['email'],
  278. 'view' => Horde::link(Util::addParameter($linkurl, 'actionID', 'view_public_key'), sprintf(_("View %s Public Key"), $val['name']), null, 'view_key'),
  279. 'info' => Horde::link(Util::addParameter($linkurl, 'actionID', 'info_public_key'), sprintf(_("Information on %s Public Key"), $val['name']), null, 'info_key'),
  280. 'delete' => Horde::link(Util::addParameter($linkurl, 'actionID', 'delete_public_key'), sprintf(_("Delete %s Public Key"), $val['name']), null, null, "if (confirm('" . addslashes(_("Are you sure you want to delete this public key?")) . "')) { return true; } else { return false; }")
  281. );
  282. }
  283. $t->set('pubkey_list', $plist);
  284. }
  285. }
  286. $t->set('no_file_upload', !$_SESSION['imp']['file_upload']);
  287. if (!$t->get('no_file_upload')) {
  288. $t->set('no_source', !$GLOBALS['prefs']->getValue('add_source'));
  289. if (!$t->get('no_source')) {
  290. require_once 'Horde/SessionObjects.php';
  291. $cacheSess = &Horde_SessionObjects::singleton();
  292. $t->set('public_import_url', Util::addParameter(Util::addParameter($selfURL, 'actionID', 'import_public_key'), 'reload', $cacheSess->storeOid($selfURL, false)));
  293. $t->set('import_pubkey-help', Help::link('imp', 'smime-import-pubkey'));
  294. }
  295. }
  296. $t->set('personalkey-help', Help::link('imp', 'smime-overview-personalkey'));
  297. $t->set('secure_check', is_a($secure_check, 'PEAR_Error'));
  298. if (!$t->get('secure_check')) {
  299. $t->set('has_key', $prefs->getValue('smime_public_key') && $prefs->getValue('smime_private_key'));
  300. if ($t->get('has_key')) {
  301. $t->set('viewpublic', Horde::link(Util::addParameter($selfURL, 'actionID', 'view_personal_public_key'), _("View Personal Public Key"), null, 'view_key'));
  302. $t->set('infopublic', Horde::link(Util::addParameter($selfURL, 'actionID', 'info_personal_public_key'), _("Information on Personal Public Key"), null, 'info_key'));
  303. $passphrase = $imp_smime->getPassphrase();
  304. $t->set('passphrase', (empty($passphrase)) ? Horde::link('#', _("Enter Passphrase"), null, null, htmlspecialchars($imp_smime->getJSOpenWinCode('open_passphrase_dialog')) . ' return false;') . _("Enter Passphrase") : Horde::link(Util::addParameter($selfURL, 'actionID', 'unset_passphrase'), _("Unload Passphrase")) . _("Unload Passphrase"));
  305. $t->set('viewprivate', Horde::link(Util::addParameter($selfURL, 'actionID', 'view_personal_private_key'), _("View Personal Private Key"), null, 'view_key'));
  306. $t->set('deletekeypair', addslashes(_("Are you sure you want to delete your keypair? (This is NOT recommended!)")));
  307. $t->set('personalkey-delete-help', Help::link('imp', 'smime-delete-personal-certs'));
  308. } else {
  309. $t->set('personal_import_url', Util::addParameter($selfURL, 'actionID', 'import_personal_certs'));
  310. $t->set('import-cert-help', Help::link('imp', 'smime-import-personal-certs'));
  311. }
  312. }
  313. } else {
  314. $t->set('use_smime_locked', $prefs->isLocked('use_smime'));
  315. if (!$t->get('use_smime_locked')) {
  316. $t->set('use_smime_label', Horde::label('use_smime', _("Enable S/MIME functionality?")));
  317. }
  318. }
  319. $t->set('prefsurl', IMP::prefsURL(true));
  320. echo $t->fetch(IMP_TEMPLATES . '/smime/smime.html');
  321. if (!$chunk) {
  322. require $registry->get('templates', 'horde') . '/common-footer.inc';
  323. }