auth/kerberos: add gensec_gssapi_try_kerberos()
authorStefan Metzmacher <metze@samba.org>
Thu, 29 Dec 2016 16:04:17 +0000 (17:04 +0100)
committerStefan Metzmacher <metze@samba.org>
Mon, 18 Feb 2019 12:38:24 +0000 (13:38 +0100)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
auth/kerberos/gensec_gssapi_helper.c [new file with mode: 0644]
auth/kerberos/gensec_gssapi_helper.h [new file with mode: 0644]
auth/kerberos/wscript_build

diff --git a/auth/kerberos/gensec_gssapi_helper.c b/auth/kerberos/gensec_gssapi_helper.c
new file mode 100644 (file)
index 0000000..3fdbf01
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+   Unix SMB/CIFS implementation.
+   GSSAPI/GENSEC helper functions
+
+   Copyright (C) Stefan Metzmacher 2016
+
+   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; either version 3 of the License, 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "system/gssapi.h"
+#include "auth/credentials/credentials.h"
+#include "auth/gensec/gensec.h"
+#include "auth/kerberos/gensec_gssapi_helper.h"
+#include "lib/util/util_net.h"
+
+NTSTATUS gensec_gssapi_try_kerberos(struct gensec_security *gensec_security)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct cli_credentials *creds = gensec_get_credentials(gensec_security);
+       const char *target_principal = gensec_get_target_principal(gensec_security);
+       const char *target_hostname = gensec_get_target_hostname(gensec_security);
+       const char *user_principal = NULL;
+       const char *user_account = NULL;
+       const char *user_domain = NULL;
+       const char *realm = NULL;
+       enum credentials_use_kerberos krb5_state;
+       bool try_kerberos = false;
+       bool auth_requested = true;
+
+       auth_requested = cli_credentials_authentication_requested(creds);
+       if (auth_requested) {
+               user_principal = cli_credentials_get_principal(creds, frame);
+               if (user_principal == NULL) {
+                       TALLOC_FREE(frame);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               realm = cli_credentials_get_realm(creds);
+       }
+       user_account = cli_credentials_get_username(creds);
+       user_domain = cli_credentials_get_domain(creds);
+
+       krb5_state = cli_credentials_get_kerberos_state(creds);
+
+       if (krb5_state != CRED_DONT_USE_KERBEROS) {
+               try_kerberos = true;
+       }
+
+       if (!auth_requested) {
+               try_kerberos = false;
+       } else if (target_principal != NULL) {
+               /* noop */
+       } else if (target_hostname == NULL) {
+               try_kerberos = false;
+       } else if (is_ipaddress(target_hostname)) {
+               try_kerberos = false;
+       } else if (strequal(target_hostname, "localhost")) {
+               try_kerberos = false;
+       } else if (strequal(target_hostname, "*SMBSERVER")) {
+               try_kerberos = false;
+       }
+
+       if (krb5_state == CRED_MUST_USE_KERBEROS && !try_kerberos) {
+               DEBUG(0, ("Kerberos auth with '%s' (%s\\%s %s) to access "
+                         "'%s' not possible\n",
+                         user_principal, user_domain, user_account, realm,
+                         target_principal ? target_principal : target_hostname));
+               TALLOC_FREE(frame);
+               return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT;
+       }
+
+       if (!try_kerberos) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       TALLOC_FREE(frame);
+       return NT_STATUS_OK;
+}
diff --git a/auth/kerberos/gensec_gssapi_helper.h b/auth/kerberos/gensec_gssapi_helper.h
new file mode 100644 (file)
index 0000000..3664127
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+   Unix SMB/CIFS implementation.
+   GSSAPI/GENSEC helper functions
+
+   Copyright (C) Stefan Metzmacher 2016
+
+   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; either version 3 of the License, 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef AUTH_KERBEROS_GENSEC_GSSAPI_HELPER_H
+#define AUTH_KERBEROS_GENSEC_GSSAPI_HELPER_H 1
+
+struct gensec_security;
+
+NTSTATUS gensec_gssapi_try_kerberos(struct gensec_security *gensec_security);
+
+#endif /* AUTH_KERBEROS_GENSEC_GSSAPI_HELPER_H */
index 1fa1b51138d9528b4ea181c9a78dbaa2569d2087..acba037e52e4a20587a4f7f6ef281dd522155e76 100644 (file)
@@ -2,3 +2,7 @@
 bld.SAMBA_SUBSYSTEM('KRB5_PAC',
                     source='gssapi_pac.c kerberos_pac.c gssapi_helper.c',
                     deps='gssapi_krb5 ndr-krb5pac krb5samba')
+
+bld.SAMBA_SUBSYSTEM('GENSEC_GSSAPI_HELPER',
+                    source='gensec_gssapi_helper.c',
+                    deps='gssapi_krb5 CREDENTIALS_KRB5')