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"
25 from samba.credentials import (
30 from samba.hostconfig import Hostconfig
34 class SambaOptions(optparse.OptionGroup):
35 """General Samba-related command line options."""
37 def __init__(self, parser):
38 from samba.param import LoadParm
39 optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
40 self.add_option("-s", "--configfile", action="callback",
41 type=str, metavar="FILE", help="Configuration file",
42 callback=self._load_configfile)
43 self.add_option("-d", "--debuglevel", action="callback",
44 type=int, metavar="DEBUGLEVEL", help="debug level",
45 callback=self._set_debuglevel)
46 self.add_option("--option", action="callback",
47 type=str, metavar="OPTION", help="set smb.conf option from command line",
48 callback=self._set_option)
49 self.add_option("--realm", action="callback",
50 type=str, metavar="REALM", help="set the realm name",
51 callback=self._set_realm)
52 self._configfile = None
55 def get_loadparm_path(self):
56 """Return the path to the smb.conf file specified on the command line. """
57 return self._configfile
59 def _load_configfile(self, option, opt_str, arg, parser):
60 self._configfile = arg
62 def _set_debuglevel(self, option, opt_str, arg, parser):
63 self._lp.set('debug level', str(arg))
65 def _set_realm(self, option, opt_str, arg, parser):
66 self._lp.set('realm', arg)
68 def _set_option(self, option, opt_str, arg, parser):
69 if arg.find('=') == -1:
70 print("--option takes a 'a=b' argument")
73 self._lp.set(a[0], a[1])
75 def get_loadparm(self):
76 """Return a loadparm object with data specified on the command line. """
77 if self._configfile is not None:
78 self._lp.load(self._configfile)
79 elif os.getenv("SMB_CONF_PATH") is not None:
80 self._lp.load(os.getenv("SMB_CONF_PATH"))
82 self._lp.load_default()
85 def get_hostconfig(self):
86 return Hostconfig(self.get_loadparm())
89 class VersionOptions(optparse.OptionGroup):
90 """Command line option for printing Samba version."""
91 def __init__(self, parser):
92 optparse.OptionGroup.__init__(self, parser, "Version Options")
93 self.add_option("--version", action="callback",
94 callback=self._display_version,
95 help="Display version number")
97 def _display_version(self, option, opt_str, arg, parser):
103 class CredentialsOptions(optparse.OptionGroup):
104 """Command line options for specifying credentials."""
105 def __init__(self, parser):
107 self.ipaddress = None
108 optparse.OptionGroup.__init__(self, parser, "Credentials Options")
109 self.add_option("--simple-bind-dn", metavar="DN", action="callback",
110 callback=self._set_simple_bind_dn, type=str,
111 help="DN to use for a simple bind")
112 self.add_option("--password", metavar="PASSWORD", action="callback",
113 help="Password", type=str, callback=self._set_password)
114 self.add_option("-U", "--username", metavar="USERNAME",
115 action="callback", type=str,
116 help="Username", callback=self._parse_username)
117 self.add_option("-W", "--workgroup", metavar="WORKGROUP",
118 action="callback", type=str,
119 help="Workgroup", callback=self._parse_workgroup)
120 self.add_option("-N", "--no-pass", action="store_true",
121 help="Don't ask for a password")
122 self.add_option("-k", "--kerberos", metavar="KERBEROS",
123 action="callback", type=str,
124 help="Use Kerberos", callback=self._set_kerberos)
125 self.add_option("", "--ipaddress", metavar="IPADDRESS",
126 action="callback", type=str,
127 help="IP address of server", callback=self._set_ipaddress)
128 self.creds = Credentials()
130 def _parse_username(self, option, opt_str, arg, parser):
131 self.creds.parse_string(arg)
133 def _parse_workgroup(self, option, opt_str, arg, parser):
134 self.creds.set_domain(arg)
136 def _set_password(self, option, opt_str, arg, parser):
137 self.creds.set_password(arg)
140 def _set_ipaddress(self, option, opt_str, arg, parser):
143 def _set_kerberos(self, option, opt_str, arg, parser):
144 if arg.lower() in ["yes", 'true', '1']:
145 self.creds.set_kerberos_state(MUST_USE_KERBEROS)
146 elif arg.lower() in ["no", 'false', '0']:
147 self.creds.set_kerberos_state(DONT_USE_KERBEROS)
149 raise optparse.BadOptionErr("invalid kerberos option: %s" % arg)
151 def _set_simple_bind_dn(self, option, opt_str, arg, parser):
152 self.creds.set_bind_dn(arg)
154 def get_credentials(self, lp, fallback_machine=False):
155 """Obtain the credentials set on the command-line.
157 :param lp: Loadparm object to use.
158 :return: Credentials object
162 self.creds.set_cmdline_callbacks()
164 # possibly fallback to using the machine account, if we have
165 # access to the secrets db
166 if fallback_machine and not self.creds.authentication_requested():
168 self.creds.set_machine_account(lp)
174 class CredentialsOptionsDouble(CredentialsOptions):
175 """Command line options for specifying credentials of two servers."""
176 def __init__(self, parser):
177 CredentialsOptions.__init__(self, parser)
179 self.add_option("--simple-bind-dn2", metavar="DN2", action="callback",
180 callback=self._set_simple_bind_dn2, type=str,
181 help="DN to use for a simple bind")
182 self.add_option("--password2", metavar="PASSWORD2", action="callback",
183 help="Password", type=str, callback=self._set_password2)
184 self.add_option("--username2", metavar="USERNAME2",
185 action="callback", type=str,
186 help="Username for second server", callback=self._parse_username2)
187 self.add_option("--workgroup2", metavar="WORKGROUP2",
188 action="callback", type=str,
189 help="Workgroup for second server", callback=self._parse_workgroup2)
190 self.add_option("--no-pass2", action="store_true",
191 help="Don't ask for a password for the second server")
192 self.add_option("--kerberos2", metavar="KERBEROS2",
193 action="callback", type=str,
194 help="Use Kerberos", callback=self._set_kerberos2)
195 self.creds2 = Credentials()
197 def _parse_username2(self, option, opt_str, arg, parser):
198 self.creds2.parse_string(arg)
200 def _parse_workgroup2(self, option, opt_str, arg, parser):
201 self.creds2.set_domain(arg)
203 def _set_password2(self, option, opt_str, arg, parser):
204 self.creds2.set_password(arg)
205 self.no_pass2 = False
207 def _set_kerberos2(self, option, opt_str, arg, parser):
208 if bool(arg) or arg.lower() == "yes":
209 self.creds2.set_kerberos_state(MUST_USE_KERBEROS)
211 self.creds2.set_kerberos_state(DONT_USE_KERBEROS)
213 def _set_simple_bind_dn2(self, option, opt_str, arg, parser):
214 self.creds2.set_bind_dn(arg)
216 def get_credentials2(self, lp, guess=True):
217 """Obtain the credentials set on the command-line.
219 :param lp: Loadparm object to use.
220 :param guess: Try guess Credentials from environment
221 :return: Credentials object
224 self.creds2.guess(lp)
225 elif not self.creds2.get_username():
226 self.creds2.set_anonymous()
229 self.creds2.set_cmdline_callbacks()