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