
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
UZ(X                 @   s  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d d	lmZ d d
lmZ d dlmZmZ d dlmZ y4d dlZd dlZd dlmZmZmZmZmZmZ W nF ek
r   d dljZd dl j!Ze"Ze#Ze$Ze%Ze&Ze&Ze'Z(Y nX yd dl)m*Z* W n ek
rF   dZ*Y nX d dl+Z+e+j,dddZ-e.e-dsre-j+e-_/e-j/Z0dZ1de1 Z2de1 Z3dZ4d dlm5Z5 e5j6 Z7ej8 Z9dd Z:dd Z;dd Z<dd Z=d d! Z>d"d# Z?d$d% Z@d&d' ZAd(d) ZBd*d+ ZCd,d- ZDd.d/ ZEd0d1 ZFd2d3 ZGd4d5 ZHd6d7 ZIG d8d9 d9ZJd^d:d;ZKd<d= ZLd>d? ZMdddddi fd@dAZNdBdC ZOdDdE ZPdFdG ZQdHdI ZRdJdK ZSdLdM ZTd_dNdOZUG dPdQ dQZVdRdS ZWdTdU ZXd`dVdWZYdXdY ZZe dZkrd[d\ Z[nd d]l\m[Z[ dS )a    N)up2dateUtils)up2dateErrors)	rhnserver)pkgUtils)
up2dateLog)rhnreg_constants)hardware)convertPackagesFromHashToList)getPlatform)ustrsstr)raise_with_tb)ListType	TupleType
StringTypeUnicodeTypeDictTypeDictionaryType)supportzrhn-client-toolsT)Zfallbackugettextz/etc/sysconfig/rhnz%s/rhn_register_remindz%s/hw-activation-codez/etc/pki/consumer/cert.pem)configc              C   s   t jdt jt jB rt jdt jrZt jdt jt jB rLt jd t jd qttd n|t jdt jt jB rzt jd nttd	 d
} t j| t jt jB st jdt jt jB rd} t jd|  }|rt jd|   d S )Nz/usr/sbin/rhnsdz%/usr/lib/systemd/system/rhnsd.servicez/usr/bin/systemctlz+/usr/bin/systemctl enable rhnsd > /dev/nullz*/usr/bin/systemctl start rhnsd > /dev/nullz,Warning: unable to enable rhnsd with systemdz/sbin/chkconfigz$/sbin/chkconfig rhnsd on > /dev/nullz.Warning: unable to enable rhnsd with chkconfigz/sbin/servicez/usr/sbin/servicez%s rhnsd status > /dev/nullz%s rhnsd start > /dev/null)osaccessR_OKX_OKsystemprint_)Zservice_pathZrc r   /usr/lib/python3.6/rhnreg.py
startRhnsdB   s     
r    c              C   s   t d p
d} tj| tjsi S t| d}|j }i }x`|D ]X}|j }|dkrPq:y|jd\}}W n" tk
r   t	t
j| Y nX |j ||< q:W |S )NZoemInfoFilez/etc/sysconfig/rhn/oeminfor :)cfgr   r   r   open	readlinesstripsplit
ValueErrorr   r   ZOemInfoFileError)Z
configFilefdLinfoikeyvaluer   r   r   
getOemInfo\   s     

r0   c              C   s*   t jtt jr"t jt} | jdkS dS dS )z@ Returns true if system is registred using subscription manager r   FN)r   r   	RHSM_FILEr   statst_size)Zstatinfor   r   r   rhsm_registereds   s    

r4   c               C   s   t jtd t jS )NsystemIdPath)r   r   r$   r   r   r   r   r   
registered{   s    r6   c              C   s$   t jtt js ttd} | j  d S )Nzw+)r   r   REMIND_FILEr   r%   close)r*   r   r   r   createSystemRegisterRemindFile~   s    
r9   c               C   s   t jtt jrt jt d S )N)r   r   r7   r   unlinkr   r   r   r   removeSystemRegisterRemindFile   s    r;   c             C   s   t jj| }t j|t jsdS t j| t jrLyt j| | d  W n
   dS t j| t jt j	B t
dd}t j|d}z|jt| W d|j  X dS )z; Write a file to disk that is not readable by other users. Fz.saveZ0600   wNT)r   pathdirnamer   W_OKF_OKrenamer%   O_WRONLYO_CREATintfdopenwriter   r8   )Zsecure_fileZfile_contentsZdir_namer*   Zfd_filer   r   r   _write_secure_file   s    
rH   c             C   s"   t td | }|rt  t  |S )Nr5   )rH   r$   r;   updateRhsmStatus)systemIdresr   r   r   writeSystemId   s
    rL   c             C   s   t t| d S )z=Returns True if the write is successful or False if it fails.
)rH   HW_CODE_FILE)hw_activation_coder   r   r   writeHWCode   s    rP   c              C   sp   y"t jjdr t \} }| |fS W n tk
r6   Y nX t \} }| dk	rR| |fS t \} }| dk	rl| |fS dS )a  
    This function returns the UUID and virtualization type of this system, if
    it is a guest.  Otherwise, it returns None.  To figure this out, we'll
    use a number of heuristics (list in order of precedence):

       1.  Check /proc/xen/xsd_port.  If exists, we know the system is a
           host; exit.
       2.  Check SMBIOS.  If vendor='Xen' and UUID is non-zero, we know the
           system is a fully-virt guest; exit.
       3.  Check /sys/hypervisor/uuid.  If exists and is non-zero, we know
           the system is a para-virt guest; exit.
       4.  If non of the above checks worked; we know we have a
           non-xen-enabled system; exit.
    z/proc/xen/xsd_portN)NN)r   r>   existsget_fully_virt_infoIOErrorget_para_virt_info)uuid	virt_typer   r   r   get_virt_info   s    


rW   c              C   sV   y<t dd} | j }| j  |j jddjd}d}||fS  tk
rP   Y nX dS )	z
    This function checks /sys/hypervisor/uuid to see if the system is a
    para-virt guest.  It returns a (uuid, virt_type) tuple.
    z/sys/hypervisor/uuidr!   -r"   z
ZparaN)NN)r%   readr8   lowerreplacerstriprS   )Z	uuid_filerU   rV   r   r   r   rT      s    
rT   c              C   s@   t j } t j }| j dkr8|j jdd}d}||fS dS dS )z
    This function looks in the SMBIOS area to determine if this is a
    fully-virt guest.  It returns a (uuid, virt_type) tuple.
    ZxenrX   r"   ZfullyN)NN)r   Z
dmi_vendorZdmi_system_uuidrZ   r[   )ZvendorrU   rV   r   r   r   rR      s    rR   c             C   s   t d|  } t| tdkS )Nz0x%sr   )evallong)rU   r   r   r   _is_host_uuid   s    r_   c              C   s   t j } | jj S )N)r   	RhnServerregistrationZwelcome_message)sr   r   r   welcomeText   s    rc   c              C   s   t j } | jj  d S )N)r   r`   capabilitiesZvalidate)rb   r   r   r   getCaps  s    re   c             C   s   t j }|jj| |S )N)r   r`   ra   Zreserve_user)usernamepasswordrb   r   r   r   reserveUser
  s    rh   c               @   sf   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )RegistrationResultNc             C   sB   || _ || _|| _|| _|| _t|dkr2|| _nd | _|| _d S )Nr   )	_systemId	_channels_failedChannels_systemSlots_failedSystemSlotslen_universalActivationKeyrawDict)selfrJ   channelsZfailedChannelssystemSlotsZfailedSystemSlotsZuniversalActivationKeyrq   r   r   r   __init__  s    zRegistrationResult.__init__c             C   s   | j S )N)rj   )rr   r   r   r   getSystemId  s    zRegistrationResult.getSystemIdc             C   s   | j S )N)rk   )rr   r   r   r   getChannels!  s    zRegistrationResult.getChannelsc             C   s   | j S )N)rl   )rr   r   r   r   getFailedChannels$  s    z$RegistrationResult.getFailedChannelsc             C   s   | j S )N)rm   )rr   r   r   r   getSystemSlots'  s    z!RegistrationResult.getSystemSlotsc                s    fdd j D S )Nc                s   g | ]} j |qS r   )_getSlotDescription).0rb   )rr   r   r   
<listcomp>+  s    z@RegistrationResult.getSystemSlotDescriptions.<locals>.<listcomp>)rm   )rr   r   )rr   r   getSystemSlotDescriptions*  s    z,RegistrationResult.getSystemSlotDescriptionsc                s    fdd j D S )Nc                s   g | ]} j |qS r   )_getFailedSlotDescription)r{   rb   )rr   r   r   r|   .  s    zFRegistrationResult.getFailedSystemSlotDescriptions.<locals>.<listcomp>)rn   )rr   r   )rr   r   getFailedSystemSlotDescriptions-  s    z2RegistrationResult.getFailedSystemSlotDescriptionsc             C   s   | j S )z5Returns None if no universal activation key was used.)rp   )rr   r   r   r   getUniversalActivationKey0  s    z,RegistrationResult.getUniversalActivationKeyc             C   s   t | jdkot | jdkS )zReturns True if the system was subscribed to at least one channel
        and was given any type of system slot so it will get updates. In other
        words, returns True if the system will be getting at least basic
        updates.

        r   )ro   rk   rm   )rr   r   r   r   hasBaseAndUpdates4  s    	z$RegistrationResult.hasBaseAndUpdatesc             C   s&   |dkrt jd t j S | j|S d S )Nvirtualization_host )r   VIRTZVIRT_FAILEDrz   )rr   slotr   r   r   r~   ?  s    z,RegistrationResult._getFailedSlotDescriptionc             C   s$   |dkrt jS |dkrt jS |S d S )NZenterprise_entitledr   )r   Z
MANAGEMENTr   )rr   r   r   r   r   rz   E  s
    z&RegistrationResult._getSlotDescription)N)__name__
__module____qualname__ru   rv   rw   rx   ry   r}   r   r   r   r~   rz   r   r   r   r   ri     s   
ri   c       
      C   s   |t j t j t j d}|r<x|j D ]\}}|||< q(W |rJ||d< n| |d< ||d< td rrttj |d< t	j
 }|jj|}	|	S )ziWrapper for the old xmlrpc to register a system. Activates subscriptions
    if a reg num is given.

    )Zprofile_nameZ
os_releaseZrelease_nameZarchitecturetokenrf   rg   supportsSMBIOSsmbios)r   
getVersiongetOSReleasegetArchitemsr$   _encode_charactersr   
get_smbiosr   r`   ra   Z
new_system)
rf   rg   profileNamer   otherZ	auth_dictr.   itemrb   retr   r   r   registerSystemN  s     
r   c              C   sl   y,t j } | j| dddd}t j|dd}W n t jk
rB   d S X y|j  W n t jk
rf   Y nX d S )Nzcom.redhat.SubscriptionManagerz/EntitlementStatusF)Z
introspectz0com.redhat.SubscriptionManager.EntitlementStatus)Zdbus_interface)dbusZ	SystemBusZProxyObjectClassZ	InterfaceZDBusExceptionZcheck_status)ZbusZvalidity_objZvalidity_ifacer   r   r   rI   k  s    
rI   c             C   s   t j }tj }tj }tj }d }y|jj| ||||}W n@ tj	k
r|   t
j d }|jdkrvttj|j n Y nX |S )N   c   )r   r`   r   r   r   Z
getReleasera   Zavailable_eus_channels	xmlrpclibZFaultsysexc_infoZ	faultCoder   r   Z
DelayErrorZfaultString)rf   rg   rb   Zserver_archZserver_versionZserver_releaseZavailableChannelsfr   r   r   getAvailableChannels  s     

r   c       
   	   C   s$  |dkri }|r6| dkst |dks(t |dk	sZt n$| dk	sBt |dk	sNt |dksZt x|j D ]}|dksdt qdW td rttj |d< tj }|r|jj	|t
j t
j t
j ||}n$|jj|t
j t
j t
j | ||}tjd	|  t|d
 |d |d |d |d |d |d}	|	S )a  Uses the new xmlrpcs to register a system. Returns a dict instead of just
    system id.

    The main differences between this and registerSystem and that this doesn't
    do activation and does child channel subscriptions if possible. See the
    documentation for the xmlrpc handlers in backend for more detail.

    If nothing is going to be in other, it can be {} or None.

    New in RHEL 5.

    Nregistration_numberorg_id	virt_uuidrV   channelr   r   zReturned:
%sZ	system_idrs   Zfailed_channelsZsystem_slotsZfailed_system_slotsZuniversal_activation_key)rq   )r   r   r   rV   r   )AssertionErrorkeysr$   r   r   r   r   r`   ra   Znew_system_activation_keyr   r   r   r   Znew_system_user_passlog	log_debugri   )
rf   rg   r   ZpackagesZactivationKeyr   r.   rb   r,   resultr   r   r   registerSystem2  sN       r   c               C   s   t d S )NZsupportsEUS)r$   r   r   r   r   server_supports_eus  s    r   c                sF   dd  t j }|jjdds0 fdd|D }|jj| t| d S )Nc             S   s   | d dkrd| kr| d= | S )NclassZNETINFOZip6addrr   )xr   r   r   remove_ip6addr  s    z$sendHardware.<locals>.remove_ip6addrZipv6r   c                s   g | ]} |qS r   r   )r{   r-   )r   r   r   r|     s    z sendHardware.<locals>.<listcomp>)r   r`   rd   hasCapabilityra   Zadd_hw_profiler   )rJ   ZhardwareListrb   r   )r   r   sendHardware  s
    r   c             C   s0   t j }|jjddst|}|jj| | d S )Nz xmlrpc.packages.extended_profile   )r   r`   rd   r   r	   ra   Zadd_packages)rJ   ZpackageListrb   r   r   r   sendPackages  s    r   c             C   s   t d k	rt j  d S )N)r   Zrefresh)rJ   r   r   r   sendVirtInfo  s    r   c             C   s   t j }t|jj|   d S )N)r   r`   r   ra   Zlist_packages)rJ   rb   r   r   r   listPackages  s    r   c             C   s   t j | \}}}}}}|dks&|dkrDd|  } t j | \}}}}}}|d	krVtjd|dksn|dksn|dkrrd}t j||||||f} | S )
zzRaises up2dateErrors.InvalidProtocolError if the server url has a
    protocol specified and it's not http or https.

    Nr"   zhttps://httpshttpzCYou specified an invalid protocol. Only https and http are allowed./z/XMLRPC)r   r   )urlparser   ZInvalidProtocolErrorZ
urlunparse)serverZprotocolhostr>   Z
parametersZqueryZfragmentIdentifierr   r   r   makeNiceServerUrl  s    
r   c             C   s   dS )zdReturns 'hosted' if the url points to a known hosted server. Otherwise
    returns 'satellite'.
    Z	satelliter   )Z	serverUrlr   r   r   getServerType  s    r   c               @   sB   e Zd ZdZdZi i fddZdd Zdd Zd	d
 Zdd Z	dS )ActivationResultr   r   c             C   s   || _ || _|| _|| _dS )zschannels and systemSlots are dicts where the key/value pairs are
        label (string) / quantity (int).

        N)_status_regNumrk   rm   )rr   ZstatusZregistrationNumberrs   rt   r   r   r   ru     s    zActivationResult.__init__c             C   s   | j S )N)r   )rr   r   r   r   	getStatus  s    zActivationResult.getStatusc             C   s   | j S )N)r   )rr   r   r   r   getRegistrationNumber!  s    z&ActivationResult.getRegistrationNumberc             C   s   | j S )z7Returns a dict- the key/value pairs are label/quantity.)rk   )rr   r   r   r   getChannelsActivated$  s    z%ActivationResult.getChannelsActivatedc             C   s   | j S )z7Returns a dict- the key/value pairs are label/quantity.)rm   )rr   r   r   r   getSystemSlotsActivated(  s    z(ActivationResult.getSystemSlotsActivatedN)
r   r   r   ACTIVATED_NOWALREADY_USEDru   r   r   r   r   r   r   r   r   r     s   r   c              G   s   g }x| D ]}t |}|tkr(t|}nZ|tkrDtdd |D }n>|tkr\dd |D }n&|tksl|tkrtdd |j	 D }|j
| q
W t|dkr|d S t|S dS )	u    All the data we gathered from dmi, bios, gudev are in utf-8,
            we need to convert characters beyond ord(127) - e.g ® to unicode.
        c             s   s   | ]}t |V  qd S )N)r   )r{   r-   r   r   r   	<genexpr>6  s    z%_encode_characters.<locals>.<genexpr>c             S   s   g | ]}t |qS r   )r   )r{   r-   r   r   r   r|   8  s    z&_encode_characters.<locals>.<listcomp>c             S   s   g | ]\}}t ||qS r   )r   )r{   namevalr   r   r   r|   :  s    r   r   N)typer   r   r   tupler   r   r   dictr   appendro   )argsr   r   Z	item_typer   r   r   r   ,  s    

r   c             C   s   d }d }d }yt j }t|}W n$   tjd tjtj   Y nX |d k	ry.t| ||}|j	 t
jkrz|j }t| W n> tjk
r   tjd Y n  tjk
r   tjd Y nX |S )NzMThere was an error while reading the hardware info from the bios. Traceback:
z<There are are no entitlements associated with this hardware.z,The hardware id was not recognized as valid.)r   Zget_hal_system_and_smbiosr   r   log_meZlog_exceptionr   r   activateHardwareInfor   r   r   r   rP   r   ZNotEntitlingErrorr   ZInvalidRegistrationNumberError)Zloginrg   ZactivateHWResulthardwareInforO   r   r   r   _activate_hardwareB  s*    

r   c       
      C   s   i }|rd|i}t j }|jj| |||}|d }|d }tjd|  |dkr\ttj|S |dkrpttj|S d| }	t	j
|	dS )	zTries to activate an entitlement linked to the hardware info that we
    read from the bios.

    Returns an ActivationResult.
    Can raise:
        Invalid number.
        Hardware info is not entitling.
        Communication errors, etc

    r   Zstatus_coder   zServer returned status code %sr   r   zNThe server returned unknown status code %s while activating the hardware info.N)r   r`   ra   Zactivate_hardware_infor   r   r   r   r   r   ZCommunicationError)
rf   rg   r   ZorgIdr   r   r   Z
statusCodeZregNummessager   r   r   r   ^  s     
r   c              C   sd   t jdt jt jB rVddlm} m} | dg|||d}tdd |jj	 |j
j	   n
tjd d S )Nz/usr/sbin/rhn_checkr   )PopenPIPE)stdinstdoutstderrc             S   s
   t j| S )N)r   r   )r   r   r   r   <lambda>  s    z$spawnRhnCheckForUI.<locals>.<lambda>z Warning: unable to run rhn_check)r   r   r   r   
subprocessr   r   mapr   r&   r   r   r   )r   r   pr   r   r   spawnRhnCheckForUI  s    
r   Zdebc               C   s   dS )z.On Debian no extra action for plugin is neededr   r   )r   r   r   r   r   r   r   pluginEnable  s    r   )r   )NNNNN)N)N)]r   r   r   Zup2date_clientr   r   r   r   r   r   r   Zup2date_client.rhnPackageInfor	   Zup2date_client.pkgplatformr
   Zrhn.i18nr   r   Zrhn.tbr   r   r   typesr   r   r   r   r   r   ImportErrorZurllib.parseparseZxmlrpc.clientZclientlistr   bytesstrr   rE   r^   Zvirtualizationr   gettextZtranslationthasattrr   r   Z	SYSID_DIRr7   rN   r1   r   ZinitUp2dateConfigr$   ZinitLogr   r    r0   r4   r6   r9   r;   rH   rL   rP   rW   rT   rR   r_   rc   re   rh   ri   r   rI   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zup2date_client.pmPluginr   r   r   r   <module>
   s   $



,?  
;


"

