LCOV - code coverage report
Current view: top level - auth/ntlmssp - ntlmssp.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 110 141 78.0 %
Date: 2024-02-29 22:57:05 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/Netbios implementation.
       3             :    Version 3.0
       4             :    handle NLTMSSP, client server side parsing
       5             : 
       6             :    Copyright (C) Andrew Tridgell      2001
       7             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
       8             :    Copyright (C) Stefan Metzmacher 2005
       9             : 
      10             :    This program is free software; you can redistribute it and/or modify
      11             :    it under the terms of the GNU General Public License as published by
      12             :    the Free Software Foundation; either version 3 of the License, or
      13             :    (at your option) any later version.
      14             : 
      15             :    This program is distributed in the hope that it will be useful,
      16             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             :    GNU General Public License for more details.
      19             : 
      20             :    You should have received a copy of the GNU General Public License
      21             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      22             : */
      23             : 
      24             : struct auth_session_info;
      25             : 
      26             : #include "includes.h"
      27             : #include <tevent.h>
      28             : #include "lib/util/tevent_ntstatus.h"
      29             : #include "auth/ntlmssp/ntlmssp.h"
      30             : #include "auth/ntlmssp/ntlmssp_private.h"
      31             : #include "../libcli/auth/libcli_auth.h"
      32             : #include "librpc/gen_ndr/ndr_dcerpc.h"
      33             : #include "auth/gensec/gensec.h"
      34             : #include "auth/gensec/gensec_internal.h"
      35             : 
      36             : #undef DBGC_CLASS
      37             : #define DBGC_CLASS DBGC_AUTH
      38             : 
      39             : /**
      40             :  * Callbacks for NTLMSSP - for both client and server operating modes
      41             :  *
      42             :  */
      43             : 
      44             : static const struct ntlmssp_callbacks {
      45             :         enum ntlmssp_role role;
      46             :         enum ntlmssp_message_type command;
      47             :         NTSTATUS (*sync_fn)(struct gensec_security *gensec_security,
      48             :                             TALLOC_CTX *out_mem_ctx,
      49             :                             DATA_BLOB in, DATA_BLOB *out);
      50             :         struct tevent_req *(*send_fn)(TALLOC_CTX *mem_ctx,
      51             :                                       struct tevent_context *ev,
      52             :                                       struct gensec_security *gensec_security,
      53             :                                       const DATA_BLOB in);
      54             :         NTSTATUS (*recv_fn)(struct tevent_req *req,
      55             :                             TALLOC_CTX *out_mem_ctx,
      56             :                             DATA_BLOB *out);
      57             : } ntlmssp_callbacks[] = {
      58             :         {
      59             :                 .role           = NTLMSSP_CLIENT,
      60             :                 .command        = NTLMSSP_INITIAL,
      61             :                 .sync_fn        = ntlmssp_client_initial,
      62             :         },{
      63             :                 .role           = NTLMSSP_CLIENT,
      64             :                 .command        = NTLMSSP_NEGOTIATE,
      65             :                 .sync_fn        = gensec_ntlmssp_resume_ccache,
      66             :         },{
      67             :                 .role           = NTLMSSP_SERVER,
      68             :                 .command        = NTLMSSP_NEGOTIATE,
      69             :                 .sync_fn        = gensec_ntlmssp_server_negotiate,
      70             :         },{
      71             :                 .role           = NTLMSSP_CLIENT,
      72             :                 .command        = NTLMSSP_CHALLENGE,
      73             :                 .sync_fn        = ntlmssp_client_challenge,
      74             :         },{
      75             :                 .role           = NTLMSSP_SERVER,
      76             :                 .command        = NTLMSSP_AUTH,
      77             :                 .send_fn        = ntlmssp_server_auth_send,
      78             :                 .recv_fn        = ntlmssp_server_auth_recv,
      79             :         }
      80             : };
      81             : 
      82             : 
      83      153893 : static NTSTATUS gensec_ntlmssp_update_find(struct gensec_security *gensec_security,
      84             :                                            struct gensec_ntlmssp_context *gensec_ntlmssp,
      85             :                                            const DATA_BLOB input, uint32_t *idx)
      86             : {
      87         740 :         uint32_t ntlmssp_command;
      88         740 :         uint32_t i;
      89             : 
      90      153893 :         if (gensec_ntlmssp->ntlmssp_state->expected_state == NTLMSSP_DONE) {
      91             :                 /* We are strict here because other modules, which we
      92             :                  * don't fully control (such as GSSAPI) are also
      93             :                  * strict, but are tested less often */
      94             : 
      95           0 :                 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
      96           0 :                 return NT_STATUS_INVALID_PARAMETER;
      97             :         }
      98             : 
      99      153893 :         if (!input.length) {
     100       39347 :                 switch (gensec_ntlmssp->ntlmssp_state->role) {
     101       39347 :                 case NTLMSSP_CLIENT:
     102       39347 :                         if (gensec_ntlmssp->ntlmssp_state->resume_ccache) {
     103             :                                 /*
     104             :                                  * make sure gensec_ntlmssp_resume_ccache()
     105             :                                  * will be called
     106             :                                  */
     107           0 :                                 ntlmssp_command = NTLMSSP_NEGOTIATE;
     108       39051 :                                 break;
     109             :                         }
     110             : 
     111       39347 :                         ntlmssp_command = NTLMSSP_INITIAL;
     112       39347 :                         break;
     113           0 :                 case NTLMSSP_SERVER:
     114           0 :                         if (gensec_security->want_features & GENSEC_FEATURE_DATAGRAM_MODE) {
     115             :                                 /* 'datagram' mode - no neg packet */
     116           0 :                                 ntlmssp_command = NTLMSSP_NEGOTIATE;
     117             :                         } else {
     118             :                                 /* This is normal in SPNEGO mech negotiation fallback */
     119           0 :                                 DEBUG(2, ("Failed to parse NTLMSSP packet: zero length\n"));
     120           0 :                                 return NT_STATUS_INVALID_PARAMETER;
     121             :                         }
     122           0 :                         break;
     123           0 :                 default:
     124           0 :                         DEBUG(1, ("NTLMSSP state has invalid role %d\n",
     125             :                                   gensec_ntlmssp->ntlmssp_state->role));
     126           0 :                         return NT_STATUS_INVALID_PARAMETER;
     127             :                 }
     128             :         } else {
     129      114546 :                 if (!msrpc_parse(gensec_ntlmssp->ntlmssp_state,
     130             :                                  &input, "Cd",
     131             :                                  "NTLMSSP",
     132             :                                  &ntlmssp_command)) {
     133           0 :                         DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
     134           0 :                         dump_data(2, input.data, input.length);
     135           0 :                         return NT_STATUS_INVALID_PARAMETER;
     136             :                 }
     137             :         }
     138             : 
     139      153893 :         if (ntlmssp_command != gensec_ntlmssp->ntlmssp_state->expected_state) {
     140           0 :                 DEBUG(2, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command,
     141             :                           gensec_ntlmssp->ntlmssp_state->expected_state));
     142           0 :                 return NT_STATUS_INVALID_PARAMETER;
     143             :         }
     144             : 
     145      497304 :         for (i=0; i < ARRAY_SIZE(ntlmssp_callbacks); i++) {
     146      497304 :                 if (ntlmssp_callbacks[i].role == gensec_ntlmssp->ntlmssp_state->role &&
     147      268780 :                     ntlmssp_callbacks[i].command == ntlmssp_command) {
     148      153893 :                         *idx = i;
     149      153893 :                         return NT_STATUS_OK;
     150             :                 }
     151             :         }
     152             : 
     153           0 :         DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
     154             :                   gensec_ntlmssp->ntlmssp_state->role, ntlmssp_command));
     155             : 
     156           0 :         return NT_STATUS_INVALID_PARAMETER;
     157             : }
     158             : 
     159             : struct gensec_ntlmssp_update_state {
     160             :         const struct ntlmssp_callbacks *cb;
     161             :         NTSTATUS status;
     162             :         DATA_BLOB out;
     163             : };
     164             : 
     165             : static void gensec_ntlmssp_update_done(struct tevent_req *subreq);
     166             : 
     167      153893 : static struct tevent_req *gensec_ntlmssp_update_send(TALLOC_CTX *mem_ctx,
     168             :                                                      struct tevent_context *ev,
     169             :                                                      struct gensec_security *gensec_security,
     170             :                                                      const DATA_BLOB in)
     171             : {
     172         740 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
     173      153893 :                 talloc_get_type_abort(gensec_security->private_data,
     174             :                                       struct gensec_ntlmssp_context);
     175      153893 :         struct tevent_req *req = NULL;
     176      153893 :         struct gensec_ntlmssp_update_state *state = NULL;
     177         740 :         NTSTATUS status;
     178      153893 :         uint32_t i = 0;
     179             : 
     180      153893 :         req = tevent_req_create(mem_ctx, &state,
     181             :                                 struct gensec_ntlmssp_update_state);
     182      153893 :         if (req == NULL) {
     183           0 :                 return NULL;
     184             :         }
     185             : 
     186      153893 :         status = gensec_ntlmssp_update_find(gensec_security,
     187             :                                             gensec_ntlmssp,
     188             :                                             in, &i);
     189      153893 :         if (tevent_req_nterror(req, status)) {
     190           0 :                 return tevent_req_post(req, ev);
     191             :         }
     192             : 
     193      153893 :         if (ntlmssp_callbacks[i].send_fn != NULL) {
     194       37967 :                 struct tevent_req *subreq = NULL;
     195             : 
     196       37967 :                 state->cb = &ntlmssp_callbacks[i];
     197             : 
     198       37967 :                 subreq = state->cb->send_fn(state, ev,
     199             :                                             gensec_security,
     200             :                                             in);
     201       37967 :                 if (tevent_req_nomem(subreq, req)) {
     202           0 :                         return tevent_req_post(req, ev);
     203             :                 }
     204       37967 :                 tevent_req_set_callback(subreq,
     205             :                                         gensec_ntlmssp_update_done,
     206             :                                         req);
     207       37967 :                 return req;
     208             :         }
     209             : 
     210      115926 :         status = ntlmssp_callbacks[i].sync_fn(gensec_security,
     211             :                                               state,
     212      115926 :                                               in, &state->out);
     213      115926 :         state->status = status;
     214      115926 :         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
     215       77489 :                 tevent_req_done(req);
     216       77489 :                 return tevent_req_post(req, ev);
     217             :         }
     218       38437 :         if (tevent_req_nterror(req, status)) {
     219         100 :                 return tevent_req_post(req, ev);
     220             :         }
     221             : 
     222       38337 :         tevent_req_done(req);
     223       38337 :         return tevent_req_post(req, ev);
     224             : }
     225             : 
     226       37967 : static void gensec_ntlmssp_update_done(struct tevent_req *subreq)
     227             : {
     228         148 :         struct tevent_req *req =
     229       37967 :                 tevent_req_callback_data(subreq,
     230             :                 struct tevent_req);
     231         148 :         struct gensec_ntlmssp_update_state *state =
     232       37967 :                 tevent_req_data(req,
     233             :                 struct gensec_ntlmssp_update_state);
     234         148 :         NTSTATUS status;
     235             : 
     236       37967 :         status = state->cb->recv_fn(subreq, state, &state->out);
     237       37967 :         TALLOC_FREE(subreq);
     238       37967 :         if (GENSEC_UPDATE_IS_NTERROR(status)) {
     239        4602 :                 tevent_req_nterror(req, status);
     240        4602 :                 return;
     241             :         }
     242             : 
     243       33365 :         state->status = status;
     244       33365 :         tevent_req_done(req);
     245             : }
     246             : 
     247      153893 : static NTSTATUS gensec_ntlmssp_update_recv(struct tevent_req *req,
     248             :                                            TALLOC_CTX *out_mem_ctx,
     249             :                                            DATA_BLOB *out)
     250             : {
     251         740 :         struct gensec_ntlmssp_update_state *state =
     252      153893 :                 tevent_req_data(req,
     253             :                 struct gensec_ntlmssp_update_state);
     254         740 :         NTSTATUS status;
     255             : 
     256      153893 :         *out = data_blob_null;
     257             : 
     258      153893 :         if (tevent_req_is_nterror(req, &status)) {
     259        4702 :                 tevent_req_received(req);
     260        4702 :                 return status;
     261             :         }
     262             : 
     263      149191 :         *out = state->out;
     264      149191 :         talloc_steal(out_mem_ctx, state->out.data);
     265      149191 :         status = state->status;
     266      149191 :         tevent_req_received(req);
     267      149191 :         return status;
     268             : }
     269             : 
     270       66028 : static NTSTATUS gensec_ntlmssp_may_reset_crypto(struct gensec_security *gensec_security,
     271             :                                                 bool full_reset)
     272             : {
     273         277 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
     274       66028 :                 talloc_get_type_abort(gensec_security->private_data,
     275             :                                       struct gensec_ntlmssp_context);
     276       66028 :         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
     277         277 :         NTSTATUS status;
     278       66028 :         bool reset_seqnums = full_reset;
     279             : 
     280       66028 :         if (!gensec_ntlmssp_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
     281         330 :                 return NT_STATUS_OK;
     282             :         }
     283             : 
     284       65698 :         status = ntlmssp_sign_reset(ntlmssp_state, reset_seqnums);
     285       65698 :         if (!NT_STATUS_IS_OK(status)) {
     286           0 :                 DEBUG(1, ("Could not reset NTLMSSP signing/sealing system (error was: %s)\n",
     287             :                           nt_errstr(status)));
     288           0 :                 return status;
     289             :         }
     290             : 
     291       65698 :         return NT_STATUS_OK;
     292             : }
     293             : 
     294       70743 : static const char *gensec_ntlmssp_final_auth_type(struct gensec_security *gensec_security)
     295             : {
     296       70743 :         return GENSEC_FINAL_AUTH_TYPE_NTLMSSP;
     297             : }
     298             : 
     299             : static const char *gensec_ntlmssp_oids[] = {
     300             :         GENSEC_OID_NTLMSSP,
     301             :         NULL
     302             : };
     303             : 
     304             : static const struct gensec_security_ops gensec_ntlmssp_security_ops = {
     305             :         .name           = "ntlmssp",
     306             :         .sasl_name      = GENSEC_SASL_NAME_NTLMSSP, /* "NTLM" */
     307             :         .auth_type      = DCERPC_AUTH_TYPE_NTLMSSP,
     308             :         .weak_crypto    = true,
     309             :         .oid            = gensec_ntlmssp_oids,
     310             :         .client_start   = gensec_ntlmssp_client_start,
     311             :         .server_start   = gensec_ntlmssp_server_start,
     312             :         .magic          = gensec_ntlmssp_magic,
     313             :         .update_send    = gensec_ntlmssp_update_send,
     314             :         .update_recv    = gensec_ntlmssp_update_recv,
     315             :         .may_reset_crypto= gensec_ntlmssp_may_reset_crypto,
     316             :         .sig_size       = gensec_ntlmssp_sig_size,
     317             :         .sign_packet    = gensec_ntlmssp_sign_packet,
     318             :         .check_packet   = gensec_ntlmssp_check_packet,
     319             :         .seal_packet    = gensec_ntlmssp_seal_packet,
     320             :         .unseal_packet  = gensec_ntlmssp_unseal_packet,
     321             :         .wrap           = gensec_ntlmssp_wrap,
     322             :         .unwrap         = gensec_ntlmssp_unwrap,
     323             :         .session_key    = gensec_ntlmssp_session_key,
     324             :         .session_info   = gensec_ntlmssp_session_info,
     325             :         .have_feature   = gensec_ntlmssp_have_feature,
     326             :         .final_auth_type = gensec_ntlmssp_final_auth_type,
     327             :         .enabled        = true,
     328             :         .priority       = GENSEC_NTLMSSP
     329             : };
     330             : 
     331             : static const struct gensec_security_ops gensec_ntlmssp_resume_ccache_ops = {
     332             :         .name           = "ntlmssp_resume_ccache",
     333             :         .weak_crypto    = true,
     334             :         .client_start   = gensec_ntlmssp_resume_ccache_start,
     335             :         .update_send    = gensec_ntlmssp_update_send,
     336             :         .update_recv    = gensec_ntlmssp_update_recv,
     337             :         .session_key    = gensec_ntlmssp_session_key,
     338             :         .have_feature   = gensec_ntlmssp_have_feature,
     339             :         .enabled        = true,
     340             :         .priority       = GENSEC_NTLMSSP
     341             : };
     342             : 
     343       52012 : _PUBLIC_ NTSTATUS gensec_ntlmssp_init(TALLOC_CTX *ctx)
     344             : {
     345        1174 :         NTSTATUS ret;
     346             : 
     347       52012 :         ret = gensec_register(ctx, &gensec_ntlmssp_security_ops);
     348       52012 :         if (!NT_STATUS_IS_OK(ret)) {
     349           0 :                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
     350             :                         gensec_ntlmssp_security_ops.name));
     351           0 :                 return ret;
     352             :         }
     353             : 
     354       52012 :         ret = gensec_register(ctx, &gensec_ntlmssp_resume_ccache_ops);
     355       52012 :         if (!NT_STATUS_IS_OK(ret)) {
     356           0 :                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
     357             :                         gensec_ntlmssp_resume_ccache_ops.name));
     358           0 :                 return ret;
     359             :         }
     360             : 
     361       52012 :         return ret;
     362             : }
     363             : 
     364       17265 : static struct gensec_security *gensec_find_child_by_ops(struct gensec_security *gensec_security,
     365             :                                                         const struct gensec_security_ops *ops)
     366             : {
     367       17265 :         struct gensec_security *current = gensec_security;
     368             : 
     369       36719 :         while (current != NULL) {
     370       34408 :                 if (current->ops == ops) {
     371       14954 :                         return current;
     372             :                 }
     373             : 
     374       19454 :                 current = current->child_security;
     375             :         }
     376             : 
     377        2311 :         return NULL;
     378             : }
     379             : 
     380         100 : uint32_t gensec_ntlmssp_neg_flags(struct gensec_security *gensec_security)
     381             : {
     382           0 :         struct gensec_ntlmssp_context *gensec_ntlmssp;
     383             : 
     384         100 :         gensec_security = gensec_find_child_by_ops(gensec_security,
     385             :                                         &gensec_ntlmssp_security_ops);
     386         100 :         if (gensec_security == NULL) {
     387           0 :                 return 0;
     388             :         }
     389             : 
     390         100 :         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
     391             :                                                struct gensec_ntlmssp_context);
     392         100 :         return gensec_ntlmssp->ntlmssp_state->neg_flags;
     393             : }
     394             : 
     395       17165 : const char *gensec_ntlmssp_server_domain(struct gensec_security *gensec_security)
     396             : {
     397           0 :         struct gensec_ntlmssp_context *gensec_ntlmssp;
     398             : 
     399       17165 :         gensec_security = gensec_find_child_by_ops(gensec_security,
     400             :                                         &gensec_ntlmssp_security_ops);
     401       17165 :         if (gensec_security == NULL) {
     402        2311 :                 return NULL;
     403             :         }
     404             : 
     405       14854 :         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
     406             :                                                struct gensec_ntlmssp_context);
     407       14854 :         return gensec_ntlmssp->ntlmssp_state->server.netbios_domain;
     408             : }

Generated by: LCOV version 1.14