source: internals/2016/linuxloginsmartcardwizard/trunk/project1/configpkcs11.py @ 16332

Last change on this file since 16332 was 16332, checked in by dcorreia, 3 years ago

Added pam_pkcs11 configuration file creation.
Updated crl wget feature to avoid duplicate files.

File size: 2.9 KB
Line 
1#!/usr/bin/python3
2import os
3import subprocess
4import sys
5import shutil
6import inspect
7
8INSTALL_COMMAND = "apt-get install "
9HASHLINK_COMMAND = "pkcs11_make_hash_link"
10WGET_COMMAND = "wget "
11GUNZIP_COMMAND = "gunzip "
12MV_COMMAND = "mv "
13
14
15def executeCommand(command, outputPath=None):
16    '''
17    Executes a shell command and redirects output if necessary
18    :param command: command to execute
19    :param outputPath: path to redirect output if necessary
20    :return: no return value
21    '''
22    try:
23        process = subprocess.Popen(command, shell=True, stdout=outputPath, stderr=subprocess.PIPE)
24        process.wait()
25        print("DEBUG: executeCommand (%s) - %s" % (command, process.stderr.read()), file=sys.stderr)
26    except subprocess.CalledProcessError as error:
27        print("DEBUG: executeCommand (%s) - %s" % (command, error), file=sys.stderr)
28
29
30def printDebugInfo(debugData):
31    '''
32    Prints debug information
33    :param debugData: string of debug data
34    :return: no return value
35    '''
36    print("DEBUG: %s - %s" % (inspect.stack()[1][3], debugData), file=sys.stderr)
37
38
39def installPackage(packageName):
40    '''
41    Installs package using apt-get
42    :param packageName: name of the package
43    :return: no return value
44    '''
45    command = INSTALL_COMMAND + packageName
46    output = open(os.devnull, "wb")
47    executeCommand(command, output)
48
49
50def makeDirectory(directoryPath):
51    '''
52    Creates a new directory
53    :param directoryPath: path of the new directory
54    :return: no return value
55    '''
56    try:
57        os.mkdir(directoryPath)
58        printDebugInfo("created directory with path "+directoryPath)
59    except OSError as error:
60        printDebugInfo(error)
61
62
63def copyFileToDirectory(filePath, directoryPath):
64    '''
65    Copy a file to a directory
66    :param filePath: path of the file to copy
67    :param directoryPath: path of the directory
68    :return: no return value
69    '''
70    try:
71        shutil.copy2(filePath, directoryPath)
72    except FileNotFoundError as error:
73        printDebugInfo(error)
74
75
76def makeHashLinks(directoryPath):
77    '''
78    Make pkcs11 hash links in a directory
79    :param directoryPath: path of directory
80    :return: no return value
81    '''
82    currentDirectoryPath = os.getcwd()
83    os.chdir(directoryPath)
84    executeCommand(HASHLINK_COMMAND)
85    os.chdir(currentDirectoryPath)
86
87
88def getCRLFile(crlWebLink, directoryPath):
89    '''
90    Download crl file into a directory
91    :param crlWebLink: link of the crl file
92    :param directoryPath: path of the directory
93    :return: no return value
94    '''
95    currentDirectoryPath = os.getcwd()
96    os.chdir(directoryPath)
97    executeCommand(WGET_COMMAND+crlWebLink)
98    os.chdir(currentDirectoryPath)
99
100
101def makeConfigFile(configZipPath, configPath, directoryPath):
102    copyFileToDirectory(configZipPath, directoryPath)
103    os.chdir(directoryPath)
104    executeCommand(GUNZIP_COMMAND+"pam_pkcs11.conf.example.gz")
105    executeCommand(MV_COMMAND+"pam_pkcs11.conf.example "+configPath)
Note: See TracBrowser for help on using the repository browser.