f9a914326f6a81baccdb43950c75cdd9e565e05b
[samba.git] / source / lib / cmdline / popt_credentials.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Credentials popt routines
4
5    Copyright (C) Jelmer Vernooij 2002,2003,2005
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "lib/cmdline/credentials.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
27
28 /* Handle command line options:
29  *              -U,--user
30  *              -A,--authentication-file
31  *              -k,--use-kerberos
32  *              -N,--no-pass
33  *              -S,--signing
34  *              -P --machine-pass
35  *                 --simple-bind-dn
36  *                 --password
37  *                 --use-security-mechanisms
38  */
39
40
41 static BOOL dont_ask;
42
43 enum opt { OPT_SIMPLE_BIND_DN, OPT_PASSWORD, OPT_KERBEROS, OPT_GENSEC_MECHS };
44
45 /*
46   disable asking for a password
47 */
48 void popt_common_dont_ask(void)
49 {
50         dont_ask = True;
51 }
52
53 static void popt_common_credentials_callback(poptContext con, 
54                                                 enum poptCallbackReason reason,
55                                                 const struct poptOption *opt,
56                                                 const char *arg, const void *data)
57 {
58         if (reason == POPT_CALLBACK_REASON_PRE) {
59                 cmdline_credentials = cli_credentials_init(talloc_autofree_context());
60                 return;
61         }
62         
63         if (reason == POPT_CALLBACK_REASON_POST) {
64                 cli_credentials_guess(cmdline_credentials);
65
66                 if (!dont_ask) {
67                         cli_credentials_set_cmdline_callbacks(cmdline_credentials);
68                 }
69                 return;
70         }
71
72         switch(opt->val) {
73         case 'U':
74         {
75                 char *lp;
76                 
77                 cli_credentials_parse_string(cmdline_credentials, arg, CRED_SPECIFIED);
78                 /* This breaks the abstraction, including the const above */
79                 if ((lp=strchr_m(arg,'%'))) {
80                         lp[0]='\0';
81                         lp++;
82                         /* Try to prevent this showing up in ps */
83                         memset(lp,0,strlen(lp));
84                 }
85         }
86         break;
87
88         case OPT_PASSWORD:
89                 cli_credentials_set_password(cmdline_credentials, arg, CRED_SPECIFIED);
90                 /* Try to prevent this showing up in ps */
91                 memset(discard_const(arg),0,strlen(arg));
92                 break;
93
94         case 'A':
95                 cli_credentials_parse_file(cmdline_credentials, arg, CRED_SPECIFIED);
96                 break;
97
98         case 'S':
99                 lp_set_cmdline("client signing", arg);
100                 break;
101
102         case 'P':
103                 /* Later, after this is all over, get the machine account details from the secrets.ldb */
104                 cli_credentials_set_machine_account_pending(cmdline_credentials);
105                 break;
106
107         case OPT_KERBEROS:
108         {
109                 BOOL use_kerberos = True;
110                 /* Force us to only use kerberos */
111                 if (arg) {
112                         if (!set_boolean(arg, &use_kerberos)) {
113                                 fprintf(stderr, "Error parsing -k %s\n", arg);
114                                 exit(1);
115                                 break;
116                         }
117                 }
118                 
119                 cli_credentials_set_kerberos_state(cmdline_credentials, 
120                                                    use_kerberos 
121                                                    ? CRED_MUST_USE_KERBEROS
122                                                    : CRED_DONT_USE_KERBEROS);
123                 break;
124         }
125         case OPT_GENSEC_MECHS:
126                 /* Convert a list of strings into a list of available authentication standards */
127                 
128                 break;
129                 
130         case OPT_SIMPLE_BIND_DN:
131                 cli_credentials_set_bind_dn(cmdline_credentials, arg);
132                 break;
133         }
134 }
135
136
137
138 struct poptOption popt_common_credentials[] = {
139         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_credentials_callback },
140         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN\\]USERNAME[%PASSWORD]" },
141         { "no-pass", 'N', POPT_ARG_NONE, &dont_ask, True, "Don't ask for a password" },
142         { "password", 0, POPT_ARG_STRING, NULL, OPT_PASSWORD, "Password" },
143         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
144         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
145         { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password (implies -k)" },
146         { "simple-bind-dn", 0, POPT_ARG_STRING, NULL, OPT_SIMPLE_BIND_DN, "DN to use for a simple bind" },
147         { "kerberos", 'k', POPT_ARG_STRING, NULL, OPT_KERBEROS, "Use Kerberos" },
148         { "use-security-mechanisms", 0, POPT_ARG_STRING, NULL, OPT_GENSEC_MECHS, "Restricted list of authentication mechanisms available for use with this authentication"},
149         { NULL }
150 };