r13524: Add -t|--password-from-stdin option to pdbedit as we had with Samba 2.2.
[kai/samba.git] / source3 / utils / passwd_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    passdb editing frontend
4    
5    Copyright (C) Simo Sorce      2000
6    Copyright (C) Andrew Bartlett 2001
7    Copyright (C) Jelmer Vernooij 2002
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /*************************************************************
27  Utility function to prompt for passwords from stdin. Each
28  password entered must end with a newline.
29 *************************************************************/
30 char *stdin_new_passwd( void)
31 {
32         static fstring new_pw;
33         size_t len;
34
35         ZERO_ARRAY(new_pw);
36
37         /*
38          * if no error is reported from fgets() and string at least contains
39          * the newline that ends the password, then replace the newline with
40          * a null terminator.
41          */
42         if ( fgets(new_pw, sizeof(new_pw), stdin) != NULL) {
43                 if ((len = strlen(new_pw)) > 0) {
44                         if(new_pw[len-1] == '\n')
45                                 new_pw[len - 1] = 0;
46                 }
47         }
48         return(new_pw);
49 }
50
51 /*************************************************************
52  Utility function to get passwords via tty or stdin
53  Used if the '-t' option is set to silently get passwords
54  to enable scripting.
55 *************************************************************/
56 char *get_pass( char *prompt, BOOL stdin_get)
57 {
58         char *p;
59         if (stdin_get) {
60                 p = stdin_new_passwd();
61         } else {
62                 p = getpass( prompt);
63         }
64         return smb_xstrdup( p);
65 }