HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //proc/self/root/lib/python3.6/site-packages/ipaclient/install/__pycache__/ipa_epn.cpython-36.pyc
3

�d[er�@s�dZddlmZmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
mZddlmZmZddlmZmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZdd
l m!Z!m"Z"ddl#m$Z$ddl%m&Z&m'Z'ddl(m)Z)ddl*m+Z+m,Z,m-Z-dZ.ddddddddddddddddd�Z/e	j0e1�Z2d(dd�Z3Gd d!�d!�Z4Gd"d#�d#e&j5�Z6Gd$d%�d%�Z7Gd&d'�d'�Z8dS))zoThis tool prepares then sends email notifications to users
   whose passwords are expiring in the near future.
�)�absolute_import�print_functionN)�deque)�datetime�	timedelta)�
formataddr�
formatdate)�
MIMEMultipart)�MIMEText)�Header)�
make_msgid)�error)�paths)�api�errors)�is_ipa_client_configured)�	admintool�ipaldap)�DN)�Environment�FileSystemLoader�TemplateSyntaxErrorz/etc/ipa/epn.conf�	localhost��<�nonezroot@localhostz28,14,7,3,1�utf8�plainzYour password will expire soon.)�smtp_server�	smtp_port�	smtp_user�
smtp_password�smtp_client_cert�smtp_client_key�smtp_client_key_pass�smtp_timeout�
smtp_security�
smtp_admin�
smtp_delay�	mail_from�notify_ttls�msg_charset�msg_subtype�msg_subject�daemoncCs�ybtj�dkrdStjg�tjtj|�j�tjtj	|�j
�tj�dkrRt��tj
d||�Wn2tk
r�}ztjd|||�WYdd}~XnXdS)z0Drop privileges, defaults to daemon:daemon.
    rNz'Dropped privileges to user=%s, group=%sz'Failed to drop privileges to %s, %s: %s)�os�getuid�	setgroups�setgid�pwd�getpwnamZpw_uid�setuid�grpZgetgrnamZgr_gid�	Exception�logger�debugr
)Znew_usernameZ
new_groupname�e�r;�/usr/lib/python3.6/ipa_epn.py�drop_privilegesLs$

r=c@sZeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
ddd�Zdd�ZdS)�EPNUserLista"Maintains a list of users whose passwords are expiring.
       Provides add(), check(), pop(), and json_print().
       From the outside, the list is considered always sorted:
       * displaying the list results in a sorted JSON representation thereof
       * pop() returns the "most urgent" item from the list.
       Internal implementation notes:
       * Uses a deque instead of a list for efficiency reasons
       * all add()-style methods MUST set _sorted to False.
       * all print() and pop-like methods MUST call _sort() first.
    cCsd|_t�|_dS)NF)�_sortedr�_expiring_password_user_dq)�selfr;r;r<�__init__uszEPNUserList.__init__cCs
t|j�S)z)If it quacks like a container...
        )�boolr@)rAr;r;r<�__bool__yszEPNUserList.__bool__cCs
t|j�S)zReturn len(self).)�lenr@)rAr;r;r<�__len__~szEPNUserList.__len__cCst|j|dg�jd��S)z9Get a single value from a multi-valued attr in a safe way�r)�str�get�pop)rA�entry�attrr;r;r<�
get_ldap_attr�szEPNUserList.get_ldap_attrcCs�yxd|_|jd�dkr(tjd|j�dS|jjt|j|d�|j|d�|j|d�|j|d�|j|d	�t	|jd��d
��Wn.t
k
r�}ztjd|�WYdd}~XnXdS)z�Parses and appends an LDAP user entry with the uid, cn,
           givenname, sn, krbpasswordexpiration and mail attributes.
        F�mailNz(IPA-EPN: No mail address defined for: %s�uid�cn�	givenname�sn�krbpasswordexpiration)rOrPrQrRrSrNz"IPA-EPN: Could not parse entry: %s)r?rIr8r
Zdnr@�append�dictrMrH�
IndexError�info)rArKr:r;r;r<�add�s 




zEPNUserList.addcCs,|j�y
|jj�Stk
r&dSXdS)zPReturns the "most urgent" user to notify.
           In fact: popleft()
        FN)�_sortr@�popleftrV)rAr;r;r<rJ�s

zEPNUserList.popcCs|jdd�dS)NF)�really_print)�
json_print)rAr;r;r<�check�szEPNUserList.checkTcCsly8|j�tjt|j�ddd�}|jd�|r6t|�Wn.tk
rf}ztj	d|�WYdd}~XnXdS)z�Dump self._expiring_password_user_dq to JSON.
           Check that the result can be re-rencoded to UTF-8.
           If really_print, print the result.
        �F)�indentZensure_asciirzIPA-EPN: unexpected error: %sN)
rY�json�dumps�listr@�encode�printr7r8r
)rAr[Ztemp_strr:r;r;r<r\�s
zEPNUserList.json_printcCs4|js0t|jt�r0tt|jdd�d��|_d|_dS)NcSs|dS)NrSr;)�itemr;r;r<�<lambda>�sz#EPNUserList._sort.<locals>.<lambda>)�keyT)r?�
isinstancer@r�sorted)rAr;r;r<rY�szEPNUserList._sortN)T)
�__name__�
__module__�__qualname__�__doc__rBrDrFrMrXrJr]r\rYr;r;r;r<r>is


r>cs�eZdZdZejZdZdZ�fdd�Z	e
�fdd��Z�fdd	�Zd/�fdd�	Z
�fd
d�Zd0dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd1d'd(�Zd)d*�Zd+d,�Zd-d.�Z�ZS)2�EPNzIPA-EPNz%prog [options]z%Expiring Password Notifications (EPN)csHtt|�j||�d|_d|_t�|_g|_g|_d|_	d|_
d|_dS)N)�superrnrB�_conn�_ssl_contextr>�_expiring_password_user_list�
_ldap_data�_date_ranges�_mailer�env�default_email_domain)rA�options�args)�	__class__r;r<rB�szEPN.__init__cshtt|�j|dd�|jdddddd�|jdd	ddd
d�|jddd
ddd�|jddd
ddd�dS)NT)Zdebug_optionz
--from-nbdays�from_nbdaysZstorezminimal number of days)�dest�action�default�helpz--to-nbdays�	to_nbdayszmaximal number of daysz	--dry-run�dry_run�
store_trueFzDry run mode. JSON ouput only.z--mail-test�mailtestzSend a test e-mail)rorn�add_optionsZ
add_option)�cls�parser)rzr;r<r��s2zEPN.add_optionscsftt|�jdd�|jjdk	rzyt|jj�dkr8td��Wn6tk
rp}z|jj	dj
|d��WYdd}~XnXd|j_|jjdk	r�yt|jj�dkr�td��Wn6tk
r�}z|jj	dj
|d��WYdd}~XnX|jjdk	o�|jjdk	�rt|jj�t|jj�k�r|jj	d�|jjdk	�rB|jjdk�rB|jj	d	�|jj
�rb|jj�rb|jj	d
�dS)NT)Z
needs_rootrzInput is negative.z/--to-nbdays must be a positive integer. {error})r
z1--from-nbdays must be a positive integer. {error}z/--from-nbdays must be smaller than --to-nbdays.z4You cannot specify --from-nbdays without --to-nbdaysz5You cannot specify --mail-test and --dry-run together)rorn�validate_optionsrxr��int�RuntimeErrorr7Z
option_parserr
�formatr�r{r�)rAr:)rzr;r<r��s<zEPN.validate_options�acstt|�jdd�dS)Nr�)�
log_file_mode)rorn�
setup_logging)rAr�)rzr;r<r�!szEPN.setup_loggingcstt|�j�t�s&tjd�tj��|j�|j	�|j
�|j�|j�|j
�|j�t�|jjrv|j�n2|jjr�|j�x |jD]}|j|�|j�q�W|jjr�|j�nJttjjtjjtjjtjj tjj!tjj"|j#|j$tjj%tjj&d�
|_'|j(�dS)Nz,IPA client is not configured on this system.)
�security_protocol�
smtp_hostnamerr%�
smtp_usernamer!�ssl_context�x_mailerr,r+))rorn�runrr8r
r�ScriptError�_get_krb5_ticket�_read_configuration�_validate_configuration�_parse_configuration�_get_connection�_read_ipa_configuration�_create_ssl_contextr=rxr��_gentestdatar��_build_cli_date_rangesrt�_fetch_data_from_ldap�_parse_ldap_datar��_pretty_print_data�
MailUserAgentrrvr&rrr%r r!rq�command_namer,r+ru�_send_emails)rA�
date_range)rzr;r<r�$sB



zEPN.runNcCshtj�}tj|tjj��}|t|d�}|dk	r@|t|d�}n|tdd�}tjd||||�||fS)z�Detects current time and returns a date range, given a number
           of days in the future.
           If only nbdays_end is specified, the range is 1d long.
        )�daysN�z�IPA-EPN: Current date: %s 
IPA-EPN: Date & time, today at midnight: %s 
IPA-EPN: Date range start: %s 
IPA-EPN: Date range end: %s 
)r�utcnowZcombine�min�timerr8r9)rA�
nbdays_end�nbdays_start�nowZtoday_at_midnightZ	range_endZrange_startr;r;r<�_get_date_range_from_nbdaysNszEPN._get_date_range_from_nbdayscCsB|j�}t|j�djdd�|j|j|j|j|jfD��}|dS)zgConvert datetime to LDAP_GENERALIZED_TIME_FORMAT
           Note: Consider moving into ipalib.
        rGcss*|]"}ddtt|��t|�VqdS)�0�N)rErH)�.0rer;r;r<�	<genexpr>msz4EPN._datetime_to_generalized_time.<locals>.<genexpr>�Z)	Z	timetuplerHZtm_year�joinZtm_monZtm_mdayZtm_hourZtm_minZtm_sec)rAZdtZgeneralized_time_strr;r;r<�_datetime_to_generalized_timegsz!EPN._datetime_to_generalized_timecCstjjdd�dtjd<dS)z�Setup the environment to obtain a krb5 ticket for us using the
           system keytab.
           Uses CCACHE = MEMORY (limited to the current process).
        ZKRB5_CLIENT_KTNAMEz/etc/krb5.keytabzMEMORY:Z
KRB5CCNAMEN)r/�environ�
setdefault)rAr;r;r<r�xszEPN._get_krb5_ticketcCs@tdtjdd�}tjf|�tjjft�tjd�s<tj	�dS)z5Merge in the EPN configuration from /etc/ipa/epn.conf�epnF)�context�confdirZ	in_server�finalizeN)
rUrZETC_IPArZ	bootstraprvZ_merge�
EPN_CONFIGZisdoner�)rAZbase_configr;r;r<r��s
zEPN._read_configurationcCstjjj�dkrtd��tjjdk	r8tjjdkr8td��tjjdkrPtdt��y dd	�t	tjj�j
d
�D�Wn6tk
r�}ztdtjj|f��WYdd}~XnXtjj�ryt
tjj�Wn.tk
r�}ztd|��WYdd}~XnXt
tjj�d
k�rtd��dS)z1Examine the user-provided configuration.
        r�starttls�sslz3smtp_security must be one of: none, starttls or sslNz&smtp_user set and smtp_password is notznotify_ttls must be set in %scSsg|]}t|��qSr;)r�)r��kr;r;r<�
<listcomp>�sz/EPN._validate_configuration.<locals>.<listcomp>�,z%Failed to parse notify_ttls: '%s': %szsmtp_delay is misformatted: %srz#smtp_delay cannot be less than zero)rr�r�)rrvr&�lowerr�r r!r*�EPN_CONFrH�split�
ValueErrorr(�float)rAr:r;r;r<r��s& "
zEPN._validate_configurationcCsrdd�ttjj�jd�D�}|j�x&|D]}|jj|jd|dd��q*Wt	t
jjtjj
d��}t|d�|_dS)	z	
        cSsg|]}t|��qSr;)r�)r�r�r;r;r<r��sz,EPN._parse_configuration.<locals>.<listcomp>r�Nr�)r�r�r�)�loader)rHrrvr*r��sortrtrTr�rr/�pathr�r�r)rAZdaylistZdayr�r;r;r<r��s
zEPN._parse_configurationcCs>tjjj�tjj�d}|jddg�d|_tjjj�dS)zGet the IPA configuration�resultZipadefaultemaildomainNr)	rZBackendZ	rpcclientZconnectZCommandZconfig_showrIrwZ
disconnect)rAr�r;r;r<r��s
zEPN._read_ipa_configurationcCs�|jdk	r|jSy tjjtjj�|_|jj�Wnjtk
r�y tjj	tjj
�|_|jj�Wn4tk
r�}ztj
d|jj|�WYdd}~XnXYnX|jS)z4Create a connection to LDAP and bind to it.
        Nz$Unable to bind to LDAP server %s: %s)rprZ
LDAPClientZ
from_realmrrvZrealmZ
external_bindr7Zfrom_hostname_secureZserverZgssapi_bindr8r
Zldap_uri)rAr:r;r;r<r��s 
zEPN._get_connectioncCsHtjjj�dkrDtj�|_tjjrD|jjtjjtjj	t
tjj�d�dS)z�Create SSL context.
           This must be done before the dropping priviliges to allow
           read in the smtp client's certificate and private key if specified.
        r�r�)ZcertfileZkeyfileZpasswordN)r�r�)rrvr&r�r�Zcreate_default_contextrqr"Zload_cert_chainr#rHr$)rAr;r;r<r��s
zEPN._create_ssl_contextcCs�|jdkrtjd�ttjjtjj�}ddddddg}d	|j|d
�|j|d�f}zDy|jj	||||jj
d�|_Wn tj
k
r�tjd
�YnXWdtjdt|j��XdS)z�Run a LDAP query to fetch a list of user entries whose passwords
           would expire in the near future. Store in self._ldap_data.
        Nz5IPA-EPN: Connection to LDAP not established. Exiting.rOrSrNrPrQZsurnamezj(&(!(nsaccountlock=TRUE))             (krbpasswordexpiration<=%s)             (krbpasswordexpiration>=%s))r�r)�filter�
attrs_listZscopez
Empty Result.z%d entries found)rpr8r
rrrvZcontainer_userZbasednr�Zget_entriesZ
SCOPE_SUBTREErsrZEmptyResultr9rE)rAr�Zsearch_baser�Z
search_filterr;r;r<r��s$
zEPN._fetch_data_from_ldapcCst|jrpx|jD]}|jj|�qWzDy|jdd�Wn.tk
rb}ztjd|�WYdd}~XnXWdg|_XdS)zHFill out self._expiring_password_user_list from data from ldap.
        F)r[z"IPA-EPN: Could not create JSON: %sN)rsrrrXr�r7r8r
)rArKr:r;r;r<r�s"zEPN._parse_ldap_dataTcCs|jj|d�dS)z8Dump self._expiring_password_user_list to JSON.
        )r[N)rrr\)rAr[r;r;r<r�szEPN._pretty_print_datacCs@|jdkrtjd�dSy|jjd�}Wn4tk
r\}ztd|j|f��WYdd}~XnXtjj	rptjj	}n
d|j
}x�|j�r0|jj�}|j
|d|d|d|d|d	d
�}|jjtjj|tj|d�|d�tj�}tj|d	d
�}tjd|d|d||j|�tjjr|tjttjj�d�q|W|jj�dS)Nz#IPA-EPN: mailer was not configured.zexpire_msg.templatezParsing template %s failed: %sz
noreply@%srOrQrRrPrS)rO�firstZlast�fullnameZ
expirationrN)�mail_subject�	mail_body�subscribersr)z%Y-%m-%d %H:%M:%Sz5Notified %s (%s). Password expiring in %d days at %s.i�)rur8r
rvZget_templaterr��filenamerr)rwrrrJZrender�send_messager-�astZliteral_evalrr�Zstrptimer9r�r(r�Zsleepr��cleanup)rA�templater:r)rKZbodyr��expdater;r;r<r�sF

 



zEPN._send_emailscCsBtj�jd�}tdgdgdgdg|gtjjgd�}|jj|�dS)z@Generate a sample user to process through the template.
        z%Y-%m-%d %H:%M:%SZSAUSERzSAMPLE USERZSAMPLEZUSER)rOrPrQrRrSrNN)	rr�ZstrftimerUrrvr'rrrX)rAr�rKr;r;r<r�EszEPN._gentestdatacCsrg|_tjd�|jjdk	rD|jj|jt|jj�t|jj�d��n*|jjdk	rn|jj|jdt|jj�d��dS)z�When self.options.to_nbdays is set, override the date ranges read
           from the configuration file and build the date ranges from the CLI
           options.
        z,IPA-EPN: Ignoring configuration file ranges.N)r�r�)	rtr8r9rxr{rTr�r�r�)rAr;r;r<r�Ss

zEPN._build_cli_date_ranges)r�)N)T)rjrkrlr�rZ
IPAEPN_LOGZ
log_file_nameZusage�descriptionrB�classmethodr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��
__classcell__r;r;)rzr<rn�s0 %*
$
)rnc@s<eZdZdZddd�Zd	d
�Zddd�Zd
d�Zdd�ZdS)�	MTAClientz/MTA Client class. Originally done for EPN.
    rrrrNcCsZ||_||_||_||_||_||_||_d|_|jdkrNd|jkrNtj	d�|j
�dS)NrrzBIPA-EPN: using cleartext for non-localhost SMTPd is not supported.)�_security_protocol�_smtp_hostname�
_smtp_port�
_smtp_timeout�	_username�	_passwordrqrpr8r
�_connect)rAr�r�rr%r�r!r�r;r;r<rBms


zMTAClient.__init__cCs|j�dS)N)�_disconnect)rAr;r;r<r��szMTAClient.cleanupcCs�d}zLy|jjtjj||�}Wn.tk
rL}ztjd|�WYdd}~XnXWd|r�x,|D]$}tjd|||d||d�q\Wtjd�XdS)Nz IPA-EPN: Failed to send mail: %sz+IPA-EPN: Failed to send mail to '%s': %s %srr�z6IPA-EPN: Failed to send mail to at least one recipient)rpZsendmailrrvr'r7r8rW)rA�message_strr�r�r:rgr;r;r<r��s"

zMTAClient.send_messagec;CsyH|jj�dkr*tj|j|j|jd�|_ntj|j|j|j|j	d�|_WnFt
tjfk
r�}z$dj|j|j|d�}t
j|��WYdd}~XnXy|jj�Wn8tjk
r�}ztjd|j|j|�WYdd}~XnX|jj�dk�rDy|jj|j	d�|jj�Wn<tjk
�rB}ztd	|j|j|f��WYdd}~XnX|j�r|j�r|jjd
��r�y,|jj|j|j�|jdk�r�tjd�Wnftjk
�r�td|j|jf��Yn<tjk
�r�}ztd
|j|j|f��WYdd}~XnXnd|j|jf}tj|�dS)Nrr�)�host�port�timeout)r�r�r�r�zPIPA-EPN: Could not connect to the configured SMTP server: {host}:{port}: {error})r�r�r
z'IPA-EPN: EHLO failed for host %s:%s: %s)r�z;IPA-EPN: Unable to create an encrypted session to %s:%s: %sZAUTHz6IPA-EPN: Username and Password were sent in the clear.zTIPA-EPN: Authentication to %s:%s failed, please check your username and/or password:zIPA-EPN: SMTP Error at %s:%s:%sz9IPA-EPN: Server at %s:%s does not support authentication.)rr�)r�r��smtplibZSMTPr�r�r�rpZSMTP_SSLrq�socketerrorZ
SMTPExceptionr�rr�Zehlor8r
r�r�r�r�Zhas_extnZloginZwarningZSMTPAuthenticationError)rAr:�msgZerr_strr;r;r<r��sn$

zMTAClient._connectcCs|jj�dS)N)rp�quit)rAr;r;r<r��szMTAClient._disconnect)rrrrNNN)NN)	rjrkrlrmrBr�r�r�r�r;r;r;r<r�is

Mr�c
@s4eZdZdZdd	d
�Zdd�Zdd
d�Zdd�ZdS)r�zThe MUA class for EPN.
    rrrrNrrc	CsL||_d|_d|_d|_|	|_|
|_d|_d|_t|||||||d�|_	dS)N)r�r�rr%r�r!r�)
�	_x_mailer�_subject�_body�_subscribers�_subtype�_charset�_msg�_message_strr��_mta_client)rAr�r�rr%r�r!r�r�r,r+r;r;r<rB�s zMailUserAgent.__init__cCs|jj�dS)N)r�r�)rAr;r;r<r�szMailUserAgent.cleanupcCsFd||||gkrtjd�dS|j||||d�|jj|j|d�dS)zfGiven mail_subject, mail_body, and subscribers, composes
           the message and sends it.
        Nz(IPA-EPN: Tried to send an empty message.F)r�r�r�r))r�r�T)r8r
�_compose_messager�r�r�)rAr�r�r�r)r;r;r<r� s
zMailUserAgent.send_messagecCs�||_||_||_t|jd�|_td|f�|jd<dj|j�|jd<tdd�|jd<t	|j|j�|jd	<t
�|jd
<d|j_d|jkr�|jr�|jj
d|j�|jjt|jd
|j|jd��|jj�|_dS)z7The composer creates a MIME multipart message.
        )r�zIPA-EPNZFromz, ZToT)Z	localtimeZDateZSubjectz
Message-IdzMultipart messagezX-Mailerz

)r�r�N)r�r�r�r	r�r�rr�rrrZpreambler�Z
add_headerZattachr
r�Z	as_stringr�)rAr�r�r�r)r;r;r<r�5s$zMailUserAgent._compose_message)
rrrrNNNNrr)NNNN)rjrkrlrmrBr�r�r�r;r;r;r<r��s

r�)r.r.)9rmZ
__future__rrr�r6r`r/r3Zloggingr�r�r��collectionsrrrZemail.utilsrrZemail.mime.multipartr	Zemail.mime.textr
Zemail.headerrrZsocketr
r�Zipaplatform.pathsrZipalibrrZipalib.factsrZ	ipapythonrrZipapython.dnrZjinja2rrrr�r�Z	getLoggerrjr8r=r>Z	AdminToolrnr�r�r;r;r;r<�<module>sf

a"