summaryrefslogtreecommitdiffstats
path: root/pykolab/setup/ldap_setup.py
blob: f38171126c3cfd74d7c45b118403bf61420beb82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- coding: utf-8 -*-
#
# Copyright 2010 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import getpass
import ldap
import logging
import os
import sys

from pykolab.translate import _
from pykolab import constants
from pykolab import utils

def setup():
    """
        Setup LDAP from here.

        # Register with existing LDAP tree?
        #* Verify schema loaded
        #* Forget about flexibility
        # Create new LDAP tree
        #* OpenLDAP
    """

    (service, other_services) = utils.is_service([
            'dirsrv',
            'ldap',
            'slapd'
        ])

    for item in other_services:
        print >> sys.stderr, _("Warning: LDAP Service '%s' is available on " + \
                            "this system as well.") % item

    print _("Found system service %s.") % service

    #ldap_uri = utils.ask_question(_("LDAP URI (read/write)"), "ldap://ldap.%s" %(constants.domainname))
    ldap_uri = utils.ask_question(_("LDAP URI (read/write)"), "ldap://localhost")
    manager_dn = utils.ask_question("Manager DN", "cn=Directory Manager")
    #manager_pw = utils.ask_question("Manager Password", password=True)
    manager_pw = utils.ask_question("Manager Password", "verysecret", password=True)

    try:
        con = ldap.initialize(ldap_uri)
        con.bind(manager_dn, manager_pw, ldap.AUTH_SIMPLE)
    except TypeError:
        # This is a funny input error ("")
        print >> sys.stderr, _("Could not connect to LDAP server due to " + \
                "invalid LDAP URI format or no local socket")
        sys.exit(1)
    except ldap.INVALID_CREDENTIALS, e:
        print >> sys.stderr, _("Your username or password are incorrect")
        sys.exit(1)
    except ldap.LDAPError, e:
        print >> sys.stderr, _("Could not connect to LDAP server due to " + \
                "invalid LDAP URI (or invalid format) or no local socket")
        sys.exit(1)
    except ldap.SERVER_DOWN, e:
        print >> sys.stderr, e['desc']
        sys.exit(1)

    # Returns a list of dicts (empty list if not found)
    kolab_config_dn_results = con.search_s('cn=kolab,cn=config', ldap.SCOPE_SUBTREE, '(cn=kolab)', ['cn'])

    if len(kolab_config_dn_results) == 1:
        print >> sys.stdout, "Success: Found cn=kolab,cn=config"

    else:
        initialize_kolab_config_dn(con)

    #if not service == "":
        #if service in constants.SERVICE_MAP.keys():
            #exec("setup_%s()" % constants.SERVICE_MAP['%s' % service]['type'])
    #else:
        ## No service found on the local system, so ask a bunch of questions.
        ##
        ## - ldap uri
        ## - manager dn
        ## - manager pw
        #pass

def setup_389ds():
    """
        Executes for a local 389 Directory Server installation.
    """

    for (path, directories, files) in os.walk("/etc/dirsrv/"):
        for directory in directories:
            if directory.startswith('slapd-'):
                print "Found a dirsrv instance %r" % directory
                dirsrv_instance = directory

#    if dirsrv_instance == '':
#        # Apparently we're working with a remote dirsrv... are we going to have
#        # to set up the local directory service as well??
#        raise NotImplementedError, _("Initializing a 389 Directory Server has not been implemented yet. Please use setup-ds-admin")
#
#    elif dirsrv_instance == 'slapd-localhost':
#        # The server is on localhost
#        ldap_conn = ldap.initialize(uri="ldap://localhost:389")
#        try:
#            ldap_conn.start_tls_s()
#        except ldap.LDAPError, e:
#            pass
#
#    else:
#        pass

def setup_openldap():
    print "im an openldap system!"


def initialize_kolab_config_dn(ldap_con=None):
    if ldap_con == None:
        return

    ldif = """
dn: cn=kolab,cn=config
cirUpdateSchedule: New
cn: kolab
objectClass: top
objectClass: extensibleobject
"""