r12592: Remove some useless dependencies
[bbaumbach/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 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 "version.h"
24 #include "system/filesys.h"
25 #include "system/passwd.h"
26 #include "lib/cmdline/popt_common.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  */
36
37
38 static BOOL dont_ask;
39
40 enum opt { OPT_SIMPLE_BIND_DN };
41
42 /*
43   disable asking for a password
44 */
45 void popt_common_dont_ask(void)
46 {
47         dont_ask = True;
48 }
49
50 static void popt_common_credentials_callback(poptContext con, 
51                                                 enum poptCallbackReason reason,
52                                                 const struct poptOption *opt,
53                                                 const char *arg, const void *data)
54 {
55         if (reason == POPT_CALLBACK_REASON_PRE) {
56                 cmdline_credentials = cli_credentials_init(talloc_autofree_context());
57                 return;
58         }
59         
60         if (reason == POPT_CALLBACK_REASON_POST) {
61                 cli_credentials_guess(cmdline_credentials);
62
63                 if (!dont_ask) {
64                         cli_credentials_set_cmdline_callbacks(cmdline_credentials);
65                 }
66                 return;
67         }
68
69         switch(opt->val) {
70         case 'U':
71                 {
72                         char *lp;
73
74                         cli_credentials_parse_string(cmdline_credentials, arg, CRED_SPECIFIED);
75                         /* This breaks the abstraction, including the const above */
76                         if ((lp=strchr_m(arg,'%'))) {
77                                 lp[0]='\0';
78                                 lp++;
79                                 memset(lp,0,strlen(lp));
80                         }
81                 }
82                 break;
83
84         case 'A':
85                 cli_credentials_parse_file(cmdline_credentials, arg, CRED_SPECIFIED);
86                 break;
87
88         case 'S':
89                 lp_set_cmdline("client signing", arg);
90                 break;
91
92         case 'P':
93                 /* Later, after this is all over, get the machine account details from the secrets.ldb */
94                 cli_credentials_set_machine_account_pending(cmdline_credentials);
95                 
96                 /* machine accounts only work with kerberos (fall though)*/
97                 break;
98         case OPT_SIMPLE_BIND_DN:
99                 cli_credentials_set_bind_dn(cmdline_credentials, arg);
100                 break;
101         }
102 }
103
104
105
106 struct poptOption popt_common_credentials[] = {
107         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, popt_common_credentials_callback },
108         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN\\]USERNAME[%PASSWORD]" },
109         { "no-pass", 'N', POPT_ARG_NONE, &dont_ask, True, "Don't ask for a password" },
110         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
111         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
112         { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password (implies -k)" },
113         { "simple-bind-dn", 0, POPT_ARG_STRING, NULL, OPT_SIMPLE_BIND_DN, "DN to use for a simple bind" },
114         POPT_TABLEEND
115 };