LCOV - code coverage report
Current view: top level - auth/gensec - gensec.c (source / functions) Hit Total Coverage
Test: coverage report for master 6248eab5 Lines: 255 303 84.2 %
Date: 2021-08-25 13:27:56 Functions: 38 38 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Generic Authentication Interface
       5             : 
       6             :    Copyright (C) Andrew Tridgell 2003
       7             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2006
       8             : 
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include "includes.h"
      24             : #include "system/network.h"
      25             : #define TEVENT_DEPRECATED 1
      26             : #include <tevent.h>
      27             : #include "lib/tsocket/tsocket.h"
      28             : #include "lib/util/tevent_ntstatus.h"
      29             : #include "auth/gensec/gensec.h"
      30             : #include "auth/gensec/gensec_internal.h"
      31             : #include "librpc/gen_ndr/dcerpc.h"
      32             : #include "auth/common_auth.h"
      33             : 
      34             : #undef DBGC_CLASS
      35             : #define DBGC_CLASS DBGC_AUTH
      36             : 
      37      104632 : _PRIVATE_ NTSTATUS gensec_may_reset_crypto(struct gensec_security *gensec_security,
      38             :                                            bool full_reset)
      39             : {
      40      104632 :         if (!gensec_security->ops->may_reset_crypto) {
      41       52679 :                 return NT_STATUS_OK;
      42             :         }
      43             : 
      44       51953 :         return gensec_security->ops->may_reset_crypto(gensec_security, full_reset);
      45             : }
      46             : 
      47             : /*
      48             :   wrappers for the gensec function pointers
      49             : */
      50      585584 : _PUBLIC_ NTSTATUS gensec_unseal_packet(struct gensec_security *gensec_security,
      51             :                               uint8_t *data, size_t length,
      52             :                               const uint8_t *whole_pdu, size_t pdu_length,
      53             :                               const DATA_BLOB *sig)
      54             : {
      55      585584 :         if (!gensec_security->ops->unseal_packet) {
      56           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
      57             :         }
      58      585584 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
      59           0 :                 return NT_STATUS_INVALID_PARAMETER;
      60             :         }
      61      585584 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
      62           0 :                 return NT_STATUS_INVALID_PARAMETER;
      63             :         }
      64      585584 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_DCE_STYLE)) {
      65           0 :                 return NT_STATUS_INVALID_PARAMETER;
      66             :         }
      67             : 
      68      585584 :         return gensec_security->ops->unseal_packet(gensec_security,
      69             :                                                    data, length,
      70             :                                                    whole_pdu, pdu_length,
      71             :                                                    sig);
      72             : }
      73             : 
      74      192606 : _PUBLIC_ NTSTATUS gensec_check_packet(struct gensec_security *gensec_security,
      75             :                              const uint8_t *data, size_t length,
      76             :                              const uint8_t *whole_pdu, size_t pdu_length,
      77             :                              const DATA_BLOB *sig)
      78             : {
      79      192606 :         if (!gensec_security->ops->check_packet) {
      80           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
      81             :         }
      82      192606 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
      83           2 :                 return NT_STATUS_INVALID_PARAMETER;
      84             :         }
      85             : 
      86      192604 :         return gensec_security->ops->check_packet(gensec_security, data, length, whole_pdu, pdu_length, sig);
      87             : }
      88             : 
      89      585590 : _PUBLIC_ NTSTATUS gensec_seal_packet(struct gensec_security *gensec_security,
      90             :                             TALLOC_CTX *mem_ctx,
      91             :                             uint8_t *data, size_t length,
      92             :                             const uint8_t *whole_pdu, size_t pdu_length,
      93             :                             DATA_BLOB *sig)
      94             : {
      95      585590 :         if (!gensec_security->ops->seal_packet) {
      96           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
      97             :         }
      98      585590 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
      99           0 :                 return NT_STATUS_INVALID_PARAMETER;
     100             :         }
     101      585590 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
     102           0 :                 return NT_STATUS_INVALID_PARAMETER;
     103             :         }
     104      585590 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_DCE_STYLE)) {
     105           0 :                 return NT_STATUS_INVALID_PARAMETER;
     106             :         }
     107             : 
     108      585590 :         return gensec_security->ops->seal_packet(gensec_security, mem_ctx, data, length, whole_pdu, pdu_length, sig);
     109             : }
     110             : 
     111      194835 : _PUBLIC_ NTSTATUS gensec_sign_packet(struct gensec_security *gensec_security,
     112             :                             TALLOC_CTX *mem_ctx,
     113             :                             const uint8_t *data, size_t length,
     114             :                             const uint8_t *whole_pdu, size_t pdu_length,
     115             :                             DATA_BLOB *sig)
     116             : {
     117      194835 :         if (!gensec_security->ops->sign_packet) {
     118           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
     119             :         }
     120      194835 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
     121           0 :                 return NT_STATUS_INVALID_PARAMETER;
     122             :         }
     123             : 
     124      194835 :         return gensec_security->ops->sign_packet(gensec_security, mem_ctx, data, length, whole_pdu, pdu_length, sig);
     125             : }
     126             : 
     127      279460 : _PUBLIC_ size_t gensec_sig_size(struct gensec_security *gensec_security, size_t data_size)
     128             : {
     129      279460 :         if (!gensec_security->ops->sig_size) {
     130         774 :                 return 0;
     131             :         }
     132      278672 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
     133         294 :                 return 0;
     134             :         }
     135      278378 :         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
     136      153407 :                 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_DCE_STYLE)) {
     137           0 :                         return 0;
     138             :                 }
     139             :         }
     140             : 
     141      278378 :         return gensec_security->ops->sig_size(gensec_security, data_size);
     142             : }
     143             : 
     144       71032 : _PUBLIC_ size_t gensec_max_wrapped_size(struct gensec_security *gensec_security)
     145             : {
     146       71032 :         if (!gensec_security->ops->max_wrapped_size) {
     147       10011 :                 return (1 << 17);
     148             :         }
     149             : 
     150       61021 :         return gensec_security->ops->max_wrapped_size(gensec_security);
     151             : }
     152             : 
     153       71032 : _PUBLIC_ size_t gensec_max_input_size(struct gensec_security *gensec_security)
     154             : {
     155       71032 :         if (!gensec_security->ops->max_input_size) {
     156       10011 :                 return (1 << 17) - gensec_sig_size(gensec_security, 1 << 17);
     157             :         }
     158             : 
     159       61021 :         return gensec_security->ops->max_input_size(gensec_security);
     160             : }
     161             : 
     162     2334321 : _PUBLIC_ NTSTATUS gensec_wrap(struct gensec_security *gensec_security,
     163             :                      TALLOC_CTX *mem_ctx,
     164             :                      const DATA_BLOB *in,
     165             :                      DATA_BLOB *out)
     166             : {
     167     2334321 :         if (!gensec_security->ops->wrap) {
     168           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
     169             :         }
     170     2334321 :         return gensec_security->ops->wrap(gensec_security, mem_ctx, in, out);
     171             : }
     172             : 
     173     2334321 : _PUBLIC_ NTSTATUS gensec_unwrap(struct gensec_security *gensec_security,
     174             :                        TALLOC_CTX *mem_ctx,
     175             :                        const DATA_BLOB *in,
     176             :                        DATA_BLOB *out)
     177             : {
     178     2334321 :         if (!gensec_security->ops->unwrap) {
     179           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
     180             :         }
     181     2334321 :         return gensec_security->ops->unwrap(gensec_security, mem_ctx, in, out);
     182             : }
     183             : 
     184       58093 : _PUBLIC_ NTSTATUS gensec_session_key(struct gensec_security *gensec_security,
     185             :                                      TALLOC_CTX *mem_ctx,
     186             :                                      DATA_BLOB *session_key)
     187             : {
     188       58093 :         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SESSION_KEY)) {
     189           0 :                 return NT_STATUS_NO_USER_SESSION_KEY;
     190             :         }
     191             : 
     192       58093 :         if (!gensec_security->ops->session_key) {
     193           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
     194             :         }
     195             : 
     196       58093 :         return gensec_security->ops->session_key(gensec_security, mem_ctx, session_key);
     197             : }
     198             : 
     199      136138 : const char *gensec_final_auth_type(struct gensec_security *gensec_security)
     200             : {
     201      136138 :         if (!gensec_security->ops->final_auth_type) {
     202        2593 :                 return gensec_security->ops->name;
     203             :         }
     204             : 
     205      133545 :         return gensec_security->ops->final_auth_type(gensec_security);
     206             : }
     207             : 
     208             : /*
     209             :  * Log details of a successful GENSEC authorization to a service.
     210             :  *
     211             :  * Only successful authorizations are logged, as only these call gensec_session_info()
     212             :  *
     213             :  * The service may later refuse authorization due to an ACL.
     214             :  *
     215             :  */
     216       56335 : static void log_successful_gensec_authz_event(struct gensec_security *gensec_security,
     217             :                                               struct auth_session_info *session_info)
     218             : {
     219       56335 :         const struct tsocket_address *remote
     220             :                 = gensec_get_remote_address(gensec_security);
     221       56335 :         const struct tsocket_address *local
     222             :                 = gensec_get_local_address(gensec_security);
     223       56335 :         const char *service_description
     224             :                 = gensec_get_target_service_description(gensec_security);
     225       56335 :         const char *final_auth_type
     226             :                 = gensec_final_auth_type(gensec_security);
     227       56335 :         const char *transport_protection = NULL;
     228       56335 :         if (gensec_security->want_features & GENSEC_FEATURE_SMB_TRANSPORT) {
     229       28294 :                 transport_protection = AUTHZ_TRANSPORT_PROTECTION_SMB;
     230       27519 :         } else if (gensec_security->want_features & GENSEC_FEATURE_LDAPS_TRANSPORT) {
     231          73 :                 transport_protection = AUTHZ_TRANSPORT_PROTECTION_TLS;
     232       27446 :         } else if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
     233       15947 :                 transport_protection = AUTHZ_TRANSPORT_PROTECTION_SEAL;
     234       11236 :         } else if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
     235       10391 :                 transport_protection = AUTHZ_TRANSPORT_PROTECTION_SIGN;
     236             :         } else {
     237         570 :                 transport_protection = AUTHZ_TRANSPORT_PROTECTION_NONE;
     238             :         }
     239       56335 :         log_successful_authz_event(gensec_security->auth_context->msg_ctx,
     240       56335 :                                    gensec_security->auth_context->lp_ctx,
     241             :                                    remote, local,
     242             :                                    service_description,
     243             :                                    final_auth_type,
     244             :                                    transport_protection,
     245             :                                    session_info);
     246       56335 : }
     247             : 
     248             : 
     249             : /**
     250             :  * Return the credentials of a logged on user, including session keys
     251             :  * etc.
     252             :  *
     253             :  * Only valid after a successful authentication
     254             :  *
     255             :  * May only be called once per authentication.  This will also make an
     256             :  * authorization log entry, as it is already called by all the
     257             :  * callers.
     258             :  *
     259             :  */
     260             : 
     261      108499 : _PUBLIC_ NTSTATUS gensec_session_info(struct gensec_security *gensec_security,
     262             :                                       TALLOC_CTX *mem_ctx,
     263             :                                       struct auth_session_info **session_info)
     264             : {
     265             :         NTSTATUS status;
     266      108499 :         if (!gensec_security->ops->session_info) {
     267           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
     268             :         }
     269      108499 :         status = gensec_security->ops->session_info(gensec_security, mem_ctx, session_info);
     270             : 
     271      108499 :         if (NT_STATUS_IS_OK(status) && !gensec_security->subcontext
     272       56449 :             && (gensec_security->want_features & GENSEC_FEATURE_NO_AUTHZ_LOG) == 0) {
     273       56335 :                 log_successful_gensec_authz_event(gensec_security, *session_info);
     274             :         }
     275             : 
     276      108499 :         return status;
     277             : }
     278             : 
     279         394 : _PUBLIC_ void gensec_set_max_update_size(struct gensec_security *gensec_security,
     280             :                                 uint32_t max_update_size)
     281             : {
     282         394 :         gensec_security->max_update_size = max_update_size;
     283         394 : }
     284             : 
     285      155332 : _PUBLIC_ size_t gensec_max_update_size(struct gensec_security *gensec_security)
     286             : {
     287      155332 :         if (gensec_security->max_update_size == 0) {
     288      152534 :                 return UINT32_MAX;
     289             :         }
     290             : 
     291         394 :         return gensec_security->max_update_size;
     292             : }
     293             : 
     294      219963 : static NTSTATUS gensec_verify_features(struct gensec_security *gensec_security)
     295             : {
     296             :         bool ok;
     297             : 
     298             :         /*
     299             :          * gensec_want_feature(GENSEC_FEATURE_SIGN)
     300             :          * and
     301             :          * gensec_want_feature(GENSEC_FEATURE_SEAL)
     302             :          * require these flags to be available.
     303             :          */
     304      219963 :         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
     305       59351 :                 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
     306           1 :                         DEBUG(0,("Did not manage to negotiate mandatory feature "
     307             :                                  "SIGN\n"));
     308           1 :                         return NT_STATUS_ACCESS_DENIED;
     309             :                 }
     310             :         }
     311      219962 :         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
     312       36003 :                 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
     313           1 :                         DEBUG(0,("Did not manage to negotiate mandatory feature "
     314             :                                  "SEAL\n"));
     315           1 :                         return NT_STATUS_ACCESS_DENIED;
     316             :                 }
     317       36002 :                 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
     318           0 :                         DEBUG(0,("Did not manage to negotiate mandatory feature "
     319             :                                  "SIGN for SEAL\n"));
     320           0 :                         return NT_STATUS_ACCESS_DENIED;
     321             :                 }
     322             :         }
     323             : 
     324      219961 :         if (gensec_security->dcerpc_auth_level < DCERPC_AUTH_LEVEL_PACKET) {
     325      194122 :                 return NT_STATUS_OK;
     326             :         }
     327             : 
     328       25839 :         ok = gensec_have_feature(gensec_security,
     329             :                                  GENSEC_FEATURE_SIGN_PKT_HEADER);
     330       25839 :         if (!ok) {
     331           0 :                 DBG_ERR("backend [%s] does not support header signing! "
     332             :                         "auth_level[0x%x]\n",
     333             :                         gensec_security->ops->name,
     334             :                         gensec_security->dcerpc_auth_level);
     335           0 :                 return NT_STATUS_INTERNAL_ERROR;
     336             :         }
     337             : 
     338       25839 :         return NT_STATUS_OK;
     339             : }
     340             : 
     341             : /**
     342             :  * Next state function for the GENSEC state machine
     343             :  *
     344             :  * @param gensec_security GENSEC State
     345             :  * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
     346             :  * @param in The request, as a DATA_BLOB
     347             :  * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
     348             :  * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
     349             :  *                or NT_STATUS_OK if the user is authenticated.
     350             :  */
     351      103939 : _PUBLIC_ NTSTATUS gensec_update(struct gensec_security *gensec_security,
     352             :                                 TALLOC_CTX *out_mem_ctx,
     353             :                                 const DATA_BLOB in, DATA_BLOB *out)
     354             : {
     355             :         NTSTATUS status;
     356      103939 :         TALLOC_CTX *frame = NULL;
     357      103939 :         struct tevent_context *ev = NULL;
     358      103939 :         struct tevent_req *subreq = NULL;
     359             :         bool ok;
     360             : 
     361      103939 :         if (gensec_security->subcontext) {
     362             :                 /*
     363             :                  * gensec modules are not allowed to call the sync version.
     364             :                  */
     365           0 :                 return NT_STATUS_INTERNAL_ERROR;
     366             :         }
     367             : 
     368      103939 :         frame = talloc_stackframe();
     369             : 
     370      103939 :         ev = samba_tevent_context_init(frame);
     371      103939 :         if (ev == NULL) {
     372           0 :                 status = NT_STATUS_NO_MEMORY;
     373           0 :                 goto fail;
     374             :         }
     375             : 
     376             :         /*
     377             :          * TODO: remove this hack once the backends
     378             :          * are fixed.
     379             :          */
     380      103939 :         tevent_loop_allow_nesting(ev);
     381             : 
     382      103939 :         subreq = gensec_update_send(frame, ev, gensec_security, in);
     383      103939 :         if (subreq == NULL) {
     384           0 :                 status = NT_STATUS_NO_MEMORY;
     385           0 :                 goto fail;
     386             :         }
     387      103939 :         ok = tevent_req_poll_ntstatus(subreq, ev, &status);
     388      103939 :         if (!ok) {
     389           0 :                 goto fail;
     390             :         }
     391      103939 :         status = gensec_update_recv(subreq, out_mem_ctx, out);
     392      103939 :  fail:
     393      103939 :         TALLOC_FREE(frame);
     394      103939 :         return status;
     395             : }
     396             : 
     397             : struct gensec_update_state {
     398             :         const struct gensec_security_ops *ops;
     399             :         struct gensec_security *gensec_security;
     400             :         NTSTATUS status;
     401             :         DATA_BLOB out;
     402             : };
     403             : 
     404             : static void gensec_update_cleanup(struct tevent_req *req,
     405             :                                   enum tevent_req_state req_state);
     406             : static void gensec_update_done(struct tevent_req *subreq);
     407             : 
     408             : /**
     409             :  * Next state function for the GENSEC state machine async version
     410             :  *
     411             :  * @param mem_ctx The memory context for the request
     412             :  * @param ev The event context for the request
     413             :  * @param gensec_security GENSEC State
     414             :  * @param in The request, as a DATA_BLOB
     415             :  *
     416             :  * @return The request handle or NULL on no memory failure
     417             :  */
     418             : 
     419      482197 : _PUBLIC_ struct tevent_req *gensec_update_send(TALLOC_CTX *mem_ctx,
     420             :                                                struct tevent_context *ev,
     421             :                                                struct gensec_security *gensec_security,
     422             :                                                const DATA_BLOB in)
     423             : {
     424      482197 :         struct tevent_req *req = NULL;
     425      482197 :         struct gensec_update_state *state = NULL;
     426      482197 :         struct tevent_req *subreq = NULL;
     427             : 
     428      482197 :         req = tevent_req_create(mem_ctx, &state,
     429             :                                 struct gensec_update_state);
     430      482197 :         if (req == NULL) {
     431           0 :                 return NULL;
     432             :         }
     433      482197 :         state->ops = gensec_security->ops;
     434      482197 :         state->gensec_security = gensec_security;
     435             : 
     436      482197 :         if (gensec_security->update_busy_ptr != NULL) {
     437           0 :                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
     438           0 :                 return tevent_req_post(req, ev);
     439             :         }
     440             : 
     441      482197 :         if (gensec_security->child_security != NULL) {
     442           0 :                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
     443           0 :                 return tevent_req_post(req, ev);
     444             :         }
     445             : 
     446      482197 :         gensec_security->update_busy_ptr = &state->gensec_security;
     447      482197 :         tevent_req_set_cleanup_fn(req, gensec_update_cleanup);
     448             : 
     449      482197 :         subreq = state->ops->update_send(state, ev, gensec_security, in);
     450      482197 :         if (tevent_req_nomem(subreq, req)) {
     451           0 :                 return tevent_req_post(req, ev);
     452             :         }
     453      482197 :         tevent_req_set_callback(subreq, gensec_update_done, req);
     454             : 
     455      482197 :         DBG_DEBUG("%s[%p]: subreq: %p\n", state->ops->name,
     456             :                   state->gensec_security, subreq);
     457             : 
     458      475543 :         return req;
     459             : }
     460             : 
     461      964394 : static void gensec_update_cleanup(struct tevent_req *req,
     462             :                                   enum tevent_req_state req_state)
     463             : {
     464      964394 :         struct gensec_update_state *state =
     465      964394 :                 tevent_req_data(req,
     466             :                 struct gensec_update_state);
     467             : 
     468      964394 :         if (state->gensec_security == NULL) {
     469      475543 :                 return;
     470             :         }
     471             : 
     472      482197 :         if (state->gensec_security->update_busy_ptr == &state->gensec_security) {
     473      482197 :                 state->gensec_security->update_busy_ptr = NULL;
     474             :         }
     475             : 
     476      482197 :         state->gensec_security = NULL;
     477             : }
     478             : 
     479      482197 : static void gensec_update_done(struct tevent_req *subreq)
     480             : {
     481      482197 :         struct tevent_req *req =
     482      482197 :                 tevent_req_callback_data(subreq,
     483             :                 struct tevent_req);
     484      482197 :         struct gensec_update_state *state =
     485      482197 :                 tevent_req_data(req,
     486             :                 struct gensec_update_state);
     487             :         NTSTATUS status;
     488      482197 :         const char *debug_subreq = NULL;
     489             : 
     490      482197 :         if (CHECK_DEBUGLVL(DBGLVL_DEBUG)) {
     491             :                 /*
     492             :                  * We need to call tevent_req_print()
     493             :                  * before calling the _recv function,
     494             :                  * before tevent_req_received() was called.
     495             :                  * in order to print the pointer value of
     496             :                  * the subreq state.
     497             :                  */
     498          10 :                 debug_subreq = tevent_req_print(state, subreq);
     499             :         }
     500             : 
     501      482197 :         status = state->ops->update_recv(subreq, state, &state->out);
     502      482197 :         TALLOC_FREE(subreq);
     503      482197 :         state->status = status;
     504      482197 :         if (GENSEC_UPDATE_IS_NTERROR(status)) {
     505        4308 :                 NTSTATUS orig_status = status;
     506        4308 :                 bool force_no_such_user = false;
     507             : 
     508             :                 /*
     509             :                  * callers only expect NT_STATUS_NO_SUCH_USER.
     510             :                  */
     511        4308 :                 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_ACCOUNT_NAME)) {
     512           0 :                         force_no_such_user = true;
     513        4308 :                 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_DOMAIN)) {
     514           0 :                         force_no_such_user = true;
     515             :                 }
     516             : 
     517        4308 :                 if (state->gensec_security->subcontext) {
     518             :                         /*
     519             :                          * We should only map on the outer
     520             :                          * gensec_update exchange, spnego
     521             :                          * needs the raw status.
     522             :                          */
     523        2334 :                         force_no_such_user = false;
     524             :                 }
     525             : 
     526        4308 :                 if (force_no_such_user) {
     527             :                         /*
     528             :                          * nt_status_squash() may map
     529             :                          * to NT_STATUS_LOGON_FAILURE later
     530             :                          */
     531           0 :                         status = NT_STATUS_NO_SUCH_USER;
     532             :                 }
     533             : 
     534        4308 :                 DBG_INFO("%s[%p]: %s%s%s%s%s\n",
     535             :                          state->ops->name,
     536             :                          state->gensec_security,
     537             :                          NT_STATUS_EQUAL(status, orig_status) ?
     538             :                          "" : nt_errstr(orig_status),
     539             :                          NT_STATUS_EQUAL(status, orig_status) ?
     540             :                          "" : " ",
     541             :                          nt_errstr(status),
     542             :                          debug_subreq ? " " : "",
     543             :                          debug_subreq ? debug_subreq : "");
     544        4308 :                 tevent_req_nterror(req, status);
     545        4308 :                 return;
     546             :         }
     547      477889 :         DBG_DEBUG("%s[%p]: %s %s\n", state->ops->name,
     548             :                   state->gensec_security, nt_errstr(status),
     549             :                   debug_subreq);
     550      477889 :         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
     551      257926 :                 tevent_req_done(req);
     552      257926 :                 return;
     553             :         }
     554             : 
     555             :         /*
     556             :          * Because callers using the
     557             :          * gensec_start_mech_by_authtype() never call
     558             :          * gensec_want_feature(), it isn't sensible for them
     559             :          * to have to call gensec_have_feature() manually, and
     560             :          * these are not points of negotiation, but are
     561             :          * asserted by the client
     562             :          */
     563      219963 :         status = gensec_verify_features(state->gensec_security);
     564      219963 :         if (tevent_req_nterror(req, status)) {
     565           2 :                 return;
     566             :         }
     567             : 
     568      219961 :         tevent_req_done(req);
     569             : }
     570             : 
     571             : /**
     572             :  * Next state function for the GENSEC state machine
     573             :  *
     574             :  * @param req request state
     575             :  * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
     576             :  * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
     577             :  * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
     578             :  *                or NT_STATUS_OK if the user is authenticated.
     579             :  */
     580      482197 : _PUBLIC_ NTSTATUS gensec_update_recv(struct tevent_req *req,
     581             :                                      TALLOC_CTX *out_mem_ctx,
     582             :                                      DATA_BLOB *out)
     583             : {
     584      482197 :         struct gensec_update_state *state =
     585      482197 :                 tevent_req_data(req, struct gensec_update_state);
     586             :         NTSTATUS status;
     587             : 
     588      482197 :         *out = data_blob_null;
     589             : 
     590      482197 :         if (tevent_req_is_nterror(req, &status)) {
     591        4310 :                 tevent_req_received(req);
     592        4310 :                 return status;
     593             :         }
     594             : 
     595      477887 :         *out = state->out;
     596      477887 :         talloc_steal(out_mem_ctx, out->data);
     597      477887 :         status = state->status;
     598      477887 :         tevent_req_received(req);
     599      477887 :         return status;
     600             : }
     601             : 
     602             : /**
     603             :  * Set the requirement for a certain feature on the connection
     604             :  *
     605             :  */
     606             : 
     607      526162 : _PUBLIC_ void gensec_want_feature(struct gensec_security *gensec_security,
     608             :                          uint32_t feature)
     609             : {
     610      526162 :         if (!gensec_security->ops || !gensec_security->ops->want_feature) {
     611      480978 :                 gensec_security->want_features |= feature;
     612      480978 :                 return;
     613             :         }
     614       45184 :         gensec_security->ops->want_feature(gensec_security, feature);
     615             : }
     616             : 
     617             : /**
     618             :  * Check the requirement for a certain feature on the connection
     619             :  *
     620             :  */
     621             : 
     622     9719807 : _PUBLIC_ bool gensec_have_feature(struct gensec_security *gensec_security,
     623             :                          uint32_t feature)
     624             : {
     625     9719807 :         if (!gensec_security->ops || !gensec_security->ops->have_feature) {
     626           0 :                 return false;
     627             :         }
     628             : 
     629             :         /* We might 'have' features that we don't 'want', because the
     630             :          * other end demanded them, or we can't negotiate them off */
     631     9719807 :         return gensec_security->ops->have_feature(gensec_security, feature);
     632             : }
     633             : 
     634       77932 : _PUBLIC_ NTTIME gensec_expire_time(struct gensec_security *gensec_security)
     635             : {
     636       77932 :         if (!gensec_security->ops->expire_time) {
     637       19448 :                 return GENSEC_EXPIRE_TIME_INFINITY;
     638             :         }
     639             : 
     640       58482 :         return gensec_security->ops->expire_time(gensec_security);
     641             : }
     642             : /**
     643             :  * Return the credentials structure associated with a GENSEC context
     644             :  *
     645             :  */
     646             : 
     647      687892 : _PUBLIC_ struct cli_credentials *gensec_get_credentials(struct gensec_security *gensec_security)
     648             : {
     649      687892 :         if (!gensec_security) {
     650           0 :                 return NULL;
     651             :         }
     652      687892 :         return gensec_security->credentials;
     653             : }
     654             : 
     655             : /**
     656             :  * Set the target service (such as 'http' or 'host') on a GENSEC context - ensures it is talloc()ed
     657             :  *
     658             :  * This is used for Kerberos service principal name resolution.
     659             :  */
     660             : 
     661      142636 : _PUBLIC_ NTSTATUS gensec_set_target_service(struct gensec_security *gensec_security, const char *service)
     662             : {
     663      142636 :         gensec_security->target.service = talloc_strdup(gensec_security, service);
     664      142636 :         if (!gensec_security->target.service) {
     665           0 :                 return NT_STATUS_NO_MEMORY;
     666             :         }
     667      142636 :         return NT_STATUS_OK;
     668             : }
     669             : 
     670      161119 : _PUBLIC_ const char *gensec_get_target_service(struct gensec_security *gensec_security)
     671             : {
     672      161119 :         if (gensec_security->target.service) {
     673      151779 :                 return gensec_security->target.service;
     674             :         }
     675             : 
     676        9148 :         return "host";
     677             : }
     678             : 
     679             : /**
     680             :  * Set the target service (such as 'samr') on an GENSEC context - ensures it is talloc()ed.
     681             :  *
     682             :  * This is not the Kerberos service principal, instead this is a
     683             :  * constant value that can be logged as part of authentication and
     684             :  * authorization logging
     685             :  */
     686       99772 : _PUBLIC_ NTSTATUS gensec_set_target_service_description(struct gensec_security *gensec_security,
     687             :                                                         const char *service)
     688             : {
     689       99772 :         gensec_security->target.service_description = talloc_strdup(gensec_security, service);
     690       99772 :         if (!gensec_security->target.service_description) {
     691           0 :                 return NT_STATUS_NO_MEMORY;
     692             :         }
     693       99772 :         return NT_STATUS_OK;
     694             : }
     695             : 
     696       84158 : _PUBLIC_ const char *gensec_get_target_service_description(struct gensec_security *gensec_security)
     697             : {
     698       84158 :         if (gensec_security->target.service_description) {
     699       81273 :                 return gensec_security->target.service_description;
     700        1815 :         } else if (gensec_security->target.service) {
     701         267 :                 return gensec_security->target.service;
     702             :         }
     703             : 
     704        1548 :         return NULL;
     705             : }
     706             : 
     707             : /**
     708             :  * Set the target hostname (suitable for kerberos resolutation) on a GENSEC context - ensures it is talloc()ed
     709             :  *
     710             :  */
     711             : 
     712       59626 : _PUBLIC_ NTSTATUS gensec_set_target_hostname(struct gensec_security *gensec_security, const char *hostname)
     713             : {
     714       59626 :         gensec_security->target.hostname = talloc_strdup(gensec_security, hostname);
     715       59626 :         if (hostname && !gensec_security->target.hostname) {
     716           0 :                 return NT_STATUS_NO_MEMORY;
     717             :         }
     718       59626 :         return NT_STATUS_OK;
     719             : }
     720             : 
     721      161319 : _PUBLIC_ const char *gensec_get_target_hostname(struct gensec_security *gensec_security)
     722             : {
     723             :         /* We allow the target hostname to be overridden for testing purposes */
     724      161319 :         if (gensec_security->settings->target_hostname) {
     725        4751 :                 return gensec_security->settings->target_hostname;
     726             :         }
     727             : 
     728      156568 :         if (gensec_security->target.hostname) {
     729      125256 :                 return gensec_security->target.hostname;
     730             :         }
     731             : 
     732             :         /* We could add use the 'set sockaddr' call, and do a reverse
     733             :          * lookup, but this would be both insecure (compromising the
     734             :          * way kerberos works) and add DNS timeouts */
     735       30478 :         return NULL;
     736             : }
     737             : 
     738             : /**
     739             :  * Set (and copy) local and peer socket addresses onto a socket
     740             :  * context on the GENSEC context.
     741             :  *
     742             :  * This is so that kerberos can include these addresses in
     743             :  * cryptographic tokens, to avoid certain attacks.
     744             :  */
     745             : 
     746             : /**
     747             :  * @brief Set the local gensec address.
     748             :  *
     749             :  * @param  gensec_security   The gensec security context to use.
     750             :  *
     751             :  * @param  remote       The local address to set.
     752             :  *
     753             :  * @return              On success NT_STATUS_OK is returned or an NT_STATUS
     754             :  *                      error.
     755             :  */
     756       99895 : _PUBLIC_ NTSTATUS gensec_set_local_address(struct gensec_security *gensec_security,
     757             :                 const struct tsocket_address *local)
     758             : {
     759       99895 :         TALLOC_FREE(gensec_security->local_addr);
     760             : 
     761       99895 :         if (local == NULL) {
     762           0 :                 return NT_STATUS_OK;
     763             :         }
     764             : 
     765       99895 :         gensec_security->local_addr = tsocket_address_copy(local, gensec_security);
     766       99895 :         if (gensec_security->local_addr == NULL) {
     767           0 :                 return NT_STATUS_NO_MEMORY;
     768             :         }
     769             : 
     770       99895 :         return NT_STATUS_OK;
     771             : }
     772             : 
     773             : /**
     774             :  * @brief Set the remote gensec address.
     775             :  *
     776             :  * @param  gensec_security   The gensec security context to use.
     777             :  *
     778             :  * @param  remote       The remote address to set.
     779             :  *
     780             :  * @return              On success NT_STATUS_OK is returned or an NT_STATUS
     781             :  *                      error.
     782             :  */
     783       99861 : _PUBLIC_ NTSTATUS gensec_set_remote_address(struct gensec_security *gensec_security,
     784             :                 const struct tsocket_address *remote)
     785             : {
     786       99861 :         TALLOC_FREE(gensec_security->remote_addr);
     787             : 
     788       99861 :         if (remote == NULL) {
     789           0 :                 return NT_STATUS_OK;
     790             :         }
     791             : 
     792       99861 :         gensec_security->remote_addr = tsocket_address_copy(remote, gensec_security);
     793       99861 :         if (gensec_security->remote_addr == NULL) {
     794           0 :                 return NT_STATUS_NO_MEMORY;
     795             :         }
     796             : 
     797       99861 :         return NT_STATUS_OK;
     798             : }
     799             : 
     800             : /**
     801             :  * @brief Get the local address from a gensec security context.
     802             :  *
     803             :  * @param  gensec_security   The security context to get the address from.
     804             :  *
     805             :  * @return              The address as tsocket_address which could be NULL if
     806             :  *                      no address is set.
     807             :  */
     808       85813 : _PUBLIC_ const struct tsocket_address *gensec_get_local_address(struct gensec_security *gensec_security)
     809             : {
     810       85813 :         if (gensec_security == NULL) {
     811           0 :                 return NULL;
     812             :         }
     813       85813 :         return gensec_security->local_addr;
     814             : }
     815             : 
     816             : /**
     817             :  * @brief Get the remote address from a gensec security context.
     818             :  *
     819             :  * @param  gensec_security   The security context to get the address from.
     820             :  *
     821             :  * @return              The address as tsocket_address which could be NULL if
     822             :  *                      no address is set.
     823             :  */
     824      114036 : _PUBLIC_ const struct tsocket_address *gensec_get_remote_address(struct gensec_security *gensec_security)
     825             : {
     826      114036 :         if (gensec_security == NULL) {
     827           0 :                 return NULL;
     828             :         }
     829      114036 :         return gensec_security->remote_addr;
     830             : }
     831             : 
     832             : /**
     833             :  * Set the target principal (assuming it it known, say from the SPNEGO reply)
     834             :  *  - ensures it is talloc()ed
     835             :  *
     836             :  */
     837             : 
     838         242 : _PUBLIC_ NTSTATUS gensec_set_target_principal(struct gensec_security *gensec_security, const char *principal)
     839             : {
     840         242 :         gensec_security->target.principal = talloc_strdup(gensec_security, principal);
     841         242 :         if (!gensec_security->target.principal) {
     842           0 :                 return NT_STATUS_NO_MEMORY;
     843             :         }
     844         242 :         return NT_STATUS_OK;
     845             : }
     846             : 
     847      119606 : _PUBLIC_ const char *gensec_get_target_principal(struct gensec_security *gensec_security)
     848             : {
     849      119606 :         if (gensec_security->target.principal) {
     850         713 :                 return gensec_security->target.principal;
     851             :         }
     852             : 
     853      115815 :         return NULL;
     854             : }

Generated by: LCOV version 1.13