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 (
31 from samba.hostconfig import Hostconfig
35 class SambaOptions(optparse.OptionGroup):
36 """General Samba-related command line options."""
38 def __init__(self, parser):
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=int, 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
57 def get_loadparm_path(self):
58 """Return path to the smb.conf file specified on the command line."""
59 return self._configfile
61 def _load_configfile(self, option, opt_str, arg, parser):
62 self._configfile = arg
64 def _set_debuglevel(self, option, opt_str, arg, parser):
65 self._lp.set('debug level', str(arg))
67 def _set_realm(self, option, opt_str, arg, parser):
68 self._lp.set('realm', arg)
70 def _set_option(self, option, opt_str, arg, parser):
71 if arg.find('=') == -1:
72 print("--option takes a 'a=b' argument")
75 self._lp.set(a[0], a[1])
77 def get_loadparm(self):
78 """Return loadparm object with data specified on the command line."""
79 if self._configfile is not None:
80 self._lp.load(self._configfile)
81 elif os.getenv("SMB_CONF_PATH") is not None:
82 self._lp.load(os.getenv("SMB_CONF_PATH"))
84 self._lp.load_default()
87 def get_hostconfig(self):
88 return Hostconfig(self.get_loadparm())
91 class VersionOptions(optparse.OptionGroup):
92 """Command line option for printing Samba version."""
93 def __init__(self, parser):
94 optparse.OptionGroup.__init__(self, parser, "Version Options")
95 self.add_option("--version", action="callback",
96 callback=self._display_version,
97 help="Display version number")
99 def _display_version(self, option, opt_str, arg, parser):
105 class CredentialsOptions(optparse.OptionGroup):
106 """Command line options for specifying credentials."""
107 def __init__(self, parser):
109 self.ipaddress = None
110 optparse.OptionGroup.__init__(self, parser, "Credentials Options")
111 self.add_option("--simple-bind-dn", metavar="DN", action="callback",
112 callback=self._set_simple_bind_dn, type=str,
113 help="DN to use for a simple bind")
114 self.add_option("--password", metavar="PASSWORD", action="callback",
115 help="Password", type=str, callback=self._set_password)
116 self.add_option("-U", "--username", metavar="USERNAME",
117 action="callback", type=str,
118 help="Username", callback=self._parse_username)
119 self.add_option("-W", "--workgroup", metavar="WORKGROUP",
120 action="callback", type=str,
121 help="Workgroup", callback=self._parse_workgroup)
122 self.add_option("-N", "--no-pass", action="store_true",
123 help="Don't ask for a password")
124 self.add_option("-k", "--kerberos", metavar="KERBEROS",
125 action="callback", type=str,
126 help="Use Kerberos", callback=self._set_kerberos)
127 self.add_option("", "--ipaddress", metavar="IPADDRESS",
128 action="callback", type=str,
129 help="IP address of server",
130 callback=self._set_ipaddress)
131 self.creds = Credentials()
133 def _parse_username(self, option, opt_str, arg, parser):
134 self.creds.parse_string(arg)
136 def _parse_workgroup(self, option, opt_str, arg, parser):
137 self.creds.set_domain(arg)
139 def _set_password(self, option, opt_str, arg, parser):
140 self.creds.set_password(arg)
143 def _set_ipaddress(self, option, opt_str, arg, parser):
146 def _set_kerberos(self, option, opt_str, arg, parser):
147 if arg.lower() in ["yes", 'true', '1']:
148 self.creds.set_kerberos_state(MUST_USE_KERBEROS)
149 elif arg.lower() in ["no", 'false', '0']:
150 self.creds.set_kerberos_state(DONT_USE_KERBEROS)
152 raise optparse.BadOptionErr("invalid kerberos option: %s" % arg)
154 def _set_simple_bind_dn(self, option, opt_str, arg, parser):
155 self.creds.set_bind_dn(arg)
157 def get_credentials(self, lp, fallback_machine=False):
158 """Obtain the credentials set on the command-line.
160 :param lp: Loadparm object to use.
161 :return: Credentials object
165 self.creds.set_cmdline_callbacks()
167 # possibly fallback to using the machine account, if we have
168 # access to the secrets db
169 if fallback_machine and not self.creds.authentication_requested():
171 self.creds.set_machine_account(lp)
178 class CredentialsOptionsDouble(CredentialsOptions):
179 """Command line options for specifying credentials of two servers."""
180 def __init__(self, parser):
181 CredentialsOptions.__init__(self, parser)
183 self.add_option("--simple-bind-dn2", metavar="DN2", action="callback",
184 callback=self._set_simple_bind_dn2, type=str,
185 help="DN to use for a simple bind")
186 self.add_option("--password2", metavar="PASSWORD2", action="callback",
187 help="Password", type=str,
188 callback=self._set_password2)
189 self.add_option("--username2", metavar="USERNAME2",
190 action="callback", type=str,
191 help="Username for second server",
192 callback=self._parse_username2)
193 self.add_option("--workgroup2", metavar="WORKGROUP2",
194 action="callback", type=str,
195 help="Workgroup for second server",
196 callback=self._parse_workgroup2)
197 self.add_option("--no-pass2", action="store_true",
198 help="Don't ask for a password for the second server")
199 self.add_option("--kerberos2", metavar="KERBEROS2",
200 action="callback", type=str,
201 help="Use Kerberos", callback=self._set_kerberos2)
202 self.creds2 = Credentials()
204 def _parse_username2(self, option, opt_str, arg, parser):
205 self.creds2.parse_string(arg)
207 def _parse_workgroup2(self, option, opt_str, arg, parser):
208 self.creds2.set_domain(arg)
210 def _set_password2(self, option, opt_str, arg, parser):
211 self.creds2.set_password(arg)
212 self.no_pass2 = False
214 def _set_kerberos2(self, option, opt_str, arg, parser):
215 if bool(arg) or arg.lower() == "yes":
216 self.creds2.set_kerberos_state(MUST_USE_KERBEROS)
218 self.creds2.set_kerberos_state(DONT_USE_KERBEROS)
220 def _set_simple_bind_dn2(self, option, opt_str, arg, parser):
221 self.creds2.set_bind_dn(arg)
223 def get_credentials2(self, lp, guess=True):
224 """Obtain the credentials set on the command-line.
226 :param lp: Loadparm object to use.
227 :param guess: Try guess Credentials from environment
228 :return: Credentials object
231 self.creds2.guess(lp)
232 elif not self.creds2.get_username():
233 self.creds2.set_anonymous()
236 self.creds2.set_cmdline_callbacks()