libcli/smb: SMB 3.0.2: define SMB2_READFLAG_READ_UNBUFFERED
[Samba.git] / python / samba / getopt.py
blob0f9765864c1089167e0e57d62470f036862abcc2
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"
22 import optparse
23 import os
24 from samba.credentials import (
25 Credentials,
26 AUTO_USE_KERBEROS,
27 DONT_USE_KERBEROS,
28 MUST_USE_KERBEROS,
30 from samba.hostconfig import Hostconfig
31 import sys
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",
48 help="set smb.conf option from command line",
49 callback=self._set_option)
50 self.add_option("--realm", action="callback",
51 type=str, metavar="REALM", help="set the realm name",
52 callback=self._set_realm)
53 self._configfile = None
54 self._lp = LoadParm()
55 self.realm = 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 if arg < 0:
66 raise optparse.OptionValueError("invalid %s option value: %s" %
67 (opt_str, arg))
68 self._lp.set('debug level', str(arg))
70 def _set_realm(self, option, opt_str, arg, parser):
71 self._lp.set('realm', arg)
72 self.realm = arg
74 def _set_option(self, option, opt_str, arg, parser):
75 if arg.find('=') == -1:
76 raise optparse.OptionValueError(
77 "--option option takes a 'a=b' argument")
78 a = arg.split('=')
79 try:
80 self._lp.set(a[0], a[1])
81 except Exception, e:
82 raise optparse.OptionValueError(
83 "invalid --option option value %r: %s" % (arg, e))
85 def get_loadparm(self):
86 """Return loadparm object with data specified on the command line."""
87 if self._configfile is not None:
88 self._lp.load(self._configfile)
89 elif os.getenv("SMB_CONF_PATH") is not None:
90 self._lp.load(os.getenv("SMB_CONF_PATH"))
91 else:
92 self._lp.load_default()
93 return self._lp
95 def get_hostconfig(self):
96 return Hostconfig(self.get_loadparm())
99 class VersionOptions(optparse.OptionGroup):
100 """Command line option for printing Samba version."""
101 def __init__(self, parser):
102 optparse.OptionGroup.__init__(self, parser, "Version Options")
103 self.add_option("-V", "--version", action="callback",
104 callback=self._display_version,
105 help="Display version number")
107 def _display_version(self, option, opt_str, arg, parser):
108 import samba
109 print samba.version
110 sys.exit(0)
113 def parse_kerberos_arg(arg, opt_str):
114 if arg.lower() in ["yes", 'true', '1']:
115 return MUST_USE_KERBEROS
116 elif arg.lower() in ["no", 'false', '0']:
117 return DONT_USE_KERBEROS
118 elif arg.lower() in ["auto"]:
119 return AUTO_USE_KERBEROS
120 else:
121 raise optparse.OptionValueError("invalid %s option value: %s" %
122 (opt_str, arg))
125 class CredentialsOptions(optparse.OptionGroup):
126 """Command line options for specifying credentials."""
128 def __init__(self, parser):
129 self.ask_for_password = True
130 self.ipaddress = None
131 self.machine_pass = False
132 optparse.OptionGroup.__init__(self, parser, "Credentials Options")
133 self.add_option("--simple-bind-dn", metavar="DN", action="callback",
134 callback=self._set_simple_bind_dn, type=str,
135 help="DN to use for a simple bind")
136 self.add_option("--password", metavar="PASSWORD", action="callback",
137 help="Password", type=str, callback=self._set_password)
138 self.add_option("-U", "--username", metavar="USERNAME",
139 action="callback", type=str,
140 help="Username", callback=self._parse_username)
141 self.add_option("-W", "--workgroup", metavar="WORKGROUP",
142 action="callback", type=str,
143 help="Workgroup", callback=self._parse_workgroup)
144 self.add_option("-N", "--no-pass", action="callback",
145 help="Don't ask for a password",
146 callback=self._set_no_password)
147 self.add_option("-k", "--kerberos", metavar="KERBEROS",
148 action="callback", type=str,
149 help="Use Kerberos", callback=self._set_kerberos)
150 self.add_option("", "--ipaddress", metavar="IPADDRESS",
151 action="callback", type=str,
152 help="IP address of server",
153 callback=self._set_ipaddress)
154 self.add_option("-P", "--machine-pass",
155 action="callback",
156 help="Use stored machine account password",
157 callback=self._set_machine_pass)
158 self.creds = Credentials()
160 def _parse_username(self, option, opt_str, arg, parser):
161 self.creds.parse_string(arg)
162 self.machine_pass = False
164 def _parse_workgroup(self, option, opt_str, arg, parser):
165 self.creds.set_domain(arg)
167 def _set_password(self, option, opt_str, arg, parser):
168 self.creds.set_password(arg)
169 self.ask_for_password = False
170 self.machine_pass = False
172 def _set_no_password(self, option, opt_str, arg, parser):
173 self.ask_for_password = False
175 def _set_machine_pass(self, option, opt_str, arg, parser):
176 self.machine_pass = True
178 def _set_ipaddress(self, option, opt_str, arg, parser):
179 self.ipaddress = arg
181 def _set_kerberos(self, option, opt_str, arg, parser):
182 self.creds.set_kerberos_state(parse_kerberos_arg(arg, opt_str))
184 def _set_simple_bind_dn(self, option, opt_str, arg, parser):
185 self.creds.set_bind_dn(arg)
187 def get_credentials(self, lp, fallback_machine=False):
188 """Obtain the credentials set on the command-line.
190 :param lp: Loadparm object to use.
191 :return: Credentials object
193 self.creds.guess(lp)
194 if self.machine_pass:
195 self.creds.set_machine_account(lp)
196 elif self.ask_for_password:
197 self.creds.set_cmdline_callbacks()
199 # possibly fallback to using the machine account, if we have
200 # access to the secrets db
201 if fallback_machine and not self.creds.authentication_requested():
202 try:
203 self.creds.set_machine_account(lp)
204 except Exception:
205 pass
207 return self.creds
210 class CredentialsOptionsDouble(CredentialsOptions):
211 """Command line options for specifying credentials of two servers."""
213 def __init__(self, parser):
214 CredentialsOptions.__init__(self, parser)
215 self.no_pass2 = True
216 self.add_option("--simple-bind-dn2", metavar="DN2", action="callback",
217 callback=self._set_simple_bind_dn2, type=str,
218 help="DN to use for a simple bind")
219 self.add_option("--password2", metavar="PASSWORD2", action="callback",
220 help="Password", type=str,
221 callback=self._set_password2)
222 self.add_option("--username2", metavar="USERNAME2",
223 action="callback", type=str,
224 help="Username for second server",
225 callback=self._parse_username2)
226 self.add_option("--workgroup2", metavar="WORKGROUP2",
227 action="callback", type=str,
228 help="Workgroup for second server",
229 callback=self._parse_workgroup2)
230 self.add_option("--no-pass2", action="store_true",
231 help="Don't ask for a password for the second server")
232 self.add_option("--kerberos2", metavar="KERBEROS2",
233 action="callback", type=str,
234 help="Use Kerberos", callback=self._set_kerberos2)
235 self.creds2 = Credentials()
237 def _parse_username2(self, option, opt_str, arg, parser):
238 self.creds2.parse_string(arg)
240 def _parse_workgroup2(self, option, opt_str, arg, parser):
241 self.creds2.set_domain(arg)
243 def _set_password2(self, option, opt_str, arg, parser):
244 self.creds2.set_password(arg)
245 self.no_pass2 = False
247 def _set_kerberos2(self, option, opt_str, arg, parser):
248 self.creds2.set_kerberos_state(parse_kerberos_arg(arg, opt_str))
250 def _set_simple_bind_dn2(self, option, opt_str, arg, parser):
251 self.creds2.set_bind_dn(arg)
253 def get_credentials2(self, lp, guess=True):
254 """Obtain the credentials set on the command-line.
256 :param lp: Loadparm object to use.
257 :param guess: Try guess Credentials from environment
258 :return: Credentials object
260 if guess:
261 self.creds2.guess(lp)
262 elif not self.creds2.get_username():
263 self.creds2.set_anonymous()
265 if self.no_pass2:
266 self.creds2.set_cmdline_callbacks()
267 return self.creds2