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."""
23 from credentials import Credentials, DONT_USE_KERBEROS, MUST_USE_KERBEROS
24 from hostconfig import Hostconfig
27 __docformat__ = "restructuredText"
29 class SambaOptions(optparse.OptionGroup):
30 """General Samba-related command line options."""
31 def __init__(self, parser):
33 optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
34 self.add_option("-s", "--configfile", action="callback",
35 type=str, metavar="FILE", help="Configuration file",
36 callback=self._load_configfile)
37 self.add_option("-d", "--debuglevel", action="callback",
38 type=int, metavar="DEBUGLEVEL", help="debug level",
39 callback=self._set_debuglevel)
40 self.add_option("--option", action="callback",
41 type=str, metavar="OPTION", help="set smb.conf option from command line",
42 callback=self._set_option)
43 self.add_option("--realm", action="callback",
44 type=str, metavar="REALM", help="set the realm name",
45 callback=self._set_realm)
46 self._configfile = None
47 self._lp = param.LoadParm()
49 def get_loadparm_path(self):
50 """Return the path to the smb.conf file specified on the command line. """
51 return self._configfile
53 def _load_configfile(self, option, opt_str, arg, parser):
54 self._configfile = arg
56 def _set_debuglevel(self, option, opt_str, arg, parser):
57 self._lp.set('debug level', str(arg))
59 def _set_realm(self, option, opt_str, arg, parser):
60 self._lp.set('realm', arg)
62 def _set_option(self, option, opt_str, arg, parser):
63 if arg.find('=') == -1:
64 print("--option takes a 'a=b' argument")
67 self._lp.set(a[0], a[1])
69 def get_loadparm(self):
70 """Return a loadparm object with data specified on the command line. """
71 if self._configfile is not None:
72 self._lp.load(self._configfile)
73 elif os.getenv("SMB_CONF_PATH") is not None:
74 self._lp.load(os.getenv("SMB_CONF_PATH"))
76 self._lp.load_default()
79 def get_hostconfig(self):
80 return Hostconfig(self.get_loadparm())
83 class VersionOptions(optparse.OptionGroup):
84 """Command line option for printing Samba version."""
85 def __init__(self, parser):
86 optparse.OptionGroup.__init__(self, parser, "Version Options")
87 self.add_option("--version", action="callback",
88 callback=self._display_version,
89 help="Display version number")
91 def _display_version(self, option, opt_str, arg, parser):
97 class CredentialsOptions(optparse.OptionGroup):
98 """Command line options for specifying credentials."""
99 def __init__(self, parser):
101 self.ipaddress = None
102 optparse.OptionGroup.__init__(self, parser, "Credentials Options")
103 self.add_option("--simple-bind-dn", metavar="DN", action="callback",
104 callback=self._set_simple_bind_dn, type=str,
105 help="DN to use for a simple bind")
106 self.add_option("--password", metavar="PASSWORD", action="callback",
107 help="Password", type=str, callback=self._set_password)
108 self.add_option("-U", "--username", metavar="USERNAME",
109 action="callback", type=str,
110 help="Username", callback=self._parse_username)
111 self.add_option("-W", "--workgroup", metavar="WORKGROUP",
112 action="callback", type=str,
113 help="Workgroup", callback=self._parse_workgroup)
114 self.add_option("-N", "--no-pass", action="store_true",
115 help="Don't ask for a password")
116 self.add_option("-k", "--kerberos", metavar="KERBEROS",
117 action="callback", type=str,
118 help="Use Kerberos", callback=self._set_kerberos)
119 self.add_option("", "--ipaddress", metavar="IPADDRESS",
120 action="callback", type=str,
121 help="IP address of server", callback=self._set_ipaddress)
122 self.creds = Credentials()
124 def _parse_username(self, option, opt_str, arg, parser):
125 self.creds.parse_string(arg)
127 def _parse_workgroup(self, option, opt_str, arg, parser):
128 self.creds.set_domain(arg)
130 def _set_password(self, option, opt_str, arg, parser):
131 self.creds.set_password(arg)
134 def _set_ipaddress(self, option, opt_str, arg, parser):
137 def _set_kerberos(self, option, opt_str, arg, parser):
138 if bool(arg) or arg.lower() == "yes":
139 self.creds.set_kerberos_state(MUST_USE_KERBEROS)
141 self.creds.set_kerberos_state(DONT_USE_KERBEROS)
143 def _set_simple_bind_dn(self, option, opt_str, arg, parser):
144 self.creds.set_bind_dn(arg)
146 def get_credentials(self, lp):
147 """Obtain the credentials set on the command-line.
149 :param lp: Loadparm object to use.
150 :return: Credentials object
154 self.creds.set_cmdline_callbacks()
157 class CredentialsOptionsDouble(CredentialsOptions):
158 """Command line options for specifying credentials of two servers."""
159 def __init__(self, parser):
160 CredentialsOptions.__init__(self, parser)
162 self.add_option("--simple-bind-dn2", metavar="DN2", action="callback",
163 callback=self._set_simple_bind_dn2, type=str,
164 help="DN to use for a simple bind")
165 self.add_option("--password2", metavar="PASSWORD2", action="callback",
166 help="Password", type=str, callback=self._set_password2)
167 self.add_option("--username2", metavar="USERNAME2",
168 action="callback", type=str,
169 help="Username for second server", callback=self._parse_username2)
170 self.add_option("--workgroup2", metavar="WORKGROUP2",
171 action="callback", type=str,
172 help="Workgroup for second server", callback=self._parse_workgroup2)
173 self.add_option("--no-pass2", action="store_true",
174 help="Don't ask for a password for the second server")
175 self.add_option("--kerberos2", metavar="KERBEROS2",
176 action="callback", type=str,
177 help="Use Kerberos", callback=self._set_kerberos2)
178 self.creds2 = Credentials()
180 def _parse_username2(self, option, opt_str, arg, parser):
181 self.creds2.parse_string(arg)
183 def _parse_workgroup2(self, option, opt_str, arg, parser):
184 self.creds2.set_domain(arg)
186 def _set_password2(self, option, opt_str, arg, parser):
187 self.creds2.set_password(arg)
188 self.no_pass2 = False
190 def _set_kerberos2(self, option, opt_str, arg, parser):
191 if bool(arg) or arg.lower() == "yes":
192 self.creds2.set_kerberos_state(MUST_USE_KERBEROS)
194 self.creds2.set_kerberos_state(DONT_USE_KERBEROS)
196 def _set_simple_bind_dn2(self, option, opt_str, arg, parser):
197 self.creds2.set_bind_dn(arg)
199 def get_credentials2(self, lp):
200 """Obtain the credentials set on the command-line.
202 :param lp: Loadparm object to use.
203 :return: Credentials object
205 self.creds2.guess(lp)
207 self.creds2.set_cmdline_callbacks()