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
26 __docformat__
= "restructuredText"
28 class SambaOptions(optparse
.OptionGroup
):
29 """General Samba-related command line options."""
30 def __init__(self
, parser
):
31 optparse
.OptionGroup
.__init
__(self
, parser
, "Samba Common Options")
32 self
.add_option("-s", "--configfile", action
="callback",
33 type=str, metavar
="FILE", help="Configuration file",
34 callback
=self
._load
_configfile
)
35 self
._configfile
= None
37 def get_loadparm_path(self
):
38 """Return the path to the smb.conf file specified on the command line. """
39 return self
._configfile
41 def _load_configfile(self
, option
, opt_str
, arg
, parser
):
42 self
._configfile
= arg
44 def get_loadparm(self
):
45 """Return a loadparm object with data specified on the command line. """
48 if self
._configfile
is not None:
49 lp
.load(self
._configfile
)
50 elif os
.getenv("SMB_CONF_PATH") is not None:
51 lp
.load(os
.getenv("SMB_CONF_PATH"))
56 def get_hostconfig(self
):
57 return Hostconfig(self
.get_loadparm())
60 class VersionOptions(optparse
.OptionGroup
):
61 """Command line option for printing Samba version."""
62 def __init__(self
, parser
):
63 optparse
.OptionGroup
.__init
__(self
, parser
, "Version Options")
66 class CredentialsOptions(optparse
.OptionGroup
):
67 """Command line options for specifying credentials."""
68 def __init__(self
, parser
):
70 optparse
.OptionGroup
.__init
__(self
, parser
, "Credentials Options")
71 self
.add_option("--simple-bind-dn", metavar
="DN", action
="callback",
72 callback
=self
._set
_simple
_bind
_dn
, type=str,
73 help="DN to use for a simple bind")
74 self
.add_option("--password", metavar
="PASSWORD", action
="callback",
75 help="Password", type=str, callback
=self
._set
_password
)
76 self
.add_option("-U", "--username", metavar
="USERNAME",
77 action
="callback", type=str,
78 help="Username", callback
=self
._parse
_username
)
79 self
.add_option("-W", "--workgroup", metavar
="WORKGROUP",
80 action
="callback", type=str,
81 help="Workgroup", callback
=self
._parse
_workgroup
)
82 self
.add_option("-N", "--no-pass", action
="store_true",
83 help="Don't ask for a password")
84 self
.add_option("-k", "--kerberos", metavar
="KERBEROS",
85 action
="callback", type=str,
86 help="Use Kerberos", callback
=self
._set
_kerberos
)
87 self
.creds
= Credentials()
89 def _parse_username(self
, option
, opt_str
, arg
, parser
):
90 self
.creds
.parse_string(arg
)
92 def _parse_workgroup(self
, option
, opt_str
, arg
, parser
):
93 self
.creds
.set_domain(arg
)
95 def _set_password(self
, option
, opt_str
, arg
, parser
):
96 self
.creds
.set_password(arg
)
99 def _set_kerberos(self
, option
, opt_str
, arg
, parser
):
100 if bool(arg
) or arg
.lower() == "yes":
101 self
.creds
.set_kerberos_state(MUST_USE_KERBEROS
)
103 self
.creds
.set_kerberos_state(DONT_USE_KERBEROS
)
105 def _set_simple_bind_dn(self
, option
, opt_str
, arg
, parser
):
106 self
.creds
.set_bind_dn(arg
)
108 def get_credentials(self
, lp
):
109 """Obtain the credentials set on the command-line.
111 :param lp: Loadparm object to use.
112 :return: Credentials object
116 self
.creds
.set_cmdline_callbacks()
119 class CredentialsOptionsDouble(CredentialsOptions
):
120 """Command line options for specifying credentials of two servers."""
121 def __init__(self
, parser
):
122 CredentialsOptions
.__init
__(self
, parser
)
124 self
.add_option("--simple-bind-dn2", metavar
="DN2", action
="callback",
125 callback
=self
._set
_simple
_bind
_dn
2, type=str,
126 help="DN to use for a simple bind")
127 self
.add_option("--password2", metavar
="PASSWORD2", action
="callback",
128 help="Password", type=str, callback
=self
._set
_password
2)
129 self
.add_option("--username2", metavar
="USERNAME2",
130 action
="callback", type=str,
131 help="Username for second server", callback
=self
._parse
_username
2)
132 self
.add_option("--workgroup2", metavar
="WORKGROUP2",
133 action
="callback", type=str,
134 help="Workgroup for second server", callback
=self
._parse
_workgroup
2)
135 self
.add_option("--no-pass2", action
="store_true",
136 help="Don't ask for a password for the second server")
137 self
.add_option("--kerberos2", metavar
="KERBEROS2",
138 action
="callback", type=str,
139 help="Use Kerberos", callback
=self
._set
_kerberos
2)
140 self
.creds2
= Credentials()
142 def _parse_username2(self
, option
, opt_str
, arg
, parser
):
143 self
.creds2
.parse_string(arg
)
145 def _parse_workgroup2(self
, option
, opt_str
, arg
, parser
):
146 self
.creds2
.set_domain(arg
)
148 def _set_password2(self
, option
, opt_str
, arg
, parser
):
149 self
.creds2
.set_password(arg
)
150 self
.no_pass2
= False
152 def _set_kerberos2(self
, option
, opt_str
, arg
, parser
):
153 if bool(arg
) or arg
.lower() == "yes":
154 self
.creds2
.set_kerberos_state(MUST_USE_KERBEROS
)
156 self
.creds2
.set_kerberos_state(DONT_USE_KERBEROS
)
158 def _set_simple_bind_dn2(self
, option
, opt_str
, arg
, parser
):
159 self
.creds2
.set_bind_dn(arg
)
161 def get_credentials2(self
, lp
):
162 """Obtain the credentials set on the command-line.
164 :param lp: Loadparm object to use.
165 :return: Credentials object
167 self
.creds2
.guess(lp
)
169 self
.creds2
.set_cmdline_callbacks()