HEX
Server: Apache/2.4.6 () OpenSSL/1.0.2k-fips PHP/8.3.8
System: Linux gateway.rmc-logistics.net 4.1.12-124.48.6.el7uek.x86_64 #2 SMP Tue Mar 16 14:57:50 PDT 2021 x86_64
User: apache (48)
PHP: 8.3.8
Disabled: NONE
Upload Files
File: //usr/share/yum-plugins/ulninfo.py
"""
Yum plugin for ULN access.
This plugin provides ULN license, notification, entitlement to yum client.
"""
import os, sys, urllib2
sys.path.append("/usr/share/rhn/")
from up2date_client import config
from up2date_client import rhnChannel
from up2date_client import up2dateAuth
from up2date_client import up2dateErrors

requires_api_version = '2.5'

def init_hook(conduit):
    # grab the global yum config
    conduit_conf = conduit.getConf()
    timeout = conduit.confFloat('main', 'timeout', conduit_conf.timeout)

    # first, check that the system is registered to ULN
    # we don't actually need to report any errors as the main yum-rhn-plugin 
    # will take care of that
    try:
        login_info = up2dateAuth.getLoginInfo(timeout=timeout)
    except up2dateErrors.RhnServerException:
        return
    
    # if no login info, just return from this plugn
    if not login_info:
        return

    # now, we're registered, we need to get the list of channels
    # a registered system with no subscribed channels is not really a valid
    # scenario, but just in case catch any associated error and die quietly
    try:
        svrChannels = rhnChannel.getChannelDetails()
    except up2dateErrors.NoChannelsError:
        return

    # grab config information from /etc/sysconfig/rhn/up2date
    up2date_cfg = config.initUp2dateConfig()

    # if the serverUrl isn't pointing to ULN, and we've gotten this far, we 
    # must be registered to a spacewalk instance, in which case, 
    # don't bother looking for licenses 
    base_url = up2date_cfg['serverURL'].split("/")[2]
    if base_url != "linux-update.oracle.com":
        return

    # check the global proxy settings and be sure to import them
    # again, any issues here would also get reported from the main plugin
    # so, just die quietly
    try:
        proxy_url = get_proxy_url(up2date_cfg)
        proxy_dict = {}
        if proxy_url:
            if up2date_cfg['useNoSSLForPackages']:
                proxy_dict = {'http' : proxy_url}
            else:
                proxy_dict = {'https' : proxy_url}
    except BadProxyConfig:
        return 

    # check commands and options which don't need network communication
    prog_name = os.path.basename(sys.argv[0])
    if prog_name == 'yum':
        cmd_args = sys.argv[1:]
        uln_enabled = True
        if ('--help' in cmd_args or '--version' in cmd_args or cmd_args == [] or '-C' in cmd_args or '--cacheonly' in cmd_args):
            uln_enabled = False
            return

    # if we got this far, we are registered and have channels
    for channel in svrChannels:
        if channel['version']:
            try:
                # if the proxy_dict is set to something other than None, we need to use it
                if proxy_dict:
                    p = urllib2.ProxyHandler(proxy_dict)
                    opener = urllib2.build_opener(p)
                    urllib2.install_opener(opener)
                # now open the url as normal, if nothing there or an error returned, die quietly
                print urllib2.urlopen("https://"+base_url+"/license/%s" %channel['label']).read()
            except:
                pass

class BadConfig(Exception):
    pass

class BadProxyConfig(BadConfig):
    pass


def get_proxy_url(up2date_cfg):
    if not up2date_cfg['enableProxy']:
        return None

    proxy_url = ""
    if up2date_cfg['useNoSSLForPackages']:
        proxy_url = 'http://'
    else:
        proxy_url = 'https://'
    if up2date_cfg['enableProxyAuth']:
        if not up2date_cfg.has_key('proxyUser') or \
            up2date_cfg['proxyUser'] == '':
            raise BadProxyConfig
        if not up2date_cfg.has_key('proxyPassword') or \
            up2date_cfg['proxyPassword'] == '':
            raise BadProxyConfig
        proxy_url = proxy_url + up2date_cfg['proxyUser']
        proxy_url = proxy_url + ':'
        proxy_url = proxy_url + urllib2.quote(up2date_cfg['proxyPassword'])
        proxy_url = proxy_url + '@'
   
    try:
        netloc = up2date_cfg['httpProxy']
        if netloc == '':
            raise BadProxyConfig
    except KeyError:
        raise BadProxyConfig

    # Check if a protocol is supplied. We'll ignore it.
    proto_split = netloc.split('://')
    if len(proto_split) > 1:
       netloc = proto_split[1]

    return proxy_url + netloc