示例#1
0
def _init(settings):
	"""
	Use config variables like PORTAGE_GRPNAME and PORTAGE_USERNAME to
	initialize global variables. This allows settings to come from make.conf
	instead of requiring them to be set in the calling environment.
	"""
	if '_portage_grpname' not in _initialized_globals and \
		'_portage_username' not in _initialized_globals:

		# Prevents "TypeError: expected string" errors
		# from grp.getgrnam() with PyPy
		native_string = platform.python_implementation() == 'PyPy'

		# PREFIX LOCAL: use var iso hardwired 'portage'
		v = settings.get('PORTAGE_GRPNAME', PORTAGE_GROUPNAME)
		# END PREFIX LOCAL
		if native_string:
			v = portage._native_string(v)
		globals()['_portage_grpname'] = v
		_initialized_globals.add('_portage_grpname')

		# PREFIX LOCAL: use var iso hardwired 'portage'
		v = settings.get('PORTAGE_USERNAME', PORTAGE_USERNAME)
		# END PREFIX LOCAL
		if native_string:
			v = portage._native_string(v)
		globals()['_portage_username'] = v
		_initialized_globals.add('_portage_username')
示例#2
0
文件: data.py 项目: Whissi/portage
def _init(settings):
	"""
	Use config variables like PORTAGE_GRPNAME and PORTAGE_USERNAME to
	initialize global variables. This allows settings to come from make.conf
	instead of requiring them to be set in the calling environment.
	"""
	if '_portage_grpname' not in _initialized_globals and \
		'_portage_username' not in _initialized_globals:

		# Prevents "TypeError: expected string" errors
		# from grp.getgrnam() with PyPy
		native_string = platform.python_implementation() == 'PyPy'

		v = settings.get('PORTAGE_GRPNAME', 'portage')
		if native_string:
			v = portage._native_string(v)
		globals()['_portage_grpname'] = v
		_initialized_globals.add('_portage_grpname')

		v = settings.get('PORTAGE_USERNAME', 'portage')
		if native_string:
			v = portage._native_string(v)
		globals()['_portage_username'] = v
		_initialized_globals.add('_portage_username')

	if 'secpass' not in _initialized_globals:
		v = 0
		if uid == 0:
			v = 2
		elif "unprivileged" in settings.features:
			v = 2
		elif portage_gid in os.getgroups():
			v = 1
		globals()['secpass'] = v
		_initialized_globals.add('secpass')
示例#3
0
def _init(settings):
	"""
	Use config variables like PORTAGE_GRPNAME and PORTAGE_USERNAME to
	initialize global variables. This allows settings to come from make.conf
	instead of requiring them to be set in the calling environment.
	"""
	if '_portage_grpname' not in _initialized_globals and \
		'_portage_username' not in _initialized_globals:

		# Prevents "TypeError: expected string" errors
		# from grp.getgrnam() with PyPy
		native_string = platform.python_implementation() == 'PyPy'

		v = settings.get('PORTAGE_GRPNAME', 'portage')
		if native_string:
			v = portage._native_string(v)
		globals()['_portage_grpname'] = v
		_initialized_globals.add('_portage_grpname')

		v = settings.get('PORTAGE_USERNAME', 'portage')
		if native_string:
			v = portage._native_string(v)
		globals()['_portage_username'] = v
		_initialized_globals.add('_portage_username')

	if 'secpass' not in _initialized_globals:
		v = 0
		if uid == 0:
			v = 2
		elif "unprivileged" in settings.features:
			v = 2
		elif portage_gid in os.getgroups():
			v = 1
		globals()['secpass'] = v
		_initialized_globals.add('secpass')
示例#4
0
def copyfile(src, dest):
    src = _native_string(src, encoding=_encodings["fs"], errors="strict")
    dest = _native_string(dest, encoding=_encodings["fs"], errors="strict")
    (rc, ctx) = selinux.lgetfilecon(src)
    if rc < 0:
        raise OSError(_('copyfile: Failed getting context of "%s".') % src)

    setfscreate(ctx)
    try:
        shutil.copyfile(src, dest)
    finally:
        setfscreate()
示例#5
0
def copyfile(src, dest):
    src = _native_string(src, encoding=_encodings['fs'], errors='strict')
    dest = _native_string(dest, encoding=_encodings['fs'], errors='strict')
    (rc, ctx) = selinux.lgetfilecon(src)
    if rc < 0:
        raise OSError(_("copyfile: Failed getting context of \"%s\".") % src)

    setfscreate(ctx)
    try:
        shutil.copyfile(src, dest)
    finally:
        setfscreate()
示例#6
0
def mkdir(target, refdir):
    target = _native_string(target, encoding=_encodings['fs'], errors='strict')
    refdir = _native_string(refdir, encoding=_encodings['fs'], errors='strict')
    (rc, ctx) = selinux.getfilecon(refdir)
    if rc < 0:
        raise OSError(
         _("mkdir: Failed getting context of reference directory \"%s\".") \
         % refdir)

    setfscreate(ctx)
    try:
        os.mkdir(target)
    finally:
        setfscreate()
示例#7
0
def rename(src, dest):
	src = _native_string(src, encoding=_encodings['fs'], errors='strict')
	dest = _native_string(dest, encoding=_encodings['fs'], errors='strict')
	(rc, ctx) = selinux.lgetfilecon(src)
	if rc < 0:
		if sys.hexversion < 0x3000000:
			src = _unicode_decode(src, encoding=_encodings['fs'], errors='replace')
		raise OSError(_("rename: Failed getting context of \"%s\".") % src)

	setfscreate(ctx)
	try:
		os.rename(src, dest)
	finally:
		setfscreate()
示例#8
0
def symlink(target, link, reflnk):
    target = _native_string(target, encoding=_encodings['fs'], errors='strict')
    link = _native_string(link, encoding=_encodings['fs'], errors='strict')
    reflnk = _native_string(reflnk, encoding=_encodings['fs'], errors='strict')
    (rc, ctx) = selinux.lgetfilecon(reflnk)
    if rc < 0:
        raise OSError(
         _("symlink: Failed getting context of reference symlink \"%s\".") \
         % reflnk)

    setfscreate(ctx)
    try:
        os.symlink(target, link)
    finally:
        setfscreate()
示例#9
0
    def _init_term(self):
        """
		Initialize term control codes.
		@rtype: bool
		@return: True if term codes were successfully initialized,
			False otherwise.
		"""

        term_type = os.environ.get("TERM", "").strip()
        if not term_type:
            return False
        tigetstr = None

        try:
            import curses
            try:
                curses.setupterm(term_type, self.out.fileno())
                tigetstr = curses.tigetstr
            except curses.error:
                pass
        except ImportError:
            pass

        if tigetstr is None:
            return False

        term_codes = {}
        for k, capname in self._termcap_name_map.items():
            # Use _native_string for PyPy compat (bug #470258).
            code = tigetstr(portage._native_string(capname))
            if code is None:
                code = self._default_term_codes[capname]
            term_codes[k] = code
        object.__setattr__(self, "_term_codes", term_codes)
        return True
示例#10
0
	def _init_term(self):
		"""
		Initialize term control codes.
		@rtype: bool
		@return: True if term codes were successfully initialized,
			False otherwise.
		"""

		term_type = os.environ.get("TERM", "").strip()
		if not term_type:
			return False
		tigetstr = None

		try:
			import curses
			try:
				curses.setupterm(term_type, self.out.fileno())
				tigetstr = curses.tigetstr
			except curses.error:
				pass
		except ImportError:
			pass

		if tigetstr is None:
			return False

		term_codes = {}
		for k, capname in self._termcap_name_map.items():
			# Use _native_string for PyPy compat (bug #470258).
			code = tigetstr(portage._native_string(capname))
			if code is None:
				code = self._default_term_codes[capname]
			term_codes[k] = code
		object.__setattr__(self, "_term_codes", term_codes)
		return True
示例#11
0
def mkdir(target, refdir):
	target = _native_string(target, encoding=_encodings['fs'], errors='strict')
	refdir = _native_string(refdir, encoding=_encodings['fs'], errors='strict')
	(rc, ctx) = selinux.getfilecon(refdir)
	if rc < 0:
		if sys.hexversion < 0x3000000:
			refdir = _unicode_decode(refdir, encoding=_encodings['fs'], errors='replace')
		raise OSError(
			_("mkdir: Failed getting context of reference directory \"%s\".") \
			% refdir)

	setfscreate(ctx)
	try:
		os.mkdir(target)
	finally:
		setfscreate()
示例#12
0
def setfscreate(ctx="\n"):
	ctx = _native_string(ctx, encoding=_encodings['content'], errors='strict')
	if selinux.setfscreatecon(ctx) < 0:
		if sys.hexversion < 0x3000000:
			ctx = _unicode_decode(ctx, encoding=_encodings['content'], errors='replace')
		raise OSError(
			_("setfscreate: Failed setting fs create context \"%s\".") % ctx)
示例#13
0
def symlink(target, link, reflnk):
	target = _native_string(target, encoding=_encodings['fs'], errors='strict')
	link = _native_string(link, encoding=_encodings['fs'], errors='strict')
	reflnk = _native_string(reflnk, encoding=_encodings['fs'], errors='strict')
	(rc, ctx) = selinux.lgetfilecon(reflnk)
	if rc < 0:
		if sys.hexversion < 0x3000000:
			reflnk = _unicode_decode(reflnk, encoding=_encodings['fs'], errors='replace')
		raise OSError(
			_("symlink: Failed getting context of reference symlink \"%s\".") \
			% reflnk)

	setfscreate(ctx)
	try:
		os.symlink(target, link)
	finally:
		setfscreate()
示例#14
0
def setexec(ctx="\n"):
	ctx = _native_string(ctx, encoding=_encodings['content'], errors='strict')
	if selinux.setexeccon(ctx) < 0:
		if sys.hexversion < 0x3000000:
			ctx = _unicode_decode(ctx, encoding=_encodings['content'], errors='replace')
		if selinux.security_getenforce() == 1:
			raise OSError(_("Failed setting exec() context \"%s\".") % ctx)
		else:
			portage.writemsg("!!! " + \
				_("Failed setting exec() context \"%s\".") % ctx, \
				noiselevel=-1)
示例#15
0
文件: _selinux.py 项目: ezc/portage
def setexec(ctx="\n"):
    ctx = _native_string(ctx, encoding=_encodings['content'], errors='strict')
    if selinux.setexeccon(ctx) < 0:
        if sys.hexversion < 0x3000000:
            ctx = _unicode_decode(ctx,
                                  encoding=_encodings['content'],
                                  errors='replace')
        if selinux.security_getenforce() == 1:
            raise OSError(_("Failed setting exec() context \"%s\".") % ctx)
        else:
            portage.writemsg("!!! " + \
             _("Failed setting exec() context \"%s\".") % ctx, \
             noiselevel=-1)
示例#16
0
def rename(src, dest):
    src = _native_string(src, encoding=_encodings['fs'], errors='strict')
    (rc, ctx) = selinux.lgetfilecon(src)
    if rc < 0:
        raise OSError(_("rename: Failed getting context of \"%s\".") % src)

    setfscreate(ctx)
    try:
        os.rename(
            _unicode_encode(src, encoding=_encodings['fs'], errors='strict'),
            _unicode_encode(dest, encoding=_encodings['fs'], errors='strict'))
    finally:
        setfscreate()
示例#17
0
def mkdir(target, refdir):
    refdir = _native_string(refdir, encoding=_encodings["fs"], errors="strict")
    (rc, ctx) = selinux.getfilecon(refdir)
    if rc < 0:
        raise OSError(
            _('mkdir: Failed getting context of reference directory "%s".') % refdir
        )

    setfscreate(ctx)
    try:
        os.mkdir(_unicode_encode(target, encoding=_encodings["fs"], errors="strict"))
    finally:
        setfscreate()
示例#18
0
def check_locale(silent=False, env=None):
    """
    Check whether the locale is sane. Returns True if it is, prints
    warning and returns False if it is not. Returns None if the check
    can not be executed due to platform limitations.
    """

    if env is not None:
        for v in ("LC_ALL", "LC_CTYPE", "LANG"):
            if v in env:
                mylocale = env[v]
                break
        else:
            mylocale = "C"

        try:
            return _check_locale_cache[mylocale]
        except KeyError:
            pass

    pid = os.fork()
    if pid == 0:
        portage._ForkWatcher.hook(portage._ForkWatcher)
        try:
            if env is not None:
                try:
                    locale.setlocale(locale.LC_CTYPE,
                                     portage._native_string(mylocale))
                except locale.Error:
                    os._exit(2)

            ret = _check_locale(silent)
            if ret is None:
                os._exit(2)
            else:
                os._exit(0 if ret else 1)
        except Exception:
            traceback.print_exc()
            os._exit(2)

    pid2, ret = os.waitpid(pid, 0)
    assert pid == pid2
    pyret = None
    if os.WIFEXITED(ret):
        ret = os.WEXITSTATUS(ret)
        if ret != 2:
            pyret = ret == 0

    if env is not None:
        _check_locale_cache[mylocale] = pyret
    return pyret
示例#19
0
def rename(src, dest):
    src = _native_string(src, encoding=_encodings["fs"], errors="strict")
    (rc, ctx) = selinux.lgetfilecon(src)
    if rc < 0:
        raise OSError(_('rename: Failed getting context of "%s".') % src)

    setfscreate(ctx)
    try:
        os.rename(
            _unicode_encode(src, encoding=_encodings["fs"], errors="strict"),
            _unicode_encode(dest, encoding=_encodings["fs"], errors="strict"),
        )
    finally:
        setfscreate()
示例#20
0
def check_locale(silent=False, env=None):
	"""
	Check whether the locale is sane. Returns True if it is, prints
	warning and returns False if it is not. Returns None if the check
	can not be executed due to platform limitations.
	"""

	if env is not None:
		for v in ("LC_ALL", "LC_CTYPE", "LANG"):
			if v in env:
				mylocale = env[v]
				break
		else:
			mylocale = "C"

		try:
			return _check_locale_cache[mylocale]
		except KeyError:
			pass

	pid = os.fork()
	if pid == 0:
		try:
			if env is not None:
				try:
					locale.setlocale(locale.LC_CTYPE,
						portage._native_string(mylocale))
				except locale.Error:
					os._exit(2)

			ret = _check_locale(silent)
			if ret is None:
				os._exit(2)
			else:
				os._exit(0 if ret else 1)
		except Exception:
			traceback.print_exc()
			os._exit(2)

	pid2, ret = os.waitpid(pid, 0)
	assert pid == pid2
	pyret = None
	if os.WIFEXITED(ret):
		ret = os.WEXITSTATUS(ret)
		if ret != 2:
			pyret = ret == 0

	if env is not None:
		_check_locale_cache[mylocale] = pyret
	return pyret
示例#21
0
def symlink(target, link, reflnk):
    reflnk = _native_string(reflnk, encoding=_encodings["fs"], errors="strict")
    (rc, ctx) = selinux.lgetfilecon(reflnk)
    if rc < 0:
        raise OSError(
            _('symlink: Failed getting context of reference symlink "%s".') % reflnk
        )

    setfscreate(ctx)
    try:
        os.symlink(
            _unicode_encode(target, encoding=_encodings["fs"], errors="strict"),
            _unicode_encode(link, encoding=_encodings["fs"], errors="strict"),
        )
    finally:
        setfscreate()
示例#22
0
def setexec(ctx="\n"):
    ctx = _native_string(ctx, encoding=_encodings['content'], errors='strict')
    rc = 0
    try:
        rc = selinux.setexeccon(ctx)
    except OSError:
        msg = _("Failed to set new SELinux execution context. " + \
         "Is your current SELinux context allowed to run Portage?")
        if selinux.security_getenforce() == 1:
            raise OSError(msg)
        else:
            portage.writemsg("!!! %s\n" % msg, noiselevel=-1)

    if rc < 0:
        if selinux.security_getenforce() == 1:
            raise OSError(_("Failed setting exec() context \"%s\".") % ctx)
        else:
            portage.writemsg("!!! " + \
             _("Failed setting exec() context \"%s\".") % ctx, \
             noiselevel=-1)
示例#23
0
def setexec(ctx="\n"):
	ctx = _native_string(ctx, encoding=_encodings['content'], errors='strict')
	rc = 0
	try:
		rc = selinux.setexeccon(ctx)
	except OSError:
		msg = _("Failed to set new SELinux execution context. " + \
			"Is your current SELinux context allowed to run Portage?")
		if selinux.security_getenforce() == 1:
			raise OSError(msg)
		else:
			portage.writemsg("!!! %s\n" % msg, noiselevel=-1)

	if rc < 0:
		if sys.hexversion < 0x3000000:
			ctx = _unicode_decode(ctx, encoding=_encodings['content'], errors='replace')
		if selinux.security_getenforce() == 1:
			raise OSError(_("Failed setting exec() context \"%s\".") % ctx)
		else:
			portage.writemsg("!!! " + \
				_("Failed setting exec() context \"%s\".") % ctx, \
				noiselevel=-1)
示例#24
0
	def __init__(self, spawn_func, selinux_type):
		self._spawn_func = spawn_func
		selinux_type = _native_string(selinux_type, encoding=_encodings['content'], errors='strict')
		self._con = settype(selinux_type)
示例#25
0
 def __init__(self, spawn_func, selinux_type):
     self._spawn_func = spawn_func
     selinux_type = _native_string(
         selinux_type, encoding=_encodings["content"], errors="strict"
     )
     self._con = settype(selinux_type)
示例#26
0
def setfscreate(ctx="\n"):
    ctx = _native_string(ctx, encoding=_encodings["content"], errors="strict")
    if selinux.setfscreatecon(ctx) < 0:
        raise OSError(_('setfscreate: Failed setting fs create context "%s".') % ctx)
示例#27
0
def setfscreate(ctx="\n"):
    ctx = _native_string(ctx, encoding=_encodings['content'], errors='strict')
    if selinux.setfscreatecon(ctx) < 0:
        raise OSError(
            _("setfscreate: Failed setting fs create context \"%s\".") % ctx)