3 # Samba-specific bits for optparse
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
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.
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.
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/>.
20 """Support for parsing Samba-related command-line options."""
23 from credentials import Credentials, DONT_USE_KERBEROS, MUST_USE_KERBEROS
24 from hostconfig import Hostconfig
27 __docformat__ = "restructuredText"
29 class SambaOptions(optparse.OptionGroup):
30 """General Samba-related command line options."""
31 def __init__(self, parser):
32 optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
33 self.add_option("-s", "--configfile", action="callback",
34 type=str, metavar="FILE", help="Configuration file",
35 callback=self._load_configfile)
36 self.add_option("-d", "--debuglevel", action="callback",
37 type=int, metavar="DEBUGLEVEL", help="debug level",
38 callback=self._set_debuglevel)
39 self._configfile = None
40 self._debuglevel = None
42 def get_loadparm_path(self):
43 """Return the path to the smb.conf file specified on the command line. """
44 return self._configfile
46 def _load_configfile(self, option, opt_str, arg, parser):
47 self._configfile = arg
49 def _set_debuglevel(self, option, opt_str, arg, parser):
50 self._debuglevel = arg
52 def get_loadparm(self):
53 """Return a loadparm object with data specified on the command line. """
56 if self._configfile is not None:
57 lp.load(self._configfile)
58 elif os.getenv("SMB_CONF_PATH") is not None:
59 lp.load(os.getenv("SMB_CONF_PATH"))
63 samba.set_debug_level(self._debuglevel)
66 def get_hostconfig(self):
67 return Hostconfig(self.get_loadparm())
70 class VersionOptions(optparse.OptionGroup):
71 """Command line option for printing Samba version."""
72 def __init__(self, parser):
73 optparse.OptionGroup.__init__(self, parser, "Version Options")
74 self.add_option("--version", action="callback",
75 callback=self._display_version,
76 help="Display version number")
78 def _display_version(self, option, opt_str, arg, parser):
84 class CredentialsOptions(optparse.OptionGroup):
85 """Command line options for specifying credentials."""
86 def __init__(self, parser):
88 optparse.OptionGroup.__init__(self, parser, "Credentials Options")
89 self.add_option("--simple-bind-dn", metavar="DN", action="callback",
90 callback=self._set_simple_bind_dn, type=str,
91 help="DN to use for a simple bind")
92 self.add_option("--password", metavar="PASSWORD", action="callback",
93 help="Password", type=str, callback=self._set_password)
94 self.add_option("-U", "--username", metavar="USERNAME",
95 action="callback", type=str,
96 help="Username", callback=self._parse_username)
97 self.add_option("-W", "--workgroup", metavar="WORKGROUP",
98 action="callback", type=str,
99 help="Workgroup", callback=self._parse_workgroup)
100 self.add_option("-N", "--no-pass", action="store_true",
101 help="Don't ask for a password")
102 self.add_option("-k", "--kerberos", metavar="KERBEROS",
103 action="callback", type=str,
104 help="Use Kerberos", callback=self._set_kerberos)
105 self.creds = Credentials()
107 def _parse_username(self, option, opt_str, arg, parser):
108 self.creds.parse_string(arg)
110 def _parse_workgroup(self, option, opt_str, arg, parser):
111 self.creds.set_domain(arg)
113 def _set_password(self, option, opt_str, arg, parser):
114 self.creds.set_password(arg)
117 def _set_kerberos(self, option, opt_str, arg, parser):
118 if bool(arg) or arg.lower() == "yes":
119 self.creds.set_kerberos_state(MUST_USE_KERBEROS)
121 self.creds.set_kerberos_state(DONT_USE_KERBEROS)
123 def _set_simple_bind_dn(self, option, opt_str, arg, parser):
124 self.creds.set_bind_dn(arg)
126 def get_credentials(self, lp):
127 """Obtain the credentials set on the command-line.
129 :param lp: Loadparm object to use.
130 :return: Credentials object
134 self.creds.set_cmdline_callbacks()
137 class CredentialsOptionsDouble(CredentialsOptions):
138 """Command line options for specifying credentials of two servers."""
139 def __init__(self, parser):
140 CredentialsOptions.__init__(self, parser)
142 self.add_option("--simple-bind-dn2", metavar="DN2", action="callback",
143 callback=self._set_simple_bind_dn2, type=str,
144 help="DN to use for a simple bind")
145 self.add_option("--password2", metavar="PASSWORD2", action="callback",
146 help="Password", type=str, callback=self._set_password2)
147 self.add_option("--username2", metavar="USERNAME2",
148 action="callback", type=str,
149 help="Username for second server", callback=self._parse_username2)
150 self.add_option("--workgroup2", metavar="WORKGROUP2",
151 action="callback", type=str,
152 help="Workgroup for second server", callback=self._parse_workgroup2)
153 self.add_option("--no-pass2", action="store_true",
154 help="Don't ask for a password for the second server")
155 self.add_option("--kerberos2", metavar="KERBEROS2",
156 action="callback", type=str,
157 help="Use Kerberos", callback=self._set_kerberos2)
158 self.creds2 = Credentials()
160 def _parse_username2(self, option, opt_str, arg, parser):
161 self.creds2.parse_string(arg)
163 def _parse_workgroup2(self, option, opt_str, arg, parser):
164 self.creds2.set_domain(arg)
166 def _set_password2(self, option, opt_str, arg, parser):
167 self.creds2.set_password(arg)
168 self.no_pass2 = False
170 def _set_kerberos2(self, option, opt_str, arg, parser):
171 if bool(arg) or arg.lower() == "yes":
172 self.creds2.set_kerberos_state(MUST_USE_KERBEROS)
174 self.creds2.set_kerberos_state(DONT_USE_KERBEROS)
176 def _set_simple_bind_dn2(self, option, opt_str, arg, parser):
177 self.creds2.set_bind_dn(arg)
179 def get_credentials2(self, lp):
180 """Obtain the credentials set on the command-line.
182 :param lp: Loadparm object to use.
183 :return: Credentials object
185 self.creds2.guess(lp)
187 self.creds2.set_cmdline_callbacks()