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

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

Added support for creating directories and copying files.
Abstracted debug printing and command execution.
Updated package installation.
Added make pkcs11 hash links and get crl file features.

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