home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2006 January / PCA126_DVD.iso / internet / nsb-setup.exe / chrome / toolkit.jar / content / mozapps / autofill / masterpassword.js < prev    next >
Encoding:
Text File  |  2005-04-22  |  5.9 KB  |  233 lines

  1. const DEBUG = false;
  2. const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
  3. const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
  4. const nsIPK11Token = Components.interfaces.nsIPK11Token;
  5. const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
  6. const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
  7. const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
  8.  
  9. var gpk11dbService = null;
  10. var gToken = null;
  11.  
  12. var gkSetMasterPasswordStr = "";
  13. var gkChangeMasterPasswordStr = "";
  14. var gbStrLoaded = false;
  15.  
  16. /**
  17.  * Startup
  18.  **/
  19. function mpStartup()
  20. {
  21.     SetNoLoginTimeoutMsg();
  22.  
  23.     // retrieve Set Master Password text
  24.     if (!gbStrLoaded)
  25.     {
  26.         var bundle = document.getElementById("formfillpasscardBundle");
  27.         if (bundle != null)
  28.         {
  29.             gkSetMasterPasswordStr = bundle.getString("setMasterPasswordTitle");
  30.             gkChangeMasterPasswordStr = bundle.getString("changeMasterPasswordTitle");
  31.             gbStrLoaded = true;
  32.         }
  33.     }
  34.     //MERC - Durga: added try catch
  35.     try
  36.     {
  37.         // update Change Password button on Master Password panel
  38.         UpdateChangePasswordButtonText(GetMasterPasswordText());
  39.     }
  40.     catch(e)
  41.     {
  42.         mpDebug('Exception in mpStartup() -- masterpassword.js');
  43.     }
  44. }
  45.  
  46. /**
  47.  * Modify the Set/Change Master Password button text on the Master Password
  48.  * property page depending if the master password has already been set or not
  49.  **/
  50. function UpdateChangePasswordButtonText(label) 
  51. {
  52.     document.getElementById("changePasswordButton").setAttribute("label", label);
  53.     document.getElementById("changePasswordTitle").setAttribute("label", label);
  54. }
  55.  
  56. /**
  57.  * Set or Change the Master Password
  58.  **/
  59. function OnChangePassword() 
  60. {
  61.     // if master password already exists, pass empty string to use default text
  62.     var dialogTitle = GetMasterPasswordText();
  63.     
  64.     var obj = new Object( );
  65.      obj.res = "";
  66.     window.openDialog("chrome://browser/content/pref/pref-masterpass.xul",
  67.                     "_blank", "chrome,dialog,modal", dialogTitle, obj);
  68. // TODO JVL: alt
  69. //    window.openDialog("chrome://pippki/content/pref-masterpass.xul",
  70. //                    "_blank", "chrome,dialog,modal", dialogTitle);
  71.     
  72.     if (obj.res == "ok")
  73.     {
  74.         if (!IsOpenInDialog())
  75.         {
  76.             // update Change Password button on Master Password panel
  77.             UpdateChangePasswordButtonText(GetMasterPasswordText());
  78.         }
  79.     
  80.         // update the message for the timeout button and the passcard deck
  81.         if (!IsOpenInDialog())
  82.         {
  83.             SetNoLoginTimeoutMsg();
  84.         }
  85. // MERC JVL : Removed (4/14/05)        SetNoLoginPasscardMsg();
  86.  
  87.         if (!IsMasterPasswordSet())
  88.         {
  89.             UnprotectAllPasscards();
  90.         }
  91.  
  92.         return true;
  93.     }
  94.  
  95.     return false;
  96. }
  97.  
  98. /**
  99.  * Reset the Master Password if user forgets
  100.  **/
  101. function OnResetPassword() 
  102. {
  103.     var param = Components.classes["@mozilla.org/embedcomp/dialogparam;1"].getService();
  104.     param = param.QueryInterface(Components.interfaces.nsIDialogParamBlock);
  105.     var bundle = document.getElementById("formfillpasscardBundle");
  106.  
  107.     //No textbox
  108.     param.SetInt(3,0);
  109.     //2 buttons
  110.     param.SetInt(2,2);
  111.  
  112.     //title
  113.     param.SetString(12,bundle.getString("resetMasterPasswordTitle"));
  114.     //info
  115.     param.SetString(0,bundle.getString("resetWarning"));
  116.  
  117.     // set the icon
  118.     param.SetString(2, "question-icon");
  119.  
  120.     window.openDialog("chrome://global/content/commonDialog.xul",
  121.                         "_blank", "chrome,dialog,modal",param);
  122.                         
  123.     if(param.GetInt(0) == 0)
  124.     {
  125.         // Delete the Datacards and Passcards before reseting the Master
  126.         // Password because we still need the Master Password to decoded
  127.         // encrypted Datacards and Passcards prior to deletion
  128.         DeleteAllDatacards(true);
  129.         DeleteEncryptedSignons(true);
  130.  
  131.         try
  132.         {
  133.             GetMasterPasswordService().reset();
  134.             UpdateChangePasswordButtonText(gkSetMasterPasswordStr);
  135.             SetNoLoginTimeoutMsg()
  136.         }
  137.         catch (e)
  138.         {
  139.             mpDebug('Exception in OnResetPassword() -- masterpassword.js');
  140.         }
  141.     }
  142. }
  143.  
  144. /**
  145.  * Get the dialog title/button label text
  146.  **/
  147. function GetMasterPasswordText()
  148. {
  149.     return IsMasterPasswordSet() ? gkChangeMasterPasswordStr : gkSetMasterPasswordStr;
  150. }
  151.  
  152. /**
  153.  * Change the timeout value
  154.  **/
  155. function OnChangeTimeout()
  156. {
  157.     window.openDialog("chrome://mozapps/content/autofill/ChangeTimeoutDialog.xul",
  158.                             "_blank", "chrome,dialog,modal");
  159. }
  160.  
  161. /**
  162.  * Checks if the master password is set
  163.  **/
  164. function IsMasterPasswordSet()
  165. {
  166.     var bIsPasswordSet = false;
  167.     try
  168.     {
  169.         // return true if the master password is set to anything but an empty string
  170.         var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
  171.         var slot = secmoddb.findSlotByName("");
  172.         if (slot)
  173.         {
  174.             mpDebug('masterpassword.js : IsMasterPasswordSet() - Before check');
  175.             bIsPasswordSet = !(slot.status == nsIPKCS11Slot.SLOT_UNINITIALIZED ||
  176.                              slot.status == nsIPKCS11Slot.SLOT_READY);
  177.             mpDebug('masterpassword.js : IsMasterPasswordSet() - After check');
  178.         }
  179.     }
  180.     catch (ex)
  181.     {
  182.         /*
  183.         if the master password is not yet set and we check
  184.         for an empty string, it will throw an exception
  185.         (see http://lxr.mozilla.org/aviarybranch/source/security/manager/ssl/src/nsPK11TokenDB.cpp#296)
  186.         just default to false for now
  187.         */
  188.         mpDebug('Exception in IsMasterPasswordSet() -- masterpassword.js');
  189.         bIsPasswordSet = false;
  190.     }
  191.     mpDebug('IsMasterPasswordSet(): ' + bIsPasswordSet);
  192.     return bIsPasswordSet;
  193. }
  194.  
  195. /**
  196.  * Retrieves the master password service
  197.  **/
  198. function GetMasterPasswordService()
  199. {
  200.     if (gToken == null && gpk11dbService == null)
  201.     {
  202.         var tokenName = "";
  203.         gpk11dbService = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
  204.         if (gpk11dbService)
  205.             gToken = gpk11dbService.findTokenByName(tokenName);
  206.     }
  207.  
  208.     return gToken;
  209. }
  210.  
  211. /**
  212.  * Set the no login timeout message
  213.  **/
  214. function SetNoLoginTimeoutMsg()
  215. {
  216.     if (IsMasterPasswordSet())
  217.     {
  218.         document.getElementById('noLoginTimeoutMsg').setAttribute('hidden', 'true');
  219.         document.getElementById('timeoutPasswordButton').removeAttribute('disabled');
  220.     }
  221.     else
  222.     {
  223.         document.getElementById('noLoginTimeoutMsg').removeAttribute('hidden');
  224.         document.getElementById('timeoutPasswordButton').setAttribute('disabled', 'true');
  225.     }
  226. }
  227.  
  228. function mpDebug(str)
  229. {
  230.     if (DEBUG)
  231.         dump('MASTER PASSWORD: ' + str + '\n');
  232. }
  233.