r19598: Ahead of a merge to current lorikeet-heimdal:
[Samba.git] / source / lib / cmdline / popt_credentials.c
blobab1d11bfbf0c73b9294bb6adc76da366c2ba868d
1 /*
2 Unix SMB/CIFS implementation.
3 Credentials popt routines
5 Copyright (C) Jelmer Vernooij 2002,2003,2005
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.
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.
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.
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/credentials/credentials_krb5.h"
27 #include "auth/gensec/gensec.h"
29 /* Handle command line options:
30 * -U,--user
31 * -A,--authentication-file
32 * -k,--use-kerberos
33 * -N,--no-pass
34 * -S,--signing
35 * -P --machine-pass
36 * --simple-bind-dn
37 * --password
38 * --use-security-mechanisms
42 static BOOL dont_ask;
44 enum opt { OPT_SIMPLE_BIND_DN, OPT_PASSWORD, OPT_KERBEROS, OPT_GENSEC_MECHS };
47 disable asking for a password
49 void popt_common_dont_ask(void)
51 dont_ask = True;
54 static void popt_common_credentials_callback(poptContext con,
55 enum poptCallbackReason reason,
56 const struct poptOption *opt,
57 const char *arg, const void *data)
59 if (reason == POPT_CALLBACK_REASON_PRE) {
60 cmdline_credentials = cli_credentials_init(talloc_autofree_context());
61 return;
64 if (reason == POPT_CALLBACK_REASON_POST) {
65 cli_credentials_guess(cmdline_credentials);
67 if (!dont_ask) {
68 cli_credentials_set_cmdline_callbacks(cmdline_credentials);
70 return;
73 switch(opt->val) {
74 case 'U':
76 char *lp;
78 cli_credentials_parse_string(cmdline_credentials, arg, CRED_SPECIFIED);
79 /* This breaks the abstraction, including the const above */
80 if ((lp=strchr_m(arg,'%'))) {
81 lp[0]='\0';
82 lp++;
83 /* Try to prevent this showing up in ps */
84 memset(lp,0,strlen(lp));
87 break;
89 case OPT_PASSWORD:
90 cli_credentials_set_password(cmdline_credentials, arg, CRED_SPECIFIED);
91 /* Try to prevent this showing up in ps */
92 memset(discard_const(arg),0,strlen(arg));
93 break;
95 case 'A':
96 cli_credentials_parse_file(cmdline_credentials, arg, CRED_SPECIFIED);
97 break;
99 case 'S':
100 lp_set_cmdline("client signing", arg);
101 break;
103 case 'P':
104 /* Later, after this is all over, get the machine account details from the secrets.ldb */
105 cli_credentials_set_machine_account_pending(cmdline_credentials);
106 break;
108 case OPT_KERBEROS:
110 BOOL use_kerberos = True;
111 /* Force us to only use kerberos */
112 if (arg) {
113 if (!set_boolean(arg, &use_kerberos)) {
114 fprintf(stderr, "Error parsing -k %s\n", arg);
115 exit(1);
116 break;
120 cli_credentials_set_kerberos_state(cmdline_credentials,
121 use_kerberos
122 ? CRED_MUST_USE_KERBEROS
123 : CRED_DONT_USE_KERBEROS);
124 break;
126 case OPT_GENSEC_MECHS:
127 /* Convert a list of strings into a list of available authentication standards */
129 break;
131 case OPT_SIMPLE_BIND_DN:
132 cli_credentials_set_bind_dn(cmdline_credentials, arg);
133 break;
139 struct poptOption popt_common_credentials[] = {
140 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, popt_common_credentials_callback },
141 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN\\]USERNAME[%PASSWORD]" },
142 { "no-pass", 'N', POPT_ARG_NONE, &dont_ask, True, "Don't ask for a password" },
143 { "password", 0, POPT_ARG_STRING, NULL, OPT_PASSWORD, "Password" },
144 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
145 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
146 { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password (implies -k)" },
147 { "simple-bind-dn", 0, POPT_ARG_STRING, NULL, OPT_SIMPLE_BIND_DN, "DN to use for a simple bind" },
148 { "kerberos", 'k', POPT_ARG_STRING, NULL, OPT_KERBEROS, "Use Kerberos" },
149 { "use-security-mechanisms", 0, POPT_ARG_STRING, NULL, OPT_GENSEC_MECHS, "Restricted list of authentication mechanisms available for use with this authentication"},
150 { NULL }