summaryrefslogtreecommitdiffstats
path: root/py-bin/mail_auth.py
blob: 27607e82d703c29e56ad0ad18bf2b2ac42703791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#mail authentication

import smtplib, hmac, sha, re, time, logging
from utils import empy_render
import subprocess
import config

class MailAuthMixIn:
	def mail_form(self, req):
		self.render_template(req, "mail_form.em")
	mail_form.web_callable = True
	
	def mail_process(self, req):
		email = req.params.get("email", "")
		
		success, status_or_token = self.jman.generate_token(email)
		if not success:
			self.render_template(req, "mail_error.em", dict(reason=status_or_token))
			return
		token = status_or_token
		
		if not self.__send_mail(email, token):
			msg = "Unerwarteter Fehler beim Versenden des Mails."
			self.render_template(req, "mail_trylater.em", dict(reason=msg))
			return
		
		success, status = self.jman.prepare_user(email, token)
		if not success:
			self.render_template(req, "mail_error.em", dict(reason=status))
			return
		
		msg = "Mail erfolgreich versandt."
		self.render_template(req, "mail_success.em", dict(status=msg, email=email))
	mail_process.web_callable = True
	
	def mail_pw_form(self, req):
		user_id = req.params.get("uid", "")
		token = req.params.get("tok", "")
		last_error = req.params.get("error", "")
		
		token_ok, status_or_user = self.jman.validate_token(user_id, token)
		if token_ok:
			user_id = status_or_user.get_user_id()
			self.render_template(req, "set_pw_form.em",
				dict(user_id=user_id, error=last_error, command="mail_pw_process"))
		else:
			self.error_page(req, status_or_user)
	mail_pw_form.web_callable = True
	
	def mail_pw_process(self, req):
		password = req.params.get("password", "")
		password2 = req.params.get("password2", "")
		
		ok, status = self.jman.is_acceptable_password(password, password2)
		if not ok:
			url = self.make_url([("cmd","mail_pw_form"), ("error", status)])
			self.redirect_to(req, url)
			return
		
		ok, status_or_user = self.jman.activate_user(password)
		if ok:
			self.redirect_to(req, config.script_url + "?cmd=setup_main")
		else:
			self.error_page(req, status_or_user)
	mail_pw_process.web_callable = True
	
	def __send_mail(self, email, token):
		date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
		url = self.make_url([("cmd","mail_pw_form"), ("uid",email), ("tok",token)])
		ctx = dict(from_addr=config.mail_from_addr, to_addr=email,
			date=date, reg_link=url)
		message = empy_render("mail_message.em", context_raw = ctx)
		
		if hasattr(config,"use_sendmail") and config.use_sendmail:
			return self.__sendmail_send_mail(email, token, message)
		else:
			return self.__smtp_send_mail(email, token, message)

	def __smtp_send_mail(self, email, token, message):
		try:
			server = smtplib.SMTP(config.smtp_server)
			server.ehlo()
			server.starttls()
			server.ehlo()
			if hasattr(config, "smtp_pass"):
				server.login(config.smtp_user, config.smtp_pass)
			server.sendmail(config.mail_from_addr, [email], message)
			server.quit()
			logging.info("Sent mail to %s." % email)
		except Exception, e:
			logging.error("Error sending mail to %s: %s" % (email, str(e)))
			return False
		
		return True
	
	def __sendmail_send_mail(self, email, token, message):
		try:
			params = ["/usr/sbin/sendmail", "-f" + config.mail_from_addr, email]
			p = subprocess.Popen(params, stdin=subprocess.PIPE)
			p.communicate(message)
			result = p.wait()
			if result != 0:
				logging.error("Error queuing mail for %s: return code %s" % 
					(email, str(result)))
				return False
			logging.info("Queued mail for %s." % email)
		except Exception, e:
			logging.error("Error queuing mail for %s: %s" % (email, str(e)))
			return False
		return True