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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# Copyright 2010-2013 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 or, at your option, any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
"""
SASL authentication daemon for multi-domain Kolab deployments.
The SASL authentication daemon can use the domain name space or realm
in the login credentials to determine the backend authentication
database, and authenticate the credentials supplied against that
backend.
"""
from optparse import OptionParser
from ConfigParser import SafeConfigParser
import os
import shutil
import time
import traceback
import pykolab
from pykolab import utils
from pykolab.auth import Auth
from pykolab.constants import *
from pykolab.translate import _
log = pykolab.getLogger('saslauthd')
conf = pykolab.getConf()
class SASLAuthDaemon(object):
def __init__(self):
daemon_group = conf.add_cli_parser_option_group(_("Daemon Options"))
daemon_group.add_option(
"--fork",
dest = "fork_mode",
action = "store_true",
default = False,
help = _("Fork to the background.")
)
daemon_group.add_option(
"-p",
"--pid-file",
dest = "pidfile",
action = "store",
default = "/var/run/kolab-saslauthd/kolab-saslauthd.pid",
help = _("Path to the PID file to use.")
)
daemon_group.add_option(
"-u",
"--user",
dest = "process_username",
action = "store",
default = "kolab",
help = _("Run as user USERNAME"),
metavar = "USERNAME"
)
daemon_group.add_option(
"-g",
"--group",
dest = "process_groupname",
action = "store",
default = "kolab",
help = _("Run as group GROUPNAME"),
metavar = "GROUPNAME"
)
conf.finalize_conf()
utils.ensure_directory(
os.path.dirname(conf.pidfile),
conf.process_username,
conf.process_groupname
)
self.thread_count = 0
def run(self):
"""
Run the SASL authentication daemon.
"""
exitcode = 0
try:
pid = 1
if conf.fork_mode:
pid = os.fork()
if pid == 0:
self.thread_count += 1
log.remove_stdout_handler()
self.set_signal_handlers()
self.write_pid()
self.do_saslauthd()
elif not conf.fork_mode:
self.do_saslauthd()
except SystemExit, e:
exitcode = e
except KeyboardInterrupt:
exitcode = 1
log.info(_("Interrupted by user"))
except AttributeError, e:
exitcode = 1
traceback.print_exc()
print >> sys.stderr, _("Traceback occurred, please report a bug at http://bugzilla.kolabsys.com")
except TypeError, e:
exitcode = 1
traceback.print_exc()
log.error(_("Type Error: %s") % e)
except:
exitcode = 2
traceback.print_exc()
print >> sys.stderr, _("Traceback occurred, please report a bug at http://bugzilla.kolabsys.com")
sys.exit(exitcode)
def do_saslauthd(self):
"""
Create the actual listener socket, and handle the authentication.
The actual authentication handling is passed on to the appropriate
backend authentication classes through the more generic Auth().
"""
import binascii
import socket
import struct
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
utils.ensure_directory(
'/var/run/saslauthd/',
conf.process_username,
conf.process_groupname
)
# TODO: The saslauthd socket path could be a setting.
try:
os.remove('/var/run/saslauthd/mux')
except:
# TODO: Do the "could not remove, could not start" dance
pass
s.bind('/var/run/saslauthd/mux')
os.chmod('/var/run/saslauthd/mux', 0777)
s.listen(5)
while 1:
(clientsocket, address) = s.accept()
received = clientsocket.recv(4096)
login = []
start = 0
end = 2
while end < len(received):
(length,) = struct.unpack("!H", received[start:end])
start += 2
end += length
(value,) = struct.unpack("!%ds" % (length), received[start:end])
start += length
end = start + 2
login.append(value)
if len(login) == 4:
realm = login[3]
elif len(login[0].split('@')) > 1:
realm = login[0].split('@')[1]
else:
realm = conf.get('kolab', 'primary_domain')
auth = Auth(domain=realm)
auth.connect()
success = False
try:
success = auth.authenticate(login)
except:
success = False
if success:
# #1170: Catch broken pipe error (incomplete authentication request)
try:
clientsocket.send(struct.pack("!H2s", 2, "OK"))
except:
pass
else:
# #1170: Catch broken pipe error (incomplete authentication request)
try:
clientsocket.send(struct.pack("!H2s", 2, "NO"))
except:
pass
clientsocket.close()
auth.disconnect()
def reload_config(self, *args, **kw):
pass
def remove_pid(self, *args, **kw):
if os.access(conf.pidfile, os.R_OK):
os.remove(conf.pidfile)
raise SystemExit
def set_signal_handlers(self):
import signal
signal.signal(signal.SIGHUP, self.reload_config)
signal.signal(signal.SIGTERM, self.remove_pid)
def write_pid(self):
pid = os.getpid()
fp = open(conf.pidfile,'w')
fp.write("%d\n" % (pid))
fp.close()
|