Example #1
0
	def __sendEmail(self, mailid, mail, envelope_from):
		"""
		This routine sends email.
		"""
		# this tranformation guaranties that each line is terminated by crlf
		mail = mail.replace('\r', '')
		mail = mail.replace('\n', '\r\n')

		# send email
		if self.testmode:
			# if tester is not set, do nothing
			if self.tester:
				status, outdata, errdata = runCommand(mailid, "%s -f %s %s" % (self.sendmail, envelope_from, self.tester), 
													mail, self.l)
			else:
				status = 0
		else:
			status, outdata, errdata = runCommand(mailid, "%s -f %s -t" % (self.sendmail, envelope_from), 
												  mail, self.l)

		if status is None: status = 0 # ok
		else: status = int(status) # sendmail failed

		return status
Example #2
0
	def __sign_email(self, mailid, mail):
		"""
		Routine for signing of email.
		"""
		# before signing remove non-MIME headers
		headerend_index = mail.find("\n\n") # find empty line
		headers = mail[:headerend_index+1]
		mimeheaders = ""
		signedmail = ""
		# throw away otherwise duplicated headers
		for header in headers.splitlines():
			if header.startswith("MIME-Version:") or \
					header.startswith("Content-Type:") or \
					header.startswith("Content-Transfer-Encoding:"):
				mimeheaders += header + '\n'
			else:
				signedmail += header + '\n'
		mail = mimeheaders + mail[headerend_index+1:]
		# create temporary file for openssl which will be used as input
		tmpfile = tempfile.mkstemp(prefix="pyfred-smime")
		os.write(tmpfile[0], mail)
		os.close(tmpfile[0])
		# do the signing
		stat, outdata, errdata = runCommand(mailid, "%s smime -sign -signer %s -inkey %s -in %s" %
				   (self.openssl, self.certfile, self.keyfile, tmpfile[1]), None, self.l)
		os.remove(tmpfile[1])
		
		if stat:
			if errdata:
				err = errdata
			else:
				err = ''
			self.l.log(self.l.ERR, "<%d> Openssl exited with failure (%d): %s" % (mailid, stat, err))
			raise Mailer_i.MailerException("Signing of email failed.")
		signedmail += outdata
		return signedmail
Example #3
0
    def __runTests(self, id, fqdns, nslist, level):
        """
		Run all enabled tests bellow given level.
		"""
        # fool the system if testmode is turned on
        if self.testmode:
            level = 0
        results = {}
        overallstatus = 0  # by default we assume that all tests were passed
        # perform all enabled tests (the ids must be sorted!)
        testkeys = self.testsuite.keys()
        testkeys.sort()
        for testid in testkeys:
            test = self.testsuite[testid]
            # check level
            if test["level"] > level:
                self.l.log(
                    self.l.DEBUG, "<%d> Omitting test '%s' becauseof its "
                    "level." % (id, test["name"]))
                continue
            # check prerequisities
            req_ok = True
            for req in test["requires"]:
                # the test might not be done if it was disabled
                try:
                    deptest = results[req]
                except KeyError, e:
                    self.l.log(
                        self.l.WARNING, "<%d> Test '%s' depends on a "
                        "test which is disabled." % (id, test["name"]))
                    continue
                # check the result of test on which we depend
                # (unknown status is considered as if the test failed)
                if results[req]["result"] != 0:
                    self.l.log(
                        self.l.DEBUG, "<%d> Omitting test '%s' becauseof "
                        "not fulfilled prerequisity." % (id, test["name"]))
                    req_ok = False
                    break
            if not req_ok:
                # prerequisities were not satisfied
                continue
            if (test["need_domain"] == 1
                    or test["need_domain"] == 3) and not fqdns:
                # list of domains is required for the test but is not given
                self.l.log(
                    self.l.DEBUG, "<%d> Omitting test '%s' because "
                    "no domains are provided." % (id, test["name"]))
                continue
            #
            # command scheduled for execution has following format:
            #    /scriptdir/script nsFqdn,ipaddr1,ipaddr2,...
            # last part is repeated as many times as many there are nameservers.
            # If test requires a domain name(s), they are supplied on stdin.
            #
            # ip addresses are provided only if the GLUE record is needed,
            # the test whether the ip addresses are needed is done here, not in
            # external test script, and all subsequent external tests use the
            # result of this pre-test. From users point of it is just another
            # ussual test.
            #
            # We recognize the GLUE test by empty string in script field
            if not test["script"]:
                stat = 0
                for ns in nslist:
                    addrs = nslist[ns]
                    data = ''
                    glue_needed = False
                    for fqdn in fqdns:
                        if ns.endswith(fqdn):
                            glue_needed = True
                            if not addrs:
                                data += " %s" % fqdn
                    # errors (missing glue) goes to 'data'
                    if data:
                        self.l.log(self.l.DEBUG,
                                   "<%d> Missing glue for ns '%s'" % (id, ns))
                        data = ns + data
                        stat = 1
                    # cancel not needed glue
                    if not glue_needed and addrs:
                        self.l.log(
                            self.l.DEBUG, "<%d> Extra glue by ns '%s' "
                            "cancelled" % (id, ns))
                        nslist[ns] = []
                note = ''
            else:
                cmd = "%s%s" % (self.scriptdir, test["script"])
                for ns in nslist:
                    addrs = nslist[ns]
                    cmd += " %s" % ns
                    for addr in addrs:
                        cmd += ",%s" % addr
                # decide if list of domains is needed
                stdin = ''
                if test["need_domain"] != 0:
                    # decide if test need only signed domains
                    if test["need_domain"] == 3:
                        # append configuration parameters to command string
                        cmd += " %s %s" % (self.drill, self.trusted_key)
                        # select only signed domains
                        list = [item for item in fqdns if fqdns[item]]
                    else:
                        list = fqdns
                    # send space separated list of domain fqdns to stdin
                    for fqdn in list:
                        stdin += fqdn + ' '

                stat, data, note = runCommand(id, cmd, stdin, self.l)

            # Status values:
            #     0 ... test OK
            #     1 ... test failed
            #     2 ... unknown result
            if stat == 1 and overallstatus != 1:
                overallstatus = 1
            elif stat == 2 and overallstatus == 0:
                overallstatus = 2
            # save the result
            results[testid] = {"result": stat, "note": note, "data": data}
Example #4
0
	def __runTests(self, id, fqdns, nslist, level):
		"""
		Run all enabled tests bellow given level.
		"""
		# fool the system if testmode is turned on
		if self.testmode:
			level = 0
		results = {}
		overallstatus = 0 # by default we assume that all tests were passed
		# perform all enabled tests (the ids must be sorted!)
		testkeys = self.testsuite.keys()
		testkeys.sort()
		for testid in testkeys:
			test = self.testsuite[testid]
			# check level
			if test["level"] > level:
				self.l.log(self.l.DEBUG, "<%d> Omitting test '%s' becauseof its "
						"level." % (id, test["name"]))
				continue
			# check prerequisities
			req_ok = True
			for req in test["requires"]:
				# the test might not be done if it was disabled
				try:
					deptest = results[req]
				except KeyError, e:
					self.l.log(self.l.WARNING, "<%d> Test '%s' depends on a "
							"test which is disabled." % (id, test["name"]))
					continue
				# check the result of test on which we depend
				# (unknown status is considered as if the test failed)
				if results[req]["result"] != 0:
					self.l.log(self.l.DEBUG, "<%d> Omitting test '%s' becauseof "
							"not fulfilled prerequisity." % (id, test["name"]))
					req_ok = False
					break
			if not req_ok:
				# prerequisities were not satisfied
				continue
			if (test["need_domain"] == 1 or test["need_domain"] == 3) and not fqdns:
				# list of domains is required for the test but is not given
				self.l.log(self.l.DEBUG, "<%d> Omitting test '%s' because "
						"no domains are provided." % (id, test["name"]))
				continue
			#
			# command scheduled for execution has following format:
			#    /scriptdir/script nsFqdn,ipaddr1,ipaddr2,...
			# last part is repeated as many times as many there are nameservers.
			# If test requires a domain name(s), they are supplied on stdin.
			#
			# ip addresses are provided only if the GLUE record is needed,
			# the test whether the ip addresses are needed is done here, not in
			# external test script, and all subsequent external tests use the
			# result of this pre-test. From users point of it is just another
			# ussual test.
			#
			# We recognize the GLUE test by empty string in script field
			if not test["script"]:
				stat = 0
				for ns in nslist:
					addrs = nslist[ns]
					data = ''
					glue_needed = False
					for fqdn in fqdns:
						if ns.endswith(fqdn):
							glue_needed = True
							if not addrs:
								data += " %s" % fqdn
					# errors (missing glue) goes to 'data'
					if data:
						self.l.log(self.l.DEBUG, "<%d> Missing glue for ns '%s'"
								% (id, ns))
						data = ns + data
						stat = 1
					# cancel not needed glue
					if not glue_needed and addrs:
						self.l.log(self.l.DEBUG, "<%d> Extra glue by ns '%s' "
								"cancelled" % (id, ns))
						nslist[ns] = []
				note = ''
			else:
				cmd = "%s%s" % (self.scriptdir, test["script"])
				for ns in nslist:
					addrs = nslist[ns]
					cmd += " %s" % ns
					for addr in addrs:
						cmd += ",%s" % addr
				# decide if list of domains is needed
				stdin = ''
				if test["need_domain"] != 0:
					# decide if test need only signed domains
					if test["need_domain"] == 3:
						# append configuration parameters to command string
						cmd += " %s %s" % (self.drill, self.trusted_key)
						# select only signed domains
						list = [ item for item in fqdns if fqdns[item] ]
					else:
						list = fqdns
					# send space separated list of domain fqdns to stdin
					for fqdn in list:
						stdin += fqdn + ' '

				stat, data, note = runCommand(id, cmd, stdin, self.l)

			# Status values:
			#     0 ... test OK
			#     1 ... test failed
			#     2 ... unknown result
			if stat == 1 and overallstatus != 1:
				overallstatus = 1
			elif stat == 2 and overallstatus == 0:
				overallstatus = 2
			# save the result
			results[testid] = { "result" : stat, "note" : note, "data" : data }