Merge branch 'master' of git://git.samba.org/samba
[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 """Support for parsing Samba-related command-line options."""
21
22 import optparse
23 from credentials import Credentials, DONT_USE_KERBEROS, MUST_USE_KERBEROS
24 from hostconfig import Hostconfig
25
26 __docformat__ = "restructuredText"
27
28 class SambaOptions(optparse.OptionGroup):
29     """General Samba-related command line options."""
30     def __init__(self, parser):
31         optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
32         self.add_option("-s", "--configfile", action="callback",
33                         type=str, metavar="FILE", help="Configuration file",
34                         callback=self._load_configfile)
35         self._configfile = None
36
37     def get_loadparm_path(self):
38         """Return the path to the smb.conf file specified on the command line.  """
39         return self._configfile
40
41     def _load_configfile(self, option, opt_str, arg, parser):
42         self._configfile = arg
43
44     def get_loadparm(self):
45         """Return a loadparm object with data specified on the command line.  """
46         import os, param
47         lp = param.LoadParm()
48         if self._configfile is not None:
49             lp.load(self._configfile)
50         elif os.getenv("SMB_CONF_PATH") is not None:
51             lp.load(os.getenv("SMB_CONF_PATH"))
52         else:
53             lp.load_default()
54         return lp
55
56     def get_hostconfig(self):
57         return Hostconfig(self.get_loadparm())
58
59
60 class VersionOptions(optparse.OptionGroup):
61     """Command line option for printing Samba version."""
62     def __init__(self, parser):
63         optparse.OptionGroup.__init__(self, parser, "Version Options")
64
65
66 class CredentialsOptions(optparse.OptionGroup):
67     """Command line options for specifying credentials."""
68     def __init__(self, parser):
69         self.no_pass = False
70         optparse.OptionGroup.__init__(self, parser, "Credentials Options")
71         self.add_option("--simple-bind-dn", metavar="DN", action="callback",
72                         callback=self._set_simple_bind_dn, type=str,
73                         help="DN to use for a simple bind")
74         self.add_option("--password", metavar="PASSWORD", action="callback",
75                         help="Password", type=str, callback=self._set_password)
76         self.add_option("-U", "--username", metavar="USERNAME",
77                         action="callback", type=str,
78                         help="Username", callback=self._parse_username)
79         self.add_option("-W", "--workgroup", metavar="WORKGROUP",
80                         action="callback", type=str,
81                         help="Workgroup", callback=self._parse_workgroup)
82         self.add_option("-N", "--no-pass", action="store_true",
83                         help="Don't ask for a password")
84         self.add_option("-k", "--kerberos", metavar="KERBEROS",
85                         action="callback", type=str,
86                         help="Use Kerberos", callback=self._set_kerberos)
87         self.creds = Credentials()
88
89     def _parse_username(self, option, opt_str, arg, parser):
90         self.creds.parse_string(arg)
91
92     def _parse_workgroup(self, option, opt_str, arg, parser):
93         self.creds.set_domain(arg)
94
95     def _set_password(self, option, opt_str, arg, parser):
96         self.creds.set_password(arg)
97
98     def _set_kerberos(self, option, opt_str, arg, parser):
99         if bool(arg) or arg.lower() == "yes":
100             self.creds.set_kerberos_state(MUST_USE_KERBEROS)
101         else:
102             self.creds.set_kerberos_state(DONT_USE_KERBEROS)
103
104     def _set_simple_bind_dn(self, option, opt_str, arg, parser):
105         self.creds.set_bind_dn(arg)
106
107     def get_credentials(self, lp):
108         """Obtain the credentials set on the command-line.
109
110         :param lp: Loadparm object to use.
111         :return: Credentials object
112         """
113         self.creds.guess(lp)
114         if not self.no_pass:
115             self.creds.set_cmdline_callbacks()
116         return self.creds