toolkit/mozapps/preferences/changemp.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
     8 const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
     9 const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
    10 const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
    11 const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
    12 const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
    13 const nsIPK11Token = Components.interfaces.nsIPK11Token;
    16 var params;
    17 var tokenName="";
    18 var pw1;
    20 function init()
    21 {
    22   pw1 = document.getElementById("pw1");
    24   process();
    25 }
    28 function process()
    29 {
    30    var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
    31    var bundle = document.getElementById("bundlePreferences");
    33    // If the token is unitialized, don't use the old password box.
    34    // Otherwise, do.
    36    var slot = secmoddb.findSlotByName(tokenName);
    37    if (slot) {
    38      var oldpwbox = document.getElementById("oldpw");
    39      var msgBox = document.getElementById("message");
    40      var status = slot.status;
    41      if (status == nsIPKCS11Slot.SLOT_UNINITIALIZED
    42          || status == nsIPKCS11Slot.SLOT_READY) {
    44        oldpwbox.setAttribute("hidden", "true");
    45        msgBox.setAttribute("value", bundle.getString("password_not_set")); 
    46        msgBox.setAttribute("hidden", "false");
    48        if (status == nsIPKCS11Slot.SLOT_READY) {
    49          oldpwbox.setAttribute("inited", "empty");
    50        } else {
    51          oldpwbox.setAttribute("inited", "true");
    52        }
    54        // Select first password field
    55        document.getElementById('pw1').focus();
    57      } else {
    58        // Select old password field
    59        oldpwbox.setAttribute("hidden", "false");
    60        msgBox.setAttribute("hidden", "true");
    61        oldpwbox.setAttribute("inited", "false");
    62        oldpwbox.focus();
    63      }
    64    }
    66   if (params) {
    67     // Return value 0 means "canceled"
    68     params.SetInt(1, 0);
    69   }
    71   checkPasswords();
    72 }
    74 function setPassword()
    75 {
    76   var pk11db = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
    77   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
    78                                 .getService(Components.interfaces.nsIPromptService);
    79   var token = pk11db.findTokenByName(tokenName);
    80   dump("*** TOKEN!!!! (name = |" + token + "|\n");
    82   var oldpwbox = document.getElementById("oldpw");
    83   var initpw = oldpwbox.getAttribute("inited");
    84   var bundle = document.getElementById("bundlePreferences");
    86   var success = false;
    88   if (initpw == "false" || initpw == "empty") {
    89     try {
    90       var oldpw = "";
    91       var passok = 0;
    93       if (initpw == "empty") {
    94         passok = 1;
    95       } else {
    96         oldpw = oldpwbox.value;
    97         passok = token.checkPassword(oldpw);
    98       }
   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   }
   153   // Terminate dialog
   154   if (success)
   155     window.close();
   156 }
   158 function setPasswordStrength()
   159 {
   160 // Here is how we weigh the quality of the password
   161 // number of characters
   162 // numbers
   163 // non-alpha-numeric chars
   164 // upper and lower case characters
   166   var pw=document.getElementById('pw1').value;
   168 //length of the password
   169   var pwlength=(pw.length);
   170   if (pwlength>5)
   171     pwlength=5;
   174 //use of numbers in the password
   175   var numnumeric = pw.replace (/[0-9]/g, "");
   176   var numeric=(pw.length - numnumeric.length);
   177   if (numeric>3)
   178     numeric=3;
   180 //use of symbols in the password
   181   var symbols = pw.replace (/\W/g, "");
   182   var numsymbols=(pw.length - symbols.length);
   183   if (numsymbols>3)
   184     numsymbols=3;
   186 //use of uppercase in the password
   187   var numupper = pw.replace (/[A-Z]/g, "");
   188   var upper=(pw.length - numupper.length);
   189   if (upper>3)
   190     upper=3;
   193   var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
   195   // make sure we're give a value between 0 and 100
   196   if ( pwstrength < 0 ) {
   197     pwstrength = 0;
   198   }
   200   if ( pwstrength > 100 ) {
   201     pwstrength = 100;
   202   }
   204   var mymeter=document.getElementById('pwmeter');
   205   mymeter.value = pwstrength;
   207   return;
   208 }
   210 function checkPasswords()
   211 {
   212   var pw1=document.getElementById('pw1').value;
   213   var pw2=document.getElementById('pw2').value;
   214   var ok=document.documentElement.getButton("accept");
   216   var oldpwbox = document.getElementById("oldpw");
   217   if (oldpwbox) {
   218     var initpw = oldpwbox.getAttribute("inited");
   220     if (initpw == "empty" && pw1 == "") {
   221       // The token has already been initialized, therefore this dialog
   222       // was called with the intention to change the password.
   223       // The token currently uses an empty password.
   224       // We will not allow changing the password from empty to empty.
   225       ok.setAttribute("disabled","true");
   226       return;
   227     }
   228   }
   230   if (pw1 == pw2){
   231     ok.setAttribute("disabled","false");
   232   } else
   233   {
   234     ok.setAttribute("disabled","true");
   235   }
   237 }

mercurial