LCOV - code coverage report
Current view: top level - source3/passdb - secrets_lsa.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 0 108 0.0 %
Date: 2024-02-28 12:06:22 Functions: 0 6 0.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Copyright (C) Guenther Deschner    2009
       4             : 
       5             :    This program is free software; you can redistribute it and/or modify
       6             :    it under the terms of the GNU General Public License as published by
       7             :    the Free Software Foundation; either version 3 of the License, or
       8             :    (at your option) any later version.
       9             : 
      10             :    This program is distributed in the hope that it will be useful,
      11             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :    GNU General Public License for more details.
      14             : 
      15             :    You should have received a copy of the GNU General Public License
      16             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      17             : */
      18             : 
      19             : #include "includes.h"
      20             : #include "librpc/gen_ndr/ndr_secrets.h"
      21             : #include "secrets.h"
      22             : 
      23             : /******************************************************************************
      24             : *******************************************************************************/
      25             : 
      26           0 : static char *lsa_secret_key(TALLOC_CTX *mem_ctx,
      27             :                             const char *secret_name)
      28             : {
      29           0 :         return talloc_asprintf_strupper_m(mem_ctx, "SECRETS/LSA/%s",
      30             :                                           secret_name);
      31             : }
      32             : 
      33             : /******************************************************************************
      34             : *******************************************************************************/
      35             : 
      36           0 : static NTSTATUS lsa_secret_get_common(TALLOC_CTX *mem_ctx,
      37             :                                       const char *secret_name,
      38             :                                       struct lsa_secret *secret)
      39             : {
      40           0 :         char *key;
      41           0 :         DATA_BLOB blob;
      42           0 :         enum ndr_err_code ndr_err;
      43             : 
      44           0 :         ZERO_STRUCTP(secret);
      45             : 
      46           0 :         key = lsa_secret_key(mem_ctx, secret_name);
      47           0 :         if (!key) {
      48           0 :                 return NT_STATUS_NO_MEMORY;
      49             :         }
      50             : 
      51           0 :         blob.data = (uint8_t *)secrets_fetch(key, &blob.length);
      52           0 :         talloc_free(key);
      53             : 
      54           0 :         if (!blob.data) {
      55           0 :                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
      56             :         }
      57             : 
      58           0 :         ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, secret,
      59             :                                 (ndr_pull_flags_fn_t)ndr_pull_lsa_secret);
      60           0 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
      61           0 :                 SAFE_FREE(blob.data);
      62           0 :                 return ndr_map_error2ntstatus(ndr_err);
      63             :         }
      64             : 
      65             :         /* This is NOT a talloc blob */
      66           0 :         BURN_FREE(blob.data, blob.length);
      67             : 
      68           0 :         if (secret->secret_current != NULL &&
      69           0 :             secret->secret_current->data != NULL) {
      70           0 :                 talloc_keep_secret(secret->secret_current->data);
      71             :         }
      72           0 :         if (secret->secret_old != NULL && secret->secret_old->data != NULL) {
      73           0 :                 talloc_keep_secret(secret->secret_old->data);
      74             :         }
      75             : 
      76           0 :         return NT_STATUS_OK;
      77             : }
      78             : 
      79             : /******************************************************************************
      80             : *******************************************************************************/
      81             : 
      82           0 : NTSTATUS lsa_secret_get(TALLOC_CTX *mem_ctx,
      83             :                         const char *secret_name,
      84             :                         DATA_BLOB *secret_current,
      85             :                         NTTIME *secret_current_lastchange,
      86             :                         DATA_BLOB *secret_old,
      87             :                         NTTIME *secret_old_lastchange,
      88             :                         struct security_descriptor **sd)
      89             : {
      90           0 :         NTSTATUS status;
      91           0 :         struct lsa_secret secret;
      92             : 
      93           0 :         status = lsa_secret_get_common(mem_ctx, secret_name, &secret);
      94           0 :         if (!NT_STATUS_IS_OK(status)) {
      95           0 :                 return status;
      96             :         }
      97             : 
      98           0 :         if (secret_current) {
      99           0 :                 *secret_current = data_blob_null;
     100           0 :                 if (secret.secret_current) {
     101           0 :                         *secret_current = *secret.secret_current;
     102             :                 }
     103             :         }
     104           0 :         if (secret_current_lastchange) {
     105           0 :                 *secret_current_lastchange = secret.secret_current_lastchange;
     106             :         }
     107           0 :         if (secret_old) {
     108           0 :                 *secret_old = data_blob_null;
     109           0 :                 if (secret.secret_old) {
     110           0 :                         *secret_old = *secret.secret_old;
     111             :                 }
     112             :         }
     113           0 :         if (secret_old_lastchange) {
     114           0 :                 *secret_old_lastchange = secret.secret_old_lastchange;
     115             :         }
     116           0 :         if (sd) {
     117           0 :                 *sd = secret.sd;
     118             :         }
     119             : 
     120           0 :         return NT_STATUS_OK;
     121             : }
     122             : 
     123             : /******************************************************************************
     124             : *******************************************************************************/
     125             : 
     126           0 : static NTSTATUS lsa_secret_set_common(TALLOC_CTX *mem_ctx,
     127             :                                       const char *key,
     128             :                                       struct lsa_secret *secret,
     129             :                                       DATA_BLOB *secret_current,
     130             :                                       DATA_BLOB *secret_old,
     131             :                                       struct security_descriptor *sd)
     132             : {
     133           0 :         enum ndr_err_code ndr_err;
     134           0 :         DATA_BLOB blob;
     135           0 :         struct timeval now = timeval_current();
     136             : 
     137           0 :         if (!secret) {
     138           0 :                 secret = talloc_zero(mem_ctx, struct lsa_secret);
     139             :         }
     140             : 
     141           0 :         if (!secret) {
     142           0 :                 return NT_STATUS_NO_MEMORY;
     143             :         }
     144             : 
     145           0 :         if (secret_old) {
     146           0 :                 secret->secret_old = secret_old;
     147           0 :                 secret->secret_old_lastchange = timeval_to_nttime(&now);
     148             :         } else {
     149           0 :                 if (secret->secret_current) {
     150           0 :                         secret->secret_old = secret->secret_current;
     151           0 :                         secret->secret_old_lastchange = secret->secret_current_lastchange;
     152             :                 } else {
     153           0 :                         secret->secret_old = NULL;
     154           0 :                         secret->secret_old_lastchange = timeval_to_nttime(&now);
     155             :                 }
     156             :         }
     157           0 :         if (secret_current) {
     158           0 :                 secret->secret_current = secret_current;
     159           0 :                 secret->secret_current_lastchange = timeval_to_nttime(&now);
     160             :         } else {
     161           0 :                 secret->secret_current = NULL;
     162           0 :                 secret->secret_current_lastchange = timeval_to_nttime(&now);
     163             :         }
     164           0 :         if (sd) {
     165           0 :                 secret->sd = sd;
     166             :         }
     167             : 
     168           0 :         ndr_err = ndr_push_struct_blob(&blob, mem_ctx, secret,
     169             :                                 (ndr_push_flags_fn_t)ndr_push_lsa_secret);
     170           0 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     171           0 :                 return ndr_map_error2ntstatus(ndr_err);
     172             :         }
     173             : 
     174           0 :         if (!secrets_store(key, blob.data, blob.length)) {
     175           0 :                 data_blob_clear(&blob);
     176           0 :                 return NT_STATUS_ACCESS_DENIED;
     177             :         }
     178             : 
     179           0 :         data_blob_clear(&blob);
     180           0 :         return NT_STATUS_OK;
     181             : }
     182             : 
     183             : /******************************************************************************
     184             : *******************************************************************************/
     185             : 
     186           0 : NTSTATUS lsa_secret_set(const char *secret_name,
     187             :                         DATA_BLOB *secret_current,
     188             :                         DATA_BLOB *secret_old,
     189             :                         struct security_descriptor *sd)
     190             : {
     191           0 :         char *key;
     192           0 :         struct lsa_secret secret;
     193           0 :         NTSTATUS status;
     194             : 
     195           0 :         key = lsa_secret_key(talloc_tos(), secret_name);
     196           0 :         if (!key) {
     197           0 :                 return NT_STATUS_NO_MEMORY;
     198             :         }
     199             : 
     200           0 :         status = lsa_secret_get_common(talloc_tos(), secret_name, &secret);
     201           0 :         if (!NT_STATUS_IS_OK(status) &&
     202           0 :             !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
     203           0 :                 talloc_free(key);
     204           0 :                 return status;
     205             :         }
     206             : 
     207           0 :         status = lsa_secret_set_common(talloc_tos(), key,
     208             :                                        &secret,
     209             :                                        secret_current,
     210             :                                        secret_old,
     211             :                                        sd);
     212           0 :         talloc_free(key);
     213             : 
     214           0 :         return status;
     215             : }
     216             : 
     217             : /******************************************************************************
     218             : *******************************************************************************/
     219             : 
     220           0 : NTSTATUS lsa_secret_delete(const char *secret_name)
     221             : {
     222           0 :         char *key;
     223           0 :         struct lsa_secret secret;
     224           0 :         NTSTATUS status;
     225             : 
     226           0 :         key = lsa_secret_key(talloc_tos(), secret_name);
     227           0 :         if (!key) {
     228           0 :                 return NT_STATUS_NO_MEMORY;
     229             :         }
     230             : 
     231           0 :         status = lsa_secret_get_common(talloc_tos(), secret_name, &secret);
     232           0 :         if (!NT_STATUS_IS_OK(status)) {
     233           0 :                 talloc_free(key);
     234           0 :                 return status;
     235             :         }
     236             : 
     237           0 :         if (!secrets_delete_entry(key)) {
     238           0 :                 talloc_free(key);
     239           0 :                 return NT_STATUS_ACCESS_DENIED;
     240             :         }
     241             : 
     242           0 :         talloc_free(key);
     243             : 
     244           0 :         return NT_STATUS_OK;
     245             : }

Generated by: LCOV version 1.14