2007-12-11 05:38:53 +00:00
|
|
|
/* ***** 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 Bookmarks Sync.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
2008-06-12 19:36:58 +00:00
|
|
|
* Myk Melez <myk@mozilla.org>
|
2009-07-23 23:52:28 +00:00
|
|
|
* Anant Narayanan <anant@kix.in>
|
2011-07-14 19:11:42 +00:00
|
|
|
* Philipp von Weitershausen <philipp@weitershausen.de>
|
2010-11-30 00:41:17 +00:00
|
|
|
* Richard Newman <rnewman@mozilla.com>
|
2011-06-27 13:22:49 +00:00
|
|
|
* Marina Samuel <msamuel@mozilla.com>
|
2007-12-11 05:38:53 +00:00
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
2010-08-25 22:49:44 +00:00
|
|
|
// 'Weave' continues to be exported for backwards compatibility.
|
|
|
|
const EXPORTED_SYMBOLS = ["Service", "Weave"];
|
2007-12-11 05:38:53 +00:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2009-08-20 01:22:22 +00:00
|
|
|
// How long before refreshing the cluster
|
2009-09-25 22:05:09 +00:00
|
|
|
const CLUSTER_BACKOFF = 5 * 60 * 1000; // 5 minutes
|
2009-08-20 01:22:22 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// How long a key to generate from an old passphrase.
|
|
|
|
const PBKDF2_KEY_BYTES = 16;
|
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
const CRYPTO_COLLECTION = "crypto";
|
|
|
|
const KEYS_WBO = "keys";
|
|
|
|
|
2011-02-25 22:42:59 +00:00
|
|
|
const LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S";
|
|
|
|
|
2007-12-15 02:07:25 +00:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2011-01-19 00:23:30 +00:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
2010-06-16 21:30:08 +00:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
Cu.import("resource://services-sync/engines.js");
|
|
|
|
Cu.import("resource://services-sync/engines/clients.js");
|
2010-08-25 22:49:44 +00:00
|
|
|
Cu.import("resource://services-sync/ext/Preferences.js");
|
2010-06-16 21:30:08 +00:00
|
|
|
Cu.import("resource://services-sync/identity.js");
|
|
|
|
Cu.import("resource://services-sync/log4moz.js");
|
|
|
|
Cu.import("resource://services-sync/resource.js");
|
2011-07-14 19:11:42 +00:00
|
|
|
Cu.import("resource://services-sync/rest.js");
|
2010-06-16 21:30:08 +00:00
|
|
|
Cu.import("resource://services-sync/status.js");
|
2011-06-27 13:22:49 +00:00
|
|
|
Cu.import("resource://services-sync/policies.js");
|
2010-06-16 21:30:08 +00:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2010-08-25 22:49:44 +00:00
|
|
|
Cu.import("resource://services-sync/main.js");
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2011-07-14 19:11:42 +00:00
|
|
|
const STORAGE_INFO_TYPES = [INFO_COLLECTIONS,
|
|
|
|
INFO_COLLECTION_USAGE,
|
|
|
|
INFO_COLLECTION_COUNTS,
|
|
|
|
INFO_QUOTA];
|
|
|
|
|
2007-12-11 05:38:53 +00:00
|
|
|
/*
|
|
|
|
* Service singleton
|
|
|
|
* Main entry point into Weave's sync framework
|
|
|
|
*/
|
|
|
|
|
2009-02-22 08:04:58 +00:00
|
|
|
function WeaveSvc() {
|
2009-06-06 05:21:14 +00:00
|
|
|
this._notify = Utils.notify("weave:service:");
|
2009-02-22 08:04:58 +00:00
|
|
|
}
|
2008-03-25 22:14:00 +00:00
|
|
|
WeaveSvc.prototype = {
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2009-06-06 18:52:49 +00:00
|
|
|
_lock: Utils.lock,
|
2010-06-04 16:05:55 +00:00
|
|
|
_locked: false,
|
2008-06-12 19:36:58 +00:00
|
|
|
_loggedIn: false,
|
2009-03-25 02:23:53 +00:00
|
|
|
|
2010-09-13 15:17:37 +00:00
|
|
|
get account() Svc.Prefs.get("account", this.username),
|
|
|
|
set account(value) {
|
|
|
|
if (value) {
|
|
|
|
value = value.toLowerCase();
|
|
|
|
Svc.Prefs.set("account", value);
|
|
|
|
} else {
|
|
|
|
Svc.Prefs.reset("account");
|
|
|
|
}
|
|
|
|
this.username = this._usernameFromAccount(value);
|
|
|
|
},
|
|
|
|
|
|
|
|
_usernameFromAccount: function _usernameFromAccount(value) {
|
|
|
|
// If we encounter characters not allowed by the API (as found for
|
|
|
|
// instance in an email address), hash the value.
|
|
|
|
if (value && value.match(/[^A-Z0-9._-]/i))
|
|
|
|
return Utils.sha1Base32(value.toLowerCase()).toLowerCase();
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
2007-12-11 05:38:53 +00:00
|
|
|
get username() {
|
2009-12-03 02:25:18 +00:00
|
|
|
return Svc.Prefs.get("username", "").toLowerCase();
|
2007-12-11 05:38:53 +00:00
|
|
|
},
|
|
|
|
set username(value) {
|
2009-12-03 02:25:18 +00:00
|
|
|
if (value) {
|
|
|
|
// Make sure all uses of this new username is lowercase
|
|
|
|
value = value.toLowerCase();
|
2009-02-10 08:56:42 +00:00
|
|
|
Svc.Prefs.set("username", value);
|
2009-12-03 02:25:18 +00:00
|
|
|
}
|
2007-12-20 20:18:41 +00:00
|
|
|
else
|
2009-02-10 08:56:42 +00:00
|
|
|
Svc.Prefs.reset("username");
|
2007-12-20 20:18:41 +00:00
|
|
|
|
2007-12-11 05:38:53 +00:00
|
|
|
// fixme - need to loop over all Identity objects - needs some rethinking...
|
2008-04-15 01:53:35 +00:00
|
|
|
ID.get('WeaveID').username = value;
|
|
|
|
ID.get('WeaveCryptoID').username = value;
|
2008-12-02 02:43:43 +00:00
|
|
|
|
2008-12-02 02:58:28 +00:00
|
|
|
// FIXME: need to also call this whenever the username pref changes
|
2009-09-22 00:13:41 +00:00
|
|
|
this._updateCachedURLs();
|
2007-12-11 05:38:53 +00:00
|
|
|
},
|
|
|
|
|
2010-04-26 18:37:12 +00:00
|
|
|
get password() ID.get("WeaveID").password,
|
|
|
|
set password(value) ID.get("WeaveID").password = value,
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
get passphrase() ID.get("WeaveCryptoID").keyStr,
|
|
|
|
set passphrase(value) ID.get("WeaveCryptoID").keyStr = value,
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
get syncKeyBundle() ID.get("WeaveCryptoID"),
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2009-09-22 00:13:41 +00:00
|
|
|
get serverURL() Svc.Prefs.get("serverURL"),
|
2009-09-22 08:00:43 +00:00
|
|
|
set serverURL(value) {
|
|
|
|
// Only do work if it's actually changing
|
|
|
|
if (value == this.serverURL)
|
|
|
|
return;
|
2009-11-20 22:34:20 +00:00
|
|
|
|
2009-09-22 08:00:43 +00:00
|
|
|
// A new server most likely uses a different cluster, so clear that
|
|
|
|
Svc.Prefs.set("serverURL", value);
|
|
|
|
Svc.Prefs.reset("clusterURL");
|
|
|
|
},
|
2009-08-26 22:01:28 +00:00
|
|
|
|
2009-09-22 01:03:56 +00:00
|
|
|
get clusterURL() Svc.Prefs.get("clusterURL", ""),
|
2009-02-10 08:56:42 +00:00
|
|
|
set clusterURL(value) {
|
|
|
|
Svc.Prefs.set("clusterURL", value);
|
2009-09-22 00:13:41 +00:00
|
|
|
this._updateCachedURLs();
|
2008-11-07 01:37:17 +00:00
|
|
|
},
|
|
|
|
|
2009-09-22 00:13:41 +00:00
|
|
|
get miscAPI() {
|
|
|
|
// Append to the serverURL if it's a relative fragment
|
|
|
|
let misc = Svc.Prefs.get("miscURL");
|
|
|
|
if (misc.indexOf(":") == -1)
|
|
|
|
misc = this.serverURL + misc;
|
2011-04-08 21:52:08 +00:00
|
|
|
return misc + MISC_API_VERSION + "/";
|
2009-09-22 00:13:41 +00:00
|
|
|
},
|
2009-08-26 00:15:36 +00:00
|
|
|
|
2009-09-22 00:13:41 +00:00
|
|
|
get userAPI() {
|
|
|
|
// Append to the serverURL if it's a relative fragment
|
|
|
|
let user = Svc.Prefs.get("userURL");
|
|
|
|
if (user.indexOf(":") == -1)
|
|
|
|
user = this.serverURL + user;
|
2011-04-08 21:52:08 +00:00
|
|
|
return user + USER_API_VERSION + "/";
|
2009-09-22 00:13:41 +00:00
|
|
|
},
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2010-04-08 21:03:10 +00:00
|
|
|
get pwResetURL() {
|
|
|
|
return this.serverURL + "weave-password-reset";
|
|
|
|
},
|
|
|
|
|
2010-05-21 21:41:19 +00:00
|
|
|
get updatedURL() {
|
|
|
|
return WEAVE_CHANNEL == "dev" ? UPDATED_DEV_URL : UPDATED_REL_URL;
|
|
|
|
},
|
|
|
|
|
2010-03-25 16:52:45 +00:00
|
|
|
get syncID() {
|
|
|
|
// Generate a random syncID id we don't have one
|
|
|
|
let syncID = Svc.Prefs.get("client.syncID", "");
|
|
|
|
return syncID == "" ? this.syncID = Utils.makeGUID() : syncID;
|
|
|
|
},
|
|
|
|
set syncID(value) {
|
|
|
|
Svc.Prefs.set("client.syncID", value);
|
|
|
|
},
|
|
|
|
|
2009-02-12 06:08:56 +00:00
|
|
|
get isLoggedIn() { return this._loggedIn; },
|
2008-07-10 00:17:24 +00:00
|
|
|
|
2009-02-12 06:08:56 +00:00
|
|
|
get locked() { return this._locked; },
|
2008-11-03 22:59:45 +00:00
|
|
|
lock: function Svc_lock() {
|
|
|
|
if (this._locked)
|
|
|
|
return false;
|
|
|
|
this._locked = true;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
unlock: function Svc_unlock() {
|
|
|
|
this._locked = false;
|
2008-05-23 19:08:03 +00:00
|
|
|
},
|
|
|
|
|
2011-01-11 05:51:29 +00:00
|
|
|
// A specialized variant of Utils.catch.
|
|
|
|
// This provides a more informative error message when we're already syncing:
|
|
|
|
// see Bug 616568.
|
|
|
|
_catch: function _catch(func) {
|
|
|
|
function lockExceptions(ex) {
|
|
|
|
if (Utils.isLockException(ex)) {
|
|
|
|
// This only happens if we're syncing already.
|
|
|
|
this._log.info("Cannot start sync: already syncing?");
|
|
|
|
}
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-01-11 05:51:29 +00:00
|
|
|
return Utils.catch.call(this, func, lockExceptions);
|
|
|
|
},
|
|
|
|
|
2009-09-22 00:13:41 +00:00
|
|
|
_updateCachedURLs: function _updateCachedURLs() {
|
2009-09-22 01:03:56 +00:00
|
|
|
// Nothing to cache yet if we don't have the building blocks
|
|
|
|
if (this.clusterURL == "" || this.username == "")
|
|
|
|
return;
|
|
|
|
|
2011-04-08 21:52:08 +00:00
|
|
|
let storageAPI = this.clusterURL + SYNC_API_VERSION + "/";
|
2010-09-11 16:39:21 +00:00
|
|
|
this.userBaseURL = storageAPI + this.username + "/";
|
|
|
|
this._log.debug("Caching URLs under storage user base: " + this.userBaseURL);
|
2009-09-22 00:13:41 +00:00
|
|
|
|
|
|
|
// Generate and cache various URLs under the storage API for this user
|
2010-09-11 16:39:21 +00:00
|
|
|
this.infoURL = this.userBaseURL + "info/collections";
|
|
|
|
this.storageURL = this.userBaseURL + "storage/";
|
2009-09-22 00:13:41 +00:00
|
|
|
this.metaURL = this.storageURL + "meta/global";
|
2011-04-19 19:35:04 +00:00
|
|
|
this.cryptoKeysURL = this.storageURL + CRYPTO_COLLECTION + "/" + KEYS_WBO;
|
2008-12-02 02:43:43 +00:00
|
|
|
},
|
|
|
|
|
2009-02-10 08:56:42 +00:00
|
|
|
_checkCrypto: function WeaveSvc__checkCrypto() {
|
|
|
|
let ok = false;
|
|
|
|
|
|
|
|
try {
|
2009-02-11 21:38:50 +00:00
|
|
|
let iv = Svc.Crypto.generateRandomIV();
|
2009-02-10 08:56:42 +00:00
|
|
|
if (iv.length == 24)
|
2009-03-12 15:34:12 +00:00
|
|
|
ok = true;
|
2009-02-10 08:56:42 +00:00
|
|
|
|
2009-02-11 21:38:50 +00:00
|
|
|
} catch (e) {
|
|
|
|
this._log.debug("Crypto check failed: " + e);
|
|
|
|
}
|
2009-02-10 08:56:42 +00:00
|
|
|
|
|
|
|
return ok;
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
/**
|
|
|
|
* Here is a disgusting yet reasonable way of handling HMAC errors deep in
|
|
|
|
* the guts of Sync. The astute reader will note that this is a hacky way of
|
|
|
|
* implementing something like continuable conditions.
|
2011-04-19 19:33:47 +00:00
|
|
|
*
|
2010-12-10 07:06:44 +00:00
|
|
|
* A handler function is glued to each engine. If the engine discovers an
|
|
|
|
* HMAC failure, we fetch keys from the server and update our keys, just as
|
|
|
|
* we would on startup.
|
2011-04-19 19:33:47 +00:00
|
|
|
*
|
2010-12-10 07:06:44 +00:00
|
|
|
* If our key collection changed, we signal to the engine (via our return
|
|
|
|
* value) that it should retry decryption.
|
2011-04-19 19:33:47 +00:00
|
|
|
*
|
2010-12-10 07:06:44 +00:00
|
|
|
* If our key collection did not change, it means that we already had the
|
|
|
|
* correct keys... and thus a different client has the wrong ones. Reupload
|
|
|
|
* the bundle that we fetched, which will bump the modified time on the
|
|
|
|
* server and (we hope) prompt a broken client to fix itself.
|
2011-04-19 19:33:47 +00:00
|
|
|
*
|
2010-12-10 07:06:44 +00:00
|
|
|
* We keep track of the time at which we last applied this reasoning, because
|
|
|
|
* thrashing doesn't solve anything. We keep a reasonable interval between
|
|
|
|
* these remedial actions.
|
|
|
|
*/
|
|
|
|
lastHMACEvent: 0,
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
/*
|
|
|
|
* Returns whether to try again.
|
|
|
|
*/
|
|
|
|
handleHMACEvent: function handleHMACEvent() {
|
|
|
|
let now = Date.now();
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
// Leave a sizable delay between HMAC recovery attempts. This gives us
|
|
|
|
// time for another client to fix themselves if we touch the record.
|
|
|
|
if ((now - this.lastHMACEvent) < HMAC_EVENT_INTERVAL)
|
|
|
|
return false;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
this._log.info("Bad HMAC event detected. Attempting recovery " +
|
|
|
|
"or signaling to other clients.");
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
// Set the last handled time so that we don't act again.
|
|
|
|
this.lastHMACEvent = now;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
// Fetch keys.
|
2011-04-19 19:35:04 +00:00
|
|
|
let cryptoKeys = new CryptoWrapper(CRYPTO_COLLECTION, KEYS_WBO);
|
2010-12-10 07:06:44 +00:00
|
|
|
try {
|
|
|
|
let cryptoResp = cryptoKeys.fetch(this.cryptoKeysURL).response;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
// Save out the ciphertext for when we reupload. If there's a bug in
|
|
|
|
// CollectionKeys, this will prevent us from uploading junk.
|
|
|
|
let cipherText = cryptoKeys.ciphertext;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
if (!cryptoResp.success) {
|
|
|
|
this._log.warn("Failed to download keys.");
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
let keysChanged = this.handleFetchedKeys(this.syncKeyBundle,
|
|
|
|
cryptoKeys, true);
|
|
|
|
if (keysChanged) {
|
|
|
|
// Did they change? If so, carry on.
|
|
|
|
this._log.info("Suggesting retry.");
|
|
|
|
return true; // Try again.
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
// If not, reupload them and continue the current sync.
|
|
|
|
cryptoKeys.ciphertext = cipherText;
|
|
|
|
cryptoKeys.cleartext = null;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
let uploadResp = cryptoKeys.upload(this.cryptoKeysURL);
|
|
|
|
if (uploadResp.success)
|
|
|
|
this._log.info("Successfully re-uploaded keys. Continuing sync.");
|
|
|
|
else
|
|
|
|
this._log.warn("Got error response re-uploading keys. " +
|
|
|
|
"Continuing sync; let's try again later.");
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
return false; // Don't try again: same keys.
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Got exception \"" + ex + "\" fetching and handling " +
|
|
|
|
"crypto keys. Will try again later.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
handleFetchedKeys: function handleFetchedKeys(syncKey, cryptoKeys, skipReset) {
|
|
|
|
// Don't want to wipe if we're just starting up!
|
|
|
|
// This is largely relevant because we don't persist
|
|
|
|
// CollectionKeys yet: Bug 610913.
|
|
|
|
let wasBlank = CollectionKeys.isClear;
|
|
|
|
let keysChanged = CollectionKeys.updateContents(syncKey, cryptoKeys);
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
if (keysChanged && !wasBlank) {
|
|
|
|
this._log.debug("Keys changed: " + JSON.stringify(keysChanged));
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 07:06:44 +00:00
|
|
|
if (!skipReset) {
|
|
|
|
this._log.info("Resetting client to reflect key change.");
|
|
|
|
|
|
|
|
if (keysChanged.length) {
|
|
|
|
// Collection keys only. Reset individual engines.
|
|
|
|
this.resetClient(keysChanged);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Default key changed: wipe it all.
|
|
|
|
this.resetClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("Downloaded new keys, client reset. Proceeding.");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-04-19 19:33:47 +00:00
|
|
|
},
|
2009-02-10 08:56:42 +00:00
|
|
|
|
2009-09-25 15:13:12 +00:00
|
|
|
/**
|
|
|
|
* Prepare to initialize the rest of Weave after waiting a little bit
|
|
|
|
*/
|
|
|
|
onStartup: function onStartup() {
|
2010-06-16 21:30:13 +00:00
|
|
|
this._migratePrefs();
|
2011-08-26 21:01:35 +00:00
|
|
|
ErrorHandler.init();
|
|
|
|
|
|
|
|
this._log = Log4Moz.repository.getLogger("Sync.Service");
|
|
|
|
this._log.level =
|
|
|
|
Log4Moz.Level[Svc.Prefs.get("log.logger.service.main")];
|
|
|
|
|
2010-04-01 22:16:19 +00:00
|
|
|
this._log.info("Loading Weave " + WEAVE_VERSION);
|
2009-09-25 15:13:12 +00:00
|
|
|
|
2009-11-04 23:27:08 +00:00
|
|
|
this.enabled = true;
|
2009-10-07 17:47:55 +00:00
|
|
|
|
2009-04-08 19:39:14 +00:00
|
|
|
this._registerEngines();
|
2009-03-25 02:23:53 +00:00
|
|
|
|
2009-02-10 08:56:42 +00:00
|
|
|
let ua = Cc["@mozilla.org/network/protocol;1?name=http"].
|
|
|
|
getService(Ci.nsIHttpProtocolHandler).userAgent;
|
|
|
|
this._log.info(ua);
|
|
|
|
|
|
|
|
if (!this._checkCrypto()) {
|
|
|
|
this.enabled = false;
|
2010-08-13 03:31:00 +00:00
|
|
|
this._log.info("Could not load the Weave crypto component. Disabling " +
|
2009-03-12 15:34:12 +00:00
|
|
|
"Weave, since it will not work correctly.");
|
2009-02-10 08:56:42 +00:00
|
|
|
}
|
2008-11-07 01:37:17 +00:00
|
|
|
|
2010-08-06 15:30:58 +00:00
|
|
|
Svc.Obs.add("weave:service:setup-complete", this);
|
2010-09-07 16:44:01 +00:00
|
|
|
Svc.Prefs.observe("engine.", this);
|
2008-11-07 01:37:17 +00:00
|
|
|
|
2011-06-27 13:22:49 +00:00
|
|
|
SyncScheduler.init();
|
|
|
|
|
2008-11-07 01:37:17 +00:00
|
|
|
if (!this.enabled)
|
|
|
|
this._log.info("Weave Sync disabled");
|
|
|
|
|
|
|
|
// Create Weave identities (for logging in, and for encryption)
|
2010-08-25 22:49:45 +00:00
|
|
|
let id = ID.get("WeaveID");
|
|
|
|
if (!id)
|
|
|
|
id = ID.set("WeaveID", new Identity(PWDMGR_PASSWORD_REALM, this.username));
|
|
|
|
Auth.defaultAuthenticator = new BasicAuthenticator(id);
|
2008-11-07 01:37:17 +00:00
|
|
|
|
2010-08-25 22:49:45 +00:00
|
|
|
if (!ID.get("WeaveCryptoID"))
|
|
|
|
ID.set("WeaveCryptoID",
|
2010-11-30 00:41:17 +00:00
|
|
|
new SyncKeyBundle(PWDMGR_PASSPHRASE_REALM, this.username));
|
2008-11-07 01:37:17 +00:00
|
|
|
|
2009-09-22 00:13:41 +00:00
|
|
|
this._updateCachedURLs();
|
2008-11-07 01:37:17 +00:00
|
|
|
|
2010-08-06 15:30:58 +00:00
|
|
|
let status = this._checkSetup();
|
|
|
|
if (status != STATUS_DISABLED && status != CLIENT_NOT_CONFIGURED)
|
|
|
|
Svc.Obs.notify("weave:engine:start-tracking");
|
|
|
|
|
2010-06-11 00:04:49 +00:00
|
|
|
// Send an event now that Weave service is ready. We don't do this
|
2011-02-17 21:41:13 +00:00
|
|
|
// synchronously so that observers can import this module before
|
|
|
|
// registering an observer.
|
2011-06-06 19:27:36 +00:00
|
|
|
Utils.nextTick(function() {
|
2011-02-17 21:41:13 +00:00
|
|
|
Status.ready = true;
|
|
|
|
Svc.Obs.notify("weave:service:ready");
|
2011-06-06 19:27:36 +00:00
|
|
|
});
|
2008-11-07 01:37:17 +00:00
|
|
|
},
|
|
|
|
|
2010-04-09 17:20:41 +00:00
|
|
|
_checkSetup: function WeaveSvc__checkSetup() {
|
2010-08-25 22:49:45 +00:00
|
|
|
if (!this.enabled)
|
|
|
|
return Status.service = STATUS_DISABLED;
|
|
|
|
return Status.checkSetup();
|
2010-04-09 17:20:41 +00:00
|
|
|
},
|
|
|
|
|
2010-06-16 21:30:13 +00:00
|
|
|
_migratePrefs: function _migratePrefs() {
|
2011-06-13 18:39:51 +00:00
|
|
|
// Migrate old debugLog prefs.
|
2011-06-14 08:41:36 +00:00
|
|
|
let logLevel = Svc.Prefs.get("log.appender.debugLog");
|
2011-06-13 18:39:51 +00:00
|
|
|
if (logLevel) {
|
2011-06-14 08:41:36 +00:00
|
|
|
Svc.Prefs.set("log.appender.file.level", logLevel);
|
|
|
|
Svc.Prefs.reset("log.appender.debugLog");
|
2011-06-13 18:39:51 +00:00
|
|
|
}
|
|
|
|
if (Svc.Prefs.get("log.appender.debugLog.enabled")) {
|
2011-06-14 08:41:36 +00:00
|
|
|
Svc.Prefs.set("log.appender.file.logOnSuccess", true);
|
|
|
|
Svc.Prefs.reset("log.appender.debugLog.enabled");
|
2011-06-13 18:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate old extensions.weave.* prefs if we haven't already tried.
|
2010-06-16 21:30:13 +00:00
|
|
|
if (Svc.Prefs.get("migrated", false))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Grab the list of old pref names
|
|
|
|
let oldPrefBranch = "extensions.weave.";
|
|
|
|
let oldPrefNames = Cc["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Ci.nsIPrefService).
|
|
|
|
getBranch(oldPrefBranch).
|
|
|
|
getChildList("", {});
|
|
|
|
|
|
|
|
// Map each old pref to the current pref branch
|
|
|
|
let oldPref = new Preferences(oldPrefBranch);
|
|
|
|
for each (let pref in oldPrefNames)
|
|
|
|
Svc.Prefs.set(pref, oldPref.get(pref));
|
|
|
|
|
|
|
|
// Remove all the old prefs and remember that we've migrated
|
|
|
|
oldPref.resetBranch("");
|
|
|
|
Svc.Prefs.set("migrated", true);
|
|
|
|
},
|
|
|
|
|
2009-04-08 19:39:14 +00:00
|
|
|
/**
|
|
|
|
* Register the built-in engines for certain applications
|
|
|
|
*/
|
|
|
|
_registerEngines: function WeaveSvc__registerEngines() {
|
|
|
|
let engines = [];
|
2010-06-10 00:22:03 +00:00
|
|
|
// Applications can provide this preference (comma-separated list)
|
|
|
|
// to specify which engines should be registered on startup.
|
|
|
|
let pref = Svc.Prefs.get("registerEngines");
|
|
|
|
if (pref) {
|
|
|
|
engines = pref.split(",");
|
2009-04-08 19:39:14 +00:00
|
|
|
}
|
|
|
|
|
2010-06-16 22:11:40 +00:00
|
|
|
// Grab the actual engines and register them
|
2009-04-08 19:39:14 +00:00
|
|
|
Engines.register(engines.map(function(name) Weave[name + "Engine"]));
|
|
|
|
},
|
|
|
|
|
2008-06-23 23:10:31 +00:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
2007-12-11 05:38:53 +00:00
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
|
2008-06-30 21:00:06 +00:00
|
|
|
observe: function WeaveSvc__observe(subject, topic, data) {
|
2008-06-23 23:10:31 +00:00
|
|
|
switch (topic) {
|
2010-08-06 15:30:58 +00:00
|
|
|
case "weave:service:setup-complete":
|
|
|
|
let status = this._checkSetup();
|
|
|
|
if (status != STATUS_DISABLED && status != CLIENT_NOT_CONFIGURED)
|
|
|
|
Svc.Obs.notify("weave:engine:start-tracking");
|
|
|
|
break;
|
2010-09-07 16:44:01 +00:00
|
|
|
case "nsPref:changed":
|
|
|
|
if (this._ignorePrefObserver)
|
|
|
|
return;
|
|
|
|
let engine = data.slice((PREFS_BRANCH + "engine.").length);
|
|
|
|
this._handleEngineStatusChanged(engine);
|
|
|
|
break;
|
2008-06-23 23:10:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-09-07 16:44:01 +00:00
|
|
|
_handleEngineStatusChanged: function handleEngineDisabled(engine) {
|
|
|
|
this._log.trace("Status for " + engine + " engine changed.");
|
|
|
|
if (Svc.Prefs.get("engineStatusChanged." + engine, false)) {
|
|
|
|
// The enabled status being changed back to what it was before.
|
|
|
|
Svc.Prefs.reset("engineStatusChanged." + engine);
|
|
|
|
} else {
|
|
|
|
// Remember that the engine status changed locally until the next sync.
|
|
|
|
Svc.Prefs.set("engineStatusChanged." + engine, true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-03-27 07:46:10 +00:00
|
|
|
// gets cluster from central LDAP server and returns it, or null on error
|
2009-09-04 04:11:32 +00:00
|
|
|
_findCluster: function _findCluster() {
|
|
|
|
this._log.debug("Finding cluster for user " + this.username);
|
2009-02-10 23:56:37 +00:00
|
|
|
|
2010-01-25 18:30:31 +00:00
|
|
|
let fail;
|
2009-09-22 00:13:41 +00:00
|
|
|
let res = new Resource(this.userAPI + this.username + "/node/weave");
|
2009-06-05 06:24:15 +00:00
|
|
|
try {
|
2009-08-26 22:32:46 +00:00
|
|
|
let node = res.get();
|
|
|
|
switch (node.status) {
|
2010-01-25 18:30:31 +00:00
|
|
|
case 400:
|
|
|
|
Status.login = LOGIN_FAILED_LOGIN_REJECTED;
|
2011-08-26 21:01:35 +00:00
|
|
|
fail = "Find cluster denied: " + ErrorHandler.errorStr(node);
|
2010-01-25 18:30:31 +00:00
|
|
|
break;
|
2009-08-20 03:27:22 +00:00
|
|
|
case 404:
|
|
|
|
this._log.debug("Using serverURL as data cluster (multi-cluster support disabled)");
|
2009-09-22 00:13:41 +00:00
|
|
|
return this.serverURL;
|
2009-08-26 22:32:46 +00:00
|
|
|
case 0:
|
2009-08-20 03:27:22 +00:00
|
|
|
case 200:
|
2009-09-30 01:33:41 +00:00
|
|
|
if (node == "null")
|
|
|
|
node = null;
|
2009-08-26 23:09:48 +00:00
|
|
|
return node;
|
2009-08-20 03:27:22 +00:00
|
|
|
default:
|
2011-09-06 16:17:26 +00:00
|
|
|
ErrorHandler.checkServerError(node);
|
2010-06-09 18:07:54 +00:00
|
|
|
fail = "Unexpected response code: " + node.status;
|
2009-08-20 03:27:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-08-20 19:14:34 +00:00
|
|
|
} catch (e) {
|
|
|
|
this._log.debug("Network error on findCluster");
|
2009-10-07 17:47:43 +00:00
|
|
|
Status.login = LOGIN_FAILED_NETWORK_ERROR;
|
2011-09-06 16:17:26 +00:00
|
|
|
ErrorHandler.checkServerError(e);
|
2010-01-25 18:30:31 +00:00
|
|
|
fail = e;
|
2009-08-20 19:14:34 +00:00
|
|
|
}
|
2010-01-25 18:30:31 +00:00
|
|
|
throw fail;
|
2009-02-10 08:56:42 +00:00
|
|
|
},
|
|
|
|
|
2009-03-27 07:46:10 +00:00
|
|
|
// gets cluster from central LDAP server and sets this.clusterURL
|
2009-09-04 04:11:32 +00:00
|
|
|
_setCluster: function _setCluster() {
|
2009-09-22 00:13:41 +00:00
|
|
|
// Make sure we didn't get some unexpected response for the cluster
|
2009-09-04 04:11:32 +00:00
|
|
|
let cluster = this._findCluster();
|
2011-06-25 13:54:50 +00:00
|
|
|
this._log.debug("Cluster value = " + cluster);
|
2009-09-22 00:13:41 +00:00
|
|
|
if (cluster == null)
|
|
|
|
return false;
|
2009-08-20 03:27:22 +00:00
|
|
|
|
2009-09-22 00:13:41 +00:00
|
|
|
// Don't update stuff if we already have the right cluster
|
|
|
|
if (cluster == this.clusterURL)
|
|
|
|
return false;
|
2009-06-06 23:24:14 +00:00
|
|
|
|
2011-06-25 13:54:50 +00:00
|
|
|
this._log.debug("Setting cluster to " + cluster);
|
2009-09-22 00:13:41 +00:00
|
|
|
this.clusterURL = cluster;
|
2011-06-25 13:54:50 +00:00
|
|
|
Svc.Prefs.set("lastClusterUpdate", Date.now().toString());
|
2009-09-22 00:13:41 +00:00
|
|
|
return true;
|
2009-03-27 07:46:10 +00:00
|
|
|
},
|
2009-08-20 03:27:22 +00:00
|
|
|
|
2011-06-25 13:54:50 +00:00
|
|
|
// Update cluster if required.
|
|
|
|
// Returns false if the update was not required.
|
2009-09-04 04:11:32 +00:00
|
|
|
_updateCluster: function _updateCluster() {
|
2011-06-25 13:54:50 +00:00
|
|
|
this._log.info("Updating cluster.");
|
2009-08-20 01:22:22 +00:00
|
|
|
let cTime = Date.now();
|
|
|
|
let lastUp = parseFloat(Svc.Prefs.get("lastClusterUpdate"));
|
|
|
|
if (!lastUp || ((cTime - lastUp) >= CLUSTER_BACKOFF)) {
|
2011-06-25 13:54:50 +00:00
|
|
|
return this._setCluster();
|
2009-08-20 01:22:22 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2009-08-20 03:27:22 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
/**
|
|
|
|
* Perform the info fetch as part of a login or key fetch.
|
|
|
|
*/
|
2011-02-01 04:55:48 +00:00
|
|
|
_fetchInfo: function _fetchInfo(url) {
|
2010-11-30 00:41:17 +00:00
|
|
|
let infoURL = url || this.infoURL;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-20 20:47:51 +00:00
|
|
|
this._log.trace("In _fetchInfo: " + infoURL);
|
2011-02-01 04:55:48 +00:00
|
|
|
let info;
|
|
|
|
try {
|
|
|
|
info = new Resource(infoURL).get();
|
|
|
|
} catch (ex) {
|
2011-08-26 21:01:35 +00:00
|
|
|
ErrorHandler.checkServerError(ex);
|
2011-02-01 04:55:48 +00:00
|
|
|
throw ex;
|
|
|
|
}
|
2010-11-30 00:41:17 +00:00
|
|
|
if (!info.success) {
|
2011-08-26 21:01:35 +00:00
|
|
|
ErrorHandler.checkServerError(info);
|
2010-11-30 00:41:17 +00:00
|
|
|
throw "aborting sync, failed to get collections";
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
},
|
|
|
|
|
|
|
|
verifyAndFetchSymmetricKeys: function verifyAndFetchSymmetricKeys(infoResponse) {
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.debug("Fetching and verifying -- or generating -- symmetric keys.");
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Don't allow empty/missing passphrase.
|
|
|
|
// Furthermore, we assume that our sync key is already upgraded,
|
|
|
|
// and fail if that assumption is invalidated.
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
let syncKey = this.syncKeyBundle;
|
|
|
|
if (!syncKey) {
|
|
|
|
this._log.error("No sync key: cannot fetch symmetric keys.");
|
|
|
|
Status.login = LOGIN_FAILED_NO_PASSPHRASE;
|
|
|
|
Status.sync = CREDENTIALS_CHANGED; // For want of a better option.
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Not sure this validation is necessary now.
|
|
|
|
if (!Utils.isPassphrase(syncKey.keyStr)) {
|
|
|
|
this._log.warn("Sync key input is invalid: cannot fetch symmetric keys.");
|
|
|
|
Status.login = LOGIN_FAILED_INVALID_PASSPHRASE;
|
|
|
|
Status.sync = CREDENTIALS_CHANGED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!infoResponse)
|
|
|
|
infoResponse = this._fetchInfo(); // Will throw an exception on failure.
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// This only applies when the server is already at version 4.
|
|
|
|
if (infoResponse.status != 200) {
|
|
|
|
this._log.warn("info/collections returned non-200 response. Failing key fetch.");
|
|
|
|
Status.login = LOGIN_FAILED_SERVER_ERROR;
|
2011-09-06 16:17:26 +00:00
|
|
|
ErrorHandler.checkServerError(infoResponse);
|
2010-11-30 00:41:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
let infoCollections = infoResponse.obj;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.info("Testing info/collections: " + JSON.stringify(infoCollections));
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
if (CollectionKeys.updateNeeded(infoCollections)) {
|
|
|
|
this._log.info("CollectionKeys reports that a key update is needed.");
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Don't always set to CREDENTIALS_CHANGED -- we will probably take care of this.
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Fetch storage/crypto/keys.
|
|
|
|
let cryptoKeys;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
if (infoCollections && (CRYPTO_COLLECTION in infoCollections)) {
|
2010-11-30 00:41:17 +00:00
|
|
|
try {
|
2011-04-19 19:35:04 +00:00
|
|
|
cryptoKeys = new CryptoWrapper(CRYPTO_COLLECTION, KEYS_WBO);
|
2010-11-30 00:41:17 +00:00
|
|
|
let cryptoResp = cryptoKeys.fetch(this.cryptoKeysURL).response;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
if (cryptoResp.success) {
|
2010-12-10 07:06:44 +00:00
|
|
|
let keysChanged = this.handleFetchedKeys(syncKey, cryptoKeys);
|
2010-11-30 00:41:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (cryptoResp.status == 404) {
|
|
|
|
// On failure, ask CollectionKeys to generate new keys and upload them.
|
|
|
|
// Fall through to the behavior below.
|
|
|
|
this._log.warn("Got 404 for crypto/keys, but 'crypto' in info/collections. Regenerating.");
|
|
|
|
cryptoKeys = null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Some other problem.
|
|
|
|
Status.login = LOGIN_FAILED_SERVER_ERROR;
|
2011-09-06 16:17:26 +00:00
|
|
|
ErrorHandler.checkServerError(cryptoResp);
|
|
|
|
this._log.warn("Got status " + cryptoResp.status + " fetching crypto keys.");
|
2010-11-30 00:41:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
this._log.warn("Got exception \"" + ex + "\" fetching cryptoKeys.");
|
|
|
|
// TODO: Um, what exceptions might we get here? Should we re-throw any?
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// One kind of exception: HMAC failure.
|
2010-12-09 18:32:03 +00:00
|
|
|
if (Utils.isHMACMismatch(ex)) {
|
2010-11-30 00:41:17 +00:00
|
|
|
Status.login = LOGIN_FAILED_INVALID_PASSPHRASE;
|
|
|
|
Status.sync = CREDENTIALS_CHANGED;
|
|
|
|
}
|
2010-12-11 00:32:16 +00:00
|
|
|
else {
|
|
|
|
// In the absence of further disambiguation or more precise
|
|
|
|
// failure constants, just report failure.
|
|
|
|
Status.login = LOGIN_FAILED;
|
|
|
|
}
|
2010-11-30 00:41:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._log.info("... 'crypto' is not a reported collection. Generating new keys.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cryptoKeys) {
|
2011-04-19 19:35:04 +00:00
|
|
|
this._log.info("No keys! Generating new ones.");
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
// Better make some and upload them, and wipe the server to ensure
|
|
|
|
// consistency. This is all achieved via _freshStart.
|
|
|
|
// If _freshStart fails to clear the server or upload keys, it will
|
|
|
|
// throw.
|
|
|
|
this._freshStart();
|
2010-11-30 00:41:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Last-ditch case.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No update needed: we're good!
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-10-20 23:21:17 +00:00
|
|
|
} catch (e) {
|
2010-11-30 00:41:17 +00:00
|
|
|
// This means no keys are present, or there's a network error.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-06-14 20:03:39 +00:00
|
|
|
verifyLogin: function verifyLogin()
|
2009-09-28 23:13:42 +00:00
|
|
|
this._notify("verify-login", "", function() {
|
2010-06-14 20:03:39 +00:00
|
|
|
if (!this.username) {
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.warn("No username in verifyLogin.");
|
2010-06-14 20:03:39 +00:00
|
|
|
Status.login = LOGIN_FAILED_NO_USERNAME;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-11 05:51:29 +00:00
|
|
|
// Unlock master password, or return.
|
2011-02-01 04:55:48 +00:00
|
|
|
// Attaching auth credentials to a request requires access to
|
|
|
|
// passwords, which means that Resource.get can throw MP-related
|
|
|
|
// exceptions!
|
|
|
|
// Try to fetch the passphrase first, while we still have control.
|
2009-08-20 01:22:22 +00:00
|
|
|
try {
|
2011-02-01 04:55:48 +00:00
|
|
|
this.passphrase;
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.debug("Fetching passphrase threw " + ex +
|
|
|
|
"; assuming master password locked.");
|
|
|
|
Status.login = MASTER_PASSWORD_LOCKED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Make sure we have a cluster to verify against
|
|
|
|
// this is a little weird, if we don't get a node we pretend
|
|
|
|
// to succeed, since that probably means we just don't have storage
|
|
|
|
if (this.clusterURL == "" && !this._setCluster()) {
|
|
|
|
Status.sync = NO_SYNC_NODE_FOUND;
|
|
|
|
Svc.Obs.notify("weave:service:sync:delayed");
|
|
|
|
return true;
|
2011-01-11 05:51:29 +00:00
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-02-01 04:55:48 +00:00
|
|
|
// Fetch collection info on every startup.
|
2009-09-28 23:13:42 +00:00
|
|
|
let test = new Resource(this.infoURL).get();
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2009-08-26 22:32:46 +00:00
|
|
|
switch (test.status) {
|
2009-08-20 03:27:22 +00:00
|
|
|
case 200:
|
2010-06-14 20:03:39 +00:00
|
|
|
// The user is authenticated.
|
2010-11-30 00:41:17 +00:00
|
|
|
|
|
|
|
// We have no way of verifying the passphrase right now,
|
|
|
|
// so wait until remoteSetup to do so.
|
|
|
|
// Just make the most trivial checks.
|
2010-06-14 20:03:39 +00:00
|
|
|
if (!this.passphrase) {
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.warn("No passphrase in verifyLogin.");
|
2010-06-14 20:03:39 +00:00
|
|
|
Status.login = LOGIN_FAILED_NO_PASSPHRASE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-19 19:33:47 +00:00
|
|
|
// Go ahead and do remote setup, so that we can determine
|
2010-11-30 00:41:17 +00:00
|
|
|
// conclusively that our passphrase is correct.
|
|
|
|
if (this._remoteSetup()) {
|
|
|
|
// Username/password verified.
|
2011-10-12 14:13:33 +00:00
|
|
|
Status.login = LOGIN_SUCCEEDED;
|
|
|
|
return true;
|
2010-11-30 00:41:17 +00:00
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.warn("Remote setup failed.");
|
|
|
|
// Remote setup must have failed.
|
|
|
|
return false;
|
2009-09-28 23:13:42 +00:00
|
|
|
|
2009-08-20 03:27:22 +00:00
|
|
|
case 401:
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.warn("401: login failed.");
|
2010-07-19 22:28:54 +00:00
|
|
|
// Login failed. If the password contains non-ASCII characters,
|
|
|
|
// perhaps the server password is an old low-byte only one?
|
|
|
|
let id = ID.get('WeaveID');
|
|
|
|
if (id.password != id.passwordUTF8) {
|
|
|
|
let res = new Resource(this.infoURL);
|
|
|
|
let auth = new BrokenBasicAuthenticator(id);
|
|
|
|
res.authenticator = auth;
|
|
|
|
test = res.get();
|
|
|
|
if (test.status == 200) {
|
|
|
|
this._log.debug("Non-ASCII password detected. "
|
|
|
|
+ "Changing to UTF-8 version.");
|
|
|
|
// Let's change the password on the server to the UTF8 version.
|
|
|
|
let url = this.userAPI + this.username + "/password";
|
|
|
|
res = new Resource(url);
|
|
|
|
res.authenticator = auth;
|
|
|
|
res.post(id.passwordUTF8);
|
|
|
|
return this.verifyLogin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Yes, we want to fall through to the 404 case.
|
2009-09-22 00:34:19 +00:00
|
|
|
case 404:
|
|
|
|
// Check that we're verifying with the correct cluster
|
2009-09-22 01:03:56 +00:00
|
|
|
if (this._setCluster())
|
2010-06-14 20:03:39 +00:00
|
|
|
return this.verifyLogin();
|
2009-08-20 03:27:22 +00:00
|
|
|
|
2009-09-22 00:34:19 +00:00
|
|
|
// We must have the right cluster, but the server doesn't expect us
|
2009-10-07 17:47:43 +00:00
|
|
|
Status.login = LOGIN_FAILED_LOGIN_REJECTED;
|
2009-08-20 03:27:22 +00:00
|
|
|
return false;
|
2009-09-28 23:13:42 +00:00
|
|
|
|
2009-08-20 03:27:22 +00:00
|
|
|
default:
|
2009-09-28 23:13:42 +00:00
|
|
|
// Server didn't respond with something that we expected
|
2009-10-07 17:47:43 +00:00
|
|
|
Status.login = LOGIN_FAILED_SERVER_ERROR;
|
2011-09-06 16:17:26 +00:00
|
|
|
ErrorHandler.checkServerError(test);
|
2009-09-28 23:13:42 +00:00
|
|
|
return false;
|
2009-08-20 01:22:22 +00:00
|
|
|
}
|
2009-09-28 23:13:42 +00:00
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
// Must have failed on some network issue
|
|
|
|
this._log.debug("verifyLogin failed: " + Utils.exceptionStr(ex));
|
2009-10-07 17:47:43 +00:00
|
|
|
Status.login = LOGIN_FAILED_NETWORK_ERROR;
|
2011-09-06 16:17:26 +00:00
|
|
|
ErrorHandler.checkServerError(ex);
|
2009-09-28 23:13:42 +00:00
|
|
|
return false;
|
2009-08-20 01:22:22 +00:00
|
|
|
}
|
2009-09-28 23:13:42 +00:00
|
|
|
})(),
|
2008-06-30 23:50:19 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
generateNewSymmetricKeys:
|
|
|
|
function WeaveSvc_generateNewSymmetricKeys() {
|
2011-04-19 19:35:04 +00:00
|
|
|
this._log.info("Generating new keys WBO...");
|
|
|
|
let wbo = CollectionKeys.generateNewKeysWBO();
|
|
|
|
this._log.info("Encrypting new key bundle.");
|
2010-11-30 00:41:17 +00:00
|
|
|
wbo.encrypt(this.syncKeyBundle);
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.info("Uploading...");
|
|
|
|
let uploadRes = wbo.upload(this.cryptoKeysURL);
|
2011-04-19 19:35:04 +00:00
|
|
|
if (uploadRes.status != 200) {
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.warn("Got status " + uploadRes.status + " uploading new keys. What to do? Throw!");
|
|
|
|
throw new Error("Unable to upload symmetric keys.");
|
|
|
|
}
|
2011-04-19 19:35:04 +00:00
|
|
|
this._log.info("Got status " + uploadRes.status + " uploading keys.");
|
|
|
|
let serverModified = uploadRes.obj; // Modified timestamp according to server.
|
2011-04-19 19:56:46 +00:00
|
|
|
this._log.debug("Server reports crypto modified: " + serverModified);
|
2011-04-19 19:35:04 +00:00
|
|
|
|
|
|
|
// Now verify that info/collections shows them!
|
|
|
|
this._log.debug("Verifying server collection records.");
|
|
|
|
let info = this._fetchInfo();
|
|
|
|
this._log.debug("info/collections is: " + info);
|
|
|
|
|
|
|
|
if (info.status != 200) {
|
|
|
|
this._log.warn("Non-200 info/collections response. Aborting.");
|
|
|
|
throw new Error("Unable to upload symmetric keys.");
|
|
|
|
}
|
|
|
|
|
|
|
|
info = info.obj;
|
2011-04-19 20:18:15 +00:00
|
|
|
if (!(CRYPTO_COLLECTION in info)) {
|
2011-04-19 19:35:04 +00:00
|
|
|
this._log.error("Consistency failure: info/collections excludes " +
|
|
|
|
"crypto after successful upload.");
|
|
|
|
throw new Error("Symmetric key upload failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't check against local modified: clock drift.
|
2011-04-19 20:18:15 +00:00
|
|
|
if (info[CRYPTO_COLLECTION] < serverModified) {
|
2011-04-19 19:35:04 +00:00
|
|
|
this._log.error("Consistency failure: info/collections crypto entry " +
|
|
|
|
"is stale after successful upload.");
|
|
|
|
throw new Error("Symmetric key upload failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't matter if the timestamp is ahead.
|
|
|
|
|
|
|
|
// Download and install them.
|
|
|
|
let cryptoKeys = new CryptoWrapper(CRYPTO_COLLECTION, KEYS_WBO);
|
|
|
|
let cryptoResp = cryptoKeys.fetch(this.cryptoKeysURL).response;
|
|
|
|
if (cryptoResp.status != 200) {
|
|
|
|
this._log.warn("Failed to download keys.");
|
|
|
|
throw new Error("Symmetric key download failed.");
|
|
|
|
}
|
|
|
|
let keysChanged = this.handleFetchedKeys(this.syncKeyBundle,
|
|
|
|
cryptoKeys, true);
|
|
|
|
if (keysChanged) {
|
|
|
|
this._log.info("Downloaded keys differed, as expected.");
|
|
|
|
}
|
2010-11-30 00:41:17 +00:00
|
|
|
},
|
2008-07-12 00:40:52 +00:00
|
|
|
|
2009-07-22 03:59:02 +00:00
|
|
|
changePassword: function WeaveSvc_changePassword(newpass)
|
2009-09-28 23:28:38 +00:00
|
|
|
this._notify("changepwd", "", function() {
|
2009-09-22 00:13:41 +00:00
|
|
|
let url = this.userAPI + this.username + "/password";
|
2009-09-28 23:28:38 +00:00
|
|
|
try {
|
2010-07-19 22:28:54 +00:00
|
|
|
let resp = new Resource(url).post(Utils.encodeUTF8(newpass));
|
2009-09-28 23:28:38 +00:00
|
|
|
if (resp.status != 200) {
|
|
|
|
this._log.debug("Password change failed: " + resp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
// Must have failed on some network issue
|
|
|
|
this._log.debug("changePassword failed: " + Utils.exceptionStr(ex));
|
|
|
|
return false;
|
2009-07-22 03:59:02 +00:00
|
|
|
}
|
2009-08-19 03:03:11 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Save the new password for requests and login manager.
|
2009-07-22 03:59:02 +00:00
|
|
|
this.password = newpass;
|
2009-09-28 23:28:38 +00:00
|
|
|
this.persistLogin();
|
2009-07-22 03:59:02 +00:00
|
|
|
return true;
|
2009-09-28 23:28:38 +00:00
|
|
|
})(),
|
2009-08-19 03:03:11 +00:00
|
|
|
|
2010-04-01 01:58:07 +00:00
|
|
|
changePassphrase: function WeaveSvc_changePassphrase(newphrase)
|
|
|
|
this._catch(this._notify("changepph", "", function() {
|
2010-11-30 00:41:17 +00:00
|
|
|
/* Wipe. */
|
2009-11-12 20:44:33 +00:00
|
|
|
this.wipeServer();
|
|
|
|
|
|
|
|
this.logout();
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
/* Set this so UI is updated on next run. */
|
2009-07-21 21:31:16 +00:00
|
|
|
this.passphrase = newphrase;
|
2010-05-04 20:56:12 +00:00
|
|
|
this.persistLogin();
|
2009-08-19 03:03:11 +00:00
|
|
|
|
2010-12-11 00:53:40 +00:00
|
|
|
/* We need to re-encrypt everything, so reset. */
|
|
|
|
this.resetClient();
|
2010-12-11 03:41:55 +00:00
|
|
|
CollectionKeys.clear();
|
2010-12-11 00:53:40 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
/* Login and sync. This also generates new keys. */
|
2011-06-06 14:49:13 +00:00
|
|
|
this.sync();
|
2009-07-21 21:31:16 +00:00
|
|
|
return true;
|
|
|
|
}))(),
|
2009-08-19 03:03:11 +00:00
|
|
|
|
2010-04-01 20:43:09 +00:00
|
|
|
startOver: function() {
|
2011-06-06 18:29:45 +00:00
|
|
|
Svc.Obs.notify("weave:engine:stop-tracking");
|
2011-10-11 01:22:58 +00:00
|
|
|
Status.resetSync();
|
2011-06-06 18:29:45 +00:00
|
|
|
|
|
|
|
// We want let UI consumers of the following notification know as soon as
|
|
|
|
// possible, so let's fake for the CLIENT_NOT_CONFIGURED status for now
|
|
|
|
// by emptying the passphrase (we still need the password).
|
|
|
|
Service.passphrase = "";
|
|
|
|
Status.login = LOGIN_FAILED_NO_PASSPHRASE;
|
|
|
|
this.logout();
|
|
|
|
Svc.Obs.notify("weave:service:start-over");
|
|
|
|
|
2011-04-13 22:34:58 +00:00
|
|
|
// Deletion doesn't make sense if we aren't set up yet!
|
|
|
|
if (this.clusterURL != "") {
|
|
|
|
// Clear client-specific data from the server, including disabled engines.
|
|
|
|
for each (let engine in [Clients].concat(Engines.getAll())) {
|
|
|
|
try {
|
|
|
|
engine.removeClientData();
|
|
|
|
} catch(ex) {
|
|
|
|
this._log.warn("Deleting client data for " + engine.name + " failed:"
|
|
|
|
+ Utils.exceptionStr(ex));
|
|
|
|
}
|
2011-04-08 21:51:55 +00:00
|
|
|
}
|
2011-04-13 22:34:58 +00:00
|
|
|
} else {
|
|
|
|
this._log.debug("Skipping client data removal: no cluster URL.");
|
2011-04-08 21:51:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-11 00:53:40 +00:00
|
|
|
// Reset all engines and clear keys.
|
2010-04-01 20:43:09 +00:00
|
|
|
this.resetClient();
|
2010-12-11 03:41:55 +00:00
|
|
|
CollectionKeys.clear();
|
2011-10-05 03:52:14 +00:00
|
|
|
Status.resetBackoff();
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Reset Weave prefs.
|
2010-09-07 16:44:01 +00:00
|
|
|
this._ignorePrefObserver = true;
|
2010-04-01 20:43:09 +00:00
|
|
|
Svc.Prefs.resetBranch("");
|
2010-09-07 16:44:01 +00:00
|
|
|
this._ignorePrefObserver = false;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-05-07 20:16:37 +00:00
|
|
|
Svc.Prefs.set("lastversion", WEAVE_VERSION);
|
2010-04-01 20:43:09 +00:00
|
|
|
// Find weave logins and remove them.
|
2010-05-07 22:39:58 +00:00
|
|
|
this.password = "";
|
2011-05-20 01:08:07 +00:00
|
|
|
Services.logins.findLogins({}, PWDMGR_HOST, "", "").map(function(login) {
|
|
|
|
Services.logins.removeLogin(login);
|
2010-04-01 20:43:09 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2009-09-04 04:11:32 +00:00
|
|
|
persistLogin: function persistLogin() {
|
2010-11-30 00:41:17 +00:00
|
|
|
// Canceled master password prompt can prevent these from succeeding.
|
2009-09-04 04:11:32 +00:00
|
|
|
try {
|
|
|
|
ID.get("WeaveID").persist();
|
|
|
|
ID.get("WeaveCryptoID").persist();
|
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
},
|
|
|
|
|
2009-06-06 18:52:54 +00:00
|
|
|
login: function WeaveSvc_login(username, password, passphrase)
|
2011-04-19 19:33:47 +00:00
|
|
|
this._catch(this._lock("service.js: login",
|
2010-11-30 00:41:17 +00:00
|
|
|
this._notify("login", "", function() {
|
2008-12-27 20:15:45 +00:00
|
|
|
this._loggedIn = false;
|
2011-05-20 01:08:07 +00:00
|
|
|
if (Services.io.offline) {
|
2011-02-03 18:23:39 +00:00
|
|
|
Status.login = LOGIN_FAILED_NETWORK_ERROR;
|
2009-08-20 03:27:22 +00:00
|
|
|
throw "Application is offline, login should not be called";
|
2011-02-03 18:23:39 +00:00
|
|
|
}
|
2008-03-30 15:40:23 +00:00
|
|
|
|
2010-08-09 23:46:54 +00:00
|
|
|
let initialStatus = this._checkSetup();
|
2009-09-16 01:54:05 +00:00
|
|
|
if (username)
|
2009-06-06 18:52:54 +00:00
|
|
|
this.username = username;
|
2009-09-16 01:54:05 +00:00
|
|
|
if (password)
|
2009-09-04 04:11:32 +00:00
|
|
|
this.password = password;
|
2009-09-16 01:54:05 +00:00
|
|
|
if (passphrase)
|
2009-09-04 04:11:32 +00:00
|
|
|
this.passphrase = passphrase;
|
2009-03-12 01:29:18 +00:00
|
|
|
|
2010-04-09 17:20:41 +00:00
|
|
|
if (this._checkSetup() == CLIENT_NOT_CONFIGURED)
|
|
|
|
throw "aborting login, client not configured";
|
|
|
|
|
2010-08-09 23:46:54 +00:00
|
|
|
// Calling login() with parameters when the client was
|
|
|
|
// previously not configured means setup was completed.
|
|
|
|
if (initialStatus == CLIENT_NOT_CONFIGURED
|
|
|
|
&& (username || password || passphrase))
|
|
|
|
Svc.Obs.notify("weave:service:setup-complete");
|
|
|
|
|
2009-09-11 05:57:36 +00:00
|
|
|
this._log.info("Logging in user " + this.username);
|
2009-03-12 01:29:18 +00:00
|
|
|
|
2010-06-14 20:03:39 +00:00
|
|
|
if (!this.verifyLogin()) {
|
2010-04-09 17:20:41 +00:00
|
|
|
// verifyLogin sets the failure states here.
|
2009-10-07 17:47:43 +00:00
|
|
|
throw "Login failed: " + Status.login;
|
2009-03-12 01:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._loggedIn = true;
|
2009-03-12 20:01:04 +00:00
|
|
|
|
2009-06-06 18:52:54 +00:00
|
|
|
return true;
|
|
|
|
})))(),
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2008-06-30 21:00:06 +00:00
|
|
|
logout: function WeaveSvc_logout() {
|
2010-11-30 00:41:17 +00:00
|
|
|
// No need to do anything if we're already logged out.
|
2009-09-30 22:16:56 +00:00
|
|
|
if (!this._loggedIn)
|
|
|
|
return;
|
|
|
|
|
2007-12-11 05:38:53 +00:00
|
|
|
this._log.info("Logging out");
|
2008-04-11 04:38:15 +00:00
|
|
|
this._loggedIn = false;
|
2009-03-10 11:15:52 +00:00
|
|
|
|
2010-04-21 01:56:44 +00:00
|
|
|
Svc.Obs.notify("weave:service:logout:finish");
|
2007-12-11 05:38:53 +00:00
|
|
|
},
|
|
|
|
|
2010-09-13 15:17:37 +00:00
|
|
|
checkAccount: function checkAccount(account) {
|
|
|
|
let username = this._usernameFromAccount(account);
|
2009-09-22 00:13:41 +00:00
|
|
|
let url = this.userAPI + username;
|
2009-08-19 03:03:11 +00:00
|
|
|
let res = new Resource(url);
|
|
|
|
res.authenticator = new NoOpAuthenticator();
|
|
|
|
|
2009-08-20 17:20:01 +00:00
|
|
|
let data = "";
|
|
|
|
try {
|
|
|
|
data = res.get();
|
2010-05-07 04:02:00 +00:00
|
|
|
if (data.status == 200) {
|
|
|
|
if (data == "0")
|
|
|
|
return "available";
|
|
|
|
else if (data == "1")
|
|
|
|
return "notAvailable";
|
|
|
|
}
|
|
|
|
|
2009-08-20 17:20:01 +00:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
2009-08-19 03:03:11 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Convert to the error string, or default to generic on exception.
|
2011-08-26 21:01:35 +00:00
|
|
|
return ErrorHandler.errorStr(data);
|
2009-08-19 03:03:11 +00:00
|
|
|
},
|
|
|
|
|
2011-03-25 07:57:23 +00:00
|
|
|
createAccount: function createAccount(email, password,
|
|
|
|
captchaChallenge, captchaResponse) {
|
|
|
|
let username = this._usernameFromAccount(email);
|
2009-08-26 00:06:13 +00:00
|
|
|
let payload = JSON.stringify({
|
2010-07-19 22:28:54 +00:00
|
|
|
"password": Utils.encodeUTF8(password),
|
|
|
|
"email": email,
|
2009-08-26 23:45:24 +00:00
|
|
|
"captcha-challenge": captchaChallenge,
|
|
|
|
"captcha-response": captchaResponse
|
2009-08-26 00:06:13 +00:00
|
|
|
});
|
|
|
|
|
2009-09-22 00:13:41 +00:00
|
|
|
let url = this.userAPI + username;
|
2009-08-19 03:03:11 +00:00
|
|
|
let res = new Resource(url);
|
2010-08-25 22:49:44 +00:00
|
|
|
res.authenticator = new NoOpAuthenticator();
|
2010-06-17 15:47:13 +00:00
|
|
|
|
|
|
|
// Hint to server to allow scripted user creation or otherwise
|
|
|
|
// ignore captcha.
|
2010-05-04 20:55:34 +00:00
|
|
|
if (Svc.Prefs.isSet("admin-secret"))
|
|
|
|
res.setHeader("X-Weave-Secret", Svc.Prefs.get("admin-secret", ""));
|
2009-06-06 23:24:17 +00:00
|
|
|
|
2009-08-26 22:32:46 +00:00
|
|
|
let error = "generic-server-error";
|
2009-06-06 23:24:17 +00:00
|
|
|
try {
|
2009-08-26 23:09:48 +00:00
|
|
|
let register = res.put(payload);
|
2009-08-26 22:32:46 +00:00
|
|
|
if (register.success) {
|
|
|
|
this._log.info("Account created: " + register);
|
2009-10-23 07:05:41 +00:00
|
|
|
return null;
|
2009-08-26 22:32:46 +00:00
|
|
|
}
|
2009-08-26 23:09:48 +00:00
|
|
|
|
2009-08-26 22:32:46 +00:00
|
|
|
// Must have failed, so figure out the reason
|
2009-08-27 00:59:50 +00:00
|
|
|
if (register.status == 400)
|
2011-08-26 21:01:35 +00:00
|
|
|
error = ErrorHandler.errorStr(register);
|
2009-08-19 03:03:11 +00:00
|
|
|
}
|
2009-08-26 22:32:46 +00:00
|
|
|
catch(ex) {
|
|
|
|
this._log.warn("Failed to create account: " + ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
2009-03-27 07:46:10 +00:00
|
|
|
},
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Stuff we need to do after login, before we can really do
|
|
|
|
// anything (e.g. key setup).
|
|
|
|
_remoteSetup: function WeaveSvc__remoteSetup(infoResponse) {
|
2009-11-12 20:44:33 +00:00
|
|
|
let reset = false;
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.debug("Fetching global metadata record");
|
2010-08-23 13:20:39 +00:00
|
|
|
let meta = Records.get(this.metaURL);
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-06 22:22:09 +00:00
|
|
|
// Checking modified time of the meta record.
|
|
|
|
if (infoResponse &&
|
|
|
|
(infoResponse.obj.meta != this.metaModified) &&
|
2010-12-10 03:37:02 +00:00
|
|
|
(!meta || !meta.isNew)) {
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-06 22:22:09 +00:00
|
|
|
// Delete the cached meta record...
|
|
|
|
this._log.debug("Clearing cached meta record. metaModified is " +
|
|
|
|
JSON.stringify(this.metaModified) + ", setting to " +
|
2010-12-06 23:09:14 +00:00
|
|
|
JSON.stringify(infoResponse.obj.meta));
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-06 22:22:09 +00:00
|
|
|
Records.del(this.metaURL);
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-06 22:22:09 +00:00
|
|
|
// ... fetch the current record from the server, and COPY THE FLAGS.
|
|
|
|
let newMeta = Records.get(this.metaURL);
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 03:37:02 +00:00
|
|
|
if (!Records.response.success || !newMeta) {
|
|
|
|
this._log.debug("No meta/global record on the server. Creating one.");
|
|
|
|
newMeta = new WBORecord("meta", "global");
|
|
|
|
newMeta.payload.syncID = this.syncID;
|
|
|
|
newMeta.payload.storageVersion = STORAGE_VERSION;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 03:37:02 +00:00
|
|
|
newMeta.isNew = true;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-10 03:37:02 +00:00
|
|
|
Records.set(this.metaURL, newMeta);
|
|
|
|
if (!newMeta.upload(this.metaURL).success) {
|
|
|
|
this._log.warn("Unable to upload new meta/global. Failing remote setup.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If newMeta, then it stands to reason that meta != null.
|
|
|
|
newMeta.isNew = meta.isNew;
|
|
|
|
newMeta.changed = meta.changed;
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-06 22:22:09 +00:00
|
|
|
// Switch in the new meta object and record the new time.
|
|
|
|
meta = newMeta;
|
2010-12-06 23:09:14 +00:00
|
|
|
this.metaModified = infoResponse.obj.meta;
|
2010-12-06 22:22:09 +00:00
|
|
|
}
|
2009-03-07 08:55:47 +00:00
|
|
|
|
2009-03-03 19:57:30 +00:00
|
|
|
let remoteVersion = (meta && meta.payload.storageVersion)?
|
|
|
|
meta.payload.storageVersion : "";
|
2009-03-02 22:15:46 +00:00
|
|
|
|
2010-03-16 23:31:55 +00:00
|
|
|
this._log.debug(["Weave Version:", WEAVE_VERSION, "Local Storage:",
|
|
|
|
STORAGE_VERSION, "Remote Storage:", remoteVersion].join(" "));
|
2009-03-02 22:15:46 +00:00
|
|
|
|
2010-03-16 23:31:55 +00:00
|
|
|
// Check for cases that require a fresh start. When comparing remoteVersion,
|
|
|
|
// we need to convert it to a number as older clients used it as a string.
|
2009-02-23 20:36:55 +00:00
|
|
|
if (!meta || !meta.payload.storageVersion || !meta.payload.syncID ||
|
2010-03-16 23:31:55 +00:00
|
|
|
STORAGE_VERSION > parseFloat(remoteVersion)) {
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-06 22:22:09 +00:00
|
|
|
this._log.info("One of: no meta, no meta storageVersion, or no meta syncID. Fresh start needed.");
|
2009-03-03 19:57:30 +00:00
|
|
|
|
2009-02-24 00:27:41 +00:00
|
|
|
// abort the server wipe if the GET status was anything other than 404 or 200
|
2009-08-26 22:32:46 +00:00
|
|
|
let status = Records.response.status;
|
2009-02-24 00:27:41 +00:00
|
|
|
if (status != 200 && status != 404) {
|
2009-10-07 17:47:43 +00:00
|
|
|
Status.sync = METARECORD_DOWNLOAD_FAIL;
|
2011-09-06 16:17:26 +00:00
|
|
|
ErrorHandler.checkServerError(Records.response);
|
2009-03-12 15:34:12 +00:00
|
|
|
this._log.warn("Unknown error while downloading metadata record. " +
|
2009-03-03 19:57:30 +00:00
|
|
|
"Aborting sync.");
|
2009-06-06 18:53:23 +00:00
|
|
|
return false;
|
2009-02-12 07:54:20 +00:00
|
|
|
}
|
2009-02-24 00:56:23 +00:00
|
|
|
|
2009-03-03 19:57:30 +00:00
|
|
|
if (!meta)
|
2009-03-12 15:34:12 +00:00
|
|
|
this._log.info("No metadata record, server wipe needed");
|
2009-03-03 19:57:30 +00:00
|
|
|
if (meta && !meta.payload.syncID)
|
2009-03-12 15:34:12 +00:00
|
|
|
this._log.warn("No sync id, server wipe needed");
|
2009-03-03 19:57:30 +00:00
|
|
|
|
2009-11-12 20:44:33 +00:00
|
|
|
reset = true;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2009-03-03 19:57:30 +00:00
|
|
|
this._log.info("Wiping server data");
|
2009-06-06 05:21:29 +00:00
|
|
|
this._freshStart();
|
2009-02-24 00:56:23 +00:00
|
|
|
|
|
|
|
if (status == 404)
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.info("Metadata record not found, server was wiped to ensure " +
|
2009-02-24 00:56:23 +00:00
|
|
|
"consistency.");
|
|
|
|
else // 200
|
2010-03-16 23:31:55 +00:00
|
|
|
this._log.info("Wiped server; incompatible metadata: " + remoteVersion);
|
2009-02-23 20:36:55 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
return true;
|
2010-03-16 23:31:55 +00:00
|
|
|
}
|
|
|
|
else if (remoteVersion > STORAGE_VERSION) {
|
2009-10-07 17:47:43 +00:00
|
|
|
Status.sync = VERSION_OUT_OF_DATE;
|
2010-03-16 23:31:55 +00:00
|
|
|
this._log.warn("Upgrade required to access newer storage version.");
|
2009-06-06 18:53:23 +00:00
|
|
|
return false;
|
2010-03-25 16:52:45 +00:00
|
|
|
}
|
|
|
|
else if (meta.payload.syncID != this.syncID) {
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-06 22:22:09 +00:00
|
|
|
this._log.info("Sync IDs differ. Local is " + this.syncID + ", remote is " + meta.payload.syncID);
|
2010-04-01 22:54:53 +00:00
|
|
|
this.resetClient();
|
2010-12-11 03:41:55 +00:00
|
|
|
CollectionKeys.clear();
|
2010-03-25 16:52:45 +00:00
|
|
|
this.syncID = meta.payload.syncID;
|
|
|
|
this._log.debug("Clear cached values and take syncId: " + this.syncID);
|
2009-12-03 01:57:13 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
if (!this.upgradeSyncKey(meta.payload.syncID)) {
|
|
|
|
this._log.warn("Failed to upgrade sync key. Failing remote setup.");
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
if (!this.verifyAndFetchSymmetricKeys(infoResponse)) {
|
|
|
|
this._log.warn("Failed to fetch symmetric keys. Failing remote setup.");
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-02 23:38:05 +00:00
|
|
|
|
2010-04-01 01:58:07 +00:00
|
|
|
// bug 545725 - re-verify creds and fail sanely
|
2010-06-14 20:03:39 +00:00
|
|
|
if (!this.verifyLogin()) {
|
2010-04-01 01:58:07 +00:00
|
|
|
Status.sync = CREDENTIALS_CHANGED;
|
|
|
|
this._log.info("Credentials have changed, aborting sync and forcing re-login.");
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-12 07:54:20 +00:00
|
|
|
|
2010-12-01 22:59:33 +00:00
|
|
|
return true;
|
2008-11-20 00:18:35 +00:00
|
|
|
}
|
2010-11-30 00:41:17 +00:00
|
|
|
else {
|
|
|
|
if (!this.upgradeSyncKey(meta.payload.syncID)) {
|
|
|
|
this._log.warn("Failed to upgrade sync key. Failing remote setup.");
|
2009-06-06 18:53:23 +00:00
|
|
|
return false;
|
2009-02-12 07:54:20 +00:00
|
|
|
}
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
if (!this.verifyAndFetchSymmetricKeys(infoResponse)) {
|
|
|
|
this._log.warn("Failed to fetch symmetric keys. Failing remote setup.");
|
2009-06-06 18:53:23 +00:00
|
|
|
return false;
|
2009-02-12 07:54:20 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 22:59:33 +00:00
|
|
|
return true;
|
2008-11-20 00:18:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-12-21 04:01:34 +00:00
|
|
|
/**
|
|
|
|
* Return whether we should attempt login at the start of a sync.
|
2011-04-19 19:33:47 +00:00
|
|
|
*
|
|
|
|
* Note that this function has strong ties to _checkSync: callers
|
2010-12-21 04:01:34 +00:00
|
|
|
* of this function should typically use _checkSync to verify that
|
|
|
|
* any necessary login took place.
|
|
|
|
*/
|
|
|
|
_shouldLogin: function _shouldLogin() {
|
|
|
|
return this.enabled &&
|
2011-05-20 01:08:07 +00:00
|
|
|
!Services.io.offline &&
|
2010-12-21 04:01:34 +00:00
|
|
|
!this.isLoggedIn;
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2009-03-10 11:15:52 +00:00
|
|
|
/**
|
2009-07-23 03:48:41 +00:00
|
|
|
* Determine if a sync should run.
|
2011-04-19 19:33:47 +00:00
|
|
|
*
|
2010-04-08 21:03:10 +00:00
|
|
|
* @param ignore [optional]
|
|
|
|
* array of reasons to ignore when checking
|
2009-03-10 11:15:52 +00:00
|
|
|
*
|
|
|
|
* @return Reason for not syncing; not-truthy if sync should run
|
|
|
|
*/
|
2010-04-08 21:03:10 +00:00
|
|
|
_checkSync: function WeaveSvc__checkSync(ignore) {
|
2009-03-10 11:15:52 +00:00
|
|
|
let reason = "";
|
|
|
|
if (!this.enabled)
|
|
|
|
reason = kSyncWeaveDisabled;
|
2011-05-20 01:08:07 +00:00
|
|
|
else if (Services.io.offline)
|
2009-03-10 11:30:36 +00:00
|
|
|
reason = kSyncNetworkOffline;
|
2009-10-07 17:47:43 +00:00
|
|
|
else if (Status.minimumNextSync > Date.now())
|
2009-09-16 01:38:52 +00:00
|
|
|
reason = kSyncBackoffNotMet;
|
2011-01-11 05:51:29 +00:00
|
|
|
else if ((Status.login == MASTER_PASSWORD_LOCKED) &&
|
|
|
|
Utils.mpLocked())
|
|
|
|
reason = kSyncMasterPasswordLocked;
|
2010-04-08 21:03:10 +00:00
|
|
|
else if (Svc.Prefs.get("firstSync") == "notReady")
|
|
|
|
reason = kFirstSyncChoiceNotMade;
|
|
|
|
|
|
|
|
if (ignore && ignore.indexOf(reason) != -1)
|
|
|
|
return "";
|
2009-03-10 11:15:52 +00:00
|
|
|
|
2009-07-23 03:48:41 +00:00
|
|
|
return reason;
|
|
|
|
},
|
|
|
|
|
2010-12-16 20:00:57 +00:00
|
|
|
sync: function sync() {
|
2011-02-25 22:42:59 +00:00
|
|
|
let dateStr = new Date().toLocaleFormat(LOG_DATE_FORMAT);
|
2011-07-21 20:53:04 +00:00
|
|
|
this._log.debug("User-Agent: " + SyncStorageRequest.prototype.userAgent);
|
2011-02-25 22:42:59 +00:00
|
|
|
this._log.info("Starting sync at " + dateStr);
|
2011-01-11 05:51:29 +00:00
|
|
|
this._catch(function () {
|
2010-12-21 04:01:34 +00:00
|
|
|
// Make sure we're logged in.
|
|
|
|
if (this._shouldLogin()) {
|
2011-01-11 05:51:29 +00:00
|
|
|
this._log.debug("In sync: should login.");
|
|
|
|
if (!this.login()) {
|
|
|
|
this._log.debug("Not syncing: login returned false.");
|
|
|
|
return;
|
|
|
|
}
|
2010-12-21 04:01:34 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._log.trace("In sync: no need to login.");
|
|
|
|
}
|
|
|
|
return this._lockedSync.apply(this, arguments);
|
2011-01-11 05:51:29 +00:00
|
|
|
})();
|
2010-12-16 20:00:57 +00:00
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2009-03-10 11:07:24 +00:00
|
|
|
/**
|
|
|
|
* Sync up engines with the server.
|
|
|
|
*/
|
2010-12-16 20:00:57 +00:00
|
|
|
_lockedSync: function _lockedSync()
|
2011-04-19 19:33:47 +00:00
|
|
|
this._lock("service.js: sync",
|
2010-12-16 20:00:57 +00:00
|
|
|
this._notify("sync", "", function() {
|
2010-11-30 00:41:17 +00:00
|
|
|
|
|
|
|
this._log.info("In sync().");
|
2009-11-12 18:11:54 +00:00
|
|
|
|
2010-05-03 21:01:08 +00:00
|
|
|
let syncStartTime = Date.now();
|
|
|
|
|
2010-04-06 03:53:31 +00:00
|
|
|
Status.resetSync();
|
2009-09-30 01:33:41 +00:00
|
|
|
|
2009-09-29 20:02:08 +00:00
|
|
|
// Make sure we should sync or record why we shouldn't
|
2009-03-10 11:15:52 +00:00
|
|
|
let reason = this._checkSync();
|
2009-09-29 20:02:08 +00:00
|
|
|
if (reason) {
|
2011-02-03 18:23:39 +00:00
|
|
|
if (reason == kSyncNetworkOffline) {
|
|
|
|
Status.sync = LOGIN_FAILED_NETWORK_ERROR;
|
|
|
|
}
|
2009-03-25 02:23:53 +00:00
|
|
|
// this is a purposeful abort rather than a failure, so don't set
|
2009-09-16 01:38:52 +00:00
|
|
|
// any status bits
|
2009-03-10 11:15:52 +00:00
|
|
|
reason = "Can't sync: " + reason;
|
|
|
|
throw reason;
|
2009-02-03 23:50:41 +00:00
|
|
|
}
|
2008-12-27 20:15:45 +00:00
|
|
|
|
2010-04-06 03:53:31 +00:00
|
|
|
// if we don't have a node, get one. if that fails, retry in 10 minutes
|
|
|
|
if (this.clusterURL == "" && !this._setCluster()) {
|
2010-07-16 00:02:06 +00:00
|
|
|
Status.sync = NO_SYNC_NODE_FOUND;
|
2010-04-06 03:53:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Ping the server with a special info request once a day.
|
2009-12-17 03:08:36 +00:00
|
|
|
let infoURL = this.infoURL;
|
|
|
|
let now = Math.floor(Date.now() / 1000);
|
|
|
|
let lastPing = Svc.Prefs.get("lastPing", 0);
|
|
|
|
if (now - lastPing > 86400) { // 60 * 60 * 24
|
2009-12-22 21:46:34 +00:00
|
|
|
infoURL += "?v=" + WEAVE_VERSION;
|
2009-12-17 03:08:36 +00:00
|
|
|
Svc.Prefs.set("lastPing", now);
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:30:40 +00:00
|
|
|
// Figure out what the last modified time is for each collection
|
2011-02-01 04:55:48 +00:00
|
|
|
let info = this._fetchInfo(infoURL);
|
2009-09-04 04:30:40 +00:00
|
|
|
|
|
|
|
// Convert the response to an object and read out the modified times
|
2009-12-03 03:03:27 +00:00
|
|
|
for each (let engine in [Clients].concat(Engines.getAll()))
|
2009-09-10 18:04:36 +00:00
|
|
|
engine.lastModified = info.obj[engine.name] || 0;
|
2009-09-04 04:30:40 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
if (!(this._remoteSetup(info)))
|
2010-05-06 00:16:17 +00:00
|
|
|
throw "aborting sync, remote setup failed";
|
|
|
|
|
|
|
|
// Make sure we have an up-to-date list of clients before sending commands
|
2011-06-25 13:54:50 +00:00
|
|
|
this._log.debug("Refreshing client list.");
|
|
|
|
if (!this._syncEngine(Clients)) {
|
|
|
|
// Clients is an engine like any other; it can fail with a 401,
|
|
|
|
// and we can elect to abort the sync.
|
|
|
|
this._log.warn("Client engine sync failed. Aborting.");
|
|
|
|
return;
|
|
|
|
}
|
2008-07-31 08:02:41 +00:00
|
|
|
|
2010-05-06 00:16:17 +00:00
|
|
|
// Wipe data in the desired direction if necessary
|
|
|
|
switch (Svc.Prefs.get("firstSync")) {
|
|
|
|
case "resetClient":
|
|
|
|
this.resetClient(Engines.getEnabled().map(function(e) e.name));
|
|
|
|
break;
|
|
|
|
case "wipeClient":
|
|
|
|
this.wipeClient(Engines.getEnabled().map(function(e) e.name));
|
|
|
|
break;
|
|
|
|
case "wipeRemote":
|
|
|
|
this.wipeRemote(Engines.getEnabled().map(function(e) e.name));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-03-16 23:39:08 +00:00
|
|
|
if (Clients.localCommands) {
|
2009-11-12 20:44:33 +00:00
|
|
|
try {
|
2011-08-04 23:19:02 +00:00
|
|
|
if (!(Clients.processIncomingCommands())) {
|
2009-11-12 20:44:33 +00:00
|
|
|
Status.sync = ABORT_SYNC_COMMAND;
|
|
|
|
throw "aborting sync, process commands said so";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repeat remoteSetup in-case the commands forced us to reset
|
2010-11-30 00:41:17 +00:00
|
|
|
if (!(this._remoteSetup(info)))
|
2009-11-12 20:44:33 +00:00
|
|
|
throw "aborting sync, remote setup failed after processing commands";
|
|
|
|
}
|
|
|
|
finally {
|
2011-06-25 13:54:50 +00:00
|
|
|
// Always immediately attempt to push back the local client (now
|
|
|
|
// without commands).
|
|
|
|
// Note that we don't abort here; if there's a 401 because we've
|
|
|
|
// been reassigned, we'll handle it around another engine.
|
2010-09-11 16:39:21 +00:00
|
|
|
this._syncEngine(Clients);
|
2009-11-12 20:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 13:22:49 +00:00
|
|
|
// Update engines because it might change what we sync.
|
2011-10-20 23:21:17 +00:00
|
|
|
this._updateEnabledEngines();
|
2009-09-25 22:05:09 +00:00
|
|
|
|
2009-01-06 21:54:18 +00:00
|
|
|
try {
|
2009-10-08 20:51:22 +00:00
|
|
|
for each (let engine in Engines.getEnabled()) {
|
2009-03-10 11:07:24 +00:00
|
|
|
// If there's any problems with syncing the engine, report the failure
|
2009-10-07 17:47:43 +00:00
|
|
|
if (!(this._syncEngine(engine)) || Status.enforceBackoff) {
|
2009-03-10 11:07:24 +00:00
|
|
|
this._log.info("Aborting sync");
|
|
|
|
break;
|
2009-01-06 21:54:18 +00:00
|
|
|
}
|
2008-04-16 00:21:34 +00:00
|
|
|
}
|
2008-07-09 00:44:00 +00:00
|
|
|
|
2011-06-25 13:54:50 +00:00
|
|
|
// If _syncEngine fails for a 401, we might not have a cluster URL here.
|
|
|
|
// If that's the case, break out of this immediately, rather than
|
|
|
|
// throwing an exception when trying to fetch metaURL.
|
|
|
|
if (!this.clusterURL) {
|
|
|
|
this._log.debug("Aborting sync, no cluster URL: " +
|
|
|
|
"not uploading new meta/global.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-25 17:05:21 +00:00
|
|
|
// Upload meta/global if any engines changed anything
|
|
|
|
let meta = Records.get(this.metaURL);
|
2010-09-13 15:17:37 +00:00
|
|
|
if (meta.isNew || meta.changed) {
|
2010-11-30 00:41:17 +00:00
|
|
|
new Resource(this.metaURL).put(meta);
|
2010-09-13 15:17:37 +00:00
|
|
|
delete meta.isNew;
|
|
|
|
delete meta.changed;
|
|
|
|
}
|
2010-03-25 17:05:21 +00:00
|
|
|
|
2011-08-26 21:01:35 +00:00
|
|
|
// If there were no sync engine failures
|
|
|
|
if (Status.service != SYNC_FAILED_PARTIAL) {
|
2009-03-10 11:07:24 +00:00
|
|
|
Svc.Prefs.set("lastSync", new Date().toString());
|
2009-10-07 17:47:43 +00:00
|
|
|
Status.sync = SYNC_SUCCEEDED;
|
2009-03-10 11:07:24 +00:00
|
|
|
}
|
2009-01-06 21:54:18 +00:00
|
|
|
} finally {
|
2009-11-26 01:08:51 +00:00
|
|
|
Svc.Prefs.reset("firstSync");
|
2011-08-26 21:01:35 +00:00
|
|
|
|
|
|
|
let syncTime = ((Date.now() - syncStartTime) / 1000).toFixed(2);
|
|
|
|
let dateStr = new Date().toLocaleFormat(LOG_DATE_FORMAT);
|
|
|
|
this._log.info("Sync completed at " + dateStr
|
|
|
|
+ " after " + syncTime + " secs.");
|
2008-07-09 00:44:00 +00:00
|
|
|
}
|
2010-12-16 20:00:57 +00:00
|
|
|
}))(),
|
2009-03-10 11:07:24 +00:00
|
|
|
|
2009-09-25 22:05:09 +00:00
|
|
|
|
2010-09-07 16:44:01 +00:00
|
|
|
_updateEnabledEngines: function _updateEnabledEngines() {
|
2011-06-27 13:24:33 +00:00
|
|
|
this._log.info("Updating enabled engines: " + SyncScheduler.numClients + " clients.");
|
2010-09-07 16:44:01 +00:00
|
|
|
let meta = Records.get(this.metaURL);
|
2010-09-13 15:17:37 +00:00
|
|
|
if (meta.isNew || !meta.payload.engines)
|
2010-09-07 16:44:01 +00:00
|
|
|
return;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-07 01:23:38 +00:00
|
|
|
// If we're the only client, and no engines are marked as enabled,
|
|
|
|
// thumb our noses at the server data: it can't be right.
|
|
|
|
// Belt-and-suspenders approach to Bug 615926.
|
2011-06-27 13:24:33 +00:00
|
|
|
if ((SyncScheduler.numClients <= 1) &&
|
2010-12-07 01:23:38 +00:00
|
|
|
([e for (e in meta.payload.engines) if (e != "clients")].length == 0)) {
|
|
|
|
this._log.info("One client and no enabled engines: not touching local engine status.");
|
|
|
|
return;
|
|
|
|
}
|
2010-09-07 16:44:01 +00:00
|
|
|
|
|
|
|
this._ignorePrefObserver = true;
|
|
|
|
|
|
|
|
let enabled = [eng.name for each (eng in Engines.getEnabled())];
|
|
|
|
for (let engineName in meta.payload.engines) {
|
2011-03-01 19:35:49 +00:00
|
|
|
if (engineName == "clients") {
|
|
|
|
// Clients is special.
|
|
|
|
continue;
|
|
|
|
}
|
2010-09-07 16:44:01 +00:00
|
|
|
let index = enabled.indexOf(engineName);
|
|
|
|
if (index != -1) {
|
|
|
|
// The engine is enabled locally. Nothing to do.
|
|
|
|
enabled.splice(index, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let engine = Engines.get(engineName);
|
|
|
|
if (!engine) {
|
|
|
|
// The engine doesn't exist locally. Nothing to do.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-09-08 19:09:40 +00:00
|
|
|
if (Svc.Prefs.get("engineStatusChanged." + engine.prefName, false)) {
|
2010-09-07 16:44:01 +00:00
|
|
|
// The engine was disabled locally. Wipe server data and
|
|
|
|
// disable it everywhere.
|
|
|
|
this._log.trace("Wiping data for " + engineName + " engine.");
|
|
|
|
engine.wipeServer();
|
|
|
|
delete meta.payload.engines[engineName];
|
|
|
|
meta.changed = true;
|
|
|
|
} else {
|
|
|
|
// The engine was enabled remotely. Enable it locally.
|
|
|
|
this._log.trace(engineName + " engine was enabled remotely.");
|
|
|
|
engine.enabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any remaining engines were either enabled locally or disabled remotely.
|
2011-08-20 00:17:58 +00:00
|
|
|
for each (let engineName in enabled) {
|
2010-09-08 19:09:40 +00:00
|
|
|
let engine = Engines.get(engineName);
|
|
|
|
if (Svc.Prefs.get("engineStatusChanged." + engine.prefName, false)) {
|
2010-09-07 16:44:01 +00:00
|
|
|
this._log.trace("The " + engineName + " engine was enabled locally.");
|
|
|
|
} else {
|
|
|
|
this._log.trace("The " + engineName + " engine was disabled remotely.");
|
2010-09-08 19:09:40 +00:00
|
|
|
engine.enabled = false;
|
2010-09-07 16:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-08 19:09:40 +00:00
|
|
|
Svc.Prefs.resetBranch("engineStatusChanged.");
|
2010-09-07 16:44:01 +00:00
|
|
|
this._ignorePrefObserver = false;
|
|
|
|
},
|
|
|
|
|
2011-06-25 13:54:50 +00:00
|
|
|
// Returns true if sync should proceed.
|
|
|
|
// false / no return value means sync should be aborted.
|
2008-03-30 15:40:23 +00:00
|
|
|
_syncEngine: function WeaveSvc__syncEngine(engine) {
|
2009-02-12 06:08:56 +00:00
|
|
|
try {
|
2009-06-05 22:34:32 +00:00
|
|
|
engine.sync();
|
2009-08-20 23:26:22 +00:00
|
|
|
return true;
|
2009-02-12 06:08:56 +00:00
|
|
|
}
|
2008-12-05 08:39:54 +00:00
|
|
|
catch(e) {
|
2011-06-25 13:54:50 +00:00
|
|
|
// Maybe a 401, cluster update perhaps needed?
|
|
|
|
if (e.status == 401) {
|
|
|
|
// Log out and clear the cluster URL pref. That will make us perform
|
|
|
|
// cluster detection and password check on next sync, which handles
|
|
|
|
// both causes of 401s; in either case, we won't proceed with this
|
2011-08-26 21:01:35 +00:00
|
|
|
// sync so return false, but kick off a sync for next time.
|
2011-06-25 13:54:50 +00:00
|
|
|
this.logout();
|
|
|
|
Svc.Prefs.reset("clusterURL");
|
|
|
|
Utils.nextTick(this.sync, this);
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-01 00:17:20 +00:00
|
|
|
return true;
|
2008-05-24 06:58:53 +00:00
|
|
|
}
|
2007-12-15 02:07:25 +00:00
|
|
|
},
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
/**
|
|
|
|
* Silently fixes case issues.
|
|
|
|
*/
|
|
|
|
syncKeyNeedsUpgrade: function syncKeyNeedsUpgrade() {
|
|
|
|
let p = this.passphrase;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Check whether it's already a key that we generated.
|
|
|
|
if (Utils.isPassphrase(p)) {
|
|
|
|
this._log.info("Sync key is up-to-date: no need to upgrade.");
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
return true;
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
/**
|
|
|
|
* If we have a passphrase, rather than a 25-alphadigit sync key,
|
|
|
|
* use the provided sync ID to bootstrap it using PBKDF2.
|
2011-04-19 19:33:47 +00:00
|
|
|
*
|
2010-11-30 00:41:17 +00:00
|
|
|
* Store the new 'passphrase' back into the identity manager.
|
2011-04-19 19:33:47 +00:00
|
|
|
*
|
2010-11-30 00:41:17 +00:00
|
|
|
* We can check this as often as we want, because once it's done the
|
|
|
|
* check will no longer succeed. It only matters that it happens after
|
|
|
|
* we decide to bump the server storage version.
|
|
|
|
*/
|
|
|
|
upgradeSyncKey: function upgradeSyncKey(syncID) {
|
|
|
|
let p = this.passphrase;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Check whether it's already a key that we generated.
|
|
|
|
if (!this.syncKeyNeedsUpgrade(p))
|
|
|
|
return true;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Otherwise, let's upgrade it.
|
|
|
|
// N.B., we persist the sync key without testing it first...
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
let s = btoa(syncID); // It's what WeaveCrypto expects. *sigh*
|
|
|
|
let k = Utils.derivePresentableKeyFromPassphrase(p, s, PBKDF2_KEY_BYTES); // Base 32.
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
if (!k) {
|
|
|
|
this._log.error("No key resulted from derivePresentableKeyFromPassphrase. Failing upgrade.");
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.info("Upgrading sync key...");
|
|
|
|
this.passphrase = k;
|
|
|
|
this._log.info("Saving upgraded sync key...");
|
|
|
|
this.persistLogin();
|
|
|
|
this._log.info("Done saving.");
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2009-11-12 20:44:33 +00:00
|
|
|
_freshStart: function WeaveSvc__freshStart() {
|
2010-11-30 00:41:17 +00:00
|
|
|
this._log.info("Fresh start. Resetting client and considering key upgrade.");
|
2009-06-06 05:21:22 +00:00
|
|
|
this.resetClient();
|
2010-12-11 03:41:55 +00:00
|
|
|
CollectionKeys.clear();
|
2010-11-30 00:41:17 +00:00
|
|
|
this.upgradeSyncKey(this.syncID);
|
2009-02-19 12:09:55 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
let meta = new WBORecord("meta", "global");
|
2010-03-25 16:52:45 +00:00
|
|
|
meta.payload.syncID = this.syncID;
|
2010-03-16 23:31:55 +00:00
|
|
|
meta.payload.storageVersion = STORAGE_VERSION;
|
2010-09-13 15:17:37 +00:00
|
|
|
meta.isNew = true;
|
2010-03-16 23:31:55 +00:00
|
|
|
|
|
|
|
this._log.debug("New metadata record: " + JSON.stringify(meta.payload));
|
2011-10-20 23:21:17 +00:00
|
|
|
let resp = new Resource(this.metaURL).put(meta);
|
|
|
|
if (!resp.success)
|
2010-03-16 23:31:55 +00:00
|
|
|
throw resp;
|
2010-11-30 00:41:17 +00:00
|
|
|
Records.set(this.metaURL, meta);
|
2009-12-03 01:57:13 +00:00
|
|
|
|
|
|
|
// Wipe everything we know about except meta because we just uploaded it
|
|
|
|
let collections = [Clients].concat(Engines.getAll()).map(function(engine) {
|
|
|
|
return engine.name;
|
|
|
|
});
|
2011-10-20 23:21:17 +00:00
|
|
|
this.wipeServer(collections);
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
// Generate, upload, and download new keys. Do this last so we don't wipe
|
|
|
|
// them...
|
2010-11-30 00:41:17 +00:00
|
|
|
this.generateNewSymmetricKeys();
|
2009-07-23 01:49:07 +00:00
|
|
|
},
|
|
|
|
|
2009-02-27 06:14:32 +00:00
|
|
|
/**
|
2010-04-08 00:06:37 +00:00
|
|
|
* Wipe user data from the server.
|
2009-03-12 06:33:14 +00:00
|
|
|
*
|
2010-04-08 00:06:37 +00:00
|
|
|
* @param collections [optional]
|
2011-10-20 23:21:17 +00:00
|
|
|
* Array of collections to wipe. If not given, all collections are wiped.
|
|
|
|
*
|
|
|
|
* @param includeKeys [optional]
|
|
|
|
* If true, keys/pubkey and keys/privkey are deleted from the server.
|
|
|
|
* This is false by default, which will cause the usual upgrade paths
|
|
|
|
* to leave those keys on the server. This is to solve Bug 614737: old
|
|
|
|
* clients check for keys *before* checking storage versions.
|
2011-04-19 19:33:47 +00:00
|
|
|
*
|
2011-10-20 23:21:17 +00:00
|
|
|
* Note that this parameter only has an effect if `collections` is not
|
|
|
|
* passed. If you explicitly pass a list of collections, they will be
|
|
|
|
* processed regardless of the value of `includeKeys`.
|
2009-02-27 06:14:32 +00:00
|
|
|
*/
|
2011-10-20 23:21:17 +00:00
|
|
|
wipeServer: function wipeServer(collections, includeKeyPairs)
|
2010-08-05 16:24:45 +00:00
|
|
|
this._notify("wipe-server", "", function() {
|
2010-04-08 00:06:37 +00:00
|
|
|
if (!collections) {
|
2011-10-20 23:21:17 +00:00
|
|
|
collections = [];
|
|
|
|
let info = new Resource(this.infoURL).get();
|
|
|
|
for (let name in info.obj) {
|
|
|
|
if (includeKeyPairs || (name != "keys"))
|
|
|
|
collections.push(name);
|
2011-10-12 20:53:13 +00:00
|
|
|
}
|
2010-04-08 00:06:37 +00:00
|
|
|
}
|
|
|
|
for each (let name in collections) {
|
2010-08-05 16:24:45 +00:00
|
|
|
let url = this.storageURL + name;
|
2011-10-20 23:21:17 +00:00
|
|
|
let response = new Resource(url).delete();
|
2010-08-05 16:24:45 +00:00
|
|
|
if (response.status != 200 && response.status != 404) {
|
2011-10-20 23:21:17 +00:00
|
|
|
throw "Aborting wipeServer. Server responded with "
|
|
|
|
+ response.status + " response for " + url;
|
2009-02-27 06:14:32 +00:00
|
|
|
}
|
2009-02-12 07:54:20 +00:00
|
|
|
}
|
2010-08-05 16:24:45 +00:00
|
|
|
})(),
|
2009-02-19 12:09:55 +00:00
|
|
|
|
2009-02-27 06:36:14 +00:00
|
|
|
/**
|
2009-03-12 06:33:14 +00:00
|
|
|
* Wipe all local user data.
|
|
|
|
*
|
2009-03-20 07:13:16 +00:00
|
|
|
* @param engines [optional]
|
|
|
|
* Array of engine names to wipe. If not given, all engines are used.
|
2009-02-27 06:36:14 +00:00
|
|
|
*/
|
2009-06-06 05:21:20 +00:00
|
|
|
wipeClient: function WeaveSvc_wipeClient(engines)
|
|
|
|
this._catch(this._notify("wipe-client", "", function() {
|
2009-03-20 07:13:16 +00:00
|
|
|
// If we don't have any engines, reset the service and wipe all engines
|
|
|
|
if (!engines) {
|
|
|
|
// Clear out any service data
|
2009-06-06 05:21:18 +00:00
|
|
|
this.resetService();
|
2009-03-20 07:13:16 +00:00
|
|
|
|
|
|
|
engines = [Clients].concat(Engines.getAll());
|
|
|
|
}
|
|
|
|
// Convert the array of names into engines
|
|
|
|
else
|
|
|
|
engines = Engines.get(engines);
|
2009-03-12 06:33:14 +00:00
|
|
|
|
2009-10-12 23:22:54 +00:00
|
|
|
// Fully wipe each engine if it's able to decrypt data
|
2009-03-20 07:13:16 +00:00
|
|
|
for each (let engine in engines)
|
2010-10-05 18:32:56 +00:00
|
|
|
if (engine.canDecrypt())
|
2009-10-12 23:22:54 +00:00
|
|
|
engine.wipeClient();
|
2009-10-12 23:45:40 +00:00
|
|
|
|
|
|
|
// Save the password/passphrase just in-case they aren't restored by sync
|
|
|
|
this.persistLogin();
|
2009-06-06 05:21:20 +00:00
|
|
|
}))(),
|
2009-03-12 06:33:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wipe all remote user data by wiping the server then telling each remote
|
|
|
|
* client to wipe itself.
|
|
|
|
*
|
2009-03-20 07:13:16 +00:00
|
|
|
* @param engines [optional]
|
|
|
|
* Array of engine names to wipe. If not given, all engines are used.
|
2009-03-12 06:33:14 +00:00
|
|
|
*/
|
2009-06-06 05:21:24 +00:00
|
|
|
wipeRemote: function WeaveSvc_wipeRemote(engines)
|
|
|
|
this._catch(this._notify("wipe-remote", "", function() {
|
2011-08-04 23:19:02 +00:00
|
|
|
// Make sure stuff gets uploaded.
|
2010-05-06 00:16:17 +00:00
|
|
|
this.resetClient(engines);
|
2009-12-11 02:39:51 +00:00
|
|
|
|
2011-08-04 23:19:02 +00:00
|
|
|
// Clear out any server data.
|
2009-12-11 02:39:51 +00:00
|
|
|
this.wipeServer(engines);
|
2009-11-12 20:44:33 +00:00
|
|
|
|
2011-08-04 23:19:02 +00:00
|
|
|
// Only wipe the engines provided.
|
|
|
|
if (engines) {
|
|
|
|
engines.forEach(function(e) Clients.sendCommand("wipeEngine", [e]), this);
|
|
|
|
}
|
|
|
|
// Tell the remote machines to wipe themselves.
|
|
|
|
else {
|
|
|
|
Clients.sendCommand("wipeAll", []);
|
|
|
|
}
|
2009-12-11 02:39:51 +00:00
|
|
|
|
2011-08-04 23:19:02 +00:00
|
|
|
// Make sure the changed clients get updated.
|
2009-12-11 02:39:51 +00:00
|
|
|
Clients.sync();
|
2009-06-06 05:21:24 +00:00
|
|
|
}))(),
|
2009-03-12 06:33:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset local service information like logs, sync times, caches.
|
|
|
|
*/
|
2009-06-06 05:21:18 +00:00
|
|
|
resetService: function WeaveSvc_resetService()
|
|
|
|
this._catch(this._notify("reset-service", "", function() {
|
2010-12-15 22:36:53 +00:00
|
|
|
this._log.info("Service reset.");
|
2009-02-19 12:09:55 +00:00
|
|
|
|
2009-02-27 06:36:14 +00:00
|
|
|
// Pretend we've never synced to the server and drop cached data
|
2010-03-25 16:52:45 +00:00
|
|
|
this.syncID = "";
|
2011-10-20 23:21:17 +00:00
|
|
|
Svc.Prefs.reset("lastSync");
|
2010-11-30 00:41:17 +00:00
|
|
|
Records.clearCache();
|
2009-06-06 05:21:18 +00:00
|
|
|
}))(),
|
2009-03-12 06:33:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the client by getting rid of any local server data and client data.
|
|
|
|
*
|
2009-03-20 07:13:16 +00:00
|
|
|
* @param engines [optional]
|
|
|
|
* Array of engine names to reset. If not given, all engines are used.
|
2009-03-12 06:33:14 +00:00
|
|
|
*/
|
2009-06-06 05:21:22 +00:00
|
|
|
resetClient: function WeaveSvc_resetClient(engines)
|
|
|
|
this._catch(this._notify("reset-client", "", function() {
|
2009-03-20 07:13:16 +00:00
|
|
|
// If we don't have any engines, reset everything including the service
|
|
|
|
if (!engines) {
|
|
|
|
// Clear out any service data
|
2009-06-06 05:21:18 +00:00
|
|
|
this.resetService();
|
2009-03-20 07:13:16 +00:00
|
|
|
|
|
|
|
engines = [Clients].concat(Engines.getAll());
|
|
|
|
}
|
|
|
|
// Convert the array of names into engines
|
|
|
|
else
|
|
|
|
engines = Engines.get(engines);
|
2009-02-19 12:09:55 +00:00
|
|
|
|
2009-02-27 06:36:14 +00:00
|
|
|
// Have each engine drop any temporary meta data
|
2009-03-20 07:13:16 +00:00
|
|
|
for each (let engine in engines)
|
2009-06-05 22:34:34 +00:00
|
|
|
engine.resetClient();
|
2009-06-06 05:21:22 +00:00
|
|
|
}))(),
|
2009-11-12 20:44:33 +00:00
|
|
|
|
2011-08-05 02:02:49 +00:00
|
|
|
/**
|
|
|
|
* Fetch storage info from the server.
|
2011-08-04 23:19:02 +00:00
|
|
|
*
|
2011-07-14 19:11:42 +00:00
|
|
|
* @param type
|
|
|
|
* String specifying what info to fetch from the server. Must be one
|
|
|
|
* of the INFO_* values. See Sync Storage Server API spec for details.
|
|
|
|
* @param callback
|
|
|
|
* Callback function with signature (error, data) where `data' is
|
|
|
|
* the return value from the server already parsed as JSON.
|
2011-08-04 23:19:02 +00:00
|
|
|
*
|
2011-07-14 19:11:42 +00:00
|
|
|
* @return RESTRequest instance representing the request, allowing callers
|
|
|
|
* to cancel the request.
|
|
|
|
*/
|
|
|
|
getStorageInfo: function getStorageInfo(type, callback) {
|
|
|
|
if (STORAGE_INFO_TYPES.indexOf(type) == -1) {
|
|
|
|
throw "Invalid value for 'type': " + type;
|
|
|
|
}
|
2010-09-11 16:39:21 +00:00
|
|
|
|
2011-07-14 19:11:42 +00:00
|
|
|
let info_type = "info/" + type;
|
|
|
|
this._log.trace("Retrieving '" + info_type + "'...");
|
|
|
|
let url = this.userBaseURL + info_type;
|
|
|
|
return new SyncStorageRequest(url).get(function onComplete(error) {
|
|
|
|
// Note: 'this' is the request.
|
|
|
|
if (error) {
|
|
|
|
this._log.debug("Failed to retrieve '" + info_type + "': " +
|
|
|
|
Utils.exceptionStr(error));
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
if (this.response.status != 200) {
|
|
|
|
this._log.debug("Failed to retrieve '" + info_type +
|
|
|
|
"': server responded with HTTP" +
|
|
|
|
this.response.status);
|
|
|
|
return callback(this.response);
|
|
|
|
}
|
2010-09-11 16:39:21 +00:00
|
|
|
|
2011-07-14 19:11:42 +00:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(this.response.body);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.debug("Server returned invalid JSON for '" + info_type +
|
|
|
|
"': " + this.response.body);
|
|
|
|
return callback(ex);
|
|
|
|
}
|
|
|
|
this._log.trace("Successfully retrieved '" + info_type + "'.");
|
|
|
|
return callback(null, result);
|
|
|
|
});
|
|
|
|
}
|
2010-09-11 16:39:21 +00:00
|
|
|
|
2009-03-03 23:29:35 +00:00
|
|
|
};
|
2009-12-15 22:21:12 +00:00
|
|
|
|
|
|
|
// Load Weave on the first time this file is loaded
|
2011-05-20 01:08:35 +00:00
|
|
|
let Service = new WeaveSvc();
|
2010-08-25 22:49:44 +00:00
|
|
|
Service.onStartup();
|