LCOV - code coverage report
Current view: top level - auth/credentials - credentials_cmdline.c (source / functions) Hit Total Coverage
Test: coverage report for support-claim-type-attributes 6b5c566e Lines: 22 26 84.6 %
Date: 2023-11-21 12:31:41 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          36 : static const char *cmdline_get_userpassword(struct cli_credentials *creds)
      24             : {
      25          36 :         TALLOC_CTX *frame = talloc_stackframe();
      26          36 :         const char *name = NULL;
      27          36 :         char *label = NULL;
      28          36 :         char *ret = NULL;
      29          36 :         char pwd[256] = {0};
      30           0 :         int rc;
      31             : 
      32          36 :         name = cli_credentials_get_unparsed_name(creds, frame);
      33          36 :         if (name == NULL) {
      34           0 :                 goto fail;
      35             :         }
      36          36 :         label = talloc_asprintf(frame, "Password for [%s]:", name);
      37          36 :         if (label == NULL) {
      38           0 :                 goto fail;
      39             :         }
      40          36 :         rc = samba_getpass(label, pwd, sizeof(pwd), false, false);
      41          36 :         if (rc != 0) {
      42           2 :                 goto fail;
      43             :         }
      44          34 :         ret = talloc_strdup(creds, pwd);
      45          34 :         if (ret == NULL) {
      46           0 :                 goto fail;
      47             :         }
      48          34 :         talloc_set_name_const(ret, __location__);
      49          36 : fail:
      50          36 :         ZERO_STRUCT(pwd);
      51          36 :         TALLOC_FREE(frame);
      52          36 :         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       17255 : 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       17255 :         return cli_credentials_set_password_callback(cred,
      72             :                                                      cmdline_get_userpassword);
      73             : }

Generated by: LCOV version 1.14