1 # Samba-specific bits for optparse
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """Support for parsing Samba-related command-line options."""
20 __docformat__ = "restructuredText"
24 from samba.credentials import (
33 class SambaOptions(optparse.OptionGroup):
34 """General Samba-related command line options."""
36 def __init__(self, parser):
37 from samba import fault_setup
39 from samba.param import LoadParm
40 optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
41 self.add_option("-s", "--configfile", action="callback",
42 type=str, metavar="FILE", help="Configuration file",
43 callback=self._load_configfile)
44 self.add_option("-d", "--debuglevel", action="callback",
45 type=str, metavar="DEBUGLEVEL", help="debug level",
46 callback=self._set_debuglevel)
47 self.add_option("--option", action="callback",
48 type=str, metavar="OPTION",
49 help="set smb.conf option from command line",
50 callback=self._set_option)
51 self.add_option("--realm", action="callback",
52 type=str, metavar="REALM", help="set the realm name",
53 callback=self._set_realm)
54 self._configfile = None
58 def get_loadparm_path(self):
59 """Return path to the smb.conf file specified on the command line."""
60 return self._configfile
62 def _load_configfile(self, option, opt_str, arg, parser):
63 self._configfile = arg
65 def _set_debuglevel(self, option, opt_str, arg, parser):
66 self._lp.set('debug level', arg)
67 parser.values.debuglevel = arg
69 def _set_realm(self, option, opt_str, arg, parser):
70 self._lp.set('realm', arg)
73 def _set_option(self, option, opt_str, arg, parser):
74 if arg.find('=') == -1:
75 raise optparse.OptionValueError(
76 "--option option takes a 'a=b' argument")
79 self._lp.set(a[0], a[1])
80 except Exception as e:
81 raise optparse.OptionValueError(
82 "invalid --option option value %r: %s" % (arg, e))
84 def get_loadparm(self):
85 """Return loadparm object with data specified on the command line."""
86 if self._configfile is not None:
87 self._lp.load(self._configfile)
88 elif os.getenv("SMB_CONF_PATH") is not None:
89 self._lp.load(os.getenv("SMB_CONF_PATH"))
91 self._lp.load_default()
95 class VersionOptions(optparse.OptionGroup):
96 """Command line option for printing Samba version."""
97 def __init__(self, parser):
98 optparse.OptionGroup.__init__(self, parser, "Version Options")
99 self.add_option("-V", "--version", action="callback",
100 callback=self._display_version,
101 help="Display version number")
103 def _display_version(self, option, opt_str, arg, parser):
109 def parse_kerberos_arg(arg, opt_str):
110 if arg.lower() in ["yes", 'true', '1']:
111 return MUST_USE_KERBEROS
112 elif arg.lower() in ["no", 'false', '0']:
113 return DONT_USE_KERBEROS
114 elif arg.lower() in ["auto"]:
115 return AUTO_USE_KERBEROS
117 raise optparse.OptionValueError("invalid %s option value: %s" %
121 class CredentialsOptions(optparse.OptionGroup):
122 """Command line options for specifying credentials."""
124 def __init__(self, parser, special_name=None):
125 self.special_name = special_name
126 if special_name is not None:
127 self.section = "Credentials Options (%s)" % special_name
129 self.section = "Credentials Options"
131 self.ask_for_password = True
132 self.ipaddress = None
133 self.machine_pass = False
134 optparse.OptionGroup.__init__(self, parser, self.section)
135 self._add_option("--simple-bind-dn", metavar="DN", action="callback",
136 callback=self._set_simple_bind_dn, type=str,
137 help="DN to use for a simple bind")
138 self._add_option("--password", metavar="PASSWORD", action="callback",
139 help="Password", type=str, callback=self._set_password)
140 self._add_option("-U", "--username", metavar="USERNAME",
141 action="callback", type=str,
142 help="Username", callback=self._parse_username)
143 self._add_option("-W", "--workgroup", metavar="WORKGROUP",
144 action="callback", type=str,
145 help="Workgroup", callback=self._parse_workgroup)
146 self._add_option("-N", "--no-pass", action="callback",
147 help="Don't ask for a password",
148 callback=self._set_no_password)
149 self._add_option("-k", "--kerberos", metavar="KERBEROS",
150 action="callback", type=str,
151 help="Use Kerberos", callback=self._set_kerberos)
152 self._add_option("", "--ipaddress", metavar="IPADDRESS",
153 action="callback", type=str,
154 help="IP address of server",
155 callback=self._set_ipaddress)
156 self._add_option("-P", "--machine-pass",
158 help="Use stored machine account password",
159 callback=self._set_machine_pass)
160 self._add_option("--krb5-ccache", metavar="KRB5CCNAME",
161 action="callback", type=str,
162 help="Kerberos Credentials cache",
163 callback=self._set_krb5_ccache)
164 self.creds = Credentials()
166 def _add_option(self, *args1, **kwargs):
167 if self.special_name is None:
168 return self.add_option(*args1, **kwargs)
172 if not a.startswith("--"):
174 args2 += (a.replace("--", "--%s-" % self.special_name),)
175 self.add_option(*args2, **kwargs)
177 def _parse_username(self, option, opt_str, arg, parser):
178 self.creds.parse_string(arg)
179 self.machine_pass = False
181 def _parse_workgroup(self, option, opt_str, arg, parser):
182 self.creds.set_domain(arg)
184 def _set_password(self, option, opt_str, arg, parser):
185 self.creds.set_password(arg)
186 self.ask_for_password = False
187 self.machine_pass = False
189 def _set_no_password(self, option, opt_str, arg, parser):
190 self.ask_for_password = False
192 def _set_machine_pass(self, option, opt_str, arg, parser):
193 self.machine_pass = True
195 def _set_ipaddress(self, option, opt_str, arg, parser):
198 def _set_kerberos(self, option, opt_str, arg, parser):
199 self.creds.set_kerberos_state(parse_kerberos_arg(arg, opt_str))
201 def _set_simple_bind_dn(self, option, opt_str, arg, parser):
202 self.creds.set_bind_dn(arg)
204 def _set_krb5_ccache(self, option, opt_str, arg, parser):
205 self.creds.set_named_ccache(arg)
207 def get_credentials(self, lp, fallback_machine=False):
208 """Obtain the credentials set on the command-line.
210 :param lp: Loadparm object to use.
211 :return: Credentials object
214 if self.machine_pass:
215 self.creds.set_machine_account(lp)
216 elif self.ask_for_password:
217 self.creds.set_cmdline_callbacks()
219 # possibly fallback to using the machine account, if we have
220 # access to the secrets db
221 if fallback_machine and not self.creds.authentication_requested():
223 self.creds.set_machine_account(lp)
230 class CredentialsOptionsDouble(CredentialsOptions):
231 """Command line options for specifying credentials of two servers."""
233 def __init__(self, parser):
234 CredentialsOptions.__init__(self, parser)
236 self.add_option("--simple-bind-dn2", metavar="DN2", action="callback",
237 callback=self._set_simple_bind_dn2, type=str,
238 help="DN to use for a simple bind")
239 self.add_option("--password2", metavar="PASSWORD2", action="callback",
240 help="Password", type=str,
241 callback=self._set_password2)
242 self.add_option("--username2", metavar="USERNAME2",
243 action="callback", type=str,
244 help="Username for second server",
245 callback=self._parse_username2)
246 self.add_option("--workgroup2", metavar="WORKGROUP2",
247 action="callback", type=str,
248 help="Workgroup for second server",
249 callback=self._parse_workgroup2)
250 self.add_option("--no-pass2", action="store_true",
251 help="Don't ask for a password for the second server")
252 self.add_option("--kerberos2", metavar="KERBEROS2",
253 action="callback", type=str,
254 help="Use Kerberos", callback=self._set_kerberos2)
255 self.creds2 = Credentials()
257 def _parse_username2(self, option, opt_str, arg, parser):
258 self.creds2.parse_string(arg)
260 def _parse_workgroup2(self, option, opt_str, arg, parser):
261 self.creds2.set_domain(arg)
263 def _set_password2(self, option, opt_str, arg, parser):
264 self.creds2.set_password(arg)
265 self.no_pass2 = False
267 def _set_kerberos2(self, option, opt_str, arg, parser):
268 self.creds2.set_kerberos_state(parse_kerberos_arg(arg, opt_str))
270 def _set_simple_bind_dn2(self, option, opt_str, arg, parser):
271 self.creds2.set_bind_dn(arg)
273 def get_credentials2(self, lp, guess=True):
274 """Obtain the credentials set on the command-line.
276 :param lp: Loadparm object to use.
277 :param guess: Try guess Credentials from environment
278 :return: Credentials object
281 self.creds2.guess(lp)
282 elif not self.creds2.get_username():
283 self.creds2.set_anonymous()
286 self.creds2.set_cmdline_callbacks()