HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/beacons/__pycache__/wtmp.cpython-310.pyc
o

�N�g')�@s�dZddlZddlZddlZddlZddlZddlZddlZdZ	dZ
dZgd�Ze�
e�ZdZdZd	Zd
Ze�e�Zz
ddlmZdZWneyOdZYnwd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Z dd�Z!dS)a�
Beacon to fire events at login of users as registered in the wtmp file

.. versionadded:: 2015.5.0


Example Configuration
=====================

.. code-block:: yaml

    # Fire events on all logins
    beacons:
      wtmp: []

    # Matching on user name, using a default time range
    beacons:
      wtmp:
        - users:
            gareth:
        - defaults:
            time_range:
                start: '8am'
                end: '4pm'

    # Matching on user name, overriding the default time range
    beacons:
      wtmp:
        - users:
            gareth:
                time_range:
                    start: '7am'
                    end: '3pm'
        - defaults:
            time_range:
                start: '8am'
                end: '4pm'

    # Matching on group name, overriding the default time range
    beacons:
      wtmp:
        - groups:
            users:
                time_range:
                    start: '7am'
                    end: '3pm'
        - defaults:
            time_range:
                start: '8am'
                end: '4pm'


How to Tell What An Event Means
===============================

In the events that this beacon fires, a type of ``7`` denotes a login, while a
type of ``8`` denotes a logout. These values correspond to the ``ut_type``
value from a wtmp/utmp event (see the ``wtmp`` manpage for more information).
In the extremely unlikely case that your platform uses different values, they
can be overridden using a ``ut_type`` key in the beacon configuration:

.. code-block:: yaml

    beacons:
      wtmp:
        - ut_type:
            login: 9
            logout: 10

This beacon's events include an ``action`` key which will be either ``login``
or ``logout`` depending on the event type.

.. versionchanged:: 2019.2.0
    ``action`` key added to beacon event, and ``ut_type`` config parameter
    added.


Use Case: Posting Login/Logout Events to Slack
==============================================

This can be done using the following reactor SLS:

.. code-block:: jinja

    report-wtmp:
      runner.salt.cmd:
        - args:
          - fun: slack.post_message
          - channel: mychannel      # Slack channel
          - from_name: someuser     # Slack user
          - message: "{{ data.get('action', 'Unknown event') | capitalize }} from `{{ data.get('user', '') or 'unknown user' }}` on `{{ data['id'] }}`"

Match the event like so in the master config file:

.. code-block:: yaml

    reactor:

      - 'salt/beacon/*/wtmp/':
        - salt://reactor/wtmp.sls

.. note::
    This approach uses the :py:mod:`slack execution module
    <salt.modules.slack_notify>` directly on the master, and therefore requires
    that the master has a slack API key in its configuration:

    .. code-block:: yaml

        slack:
          api_key: xoxb-XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX

    See the :py:mod:`slack execution module <salt.modules.slack_notify>`
    documentation for more information. While you can use an individual user's
    API key to post to Slack, a bot user is likely better suited for this. The
    :py:mod:`slack engine <salt.engines.slack>` documentation has information
    on how to set up a bot user.
�NZwtmpz
/var/log/wtmpshi32s4s32s256shhiii4i20x)
�typeZPID�lineZinittab�user�hostname�exit_status�session�time�addrzwtmp.locz	wtmp.tty.��TFcCs0tj�t�rtSt�d�}t�dt|�d|fS)Nz does not exist.zUnable to load %s beacon: %sF)�os�path�isfile�WTMP�__virtualname__�log�error)�err_msg�r�E/opt/saltstack/salt/lib/python3.10/site-packages/salt/beacons/wtmp.py�__virtual__�s

rcsD�s||fSt�t�sd}d}t�fdd�dD��sd}d}||fS)�
    Check time range
    Fz>The time_range parameter for wtmp beacon must be a dictionary.c3s�|]}|�vVqdS)Nr)�.0�k��trangerr�	<genexpr>�s�z'_validate_time_range.<locals>.<genexpr>)�start�endzJThe time_range parameter for wtmp beacon must contain start & end options.)�
isinstance�dict�all)r�status�msgrrr�_validate_time_range�s
�r$cCsFtd|�}|st�d|�dS|dD]}||vr ||||<qdS)z
    Gather group members
    z
group.infoz"Group %s does not exist, ignoring.N�members)Z__salt__r�warning)�group�groups�users�_group�memberrrr�_gather_group_members�s��r,cCsLtrt�|d�}t�|d�}t||ko|k�S�St�d�dS)rrrzDateutil is required.F)�_TIME_SUPPORTED�dateutil_parser�parse�boolrr)�
time_range�nowZ_startZ_endrrr�_check_time_range�s
r3cCsttvrttSdS)z)
    return the active file location
    N)�LOC_KEY�__context__rrrr�_get_loc�s�r6cCs@d}d}t|t�sd}d}||fStjj�|�}d|vrGt|dt�s(d}d}n|dD]}|d|�di�}t|||�\}}q,|sG||fSd|vrvt|dt�sWd}d	}n|dD]}|d|�di�}t|||�\}}q[|sv||fSd
|vr�t|d
t�s�d}d}n|d
�di�}t|||�\}}|s�||fS||fS)z+
    Validate the beacon configuration
    TzValid beacon configurationFz-Configuration for wtmp beacon must be a list.r)z8User configuration for wtmp beacon must be a dictionary.r1r(z9Group configuration for wtmp beacon must be a dictionary.�defaultsz<Defaults configuration for wtmp beacon must be a dictionary.)	r�list�salt�utilsZbeaconsZlist_to_dictr �getr$)�configZvstatusZvmsgrZ_time_ranger'rrr�validate�sD
%�r=c	Csg}i}i}d}t}t}|D]B}d|vr|d}d|vr |d}d|vr(|d}|dkrPz|dd}Wn	ty=Ynwz|dd}WqtyOYqwqtjj�td���)}t�	t
d	�}	|	d	krz|�d	d
�|��tt
<|Wd�S|�|	�	t
j
��}
|�t�}t|�tkr�|Wd�S|��tt
<t�t|�}i}
tt�D].\}}|||
|<t|
|ttf�r�t|
|t�r�tjj�|
|�|
|<|
|�d�|
|<q�|
d
|kr�d|
d<|
dtd�t|
d�<n%|
d
|k�rd|
d<zt�d�t|
d��|
d<Wn
t�yYnw|D]	}t |||��q|�rg|
d|v�rf||
d}t|t!��rKd|v�rKt"|d|
��rJ|�#|
�n7|�rad|v�rat"|d|
��r`|�#|
�n!|�#|
�n|�r}d|v�r}t"|d|
��r||�#|
�n|�#|
�q�1�s�wY|S)zF
    Read the last wtmp file and return information on the logins
    Nr)r(r7Zut_typeZloginZlogout�rbr�T�r�actionrz{}{}rr1)$�
LOGIN_TYPE�LOGOUT_TYPE�KeyErrorr9r:�filesZfopenrr5r;r4�seek�tell�datetimer2�read�SIZE�len�struct�unpack�FMT�	enumerate�FIELDSr�str�bytesZstringutilsZ
to_unicode�strip�format�TTY_KEY_PREFIX�popr,r r3�append)r<�retr)r(r7Z
login_typeZlogout_typeZconfig_itemZfp_�locr2�raw�pack�eventZind�fieldr'�_userrrr�beacons����
�


�
����
�
�
�
�
��;r_)"�__doc__rH�loggingrrLZsalt.utils.beaconsr9Zsalt.utils.filesZsalt.utils.stringutilsrrrNrP�calcsizerJr4rUrBrC�	getLogger�__name__rZdateutil.parser�parserr.r-�ImportErrorrr$r,r3r6r=r_rrrr�<module>s>v

�2