
/Jc           @   s  d  d k  Z d  d k Z d  d k Z d  d k Z d  d k Z d  d k Z d  d k Z d   Z d d  Z d d  Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z d d d d  Z d   Z d d d d d  Z d d  Z d d  Z d d d d  Z d d d  Z d d d d d g  d  Z d d d d d d g  d  Z d   Z d d  Z d   Z d   Z d   Z d d  Z d S(   iNc         C   sI   y/ t  i |  d  } | d j o |  |  Sn Wn t j
 o n Xd  S(   Nt   /i    (   t   stringt   rindext
   ValueErrort   None(   t   patht   indx(    (    s   /usr/share/jailkit/jk_lib.pyt
   nextpathup(   s    i    c         C   s  y t  i |   } Wn@ t j
 o4 | d j o t i i d |  d  n d Sn Xt i | t i  pS t i	 | t i  o t i i d |  d  n t i i d |  d  d Sn t i
 d	 d
 j oX | t i d j p  | t i t i d  i j o  t i i d |  d  d	 SqtnI | t i d j p | t i d j o  t i i d |  d  d	 Sn | t i t i @p | t i t i @o  t i i d |  d  d Sn d S(   Ni    s   ERROR: cannot lstat() s   
is   ERROR: s2    is a symlink, please point to the real directory
s    is not a directory!
iit   bsdt   wheels    is not owned by root:wheel!
s    is not owned by root:root!
s     is writable by group or others!ii   (   t   ost   lstatt   OSErrort   syst   stderrt   writet   statt   S_ISDIRt   ST_MODEt   S_ISLNKt   platformt   ST_UIDt   ST_GIDt   grpt   getgrnamt   gr_gidt   S_IWOTHt   S_IWGRP(   R   t	   failquiett   statbuf(    (    s   /usr/share/jailkit/jk_lib.pyt   path_is_safe4   s,    	4(*c      	   C   s   t  i i |   }  t |  |  } | d j  o | Sn x: d D]2 } t |  d | d  } | d j  o | Sq= q= Wt |   } xB | d j o4 t | d  } | d j o | Sn t |  } q Wd S(   s]   tests if path is a safe jail, not writable, no writable /etc/ and /lib, return 1 if all is OKit   libt   etct   usrt   vart   bint   devt   proct   sbinR   R    i   i    (	   s   libR    R!   s   varR#   R$   s   procR&   s   sysN(   R
   R   t   abspathR   R   R   (   R   R   t   retvalt   subdt   npath(    (    s   /usr/share/jailkit/jk_lib.pyt   chroot_is_safeO   s"      c         C   s7   t  i |   } | t i t i t i B@o d Sn d S(   sA   returns 1 if the file is setuid or setgid, returns 0 if it is noti   i    (   R
   R   R   R   t   S_ISUIDt   S_ISGID(   R   R   (    (    s   /usr/share/jailkit/jk_lib.pyt   test_suid_sgidb   s    c         C   sO   t  i d  d j o7 t |  d d d d d d d d t i d |   n d  S(	   Ni   t   linuxs   /etc/i    t   copy_permissionst
   allow_suidt   copy_ownerships   ldconfig -r (   R   R   t   create_parent_pathR
   t   system(   t   jail(    (    s   /usr/share/jailkit/jk_lib.pyt   gen_library_cachei   s    "c         C   s  g  } t  i d |   } | d i   } xt |  d j ot i |  } t |  d j o| d d j o | d d j o | Sq| d d j o* | d d j o | d	 d
 j o | Sq| d d j o qt |  d j o& | d d j o | d	 d j o qt |  d	 j oE t  i i | d  o | | d g 7} qd | d d |  GHqt |  d j oR | d d d j o= t  i i | d  o | | d g 7} qd | d GHqd | d  GHn d | d  GH| d i   } q, W| S(   s;   returns a list of libraries that the executable depends on s   ldd i   i    t
   staticallyt   linkedt   noti   t   dynamici   t
   executables   linux-gate.so.1i   t   founds!   ldd returns non existing library s    for R    s$   WARNING: failed to parse ldd output i(   R
   t   popen3t   readlinet   lenR   t   splitR   t   exists(   R;   R(   t   pdt   linet   subl(    (    s   /usr/share/jailkit/jk_lib.pyt   lddlist_libraries_linuxo   s6     "35(c         C   s  g  } d } t  i d |   } | d i   } xtt |  d j o`t i |  } t |  d j o| d |  d j o q| d d j o2 t |  d j o | d d	 j o
 d
 } qqt |  d j o | d j o= t  i i | d
  o | | d
 g 7} qod | d
 GHq| d
 j o= t  i i | d  o | | d g 7} qod | d GHqd GHqd | d  GHn d | d  GH| d i   } q2 W| S(   s;   returns a list of libraries that the executable depends on i   s   ldd i   i    t   :t   Starti   i   t   Namei   i   s!   ldd returns non existing library s1   unknown mode, please report this bug in jk_lib.pys$   WARNING: failed to parse ldd output i(   R
   R=   R>   R?   R   R@   R   RA   (   R;   R(   t   modeRB   RC   RD   (    (    s   /usr/share/jailkit/jk_lib.pyt   lddlist_libraries_openbsd   s8     $
	c         C   s  g  } t  i d |   } | d i   } xZt |  d j oFt i |  } t |  d j o t |  d j o' | d t |   d  |  d j o qqt |  d j o* | d d j o | d d	 j o | Sqqt |  d j o= t  i i | d  o | | d g 7} q9d
 | d GHqqd | d  d GHn5 | t |   d  |  d j o n d | d  d GH| d i   } q, W| S(   s;   returns a list of libraries that the executable depends on s   ldd i   i    RF   i   i   R9   i   R:   s!   ldd returns non existing library s%   WARNING: failed to parse ldd output "it   "(   R
   R=   R>   R?   R   R@   R   RA   (   R;   R(   RB   RC   RD   (    (    s   /usr/share/jailkit/jk_lib.pyt   lddlist_libraries_freebsd   s*     65c         C   s   t  i d  d j o t |   Snh t  i d  d j o t |   SnF t  i d  d j o t |   Sn$ t |   } | d d d g 7} | Sd  S(	   Ni   R/   i   t   openbsdt   freebsds   /usr/libexec/ld.sos   /usr/libexec/ld-elf.so.1s   /libexec/ld-elf.so.1(   R   R   RE   RJ   RL   (   R;   R(   (    (    s   /usr/share/jailkit/jk_lib.pyt   lddlist_libraries   s    c         C   s   t  i |   } t i | t i  } | p< | t i t i B@o# d | GH| t i @t i @} qh n t  i | |  t  i | | t i | t i	 f  | o% t  i
 | | t i | t i  n d  S(   Ns,   removing setuid and setgid permissions from (   R
   R   t   S_IMODER   R,   R-   t   chmodt   utimet   ST_ATIMEt   ST_MTIMEt   chownR   R   (   t   srct   dstt
   be_verboseR1   R2   t   sbufRI   (    (    s   /usr/share/jailkit/jk_lib.pyt   copy_time_and_permissions   s    	$c         C   sS   |  } xF t  i i |  o1 | d j p
 | d j o t  i i |  } q	 W| S(   ss   This function tests if a directory exists, if not tries the parent etc. etc. until it finds a directory that existsR    t    (   R
   R   RA   t   dirname(   R   t   tmp(    (    s   /usr/share/jailkit/jk_lib.pyt   return_existing_base_directory   s
     /i   c         C   s4  | } | d d j o | d  } n t  i i |  |  o d Sn t |  |  } t |  t |   } t i | d | d  }	 x|	 d j o | d |	 j o
 |	 } n | o d |  | |	  GHn t  i |  | |	  d  | o| y& t | |	  |  | |	  | | |  Wqet	 j
 oC \ }
 } t
 i i d | |	  d |  | |	  d	 | d
  qeXn |	 } t i | d | d  }	 q W| o d |  | GHn t  i |  | d  | ot y t | |  | | | |  Wq0t	 j
 oC \ }
 } t
 i i d | |	  d |  | |	  d	 | d
  q0Xn d S(   su   creates the directory and all its parents id needed. copy_ownership can only be used if copy permissions is also usediR    Ni   s   Creating directory i  s2   ERROR: failed to copy time/permissions/owner from s    to s   : s   
(   R
   R   RA   R^   R?   R   t   findt   mkdirRZ   R   R   R   R   (   t   chrootR   RX   R0   R1   R2   t	   directoryR]   t   oldindxR   t   errnot   strerror(    (    s   /usr/share/jailkit/jk_lib.pyR3      s<     
&=c   	      C   s  yA | o d | GHn t  i |  t |  | | d d d d WnM t t f j
 o; \ } } t i i d |  d | d | d	  d Sn Xx>t  i |   D]-\ } } } x | D] } | o% d
 | d | d | d | GHn yP t	 i
 | d | | d |  t | d | | d | | d d d d Wq t t f j
 oK \ } } t i i d | d | d | d | d | d	  d Sq Xq Wx. | D]& } t | d | | d | |  qWq Wd S(   Ns   Creating directoryR1   i    R2   i   s)   ERROR: copying directory and permissions s    to s   : s   
s   Copying R    s$   ERROR: copying file and permissions (   R
   R`   RZ   t   IOErrorR   R   R   R   t   walkt   shutilt   copyfilet#   move_dir_with_permissions_and_owner(	   t   srcdirt   dstdirRX   Rd   Re   t   roott   dirst   filest   name(    (    s   /usr/share/jailkit/jk_lib.pyt#   copy_dir_with_permissions_and_owner  s0     (	  % 08 (c         C   s   t  |  | |  } | d j os | d j o d |  GHn y t i |   Wq t t f j
 o/ \ } } t i i d |  d | d  q Xn d | d |  GHd  S(   Ni   s!   Removing original home directory s   ERROR: failed to remove s   : s   
s   Not everything was copied to s   , keeping the old directory (   Rq   Rh   t   rmtreeR   Rf   R   R   R   (   Rk   Rl   RX   R(   Rd   Re   (    (    s   /usr/share/jailkit/jk_lib.pyRj   1  s    )c         C   s   d } | d j o= y t  i |  |  d } WqP d |  d | d GHqP Xn | d j o y0 t i |  |  t |  | | d d d | Wq t t f j
 o7 \ } } t i i	 d |  d | d	 | d
  q Xn d S(   sK   copies/links the file and the permissions, except any setuid or setgid bitsi   i    s   Linking s    to s    failed, will revert to copyingR1   R2   s$   ERROR: copying file and permissions s   : s   
N(
   R
   t   linkRh   Ri   RZ   Rf   R   R   R   R   (   RV   RW   RX   t   try_hardlinkt   retain_ownert   do_normal_copyRd   Re   (    (    s   /usr/share/jailkit/jk_lib.pyt   copy_with_permissions>  s    
 c   	      C   s  t  |  t i i |  | d d d d d d t i i |  |  o d |  | d GHd  Sn t i |  } y | i d } | i d } t i | i  o
 d	 } n/ t i	 | i  o
 d
 } n d | d GHd S| d j o d |  | GHn t i
 t i d d |  | t |  t |  t |   } t | |  | d d d | Wn3 d | d GHd | d GHd | d GHd GHn Xd  S(   NR0   i   R1   i    R2   s   Device s    does exist alreadyi   t   ct   bs	   WARNING, s#    is not a character or block devices   Creating device t   mknods   Failed to create device s(   , this is a know problem with python 2.1s   use "ls -l s6   " to find out the mode, major and minor for the devices   use "mknod s'    mode major minor" to create the devices<   use chmod and chown to set the permissions as found by ls -l(   R3   R
   R   R\   RA   R   t   st_rdevt   S_ISCHRt   st_modet   S_ISBLKt   spawnlpt   P_WAITt   strRZ   (	   Ra   R   RX   Ru   t   sbt   majort   minorRI   t   ret(    (    s   /usr/share/jailkit/jk_lib.pyt   copy_deviceO  s.    .

:!c         C   s!  d
 } x t  i |  D] }	 t  i i | |	  }
 y t  i |
  } t i | i  oJ t |  |
 d | d d d d d | t	 |  |
 | | | | | |  } n | t  i i | |	  f 7} Wq t
 j
 o, } t i i d |
 d | i d	  q Xq Wt |  | | | | | | |  } | S(   sR   copies a directory and the permissions recursive, except any setuid or setgid bitsRX   R0   i   R1   i    R2   s)   ERROR: failed to investigate source file s   : s   
(    (   R
   t   listdirR   t   joinR   R   R   R}   R3   t   copy_dir_recursiveR   R   R   R   Re   t   copy_binaries_and_libs(   Ra   t   dirt   force_overwriteRX   t
   check_libsRt   Ru   t   handledfilest   files2t   entryR]   RY   t   e(    (    s   /usr/share/jailkit/jk_lib.pyR   j  s     %% ,!c	         C   s  |  d d j o |  d  }  n xs| D]k}	 |	 | j o q& n y t  i |	  }
 Wn t j
 o } | i d j o | d j or t i |	  } t |  d j o4 t |  | | | | d | d | d d d	 | } q| o d
 |	 d GHqq4| o d |	 d GHq4q& t i i	 d |	 d | i
 d  q& n Xy t  i |  |	  } d } WnV t j
 oJ } | i d j o
 d } qt i i	 d |  |	 d | i
 d  n X| d j o; | o4 t i | i  o  | o d |  |	 d GHqq& | o | o t i | i  o | o d |  |	 d GHn y t  i |  |	  Wqt j
 o< } t i i	 d |  |	 d | i
 d |  |	 d  qXqt i | i  o d |  |	 d GHqqt i | i  o q| o d |  |	 d GHq& q& n t |  t  i i |	  | d d d d d | t i |
 i  o t  i |	  } d |  |	 d | GHy t  i | |  |	  Wn t j
 o n X| i |	  | d d j o t  i i |	  d | } n t |  | g | | | | | |  } nt i |
 i  o% t |  |	 | | | | | |  } n t i |
 i  o` | o d |	 d |  |	 GHn d  |	 d |  |	 GHt |	 |  |	 | | |  | i |	  nV t i |
 i  p t i |
 i  o t |  |	 | |  n t i i	 d! |	 d"  t i |
 t i  } | o| t i |	 d#  d j p5 t i |	 d$  d j p | t i  t i! Bt i" B@o. t# |	  } t |  | | | d | |  } q& q& W| S(%   s>   copies a list of executables and their libraries to the chrootiR    i   i   i    Rt   Ru   t   try_glob_matchingR   s   Source file(s) s    do not exists   Source file s    does not exists)   ERROR: failed to investigate source file s   : s   
s.   ERROR: failed to investigate destination file R[   s"    already exists, will not touch its   Destination file s$    exists, will delete to force updates   ERROR: failed to delete s   
cannot overwrite s   Destination dir s    existsR0   R1   R2   s   Creating symlink s    to s   Trying to link s   Copying s   Failed to find how to copy s=    into a chroot jail, please report to the Jailkit developers
R   s   .so($   R
   R   R   Rd   t   globR?   R   R   R   R   Re   R   R   R}   t   S_ISREGt   unlinkR3   R   R\   R   t   readlinkt   symlinkt   appendR   Rw   R|   R~   R   RP   R   R   R_   t   S_IXUSRt   S_IXGRPt   S_IXOTHRO   (   Ra   t   binarieslistR   RX   R   Rt   Ru   R   R   t   fileR   R   R   t   chrootsbt   chrootfile_existst   realfileRI   t   libs(    (    s   /usr/share/jailkit/jk_lib.pyR     s     4#

,(<.(%&U&c         C   sf   g  } |  i  | |  oI |  i | |  } x4 t i | d  D] } | t i |  g 7} q> Wn | S(   s   retrieves a comma separated option from the configparser and splits it into a list, returning an empty list if it does not existt   ,(   t
   has_optiont   getR   R@   t   strip(   t	   cfgparsert   sectionnamet
   optionnameR(   t   inputstrR]   (    (    s   /usr/share/jailkit/jk_lib.pyt   config_get_option_as_list  s     t   ERRORc         C   s*   d GH| d | GH|   t  i |   d  S(   NR[   s   : (   R   t   exit(   t   exitnot   messaget	   usagefunct   type(    (    s   /usr/share/jailkit/jk_lib.pyt
   clean_exit  s    c         C   s   y t  | d  } Wn d Sn X| i   } xl t |  d j oX t i | d  } t |  | j o# | | |  j o | i   d Sn | i   } q0 Wd S(   Nt   ri    RF   i   (   t   openR>   R?   R   R@   t   close(   t   itemt   numt   filenamet   fdRC   t   pwstruct(    (    s   /usr/share/jailkit/jk_lib.pyt   test_numitem_exist  s     $
c         C   s   t  |  d |  S(   Ni    (   R   (   t   usert
   passwdfile(    (    s   /usr/share/jailkit/jk_lib.pyt   test_user_exist   s    c         C   s   t  |  d |  S(   Ni    (   R   (   t   groupt	   groupfile(    (    s   /usr/share/jailkit/jk_lib.pyt   test_group_exist  s    c   	      C   sV  |  d d j o |  d  }  n t  |  d | d d d d d d t i d d	 !d
 j o` t |  d d  i   t |  d d  i   t |  d d  i   t |  d d  i   nat i i |  d  p t |  d d  } n t |  d d  } | i   } x t	 |  d j o t
 i | d  } t	 |  d j o | d | j p | d | j o~ | o d | d d |  d GHn y | i | d  Wn t j
 o n Xy | i | d  Wqt j
 o qXqn | i   } qW| i d d  t	 |  d j o t d d  } | i   } x t	 |  d j o t
 i | d  } t	 |  d j o | d | j p | d | j o[ | i |  | o d | d d |  d GHn | d | j o | | d g 7} qqn | i   } q6W| i   n | i   t i i |  d  p t |  d d  } n t |  d d  } | i   } x t	 |  d j o t
 i | d  } t	 |  d j o | d | j p | d | j o~ | o d | d d |  d GHn y | i | d  Wn t j
 o n Xy | i | d  Wq@t j
 o q@XqDn | i   } qhW| i d d  t	 |  d j o t d d  } | i   } x t	 |  d j o t
 i | d  } t	 |  d j o[ | d | j p | d | j o5 | i |  | o d | d d |  d GHq%q)n | i   } qW| i   n | i   d  S(   NiR    s   /etc/R0   i    R1   R2   i   i   R   s   /etc/passwdt   as   /etc/spwd.dbs   /etc/pwd.dbs   /etc/master.passwdt   ws   r+RF   i   i   s   user s    exists in R   s   writing user s    to s
   /etc/groups   group s   writing group (   R3   R   R   R   R   R
   R   t   isfileR>   R?   R   R@   t   removeR   t   seekR   (	   Ra   t   userst   groupsRX   t   fd2RC   R   R   t   groupstruct(    (    s   /usr/share/jailkit/jk_lib.pyt   init_passwd_and_group  s    " " "
 " "%(   t   os.pathR
   R   R   R   Rh   R   R   R   R+   R.   R6   RE   RJ   RL   RO   RZ   R^   R3   Rq   Rj   Rw   R   R   R   R   R   R   R   R   R   (    (    (    s   /usr/share/jailkit/jk_lib.pys   <module>    s<   				"	#			$\					