PEP8: fix E128: continuation line under-indented for visual indent
[metze/samba/wip.git] / python / samba / netcmd / common.py
1 # common functions for samba-tool python commands
2 #
3 # Copyright Andrew Tridgell 2010
4 # Copyright Giampaolo Lauria 2011 <lauria2@yahoo.com>
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 re
21 from samba.dcerpc import nbt
22 from samba.net import Net
23
24
25 def _get_user_realm_domain(user):
26     r""" get the realm or the domain and the base user
27         from user like:
28         * username
29         * DOMAIN\username
30         * username@REALM
31     """
32     baseuser = user
33     realm = ""
34     domain = ""
35     m = re.match(r"(\w+)\\(\w+$)", user)
36     if m:
37         domain = m.group(1)
38         baseuser = m.group(2)
39         return (baseuser.lower(), domain.upper(), realm)
40     m = re.match(r"(\w+)@(\w+)", user)
41     if m:
42         baseuser = m.group(1)
43         realm = m.group(2)
44     return (baseuser.lower(), domain, realm.upper())
45
46
47 def netcmd_dnsname(lp):
48     '''return the full DNS name of our own host. Used as a default
49        for hostname when running status queries'''
50     return lp.get('netbios name').lower() + "." + lp.get('realm').lower()
51
52
53 def netcmd_finddc(lp, creds, realm=None):
54     '''Return domain-name of a writable/ldap-capable DC for the default
55        domain (parameter "realm" in smb.conf) unless another realm has been
56        specified as argument'''
57     net = Net(creds=creds, lp=lp)
58     if realm is None:
59         realm = lp.get('realm')
60     cldap_ret = net.finddc(domain=realm,
61                            flags=nbt.NBT_SERVER_LDAP | nbt.NBT_SERVER_DS | nbt.NBT_SERVER_WRITABLE)
62     return cldap_ret.pdc_dns_name
63
64
65 def netcmd_get_domain_infos_via_cldap(lp, creds, address=None):
66     '''Return domain informations (CLDAP record) of the ldap-capable
67        DC with the specified address'''
68     net = Net(creds=creds, lp=lp)
69     cldap_ret = net.finddc(address=address,
70                            flags=nbt.NBT_SERVER_LDAP | nbt.NBT_SERVER_DS)
71     return cldap_ret