Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-abartlet
[nivanova/samba-autobuild/.git] / source4 / scripting / python / samba / getopt.py
1 #!/usr/bin/python
2
3 # Samba-specific bits for optparse
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5 #   
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.
10 #   
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.
15 #   
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/>.
18 #
19
20 import optparse
21 from credentials import Credentials, AUTO_USE_KERBEROS, DONT_USE_KERBEROS, MUST_USE_KERBEROS
22
23 class SambaOptions(optparse.OptionGroup):
24     def __init__(self, parser):
25         optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
26         self.add_option("-s", "--configfile", action="callback",
27                         type=str, metavar="FILE", help="Configuration file", 
28                         callback=self._load_configfile)
29         self._configfile = None
30
31     def get_loadparm_path(self):
32         return self._configfile
33
34     def _load_configfile(self, option, opt_str, arg, parser):
35         self._configfile = arg
36
37     def get_loadparm(self):
38         import os, param
39         lp = param.LoadParm()
40         if self._configfile is not None:
41             lp.load(self._configfile)
42         elif os.getenv("SMB_CONF_PATH") is not None:
43             lp.load(os.getenv("SMB_CONF_PATH"))
44         else:
45             lp.load_default()
46         return lp
47
48 class VersionOptions(optparse.OptionGroup):
49     def __init__(self, parser):
50         optparse.OptionGroup.__init__(self, parser, "Version Options")
51
52
53 class CredentialsOptions(optparse.OptionGroup):
54     def __init__(self, parser):
55         self.no_pass = False
56         optparse.OptionGroup.__init__(self, parser, "Credentials Options")
57         self.add_option("--simple-bind-dn", metavar="DN", action="callback",
58                         callback=self._set_simple_bind_dn, type=str,
59                         help="DN to use for a simple bind")
60         self.add_option("--password", metavar="PASSWORD", action="callback",
61                         help="Password", type=str, callback=self._set_password)
62         self.add_option("-U", "--username", metavar="USERNAME", 
63                         action="callback", type=str,
64                         help="Username", callback=self._parse_username)
65         self.add_option("-W", "--workgroup", metavar="WORKGROUP", 
66                         action="callback", type=str,
67                         help="Workgroup", callback=self._parse_workgroup)
68         self.add_option("-N", "--no-pass", action="store_true",
69                         help="Don't ask for a password")
70         self.add_option("-k", "--kerberos", metavar="KERBEROS", 
71                         action="callback", type=str,
72                         help="Use Kerberos", callback=self._set_kerberos)
73         self.creds = Credentials()
74
75     def _parse_username(self, option, opt_str, arg, parser):
76         self.creds.parse_string(arg)
77
78     def _parse_workgroup(self, option, opt_str, arg, parser):
79         self.creds.set_domain(arg)
80
81     def _set_password(self, option, opt_str, arg, parser):
82         self.creds.set_password(arg)
83
84     def _set_kerberos(self, option, opt_str, arg, parser):
85         if bool(arg) or arg.lower() == "yes":
86             self.creds.set_kerberos_state(MUST_USE_KERBEROS)
87         else:
88             self.creds.set_kerberos_state(DONT_USE_KERBEROS)
89
90     def _set_simple_bind_dn(self, option, opt_str, arg, parser):
91         self.creds.set_bind_dn(arg)
92
93     def get_credentials(self, lp):
94         self.creds.guess(lp)
95         if not self.no_pass:
96             self.creds.set_cmdline_callbacks()
97         return self.creds