LCOV - code coverage report
Current view: top level - auth/credentials - credentials_cmdline.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 22 26 84.6 %
Date: 2024-04-13 12:30:31 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2005      Jelmer Vernooij <jelmer@samba.org>
       3             :  * Copyright (c) 2016      Stefan Metzmacher <metze@samba.org>
       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 "system/filesys.h"
      21             : #include "auth/credentials/credentials.h"
      22             : 
      23          38 : static const char *cmdline_get_userpassword(struct cli_credentials *creds)
      24             : {
      25          38 :         TALLOC_CTX *frame = talloc_stackframe();
      26          38 :         const char *name = NULL;
      27          38 :         char *label = NULL;
      28          38 :         char *ret = NULL;
      29          38 :         char pwd[256] = {0};
      30           0 :         int rc;
      31             : 
      32          38 :         name = cli_credentials_get_unparsed_name(creds, frame);
      33          38 :         if (name == NULL) {
      34           0 :                 goto fail;
      35             :         }
      36          38 :         label = talloc_asprintf(frame, "Password for [%s]:", name);
      37          38 :         if (label == NULL) {
      38           0 :                 goto fail;
      39             :         }
      40          38 :         rc = samba_getpass(label, pwd, sizeof(pwd), false, false);
      41          38 :         if (rc != 0) {
      42           2 :                 goto fail;
      43             :         }
      44          36 :         ret = talloc_strdup(creds, pwd);
      45          36 :         if (ret == NULL) {
      46           0 :                 goto fail;
      47             :         }
      48          36 :         talloc_set_name_const(ret, __location__);
      49          38 : fail:
      50          38 :         ZERO_STRUCT(pwd);
      51          38 :         TALLOC_FREE(frame);
      52          38 :         return ret;
      53             : }
      54             : 
      55             : /**
      56             :  * @brief Set the command line password callback.
      57             :  *
      58             :  * This will set the callback to get the password from the command prompt or
      59             :  * read it from 'stdin'.
      60             :  *
      61             :  * @param[in]  cred   The credential context.
      62             :  *
      63             :  * @return On success true, false otherwise.
      64             :  */
      65       17657 : bool cli_credentials_set_cmdline_callbacks(struct cli_credentials *cred)
      66             : {
      67             :         /*
      68             :          * If there is no tty, we will try to read the password from
      69             :          * stdin.
      70             :          */
      71       17657 :         return cli_credentials_set_password_callback(cred,
      72             :                                                      cmdline_get_userpassword);
      73             : }

Generated by: LCOV version 1.14