Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-local
[samba.git] / source4 / scripting / python / samba / getopt.py
1 #!/usr/bin/python
2
3 # Samba-specific bits for optparse
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
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 3 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, see <http://www.gnu.org/licenses/>.
18 #
19
20 import optparse
21 from credentials import Credentials, AUTO_USE_KERBEROS, DONT_USE_KERBEROS, MUST_USE_KERBEROS
22
23 class SambaOptions(optparse.OptionGroup):
24     def __init__(self, parser):
25         optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
26         self.add_option("-s", "--configfile", action="callback",
27                         type=str, metavar="FILE", help="Configuration file", 
28                         callback=self._load_configfile)
29         self._configfile = None
30
31     def get_loadparm_path(self):
32         return self._configfile
33
34     def _load_configfile(self, option, opt_str, arg, parser):
35         self._configfile = arg
36
37     def get_loadparm(self):
38         import param
39         lp = param.LoadParm()
40         if self._configfile is None:
41             lp.load_default()
42         else:
43             lp.load(self._configfile)
44         return lp
45
46 class VersionOptions(optparse.OptionGroup):
47     def __init__(self, parser):
48         optparse.OptionGroup.__init__(self, parser, "Version Options")
49
50
51 class CredentialsOptions(optparse.OptionGroup):
52     def __init__(self, parser):
53         self.no_pass = False
54         optparse.OptionGroup.__init__(self, parser, "Credentials Options")
55         self.add_option("--simple-bind-dn", metavar="DN", action="callback",
56                         callback=self._set_simple_bind_dn, type=str,
57                         help="DN to use for a simple bind")
58         self.add_option("--password", metavar="PASSWORD", action="callback",
59                         help="Password", type=str, callback=self._set_password)
60         self.add_option("-U", "--username", metavar="USERNAME", 
61                         action="callback", type=str,
62                         help="Username", callback=self._parse_username)
63         self.add_option("-W", "--workgroup", metavar="WORKGROUP", 
64                         action="callback", type=str,
65                         help="Workgroup", callback=self._parse_workgroup)
66         self.add_option("-N", "--no-pass", action="store_true",
67                         help="Don't ask for a password")
68         self.add_option("-k", "--kerberos", metavar="KERBEROS", 
69                         action="callback", type=str,
70                         help="Use Kerberos", callback=self._set_kerberos)
71         self.creds = Credentials()
72
73     def _parse_username(self, option, opt_str, arg, parser):
74         self.creds.parse_string(arg)
75
76     def _parse_workgroup(self, option, opt_str, arg, parser):
77         self.creds.set_domain(arg)
78
79     def _set_password(self, option, opt_str, arg, parser):
80         self.creds.set_password(arg)
81
82     def _set_kerberos(self, option, opt_str, arg, parser):
83         if bool(arg) or arg.lower() == "yes":
84             self.creds.set_kerberos_state(MUST_USE_KERBEROS)
85         else:
86             self.creds.set_kerberos_state(DONT_USE_KERBEROS)
87
88     def _set_simple_bind_dn(self, option, opt_str, arg, parser):
89         self.creds.set_bind_dn(arg)
90
91     def get_credentials(self, lp):
92         self.creds.guess(lp)
93         if not self.no_pass:
94             self.creds.set_cmdline_callbacks()
95         return self.creds