"Fossies" - the Fresh Open Source Software Archive

Member "jailkit-2.21/py/jk_cp.in" (26 Aug 2019, 4209 Bytes) of package /linux/privat/jailkit-2.21.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "jk_cp.in": 2.20_vs_2.21.

    1 #!/usr/bin/python
    2 #
    3 #Copyright (c) 2003, 2004, 2005, 2006, 2007 Olivier Sessink
    4 #All rights reserved.
    5 #
    6 #Redistribution and use in source and binary forms, with or without
    7 #modification, are permitted provided that the following conditions 
    8 #are met:
    9 #  * Redistributions of source code must retain the above copyright 
   10 #    notice, this list of conditions and the following disclaimer.
   11 #  * Redistributions in binary form must reproduce the above 
   12 #    copyright notice, this list of conditions and the following 
   13 #    disclaimer in the documentation and/or other materials provided 
   14 #    with the distribution.
   15 #  * The names of its contributors may not be used to endorse or 
   16 #    promote products derived from this software without specific 
   17 #    prior written permission.
   18 #
   19 #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
   20 #"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
   21 #LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
   22 #FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
   23 #COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
   24 #INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
   25 #BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
   26 #LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
   27 #CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
   28 #LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
   29 #ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
   30 #POSSIBILITY OF SUCH DAMAGE.
   31 #
   32 
   33 from __future__ import print_function
   34 
   35 import shutil
   36 import sys
   37 import os.path
   38 import os
   39 import getopt
   40 import string
   41 
   42 INIPREFIX='/etc/jailkit'
   43 LIBDIR='/usr/share/jailkit'
   44 sys.path.append(LIBDIR)
   45 import jk_lib
   46 
   47 def startcopy(config, chroot, filestocopy, checklibs=1):
   48     jk_lib.copy_binaries_and_libs(chroot,filestocopy,config['force'] , config['verbose'], try_hardlink=config['hardlink'], 
   49         allow_suid=config['retainsetuid'], retain_owner=config['retainowner'])
   50     jk_lib.gen_library_cache(chroot)
   51 
   52 def usage():
   53     print("Usage: "+sys.argv[0]+" -j <jail> [OPTIONS] <files and directories>")
   54     print("")
   55     print("-h --help          : this help screen")
   56     print("-j, --jail         : the jail to copy to")
   57     print("-v, --verbose      : show what is being copied")
   58     print("-f, --force        : overwrite existing files")
   59     print("-k, --hardlink     : use hardlinks if possible")
   60     print("-o, --owner        : retain file ownership and group")
   61     print("-s, --setuid       : retain file setuid/setgid bits")
   62     print("\nNote: if no jail is specified, the first argument is\nconsidered to be the jail\n")
   63 
   64 def testargs(jail, args):
   65     if (len(args) == 0 or jail == None):
   66         sys.stderr.write('ERROR: need at least a chroot directory and a file to copy\n\n')
   67         usage()
   68         sys.exit(2)
   69     if (not os.path.isdir(jail)):
   70         sys.stderr.write('ERROR: '+jail+' is not a directory\n')
   71         print("")
   72         usage()
   73         sys.exit(3)
   74     for file in args:
   75         if (not os.path.exists(file)):
   76             sys.stderr.write('ERROR: '+file+' does not exist\n\n')
   77             usage()
   78             sys.exit(4)
   79 
   80 def main():
   81     if (os.getuid()!=0):
   82         print('cannot run without root privileges')
   83         sys.exit(5)
   84     try:
   85         opts, args = getopt.getopt(sys.argv[1:], 'fh?vkosj:', ['help', 'verbose', 'force', 'hardlink', 'owner', 'jail', 'setuid'])
   86     except getopt.GetoptError:
   87         usage()
   88         sys.exit(1)
   89     config = {}
   90     config['verbose'] = 0
   91     config['force'] = 0
   92     config['hardlink'] = 0
   93     config['retainowner'] = 0
   94     config['retainsetuid'] = 0
   95     jail = None
   96     for o, a in opts:
   97         if o in ("-h",'?', "--help"):
   98             usage()
   99             sys.exit()
  100         if o in ("-j", "--jail"):
  101             jail = a
  102         if o in ("-v", "--verbose"):
  103             config['verbose'] = 1
  104         if o in ("-f", "--force"):
  105             config['force'] = 1
  106         if o in ("-k", "--hardlink"):
  107             config['hardlink'] = 1
  108         if o in ("-o", "--owner"):
  109             config['retainowner'] = 1
  110         if o in ("-s", "--setuid"):
  111             config['retainsetuid'] = 1
  112     if (jail == None and len(args)>0 ):
  113         jail = args[0]
  114         args = args[1:]
  115     args = jk_lib.find_files_in_path(args)
  116     testargs(jail, args)
  117     if (jail[-1] != '/'): jail = jail+'/'
  118     if (jk_lib.chroot_is_safe(jail)!=1):
  119         sys.exit(6)
  120     startcopy(config, jail, args)
  121 
  122 if __name__ == "__main__":
  123     main()