s3-librpc Call SPENGO/GSSAPI via the auth_generic layer and gensec
[kai/samba.git] / source3 / rpc_server / dcesrv_gssapi.c
index c8a015e066c38b70e1c4e1ccdae37d3df5d9e9d3..be97a64cb427a1f6767845ca932744581ece3fae 100644 (file)
 #include "includes.h"
 #include "rpc_server/dcesrv_gssapi.h"
 #include "../librpc/gen_ndr/ndr_krb5pac.h"
+#include "../lib/tsocket/tsocket.h"
 #include "librpc/crypto/gse.h"
 #include "auth.h"
-
+#ifdef HAVE_KRB5
+#include "libcli/auth/krb5_wrap.h"
+#endif
 NTSTATUS gssapi_server_auth_start(TALLOC_CTX *mem_ctx,
                                  bool do_sign,
                                  bool do_seal,
@@ -41,13 +44,8 @@ NTSTATUS gssapi_server_auth_start(TALLOC_CTX *mem_ctx,
        }
 
        /* Let's init the gssapi machinery for this connection */
-       /* passing a NULL server name means the server will try
-        * to accept any connection regardless of the name used as
-        * long as it can find a decryption key */
-       /* by passing NULL, the code will attempt to set a default
-        * keytab based on configuration options */
        status = gse_init_server(mem_ctx, do_sign, do_seal,
-                                add_flags, NULL, &gse_ctx);
+                                add_flags, &gse_ctx);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("Failed to init dcerpc gssapi server (%s)\n",
                          nt_errstr(status)));
@@ -101,18 +99,13 @@ NTSTATUS gssapi_server_check_flags(struct gse_context *gse_ctx)
 
 NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
                                     TALLOC_CTX *mem_ctx,
-                                    struct client_address *client_id,
-                                    struct auth_serversupplied_info **server_info)
+                                    const struct tsocket_address *remote_address,
+                                    struct auth_session_info **session_info)
 {
        TALLOC_CTX *tmp_ctx;
-       DATA_BLOB auth_data;
-       time_t tgs_authtime;
-       NTTIME tgs_authtime_nttime;
-       DATA_BLOB pac;
-       struct PAC_DATA *pac_data;
-       struct PAC_LOGON_NAME *logon_name = NULL;
+       DATA_BLOB pac_blob;
+       struct PAC_DATA *pac_data = NULL;
        struct PAC_LOGON_INFO *logon_info = NULL;
-       enum ndr_err_code ndr_err;
        unsigned int i;
        bool is_mapped;
        bool is_guest;
@@ -120,16 +113,17 @@ NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
        char *ntuser;
        char *ntdomain;
        char *username;
+       char *rhost;
        struct passwd *pw;
        NTSTATUS status;
-       bool bret;
+       int rc;
 
        tmp_ctx = talloc_new(mem_ctx);
        if (!tmp_ctx) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = gse_get_authz_data(gse_ctx, tmp_ctx, &auth_data);
+       status = gse_get_pac_blob(gse_ctx, tmp_ctx, &pac_blob);
        if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
                /* TODO: Fetch user by principal name ? */
                status = NT_STATUS_ACCESS_DENIED;
@@ -139,37 +133,22 @@ NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
                goto done;
        }
 
-       bret = unwrap_pac(tmp_ctx, &auth_data, &pac);
-       if (!bret) {
-               DEBUG(1, ("Failed to unwrap PAC\n"));
-               status = NT_STATUS_ACCESS_DENIED;
-               goto done;
-       }
-
-       status = gse_get_client_name(gse_ctx, tmp_ctx, &princ_name);
+#ifdef HAVE_KRB5
+       status = kerberos_decode_pac(tmp_ctx,
+                                    pac_blob,
+                                    NULL, NULL, NULL, NULL, 0, &pac_data);
+#else
+       status = NT_STATUS_ACCESS_DENIED;
+#endif
+       data_blob_free(&pac_blob);
        if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
 
-       status = gse_get_authtime(gse_ctx, &tgs_authtime);
+       status = gse_get_client_name(gse_ctx, tmp_ctx, &princ_name);
        if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
-       unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
-
-       pac_data = talloc_zero(tmp_ctx, struct PAC_DATA);
-       if (!pac_data) {
-               status = NT_STATUS_NO_MEMORY;
-               goto done;
-       }
-
-       ndr_err = ndr_pull_struct_blob(&pac, pac_data, pac_data,
-                               (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               DEBUG(1, ("Failed to parse the PAC for %s\n", princ_name));
-               status = ndr_map_error2ntstatus(ndr_err);
-               goto done;
-       }
 
        /* get logon name and logon info */
        for (i = 0; i < pac_data->num_buffers; i++) {
@@ -182,9 +161,6 @@ NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
                        }
                        logon_info = data_buf->info->logon_info.info;
                        break;
-               case PAC_TYPE_LOGON_NAME:
-                       logon_name = &data_buf->info->logon_name;
-                       break;
                default:
                        break;
                }
@@ -194,27 +170,24 @@ NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
                status = NT_STATUS_NOT_FOUND;
                goto done;
        }
-       if (!logon_name) {
-               DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
-               status = NT_STATUS_NOT_FOUND;
-               goto done;
-       }
 
-       /* check time */
-       if (tgs_authtime_nttime != logon_name->logon_time) {
-               DEBUG(1, ("Logon time mismatch between ticket and PAC!\n"
-                         "PAC Time = %s | Ticket Time = %s\n",
-                         nt_time_string(tmp_ctx, logon_name->logon_time),
-                         nt_time_string(tmp_ctx, tgs_authtime_nttime)));
-               status = NT_STATUS_ACCESS_DENIED;
+       rc = get_remote_hostname(remote_address,
+                                &rhost,
+                                tmp_ctx);
+       if (rc < 0) {
+               status = NT_STATUS_NO_MEMORY;
                goto done;
        }
+       if (strequal(rhost, "UNKNOWN")) {
+               rhost = tsocket_address_inet_addr_string(remote_address,
+                                                        tmp_ctx);
+               if (rhost == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto done;
+               }
+       }
 
-       /* TODO: Should we check princ_name against account_name in
-        * logon_name ? Are they supposed to be identical, or can an
-        * account_name be different from the UPN ? */
-
-       status = get_user_from_kerberos_info(tmp_ctx, client_id->name,
+       status = get_user_from_kerberos_info(tmp_ctx, rhost,
                                             princ_name, logon_info,
                                             &is_mapped, &is_guest,
                                             &ntuser, &ntdomain,
@@ -231,7 +204,7 @@ NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
        status = make_session_info_krb5(mem_ctx,
                                        ntuser, ntdomain, username, pw,
                                        logon_info, is_guest, is_mapped, NULL /* No session key for now */,
-                                       server_info);
+                                       session_info);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
                          nt_errstr(status)));
@@ -240,7 +213,7 @@ NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
        }
 
        DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
-                 ntuser, ntdomain, client_id->name));
+                 ntuser, ntdomain, rhost));
 
        status = NT_STATUS_OK;