r9728: A *major* update to the credentials system, to incorporate the
[samba.git] / source / lib / cmdline / credentials.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Jelmer Vernooij 2005
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "version.h"
23 #include "dynconfig.h"
24 #include "system/filesys.h"
25 #include "system/passwd.h"
26 #include "lib/cmdline/popt_common.h"
27
28 static const char *cmdline_get_userpassword(struct cli_credentials *credentials)
29 {
30         char *prompt;
31         char *ret;
32         char *domain;
33         char *username;
34         TALLOC_CTX *mem_ctx = talloc_new(NULL);
35
36         domain = cli_credentials_get_domain(credentials);
37         username = cli_credentials_get_username(credentials, mem_ctx);
38         if (domain && domain[0]) {
39                 prompt = talloc_asprintf(mem_ctx, "Password for [%s\\%s]:", 
40                                          domain, username);
41         } else {
42                 prompt = talloc_asprintf(mem_ctx, "Password for [%s]:", 
43                                          username);
44         }
45
46         ret = getpass(prompt);
47
48         talloc_free(mem_ctx);
49         return ret;
50 }
51
52 void cli_credentials_set_cmdline_callbacks(struct cli_credentials *cred)
53 {
54         if (cred->password_obtained <= CRED_CALLBACK) {
55                 cred->password_cb = cmdline_get_userpassword;
56                 cred->password_obtained = CRED_CALLBACK;
57         }
58 }