[Dovecot] [Patch] Samba's proposed "ntlm_auth + winbind" support for dovecot-auth
The Samba team recommends to use their "ntlm_auth" command line helper
for "NTLM" and "GSS-SPNEGO" authentication. This helper interacts with
the Samba's winbind daemon, and this way can authenticate users against
NT or Active Directory windows domain.
Currently Dovecot can do "NTLM" authentication too, but just "locally" (against a local or sql database etc.).
I've made a patch (attached), which adds "ntlm_auth" (or "winbind") support for Dovecot.
The idea is to add two new authentication mechanisms: "mech_winbind_ntlm" and "mech_winbind_spnego". Both are coded in one additional file, "mech-winbind.c". An option "auth_ntlm_use_winbind" specifies whether to use the current implementation of ntlm, or do it by the "ntlm_auth" helper. "GSS-SPNEGO" always performed by the helper. Normally, "ntlm_auth" is invoked once, for all further requests.
Such a way, "ntlm_auth helper from the Samba package, interacting with
the Samba's winbind daemon", is used now by Squid, Apache and AFAIK some
other applications. It is "strongly recommended" by the Samba team, and
was already proposed even in this maillist 3 year ago (see f.e.
http://www.dovecot.org/list/dovecot/2004-September/004775.html ).
I hope there are no any serious performance issues for such a "complex way" -- f.e. with our web proxy, using this way, ~200 users do not feel any actual delays etc.
I've successfully tested this patch with NTLM against AD domain.
This patch can considerably improve the situation of "Email client on Windows desktop under Windows domain, but imap/pop at UNIX server".
Currently, windows users have to specify their "login/password" for email accounts manually. There is an "SPA" (Secure Password Authentication) alternative for them, where just the desktop's login is used transparently, but it cannot be used now, because Dovecot cannot perform NTLM against, say, Active Directory domain.
Since "dovecot-auth" daemon can be utilized by MTA as well (Postfix and other), the support of "ntlm_auth" in Dovecot can satisfy both SMTP and IMAP servers at UNIX side, and solve the issue completely.
Questions and requests: the helper's cmdline exactly ?
- I try to code things most close to used style, i.e. using Dovecot's memory-management and io-pipe routines etc., but could someone look at it and check whether I've missed something or not?
- Perhaps some names (of routines, modules) could be chosen better?
- Maybe some other options should be implemented, i.e. "auth_winbind_helper_ntlm" and "auth_winbind_helper_spnego" to specify
- Currently I strip domain part of the username returned, i.e. from "DOMAIN\user" just to "user". Maybe better add some option "auth_winbind_strip_domain" for this?
Certainly, it will be fine if someone else check it more, especially for "GSS-SPNEGO" which I cannot test for a while.
Regards, Dmitry Butskoy http://www.fedoraproject.org/wiki/DmitryButskoy
On Mon, 2007-07-02 at 16:19 +0400, Dmitry Butskoy wrote:
- I try to code things most close to used style, i.e. using Dovecot's memory-management and io-pipe routines etc., but could someone look at it and check whether I've missed something or not?
close() and dup2() errors should be reported, although they're unlikely.
ostream is a bit pointless there since you're doing only a single write. You could just use write_full().
- Maybe some other options should be implemented, i.e. "auth_winbind_helper_ntlm" and "auth_winbind_helper_spnego" to specify the helper's cmdline exactly ?
Yes.
- Currently I strip domain part of the username returned, i.e. from "DOMAIN\user" just to "user". Maybe better add some option "auth_winbind_strip_domain" for this?
What if you changed it to user@domain? Then you could use Dovecot's standard %n or %u variables.
There's one thing I'd want changed: make it non-blocking. Both input and output are currently blocking, so dovecot-auth is stuck while waiting for ntlm_auth to reply. I want to avoid this whenever possible (I don't ever want to see "authentication just gets stuck, why??" mails. "ntlm_auth timed out" message in log is much nicer).
I guess ntlm_auth can handle only a single session at a time? So this would pretty much require that you either implement some kind of a queue or execute multiple ntlm_auths. Or maybe both. Using auth worker processes would probably be best. Unfortunately that currently works only for passdbs and userdbs, not for mechanisms. I guess I could try changing this for v1.1, unless you want to try? :)
Timo Sirainen wrote:
- Currently I strip domain part of the username returned, i.e. from "DOMAIN\user" just to "user". Maybe better add some option "auth_winbind_strip_domain" for this?
What if you changed it to user@domain? Then you could use Dovecot's standard %n or %u variables.
AFAIK "user@domain" should have an actual form of "user@example.com", but "ntlm_auth" returns "EXAMPLE\user" in such a case, not "EXAMPLE.COM\user". At least for NTLM (against AD). OTOH the spnego could retrurn the actual "user@example.com", but it is still unknown to me :)
There's one thing I'd want changed: make it non-blocking. Both input and output are currently blocking, so dovecot-auth is stuck while waiting for ntlm_auth to reply. I want to avoid this whenever possible (I don't ever want to see "authentication just gets stuck, why??" mails. "ntlm_auth timed out" message in log is much nicer).
I guess ntlm_auth can handle only a single session at a time?
No.
Normally ntlm_auth invoked at once for ALL authentication sessions. It is a child process which lives all the time while the dovecot-auth is alive.
There's one thing I'd want changed: make it non-blocking
Still actual after the clarification above?..
So this would pretty much require that you either implement some kind of a queue
Currently I prefer to use blocking io, which provides such "a queue" de-facto. At least for initial implementation. (I hope "ntlm_auth" is fast enough).
But some timeout surely could be useful. How can it be implemented?
or execute multiple ntlm_auths.
Does the use of worker for, say, PAM userdb affects us here too?
Or maybe both. Using auth worker processes would probably be best. Unfortunately that currently works only for passdbs and userdbs, not for mechanisms. I guess I could try changing this for v1.1, unless you want to try? :)
It seems that I prefer you try it... :)
Does "blocking io" + "worker" look like the best way?
Regards Dmitry Butskoy
On Mon, 2007-07-02 at 20:19 +0400, Dmitry Butskoy wrote:
Timo Sirainen wrote:
- Currently I strip domain part of the username returned, i.e. from "DOMAIN\user" just to "user". Maybe better add some option "auth_winbind_strip_domain" for this?
What if you changed it to user@domain? Then you could use Dovecot's standard %n or %u variables.
AFAIK "user@domain" should have an actual form of "user@example.com", but "ntlm_auth" returns "EXAMPLE\user" in such a case, not "EXAMPLE.COM\user". At least for NTLM (against AD). OTOH the spnego could retrurn the actual "user@example.com", but it is still unknown to me :)
I don't think it's a problem even if it returned only user@example. %d then just expands to example. At least I think it's better than not having the domain at all.
There's one thing I'd want changed: make it non-blocking. Both input and output are currently blocking, so dovecot-auth is stuck while waiting for ntlm_auth to reply. I want to avoid this whenever possible (I don't ever want to see "authentication just gets stuck, why??" mails. "ntlm_auth timed out" message in log is much nicer).
I guess ntlm_auth can handle only a single session at a time?
No.
I mean simultaneously. Like you can't send multiple "begin/continue authentication" commands to it and then just wait until one of them finishes?
There's one thing I'd want changed: make it non-blocking
Still actual after the clarification above?..
Yep.
So this would pretty much require that you either implement some kind of a queue
Currently I prefer to use blocking io, which provides such "a queue" de-facto. At least for initial implementation. (I hope "ntlm_auth" is fast enough).
I think the problem isn't ntlm_auth itself, but that it also has to talk to AD. So any network problems there could leave it hanging.
But some timeout surely could be useful. How can it be implemented?
By using non-blocking I/O :)
or execute multiple ntlm_auths.
Does the use of worker for, say, PAM userdb affects us here too?
PAM is passdb. But anyway it shouldn't affect.
Or maybe both. Using auth worker processes would probably be best. Unfortunately that currently works only for passdbs and userdbs, not for mechanisms. I guess I could try changing this for v1.1, unless you want to try? :)
It seems that I prefer you try it... :)
Does "blocking io" + "worker" look like the best way?
I think so. Or some kind of a generic queue maybe.. Hmm. Doesn't GSSAPI have the exact same problem? I think it does. Maybe I could figure out something for them both.
Timo Sirainen wrote:
On Mon, 2007-07-02 at 20:19 +0400, Dmitry Butskoy wrote:
Timo Sirainen wrote:
- Currently I strip domain part of the username returned, i.e. from "DOMAIN\user" just to "user". Maybe better add some option "auth_winbind_strip_domain" for this?
What if you changed it to user@domain? Then you could use Dovecot's standard %n or %u variables.
AFAIK "user@domain" should have an actual form of "user@example.com", but "ntlm_auth" returns "EXAMPLE\user" in such a case, not "EXAMPLE.COM\user". At least for NTLM (against AD). OTOH the spnego could retrurn the actual "user@example.com", but it is still unknown to me :)
I don't think it's a problem even if it returned only user@example. %d then just expands to example. At least I think it's better than not having the domain at all.
I propose to have an option. For example, winbind already has "winbind use default domain" option, which cause (sometimes :) ) it to return usernames without domain part. It is useful at UNIX machines, where the actual UNIX username is just "user", not "DOMAIN\user" .
What happens when dovecot receives "DOMAIN\user" or "user@exmaple.com" form? How it determine then the local UNIX username?
There's one thing I'd want changed: make it non-blocking. Both input and output are currently blocking, so dovecot-auth is stuck while waiting for ntlm_auth to reply. I want to avoid this whenever possible (I don't ever want to see "authentication just gets stuck, why??" mails. "ntlm_auth timed out" message in log is much nicer).
I guess ntlm_auth can handle only a single session at a time?
No.
I mean simultaneously. Like you can't send multiple "begin/continue authentication" commands to it and then just wait until one of them finishes?
Oh, yep. You're right, only one at a time.
So this would pretty much require that you either implement some kind of a queue
Currently I prefer to use blocking io, which provides such "a queue" de-facto. At least for initial implementation. (I hope "ntlm_auth" is fast enough).
I think the problem isn't ntlm_auth itself, but that it also has to talk to AD. So any network problems there could leave it hanging.
OTOH winbind daemon can "cache" something now...
Hmm.. Apache seems to use blocking-io. I'll look later for Squid.
or execute multiple ntlm_auths.
Does the use of worker for, say, PAM userdb affects us here too?
PAM is passdb. But anyway it shouldn't affect.
I mean that when we use worker for some userdb, we actually have several dovecot-auth processes, hence several ntlm_auth as well. Am I right?
~buc
Version 2 of the patch.
Options "auth_winbind_helper_ntlm" and "auth_winbind_helper_spnego" added.
(The default values for them are actually placed in mech-winbind.c .
Hmm... )
dup2() and fork() errors reported.
Don't want to report close() errors here :)
Usernames in form "DOMAIN\user" are transformed to "user@DOMAIN"
(call to auth_request_set_username() is used).
Still don't want to simplify output by "write_full()" -- for more clean
code it is better to do input/output in a similar style.
Does an extra "ostream" grab a significant amount of resources?
I've not found an easy way how the io can be re-coded "non-blocking". I
hope in the case of the network failures (where Dovecot is reachable,
but domain controller not) the winbind daemon itself can do timeouts
(which will be "transferred" then to the pending ntlm_auth etc.)
Maybe to leave things as is (i.e. blocking), and later some new common
code (a worker for mech) could help for all such mechanisms (including
gssapi) ?
Regards,
Dmitry Butskoy
diff -Nrbu dovecot-1.0.1/src/auth/Makefile.am dovecot-1.0.1-OK/src/auth/Makefile.am
--- dovecot-1.0.1/src/auth/Makefile.am 2007-05-19 15:14:04.000000000 +0400
+++ dovecot-1.0.1-OK/src/auth/Makefile.am 2007-07-03 17:53:14.000000000 +0400
@@ -56,6 +56,7 @@
mech-cram-md5.c \
mech-digest-md5.c \
mech-ntlm.c \
+ mech-winbind.c \
mech-gssapi.c \
mech-rpa.c \
mech-apop.c \
diff -Nrbu dovecot-1.0.1/src/auth/Makefile.in dovecot-1.0.1-OK/src/auth/Makefile.in
--- dovecot-1.0.1/src/auth/Makefile.in 2007-06-14 16:02:13.000000000 +0400
+++ dovecot-1.0.1-OK/src/auth/Makefile.in 2007-07-03 17:53:41.000000000 +0400
@@ -78,6 +78,7 @@
mech.$(OBJEXT) mech-anonymous.$(OBJEXT) mech-plain.$(OBJEXT) \
mech-login.$(OBJEXT) mech-cram-md5.$(OBJEXT) \
mech-digest-md5.$(OBJEXT) mech-ntlm.$(OBJEXT) \
+ mech-winbind.$(OBJEXT) \
mech-gssapi.$(OBJEXT) mech-rpa.$(OBJEXT) mech-apop.$(OBJEXT) \
passdb.$(OBJEXT) passdb-blocking.$(OBJEXT) \
passdb-bsdauth.$(OBJEXT) passdb-cache.$(OBJEXT) \
@@ -325,6 +326,7 @@
mech-cram-md5.c \
mech-digest-md5.c \
mech-ntlm.c \
+ mech-winbind.c \
mech-gssapi.c \
mech-rpa.c \
mech-apop.c \
@@ -494,6 +496,7 @@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech-ntlm.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech-plain.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech-rpa.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech-winbind.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mycrypt.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/passdb-blocking.Po@am__quote@
diff -Nrbu dovecot-1.0.1/src/auth/mech-winbind.c dovecot-1.0.1-OK/src/auth/mech-winbind.c
--- dovecot-1.0.1/src/auth/mech-winbind.c 1970-01-01 03:00:00.000000000 +0300
+++ dovecot-1.0.1-OK/src/auth/mech-winbind.c 2007-07-03 17:53:14.000000000 +0400
@@ -0,0 +1,298 @@
+/*
+ * NTLM and Negotiate authentication mechanisms,
+ * using Samba winbind daemon
+ *
+ * Copyright (c) 2007 Dmitry Butskoy
Version 3 of the patch.
This version sent also to Samba team, in hope they have more chances to
test it properly.
Dmitry Butskoy
diff -Nrbu dovecot-1.0.1/dovecot-example.conf dovecot-1.0.1-OK/dovecot-example.conf
--- dovecot-1.0.1/dovecot-example.conf 2007-06-13 04:47:39.000000000 +0400
+++ dovecot-1.0.1-OK/dovecot-example.conf 2007-07-06 18:40:35.000000000 +0400
@@ -748,6 +748,27 @@
# default (usually /etc/krb5.keytab) if not specified.
#auth_krb5_keytab =
+# If you have "ntlm_auth" helper and running winbind daemon
+# (both from Samba package, see http://www.samba.org),
+# you can use it for "ntlm" and "gss-spnego" authentication mechanisms.
+# It is useful when you need to authenticate users against a Windows domain
+# (either AD or NT).
+#
+# The usernames, returned by winbind, can contain some domain part
+# (either "DOMAIN\user" or "user@example.com"). Such usernames
+# are always transformed to the form of "user@domain". To strip domain part
+# (to obtain correspond local username, for example), specify
+# auth_username_format = %n
+
+# Use "ntlm-auth + winbind" way for "ntlm" mechanism,
+# instead of own Dovecot's ntlm support ("gss-spnego" always requires winbind)
+#auth_ntlm_use_winbind = no
+
+# Specify an alternate path and command line for the helper for "ntlm"
+#auth_winbind_helper_ntlm = /usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp"
+# The same for "gss-spnego"
+#auth_winbind_helper_spnego = /usr/bin/ntlm_auth --helper-protocol=gss-spnego
+
auth default {
# Space separated list of wanted authentication mechanisms:
# plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi
diff -Nrbu dovecot-1.0.1/src/auth/Makefile.am dovecot-1.0.1-OK/src/auth/Makefile.am
--- dovecot-1.0.1/src/auth/Makefile.am 2007-05-19 15:14:04.000000000 +0400
+++ dovecot-1.0.1-OK/src/auth/Makefile.am 2007-07-03 17:53:14.000000000 +0400
@@ -56,6 +56,7 @@
mech-cram-md5.c \
mech-digest-md5.c \
mech-ntlm.c \
+ mech-winbind.c \
mech-gssapi.c \
mech-rpa.c \
mech-apop.c \
diff -Nrbu dovecot-1.0.1/src/auth/Makefile.in dovecot-1.0.1-OK/src/auth/Makefile.in
--- dovecot-1.0.1/src/auth/Makefile.in 2007-06-14 16:02:13.000000000 +0400
+++ dovecot-1.0.1-OK/src/auth/Makefile.in 2007-07-03 17:53:41.000000000 +0400
@@ -78,6 +78,7 @@
mech.$(OBJEXT) mech-anonymous.$(OBJEXT) mech-plain.$(OBJEXT) \
mech-login.$(OBJEXT) mech-cram-md5.$(OBJEXT) \
mech-digest-md5.$(OBJEXT) mech-ntlm.$(OBJEXT) \
+ mech-winbind.$(OBJEXT) \
mech-gssapi.$(OBJEXT) mech-rpa.$(OBJEXT) mech-apop.$(OBJEXT) \
passdb.$(OBJEXT) passdb-blocking.$(OBJEXT) \
passdb-bsdauth.$(OBJEXT) passdb-cache.$(OBJEXT) \
@@ -325,6 +326,7 @@
mech-cram-md5.c \
mech-digest-md5.c \
mech-ntlm.c \
+ mech-winbind.c \
mech-gssapi.c \
mech-rpa.c \
mech-apop.c \
@@ -494,6 +496,7 @@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech-ntlm.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech-plain.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech-rpa.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech-winbind.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mech.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mycrypt.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/passdb-blocking.Po@am__quote@
diff -Nrbu dovecot-1.0.1/src/auth/mech-winbind.c dovecot-1.0.1-OK/src/auth/mech-winbind.c
--- dovecot-1.0.1/src/auth/mech-winbind.c 1970-01-01 03:00:00.000000000 +0300
+++ dovecot-1.0.1-OK/src/auth/mech-winbind.c 2007-07-06 17:53:00.000000000 +0400
@@ -0,0 +1,310 @@
+/*
+ * NTLM and Negotiate authentication mechanisms,
+ * using Samba winbind daemon
+ *
+ * Copyright (c) 2007 Dmitry Butskoy
participants (2)
-
Dmitry Butskoy
-
Timo Sirainen