59a724c6f9ecce78855d9619e5f0552f443b0ebd
[gd/samba-autobuild/.git] / source4 / 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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/cmdline/popt_common.h"
23 #include "lib/cmdline/credentials.h"
24 #include "auth/credentials/credentials.h"
25 #include "auth/gensec/gensec.h"
26 #include "param/param.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  *              --krb5-ccache
38  */
39
40 static bool dont_ask;
41 static bool machine_account_pending;
42
43 enum opt { OPT_SIMPLE_BIND_DN, OPT_PASSWORD, OPT_KERBEROS, OPT_SIGN, OPT_ENCRYPT, OPT_KRB5_CCACHE };
44
45 static void popt_common_credentials_callback(poptContext con, 
46                                                 enum poptCallbackReason reason,
47                                                 const struct poptOption *opt,
48                                                 const char *arg, const void *data)
49 {
50         if (reason == POPT_CALLBACK_REASON_PRE) {
51                 cmdline_credentials = cli_credentials_init(talloc_autofree_context());
52                 return;
53         }
54         
55         if (reason == POPT_CALLBACK_REASON_POST) {
56                 cli_credentials_guess(cmdline_credentials, cmdline_lp_ctx);
57
58                 if (!dont_ask) {
59                         cli_credentials_set_cmdline_callbacks(cmdline_credentials);
60                 }
61
62                 if (machine_account_pending) {
63                         cli_credentials_set_machine_account(cmdline_credentials, cmdline_lp_ctx);
64                 }
65
66                 return;
67
68         }
69
70         switch(opt->val) {
71         case 'U':
72         {
73                 char *lp;
74                 
75                 cli_credentials_parse_string(cmdline_credentials, arg, CRED_SPECIFIED);
76                 /* This breaks the abstraction, including the const above */
77                 if ((lp=strchr_m(arg,'%'))) {
78                         lp[0]='\0';
79                         lp++;
80                         /* Try to prevent this showing up in ps */
81                         memset(lp,0,strlen(lp));
82                 }
83         }
84         break;
85
86         case OPT_PASSWORD:
87                 cli_credentials_set_password(cmdline_credentials, arg, CRED_SPECIFIED);
88                 /* Try to prevent this showing up in ps */
89                 memset(discard_const(arg),0,strlen(arg));
90                 break;
91
92         case 'A':
93                 cli_credentials_parse_file(cmdline_credentials, arg, CRED_SPECIFIED);
94                 break;
95
96         case 'P':
97                 /* Later, after this is all over, get the machine account details from the secrets.ldb */
98                 machine_account_pending = true;
99                 break;
100
101         case OPT_KERBEROS:
102         {
103                 bool use_kerberos = true;
104                 /* Force us to only use kerberos */
105                 if (arg) {
106                         if (!set_boolean(arg, &use_kerberos)) {
107                                 fprintf(stderr, "Error parsing -k %s. Should be "
108                                         "-k [yes|no]\n", arg);
109                                 exit(1);
110                                 break;
111                         }
112                 }
113                 
114                 cli_credentials_set_kerberos_state(cmdline_credentials, 
115                                                    use_kerberos 
116                                                    ? CRED_MUST_USE_KERBEROS
117                                                    : CRED_DONT_USE_KERBEROS);
118                 break;
119         }
120                 
121         case OPT_SIMPLE_BIND_DN:
122         {
123                 cli_credentials_set_bind_dn(cmdline_credentials, arg);
124                 break;
125         }
126         case OPT_KRB5_CCACHE:
127         {
128                 const char *error_string;
129                 if (cli_credentials_set_ccache(cmdline_credentials, cmdline_lp_ctx, arg, CRED_SPECIFIED,
130                                                &error_string) != 0) {
131                         fprintf(stderr, "Error reading krb5 credentials cache: '%s' %s", arg, error_string);
132                         exit(1);
133                 }
134                 break;
135         }
136         case OPT_SIGN:
137         {
138                 uint32_t gensec_features;
139
140                 gensec_features = cli_credentials_get_gensec_features(cmdline_credentials);
141
142                 gensec_features |= GENSEC_FEATURE_SIGN;
143                 cli_credentials_set_gensec_features(cmdline_credentials,
144                                                     gensec_features);
145                 break;
146         }
147         case OPT_ENCRYPT:
148         {
149                 uint32_t gensec_features;
150
151                 gensec_features = cli_credentials_get_gensec_features(cmdline_credentials);
152
153                 gensec_features |= GENSEC_FEATURE_SEAL;
154                 cli_credentials_set_gensec_features(cmdline_credentials,
155                                                     gensec_features);
156                 break;
157         }
158         }
159 }
160
161
162
163 struct poptOption popt_common_credentials4[] = {
164         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_credentials_callback },
165         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
166         { "no-pass", 'N', POPT_ARG_NONE, &dont_ask, 'N', "Don't ask for a password" },
167         { "password", 0, POPT_ARG_STRING, NULL, OPT_PASSWORD, "Password" },
168         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
169         { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
170         { "simple-bind-dn", 0, POPT_ARG_STRING, NULL, OPT_SIMPLE_BIND_DN, "DN to use for a simple bind" },
171         { "kerberos", 'k', POPT_ARG_STRING, NULL, OPT_KERBEROS, "Use Kerberos, -k [yes|no]" },
172         { "krb5-ccache", 0, POPT_ARG_STRING, NULL, OPT_KRB5_CCACHE, "Credentials cache location for Kerberos" },
173         { "sign", 'S', POPT_ARG_NONE, NULL, OPT_SIGN, "Sign connection to prevent modification in transit" },
174         { "encrypt", 'e', POPT_ARG_NONE, NULL, OPT_ENCRYPT, "Encrypt connection for privacy" },
175         { NULL }
176 };