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."""
22 __docformat__ = "restructuredText"
26 from samba.credentials import (
32 from samba.hostconfig import Hostconfig
36 class SambaOptions(optparse.OptionGroup):
37 """General Samba-related command line options."""
39 def __init__(self, parser):
40 from samba.param import LoadParm
41 optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
42 self.add_option("-s", "--configfile", action="callback",
43 type=str, metavar="FILE", help="Configuration file",
44 callback=self._load_configfile)
45 self.add_option("-d", "--debuglevel", action="callback",
46 type=int, metavar="DEBUGLEVEL", help="debug level",
47 callback=self._set_debuglevel)
48 self.add_option("--option", action="callback",
49 type=str, metavar="OPTION",
50 help="set smb.conf option from command line",
51 callback=self._set_option)
52 self.add_option("--realm", action="callback",
53 type=str, metavar="REALM", help="set the realm name",
54 callback=self._set_realm)
55 self._configfile = None
59 def get_loadparm_path(self):
60 """Return path to the smb.conf file specified on the command line."""
61 return self._configfile
63 def _load_configfile(self, option, opt_str, arg, parser):
64 self._configfile = arg
66 def _set_debuglevel(self, option, opt_str, arg, parser):
67 self._lp.set('debug level', str(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 print("--option takes a 'a=b' argument")
78 self._lp.set(a[0], a[1])
80 def get_loadparm(self):
81 """Return loadparm object with data specified on the command line."""
82 if self._configfile is not None:
83 self._lp.load(self._configfile)
84 elif os.getenv("SMB_CONF_PATH") is not None:
85 self._lp.load(os.getenv("SMB_CONF_PATH"))
87 self._lp.load_default()
90 def get_hostconfig(self):
91 return Hostconfig(self.get_loadparm())
94 class VersionOptions(optparse.OptionGroup):
95 """Command line option for printing Samba version."""
96 def __init__(self, parser):
97 optparse.OptionGroup.__init__(self, parser, "Version Options")
98 self.add_option("--version", action="callback",
99 callback=self._display_version,
100 help="Display version number")
102 def _display_version(self, option, opt_str, arg, parser):
108 def parse_kerberos_arg(arg):
109 if arg.lower() in ["yes", 'true', '1']:
110 return MUST_USE_KERBEROS
111 elif arg.lower() in ["no", 'false', '0']:
112 return DONT_USE_KERBEROS
113 elif arg.lower() in ["auto"]:
114 return AUTO_USE_KERBEROS
116 raise optparse.BadOptionError("invalid kerberos option: %s" % arg)
119 class CredentialsOptions(optparse.OptionGroup):
120 """Command line options for specifying credentials."""
122 def __init__(self, parser):
124 self.ipaddress = None
125 optparse.OptionGroup.__init__(self, parser, "Credentials Options")
126 self.add_option("--simple-bind-dn", metavar="DN", action="callback",
127 callback=self._set_simple_bind_dn, type=str,
128 help="DN to use for a simple bind")
129 self.add_option("--password", metavar="PASSWORD", action="callback",
130 help="Password", type=str, callback=self._set_password)
131 self.add_option("-U", "--username", metavar="USERNAME",
132 action="callback", type=str,
133 help="Username", callback=self._parse_username)
134 self.add_option("-W", "--workgroup", metavar="WORKGROUP",
135 action="callback", type=str,
136 help="Workgroup", callback=self._parse_workgroup)
137 self.add_option("-N", "--no-pass", action="store_true",
138 help="Don't ask for a password")
139 self.add_option("-k", "--kerberos", metavar="KERBEROS",
140 action="callback", type=str,
141 help="Use Kerberos", callback=self._set_kerberos)
142 self.add_option("", "--ipaddress", metavar="IPADDRESS",
143 action="callback", type=str,
144 help="IP address of server",
145 callback=self._set_ipaddress)
146 self.creds = Credentials()
148 def _parse_username(self, option, opt_str, arg, parser):
149 self.creds.parse_string(arg)
151 def _parse_workgroup(self, option, opt_str, arg, parser):
152 self.creds.set_domain(arg)
154 def _set_password(self, option, opt_str, arg, parser):
155 self.creds.set_password(arg)
158 def _set_ipaddress(self, option, opt_str, arg, parser):
161 def _set_kerberos(self, option, opt_str, arg, parser):
162 self.creds.set_kerberos_state(parse_kerberos_arg(arg))
164 def _set_simple_bind_dn(self, option, opt_str, arg, parser):
165 self.creds.set_bind_dn(arg)
167 def get_credentials(self, lp, fallback_machine=False):
168 """Obtain the credentials set on the command-line.
170 :param lp: Loadparm object to use.
171 :return: Credentials object
175 self.creds.set_cmdline_callbacks()
177 # possibly fallback to using the machine account, if we have
178 # access to the secrets db
179 if fallback_machine and not self.creds.authentication_requested():
181 self.creds.set_machine_account(lp)
188 class CredentialsOptionsDouble(CredentialsOptions):
189 """Command line options for specifying credentials of two servers."""
191 def __init__(self, parser):
192 CredentialsOptions.__init__(self, parser)
194 self.add_option("--simple-bind-dn2", metavar="DN2", action="callback",
195 callback=self._set_simple_bind_dn2, type=str,
196 help="DN to use for a simple bind")
197 self.add_option("--password2", metavar="PASSWORD2", action="callback",
198 help="Password", type=str,
199 callback=self._set_password2)
200 self.add_option("--username2", metavar="USERNAME2",
201 action="callback", type=str,
202 help="Username for second server",
203 callback=self._parse_username2)
204 self.add_option("--workgroup2", metavar="WORKGROUP2",
205 action="callback", type=str,
206 help="Workgroup for second server",
207 callback=self._parse_workgroup2)
208 self.add_option("--no-pass2", action="store_true",
209 help="Don't ask for a password for the second server")
210 self.add_option("--kerberos2", metavar="KERBEROS2",
211 action="callback", type=str,
212 help="Use Kerberos", callback=self._set_kerberos2)
213 self.creds2 = Credentials()
215 def _parse_username2(self, option, opt_str, arg, parser):
216 self.creds2.parse_string(arg)
218 def _parse_workgroup2(self, option, opt_str, arg, parser):
219 self.creds2.set_domain(arg)
221 def _set_password2(self, option, opt_str, arg, parser):
222 self.creds2.set_password(arg)
223 self.no_pass2 = False
225 def _set_kerberos2(self, option, opt_str, arg, parser):
226 self.creds2.set_kerberos_state(parse_kerberos_arg(arg))
228 def _set_simple_bind_dn2(self, option, opt_str, arg, parser):
229 self.creds2.set_bind_dn(arg)
231 def get_credentials2(self, lp, guess=True):
232 """Obtain the credentials set on the command-line.
234 :param lp: Loadparm object to use.
235 :param guess: Try guess Credentials from environment
236 :return: Credentials object
239 self.creds2.guess(lp)
240 elif not self.creds2.get_username():
241 self.creds2.set_anonymous()
244 self.creds2.set_cmdline_callbacks()