示例#1
0
	def get_string_from_file(filename):
		if not filename: return None
		rp = rpath.RPath(Globals.local_connection, filename)
		try: return rp.get_data()
		except OSError, e:
			Log.FatalError("Error '%s' reading mapping file '%s'" %
						   (str(e), filename))
示例#2
0
def vet_filename(request, arglist):
    """Check to see if file operation is within the restrict_path"""
    i = file_requests[request.function_string]
    if len(arglist) <= i: raise_violation(request, arglist)
    filename = arglist[i]
    if type(filename) is not types.StringType:
        raise_violation(request, arglist)
    vet_rpath(rpath.RPath(Globals.local_connection, filename))
示例#3
0
def restore_set_root(rpin):
    """Set data dir, restore_root and index, or return None if fail

	The idea here is to keep backing up on the path until we find
	a directory that contains "rdiff-backup-data".  That is the
	mirror root.  If the path from there starts
	"rdiff-backup-data/increments*", then the index is the
	remainder minus that.  Otherwise the index is just the path
	minus the root.

	All this could fail if the increment file is pointed to in a
	funny way, using symlinks or somesuch.

	"""
    global restore_root, restore_index, restore_root_set
    if rpin.isincfile(): relpath = rpin.getincbase().path
    else: relpath = rpin.path
    if rpin.conn is not Globals.local_connection:
        # For security checking consistency, don't get absolute path
        pathcomps = relpath.split('/')
    else:
        pathcomps = os.path.join(os.getcwd(), relpath).split("/")
    if not pathcomps[0]: min_len_pathcomps = 2  # treat abs paths differently
    else: min_len_pathcomps = 1

    i = len(pathcomps)
    while i >= min_len_pathcomps:
        parent_dir = rpath.RPath(rpin.conn, "/".join(pathcomps[:i]))
        if (parent_dir.isdir() and parent_dir.readable()
                and "rdiff-backup-data" in parent_dir.listdir()):
            break
        if parent_dir.path == rpin.conn.Globals.get('restrict_path'):
            return None
        i = i - 1
    else:
        return None

    restore_root = parent_dir
    Log("Using mirror root directory %s" % restore_root.path, 6)
    if restore_root.conn is Globals.local_connection:
        Security.reset_restrict_path(restore_root)
    SetConnections.UpdateGlobal('rbdir',
                                restore_root.append_path("rdiff-backup-data"))
    if not Globals.rbdir.isdir():
        Log.FatalError("Unable to read rdiff-backup-data directory %s" %
                       Globals.rbdir.path)

    from_datadir = tuple(pathcomps[i:])
    if not from_datadir or from_datadir[0] != "rdiff-backup-data":
        restore_index = from_datadir  # in mirror, not increments
    else:
        assert (from_datadir[1] == "increments" or
                (len(from_datadir) == 2
                 and from_datadir[1].startswith('increments'))), from_datadir
        restore_index = from_datadir[2:]
    restore_root_set = 1
    return 1
示例#4
0
 def isincfile(self):
     """Return true if path indicates increment, sets various variables"""
     if not self.index:  # consider the last component as quoted
         dirname, basename = self.dirsplit()
         temp_rp = rpath.RPath(self.conn, dirname, (unquote(basename), ))
         result = temp_rp.isincfile()
         if result:
             self.inc_basestr = unquote(temp_rp.inc_basestr)
             self.inc_timestr = unquote(temp_rp.inc_timestr)
     else:
         result = rpath.RPath.isincfile(self)
         if result: self.inc_basestr = unquote(self.inc_basestr)
     return result
示例#5
0
	def next(self):
		"""Return next object, or StopIteration"""
		while not self.stored_rorps:
			try: next_rorp = self.rorp_iter.next()
			except StopIteration:
				if self.itr_finished: raise
				else:
					self.ITR.Finish()
					self.itr_finished = 1
			else:
				next_rp = rpath.RPath(self.base_rp.conn, self.base_rp.base,
									  next_rorp.index, next_rorp.data)
				self.ITR(next_rorp.index, next_rp, next_rorp)
		return self.stored_rorps.pop(0)
示例#6
0
 def getrpiter(rorp_iter):
     """Return rp iter by adding indicies of rorp_iter to self.rpath"""
     for rorp in rorp_iter:
         yield rpath.RPath(self.rpath.conn, self.rpath.base, rorp.index,
                           rorp.data)
示例#7
0
	def normalize_path(path):
		"""Used below to normalize the security paths before setting"""
		return rpath.RPath(Globals.local_connection, path).normalize().path
示例#8
0
def cmdpair2rp(cmd_pair):
    """Return normalized RPath from cmd_pair (remote_cmd, filename)"""
    cmd, filename = cmd_pair
    if cmd: conn = init_connection(cmd)
    else: conn = Globals.local_connection
    return rpath.RPath(conn, filename).normalize()
示例#9
0
def get_quoted_sep_base(filename):
    """Get QuotedRPath from filename assuming last bit is quoted"""
    return get_quotedrpath(rpath.RPath(Globals.local_connection, filename), 1)
示例#10
0
def set_security_level(action, cmdpairs):
    """If running client, set security level and restrict_path

	To find these settings, we must look at the action to see what is
	supposed to happen, and then look at the cmdpairs to see what end
	the client is on.

	"""
    def islocal(cmdpair):
        return not cmdpair[0]

    def bothlocal(cp1, cp2):
        return islocal(cp1) and islocal(cp2)

    def bothremote(cp1, cp2):
        return not islocal(cp1) and not islocal(cp2)

    def getpath(cmdpair):
        return cmdpair[1]

    if Globals.server: return
    cp1 = cmdpairs[0]
    if len(cmdpairs) > 1: cp2 = cmdpairs[1]
    else: cp2 = cp1

    if action == "backup" or action == "check-destination-dir":
        if bothlocal(cp1, cp2) or bothremote(cp1, cp2):
            sec_level = "minimal"
            rdir = tempfile.gettempdir()
        elif islocal(cp1):
            sec_level = "read-only"
            rdir = getpath(cp1)
        else:
            assert islocal(cp2)
            sec_level = "update-only"
            rdir = getpath(cp2)
    elif action == "restore" or action == "restore-as-of":
        if len(cmdpairs) == 1 or bothlocal(cp1, cp2) or bothremote(cp1, cp2):
            sec_level = "minimal"
            rdir = tempfile.gettempdir()
        elif islocal(cp1):
            sec_level = "read-only"
            Main.restore_set_root(
                rpath.RPath(Globals.local_connection, getpath(cp1)))
            if Main.restore_root: rdir = Main.restore_root.path
            else: log.Log.FatalError("Invalid restore directory")
        else:
            assert islocal(cp2)
            sec_level = "all"
            rdir = getpath(cp2)
    elif action == "mirror":
        if bothlocal(cp1, cp2) or bothremote(cp1, cp2):
            sec_level = "minimal"
            rdir = tempfile.gettempdir()
        elif islocal(cp1):
            sec_level = "read-only"
            rdir = getpath(cp1)
        else:
            assert islocal(cp2)
            sec_level = "all"
            rdir = getpath(cp2)
    elif action in [
            "test-server", "list-increments", 'list-increment-sizes',
            "list-at-time", "list-changed-since", "calculate-average",
            "remove-older-than", "compare", "compare-hash", "compare-full",
            "verify"
    ]:
        sec_level = "minimal"
        rdir = tempfile.gettempdir()
    else:
        assert 0, "Unknown action %s" % action

    Globals.security_level = sec_level
    Globals.restrict_path = rpath.RPath(Globals.local_connection,
                                        rdir).normalize().path