LCOV - code coverage report
Current view: top level - source3/utils - passwd_util.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 16 23 69.6 %
Date: 2024-02-28 12:06:22 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    passdb editing frontend
       4             : 
       5             :    Copyright (C) Jeremy Allison  1998
       6             :    Copyright (C) Andrew Tridgell 1998
       7             :    Copyright (C) Tim Potter      2000
       8             :    Copyright (C) Simo Sorce      2000
       9             :    Copyright (C) Martin Pool     2001
      10             :    Copyright (C) Gerald Carter   2002
      11             :    Copyright (C) Andrew Bartlett 2003
      12             : 
      13             :    This program is free software; you can redistribute it and/or modify
      14             :    it under the terms of the GNU General Public License as published by
      15             :    the Free Software Foundation; either version 3 of the License, or
      16             :    (at your option) any later version.
      17             :    
      18             :    This program is distributed in the hope that it will be useful,
      19             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      21             :    GNU General Public License for more details.
      22             :    
      23             :    You should have received a copy of the GNU General Public License
      24             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      25             : */
      26             : 
      27             : #include "includes.h"
      28             : #include "passwd_proto.h"
      29             : 
      30             : /*************************************************************
      31             :  Utility function to prompt for passwords from stdin. Each
      32             :  password entered must end with a newline.
      33             : *************************************************************/
      34        1000 : static char *stdin_new_passwd( void)
      35             : {
      36           0 :         static fstring new_pw;
      37           0 :         size_t len;
      38             : 
      39        1000 :         ZERO_ARRAY(new_pw);
      40             : 
      41             :         /*
      42             :          * if no error is reported from fgets() and string at least contains
      43             :          * the newline that ends the password, then replace the newline with
      44             :          * a null terminator.
      45             :          */
      46        1000 :         if ( fgets(new_pw, sizeof(new_pw), stdin) == NULL) {
      47           0 :                 return NULL;
      48             :         }
      49        1000 :         if ((len = strlen(new_pw)) > 0) {
      50        1000 :                 if(new_pw[len-1] == '\n')
      51        1000 :                         new_pw[len - 1] = 0;
      52             :         }
      53        1000 :         return(new_pw);
      54             : }
      55             : 
      56             : /*************************************************************
      57             :  Utility function to get passwords via tty or stdin
      58             :  Used if the '-s' (smbpasswd) or '-t' (pdbedit) option is set
      59             :  to silently get passwords to enable scripting.
      60             : *************************************************************/
      61        1042 : char *get_pass( const char *prompt, bool stdin_get)
      62             : {
      63        1042 :         char pwd[256] = {0};
      64           0 :         char *p;
      65           0 :         int rc;
      66             : 
      67        1042 :         if (stdin_get) {
      68        1000 :                 p = stdin_new_passwd();
      69        1000 :                 if (p == NULL) {
      70           0 :                         return NULL;
      71             :                 }
      72             :         } else {
      73          42 :                 rc = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
      74          42 :                 if (rc < 0) {
      75           0 :                         return NULL;
      76             :                 }
      77          42 :                 p = pwd;
      78             :         }
      79        1042 :         return smb_xstrdup( p);
      80             : }

Generated by: LCOV version 1.14