home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / browser.jar / content / browser / pref / pref-masterpass.js < prev    next >
Encoding:
Text File  |  2005-07-29  |  7.2 KB  |  252 lines

  1.  
  2. const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
  3. const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
  4. const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
  5. const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
  6. const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
  7. const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
  8. const nsIPK11Token = Components.interfaces.nsIPK11Token;
  9.  
  10.  
  11. var params;
  12. var tokenName="";
  13. var pw1;
  14.  
  15. function onLoad()
  16. {
  17.   // MERC JVL BEGIN
  18.   var dialogTitle = window.arguments[0];
  19.   if (dialogTitle != null && dialogTitle.length > 0)
  20.     document.getElementById("set_password").setAttribute("title", dialogTitle);
  21.   // MERC END
  22.   
  23.   pw1 = document.getElementById("pw1");
  24.           
  25.   process();
  26. }
  27.  
  28.  
  29. function process()
  30. {
  31.    var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
  32.    var bundle = document.getElementById("prefBundle");
  33.  
  34.    // If the token is unitialized, don't use the old password box.
  35.    // Otherwise, do.
  36.  
  37.    var slot = secmoddb.findSlotByName(tokenName);
  38.    if (slot) {
  39.      var oldpwbox = document.getElementById("oldpw");
  40.      var msgBox = document.getElementById("message");
  41.      var status = slot.status;
  42.      if (status == nsIPKCS11Slot.SLOT_UNINITIALIZED
  43.          || status == nsIPKCS11Slot.SLOT_READY) {
  44.       
  45.        oldpwbox.setAttribute("hidden", "true");
  46.        msgBox.setAttribute("value", bundle.getString("password_not_set")); 
  47.        msgBox.setAttribute("hidden", "false");
  48.  
  49.        if (status == nsIPKCS11Slot.SLOT_READY) {
  50.          oldpwbox.setAttribute("inited", "empty");
  51.        } else {
  52.          oldpwbox.setAttribute("inited", "true");
  53.        }
  54.       
  55.        // Select first password field
  56.        document.getElementById('pw1').focus();
  57.     
  58.      } else {
  59.        // Select old password field
  60.        oldpwbox.setAttribute("hidden", "false");
  61.        msgBox.setAttribute("hidden", "true");
  62.        oldpwbox.setAttribute("inited", "false");
  63.        oldpwbox.focus();
  64.      }
  65.    }
  66.  
  67.   if (params) {
  68.     // Return value 0 means "canceled"
  69.     params.SetInt(1, 0);
  70.   }
  71.   
  72.   checkPasswords();
  73. }
  74.  
  75. function setPassword()
  76. {
  77.   var pk11db = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
  78.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  79.                                 .getService(Components.interfaces.nsIPromptService);
  80.   var token = pk11db.findTokenByName(tokenName);
  81.  
  82.   var oldpwbox = document.getElementById("oldpw");
  83.   var initpw = oldpwbox.getAttribute("inited");
  84.   var bundle = document.getElementById("prefBundle");
  85.   
  86.   var success = false;
  87.   
  88.   if (initpw == "false" || initpw == "empty") {
  89.     try {
  90.       var oldpw = "";
  91.       var passok = 0;
  92.       
  93.       if (initpw == "empty") {
  94.         passok = 1;
  95.       } else {
  96.         oldpw = oldpwbox.value;
  97.         passok = token.checkPassword(oldpw);
  98.       }
  99.       
  100.       if (passok) {
  101.         if (initpw == "empty" && pw1.value == "") {
  102.           // This makes no sense that we arrive here, 
  103.           // we reached a case that should have been prevented by checkPasswords.
  104.         } else {
  105.           if (pw1.value == "") {
  106.             var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
  107.             if (secmoddb.isFIPSEnabled) {
  108.               // empty passwords are not allowed in FIPS mode
  109.               promptService.alert(window,
  110.                                   bundle.getString("pw_change_failed_title"),
  111.                                   bundle.getString("pw_change2empty_in_fips_mode"));
  112.               passok = 0;
  113.             }
  114.           }
  115.           if (passok) {
  116.             token.changePassword(oldpw, pw1.value);
  117.             if (pw1.value == "") {
  118.               promptService.alert(window,
  119.                                   bundle.getString("pw_change_success_title"),
  120.                                   bundle.getString("pw_erased_ok") 
  121.                                   + " " + bundle.getString("pw_empty_warning"));
  122.             } else {
  123.               promptService.alert(window,
  124.                                   bundle.getString("pw_change_success_title"),
  125.                                   bundle.getString("pw_change_ok"));
  126.             }
  127.             success = true;
  128.           }
  129.         }
  130.       } else {
  131.         oldpwbox.focus();
  132.         oldpwbox.setAttribute("value", "");
  133.         promptService.alert(window,
  134.                             bundle.getString("pw_change_failed_title"),
  135.                             bundle.getString("incorrect_pw"));
  136.       }
  137.     } catch (e) {
  138.       promptService.alert(window,
  139.                           bundle.getString("pw_change_failed_title"),
  140.                           bundle.getString("failed_pw_change"));
  141.     }
  142.   } else {
  143.     token.initPassword(pw1.value);
  144.     if (pw1.value == "") {
  145.       promptService.alert(window,
  146.                           bundle.getString("pw_change_success_title"),
  147.                           bundle.getString("pw_not_wanted")
  148.                           + " " + bundle.getString("pw_empty_warning"));
  149.     }
  150.     success = true;
  151.   }
  152.  
  153.   // Terminate dialog
  154.   if (success)
  155.   {
  156.     //MERC - Durga
  157.     //Return "ok" to parent window (Save/Update passcard prompt dialogs)
  158.     if(window.arguments[1] != null)
  159.     {
  160.         try{
  161.             window.arguments[1].res = "ok";
  162.         }
  163.         catch(ex)
  164.         {
  165.             dump("Exception in setPassword - pref-masterpass.js");
  166.         }
  167.         window.close();
  168.     }
  169.   }
  170. }
  171.  
  172. function setPasswordStrength()
  173. {
  174. // Here is how we weigh the quality of the password
  175. // number of characters
  176. // numbers
  177. // non-alpha-numeric chars
  178. // upper and lower case characters
  179.  
  180.   var pw=document.getElementById('pw1').value;
  181.  
  182. //length of the password
  183.   var pwlength=(pw.length);
  184.   if (pwlength>5)
  185.     pwlength=5;
  186.  
  187.  
  188. //use of numbers in the password
  189.   var numnumeric = pw.replace (/[0-9]/g, "");
  190.   var numeric=(pw.length - numnumeric.length);
  191.   if (numeric>3)
  192.     numeric=3;
  193.  
  194. //use of symbols in the password
  195.   var symbols = pw.replace (/\W/g, "");
  196.   var numsymbols=(pw.length - symbols.length);
  197.   if (numsymbols>3)
  198.     numsymbols=3;
  199.  
  200. //use of uppercase in the password
  201.   var numupper = pw.replace (/[A-Z]/g, "");
  202.   var upper=(pw.length - numupper.length);
  203.   if (upper>3)
  204.     upper=3;
  205.  
  206.  
  207.   var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
  208.  
  209.   // make sure we're give a value between 0 and 100
  210.   if ( pwstrength < 0 ) {
  211.     pwstrength = 0;
  212.   }
  213.   
  214.   if ( pwstrength > 100 ) {
  215.     pwstrength = 100;
  216.   }
  217.  
  218.   var mymeter=document.getElementById('pwmeter');
  219.   mymeter.setAttribute("value",pwstrength);
  220.  
  221.   return;
  222. }
  223.  
  224. function checkPasswords()
  225. {
  226.   var pw1=document.getElementById('pw1').value;
  227.   var pw2=document.getElementById('pw2').value;
  228.   var ok=document.documentElement.getButton("accept");
  229.  
  230.   var oldpwbox = document.getElementById("oldpw");
  231.   if (oldpwbox) {
  232.     var initpw = oldpwbox.getAttribute("inited");
  233.  
  234.     if (initpw == "empty" && pw1 == "") {
  235.       // The token has already been initialized, therefore this dialog
  236.       // was called with the intention to change the password.
  237.       // The token currently uses an empty password.
  238.       // We will not allow changing the password from empty to empty.
  239.       ok.setAttribute("disabled","true");
  240.       return;
  241.     }
  242.   }
  243.  
  244.   if ((pw1 == pw2) && (pw1 != "" && pw2 != "")){
  245.     ok.setAttribute("disabled","false");
  246.   } else
  247.   {
  248.     ok.setAttribute("disabled","true");
  249.   }
  250.  
  251. }
  252.