
    Eg4                         d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZm	Z	 d dl
Z
d dlZd dlmZ  G d dej                        Zy)    N)GioGObjectGLibCScreensaver)DEBUGc                       e Zd ZdZej
                  j                  ddfej
                  j                  ddfej
                  j                  ddfej
                  j                  deffej
                  j                  deffej
                  j                  deffdZ	 fdZ
d Zd Zd Zd	 Zdd
Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )
AuthClientzc
    The AuthClient manages spawning of our pam helper process, and
    communication with it.
    N )auth-successauth-failureauth-cancel	auth-busyauth-prompt	auth-infoc                 J    t         t        |           | j                          y N)superr	   __init__reset)self	__class__s    7/usr/share/cinnamon-screensaver/pamhelper/authClient.pyr   zAuthClient.__init__   s    j$(*

    c                    | j                   ryt        d       t        j                         | _        	 d }t
        j                  j                  t        j                  d      }t
        j                  j                  |      r|}|t        d       t        j                  r^|dd f}t        j                  j                  |t        j                   j"                  t        j                   j$                  z        | _        nw|d f}t        j                  j                  |t        j                   j"                  t        j                   j$                  z  t        j                   j(                  z        | _        | j&                  j1                  | j                  | j2                  d        | j&                  j5                         | _        | j6                  j9                  dt*        j:                  | j                  | j<                         | j&                  j?                         | _         d| _         t        d	| j&                  jC                         z         y# t*        j,                  $ r"}t        d|j.                  z         Y d }~yd }~ww xY w)
NTz$authClient: attempting to initializezcinnamon-screensaver-pam-helperz:authClient: critical Error: PAM Helper could not be found!z--debugz>authClient: error starting cinnamon-screensaver-pam-helper: %sF   z'authClient: initialized (helper pid %s))"initializedr   r   Cancellablecancellableospathjoinconfig	pkglibdirexistsprintstatusDebug
SubprocessnewSubprocessFlags
STDIN_PIPESTDOUT_PIPEprocSTDERR_SILENCEr   Errormessagewait_check_asyncon_proc_completedget_stdout_pipeout_piperead_bytes_asyncPRIORITY_DEFAULTmessage_from_childget_stdin_pipein_pipeget_identifier)r   helper_path	full_pathargves        r   
initializezAuthClient.initialize!   s   45??,	KV%5%57XYIww~~i('"ST||#Y5NN..t/2/B/B/M/M/2/B/B/N/N0OP	 $T*NN..t/2/B/B/M/M/2/B/B/N/N0O/2/B/B/Q/Q0RS	 			""4#3#3T5K5KTR		113&&tT-B-BDDTDTVZVmVmnyy//17$)):R:R:UUV zz 	RUVU^U^^_	s   EI J&JJc                 J    d| _         d | _        d | _        d | _        d | _        y )NF)r   r   r-   r9   r4   r   s    r   r   zAuthClient.resetP   s'     	r   c                 $    | j                          y r   )end_procrA   s    r   cancelzAuthClient.cancelW   s    r   c                 
   | j                   y | j                   j                          | j                  5t        d       | j                  j	                  t
        j                         nt        d       | j                          y )Nz-authClient: cancel requested, killing helper.z3authClient: cancel requested, but no helper process)r   rD   r-   r   send_signalsignalSIGTERMr   rA   s    r   rC   zAuthClient.end_procZ   s^    #!99 ABII!!&..1GH

r   c                 .   t        d|j                         z         	 |j                  |      }|j                         }|"|j                          	 |j                  d        |j!                         }|"|j                          	 |j                  d        | j"                  |k(  r| j%                          y y # t        j                  $ rY}t
        j                  r?|j                  t        j                  j                  k7  rt        d|j                  z         Y d }~d }~ww xY w# t        j                  $ r"}t        d|j                  z         Y d }~d }~ww xY w# t        j                  $ r"}t        d|j                  z         Y d }~d }~ww xY w)Nz0authClient: helper process (pid %s) completed...z'helper process did not exit cleanly: %sz0helper process did not close in_pipe cleanly: %sz1helper process did not close out_pipe cleanly: %s)r   r:   wait_check_finishr   r/   r&   r'   coder   IOErrorEnum	CANCELLEDr%   r0   r8   clear_pendingcloser3   r-   r   )r   r-   resdataretr>   pipes          r   r2   zAuthClient.on_proc_completedg   sR   @4CVCVCXXY	M((-C
 ""$ V

4  ##% W

4 
 99JJL / zz 	M||#//*C*C C?!))KL	M :: VH199TUUV :: WIAIIUVVWsH   B8 D' E 8D$ADD$'E:EEF2FFc                    | j                   sy | j                  | j                  j                         ry t        d       	 t        j
                  j                  |j                               }	 | j                  j                  |      }| j                  j!                  d        y # t        j                  $ r<}|j                  t        j                  j                  k7  rt        d       Y d }~y d }~ww xY w# t        j                  $ rN}|j                  t"        j                  j                  k7  rt        d|j$                  z         Y d }~y Y d }~y d }~ww xY w)NzauthClient: message to childz%Error reading message from pam helperzError writing to pam helper: %s)r   r   is_cancelledr   r   Bytesr)   encoder9   write_bytesr/   rK   r   rL   rM   r%   flushGibr0   )r   stringbsr>   s        r   message_to_childzAuthClient.message_to_child   s   #t'7'7'D'D'F,-	E

v}}/ALL,,Q/ LLt$ :: 66S__666AB zz 	Evv2227!))CDD 3	EsB   -C8 /B& 
C8 &C592C0+C8 0C55C8 8E?EEc                    | j                   | j                   j                         ry d}	 |j                  |      }|r&|j                         j                         }|j                  d      }|D ]  }t        d|z         |sd|v r| j                          d|v r| j!                          d}d|v r| j#                          d}d	|v r| j%                  d       d
|v r| j%                  d       d|v r6t'        j(                  d|      j+                  d      }	| j-                  |	       d|v st'        j(                  d|      j+                  d      }
| j/                  |
        |r| j1                          y |j3                  dt        j4                  d | j6                         y # t        j                  $ rI}|j
                  t        j                  j                  k7  rt        d|j                  z         Y d }~y d }~ww xY w)NFz)Error reading message from pam helper: %s
zOutput from pam helper: '%s'CS_PAM_AUTH_FAILURECS_PAM_AUTH_SUCCESSTCS_PAM_AUTH_CANCELLEDCS_PAM_AUTH_BUSY_TRUECS_PAM_AUTH_BUSY_FALSECS_PAM_AUTH_SET_PROMPTz%(?<=CS_PAM_AUTH_SET_PROMPT_)(.*)(?=_)r   CS_PAM_AUTH_SET_INFOz#(?<=CS_PAM_AUTH_SET_INFO_)(.*)(?=_)r   )r   rU   read_bytes_finishr   r/   rK   r   rL   rM   r%   r0   get_datadecodesplitr   emit_idle_failureemit_idle_successemit_idle_cancelemit_idle_busy_stateresearchgroupemit_idle_auth_promptemit_auth_inforC   r5   r6   r7   )r   rS   rP   	terminate
bytes_readr>   
raw_stringlinesoutputpromptinfos              r   r7   zAuthClient.message_from_child   s   #t'7'7'D'D'F		//4J #,,.557J$$T*E 24v=>,6..0,6..0$(	.&8--/$(	.&811$7/6911%8/69!#+RTZ![!a!abc!d226:-7!yy)NPVW]]^_`++D1)2, MMOdD$9$94AXAXYE zz 	vv222AAIIMN	s   F% %H8?G<<Hc                 \    t        d       t        j                  | j                  d|       y )NzauthClient: idle add auth-busyr   r   r   idle_addemit)r   busys     r   ro   zAuthClient.emit_idle_busy_state   s!    ./K6r   c                 Z    t        d       t        j                  | j                  d       y )NzauthClient: idle add failurer   r}   rA   s    r   rl   zAuthClient.emit_idle_failure       ,-N3r   c                 Z    t        d       t        j                  | j                  d       y )NzauthClient: idle add successr   r}   rA   s    r   rm   zAuthClient.emit_idle_success   r   r   c                 Z    t        d       t        j                  | j                  d       y )NzauthClient: idle add cancelr   r}   rA   s    r   rn   zAuthClient.emit_idle_cancel   s    +,M2r   c                 \    t        d       t        j                  | j                  d|       y )Nz authClient: idle add auth-promptr   r}   )r   rz   s     r   rs   z AuthClient.emit_idle_auth_prompt   s!    01M6:r   c                 \    t        d       t        j                  | j                  d|       y )NzauthClient: auth-infor   r}   )r   r{   s     r   rt   zAuthClient.emit_auth_info   s!    %&K6r   r   )__name__
__module____qualname____doc__r   SignalFlagsRUN_LASTboolstr__gsignals__r   r?   r   rD   rC   r2   r^   r7   ro   rl   rm   rn   rs   rt   __classcell__)r   s   @r   r	   r	      s    
 !,,55tR@ ,,55tR@++44dB?))22D4'B++44dSFC))22D3&AL-^>E0*ZX7443;7r   r	   )rp   r   rG   platformsysgi.repositoryr   r   r   r   r"   r&   
util.utilsr   Objectr	   r
   r   r   <module>r      s7    
 	   
 : :   Q7 Q7r   