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

Last change on this file since 16345 was 16345, checked in by dcorreia, 4 years ago

Added features of second part of configuration process: subject mapping, card certificate operations and pam-configs.

File size: 4.8 KB
Line 
1#!/usr/bin/python3
2import os
3import subprocess
4import sys
5import shutil
6import inspect
7
8
9INSTALL_COMMAND = "apt-get install "
10HASHLINK_COMMAND = "pkcs11_make_hash_link"
11WGET_COMMAND = "wget "
12GUNZIP_COMMAND = "gunzip "
13MV_COMMAND = "mv "
14INSPECT_COMMAND = "pkcs11_inspect"
15PAMAUTHUP_COMMAND = "pam-auth-update"
16
17
18def executeCommand(command, outputPath=None):
19    '''
20    Executes a shell command and redirects output if necessary
21    :param command: command to execute
22    :param outputPath: path to redirect output if necessary
23    :return: no return value
24    '''
25    try:
26        process = subprocess.Popen(command, shell=True, stdout=outputPath, stderr=subprocess.PIPE)
27        process.wait()
28        print("DEBUG: executeCommand (%s) - %s" % (command, process.stderr.read()), file=sys.stderr)
29    except subprocess.CalledProcessError as error:
30        print("DEBUG: executeCommand (%s) - %s" % (command, error), file=sys.stderr)
31
32
33def printDebugInfo(debugData):
34    '''
35    Prints debug information
36    :param debugData: string of debug data
37    :return: no return value
38    '''
39    print("DEBUG: %s - %s" % (inspect.stack()[1][3], debugData), file=sys.stderr)
40
41
42def installPackage(packageName):
43    '''
44    Installs package using apt-get
45    :param packageName: name of the package
46    :return: no return value
47    '''
48    command = INSTALL_COMMAND + packageName
49    output = open(os.devnull, "wb")
50    executeCommand(command, output)
51
52
53def makeDirectory(directoryPath):
54    '''
55    Creates a new directory
56    :param directoryPath: path of the new directory
57    :return: no return value
58    '''
59    try:
60        os.mkdir(directoryPath)
61        printDebugInfo("created directory with path "+directoryPath)
62    except OSError as error:
63        printDebugInfo(error)
64
65
66def copyFileToDirectory(filePath, directoryPath):
67    '''
68    Copy a file to a directory
69    :param filePath: path of the file to copy
70    :param directoryPath: path of the directory
71    :return: no return value
72    '''
73    try:
74        shutil.copy2(filePath, directoryPath)
75    except FileNotFoundError as error:
76        printDebugInfo(error)
77
78
79def makeHashLinks(directoryPath):
80    '''
81    Make pkcs11 hash links in a directory
82    :param directoryPath: path of directory
83    :return: no return value
84    '''
85    currentDirectoryPath = os.getcwd()
86    os.chdir(directoryPath)
87    executeCommand(HASHLINK_COMMAND)
88    os.chdir(currentDirectoryPath)
89
90
91def getCRLFile(crlWebLink, directoryPath):
92    '''
93    Download crl file into a directory
94    :param crlWebLink: link of the crl file
95    :param directoryPath: path of the directory
96    :return: no return value
97    '''
98    currentDirectoryPath = os.getcwd()
99    os.chdir(directoryPath)
100    executeCommand(WGET_COMMAND+crlWebLink)
101    os.chdir(currentDirectoryPath)
102
103
104def makeConfigFile(configZipPath, configPath, directoryPath):
105    currentDirectoryPath = os.getcwd()
106    copyFileToDirectory(configZipPath, directoryPath)
107    os.chdir(directoryPath)
108    executeCommand(GUNZIP_COMMAND+"pam_pkcs11.conf.example.gz")
109    executeCommand(MV_COMMAND+"pam_pkcs11.conf.example "+configPath)
110    os.chdir(currentDirectoryPath)
111
112
113def addConfigModule(moduleFileName, configPath):
114    configData = open(configPath, mode="r").readlines()
115    moduleData = open(moduleFileName, mode="r").readlines()
116    old_last = configData[len(configData)-1]
117    configData[len(configData)-1] = "".join(moduleData) + "\n" + old_last
118    open(configPath,mode="w").write("".join(configData))
119
120
121def changeActiveModule(newModule, configPath):
122    configData = open(configPath, mode="r").readlines()
123    for i in range(0,len(configData)):
124        if "use_pkcs11_module" in configData[i]:
125            configData[i] = "  use_pkcs11_module = " + newModule + ";\n"
126            break
127    open(configPath, mode="w").write("".join(configData))
128
129
130def testInspect():
131    executeCommand(INSPECT_COMMAND)
132
133
134def changeUseMappers(newValue, configPath):
135    configData = open(configPath, mode="r").readlines()
136    for i in range(0, len(configData)):
137        if "use_mappers = " in configData[i]:
138            configData[i] = "  #" + configData[i] + "  use_mappers = " + newValue + "\n"
139            break
140    open(configPath, mode="w").write("".join(configData))
141
142
143def getCardAuthCertificate(outputFilePath):
144    executeCommand(INSPECT_COMMAND, open(outputFilePath, mode="w"))
145    cardData = open(outputFilePath, mode="r").readlines()
146    for i in range(0, len(cardData)):
147        if "Autentica" in cardData[i] and "serialNumber=" in cardData[i]:
148            open(outputFilePath, mode="w").write(cardData[i])
149            break
150
151
152def addUserMapping(cardDataFilePath, mappingConfigPath, username):
153    certificate = "".join(open(cardDataFilePath, mode="r").readlines()).strip()
154    combo = certificate + " -> " + username + "\n"
155    open(mappingConfigPath, mode="a").write(combo)
156
157
158def runPamAuthUpdate():
159    executeCommand(PAMAUTHUP_COMMAND)
Note: See TracBrowser for help on using the repository browser.