/security/manager/pki/resources/content/crlManager.js

http://github.com/zpao/v8monkey · JavaScript · 255 lines · 179 code · 25 blank · 51 comment · 16 complexity · 937f21cebcfbbb1d340416d9f939e72d MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is mozilla.org code.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 2001
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * David Drinan <ddrinan@netscape.com>
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. const nsICRLManager = Components.interfaces.nsICRLManager;
  38. const nsCRLManager = "@mozilla.org/security/crlmanager;1";
  39. const nsICRLInfo = Components.interfaces.nsICRLInfo;
  40. const nsISupportsArray = Components.interfaces.nsISupportsArray;
  41. const nsIPKIParamBlock = Components.interfaces.nsIPKIParamBlock;
  42. const nsPKIParamBlock = "@mozilla.org/security/pkiparamblock;1";
  43. const nsIPrefService = Components.interfaces.nsIPrefService;
  44. var crlManager;
  45. var crls;
  46. var prefService;
  47. var prefBranch;
  48. var autoupdateEnabledBaseString = "security.crl.autoupdate.enable.";
  49. var autoupdateTimeTypeBaseString = "security.crl.autoupdate.timingType.";
  50. var autoupdateTimeBaseString = "security.crl.autoupdate.nextInstant.";
  51. var autoupdateURLBaseString = "security.crl.autoupdate.url.";
  52. var autoupdateErrCntBaseString = "security.crl.autoupdate.errCount.";
  53. var autoupdateErrDetailBaseString = "security.crl.autoupdate.errDetail.";
  54. var autoupdateDayCntString = "security.crl.autoupdate.dayCnt.";
  55. var autoupdateFreqCntString = "security.crl.autoupdate.freqCnt.";
  56. function onLoad()
  57. {
  58. var crlEntry;
  59. var i;
  60. crlManager = Components.classes[nsCRLManager].getService(nsICRLManager);
  61. crls = crlManager.getCrls();
  62. prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(nsIPrefService);
  63. prefBranch = prefService.getBranch(null);
  64. var bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  65. var autoupdateEnabledString;
  66. var autoupdateErrCntString;
  67. for (i=0; i<crls.length; i++) {
  68. crlEntry = crls.queryElementAt(i, nsICRLInfo);
  69. var org = crlEntry.organization;
  70. var orgUnit = crlEntry.organizationalUnit;
  71. var lastUpdate = crlEntry.lastUpdateLocale;
  72. var nextUpdate = crlEntry.nextUpdateLocale;
  73. autoupdateEnabledString = autoupdateEnabledBaseString + crlEntry.nameInDb;
  74. autoupdateErrCntString = autoupdateErrCntBaseString + crlEntry.nameInDb;
  75. var enabled = false;
  76. var enabledStr = bundle.GetStringFromName("crlAutoupdateNotEnabled");
  77. var status = "";
  78. try{
  79. enabled = prefBranch.getBoolPref(autoupdateEnabledString)
  80. if(enabled){
  81. enabledStr = bundle.GetStringFromName("crlAutoupdateEnabled");
  82. }
  83. var cnt;
  84. cnt = prefBranch.getIntPref(autoupdateErrCntString);
  85. if(cnt > 0){
  86. status = bundle.GetStringFromName("crlAutoupdateFailed");
  87. } else {
  88. status = bundle.GetStringFromName("crlAutoupdateOk");
  89. }
  90. }catch(exception){}
  91. AddItem("crlList", [org, orgUnit, lastUpdate, nextUpdate, enabledStr, status], "crltree_", i);
  92. }
  93. }
  94. function AddItem(children,cells,prefix,idfier)
  95. {
  96. var kids = document.getElementById(children);
  97. var item = document.createElement("treeitem");
  98. var row = document.createElement("treerow");
  99. for(var i = 0; i < cells.length; i++)
  100. {
  101. var cell = document.createElement("treecell");
  102. cell.setAttribute("class", "propertylist");
  103. cell.setAttribute("label", cells[i])
  104. row.appendChild(cell);
  105. }
  106. item.appendChild(row);
  107. item.setAttribute("id",prefix + idfier);
  108. kids.appendChild(item);
  109. }
  110. function DeleteCrlSelected() {
  111. var crlEntry;
  112. // delete selected item
  113. var crltree = document.getElementById("crltree");
  114. var i = crltree.currentIndex;
  115. if(i<0){
  116. return;
  117. }
  118. crlEntry = crls.queryElementAt(i, nsICRLInfo);
  119. var autoupdateEnabled = false;
  120. var autoupdateParamAvailable = false;
  121. var id = crlEntry.nameInDb;
  122. //First, check if autoupdate was enabled for this crl
  123. try {
  124. autoupdateEnabled = prefBranch.getBoolPref(autoupdateEnabledBaseString + id);
  125. //Note, if the pref is not present, we get an exception right here,
  126. //and autoupdateEnabled remains false
  127. autoupdateParamAvailable = true;
  128. prefBranch.clearUserPref(autoupdateEnabledBaseString + id);
  129. prefBranch.clearUserPref(autoupdateTimeTypeBaseString + id);
  130. prefBranch.clearUserPref(autoupdateTimeBaseString + id);
  131. prefBranch.clearUserPref(autoupdateURLBaseString + id);
  132. prefBranch.clearUserPref(autoupdateDayCntString + id);
  133. prefBranch.clearUserPref(autoupdateFreqCntString + id);
  134. prefBranch.clearUserPref(autoupdateErrCntBaseString + id);
  135. prefBranch.clearUserPref(autoupdateErrDetailBaseString + id);
  136. } catch(Exception){}
  137. //Once we have deleted the prefs that can be deleted, we save the
  138. //file if relevant, restart the scheduler, and once we are successful
  139. //in doind that, we try to delete the crl
  140. try{
  141. if(autoupdateParamAvailable){
  142. prefService.savePrefFile(null);
  143. }
  144. if(autoupdateEnabled){
  145. crlManager.rescheduleCRLAutoUpdate();
  146. }
  147. // Now, try to delete it
  148. crlManager.deleteCrl(i);
  149. DeleteItemSelected("crltree", "crltree_", "crlList");
  150. //To do: If delete fails, we should be able to retrieve the deleted
  151. //settings
  152. //XXXXXXXXXXXXXXXXXXXXX
  153. }catch(exception) {
  154. //To Do: Possibly show an error ...
  155. //XXXXXXXXXXXX
  156. }
  157. EnableCrlActions();
  158. }
  159. function EnableCrlActions() {
  160. var tree = document.getElementById("crltree");
  161. if (tree.view.selection.count) {
  162. document.getElementById("deleteCrl").removeAttribute("disabled");
  163. document.getElementById("editPrefs").removeAttribute("disabled");
  164. document.getElementById("updateCRL").removeAttribute("disabled");
  165. } else {
  166. document.getElementById("deleteCrl").setAttribute("disabled", "true");
  167. document.getElementById("editPrefs").setAttribute("disabled", "true");
  168. document.getElementById("updateCRL").setAttribute("disabled", "true");
  169. }
  170. }
  171. function DeleteItemSelected(tree, prefix, kids) {
  172. var i;
  173. var delnarray = [];
  174. var rv = "";
  175. var cookietree = document.getElementById(tree);
  176. var rangeCount = cookietree.view.selection.getRangeCount();
  177. for(i = 0; i < rangeCount; ++i)
  178. {
  179. var start = {}, end = {};
  180. cookietree.view.selection.getRangeAt(i, start, end);
  181. for (var k = start.value; k <= end.value; ++k) {
  182. var item = cookietree.contentView.getItemAtIndex(k);
  183. delnarray[i] = document.getElementById(item.id);
  184. var itemid = parseInt(item.id.substring(prefix.length, item.id.length));
  185. rv += (itemid + ",");
  186. }
  187. }
  188. for(i = 0; i < delnarray.length; i++)
  189. {
  190. document.getElementById(kids).removeChild(delnarray[i]);
  191. }
  192. return rv;
  193. }
  194. function EditAutoUpdatePrefs() {
  195. var crlEntry;
  196. // delete selected item
  197. var crltree = document.getElementById("crltree");
  198. var i = crltree.currentIndex;
  199. if(i<0){
  200. return;
  201. }
  202. crlEntry = crls.queryElementAt(i, nsICRLInfo);
  203. var params = Components.classes[nsPKIParamBlock].createInstance(nsIPKIParamBlock);
  204. params.setISupportAtIndex(1, crlEntry);
  205. window.openDialog("chrome://pippki/content/pref-crlupdate.xul","",
  206. "chrome,centerscreen,modal", params);
  207. }
  208. function UpdateCRL()
  209. {
  210. var crlEntry;
  211. var crltree = document.getElementById("crltree");
  212. var i = crltree.currentIndex;
  213. if(i<0){
  214. return;
  215. }
  216. crlEntry = crls.queryElementAt(i, nsICRLInfo);
  217. crlManager.updateCRLFromURL(crlEntry.lastFetchURL, crlEntry.nameInDb);
  218. }
  219. function ImportCRL()
  220. {
  221. // prompt for the URL to import from
  222. var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  223. var CRLLocation = {value:null};
  224. var dummy = { value: 0 };
  225. var strBundle = document.getElementById('pippki_bundle');
  226. var addCRL = promptService.prompt(window, strBundle.getString('crlImportNewCRLTitle'),
  227. strBundle.getString('crlImportNewCRLLabel'), CRLLocation, null, dummy);
  228. if (addCRL)
  229. crlManager.updateCRLFromURL(CRLLocation.value, "");
  230. }