LCOV - code coverage report
Current view: top level - source3/auth - auth_samba4.c (source / functions) Hit Total Coverage
Test: coverage report for recycleplus df22b230 Lines: 100 163 61.3 %
Date: 2024-02-14 10:14:15 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Authenticate against Samba4's auth subsystem
       4             :    Copyright (C) Volker Lendecke 2008
       5             :    Copyright (C) Andrew Bartlett 2010
       6             : 
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             : 
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include "includes.h"
      22             : #include "source3/include/auth.h"
      23             : #include "source3/include/messages.h"
      24             : #include "source4/auth/auth.h"
      25             : #include "auth/auth_sam_reply.h"
      26             : #include "param/param.h"
      27             : #include "source4/lib/events/events.h"
      28             : #include "source4/lib/messaging/messaging.h"
      29             : #include "auth/gensec/gensec.h"
      30             : #include "auth/credentials/credentials.h"
      31             : #include "lib/global_contexts.h"
      32             : #include "lib/util/idtree.h"
      33             : 
      34             : #undef DBGC_CLASS
      35             : #define DBGC_CLASS DBGC_AUTH
      36             : 
      37             : static NTSTATUS make_auth4_context_s4(const struct auth_context *auth_context,
      38             :                                       TALLOC_CTX *mem_ctx,
      39             :                                       struct auth4_context **auth4_context);
      40             : 
      41             : static struct idr_context *task_id_tree;
      42             : 
      43        9948 : static int free_task_id(struct server_id *server_id)
      44             : {
      45        9948 :         idr_remove(task_id_tree, server_id->task_id);
      46        9948 :         return 0;
      47             : }
      48             : 
      49             : /* Return a server_id with a unique task_id element.  Free the
      50             :  * returned pointer to de-allocate the task_id via a talloc destructor
      51             :  * (ie, use talloc_free()) */
      52        9948 : static struct server_id *new_server_id_task(TALLOC_CTX *mem_ctx)
      53             : {
      54             :         struct messaging_context *msg_ctx;
      55             :         struct server_id *server_id;
      56             :         int task_id;
      57        9948 :         if (!task_id_tree) {
      58        3713 :                 task_id_tree = idr_init(NULL);
      59        3713 :                 if (!task_id_tree) {
      60           0 :                         return NULL;
      61             :                 }
      62             :         }
      63             : 
      64        9948 :         msg_ctx = global_messaging_context();
      65        9948 :         if (msg_ctx == NULL) {
      66           0 :                 return NULL;
      67             :         }
      68             : 
      69        9948 :         server_id = talloc(mem_ctx, struct server_id);
      70             : 
      71        9948 :         if (!server_id) {
      72           0 :                 return NULL;
      73             :         }
      74        9948 :         *server_id = messaging_server_id(msg_ctx);
      75             : 
      76             :         /* 0 is the default server_id, so we need to start with 1 */
      77        9948 :         task_id = idr_get_new_above(task_id_tree, server_id, 1, INT32_MAX);
      78             : 
      79        9948 :         if (task_id == -1) {
      80           0 :                 talloc_free(server_id);
      81           0 :                 return NULL;
      82             :         }
      83             : 
      84        9948 :         talloc_set_destructor(server_id, free_task_id);
      85        9948 :         server_id->task_id = task_id;
      86        9948 :         return server_id;
      87             : }
      88             : 
      89             : /*
      90             :  * This module is not an ordinary authentication module.  It is really
      91             :  * a way to redirect the whole authentication and authorization stack
      92             :  * to use the source4 auth code, not a way to just handle NTLM
      93             :  * authentication.
      94             :  *
      95             :  * See the comments above each function for how that hook changes the
      96             :  * behaviour.
      97             :  */
      98             : 
      99             : /* 
     100             :  * This hook is currently used by winbindd only, as all other NTLM
     101             :  * logins go via the hooks provided by make_auth4_context_s4() below.
     102             :  *
     103             :  * This is only left in case we find a way that it might become useful
     104             :  * in future.  Importantly, this routine returns the information
     105             :  * needed for a NETLOGON SamLogon, not what is needed to establish a
     106             :  * session.
     107             :  *
     108             :  * We expect we may use this hook in the source3/ winbind when this
     109             :  * services the AD DC.  It is tested via pdbtest.
     110             :  */
     111             : 
     112           1 : static NTSTATUS check_samba4_security(
     113             :         const struct auth_context *auth_context,
     114             :         void *my_private_data,
     115             :         TALLOC_CTX *mem_ctx,
     116             :         const struct auth_usersupplied_info *user_info,
     117             :         struct auth_serversupplied_info **pserver_info)
     118             : {
     119           1 :         TALLOC_CTX *frame = talloc_stackframe();
     120           1 :         struct netr_SamInfo3 *info3 = NULL;
     121             :         NTSTATUS nt_status;
     122             :         struct auth_user_info_dc *user_info_dc;
     123             :         struct auth4_context *auth4_context;
     124           1 :         uint8_t authoritative = 1;
     125           1 :         struct auth_serversupplied_info *server_info = NULL;
     126             : 
     127           1 :         nt_status = make_auth4_context_s4(auth_context, mem_ctx, &auth4_context);
     128           1 :         if (!NT_STATUS_IS_OK(nt_status)) {
     129           0 :                 TALLOC_FREE(frame);
     130           0 :                 goto done;
     131             :         }
     132             :                 
     133           1 :         nt_status = auth_context_set_challenge(auth4_context, auth_context->challenge.data, "auth_samba4");
     134           1 :         if (!NT_STATUS_IS_OK(nt_status)) {
     135           0 :                 TALLOC_FREE(auth4_context);
     136           0 :                 TALLOC_FREE(frame);
     137           0 :                 return nt_status;
     138             :         }
     139             : 
     140           1 :         nt_status = auth_check_password(auth4_context, auth4_context, user_info,
     141             :                                         &user_info_dc, &authoritative);
     142           1 :         if (!NT_STATUS_IS_OK(nt_status)) {
     143           0 :                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) &&
     144           0 :                                     authoritative == 0)
     145             :                 {
     146           0 :                         nt_status = NT_STATUS_NOT_IMPLEMENTED;
     147             :                 }
     148           0 :                 TALLOC_FREE(auth4_context);
     149           0 :                 TALLOC_FREE(frame);
     150           0 :                 return nt_status;
     151             :         }
     152             : 
     153           1 :         nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx,
     154             :                                                        user_info_dc,
     155             :                                                        &info3);
     156           1 :         if (NT_STATUS_IS_OK(nt_status)) {
     157             :                 /* We need the strings from the server_info to be valid as long as the info3 is around */
     158           1 :                 talloc_steal(info3, user_info_dc);
     159             :         }
     160           1 :         talloc_free(auth4_context);
     161             : 
     162           1 :         if (!NT_STATUS_IS_OK(nt_status)) {
     163           0 :                 goto done;
     164             :         }
     165             : 
     166           1 :         if (user_info->flags & USER_INFO_INFO3_AND_NO_AUTHZ) {
     167           0 :                 server_info = make_server_info(mem_ctx);
     168           0 :                 if (server_info == NULL) {
     169           0 :                         nt_status = NT_STATUS_NO_MEMORY;
     170           0 :                         goto done;
     171             :                 }
     172           0 :                 server_info->info3 = talloc_move(server_info, &info3);
     173             :         } else {
     174           1 :                 nt_status = make_server_info_info3(
     175             :                         mem_ctx,
     176           1 :                         user_info->client.account_name,
     177           1 :                         user_info->mapped.domain_name,
     178             :                         &server_info,
     179             :                         info3);
     180           1 :                 if (!NT_STATUS_IS_OK(nt_status)) {
     181           0 :                         DEBUG(10, ("make_server_info_info3 failed: %s\n",
     182             :                                    nt_errstr(nt_status)));
     183           0 :                         goto done;
     184             :                 }
     185             :         }
     186             : 
     187           1 :         *pserver_info = server_info;
     188           1 :         nt_status = NT_STATUS_OK;
     189             : 
     190           1 :  done:
     191           1 :         TALLOC_FREE(frame);
     192           1 :         return nt_status;
     193             : }
     194             : 
     195             : /*
     196             :  * Hook to allow the source4 set of GENSEC modules to handle
     197             :  * blob-based authentication mechanisms, without directly linking the
     198             :  * mechanism code.
     199             :  *
     200             :  * This may eventually go away, when the GSSAPI acceptors are merged,
     201             :  * when we will just rely on the make_auth4_context_s4 hook instead.
     202             :  *
     203             :  * Even for NTLMSSP, which has a common module, significant parts of
     204             :  * the behaviour are overridden here, because it uses the source4 NTLM
     205             :  * stack and the source4 mapping between the PAC/SamLogon response and
     206             :  * the local token.
     207             :  *
     208             :  * It is important to override all this to ensure that the exact same
     209             :  * token is generated and used in the SMB and LDAP servers, for NTLM
     210             :  * and for Kerberos.
     211             :  */
     212        9924 : static NTSTATUS prepare_gensec(const struct auth_context *auth_context,
     213             :                                TALLOC_CTX *mem_ctx,
     214             :                                struct gensec_security **gensec_context)
     215             : {
     216             :         NTSTATUS status;
     217             :         struct loadparm_context *lp_ctx;
     218             :         struct tevent_context *event_ctx;
     219        9924 :         TALLOC_CTX *frame = talloc_stackframe();
     220             :         struct gensec_security *gensec_ctx;
     221             :         struct imessaging_context *msg_ctx;
     222             :         struct cli_credentials *server_credentials;
     223             :         struct server_id *server_id;
     224             : 
     225        9924 :         lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
     226        9924 :         if (lp_ctx == NULL) {
     227           0 :                 DEBUG(1, ("loadparm_init_s3 failed\n"));
     228           0 :                 TALLOC_FREE(frame);
     229           0 :                 return NT_STATUS_INVALID_SERVER_STATE;
     230             :         }
     231        9924 :         event_ctx = s4_event_context_init(frame);
     232        9924 :         if (event_ctx == NULL) {
     233           0 :                 DEBUG(1, ("s4_event_context_init failed\n"));
     234           0 :                 TALLOC_FREE(frame);
     235           0 :                 return NT_STATUS_INVALID_SERVER_STATE;
     236             :         }
     237             : 
     238        9924 :         server_id = new_server_id_task(frame);
     239        9924 :         if (server_id == NULL) {
     240           0 :                 DEBUG(1, ("new_server_id_task failed\n"));
     241           0 :                 TALLOC_FREE(frame);
     242           0 :                 return NT_STATUS_INVALID_SERVER_STATE;
     243             :         }
     244             : 
     245        9924 :         msg_ctx = imessaging_init_discard_incoming(frame,
     246             :                                   lp_ctx,
     247             :                                   *server_id,
     248             :                                   event_ctx);
     249        9924 :         if (msg_ctx == NULL) {
     250           0 :                 DEBUG(1, ("imessaging_init_discard_incoming failed\n"));
     251           0 :                 TALLOC_FREE(frame);
     252           0 :                 return NT_STATUS_INVALID_SERVER_STATE;
     253             :         }
     254             : 
     255        9924 :         talloc_reparent(frame, msg_ctx, server_id);
     256             : 
     257             :         server_credentials
     258        9924 :                 = cli_credentials_init_server(frame, lp_ctx);
     259        9924 :         if (!server_credentials) {
     260           0 :                 DEBUG(1, ("Failed to init server credentials"));
     261           0 :                 TALLOC_FREE(frame);
     262           0 :                 return NT_STATUS_INVALID_SERVER_STATE;
     263             :         }
     264             : 
     265        9924 :         status = samba_server_gensec_start(mem_ctx,
     266             :                                            event_ctx, msg_ctx,
     267             :                                            lp_ctx, server_credentials, "cifs",
     268             :                                            &gensec_ctx);
     269        9924 :         if (!NT_STATUS_IS_OK(status)) {
     270           0 :                 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
     271           0 :                 TALLOC_FREE(frame);
     272           0 :                 return status;
     273             :         }
     274             : 
     275        9924 :         talloc_reparent(frame, gensec_ctx, msg_ctx);
     276        9924 :         talloc_reparent(frame, gensec_ctx, event_ctx);
     277        9924 :         talloc_reparent(frame, gensec_ctx, lp_ctx);
     278        9924 :         talloc_reparent(frame, gensec_ctx, server_credentials);
     279             : 
     280        9924 :         gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
     281        9924 :         gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
     282             : 
     283        9924 :         *gensec_context = gensec_ctx;
     284        9924 :         TALLOC_FREE(frame);
     285        9924 :         return status;
     286             : }
     287             : 
     288             : /*
     289             :  * Hook to allow handling of NTLM authentication for AD operation
     290             :  * without directly linking the s4 auth stack
     291             :  *
     292             :  * This ensures we use the source4 authentication stack, as well as
     293             :  * the authorization stack to create the user's token.  This ensures
     294             :  * consistency between NTLM logins and NTLMSSP logins, as NTLMSSP is
     295             :  * handled by the hook above.
     296             :  */
     297          24 : static NTSTATUS make_auth4_context_s4(const struct auth_context *auth_context,
     298             :                                       TALLOC_CTX *mem_ctx,
     299             :                                       struct auth4_context **auth4_context)
     300             : {
     301             :         NTSTATUS status;
     302             :         struct loadparm_context *lp_ctx;
     303             :         struct tevent_context *event_ctx;
     304          24 :         TALLOC_CTX *frame = talloc_stackframe();
     305             :         struct imessaging_context *msg_ctx;
     306             :         struct server_id *server_id;
     307             : 
     308          24 :         lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
     309          24 :         if (lp_ctx == NULL) {
     310           0 :                 DEBUG(1, ("loadparm_init_s3 failed\n"));
     311           0 :                 TALLOC_FREE(frame);
     312           0 :                 return NT_STATUS_INVALID_SERVER_STATE;
     313             :         }
     314          24 :         event_ctx = s4_event_context_init(frame);
     315          24 :         if (event_ctx == NULL) {
     316           0 :                 DEBUG(1, ("s4_event_context_init failed\n"));
     317           0 :                 TALLOC_FREE(frame);
     318           0 :                 return NT_STATUS_INVALID_SERVER_STATE;
     319             :         }
     320             : 
     321          24 :         server_id = new_server_id_task(frame);
     322          24 :         if (server_id == NULL) {
     323           0 :                 DEBUG(1, ("new_server_id_task failed\n"));
     324           0 :                 TALLOC_FREE(frame);
     325           0 :                 return NT_STATUS_INVALID_SERVER_STATE;
     326             :         }
     327             : 
     328          24 :         msg_ctx = imessaging_init_discard_incoming(frame,
     329             :                                   lp_ctx,
     330             :                                   *server_id,
     331             :                                   event_ctx);
     332          24 :         if (msg_ctx == NULL) {
     333           0 :                 DEBUG(1, ("imessaging_init_discard_incoming failed\n"));
     334           0 :                 TALLOC_FREE(frame);
     335           0 :                 return NT_STATUS_INVALID_SERVER_STATE;
     336             :         }
     337          24 :         talloc_reparent(frame, msg_ctx, server_id);
     338             : 
     339             :         /* Allow forcing a specific auth4 module */
     340          24 :         if (!auth_context->forced_samba4_methods) {
     341          24 :                 status = auth_context_create(mem_ctx,
     342             :                                              event_ctx,
     343             :                                              msg_ctx,
     344             :                                              lp_ctx,
     345             :                                              auth4_context);
     346             :         } else {
     347           0 :                 const char * const *forced_auth_methods = (const char * const *)str_list_make(mem_ctx, auth_context->forced_samba4_methods, NULL);
     348           0 :                 status = auth_context_create_methods(mem_ctx, forced_auth_methods, event_ctx, msg_ctx, lp_ctx, NULL, auth4_context);
     349             :         }
     350          24 :         if (!NT_STATUS_IS_OK(status)) {
     351           0 :                 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status)));
     352           0 :                 TALLOC_FREE(frame);
     353           0 :                 return status;
     354             :         }
     355             : 
     356          24 :         talloc_reparent(frame, *auth4_context, msg_ctx);
     357          24 :         talloc_reparent(frame, *auth4_context, event_ctx);
     358          24 :         talloc_reparent(frame, *auth4_context, lp_ctx);
     359             : 
     360          24 :         TALLOC_FREE(frame);
     361          24 :         return status;
     362             : }
     363             : 
     364             : /* module initialisation */
     365        9948 : static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
     366             :                                  const char *param,
     367             :                                  struct auth_methods **auth_method)
     368             : {
     369             :         struct auth_methods *result;
     370             : 
     371        9948 :         gensec_init();
     372             : 
     373        9948 :         result = talloc_zero(auth_context, struct auth_methods);
     374        9948 :         if (result == NULL) {
     375           0 :                 return NT_STATUS_NO_MEMORY;
     376             :         }
     377        9948 :         result->name = "samba4";
     378        9948 :         result->auth = check_samba4_security;
     379        9948 :         result->prepare_gensec = prepare_gensec;
     380        9948 :         result->make_auth4_context = make_auth4_context_s4;
     381             : 
     382        9948 :         if (param && *param) {
     383           0 :                 auth_context->forced_samba4_methods = talloc_strdup(result, param);
     384           0 :                 if (!auth_context->forced_samba4_methods) {
     385           0 :                         return NT_STATUS_NO_MEMORY;
     386             :                 }
     387             :         }
     388             : 
     389        9948 :         *auth_method = result;
     390        9948 :         return NT_STATUS_OK;
     391             : }
     392             : 
     393             : NTSTATUS auth_samba4_init(TALLOC_CTX *mem_ctx);
     394        5093 : NTSTATUS auth_samba4_init(TALLOC_CTX *mem_ctx)
     395             : {
     396        5093 :         smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
     397             :                           auth_init_samba4);
     398        5093 :         return NT_STATUS_OK;
     399             : }

Generated by: LCOV version 1.14