LCOV - code coverage report
Current view: top level - auth/credentials - credentials_gmsa.c (source / functions) Hit Total Coverage
Test: coverage report for master 7edf5467 Lines: 0 39 0.0 %
Date: 2024-03-23 18:40:31 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    User credentials handling for Group Managed Service Accounts
       5             : 
       6             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2023
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "librpc/gen_ndr/ndr_gmsa.h" /* for struct MANAGEDPASSWORD_BLOB */
      24             : #include "auth/credentials/credentials.h"
      25             : #include "auth/credentials/credentials_internal.h"
      26             : #include "lib/util/charset/charset.h"
      27             : #include "lib/crypto/gkdi.h"
      28             : /*
      29             :  * All current callers set "for_keytab = true", but if we start using
      30             :  * this for getting a TGT we need the logic to ignore a very new
      31             :  * key
      32             :  */
      33           0 : NTSTATUS cli_credentials_set_gmsa_passwords(struct cli_credentials *creds,
      34             :                                             const DATA_BLOB *managed_password_blob,
      35             :                                             bool for_keytab,
      36             :                                             const char **error_string)
      37             : {
      38           0 :         struct MANAGEDPASSWORD_BLOB managed_password;
      39           0 :         DATA_BLOB managed_pw_utf16;
      40           0 :         DATA_BLOB previous_managed_pw_utf16;
      41           0 :         enum ndr_err_code ndr_err;
      42           0 :         TALLOC_CTX *frame = talloc_stackframe();
      43             : 
      44             :         /*
      45             :          * We check if this is 'for keytab' as a keytab wants to know
      46             :          * about a near-future password as it will be on disk for some
      47             :          * time
      48             :          */
      49           0 :         bool only_use_previous_pw =
      50           0 :                 managed_password.passwords.query_interval != NULL
      51           0 :                 && *managed_password.passwords.query_interval <= gkdi_max_clock_skew
      52           0 :                 && for_keytab == false;
      53             : 
      54             :         /*
      55             :          * Group Managed Service Accounts are type
      56             :          * UF_WORKSTATION_TRUST_ACCOUNT and will follow those salting
      57             :          * rules
      58             :          */
      59           0 :         cli_credentials_set_secure_channel_type(creds, SEC_CHAN_WKSTA);
      60             : 
      61           0 :         ndr_err = ndr_pull_struct_blob_all(managed_password_blob,
      62             :                                            frame,
      63             :                                            &managed_password,
      64             :                                            (ndr_pull_flags_fn_t)ndr_pull_MANAGEDPASSWORD_BLOB);
      65           0 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
      66           0 :                 *error_string = talloc_asprintf(creds,
      67             :                                                 "Failed to parse msDS-ManagedPassword "
      68             :                                                 "as MANAGEDPASSWORD_BLOB");
      69           0 :                 TALLOC_FREE(frame);
      70           0 :                 return NT_STATUS_ILL_FORMED_PASSWORD;
      71             :         }
      72             : 
      73             :         /*
      74             :          * We look at the old password first as we might bail out
      75             :          * early if the new password is "too fresh"
      76             :          */
      77           0 :         if (managed_password.passwords.previous) {
      78           0 :                 previous_managed_pw_utf16
      79           0 :                         = data_blob_const(managed_password.passwords.previous,
      80           0 :                                           utf16_len(managed_password.passwords.previous));
      81             : 
      82           0 :                 cli_credentials_set_old_utf16_password(creds, &previous_managed_pw_utf16);
      83             : 
      84           0 :                 if (only_use_previous_pw) {
      85             :                         /* We are in the 5 mins where we know the next
      86             :                          * password, but it will not be available yet, just
      87             :                          * use the old password for now.
      88             :                          */
      89           0 :                         cli_credentials_set_utf16_password(creds, &previous_managed_pw_utf16,
      90             :                                                            CRED_SPECIFIED);
      91             : 
      92             :                         /*
      93             :                          * We are done, the new password is of no
      94             :                          * interest to us
      95             :                          */
      96           0 :                         TALLOC_FREE(frame);
      97           0 :                         return NT_STATUS_OK;
      98             :                 }
      99             : 
     100             :         }
     101             : 
     102           0 :         if (only_use_previous_pw) {
     103           0 :                 *error_string = talloc_asprintf(creds,
     104             :                                                 "No old password but new password is too new "
     105             :                                                 "(< 5min) in msDS-ManagedPassword "
     106             :                                                 "MANAGEDPASSWORD_BLOB");
     107           0 :                 TALLOC_FREE(frame);
     108           0 :                 return NT_STATUS_ILL_FORMED_PASSWORD;
     109             :         }
     110             : 
     111           0 :         if (managed_password.passwords.current == NULL) {
     112           0 :                 *error_string = talloc_asprintf(creds,
     113             :                                                 "Failed to find new password in msDS-ManagedPassword "
     114             :                                                 "MANAGEDPASSWORD_BLOB");
     115           0 :                 TALLOC_FREE(frame);
     116           0 :                 return NT_STATUS_ILL_FORMED_PASSWORD;
     117             :         }
     118             : 
     119           0 :         managed_pw_utf16
     120           0 :                 = data_blob_const(managed_password.passwords.current,
     121           0 :                                   utf16_len(managed_password.passwords.current));
     122             : 
     123           0 :         cli_credentials_set_utf16_password(creds, &managed_pw_utf16,
     124             :                                            CRED_SPECIFIED);
     125             : 
     126           0 :         TALLOC_FREE(frame);
     127           0 :         return NT_STATUS_OK;
     128             : }

Generated by: LCOV version 1.14