-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpasshash-common.js
executable file
·811 lines (763 loc) · 31.7 KB
/
passhash-common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/* jslint browser: true */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Password Hasher
*
* The Initial Developer of the Original Code is Steve Cooper.
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): (none)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
var PassHashCommon = {
/* Artificial host name used for for saving to the password database */
host: "passhash.passhash",
log: function (msg) {
"use strict";
var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
consoleService.logStringMessage(msg);
},
loadOptions: function () {
"use strict";
var opts = this.createOptions();
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("passhash.");
var forceSave = false;
var elementKey;
if (prefs.prefHasUserValue("optSecurityLevel")) {
opts.securityLevel = prefs.getIntPref("optSecurityLevel");
opts.firstTime = false;
forceSave = true;
}
if (prefs.prefHasUserValue("optGuessSiteTag")) {
opts.guessSiteTag = prefs.getBoolPref("optGuessSiteTag");
}
if (prefs.prefHasUserValue("optRememberSiteTag")) {
opts.rememberSiteTag = prefs.getBoolPref("optRememberSiteTag");
}
if (prefs.prefHasUserValue("optRememberMasterKey")) {
opts.rememberMasterKey = prefs.getBoolPref("optRememberMasterKey");
}
if (prefs.prefHasUserValue("optRevealSiteTag")) {
opts.revealSiteTag = prefs.getBoolPref("optRevealSiteTag");
}
if (prefs.prefHasUserValue("optRevealHashWord")) {
opts.revealHashWord = prefs.getBoolPref("optRevealHashWord");
}
if (prefs.prefHasUserValue("optShowMarker")) {
opts.showMarker = prefs.getBoolPref("optShowMarker");
}
if (prefs.prefHasUserValue("optUnmaskMarker")) {
opts.unmaskMarker = prefs.getBoolPref("optUnmaskMarker");
}
if (prefs.prefHasUserValue("optGuessFullDomain")) {
opts.guessFullDomain = prefs.getBoolPref("optGuessFullDomain");
}
if (prefs.prefHasUserValue("optDigitDefault")) {
opts.digitDefault = prefs.getBoolPref("optDigitDefault");
}
if (prefs.prefHasUserValue("optPunctuationDefault")) {
opts.punctuationDefault = prefs.getBoolPref("optPunctuationDefault");
}
if (prefs.prefHasUserValue("optMixedCaseDefault")) {
opts.mixedCaseDefault = prefs.getBoolPref("optMixedCaseDefault");
}
if (prefs.prefHasUserValue("optHashWordSizeDefault")) {
opts.hashWordSizeDefault = prefs.getIntPref("optHashWordSizeDefault");
}
if (prefs.prefHasUserValue("optShortcutKeyCode")) {
opts.shortcutKeyCode = prefs.getCharPref("optShortcutKeyCode");
}
if (!opts.shortcutKeyCode) {
/* Set shortcut key to XUL-defined default. */
forceSave = true;
elementKey = document.getElementById("key_passhash");
if (elementKey !== null) {
opts.shortcutKeyCode = elementKey.getAttribute("key");
if (!opts.shortcutKeyCode) {
opts.shortcutKeyCode = elementKey.getAttribute("keycode");
}
}
}
if (prefs.prefHasUserValue("optShortcutKeyMods")) {
opts.shortcutKeyMods = prefs.getCharPref("optShortcutKeyMods");
}
if (!opts.shortcutKeyMods) {
/* Set shortcut modifiers to XUL-defined default. */
forceSave = true;
elementKey = document.getElementById("key_passhash");
if (elementKey !== null) {
opts.shortcutKeyMods = elementKey.getAttribute("modifiers");
}
}
/* Force saving options if the key options are not present to give them visibility */
if (forceSave) {
this.saveOptions(opts);
}
return opts;
},
createOptions: function () {
"use strict";
var opts = Object.create(null);
opts.securityLevel = 2;
opts.guessSiteTag = true;
opts.rememberSiteTag = true;
opts.rememberMasterKey = false;
opts.revealSiteTag = true;
opts.revealHashWord = false;
opts.showMarker = true;
opts.unmaskMarker = false;
opts.guessFullDomain = false;
opts.digitDefault = true;
opts.punctuationDefault = true;
opts.mixedCaseDefault = true;
opts.hashWordSizeDefault = 8;
opts.firstTime = true;
opts.shortcutKeyCode = "";
opts.shortcutKeyMods = "";
return opts;
},
saveOptions: function (opts) {
"use strict";
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("passhash.");
prefs.setIntPref("optSecurityLevel", opts.securityLevel);
prefs.setBoolPref("optGuessSiteTag", opts.guessSiteTag);
prefs.setBoolPref("optRememberSiteTag", opts.rememberSiteTag);
prefs.setBoolPref("optRememberMasterKey", opts.rememberMasterKey);
prefs.setBoolPref("optRevealSiteTag", opts.revealSiteTag);
prefs.setBoolPref("optRevealHashWord", opts.revealHashWord);
prefs.setBoolPref("optShowMarker", opts.showMarker);
prefs.setBoolPref("optUnmaskMarker", opts.unmaskMarker);
prefs.setBoolPref("optGuessFullDomain", opts.guessFullDomain);
prefs.setBoolPref("optDigitDefault", opts.digitDefault);
prefs.setBoolPref("optPunctuationDefault", opts.punctuationDefault);
prefs.setBoolPref("optMixedCaseDefault", opts.mixedCaseDefault);
prefs.setIntPref("optHashWordSizeDefault", opts.hashWordSizeDefault);
prefs.setCharPref("optShortcutKeyCode", opts.shortcutKeyCode);
prefs.setCharPref("optShortcutKeyMods", opts.shortcutKeyMods);
},
loadSecureValue: function (option, name, suffix, valueDefault) {
"use strict";
return (this.hasLoginManager()
? this.loadLoginManagerValue(option, name, suffix, valueDefault)
: this.loadPasswordManagerValue(option, name, suffix, valueDefault));
},
loadLoginManagerValue: function (option, name, suffix, valueDefault) {
"use strict";
var user = (suffix
? name + "-" + suffix
: name);
var value = valueDefault;
if (option && suffix !== null) {
var login = this.findLoginManagerUserLogin(user);
if (login !== null && login.password !== "" && login.password !== "n/a") {
value = login.password;
}
}
return value;
},
loadPasswordManagerValue: function (option, name, suffix, valueDefault) {
"use strict";
var user = (suffix
? name + "-" + suffix
: name);
var value = valueDefault;
var found = false;
if (option && suffix !== null) {
var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"].getService(Components.interfaces.nsIPasswordManager);
var e = passwordManager.enumerator;
var pass;
while (!found && e.hasMoreElements()) {
try {
pass = e.getNext().QueryInterface(Components.interfaces.nsIPassword);
if (pass.host === this.host && pass.user === user) {
value = pass.password;
found = true;
}
} catch (ex) {
throw (ex);
}
}
}
return value;
},
saveSecureValue: function (option, name, suffix, value) {
"use strict";
return (this.hasLoginManager()
? this.saveLoginManagerValue(option, name, suffix, value)
: this.savePasswordManagerValue(option, name, suffix, value));
},
saveLoginManagerValue: function (option, name, suffix, value) {
"use strict";
if (!value || suffix === null) {
return false;
}
var valueSave = (option
? value
: "n/a");
var user = (suffix
? name + "-" + suffix
: name);
var loginManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager);
var newLogin = Components.classes["@mozilla.org/login-manager/loginInfo;1"].createInstance(Components.interfaces.nsILoginInfo);
newLogin.init(this.host, "passhash", null, user, valueSave, "", "");
var currentLogin = this.findLoginManagerUserLogin(user);
if (currentLogin === null) {
loginManager.addLogin(newLogin);
} else {
loginManager.modifyLogin(currentLogin, newLogin);
}
return true;
},
savePasswordManagerValue: function (option, name, suffix, value) {
"use strict";
if (!value || suffix === null) {
return false;
}
var valueSave = (option
? value
: "");
var user = (suffix
? name + "-" + suffix
: name);
var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"].getService(Components.interfaces.nsIPasswordManager);
try {
/* Firefox 2 seems to lose info from subsequent addUser calls */
/* addUser on an existing host/user after restarting. */
passwordManager.removeUser(this.host, user);
} catch (ex) {
throw (ex);
}
passwordManager.addUser(this.host, user, valueSave);
return true;
},
hasLoginManager: function () {
"use strict";
return ("@mozilla.org/login-manager;1" in Components.classes);
},
findLoginManagerUserLogin: function (user) {
"use strict";
/* Find user from returned array of nsILoginInfo objects */
var logins = this.findAllLoginManagerLogins();
var i;
for (i = 0; i < logins.length; i = i + 1) {
if (logins[i].username === user) {
return logins[i];
}
}
return null;
},
findAllLoginManagerLogins: function () {
"use strict";
var loginManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager);
return loginManager.findLogins({}, this.host, "passhash", null);
},
/* There's probably a better way */
getDomain: function (input) {
"use strict";
var h = input.host.split(".");
if (h.length <= 1) {
return null;
}
/* Handle domains like co.uk */
if (h.length > 2 && h[h.length - 1].length === 2 && h[h.length - 2] === "co") {
return h[h.length - 3] + "." + h[h.length - 2] + "." + h[h.length - 1];
}
return h[h.length - 2] + "." + h[h.length - 1];
},
/* IMPORTANT: This function should be changed carefully. It must be
completely deterministic and consistent between releases. Otherwise
users would be forced to update their passwords. In other words, the
algorithm must always be backward-compatible. It's only acceptable to
violate backward compatibility when new options are used.
SECURITY: The optional adjustments are positioned and calculated based
on the sum of all character codes in the raw hash string. So it becomes
far more difficult to guess the injected special characters without
knowing the master key.
Is it ok to assume ASCII is ok for adjustments? */
generateHashWord: function (
siteTag,
masterKey,
hashWordSize,
requireDigit,
requirePunctuation,
requireMixedCase,
restrictSpecial,
restrictDigits
) {
"use strict";
/* Start with the SHA1-encrypted master key/site tag. */
/* var s = b64_hmac_sha1(masterKey, siteTag); */
var s = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA512(siteTag, masterKey));
/* Use the checksum of all characters as a pseudo-randomizing seed to
avoid making the injected characters easy to guess. Note that it
isn't random in the sense of not being deterministic (i.e.
repeatable). Must share the same seed between all injected
characters so that they are guaranteed unique positions based on
their offsets. */
var sum = 0;
var i;
for (i = 0; i < s.length; i = i + 1) {
sum += s.charCodeAt(i);
}
/* Restrict digits just does a mod 10 of all the characters */
if (restrictDigits) {
s = PassHashCommon.convertToDigits(s, sum, hashWordSize);
} else {
/* Inject digit, punctuation, and mixed case as needed. */
if (requireDigit) {
s = PassHashCommon.injectSpecialCharacter(s, 0, 4, sum, hashWordSize, 48, 10);
}
if (requirePunctuation && !restrictSpecial) {
s = PassHashCommon.injectSpecialCharacter(s, 1, 4, sum, hashWordSize, 33, 15);
}
if (requireMixedCase) {
s = PassHashCommon.injectSpecialCharacter(s, 2, 4, sum, hashWordSize, 65, 26);
s = PassHashCommon.injectSpecialCharacter(s, 3, 4, sum, hashWordSize, 97, 26);
}
/* Strip out special characters as needed. */
if (restrictSpecial) {
s = PassHashCommon.removeSpecialCharacters(s, sum, hashWordSize);
}
}
/* Trim it to size. */
return s.substr(0, hashWordSize);
},
/* This is a very specialized method to inject a character chosen from a
range of character codes into a block at the front of a string if one of
those characters is not already present.
Parameters:
sInput = input string
offset = offset for position of injected character
reserved = # of offsets reserved for special characters
seed = seed for pseudo-randomizing the position and injected character
lenOut = length of head of string that will eventually survive truncation.
cStart = character code for first valid injected character.
cNum = number of valid character codes starting from cStart. */
injectSpecialCharacter: function (sInput, offset, reserved, seed, lenOut, cStart, cNum) {
"use strict";
var pos0 = seed % lenOut;
var pos = (pos0 + offset) % lenOut;
/* Check if a qualified character is already present
Write the loop so that the reserved block is ignored. */
var i;
var i2;
var c;
for (i = 0; i < lenOut - reserved; i = i + 1) {
i2 = (pos0 + reserved + i) % lenOut;
c = sInput.charCodeAt(i2);
if (c >= cStart && c < cStart + cNum) {
return sInput; /* Already present - nothing to do */
}
}
var sHead = (pos > 0
? sInput.substring(0, pos)
: "");
var sInject = String.fromCharCode(((seed + sInput.charCodeAt(pos)) % cNum) + cStart);
var sTail = (pos + 1 < sInput.length
? sInput.substring(pos + 1, sInput.length)
: "");
return (sHead + sInject + sTail);
},
/* Another specialized method to replace a class of character, e.g.
punctuation, with plain letters and numbers.
Parameters:
sInput = input string
seed = seed for pseudo-randomizing the position and injected character
lenOut = length of head of string that will eventually survive truncation. */
removeSpecialCharacters: function (sInput, seed, lenOut) {
"use strict";
var s = "";
var i = 0;
var j;
while (i < lenOut) {
j = sInput.substring(i).search(/[^a-z0-9]/i);
if (j < 0) {
break;
}
if (j > 0) {
s += sInput.substring(i, i + j);
}
s += String.fromCharCode((seed + i) % 26 + 65);
i += (j + 1);
}
if (i < sInput.length) {
s += sInput.substring(i);
}
return s;
},
/* Convert input string to digits-only.
Parameters:
sInput = input string
seed = seed for pseudo-randomizing the position and injected character
lenOut = length of head of string that will eventually survive truncation. */
convertToDigits: function (sInput, seed, lenOut) {
"use strict";
var s = "";
var i = 0;
var j;
while (i < lenOut) {
j = sInput.substring(i).search(/[^0-9]/i);
if (j < 0) {
break;
}
if (j > 0) {
s += sInput.substring(i, i + j);
}
s += String.fromCharCode((seed + sInput.charCodeAt(i)) % 10 + 48);
i += (j + 1);
}
if (i < sInput.length) {
s += sInput.substring(i);
}
return s;
},
bumpSiteTag: function (siteTag) {
"use strict";
var tag = siteTag.replace(/^[\u0020\t]*(.*)[\u0020\t]*$/, "$1"); /* redundant */
if (tag) {
var splitTag = tag.match(/^(.*):([0-9]+)?$/);
if (splitTag === null || splitTag.length < 3) {
tag += ":1";
} else {
tag = splitTag[1] + ":" + (parseInt(splitTag[2]) + 1);
}
}
return tag;
},
/* Returns true if an HTML node is some kind of text field. */
isTextNode: function (node) {
"use strict";
try {
var name = node.localName.toUpperCase();
if (name === "TEXTAREA" || name === "TEXTBOX" || (name === "INPUT" && (node.type === "text" || node.type === "password"))) {
return true;
}
} catch (e) {
throw (e);
}
return false;
},
/* From Mozilla utilityOverlay.js
Can I access it directly? */
openUILinkIn: function (url, where) {
"use strict";
if (!where) {
return;
}
if ((url === null) || (url === "")) {
return;
}
/* xlate the URL if necessary */
if (url.indexOf("urn:") === 0) {
url = xlateURL(url); /* does RDF urn expansion */
}
/* avoid loading "", since this loads a directory listing */
if (url === "") {
url = "about:blank";
}
if (where === "save") {
saveURL(url, null, null, true);
return;
}
var w = (where === "window")
? null
: this.getTopWin();
if (!w) {
openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no", url);
return;
}
var browser = w.document.getElementById("content");
var tab;
switch (where) {
case "current":
browser.loadURI(url);
w.content.focus();
break;
case "tabshifted":
case "tab":
tab = browser.addTab(url);
if ((where === "tab") ^ this.getBoolPref("browser.tabs.loadBookmarksInBackground", false)) {
browser.selectedTab = tab;
w.content.focus();
}
break;
}
},
/* From Mozilla utilityOverlay.js */
getTopWin: function () {
"use strict";
var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
var topWindowOfType = windowManagerInterface.getMostRecentWindow("navigator:browser");
if (topWindowOfType) {
return topWindowOfType;
}
return null;
},
/* From Mozilla utilityOverlay.js */
getBoolPref: function (prefname, def) {
"use strict";
try {
var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
return pref.getBoolPref(prefname);
} catch (ex) {
return def;
}
},
/* Build an array sorted by domain name with properties populated, as */
/* available, for site tag, master key and options. */
getSavedEntries: function () {
/* Because of Javascript limitations on associative arrays, e.g. not
handling non-alphanumeric, we'll go to the trouble of building
separate sortable arrays of site tags, master keys and options using
domain/value objects. After sorting the three arrays we can walk
through them and build the returned array of fully-fleshed-out
objects. */
"use strict";
var siteTags = [];
var masterKeys = [];
var options = [];
if (this.hasLoginManager()) {
this.getAllLoginManagerEntries(siteTags, masterKeys, options);
} else {
this.getAllPasswordManagerEntries(siteTags, masterKeys, options);
}
var entries = [];
siteTags.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
masterKeys.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
options.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
var iSiteTag = 0;
var iMasterKey = 0;
var iOption = 0;
var next;
while (
iSiteTag < siteTags.length ||
iMasterKey < masterKeys.length ||
iOption < options.length
) {
/* Find the lowest domain name from the three waiting values */
next = null;
if (iSiteTag < siteTags.length && (next === null || siteTags[iSiteTag].name < next)) {
next = siteTags[iSiteTag].name;
}
if (iMasterKey < masterKeys.length && (next === null || masterKeys[iMasterKey].name < next)) {
next = masterKeys[iMasterKey].name;
}
if (iOption < options.length && (next === null || options[iOption].name < next)) {
next = options[iOption].name;
}
/* Grab all data with a matching domain name and advance the corresponding index */
entries[entries.length] = {name: next};
if (iSiteTag < siteTags.length && next === siteTags[iSiteTag].name) {
entries[entries.length - 1].siteTag = siteTags[iSiteTag].value;
iSiteTag += 1;
} else {
entries[entries.length - 1].siteTag = "";
}
if (iMasterKey < masterKeys.length && next === masterKeys[iMasterKey].name) {
entries[entries.length - 1].masterKey = masterKeys[iMasterKey].value;
iMasterKey += 1;
} else {
entries[entries.length - 1].masterKey = "";
}
if (iOption < options.length && next === options[iOption].name) {
entries[entries.length - 1].options = options[iOption].value;
iOption += 1;
} else {
entries[entries.length - 1].options = "";
}
}
return entries;
},
/* Gather all extension-related FF3 login manager entries. Return as 3
arrays for site tags, master keys and opt i o n s . */
getAllLoginManagerEntries: function (siteTags, masterKeys, options) {
"use strict";
var logins = this.findAllLoginManagerLogins();
var i;
var login;
var o;
for (i = 0; i < logins.length; i = i + 1) {
login = logins[i];
try {
if (login.hostname === this.host) {
if (login.username.indexOf("site-tag-") === 0) {
o = Object.create(null);
o.name = login.username.substring(9);
o.value = login.password;
siteTags[siteTags.length] = o;
} else {
if (login.username.indexOf("master-key-") === 0) {
o = Object.create(null);
o.name = login.username.substring(11);
o.value = login.password;
masterKeys[masterKeys.length] = o;
} else {
if (login.username.indexOf("options-") === 0) {
o = Object.create(null);
o.name = login.username.substring(8);
o.value = login.password;
options[options.length] = o;
}
}
}
}
} catch (e) {
throw (e);
}
}
},
/* Gather all extension-related FF2 login manager entries. Return as 3
arrays for site tags, master keys and options. */
getAllPasswordManagerEntries: function (siteTags, masterKeys, options) {
"use strict";
var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"].createInstance();
passwordManager.QueryInterface(Components.interfaces.nsIPasswordManager);
passwordManager.QueryInterface(Components.interfaces.nsIPasswordManagerInternal);
var passwordEnumerator = passwordManager.enumerator;
var o;
var pw;
while (passwordEnumerator.hasMoreElements()) {
try {
pw = passwordEnumerator.getNext().QueryInterface(Components.interfaces.nsIPasswordInternal);
if (pw.host === this.host) {
if (pw.user.indexOf("site-tag-") === 0) {
o = Object.create(null);
o.name = pw.user.substring(9);
o.value = pw.password;
siteTags[siteTags.length] = o;
} else {
if (pw.user.indexOf("master-key-") === 0) {
o = Object.create(null);
o.name = pw.user.substring(11);
o.value = pw.password;
masterKeys[masterKeys.length] = o;
} else {
if (pw.user.indexOf("options-") === 0) {
o = Object.create(null);
o.name = pw.user.substring(8);
o.value = pw.password;
options[options.length] = o;
}
}
}
}
} catch (e) {
throw (e);
}
}
},
getResourceFile: function (uri) {
"use strict";
var handler = Components.classes["@mozilla.org/network/protocol;1?name=file"].createInstance(Components.interfaces.nsIFileProtocolHandler);
var urlSrc = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURL);
urlSrc.spec = uri;
var chromeReg = Components.classes["@mozilla.org/chrome/chrome-registry;1"].getService(Components.interfaces.nsIChromeRegistry);
var urlIn = chromeReg.convertChromeURL(urlSrc);
return handler.getFileFromURLSpec(urlIn.spec);
},
openInputFile: function (fileIn) {
"use strict";
var streamIn = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
streamIn.init(fileIn, 0x01, "0444", 0);
streamIn.QueryInterface(Components.interfaces.nsILineInputStream);
return streamIn;
},
openOutputFile: function (fileOut) {
"use strict";
var streamOut = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
/* streamOut.init(fileOut, 0x02 | 0x08 | 0x20, 0664, 0); write, create, truncate */
return streamOut;
},
streamWriteLine: function (stream, line) {
"use strict";
stream.write(line, line.length);
stream.write("\n", 1);
},
/* Expand variables and return resulting string */
expandLine: function (lineIn) {
"use strict";
var strings = document.getElementById("pshOpt_strings");
var lineOut = "";
var splicePos = 0;
var re = /[$][{][\u0020\t]*([^\u0020}]+)[^}]*[}]/g;
var match;
while (match = re.exec(lineIn) !== null) {
lineOut += lineIn.substr(splicePos, match.index);
try {
lineOut += strings.getString(match[1]);
} catch (ex) {
alert("Couldn't find string \"" + match[1] + "\"");
lineOut += "???" + match[1] + "???";
}
splicePos = re.lastIndex;
}
lineOut += lineIn.substr(splicePos);
return lineOut;
},
/* Expand variables and write line to output stream */
streamWriteExpandedLine: function (stream, line) {
"use strict";
PassHashCommon.streamWriteLine(stream, PassHashCommon.expandLine(line));
},
browseFile: function (file, where) {
"use strict";
var handler = Components.classes["@mozilla.org/network/protocol;1?name=file"].createInstance(Components.interfaces.nsIFileProtocolHandler);
PassHashCommon.openUILinkIn(handler.getURLSpecFromFile(file), where);
},
pickHTMLFile: function (titleTag, defaultName) {
"use strict";
var title = document.getElementById("pshOpt_strings").getString(titleTag);
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var picker = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
if (defaultName) {
picker.defaultString = defaultName;
}
picker.appendFilters(nsIFilePicker.filterHTML);
picker.init(window, title, nsIFilePicker.modeSave);
var file;
var action;
do {
action = picker.show();
if (action === 1) {
return null;
}
file = picker.file;
if (!(/\.html{0,1}$/).test(picker.file.path)) {
file.initWithPath(picker.file.path + ".html");
}
picker.defaultString = file.leafName;
} while (file.exists() && (action === 0));
return file;
}
};