def checkCertificate(self, certFile): # Проверка сертификата textLine = execProg("%s x509 -subject -fingerprint -noout -in '%s'" %(self.sslFile, certFile)) if textLine is False: self.printERROR(_("Error checking certificate %s")%certFile) return False return True
def del_runlevel(self): """Delete daemon from runlevel""" if self.is_runlevel(): if execProg(self.cmdDelRunlevel) is False: self.printERROR(_("Can't execute '%s'") % self.cmdDelRunlevel) self.printNotOK(_("service %(name)s removed from runlevel") % self._templDict + " ...") return False return True
def add_runlevel(self): """Add daemon to runlevel""" if not self.is_runlevel(): if execProg(self.cmdAddRunlevel) is False: self.printERROR(_("Can't execute '%s'") % self.cmdAddRunlevel) self.printNotOK(_("service %(name)s added to runlevel") % self._templDict + " ...") return False return True
def stop(self): """Stop LDAP server""" if self.is_start(): if execProg(self.cmdStop) is False: self.printERROR(_("Can't execute '%s'") % self.cmdStop) self.printNotOK(_("Stopping LDAP") + " ...") return False return True
def restart(self): """Restart LDAP server""" if self.is_start(): if execProg(self.cmdReStart) is False: self.printERROR(_("Can't execute '%s'") % self.cmdReStart) self.printNotOK(_("Restarting LDAP") + " ...") return False else: return self.start() return True
def _getRunlevelDaemons(self): """Получаем всех демонов в default уровне""" textLines = execProg(self.cmdShowDaemons) if textLines is False: self.printERROR(_("ERROR") + ": " + self.cmdShowDaemons) return False else: daemons = [] for line in textLines: res = self.reShowDaemons.search(line) if res: daemon = res.groups(0)[0] daemons.append(daemon) return daemons
def createCertificate(self, sslCountry=sslCountry, sslState=sslCountry, sslLocality=sslLocality, sslOrganization=sslOrganization, sslUnit=sslUnit, sslCommonName=sslCommonName, sslEmail=sslEmail, nsCertType=nsCertType, sslDays=sslDays, sslBits=sslBits, userName=userName, certFile=certFile, fileMode=fileMode, keyFile=keyFile): """Создает сертификат""" certAndKeyFiles = [certFile, keyFile] foundCertFiles = filter(lambda x: os.path.exists(x), certAndKeyFiles) if len(foundCertFiles)==2: return True # Удаляем файл сертификата map(lambda x: os.remove(x), foundCertFiles) # получаем id и gid пользователя try: pwdObj = pwd.getpwnam(userName) except: self.printERROR(_("User %s not found")%userName) return False uid = pwdObj.pw_uid gid = pwdObj.pw_gid textCnf=self.templCnfData%{'sslBits':sslBits, 'sslCountry':sslCountry, 'sslState':sslState, 'sslLocality':sslLocality, 'sslOrganization':sslOrganization, 'sslUnit':sslUnit, 'sslCommonName':sslCommonName, 'sslEmail':sslEmail, 'nsCertType':nsCertType} cnfFile = self.createCnfFile(textCnf) if cnfFile is False: return False # Cоздание директорий for fileName in certAndKeyFiles: dirName = os.path.split(fileName)[0] if not os.path.exists(dirName): self._createDir(dirName, uid=uid, gid=gid) # Создание сертификата textLine = execProg("%s req -new -x509 -nodes -config '%s'" "-days %s -out '%s'-keyout '%s'" %(self.sslFile, cnfFile, sslDays, certFile, keyFile)) if textLine is False: self.printERROR(_("Failed to create certificate %s")%certFile) return False # Удаление конфигурационного файла if os.path.exists(cnfFile): os.remove(cnfFile) # Меняем права if os.path.exists(certFile): os.chown(certFile, uid,gid) os.chmod(certFile, fileMode) if os.path.exists(keyFile): os.chown(keyFile, uid,gid) os.chmod(keyFile, fileMode) return self.checkCertificate(certFile)
def createSignedCertificate(self, sslCountry=sslCountry, sslState=sslState, sslLocality=sslLocality, sslOrganization=sslOrganization, sslUnit=sslUnit, sslCommonName=sslCommonName, sslEmail=sslEmail, nsCertType=nsCertType, sslDays=sslDays, sslBits=sslBits, userName=userName, CAPath=CAPath, CACertFileName=CACertFileName, CAKeyFileName=CAKeyFileName, CACrlFileName=CACrlFileName, certFile=certFile, fileMode=fileMode, keyFile=keyFile, csrFile=csrFile, force=False): '''Create signed CA certificate''' certAndKeyFiles = [certFile, keyFile, csrFile] foundCertFiles = map(lambda x: os.path.exists(x), certAndKeyFiles) if not force and foundCertFiles[0] and foundCertFiles[1]: return True foundCertFiles = map(lambda x: x[1], filter(lambda x: x[0], zip(foundCertFiles, certAndKeyFiles))) # Удаляем файлы map(lambda x: os.remove(x), foundCertFiles) # получаем id и gid пользователя try: pwdObj = pwd.getpwnam(userName) except: self.printERROR(_("User %s not found")%userName) return False uid = pwdObj.pw_uid gid = pwdObj.pw_gid # create dirs for fileName in certAndKeyFiles: dirName = os.path.split(fileName)[0] if not os.path.exists(dirName): self._createDir(dirName, uid=uid, gid=gid) rCACertFile = os.path.join(self.rCACertPath, CACertFileName) rCAKeyFile = os.path.join(self.rCAKeyPath, CAKeyFileName) rCACrlFile = os.path.join(self.rCACrlPath, CACrlFileName) rRandFile = os.path.join(self.rCAKeyPath,".rnd") textCnf = self.templCnfCA%{'CAPath':CAPath, 'rCACertPath':self.rCACertPath, 'rCACrlPath':self.rCACrlPath, 'rDatabaseFileName':self.rDatabaseFileName, 'rCACertFile':rCACertFile, 'rSerialFileName':self.rSerialFileName, 'rCACrlFile':rCACrlFile, 'rCAKeyFile':rCAKeyFile, 'rRandFile':rRandFile, 'sslBits':sslBits, 'sslCountry':sslCountry, 'sslState':sslState, 'sslLocality':sslLocality, 'sslOrganization':sslOrganization, 'sslUnit':sslUnit, 'sslCommonName':sslCommonName, 'sslEmail':sslEmail, 'nsCertType':nsCertType} cnfFile = self.createCnfFile(textCnf) if cnfFile is False: return False # generate RSA key execStr = self.templCreateKey%{'sslFile':self.sslFile, 'certKeyFile':keyFile, 'sslBits':sslBits} if execProg(execStr) is False: self.printERROR(_("Can not execute '%s'")%execStr) return False if os.path.exists(keyFile): os.chown(keyFile, uid,gid) os.chmod(keyFile, fileMode) # generate request execStr = self.templCreateReq%{'sslFile':self.sslFile, 'sslDays':sslDays, 'cnfFile':cnfFile, 'certKeyFile':keyFile, 'certCsrFile':csrFile} if execProg(execStr) is False: self.printERROR(_("Can not execute '%s'")%execStr) return False if os.path.exists(csrFile): os.chown(csrFile, uid,gid) os.chmod(csrFile, fileMode) # set database attribute databaseAttrFileName = os.path.join(CAPath, "index.dat.attr") self._createFile(databaseAttrFileName, "unique_subject = no\n") # generate signed cerificate execStr = self.templCreateSignCert%{'sslFile':self.sslFile, 'sslDays':sslDays, 'cnfFile':cnfFile, 'certFile':certFile, 'certCsrFile':csrFile} if execProg(execStr) is False: self.printERROR(_("Can not execute '%s'")%execStr) return False if os.path.exists(certFile): os.chown(certFile, uid,gid) os.chmod(certFile, fileMode) if os.path.exists(cnfFile): os.remove(cnfFile) # check certificate return self.checkCertificate(certFile)
def createCertificateAutority(self,sslCountry=sslCountry, sslState=sslState, sslLocality=sslLocality, sslOrganization=sslOrganization, sslUnit=sslUnit, sslCommonName=sslCommonName, sslEmail=sslEmail, nsCertType=nsCertType, sslDays=sslDays, sslBits=sslBits, userName=userName, CAPath=CAPath, CACertFileName=CACertFileName, CAKeyFileName=CAKeyFileName, CACrlFileName=CACrlFileName, fileMode=fileMode, force=False): '''Create CA''' rCACertFile = os.path.join(self.rCACertPath, CACertFileName) rCAKeyFile = os.path.join(self.rCAKeyPath, CAKeyFileName) rCACrlFile = os.path.join(self.rCACrlPath, CACrlFileName) rRandFile = os.path.join(self.rCAKeyPath,".rnd") CACertFile = os.path.join(CAPath, rCACertFile) CAKeyFile = os.path.join(CAPath, rCAKeyFile) # Cerificates exists if not force and os.path.isfile(CACertFile) and\ os.path.isfile(CAKeyFile): return True # получаем id и gid пользователя try: pwdObj = pwd.getpwnam(userName) except: self.printERROR(_("User %s not found")%userName) return False uid = pwdObj.pw_uid gid = pwdObj.pw_gid # delete certificate dir if os.path.isdir(CAPath): removeDir(CAPath) # create certificate dirs self._createDir(CAPath) CACertPath = os.path.join(CAPath, self.rCACertPath) CAKeyPath = os.path.join(CAPath, self.rCAKeyPath) CACrlPath = os.path.join(CAPath, self.rCACrlPath) for createDir in [CACertPath, CAKeyPath, CACrlPath]: self._createDir(createDir) # save serial number SerialFile = os.path.join(CAPath, self.rSerialFileName) self._createFile(SerialFile, "01\n") # create database file DatabaseFile = os.path.join(CAPath, self.rDatabaseFileName) self._createFile(DatabaseFile, "") textCnf = self.templCnfCA%{'CAPath':CAPath, 'rCACertPath':self.rCACertPath, 'rCACrlPath':self.rCACrlPath, 'rDatabaseFileName':self.rDatabaseFileName, 'rCACertFile':rCACertFile, 'rSerialFileName':self.rSerialFileName, 'rCACrlFile':rCACrlFile, 'rCAKeyFile':rCAKeyFile, 'rRandFile':rRandFile, 'sslBits':sslBits, 'sslCountry':sslCountry, 'sslState':sslState, 'sslLocality':sslLocality, 'sslOrganization':sslOrganization, 'sslUnit':sslUnit, 'sslCommonName':sslCommonName, 'sslEmail':sslEmail, 'nsCertType':nsCertType} cnfFile = self.createCnfFile(textCnf) if cnfFile is False: return False # generate CA RSA key execStr = self.templCreateKey%{'sslFile':self.sslFile, 'certKeyFile':CAKeyFile, 'sslBits':sslBits} if execProg(execStr) is False: self.printERROR(_("Can not execute '%s'")%execStr) return False if os.path.exists(CAKeyFile): os.chown(CAKeyFile, uid,gid) os.chmod(CAKeyFile, fileMode) # create CA execStr = self.templCreateCert%{'sslFile':self.sslFile, 'sslDays':sslDays, 'cnfFile':cnfFile, 'certKeyFile':CAKeyFile, 'certFile':CACertFile} if execProg(execStr) is False: self.printERROR(_("Can not execute '%s'")%execStr) return False if os.path.exists(CACertFile): os.chown(CACertFile, uid,gid) os.chmod(CACertFile, fileMode) if os.path.exists(cnfFile): os.remove(cnfFile) # check certificate return self.checkCertificate(CACertFile)