예제 #1
0
    def _chown_chmod_chcon(self,
                           temp_file_path,
                           dest_path,
                           file_info,
                           strict_ownership=1):
        if file_info['filetype'] != 'symlink':
            uid = file_info.get('uid')
            if uid is None:
                if file_info.has_key('username'):
                    # determine uid

                    try:
                        user_record = pwd.getpwnam(file_info['username'])
                        uid = user_record[2]
                    except Exception, e:
                        #Check if username is an int
                        try:
                            uid = int(file_info['username'])
                        except ValueError:
                            raise cfg_exceptions.UserNotFound(
                                file_info['username']), None, sys.exc_info()[2]
                else:
                    #default to root (3.2 sats)
                    uid = 0

            gid = file_info.get('gid')
            if gid is None:
                if file_info.has_key('groupname'):
                    # determine gid
                    try:
                        group_record = grp.getgrnam(file_info['groupname'])
                        gid = group_record[2]
                    except Exception, e:
                        try:
                            gid = int(file_info['groupname'])
                        except ValueError:
                            raise cfg_exceptions.GroupNotFound(
                                file_info['groupname']), None, sys.exc_info(
                                )[2]

                else:
                    #default to root (3.2 sats)
                    gid = 0
예제 #2
0
    def _chown_chmod_chcon(self,
                           temp_file_path,
                           dest_path,
                           file_info,
                           strict_ownership=1):
        if file_info['filetype'] != 'symlink':
            uid = file_info.get('uid')
            if uid is None:
                if 'username' in file_info:
                    # determine uid

                    try:
                        user_record = pwd.getpwnam(file_info['username'])
                        uid = user_record[2]
                    except Exception:
                        e = sys.exc_info()[1]
                        #Check if username is an int
                        try:
                            uid = int(file_info['username'])
                        except ValueError:
                            raise_with_tb(
                                cfg_exceptions.UserNotFound(
                                    file_info['username']),
                                sys.exc_info()[2])
                else:
                    #default to root (3.2 sats)
                    uid = 0

            gid = file_info.get('gid')
            if gid is None:
                if 'groupname' in file_info:
                    # determine gid
                    try:
                        group_record = grp.getgrnam(file_info['groupname'])
                        gid = group_record[2]
                    except Exception:
                        e = sys.exc_info()[1]
                        try:
                            gid = int(file_info['groupname'])
                        except ValueError:
                            raise_with_tb(
                                cfg_exceptions.GroupNotFound(
                                    file_info['groupname']),
                                sys.exc_info()[2])

                else:
                    #default to root (3.2 sats)
                    gid = 0

        try:
            if file_info['filetype'] != 'symlink':
                os.chown(temp_file_path, uid, gid)

                mode = '600'
                if 'filemode' in file_info:
                    if file_info['filemode'] is "":
                        mode = '000'
                    else:
                        mode = file_info['filemode']

                mode = int(str(mode), 8)
                os.chmod(temp_file_path, mode)

            if 'selinux_ctx' in file_info:
                sectx = file_info.get('selinux_ctx')
                if sectx is not None and sectx is not "":
                    log_debug(1, "selinux context: " + sectx)
                    try:
                        if lsetfilecon(temp_file_path, sectx) < 0:
                            raise Exception(
                                "failed to set selinux context on %s" %
                                dest_path)
                    except OSError:
                        e = sys.exc_info()[1]
                        raise_with_tb(
                            Exception(
                                "failed to set selinux context on %s" %
                                dest_path, e),
                            sys.exc_info()[2])

        except OSError:
            e = sys.exc_info()[1]
            if e.errno == errno.EPERM and not strict_ownership:
                sys.stderr.write(
                    "cannonical file ownership and permissions lost on %s\n" %
                    dest_path)
            else:
                raise