Ejemplo n.º 1
0
def main():
    global logger
    args = processArgs()
    kwargs = processMountOpts(args.mountopts)
    logger = Util.setupLogging(args.verbose)

    try:
        argsDict = vars(args)
        def getarg(name):
            """ Extract a value from either the kwargs, or the regular args """
            return kwargs.get(name) or argsDict.get(name)

        # Extract the password file and program, if they exist.  Names differ, so getarg doesn't work.
        pwfile = kwargs.get('pwfile') or argsDict.get('passwordfile')
        pwprog = kwargs.get('pwprog') or argsDict.get('passwordprog')

        password = Util.getPassword(getarg('password'), pwfile, pwprog, prompt="Password for %s: " % (getarg('client')))
        args.password = None
        (tardis, cache, crypt) = Util.setupDataConnection(getarg('database'), getarg('client'), password, getarg('keys'), getarg('dbname'), getarg('dbdir'))
    except TardisDB.AuthenticationException as e:
        logger.error("Authentication failed.  Bad password")
        #if args.exceptions:
            #logger.exception(e)
        sys.exit(1)
    except Exception as e:
        logger.error("DB Connection failed: %s", e)
        sys.exit(1)

    delTardisKeys(kwargs)

    fs = TardisFS(tardis, cache, crypt, args)
    FUSE(fs, args.mountpoint[0], debug=args.debug, nothreads=True, foreground=args.foreground, **kwargs)
Ejemplo n.º 2
0
def main():
    global logger
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('')
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile,
                                args.passwordurl, args.passwordprog)

    crypto = TardisCrypto.TardisCrypto(password, args.client)
    token = crypto.createToken()

    #logger.info("Created token: %s", token)
    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, token=token, backup=False)
    (f, c) = db.getKeys()
    crypto.setKeys(f, c)

    cacheDir = CacheDir.CacheDir(os.path.join(args.database, args.client))

    #if args.sigs:
    #    generateSignatures(db, cacheDir)
    if args.filenames:
        encryptFilenames(db, crypto)
    if args.files:
        encryptFiles(db, crypto, cacheDir)
    if args.dirhash:
        generateDirHashes(db, crypto, cacheDir)
    if args.meta:
        generateMetadata(db, cacheDir)
Ejemplo n.º 3
0
def getDB(crypt, password, new=False, allowRemote=True, allowUpgrade=False):
    loc = urlparse.urlparse(args.database)
    # This is basically the same code as in Util.setupDataConnection().  Should consider moving to it.
    if (loc.scheme == 'http') or (loc.scheme == 'https'):
        if not allowRemote:
            raise Exception("This command cannot be executed remotely.  You must execute it on the server directly.")
        # If no port specified, insert the port
        if loc.port is None:
            netloc = loc.netloc + ":" + Defaults.getDefault('TARDIS_REMOTE_PORT')
            dbLoc = urlparse.urlunparse((loc.scheme, netloc, loc.path, loc.params, loc.query, loc.fragment))
        else:
            dbLoc = args.database
        tardisdb = RemoteDB.RemoteDB(dbLoc, args.client)
        cache = tardisdb
    else:
        basedir = os.path.join(args.database, args.client)
        if not args.dbdir:
            dbdir = os.path.join(args.database, args.client)
        else:
            dbdir = os.path.join(args.dbdir, args.client)
        dbfile = os.path.join(dbdir, args.dbname)
        if new and os.path.exists(dbfile):
            raise Exception("Database for client %s already exists." % (args.client))

        cache = CacheDir.CacheDir(basedir, 2, 2, create=new)
        schema = args.schema if new else None
        tardisdb = TardisDB.TardisDB(dbfile, backup=False, initialize=schema, allow_upgrade=allowUpgrade)

    if tardisdb.needsAuthentication():
        if password is None:
            password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client), allowNone=False, confirm=False)
        Util.authenticate(tardisdb, args.client, password)

    return (tardisdb, cache)
Ejemplo n.º 4
0
def main():
    global logger
    progressbar.streams.wrap_stderr()
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('')
    args = processArgs()
    password = Util.getPassword(args.password,
                                args.passwordfile,
                                args.passwordprog,
                                allowNone=False)

    (db, cacheDir,
     crypto) = Util.setupDataConnection(args.database, args.client, password,
                                        args.keys, args.dbname, args.dbdir)

    if args.input:
        files = json.load(open(args.input, "r"))
        for x in files:
            try:
                processFile(args.output, crypto, files[x], args.compress,
                            args.signature)
            except Exception as e:
                print(f"----> {str(e)} Processing {files[x]}")
                traceback.print_exc()

    for name in args.names:
        try:
            processFile(args.output, crypto, name, args.signature)
        except Exception as e:
            print(f"----> {str(e)} Processing {name}")
            traceback.print_exc()
Ejemplo n.º 5
0
def main():
    global logger
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('')
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordprog)

    (_, _, crypto) = Util.setupDataConnection(args.database, args.client, password, args.keys, args.dbname, args.dbdir)

    data = args.names
    if not data:
        tty = os.isatty(0)
        if not tty:
            data = list(map(str.strip, sys.stdin.readlines()))
        else:
            data = reader(args.quiet)

    for i in data:
        if i:
            if not args.quiet:
                print(i, " \t => \t", end=' ')
            try:
                if (args.encrypt):
                    print(crypto.encryptPath(i))
                else:
                    print(crypto.decryptPath(i))
            except Exception as e:
                print("Caught exception: " + str(e))
Ejemplo n.º 6
0
def main():
    global logger
    args = processArgs()
    kwargs = processMountOpts(args.mountopts)
    logger = Util.setupLogging(args.verbose)

    try:
        argsDict = vars(args)
        def getarg(name):
            """ Extract a value from either the kwargs, or the regular args """
            return kwargs.get(name) or argsDict.get(name)

        # Extract the password file and program, if they exist.  Names differ, so getarg doesn't work.
        pwfile = kwargs.get('pwfile') or argsDict.get('passwordfile')
        pwprog = kwargs.get('pwprog') or argsDict.get('passwordprog')

        password = Util.getPassword(getarg('password'), pwfile, pwprog, prompt="Password for %s: " % (getarg('client')))
        args.password = None
        (tardis, cache, crypt) = Util.setupDataConnection(getarg('database'), getarg('client'), password, getarg('keys'), getarg('dbname'), getarg('dbdir'))
    except TardisDB.AuthenticationException as e:
        logger.error("Authentication failed.  Bad password")
        #if args.exceptions:
            #logger.exception(e)
        sys.exit(1)
    except Exception as e:
        logger.error("DB Connection failed: %s", e)
        sys.exit(1)

    delTardisKeys(kwargs)

    fs = TardisFS(tardis, cache, crypt, args)
    FUSE(fs, args.mountpoint[0], debug=args.debug, nothreads=True, foreground=args.foreground, **kwargs)
Ejemplo n.º 7
0
def main():
    global logger
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('')
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile,
                                args.passwordprog)

    (_, _, crypto) = Util.setupDataConnection(args.database, args.client,
                                              password, args.keys, args.dbname,
                                              args.dbdir)

    data = args.names
    if not data:
        tty = os.isatty(0)
        if not tty:
            data = list(map(str.strip, sys.stdin.readlines()))
        else:
            data = reader(args.quiet)

    for i in data:
        if i:
            if not args.quiet:
                print(i, " \t => \t", end=' ')
            try:
                if (args.encrypt):
                    print(crypto.encryptPath(i))
                else:
                    print(crypto.decryptPath(i))
            except Exception as e:
                print("Caught exception: " + str(e))
Ejemplo n.º 8
0
def getDB(crypt, password, new=False, allowRemote=True, allowUpgrade=False):
    loc = urllib.parse.urlparse(args.database)
    # This is basically the same code as in Util.setupDataConnection().  Should consider moving to it.
    if (loc.scheme == 'http') or (loc.scheme == 'https'):
        if not allowRemote:
            raise Exception("This command cannot be executed remotely.  You must execute it on the server directly.")
        # If no port specified, insert the port
        if loc.port is None:
            netloc = loc.netloc + ":" + Defaults.getDefault('TARDIS_REMOTE_PORT')
            dbLoc = urllib.parse.urlunparse((loc.scheme, netloc, loc.path, loc.params, loc.query, loc.fragment))
        else:
            dbLoc = args.database
        tardisdb = RemoteDB.RemoteDB(dbLoc, args.client)
        cache = tardisdb
    else:
        basedir = os.path.join(args.database, args.client)
        if not args.dbdir:
            dbdir = os.path.join(args.database, args.client)
        else:
            dbdir = os.path.join(args.dbdir, args.client)
        dbfile = os.path.join(dbdir, args.dbname)
        if new and os.path.exists(dbfile):
            raise Exception("Database for client %s already exists." % (args.client))

        cache = CacheDir.CacheDir(basedir, 2, 2, create=new)
        schema = args.schema if new else None
        tardisdb = TardisDB.TardisDB(dbfile, backup=False, initialize=schema, allow_upgrade=allowUpgrade)

    if tardisdb.needsAuthentication():
        if password is None:
            password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client), allowNone=False, confirm=False)
            crypt = TardisCrypto.TardisCrypto(password, args.client)
        Util.authenticate(tardisdb, args.client, password)

    return (tardisdb, cache, crypt)
Ejemplo n.º 9
0
def main():
    global logger
    progressbar.streams.wrap_stderr()
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('')
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, allowNone=False)

    crypto = TardisCrypto.TardisCrypto(password, args.client)

    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, backup=False)

    Util.authenticate(db, args.client, password)
    (f, c) = db.getKeys()
    crypto.setKeys(f, c)

    cacheDir = CacheDir.CacheDir(os.path.join(args.database, args.client))

    if args.names or args.all:
        encryptFilenames(db, crypto)
    if args.dirs or args.all:
        generateDirHashes(db, crypto, cacheDir)
    if args.sigs or args.all:
        generateSignatures(db, crypto, cacheDir)
    if args.files or args.all:
        encryptFiles(db, crypto, cacheDir)
    if args.meta or args.all:
        generateMetadata(db, cacheDir)
Ejemplo n.º 10
0
def main():
    global logger
    progressbar.streams.wrap_stderr()
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('')
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordprog)

    crypto = TardisCrypto.TardisCrypto(password, args.client)

    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, backup=False)

    Util.authenticate(db, args.client, password)
    (f, c) = db.getKeys()
    crypto.setKeys(f, c)

    cacheDir = CacheDir.CacheDir(os.path.join(args.database, args.client))

    if args.names or args.all:
        encryptFilenames(db, crypto)
    if args.dirs or args.all:
        generateDirHashes(db, crypto, cacheDir)
    if args.sigs or args.all:
        generateSignatures(db, crypto, cacheDir)
    if args.files or args.all:
        encryptFiles(db, crypto, cacheDir)
    if args.meta or args.all:
        generateMetadata(db, cacheDir)
Ejemplo n.º 11
0
def changePassword(crypt, oldpw):
    try:
        (db, _, crypt) = getDB(oldpw)

        # Get the new password
        try:
            newpw = Util.getPassword(args.newpw,
                                     args.newpwf,
                                     args.newpwp,
                                     prompt="New Password for %s: " %
                                     (args.client),
                                     allowNone=False,
                                     confirm=True,
                                     strength=True)
        except Exception as e:
            logger.critical(str(e))
            if args.exceptions:
                logger.exception(e)
            return -1

        scheme = db.getConfigValue('CryptoScheme', 1)
        crypt2 = TardisCrypto.getCrypto(scheme, newpw, args.client)

        # Load the keys, and insert them into the crypt object, to decyrpt them
        if args.keys:
            (f, c) = Util.loadKeys(args.keys, db.getConfigValue('ClientID'))
            # No need to check here, loadKeys() throws exception if nothing set.
        else:
            (f, c) = db.getKeys()
            if f is None or c is None:
                logger.critical(
                    "No keys loaded from database.  Please specify --keys as appropriate"
                )
                raise Exception("No keys loaded")
        crypt.setKeys(f, c)

        # Grab the keys from one crypt object.
        # Need to do this because getKeys/setKeys assumes they're encrypted, and we need the raw
        # versions
        crypt2._filenameKey = crypt._filenameKey
        crypt2._contentKey = crypt._contentKey
        # Now get the encrypted versions
        (f, c) = crypt2.getKeys()

        (salt, vkey) = srp.create_salted_verification_key(args.client, newpw)

        if args.keys:
            db.beginTransaction()
            db.setSrpValues(salt, vkey)
            Util.saveKeys(args.keys, db.getConfigValue('ClientID'), f, c)
            db.commit()
        else:
            db.setKeys(salt, vkey, f, c)
        return 0
    except Exception as e:
        logger.error(str(e))
        if args.exceptions:
            logger.exception(e)
        return 1
Ejemplo n.º 12
0
def main():
    global args, logger
    tardis = None
    try:
        args = processArgs()
        logger = Util.setupLogging(args.verbose)

        setColors(Defaults.getDefault('TARDIS_LS_COLORS'))

        # Load any password info
        password = Util.getPassword(args.password,
                                    args.passwordfile,
                                    args.passwordprog,
                                    prompt="Password for %s: " % (args.client))
        args.password = None

        (tardis, _, crypt) = Util.setupDataConnection(args.database,
                                                      args.client, password,
                                                      args.keys, args.dbname,
                                                      args.dbdir)

        setupDisplay(tardis)

        if args.headers:
            doprint("Client: %s    DB: %s" % (args.client, args.database),
                    color=colors['name'],
                    eol=True)

        if args.glob:
            directories = []
            for d in args.directories:
                if not Util.isMagic(d):
                    directories.append(d)
                else:
                    directories += globPath(os.path.abspath(d), tardis, crypt)
        else:
            directories = args.directories

        for d in directories:
            d = unicode(os.path.abspath(d).decode(fsEncoding))
            if args.realpath:
                d = os.path.realpath(d)
            fInfos = collectFileInfo(d, tardis, crypt)
            recurse = args.maxdepth if args.recurse else 0
            processFile(d,
                        fInfos,
                        tardis,
                        crypt,
                        printContents=(not args.dirinfo),
                        recurse=recurse)
    except KeyboardInterrupt:
        pass
    except Exception as e:
        logger.error("Caught exception: %s", str(e))
        if args.exceptions:
            logger.exception(e)
    finally:
        if tardis:
            tardis.close()
Ejemplo n.º 13
0
def main():
    global logger
    try:
        parseArgs()
        logger = Util.setupLogging(args.verbose)

        if len(args.backup) > 2:
            logger.error(args.backup)
            logger.error("Too many backups (%d) specified.  Only one or two allowed", len(args.backup))
            sys.exit(1)

        password = Util.getPassword(
            args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client)
        )
        args.password = None
        (tardis, cache, crypt) = Util.setupDataConnection(
            args.database, args.client, password, args.keys, args.dbname, args.dbdir
        )
        password = None

        bsets = []
        for i in args.backup:
            bset = getBackupSet(tardis, i)
            if bset:
                logger.debug("Got backupset %s", str(bset))
                logger.debug("backupset: %s", bset["backupset"])
                bsets.append(bset)
            else:
                sys.exit(1)

        if len(bsets) == 1:
            bsets.append(None)

        r = Regenerator.Regenerator(cache, tardis, crypt)
        then = time.asctime(time.localtime(float(bsets[0]["starttime"]))) + "  (" + bsets[0]["name"] + ")"
        if bsets[1]:
            now = time.asctime(time.localtime(float(bsets[1]["starttime"]))) + "  (" + bsets[1]["name"] + ")"
        else:
            now = time.asctime() + "  (filesystem)"

        for f in args.files:
            if bsets[1] is None and os.path.isdir(f):
                diffDir(os.path.abspath(f), r, bsets, tardis, crypt, args.reduce, now, then, recurse=args.recurse)
            else:
                (i0, _) = getFileInfo(os.path.abspath(f), bsets[0]["backupset"], tardis, crypt, args.reduce)
                if i0 and i0["dir"]:
                    (i1, _) = getFileInfo(os.path.abspath(f), bsets[1]["backupset"], tardis, crypt, args.reduce)
                    if i1 and i1["dir"]:
                        diffDir(
                            os.path.abspath(f), r, bsets, tardis, crypt, args.reduce, now, then, recurse=args.recurse
                        )
                        continue
                diffFile(f, r, bsets, tardis, crypt, args.reduce, args.recurse, now, then)
    except KeyboardInterrupt:
        pass
    except Exception as e:
        logger.error("Caught exception: %s", str(e))
        logger.exception(e)
Ejemplo n.º 14
0
def main():
    logging.basicConfig(level=logging.INFO)
    crypto = None
    token = None
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordurl, args.passwordprog)

    if password:
        crypto = TardisCrypto.TardisCrypto(password, args.client)
        token = crypto.createToken()

    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, token=token, backup=False)

    if crypto:
        (a, b) = db.getKeys()
        crypto.setKeys(a, b)

    conn = db.conn
    dirs = conn.execute("SELECT Name as name, Inode AS inode, Device AS device, FirstSet as firstset, LastSet AS lastset FROM Files JOIN Names ON Files.NameId = Names.NameId WHERE Dir = 1")
    while True:
        batch = dirs.fetchmany(1000)
        if not batch:
            break

        for d in batch:
            name     = d['name']
            inode    = d['inode']
            device   = d['device']
            firstset = d['firstset']
            lastset  = d['lastset']

            files = db.readDirectory((inode, device), current=lastset)
            names = [x['name'] for x in files]
            nfiles = len(names)
            if nfiles:
                if crypto:
                    names = map(crypto.decryptFilename, names)
                    name = crypto.decryptFilename(name)

                    names = sorted(names)
                    m = hashlib.md5()
                    for f in names:
                        m.update(f)
                    checksum = m.hexdigest()
            else:
                checksum = 'd41d8cd98f00b204e9800998ecf8427e'

            print("%-20s (%d, %d) [%d %d] -- %s %d") % (name, inode, device, firstset, lastset, checksum, nfiles)
            ckinfo = db.getChecksumInfo(checksum)
            if ckinfo:
                cksid = ckinfo['checksumid']
            else:
                cksid = db.insertChecksumFile(checksum, size=nfiles, isFile=False)

            db.updateDirChecksum((inode, device), cksid, current=lastset)
        conn.commit()
Ejemplo n.º 15
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordurl, args.passwordprog)

    crypto = TardisCrypto.TardisCrypto(password, args.client)
    token = createToken(crypto, args.client)

    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, backup=False)
    db.setToken(token)
Ejemplo n.º 16
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile,
                                args.passwordurl, args.passwordprog)

    crypto = TardisCrypto.TardisCrypto(password, args.client)
    token = crypto.createToken()

    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, backup=False)
    db.setToken(token)
Ejemplo n.º 17
0
def main():
    global args, logger
    tardis = None
    try:
        args = processArgs()
        logger = Util.setupLogging(args.verbose)

        setColors(Defaults.getDefault('TARDIS_LS_COLORS'))

        # Load any password info
        password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client))
        args.password = None

        (tardis, _, crypt) = Util.setupDataConnection(args.database, args.client, password, args.keys, args.dbname, args.dbdir)

        setupDisplay(tardis)

        if args.headers:
            doprint("Client: %s    DB: %s" %(args.client, args.database), color=colors['name'], eol=True)

        if args.glob:
            directories = []
            for d in args.directories:
                if not Util.isMagic(d):
                    directories.append(d)
                else:
                    directories += globPath(os.path.abspath(d), tardis, crypt)
        else:
            directories = args.directories

        for d in directories:
            d = os.path.abspath(d)
            if args.realpath:
                d = os.path.realpath(d)
            fInfos = collectFileInfo(d, tardis, crypt)
            recurse = args.maxdepth if args.recurse else 0
            processFile(d, fInfos, tardis, crypt, printContents=(not args.dirinfo), recurse=recurse)
    except KeyboardInterrupt:
        pass
    except TardisDB.AuthenticationException as e:
        logger.error("Authentication failed.  Bad password")
        if args.exceptions:
            logger.exception(e)
    except Exception as e:
        logger.error("Caught exception: %s", str(e))
        if args.exceptions:
            logger.exception(e)
    finally:
        if tardis:
            tardis.close()
Ejemplo n.º 18
0
def changePassword(crypt, oldpw) :
    try:
        (db, _, crypt) = getDB(crypt, oldpw)

        # Get the new password
        try:
            newpw = Util.getPassword(args.newpw, args.newpwf, args.newpwp, prompt="New Password for %s: " % (args.client),
                                     allowNone=False, confirm=True, strength=True)
        except Exception as e:
            logger.critical(str(e))
            if args.exceptions:
                logger.exception(e)
            return -1

        crypt2 = TardisCrypto.TardisCrypto(newpw, args.client)

        # Load the keys, and insert them into the crypt object, to decyrpt them
        if args.keys:
            (f, c) = Util.loadKeys(args.keys, db.getConfigValue('ClientID'))
            # No need to check here, loadKeys() throws exception if nothing set.
        else:
            (f, c) = db.getKeys()
            if f is None or c is None:
                logger.critical("No keys loaded from database.  Please specify --keys as appropriate")
                raise Exception("No keys loaded")
        crypt.setKeys(f, c)

        # Grab the keys from one crypt object.
        # Need to do this because getKeys/setKeys assumes they're encrypted, and we need the raw
        # versions
        crypt2._filenameKey = crypt._filenameKey
        crypt2._contentKey  = crypt._contentKey
        # Now get the encrypted versions
        (f, c) = crypt2.getKeys()

        (salt, vkey) = srp.create_salted_verification_key(args.client, newpw)

        if args.keys:
            db.beginTransaction()
            db.setSrpValues(salt, vkey)
            Util.saveKeys(args.keys, db.getConfigValue('ClientID'), f, c)
            db.commit()
        else:
            db.setKeys(salt, vkey, f, c)
        return 0
    except Exception as e:
        logger.error(str(e))
        if args.exceptions:
            logger.exception(e)
        return 1
Ejemplo n.º 19
0
def main():
    logging.basicConfig(level=logging.INFO)
    crypto = None
    token = None
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile,
                                args.passwordurl, args.passwordprog)

    if password:
        crypto = TardisCrypto.TardisCrypto(password, args.client)

    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, token=token, backup=False)

    if crypto:
        (a, b) = db.getKeys()
        crypto.setKeys(a, b)

    conn = db.conn
    dirs = conn.execute(
        "SELECT Name as name, Inode AS inode, Device AS device, FirstSet as firstset, LastSet AS lastset FROM Files JOIN Names ON Files.NameId = Names.NameId WHERE Dir = 1"
    )
    while True:
        batch = dirs.fetchmany(1000)
        if not batch:
            break

        for d in batch:
            name = d['name']
            inode = d['inode']
            device = d['device']
            firstset = d['firstset']
            lastset = d['lastset']

            files = db.readDirectory((inode, device), current=lastset)
            (checksum, nfiles) = Util.hashDir(crypto, files, True)

            print("%-20s (%d, %d) [%d %d] -- %s %d") % (
                name, inode, device, firstset, lastset, checksum, nfiles)
            ckinfo = db.getChecksumInfo(checksum)
            if ckinfo:
                cksid = ckinfo['checksumid']
            else:
                cksid = db.insertChecksumFile(checksum,
                                              size=nfiles,
                                              isFile=False)

            db.updateDirChecksum((inode, device), cksid, current=lastset)
        conn.commit()
Ejemplo n.º 20
0
def main():
    global args
    args = processArgs()

    password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client))
    (tardis, _, crypt) = Util.setupDataConnection(args.database, args.client, password, args.keys, args.dbname, args.dbdir)

    (f, c) = crypt.getKeys()
    client = tardis.getConfigValue('ClientID')

    data = Util.mkKeyString(client, f, c)

    qrfile = mkQrFile(data)
    makePdf(args.output, qrfile.name, data, args.client)
    return 0
Ejemplo n.º 21
0
def main():
    global args, logger
    try:
        FORMAT = "%(levelname)s : %(message)s"
        logging.basicConfig(stream=sys.stderr, format=FORMAT, level=logging.INFO)
        logger = logging.getLogger("")

        args = processArgs()

        setColors(Defaults.getDefault('TARDIS_LS_COLORS'))

        # Load any password info
        password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client))
        args.password = None

        (tardis, _, crypt) = Util.setupDataConnection(args.database, args.client, password, args.keys, args.dbname, args.dbdir)

        setupDisplay(tardis)

        if args.headers:
            doprint("Client: %s    DB: %s" %(args.client, args.database), color=colors['name'], eol=True)

        if args.glob:
            directories = []
            for d in args.directories:
                if not Util.isMagic(d):
                    directories.append(d)
                else:
                    directories += globPath(os.path.abspath(d), tardis, crypt)
        else:
            directories = args.directories

        for d in directories:
            d = os.path.abspath(d)
            if args.realpath:
                d = os.path.realpath(d)
            fInfos = collectFileInfo(d, tardis, crypt)
            recurse = args.maxdepth if args.recurse else 0
            processFile(d, fInfos, tardis, crypt, printContents=(not args.dirinfo), recurse=recurse)
    except KeyboardInterrupt:
        pass
    except Exception as e:
        logger.error("Caught exception: %s", str(e))
        logger.exception(e)
Ejemplo n.º 22
0
def main():
    logging.basicConfig(level=logging.INFO)
    crypto = None
    token = None
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordurl, args.passwordprog)

    if password:
        crypto = TardisCrypto.TardisCrypto(password, args.client)

    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, token=token, backup=False)

    if crypto:
        (a, b) = db.getKeys()
        crypto.setKeys(a, b)

    conn = db.conn
    dirs = conn.execute("SELECT Name as name, Inode AS inode, Device AS device, FirstSet as firstset, LastSet AS lastset FROM Files JOIN Names ON Files.NameId = Names.NameId WHERE Dir = 1")
    while True:
        batch = dirs.fetchmany(1000)
        if not batch:
            break

        for d in batch:
            name     = d['name']
            inode    = d['inode']
            device   = d['device']
            firstset = d['firstset']
            lastset  = d['lastset']

            files = db.readDirectory((inode, device), current=lastset)
            (checksum, nfiles) = Util.hashDir(crypto, files, True)

            print(("%-20s (%d, %d) [%d %d] -- %s %d") % (name, inode, device, firstset, lastset, checksum, nfiles))
            ckinfo = db.getChecksumInfo(checksum)
            if ckinfo:
                cksid = ckinfo['checksumid']
            else:
                cksid = db.insertChecksumFile(checksum, size=nfiles, isFile=False)

            db.updateDirChecksum((inode, device), cksid, current=lastset)
        conn.commit()
Ejemplo n.º 23
0
def main():
    global logger
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('')
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordprog)

    (tardis, cache, crypto) = Util.setupDataConnection(args.database, args.client, password, args.keys, args.dbname, args.dbdir)

    name = args.name[0]

    if args.from_cache:
        sz = cache.size(name)
        infile = cache.open(name, "rb")
    else:
        st = os.stat(name)
        sz = st.st_size
        infile = open(name, "rb")
    decryptFile(infile, args.output, sz, crypto, authenticate=not args.noauth)
Ejemplo n.º 24
0
def main():
    global logger
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('')
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordprog)

    (tardis, _, crypto) = Util.setupDataConnection(args.database, args.client, password, args.keys, args.dbname, args.dbdir)

    if args.backup is not None:
        bsetInfo = Util.getBackupSet(tardis, args.backup)
        if bsetInfo:
            bset = bsetInfo['backupset']
        else:
            logger.critical("No backupset at for name: %s", args.backup)
            sys.exit(1)
    else:
        bset = False

    data = args.checksums
    if not data:
        tty = os.isatty(0)
        if not tty:
            data = list(map(str.strip, sys.stdin.readlines()))
        else:
            data = reader(args.quiet)

    prevInode = None
    for i in data:
        try:
            for finfo in tardis.getFileInfoByChecksum(i, bset):
                inode = (finfo['inode'], finfo['device'])
                if inode == prevInode:
                    next
                prevInode = inode
                if args.quiet:
                    print(_path(tardis, crypto, bset, inode))
                else:
                    print(f"{i} => {_path(tardis, crypto, bset, inode)}")

        except Exception as e:
            print("Caught exception: " + str(e))
Ejemplo n.º 25
0
def main():
    global args
    args = processArgs()

    password = Util.getPassword(args.password,
                                args.passwordfile,
                                args.passwordprog,
                                prompt="Password for %s: " % (args.client))
    (tardis, _, crypt) = Util.setupDataConnection(args.database, args.client,
                                                  password, args.keys,
                                                  args.dbname, args.dbdir)

    (f, c) = crypt.getKeys()
    client = tardis.getConfigValue('ClientID')

    data = Util.mkKeyString(client, f, c)

    qrfile = mkQrFile(data)
    makePdf(args.output, qrfile.name, data, args.client)
    return 0
Ejemplo n.º 26
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger('')
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordurl, args.passwordprog)

    crypto = TardisCrypto.TardisCrypto(password, args.client)
    token = crypto.createToken()

    logger.info("Created token: %s", token)
    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, token=token, backup=False)
    (f, c) = db.getKeys()
    crypto.setKeys(f, c)

    cacheDir = CacheDir.CacheDir(os.path.join(args.database, args.client))

    if args.filenames:
        encryptFilenames(db, crypto)
    if args.files:
        encryptFiles(db, crypto, cacheDir)
Ejemplo n.º 27
0
def main():
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger()
    crypto = None
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordurl, args.passwordprog)

    if password:
        crypto = TardisCrypto.TardisCrypto(password, args.client)

    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, backup=False)

    token = createToken(crypto, args.client)
    if not checkToken(db, token):
        logger.error("Password does not match")
        sys.exit(1)

    salt, vkey = srp.create_salted_verification_key(args.client, password)
    db.setSrpValues(salt, vkey)
    db._setConfigValue('Token', None)
Ejemplo n.º 28
0
def main():
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger()
    crypto = None
    args = processArgs()
    password = Util.getPassword(args.password, args.passwordfile, args.passwordurl, args.passwordprog)

    if password:
        crypto = TardisCrypto.TardisCrypto(password, args.client)

    path = os.path.join(args.database, args.client, args.dbname)
    db = TardisDB.TardisDB(path, backup=False)

    token = createToken(crypto, args.client)
    if not checkToken(db, token):
        logger.error("Password does not match")
        sys.exit(1)

    salt, vkey = srp.create_salted_verification_key(args.client, password)
    db.setSrpValues(salt, vkey)
    db._setConfigValue('Token', None)
Ejemplo n.º 29
0
def main():
    global args
    args = processArgs()

    password = Util.getPassword(args.password,
                                args.passwordfile,
                                args.passwordprog,
                                prompt="Password for %s: " % (args.client))
    (tardis, cache,
     crypt) = Util.setupDataConnection(args.database, args.client, password,
                                       args.keys, args.dbname, args.dbdir)

    count = 0
    for (checksum, size, basis, compressed, encrypted,
         added) in listChecksums(tardis):
        count += 1
        checkFile(cache, crypt, checksum, size, basis, compressed, encrypted,
                  added, args.authenticate)

    print(
        f"Files: {count} Missing Files: {len(missing)} Empty: {len(zero)} Size mismatch: {len(mismatch)} Not Delta: {len(notdelta)}"
    )
    #for i in sizes:
    #    print(f"   Size: {i}: Count {len(sizes[i])}")

    if args.output:
        out = {
            "missing": missing,
            "empty": zero,
            "size": mismatch,
            "notauth": notauth,
            "notdelta": notdelta
        }
        json.dump(out, args.output, indent=2)

    return 0
Ejemplo n.º 30
0
    def __init__(self, *args, **kw):
        super(TardisFS, self).__init__(*args, **kw)

        try:
            client = Defaults.getDefault('TARDIS_CLIENT')
            database = Defaults.getDefault('TARDIS_DB')
            dbdir = Defaults.getDefault('TARDIS_DBDIR') % {
                'TARDIS_DB': database
            }  # HACK
            dbname = Defaults.getDefault('TARDIS_DBNAME')
            current = Defaults.getDefault('TARDIS_RECENT_SET')

            # Parameters
            self.database = database
            self.client = client
            self.repoint = False
            self.password = None
            self.pwfile = None
            self.pwprog = None
            self.keys = None
            self.dbname = dbname
            self.dbdir = dbdir
            self.cachetime = 60
            self.nocrypt = False
            self.noauth = False
            self.current = current
            self.authenticate = True

            self.crypt = None

            logging.basicConfig(level=logging.WARNING)
            self.log = logging.getLogger("TardisFS")

            self.parser.add_option(
                "--database",
                '-D',
                help="Path to the Tardis database directory")
            self.parser.add_option("--client",
                                   '-C',
                                   help="Client to load database for")
            self.parser.add_option(
                "--password",
                '-P',
                help=
                "Password for this archive (use '-o password='******'-o password='******'nocrypt',
                                   help="Disable encryption")
            self.parser.add_option(mountopt='noauth',
                                   help="Disable authentication")
            self.parser.add_option(
                mountopt='current',
                help="Name to use for most recent complete backup")

            res = self.parse(values=self, errex=1)

            self.mountpoint = res.mountpoint

            self.name = "TardisFS:<{}/{}>".format(self.database, self.client)

            password = Util.getPassword(self.password,
                                        self.pwfile,
                                        self.pwprog,
                                        prompt="Password for %s: " %
                                        (self.client))
            self.password = None

            self.cache = Cache.Cache(0, float(self.cachetime))
            self.fileCache = Cache.Cache(0, float(self.cachetime), 'FileCache')

            #if password:
            #    self.crypt = TardisCrypto.TardisCrypto(password, self.client)
            (self.tardis, self.cacheDir,
             self.crypt) = Util.setupDataConnection(self.database, self.client,
                                                    password, self.keys,
                                                    self.dbname, self.dbdir)
            password = None

            # Remove the crypto object if not encyrpting files.
            if self.nocrypt or self.nocrypt is None:
                self.crypt = None

            if self.noauth or self.noauth is None:
                self.authenticate = False

            # Create a regenerator.
            self.regenerator = Regenerator.Regenerator(self.cacheDir,
                                                       self.tardis,
                                                       crypt=self.crypt)
            self.files = {}

            # Fuse variables
            self.flags = 0
            self.multithreaded = 0

        except Exception as e:
            self.log.exception(e)
            sys.exit(2)
Ejemplo n.º 31
0
def main():
    global logger, exceptionLogger
    tardis = None
    try:
        parseArgs()
        logger = Util.setupLogging(args.verbose)
        exceptionLogger = Util.ExceptionLogger(logger, args.exceptions)

        if len(args.backup) > 2:
            logger.error(args.backup)
            logger.error("Too many backups (%d) specified.  Only one or two allowed", len(args.backup))
            sys.exit(1)

        password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client))
        args.password = None
        (tardis, cache, crypt) = Util.setupDataConnection(args.database, args.client, password, args.keys, args.dbname, args.dbdir)
        password = None

        bsets = []
        for i in args.backup:
            bset = getBackupSet(tardis, i)
            if bset:
                logger.debug("Got backupset %s", str(bset))
                logger.debug("backupset: %s", bset['backupset'])
                bsets.append(bset)
            else:
                sys.exit(1)

        if len(bsets) == 1:
            bsets.append(None)

        r = Regenerator.Regenerator(cache, tardis, crypt)
        then = time.asctime(time.localtime(float(bsets[0]['starttime']))) + '  (' + bsets[0]['name'] + ')'
        if bsets[1]:
            now = time.asctime(time.localtime(float(bsets[1]['starttime']))) + '  (' + bsets[1]['name'] + ')'
        else:
            now = time.asctime() + '  (filesystem)'

        for f in args.files:
            f = unicode(f.decode(sys.getfilesystemencoding()))
            if bsets[1] is None and os.path.isdir(f):
                diffDir(os.path.abspath(f), r, bsets, tardis, crypt, args.reduce, now, then, recurse=args.recurse)
            else:
                (i0, _) = getFileInfo(os.path.abspath(f), bsets[0]['backupset'], tardis, crypt, args.reduce)
                if i0 and i0['dir']:
                    (i1, _) = getFileInfo(os.path.abspath(f), bsets[1]['backupset'], tardis, crypt, args.reduce)
                    if i1 and i1['dir']:
                        diffDir(os.path.abspath(f), r, bsets, tardis, crypt, args.reduce, now, then, recurse=args.recurse)
                        continue
                diffFile(f, r, bsets, tardis, crypt, args.reduce, args.recurse, now, then)
    except KeyboardInterrupt:
        pass
    except TardisDB.AuthenticationException as e:
        logger.error("Authentication failed.  Bad password")
        exceptionLogger.log(e)
        sys.exit(1)
    except Exception as e:
        logger.error("Caught exception: %s", str(e))
        exceptionLogger.log(e)
    finally:
        if tardis:
            tardis.close()
Ejemplo n.º 32
0
def main():
    global logger
    crypto = None

    args = processArgs()

    Util.setupLogging(args.verbose, levels=[logging.WARNING, logging.DEBUG])
    logger = logging.getLogger('')

    if not validateShell(args.shell):
        sys.exit(1)

    password = Util.getPassword(args.password, args.passwordfile,
                                args.passwordprog)

    try:
        (tardis, cache, crypt,
         password) = Util.setupDataConnection(args.database,
                                              args.client,
                                              password,
                                              args.keys,
                                              args.dbname,
                                              args.dbdir,
                                              retpassword=True)
    except TardisDB.AuthenticationFailed as e:
        logger.error("Authentication failed")
        sys.exit(1)

    try:
        logger.debug("Setting environment variables")
        if password:
            pwFileName = ".tardis-" + str(os.getpid())
            pwFilePath = os.path.join(os.path.expanduser("~"), pwFileName)
            logger.debug("Storing password in %s", pwFilePath)
            with os.fdopen(
                    os.open(pwFilePath, os.O_WRONLY | os.O_CREAT, 0o400),
                    'w') as handle:
                handle.write(password)
            os.environ['TARDIS_PWFILE'] = pwFilePath
        os.environ['TARDIS_CLIENT'] = args.client
        os.environ['TARDIS_DB'] = args.database
        if args.keys:
            os.environ['TARDIS_KEYFILE'] = os.path.abspath(args.keys)

        logger.warning(
            "Spawning interactive shell with security preauthenticated.")

        # Run the shell, and wait
        status = subprocess.run(args.shell)

        # Check the return code.
        if status.returncode != 0:
            logger.warning("Child exited with status %d", status.returncode)

        logger.warning("Returned to unauthenticated environment")
    finally:
        if password:
            try:
                os.unlink(pwFilePath)
            except Exception as e:
                logger.critical("Unable to delete password file: %s :: %s",
                                pwFilePath, str(e))

    sys.exit(status.returncode)
Ejemplo n.º 33
0
def main():
    global logger
    parseArgs()
    logger = Util.setupLogging(args.verbose)

    # Commands which cannot be executed on remote databases
    allowRemote = args.command not in ['create', 'upgrade']

    db      = None
    crypt   = None
    cache   = None
    try:
        confirm = args.command in ['setpass', 'create']
        allowNone = args.command not in ['setpass', 'chpass']
        try:
            password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client), allowNone=allowNone, confirm=confirm)
        except Exception as e:
            logger.critical(str(e))
            if args.exceptions:
                logger.exception(e)
            return -1
            
        if password:
            crypt = TardisCrypto.TardisCrypto(password, args.client)
            args.password = None

        if args.command == 'create':
            return createClient(crypt, password)

        if args.command == 'setpass':
            if not Util.checkPasswordStrength(password):
                return -1

            if not crypt:
                logger.error("No password specified")
                return -1
            return setPassword(crypt, password)

        if args.command == 'chpass':
            return changePassword(crypt, password)

        upgrade = (args.command == 'upgrade')

        try:
            (db, cache, crypt) = getDB(crypt, password, allowRemote=allowRemote, allowUpgrade=upgrade)

            if crypt and args.command != 'keys':
                if args.keys:
                    (f, c) = Util.loadKeys(args.keys, db.getConfigValue('ClientID'))
                else:
                    (f, c) = db.getKeys()
                crypt.setKeys(f, c)
        except TardisDB.AuthenticationException as e:
            logger.error("Authentication failed.  Bad password")
            if args.exceptions:
                logger.exception(e)
            sys.exit(1)
        except Exception as e:
            logger.critical("Unable to connect to database: %s", e)
            if args.exceptions:
                logger.exception(e)
            sys.exit(1)

        if args.command == 'keys':
            return moveKeys(db, crypt)
        elif args.command == 'list':
            return listBSets(db, crypt, cache)
        elif args.command == 'files':
            return listFiles(db, crypt)
        elif args.command == 'info':
            return bsetInfo(db)
        elif args.command == 'purge':
            return purge(db, cache)
        elif args.command == 'delete':
            return deleteBsets(db, cache)
        elif args.command == 'priority':
            return setPriority(db)
        elif args.command == 'rename':
            return renameSet(db)
        elif args.command == 'getconfig':
            return getConfig(db)
        elif args.command == 'setconfig':
            return setConfig(db)
        elif args.command == 'orphans':
            return removeOrphans(db, cache)
        elif args.command == 'upgrade':
            return
    except KeyboardInterrupt:
        pass
    except TardisDB.AuthenticationException as e:
        logger.error("Authentication failed.  Bad password")
        sys.exit(1)
    except Exception as e:
        logger.error("Caught exception: %s", str(e))
        if args.exceptions:
            logger.exception(e)
    finally:
        if db:
            db.close()
Ejemplo n.º 34
0
def main():
    global logger, crypt, tardis, args, owMode
    args = parseArgs()
    logger = Util.setupLogging(args.verbose, stream=sys.stderr)

    try:
        password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client))
        args.password = None
        (tardis, cache, crypt) = Util.setupDataConnection(args.database, args.client, password, args.keys, args.dbname, args.dbdir)

        r = Regenerator.Regenerator(cache, tardis, crypt=crypt)
    except TardisDB.AuthenticationException as e:
        logger.error("Authentication failed.  Bad password")
        #if args.exceptions:
            #logger.exception(e)
        sys.exit(1)
    except Exception as e:
        logger.error("Regeneration failed: %s", e)
        sys.exit(1)

    try:
        bset = False

        if args.date:
            cal = parsedatetime.Calendar()
            (then, success) = cal.parse(args.date)
            if success:
                timestamp = time.mktime(then)
                logger.info("Using time: %s", time.asctime(then))
                bsetInfo = tardis.getBackupSetInfoForTime(timestamp)
                if bsetInfo and bsetInfo['backupset'] != 1:
                    bset = bsetInfo['backupset']
                    logger.debug("Using backupset: %s %d", bsetInfo['name'], bsetInfo['backupset'])
                else:
                    logger.critical("No backupset at date: %s (%s)", args.date, time.asctime(then))
                    sys.exit(1)
            else:
                logger.critical("Could not parse date string: %s", args.date)
                sys.exit(1)
        elif args.backup:
            #bsetInfo = tardis.getBackupSetInfo(args.backup)
            bsetInfo = Util.getBackupSet(tardis, args.backup)
            if bsetInfo:
                bset = bsetInfo['backupset']
            else:
                logger.critical("No backupset at for name: %s", args.backup)
                sys.exit(1)

        outputdir = None
        output    = sys.stdout.buffer
        outname   = None
        linkDB    = None

        owMode    = overwriteNames[args.overwrite]

        if args.output:
            if len(args.files) > 1:
                outputdir = mkOutputDir(args.output)
            elif os.path.isdir(args.output):
                outputdir = args.output
            else:
                outname = args.output
        logger.debug("Outputdir: %s  Outname: %s", outputdir, outname)

        if args.hardlinks:
            linkDB = {}

        #if args.cksum and (args.settime or args.setperm):
            #logger.warning("Unable to set time or permissions on files specified by checksum.")

        permChecker = setupPermissionChecks()

        retcode = 0
        hasher = None

        # do the work here
        if args.cksum:
            for i in args.files:
                try:
                    if args.auth:
                        hasher = Util.getHash(crypt)
                    ckname = i
                    if args.recovername:
                        ckname = recoverName(i)
                    f = r.recoverChecksum(i, args.auth)
                    if f:
                        logger.info("Recovering checksum %s", ckname)
                    # Generate an output name
                        if outname:
                            # Note, this should ONLY be true if only one file
                            output = open(outname,  "wb")
                        elif outputdir:
                            outname = os.path.join(outputdir, ckname)
                            if os.path.exists(outname) and owMode == OW_NEVER:
                                logger.warning("File %s exists.  Skipping", outname)
                                continue
                            logger.debug("Writing output to %s", outname)
                            output = open(outname,  "wb")
                        elif outname:
                            # Note, this should ONLY be true if only one file
                            if os.path.exists(outname) and owMode == OW_NEVER:
                                logger.warning("File %s exists.  Skipping", outname)
                                continue
                            output = file(outname,  "wb")
                        try:
                            x = f.read(64 * 1024)
                            while x:
                                output.write(x)
                                if hasher:
                                    hasher.update(x)
                                x = f.read(64 * 1024)
                        except Exception as e:
                            logger.error("Unable to read file: {}: {}".format(i, repr(e)))
                            raise
                        finally:
                            f.close()
                            if output is not sys.stdout.buffer:
                                output.close()
                        if args.auth:
                            logger.debug("Checking authentication")
                            outname = doAuthenticate(outname, i, hasher.hexdigest())

                except TardisDB.AuthenticationException as e:
                    logger.error("Authentication failed.  Bad password")
                    #if args.exceptions:
                        #logger.exception(e)
                    sys.exit(1)
                except Exception as e:
                    logger.error("Could not recover: %s: %s", i, e)
                    if args.exceptions:
                        logger.exception(e)
                    retcode += 1

        else: # Not checksum, but acutal pathnames
            for i in args.files:
                try:
                    i = os.path.abspath(i)
                    logger.info("Processing %s", Util.shortPath(i))
                    path = None
                    f = None
                    if args.last:
                        (bset, path, name) = findLastPath(i, args.reduce)
                        if bset is None:
                            logger.error("Unable to find a latest version of %s", i)
                            raise Exception("Unable to find a latest version of " + i)
                        logger.info("Found %s in backup set %s", i, name)
                    elif args.reduce:
                        path = Util.reducePath(tardis, bset, i, args.reduce, crypt)
                        logger.debug("Reduced path %s to %s", path, i)
                        if not path:
                            logger.error("Unable to find a compute path for %s", i)
                            raise Exception("Unable to compute path for " + i)
                    else:
                        path = i

                    if args.crypt and crypt:
                        actualPath = crypt.encryptPath(path)
                    else:
                        actualPath = path
                    logger.debug("Actual path is %s -- %s", actualPath, bset)
                    info = tardis.getFileInfoByPath(actualPath, bset)
                    if info:
                        retcode += recoverObject(r, info, bset, outputdir, path, linkDB, name=outname, authenticate=args.auth)
                    else:
                        logger.error("Could not recover info for %s (File not found)", i)
                        retcode += 1
                except TardisDB.AuthenticationException as e:
                    logger.error("Authentication failed.  Bad password")
                    #if args.exceptions:
                        #logger.exception(e)
                    sys.exit(1)
                except Exception as e:
                    logger.error("Could not recover: %s: %s", i, e)
                    if args.exceptions:
                        logger.exception(e)
    except KeyboardInterrupt:
        logger.error("Recovery interupted")
    except TardisDB.AuthenticationException as e:
        logger.error("Authentication failed.  Bad password")
        if args.exceptions:
            logger.exception(e)
    except Exception as e:
        logger.error("Regeneration failed: %s", e)
        if args.exceptions:
            logger.exception(e)

    if errors:
        logger.warning("%d files could not be recovered.")

    return retcode
Ejemplo n.º 35
0
def main():
    global logger
    parseArgs()
    logger = Util.setupLogging(args.verbose)

    # Commands which cannot be executed on remote databases
    allowRemote = args.command not in ['create']

    db = None
    crypt = None
    cache = None
    try:
        password = Util.getPassword(args.password,
                                    args.passwordfile,
                                    args.passwordprog,
                                    prompt="Password for %s: " % (args.client),
                                    allowNone=(args.command != 'setPass'))
        if args.command in ['setpass', 'create']:
            if password and not checkPasswordStrength(password):
                return -1

            if args.password:
                pw2 = Util.getPassword(args.password,
                                       args.passwordfile,
                                       args.passwordprog,
                                       prompt='Confirm Password: '******'t match")
                    return -1
                pw2 = None

        if password:
            crypt = TardisCrypto.TardisCrypto(password, args.client)
            password = None
            args.password = None

        if args.command == 'create':
            return createClient(crypt)

        if args.command == 'setpass':
            if not crypt:
                logger.error("No password specified")
                return -1
            return setToken(crypt)

        if args.command == 'chpass':
            newpw = Util.getPassword(args.newpw,
                                     args.newpwf,
                                     args.newpwp,
                                     prompt="New Password for %s: " %
                                     (args.client),
                                     allowNone=False)
            if not checkPasswordStrength(newpw):
                return -1

            if args.newpw is True:
                newpw2 = Util.getPassword(args.newpw,
                                          args.newpwf,
                                          args.newpwp,
                                          prompt="New Password for %s: " %
                                          (args.client),
                                          allowNone=False)
                if newpw2 != newpw:
                    logger.error("Passwords don't match")
                    return -1
                newpw2 = None

            crypt2 = TardisCrypto.TardisCrypto(newpw, args.client)
            newpw = None
            args.newpw = None
            return changePassword(crypt, crypt2)

        try:
            (db, cache) = getDB(crypt, allowRemote=allowRemote)
            if crypt:
                if args.keys:
                    (f, c) = Util.loadKeys(args.keys,
                                           db.getConfigValue('ClientID'))
                else:
                    (f, c) = db.getKeys()
                crypt.setKeys(f, c)
        except Exception as e:
            logger.critical("Unable to connect to database: %s", e)
            sys.exit(1)

        if args.command == 'keys':
            return moveKeys(db, crypt)
        elif args.command == 'list':
            return listBSets(db)
        elif args.command == 'files':
            return listFiles(db, crypt)
        elif args.command == 'info':
            return bsetInfo(db)
        elif args.command == 'purge':
            return purge(db, cache)
        elif args.command == 'delete':
            return deleteBsets(db, cache)
        elif args.command == 'getconfig':
            return getConfig(db)
        elif args.command == 'setconfig':
            return setConfig(db)
        elif args.command == 'orphans':
            return removeOrphans(db, cache)
    except KeyboardInterrupt:
        pass
    except Exception as e:
        logger.error("Caught exception: %s", str(e))
        logger.exception(e)
    finally:
        if db:
            db.close()
Ejemplo n.º 36
0
def main():
    parseArgs()
    setupLogging()

    # Commands which cannot be executed on remote databases
    allowRemote = args.command not in ["create"]

    try:
        crypt = None
        password = Util.getPassword(
            args.password,
            args.passwordfile,
            args.passwordprog,
            prompt="Password for %s: " % (args.client),
            allowNone=(args.command != "setPass"),
        )
        if args.command in ["setpass", "create"]:
            if password and not checkPasswordStrength(password):
                return -1

            if args.password:
                pw2 = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Confirm Password: "******"Passwords don't match")
                    return -1
                pw2 = None

        if password:
            crypt = TardisCrypto.TardisCrypto(password, args.client)
            password = None
            args.password = None

        if args.command == "create":
            return createClient(crypt)

        if args.command == "setpass":
            if not crypt:
                logger.error("No password specified")
                return -1
            return setToken(crypt)

        if args.command == "chpass":
            newpw = Util.getPassword(
                args.newpw, args.newpwf, args.newpwp, prompt="New Password for %s: " % (args.client), allowNone=False
            )
            if not checkPasswordStrength(newpw):
                return -1

            if args.newpw is True:
                newpw2 = Util.getPassword(
                    args.newpw,
                    args.newpwf,
                    args.newpwp,
                    prompt="New Password for %s: " % (args.client),
                    allowNone=False,
                )
                if newpw2 != newpw:
                    logger.error("Passwords don't match")
                    return -1
                newpw2 = None

            crypt2 = TardisCrypto.TardisCrypto(newpw, args.client)
            newpw = None
            args.newpw = None
            return changePassword(crypt, crypt2)

        db = None
        cache = None
        try:
            (db, cache) = getDB(crypt, allowRemote=allowRemote)
        except Exception as e:
            logger.critical("Unable to connect to database: %s", e)
            sys.exit(1)

        if args.command == "keys":
            return moveKeys(db, crypt)
        elif args.command == "list":
            return listBSets(db)
        elif args.command == "info":
            return bsetInfo(db)
        elif args.command == "purge":
            return purge(db, cache)
        elif args.command == "delete":
            return deleteBsets(db, cache)
        elif args.command == "getconfig":
            return getConfig(db)
        elif args.command == "setconfig":
            return setConfig(db)
        elif args.command == "orphans":
            return removeOrphans(db, cache)
    except KeyboardInterrupt:
        pass
    except Exception as e:
        logger.error("Caught exception: %s", str(e))
        logger.exception(e)
Ejemplo n.º 37
0
def main():
    global logger, exceptionLogger, args
    parseArgs()
    logger = Util.setupLogging(args.verbose)
    exceptionLogger = Util.ExceptionLogger(logger, args.exceptions)

    # Commands which cannot be executed on remote databases
    allowRemote = args.command not in ['create', 'upgrade']

    db      = None
    crypt   = None
    cache   = None
    try:
        confirm = args.command in ['setpass', 'create']
        allowNone = args.command not in ['setpass', 'chpass']
        try:
            password = Util.getPassword(args.password, args.passwordfile, args.passwordprog, prompt="Password for %s: " % (args.client), allowNone=allowNone, confirm=confirm)
        except Exception as e:
            logger.critical(str(e))
            exceptionLogger.log(e)
            return -1
            
        if password:
            crypt = TardisCrypto.TardisCrypto(password, args.client)
            args.password = None

        if args.command == 'create':
            return createClient(crypt, password)

        if args.command == 'setpass':
            if not Util.checkPasswordStrength(password):
                return -1

            if not crypt:
                logger.error("No password specified")
                return -1
            return setPassword(crypt, password)

        if args.command == 'chpass':
            return changePassword(crypt, password)

        upgrade = (args.command == 'upgrade')

        try:
            (db, cache) = getDB(crypt, password, allowRemote=allowRemote, allowUpgrade=upgrade)

            if crypt and args.command != 'keys':
                if args.keys:
                    (f, c) = Util.loadKeys(args.keys, db.getConfigValue('ClientID'))
                else:
                    (f, c) = db.getKeys()
                crypt.setKeys(f, c)
        except TardisDB.AuthenticationException as e:
            logger.error("Authentication failed.  Bad password")
            exceptionLogger.log(e)
            sys.exit(1)
        except Exception as e:
            logger.critical("Unable to connect to database: %s", e)
            exceptionLogger.log(e)
            sys.exit(1)

        if args.command == 'keys':
            return moveKeys(db, crypt)
        elif args.command == 'list':
            return listBSets(db, crypt, cache)
        elif args.command == 'files':
            return listFiles(db, crypt)
        elif args.command == 'info':
            return bsetInfo(db)
        elif args.command == 'purge':
            return purge(db, cache)
        elif args.command == 'delete':
            return deleteBsets(db, cache)
        elif args.command == 'priority':
            return setPriority(db)
        elif args.command == 'rename':
            return renameSet(db)
        elif args.command == 'getconfig':
            return getConfig(db)
        elif args.command == 'setconfig':
            return setConfig(db)
        elif args.command == 'orphans':
            return removeOrphans(db, cache)
        elif args.command == 'upgrade':
            return
    except KeyboardInterrupt:
        pass
    except TardisDB.AuthenticationException as e:
        logger.error("Authentication failed.  Bad password")
        sys.exit(1)
    except Exception as e:
        logger.error("Caught exception: %s", str(e))
        exceptionLogger.log(e)
    finally:
        if db:
            db.close()
Ejemplo n.º 38
0
    def __init__(self, *args, **kw):
        super(TardisFS, self).__init__(*args, **kw)

        try:
            client   = Defaults.getDefault('TARDIS_CLIENT')
            database = Defaults.getDefault('TARDIS_DB')
            dbdir    = Defaults.getDefault('TARDIS_DBDIR') % { 'TARDIS_DB': database }          # HACK
            dbname   = Defaults.getDefault('TARDIS_DBNAME')
            current  = Defaults.getDefault('TARDIS_RECENT_SET')

            # Parameters
            self.database       = database
            self.client         = client
            self.repoint        = False
            self.password       = None
            self.pwfile         = None
            self.pwprog         = None
            self.keys           = None
            self.dbname         = dbname
            self.dbdir          = dbdir
            self.cachetime      = 60
            self.nocrypt        = False
            self.noauth         = False
            self.current        = current
            self.authenticate   = True

            self.crypt          = None

            logging.basicConfig(level=logging.WARNING)
            self.log = logging.getLogger("TardisFS")

            self.parser.add_option("--database", '-D',      help="Path to the Tardis database directory")
            self.parser.add_option("--client", '-C',        help="Client to load database for")
            self.parser.add_option("--password", '-P',      help="Password for this archive (use '-o password='******'-o password='******'nocrypt',      help="Disable encryption")
            self.parser.add_option(mountopt='noauth',       help="Disable authentication")
            self.parser.add_option(mountopt='current',      help="Name to use for most recent complete backup")

            res = self.parse(values=self, errex=1)

            self.mountpoint = res.mountpoint

            self.name = "TardisFS:<{}/{}>".format(self.database, self.client)

            password = Util.getPassword(self.password, self.pwfile, self.pwprog, prompt="Password for %s: " % (self.client))
            self.password = None

            self.cache      = Cache.Cache(0, float(self.cachetime))
            self.fileCache  = Cache.Cache(0, float(self.cachetime), 'FileCache')

            #if password:
            #    self.crypt = TardisCrypto.TardisCrypto(password, self.client)
            (self.tardis, self.cacheDir, self.crypt) = Util.setupDataConnection(self.database, self.client, password, self.keys, self.dbname, self.dbdir)
            password = None

            # Remove the crypto object if not encyrpting files.
            if self.nocrypt or self.nocrypt is None:
                self.crypt = None

            if self.noauth or self.noauth is None:
                self.authenticate = False

            # Create a regenerator.
            self.regenerator = Regenerator.Regenerator(self.cacheDir, self.tardis, crypt=self.crypt)
            self.files = {}

            # Fuse variables
            self.flags = 0
            self.multithreaded = 0

        except Exception as e:
            self.log.exception(e)
            sys.exit(2)