
    Vh7                     z    d dl mZmZmZ eZdZdZd dlm	Z	m
Z
 d dlmZmZ  G d de      Zd Zed	k(  r e        y
y
)    )absolute_importdivisionprint_functiona  
module: krb_ticket
short_description: Kerberos utils for managing tickets
version_added: 10.0.0
description:
  - Manage Kerberos tickets with C(kinit), C(klist) and C(kdestroy) base utilities.
  - See U(https://web.mit.edu/kerberos/krb5-1.12/doc/user/user_commands/index.html) for reference.
author: "Alexander Bakanovskii (@abakanovskii)"
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  password:
    description:
      - Principal password.
      - It is required to specify O(password) or O(keytab_path).
    type: str
  principal:
    description:
      - The principal name.
      - If not set, the user running this module will be used.
    type: str
  state:
    description:
      - The state of the Kerberos ticket.
      - V(present) is equivalent of C(kinit) command.
      - V(absent) is equivalent of C(kdestroy) command.
    type: str
    default: present
    choices: ["present", "absent"]
  kdestroy_all:
    description:
      - When O(state=absent) destroys all credential caches in collection.
      - Equivalent of running C(kdestroy -A).
    type: bool
  cache_name:
    description:
      - Use O(cache_name) as the ticket cache name and location.
      - If this option is not used, the default cache name and location are used.
      - The default credentials cache may vary between systems.
      - If not set the the value of E(KRB5CCNAME) environment variable will be used instead, its value is used to name the
        default ticket cache.
    type: str
  lifetime:
    description:
      - Requests a ticket with the lifetime, if the O(lifetime) is not specified, the default ticket lifetime is used.
      - Specifying a ticket lifetime longer than the maximum ticket lifetime (configured by each site) will not override the
        configured maximum ticket lifetime.
      - 'The value for O(lifetime) must be followed by one of the following suffixes: V(s) - seconds, V(m) - minutes, V(h)
        - hours, V(d) - days.'
      - You cannot mix units; a value of V(3h30m) will result in an error.
      - See U(https://web.mit.edu/kerberos/krb5-1.12/doc/basic/date_format.html) for reference.
    type: str
  start_time:
    description:
      - Requests a postdated ticket.
      - Postdated tickets are issued with the invalid flag set, and need to be resubmitted to the KDC for validation before
        use.
      - O(start_time) specifies the duration of the delay before the ticket can become valid.
      - You can use absolute time formats, for example V(July 27, 2012 at 20:30) you would neet to set O(start_time=20120727203000).
      - You can also use time duration format similar to O(lifetime) or O(renewable).
      - See U(https://web.mit.edu/kerberos/krb5-1.12/doc/basic/date_format.html) for reference.
    type: str
  renewable:
    description:
      - Requests renewable tickets, with a total lifetime equal to O(renewable).
      - 'The value for O(renewable) must be followed by one of the following delimiters: V(s) - seconds, V(m) - minutes, V(h)
        - hours, V(d) - days.'
      - You cannot mix units; a value of V(3h30m) will result in an error.
      - See U(https://web.mit.edu/kerberos/krb5-1.12/doc/basic/date_format.html) for reference.
    type: str
  forwardable:
    description:
      - Request forwardable or non-forwardable tickets.
    type: bool
  proxiable:
    description:
      - Request proxiable or non-proxiable tickets.
    type: bool
  address_restricted:
    description:
      - Request tickets restricted to the host's local address or non-restricted.
    type: bool
  anonymous:
    description:
      - Requests anonymous processing.
    type: bool
  canonicalization:
    description:
      - Requests canonicalization of the principal name, and allows the KDC to reply with a different client principal from
        the one requested.
    type: bool
  enterprise:
    description:
      - Treats the principal name as an enterprise name (implies the O(canonicalization) option).
    type: bool
  renewal:
    description:
      - Requests renewal of the ticket-granting ticket.
      - Note that an expired ticket cannot be renewed, even if the ticket is still within its renewable life.
    type: bool
  validate:
    description:
      - Requests that the ticket-granting ticket in the cache (with the invalid flag set) be passed to the KDC for validation.
      - If the ticket is within its requested time range, the cache is replaced with the validated ticket.
    type: bool
  keytab:
    description:
      - Requests a ticket, obtained from a key in the local host's keytab.
      - If O(keytab_path) is not specified will try to use default client keytab path (C(-i) option).
    type: bool
  keytab_path:
    description:
      - Use when O(keytab=true) to specify path to a keytab file.
      - It is required to specify O(password) or O(keytab_path).
    type: path
requirements:
  - krb5-user and krb5-config packages
extends_documentation_fragment:
  - community.general.attributes
a  
- name: Get Kerberos ticket using default principal
  community.general.krb_ticket:
    password: some_password

- name: Get Kerberos ticket using keytab
  community.general.krb_ticket:
    keytab: true
    keytab_path: /etc/ipa/file.keytab

- name: Get Kerberos ticket with a lifetime of 7 days
  community.general.krb_ticket:
    password: some_password
    lifetime: 7d

- name: Get Kerberos ticket with a starting time of July 2, 2024, 1:35:30 p.m.
  community.general.krb_ticket:
    password: some_password
    start_time: "240702133530"

- name: Get Kerberos ticket using principal name
  community.general.krb_ticket:
    password: some_password
    principal: admin

- name: Get Kerberos ticket using principal with realm
  community.general.krb_ticket:
    password: some_password
    principal: admin@IPA.TEST

- name: Check for existence by ticket cache
  community.general.krb_ticket:
    cache_name: KEYRING:persistent:0:0

- name: Make sure default ticket is destroyed
  community.general.krb_ticket:
    state: absent

- name: Make sure specific ticket destroyed by principal
  community.general.krb_ticket:
    state: absent
    principal: admin@IPA.TEST

- name: Make sure specific ticket destroyed by cache_name
  community.general.krb_ticket:
    state: absent
    cache_name: KEYRING:persistent:0:0

- name: Make sure all tickets are destroyed
  community.general.krb_ticket:
    state: absent
    kdestroy_all: true
)AnsibleModuleenv_fallback)	CmdRunnercmd_runner_fmtc                   *    e Zd Zd Zd Zd Zd Zd Zy)	IPAKeytabc                    || _         |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d	   | _	        |d
   | _
        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        t#        |dt%        t'        j(                  d      t'        j(                  d      t'        j(                  d      t'        j*                  ddd      t'        j*                  ddd      t'        j*                  ddd      t'        j*                  d      t'        j*                  d      t'        j*                  d      t'        j*                  d       t'        j*                  d!      t'        j*                  d"      t'        j,                  d#       t'        j(                  d$      t'        j.                         %      &      | _        t#        |d't%        t'        j*                  d      t'        j(                  d$      t'        j(                  d      (      &      | _        t#        |d)t%        t'        j*                  d      *      &      | _        y )+Npassword	principalstatekdestroy_all
cache_name
start_time	renewableforwardable	proxiableaddress_restrictedcanonicalization
enterpriserenewalvalidatekeytabkeytab_pathkinitz-lz-sz-rz-fz-FT)ignore_nonez-pz-Pz-az-Az-nz-Cz-Ez-Rz-vz-kc                     | rd| gS dgS )Nz-tz-i )vs    p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/krb_ticket.py<lambda>z$IPAKeytab.__init__.<locals>.<lambda>   s    !dAY RVQW     z-c)lifetimer   r   r   r   r   	anonymousr   r   r   r   r   r   r   r   )commandarg_formatskdestroy)r   r   r   klist	show_list)moduler   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   dictr	   
as_opt_valas_boolas_funcas_listr   r)   r*   )selfr-   kwargss      r"   __init__zIPAKeytab.__init__   s7   z*,G_
">2 . .,!-0,"()="> &'9 : .i(z*X&!-0'2248)44T:(33D9*2244P(00tN#1#9#9$RV#W(006!/!7!7!=)11$7&..t4'//5%--d3*223WX)44T:(002

, "+33D9)44T:(33D9
 (006

r$   c                     t        | j                  j                        }| j                  dd| j                        5 } |j
                  di |\  }}}d d d        |S # 1 sw Y   S xY w)Nzlifetime start_time renewable forwardable proxiable address_restricted anonymous canonicalization enterprise renewal validate keytab keytab_path cache_name principalT)check_rcdatar    )r.   r-   paramsr   r   runr3   r9   ctxrcouterrs         r"   
exec_kinitzIPAKeytab.exec_kinit   ss    dkk(()ZZc	  
 	-
 "377,V,LBS	- 
	- 
s   AA)c                     t        | j                  j                        }| j                  dd      5 } |j                  di |\  }}}d d d        |S # 1 sw Y   S xY w)Nz!kdestroy_all cache_name principalTr7   r    )r.   r-   r9   r)   r:   r;   s         r"   exec_kdestroyzIPAKeytab.exec_kdestroy  si    dkk(()]]/  
 	- "377,V,LBS		-
 
	-
 
s   AAc                     t        |      }| j                  dd      5 } |j                  di |\  }}}d d d        fS # 1 sw Y   xY w)Nr+   r,   FrB   r    )r.   r*   r:   )r3   r,   r9   r<   r=   r>   r?   s          r"   
exec_klistzIPAKeytab.exec_klist  sf     	*ZZ  
 	- "377,V,LBS		-
 3|	- 	-s   AAc                 <   d}d}| j                   s*| j                  s| j                  |      \  }}}|dk7  rd}|S d}| j                  |      \  }}}| j                   r| j                   t        |      vrd}| j                  r| j                  t        |      vrd}|S )NTFr   )r   r   rE   str)r3   ticket_presentr,   r=   r>   r?   s         r"   check_ticket_presentzIPAKeytab.check_ticket_present  s    	~~doo??95LBSQw!&  I??95LBS~~$..C"@!&4??#c(#B!&r$   N)__name__
__module____qualname__r5   r@   rC   rE   rI   r    r$   r"   r   r      s    9
v		r$   r   c            
         t        d$i dt        d      dt        dd      dt        ddd	g
      dt        d      dt        dt        dgf      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      } t        | dddidg      }|j                  d   }|j                  d   }t	        |fi d|d|d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   }|j                  d   #|j                  d   dur|j                  d !       d"}|dk(  r.|j                         sd}|j                  s|j                          |d	k(  rO|rd}|j                  s?|j                          n.|j                         rd}|j                  s|j                          |j                  |#       y )%Nr   rG   )typer   T)rN   no_logr   presentabsent)defaultchoicesr   boolr   
KRB5CCNAME)rN   fallbackr%   r   r   r   r   r   r&   r   r   r   r   r   r   path)r   rP   )r   r   T)argument_specsupports_check_moderequired_byrequired_ifz>If keytab_path is specified then keytab parameter must be True)msgF)changedr    )r.   r   r   r9   r   	fail_jsonrI   
check_moder@   rC   	exit_json)arg_specr-   r   r   r   r]   s         r"   mainrb   /  s~    E"5. 9y(.CD v&	
 Ul\N-KL 5! U# E" f% F#  V, F# 6* V$ &!  6"!"  #$ f%%H(  8
 D
	F MM'"E==0Lv "$0 "({!; !'j 9	
 #)--"= !'j 9 #)--"= "({!; $*==#? "({!; +1--8L*M "({!; )/6H(I #)--"=  &}}Y7  !'j 9!" %mmH5#$ $*==#?%F* }}]#/FMM(4KSW4W]^G	**,G$$!!#G$$$$&((*G$$$$&
W%r$   __main__N)
__future__r   r   r   rN   __metaclass__DOCUMENTATIONEXAMPLESansible.module_utils.basicr   r   Eansible_collections.community.general.plugins.module_utils.cmd_runnerr   r	   objectr   rb   rJ   r    r$   r"   <module>rk      sW    A @zx4l C kk k\L&^ zF r$   