~usmteam/usm/usm_directory

« back to all changes in this revision

Viewing changes to usm_directory_fr.py

  • Committer: oly at digitaloctave
  • Date: 2010-11-28 21:41:43 UTC
  • Revision ID: oly@digitaloctave.com-20101128214143-9cw5kqplc0xqmd13
rearrangedĀ files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python -O 
 
2
#Ubuntu Server Manager
 
3
#Copyright (C) 2007, 2008, 2009 usmteam
 
4
#
 
5
#This file is part of Ubuntu Server Manager 
 
6
#
 
7
#This program is free software; you can redistribute it and/or
 
8
#modify it under the terms of the GNU General Public License
 
9
#as published by the Free Software Foundation; either version 2
 
10
#of the License, or (at your option) any later version.
 
11
#
 
12
#This program is distributed in the hope that it will be useful,
 
13
#but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#GNU General Public License for more details.
 
16
#
 
17
#You should have received a copy of the GNU General Public License
 
18
#along with this program; if not, write to the Free Software
 
19
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
20
#
 
21
#In addition, as a special exception, Oliver Marks gives permission to link
 
22
#the code of its release of usm with the OpenSSL project's "OpenSSL" library
 
23
#(or modified versions of the "OpenSSL" library that use the same license
 
24
#as the original version), and distribute the linked executables.
 
25
 
 
26
import sys
 
27
import os
 
28
import commands
 
29
import ldap
 
30
import ldif
 
31
import ldap.modlist as modlist
 
32
 
 
33
from backend import *
 
34
from libs.v3.lib_plugins_helper import plugins
 
35
from libs.v3.lib_xml_data_form import forms
 
36
 
 
37
 
 
38
import gettext
 
39
gettext.bindtextdomain('usm_directory', './lang/')
 
40
gettext.textdomain('usm_directory')
 
41
_ = gettext.gettext
 
42
 
 
43
#info for building the debs
 
44
__debname__="usm-directory"
 
45
__debversion__="0.1"
 
46
#space new line full stop for blank line 
 
47
__debdescription__="""This Package Contains a plugin for managing a ldap directory service.\n"""
 
48
__debdepends__="python-ldap (>= 2.3.1), slapd (>= 2.4.9)"
 
49
 
 
50
 
 
51
#slapd -d 1
 
52
#ldapsearch -x -s base (objectclass=*) namingContexts
 
53
#gui for brwsing directory gq 
 
54
 
 
55
class ldifcfg:
 
56
    def __init__(self):
 
57
        pass
 
58
        
 
59
    def user(self):
 
60
        pass
 
61
        
 
62
    def machine(self):
 
63
        pass
 
64
    
 
65
    def group(self,name,desc="New Organizational unit / group"):
 
66
        fpldif="dn: ou="+name+",o=domain-name\n"
 
67
        fpldif+="ou: "+name+"\n"
 
68
        fpldif+="objectClass: top\n"
 
69
        fpldif+="objectClass: organizationalUnit\n"
 
70
        fpldif+="description: "+desc+"\n"
 
71
        return fpldif
 
72
        
 
73
    def group_list(self,name,desc="New Organizational unit / group"):
 
74
        fpldif={}
 
75
        #fpldif="dn: ou="+name+",o=domain-name\n"
 
76
        fpldif["ou"]=name
 
77
        fpldif["objectClass"]="top"
 
78
        fpldif["objectClass"]="organizationalUnit"
 
79
        fpldif["description"]=desc
 
80
        return fpldif
 
81
 
 
82
    def user_list(self,uid,fname,sname,password):
 
83
        fpldif={}
 
84
        #dn: cn=John Doe, ou=Rochester, o=IBM, c=US
 
85
        fpldif["objectclass"]=["person","inetOrgPerson","top","organizationalPerson","ePerson"]
 
86
        fpldif["objectclass"]=["person","inetOrgPerson","top"]
 
87
        fpldif["cn"]=fname+" "+sname
 
88
        fpldif["sn"]=sname
 
89
        fpldif["uid"]=uid#"jdoe"
 
90
        fpldif["userpassword"]=password #"secretpass"
 
91
        return fpldif
 
92
 
 
93
 
 
94
class plugin(plugins):
 
95
    plugin_name="usm_directory"
 
96
    plugin_fold="usm_directory"
 
97
    plugin_form="usm_directory.xml"
 
98
    
 
99
    def __init__(self,core_globals):
 
100
        plugins.__init__(self,globals=core_globals)  #run __init__ method from plugins class instead of overridding
 
101
        self.path_schema="/etc/ldap/schema/"
 
102
        #/proc/sys/kernel/domainname for fqdn
 
103
        fp=open("/proc/sys/kernel/hostname")
 
104
        self.hostname=fp.read().strip("\n")
 
105
        fp.close()
 
106
        #self.load_ldif=from_ldif_file()
 
107
        
 
108
        
 
109
        self.file_form=self.pluginpath+self.plugin_form
 
110
        self.file_data=self.profilepath+"dns.xml"
 
111
        self.ldap_group_ts=self.config.load_form(self.file_form,"group")
 
112
        self.ldap_user_ts=self.config.load_form(self.file_form,"user")
 
113
 
 
114
        
 
115
        
 
116
        
 
117
        
 
118
        self.server="127.0.0.1"
 
119
        self.rootdn=self.get_basedn()
 
120
 
 
121
        self.password=self.get_system_password()#"admin"#self.get_password()
 
122
        #self.md5password=self.get_password().strip("\n")
 
123
        print "slapd password = \"%s\"" % self.password
 
124
        print "slappasswd -h {MD5} -s "+self.password
 
125
        print "slappasswd -h {MD5} -s \"%s\"" % self.password
 
126
        print commands.getstatusoutput("slappasswd -h {MD5} -s \"%s\"" % self.password)
 
127
        self.md5password=commands.getstatusoutput("slappasswd -h {MD5} -s \"%s\"" % self.password)[1]
 
128
        print "slapd password = "+self.md5password
 
129
        self.admindn="cn=admin,"+self.rootdn  
 
130
        self.directory=None 
 
131
    
 
132
        self.newldif=ldifcfg()
 
133
    
 
134
        if not os.path.exists(self.configpath+"slapd.conf"):
 
135
            self.generateconfig()
 
136
            
 
137
        self.inital_ldap()
 
138
 
 
139
    def index(self,form,web_page,globals={}):
 
140
        xmlid=self.config.page(web_page,form)
 
141
        htmout=plugins.index(self,form,web_page,globals=globals)
 
142
        print self.get_basedn()
 
143
        htmout+=self.setup_test()
 
144
        
 
145
        #htmout+=plugins.index(self,form,web_page)
 
146
        
 
147
        if self.rootdn=="":
 
148
            self.notify+=self.web.notifybox("No Base Domain have you setup your servers primary settings ?")
 
149
        #try:
 
150
        #    self.directory = ldap.open(self.server)
 
151
        #    self.directory.simple_bind_s(self.admindn, self.password)
 
152
        #status=self.open()
 
153
        #if status==1:
 
154
        #    self.notify+=self.web.successbox("Successsfully binded to ldap server")
 
155
        #else:
 
156
        #    self.notify+= "Couldn't Connect. %s " % self.directory
 
157
            
 
158
        htmout+=self.toolbar()
 
159
        if self.web_page["subpage"]=="":
 
160
            #htmout+=self.toolbar()
 
161
            
 
162
            if self.web_page["action"]=="restart":
 
163
                htmout+=self.page_restart()
 
164
                
 
165
            if self.web_page["action"]=="newgroup":
 
166
                #self.tagset2["config"]["iface"]["options"]=self.get_server_interfaces()
 
167
                result,htm=self.config.handle_form(self.file_data+"mediatomb.xml")
 
168
                htmout+=htm
 
169
                #htmout+=self.newgroup(form)
 
170
 
 
171
        if self.web_page["subpage"]=="groups":
 
172
            if self.web_page["action"]=="form":
 
173
                #self.tagset2["config"]["iface"]["options"]=self.get_server_interfaces()
 
174
 
 
175
                htmout+=self.create_group(self.web_page["id"])
 
176
                #htmout+=self.newgroup(form)
 
177
            
 
178
            htmout+=self.show_groups()
 
179
 
 
180
        if self.web_page["subpage"]=="users":
 
181
            htmout+="users"
 
182
            if self.web_page["action"]=="form":
 
183
                #self.tagset2["config"]["iface"]["options"]=self.get_server_interfaces()
 
184
                self.ldap_user_ts["config"]["group"]["options"]=self.list_groups()
 
185
                result,htm=self.config.handle_form(self.ldap_user_ts)
 
186
                htmout+="test"
 
187
                htmout+=str(self.config.results)
 
188
                htmout+=htm
 
189
                if result:
 
190
                    self.create_user()
 
191
                #htmout+=self.newgroup(form)
 
192
            htmout+=self.show_users()
 
193
 
 
194
        if not self.check_process("slapd"):
 
195
            self.notify+=self.web.notifybox("The slapd directopry Service is not running, try hitting restart icon above.")
 
196
        
 
197
        if self.directory is not None:
 
198
            self.directory=None
 
199
            
 
200
        #if status==1:
 
201
        #    self.close()
 
202
        return self.web.notify(self.notify)+htmout
 
203
    
 
204
    def create_group(self,id=""):
 
205
        id=id.replace("%3D","=")
 
206
        if id!="":
 
207
            print "got dn id what is it ?"
 
208
            print id
 
209
            
 
210
            for record in self.finddn(id,("ou","description",)):
 
211
                self.ldap_group_ts["config"]["name"]["values"]=record[1]["ou"][0]
 
212
                self.ldap_group_ts["config"]["description"]["values"]=record[1]["description"][0]
 
213
                print "get data from ldap"
 
214
                print record   
 
215
            
 
216
            
 
217
        result,htm=self.config.handle_form(self.ldap_group_ts)
 
218
        htmout="test"
 
219
        htmout+=str(self.config.results)
 
220
        htmout+=htm
 
221
        if result:
 
222
            htmout=""
 
223
            dn="ou="+self.config.results["name"][1]+",ou=groups,"+self.rootdn
 
224
            newrecord=self.newldif.group_list(self.config.results["name"][1],self.config.results["description"][1])
 
225
            ldif = modlist.addModlist(newrecord)
 
226
            htmout=self.add_ldif(dn,ldif)
 
227
        return htmout
 
228
    
 
229
    def create_user(self):
 
230
        htmout=""
 
231
        dn="cn="+self.config.results["fname"][1]+" "+self.config.results["sname"][1]+",ou=groups,"+self.rootdn
 
232
        newrecord=self.newldif.user_list(self.config.results["uid"][1],self.config.results["fname"][1],self.config.results["sname"][1],self.config.results["password"][1])
 
233
        
 
234
        print "--------------------------------"
 
235
        print dn
 
236
        print newrecord
 
237
        ldif = modlist.addModlist(newrecord)
 
238
        htmout+=self.add_ldif(dn,ldif)
 
239
        #if self.open():
 
240
        #    print "connection opened"
 
241
        #    ldif = modlist.addModlist(newrecord)
 
242
        #    #htmout+=str(newrecord)
 
243
        #    htmout+=str(ldif)
 
244
        #    try:
 
245
        #        self.directory.add_s(dn,ldif)
 
246
        #    except ldap.LDAPError, error_message:
 
247
        #        if error_message[0]["desc"]=="Already exists":
 
248
        #            htmout+=self.web.notifybox(_("Group Already Exists"))
 
249
        #        htmout+=str(error_message)
 
250
        #    self.close()
 
251
        return htmout
 
252
    
 
253
    def search(self,filter,attributes):
 
254
        htmout=""
 
255
        dn="ou=groups,"+self.rootdn
 
256
        print dn
 
257
        if self.open():
 
258
            
 
259
            print "connection opened"
 
260
            #htmout+=str(ldif)
 
261
            try:
 
262
                ldap_result_id=self.directory.search(dn,ldap.SCOPE_SUBTREE,filter,attributes)
 
263
                #print self.directory.result(ldap_result_id, 0)
 
264
                #result_type, result_data = self.directory.result(ldap_result_id, 15)
 
265
                #print result_data
 
266
                result_type, result_data = self.directory.result(ldap_result_id, 5)
 
267
                print result_data
 
268
                for result in result_data:
 
269
                    print "result data"
 
270
                    htmout+=self.web.boxrow(str(result))
 
271
                
 
272
            except ldap.LDAPError, error_message:
 
273
                if error_message[0]["desc"]=="Already exists":
 
274
                    htmout+=self.web.notifybox(_("Group Already Exists"))
 
275
                htmout+=str(error_message)
 
276
            self.close()
 
277
        return htmout
 
278
    
 
279
    def finddn(self,filter,attributes):
 
280
        htmout=""
 
281
        dn="ou=groups,"+self.rootdn
 
282
        print dn
 
283
        if self.open():
 
284
            
 
285
            print "connection opened"
 
286
            #htmout+=str(ldif)
 
287
            try:
 
288
                print self.rootdn
 
289
                ldap_result_id=self.directory.search(filter,ldap.SCOPE_BASE,"objectclass=*",attributes)
 
290
                #print self.directory.result(ldap_result_id, 0)
 
291
                print ldap_result_id
 
292
                result_type, result_data = self.directory.result(ldap_result_id)
 
293
                print result_type
 
294
                print result_data
 
295
                for result in result_data:
 
296
                    print "result data"
 
297
                    print result
 
298
                    yield result
 
299
                
 
300
            except ldap.LDAPError, error_message:
 
301
                if error_message[0]["desc"]=="Already exists":
 
302
                    htmout+=self.web.notifybox(_("Group Already Exists"))
 
303
                htmout+=str(error_message)
 
304
            self.close()
 
305
    
 
306
    def find(self,filter,attributes):
 
307
        htmout=""
 
308
        dn="ou=groups,"+self.rootdn
 
309
        print dn
 
310
        if self.open():
 
311
            
 
312
            print "connection opened"
 
313
            #htmout+=str(ldif)
 
314
            try:
 
315
                ldap_result_id=self.directory.search(dn,ldap.SCOPE_SUBTREE,filter,attributes)
 
316
                #print self.directory.result(ldap_result_id, 0)
 
317
                #result_type, result_data = self.directory.result(ldap_result_id, 15)
 
318
                #print result_data
 
319
                result_type, result_data = self.directory.result(ldap_result_id, 5)
 
320
                print result_data
 
321
                for result in result_data:
 
322
                    yield result
 
323
                    print "result data"
 
324
                    print result
 
325
                
 
326
            except ldap.LDAPError, error_message:
 
327
                if error_message[0]["desc"]=="Already exists":
 
328
                    htmout+=self.web.notifybox(_("Group Already Exists"))
 
329
                htmout+=str(error_message)
 
330
            self.close()
 
331
        #return htmout
 
332
    
 
333
    def add_ldif(self,dn,ldif):
 
334
        htmout=""
 
335
        if self.open():
 
336
            print "connection opened"
 
337
            htmout+=str(ldif)
 
338
            try:
 
339
                print self.directory.add_s(dn,ldif)
 
340
            except ldap.LDAPError, error_message:
 
341
                print ldap.LDAPError
 
342
                print error_message
 
343
                if error_message[0]["desc"]=="Already exists":
 
344
                    htmout+=self.web.notifybox(_("Group Already Exists"))
 
345
                htmout+=str(error_message)
 
346
            self.close()
 
347
        return htmout
 
348
    
 
349
    def list_groups(self):
 
350
        list=""
 
351
        for r in self.find("ou=*",("ou","uniqueid",)):
 
352
            print r
 
353
            list+=r[0]+"::"+r[1]["ou"][0]+"::::"
 
354
        return list.rstrip("::::")
 
355
            
 
356
            
 
357
            
 
358
    def show_groups(self):
 
359
        htmout=self.web.container_start()
 
360
        htmout+=self.search("ou=*",("ou",))
 
361
        htmout+=self.web.container_end()
 
362
        htmout=self.web.container_start()
 
363
        for r in self.find("ou=*",("ou","description",)):
 
364
            text=str(r)
 
365
            
 
366
            text=r[1]["ou"][0]+"<br />"
 
367
            if "description" in r[1].keys():
 
368
                text+=r[1]["description"][0]
 
369
            button="<a href=\"index.py?Page="+self.web_page["page"]+"&amp;subpage=groups&amp;action=form&amp;id="+ r[0].replace("=","%3D") +"\"><img alt=\"Edit\" src=\"/images/icons/silk/icons/table_edit.png\" />Modify Description</a><br />"
 
370
            button+=self.web.but_img_confirm("index.py?Page="+self.web_page["page"]+"&amp;subpage=groups&amp;action=del&amp;id="+ r[0].replace("=","%3D") ,"/images/icons/silk/icons/delete.png","Are You Sure you wish to delete this entry ?","Del")
 
371
            htmout+=self.web.boxrow(text,right=button)
 
372
        
 
373
        htmout+=self.web.container_end()
 
374
        return htmout
 
375
    
 
376
    def show_users(self):
 
377
        htmout=self.web.container_start()
 
378
        htmout+=self.search("cn=*",("cn",))
 
379
        
 
380
        htmout+=self.web.container_end()
 
381
        
 
382
        
 
383
        htmout=self.web.container_start()
 
384
        for r in self.find("cn=*",("cn","uid",)):
 
385
            text=r[1]["cn"][0]+"<br />"+r[1]["uid"][0]
 
386
            button="<a href=\"index.py?Page="+self.web_page["page"]+"&amp;subpage=groups&amp;action=form&amp;id="+ r[0] +"\"><img alt=\"Edit\" src=\"/images/icons/silk/icons/table_edit.png\" />Modify Description</a><br />"
 
387
            button+=self.web.but_img_confirm("index.py?Page="+self.web_page["page"]+"&amp;subpage=groups&amp;action=del&amp;id="+ r[0] ,"/images/icons/silk/icons/delete.png","Are You Sure you wish to delete this entry ?","Del")
 
388
            htmout+=self.web.boxrow(text,right=button)
 
389
        
 
390
        htmout+=self.web.container_end()
 
391
        return htmout
 
392
    
 
393
    def newgroup(self,form):
 
394
        v=form_validate(self.web_page,self.ldap_group_ts,self.web)
 
395
        result,htm=v.parse_form(form,self.ldap_group_ts)
 
396
        htmout=htm
 
397
        if result==1:
 
398
            
 
399
        
 
400
            #def group(self,name,desc="New Organizational unit / group"):
 
401
            newrecord={}
 
402
            dn="ou="+v.results["name"]+","+self.rootdn
 
403
            #newrecord['dn']="ou="+v.results["name"]+","+self.rootdn
 
404
            newrecord['ou']=v.results["name"]
 
405
            newrecord['objectclass']=['top','organizationalUnit']
 
406
            newrecord['description']="test description"#v.results["description"]
 
407
            
 
408
            if self.open():
 
409
                ldif = modlist.addModlist(newrecord)
 
410
                #htmout+=str(newrecord)
 
411
                htmout+=str(ldif)
 
412
                try:
 
413
                    self.directory.add_s(dn,ldif)
 
414
                except ldap.LDAPError, error_message:
 
415
                    if error_message[0]["desc"]=="Already exists":
 
416
                        htmout+=self.web.notifybox(_("Group Already Exists"))
 
417
                    htmout+=str(error_message)
 
418
                self.close()
 
419
 
 
420
        
 
421
        #ldif="dn: ou="+name+",o=domain-name\n"
 
422
        #ldif+="ou: "+name+"\n"
 
423
        #ldif+="objectClass: top\n"
 
424
        #ldif+="objectClass: organizationalUnit\n"
 
425
        #ldif+="description: "+desc+"\n"
 
426
        return htmout
 
427
    
 
428
    def addldif(self,newldif):
 
429
        htmout=""
 
430
        if self.open():
 
431
            ldif = modlist.addModlist(newldif)
 
432
            #htmout+=str(newrecord)
 
433
            htmout+=str(ldif)
 
434
            try:
 
435
                self.directory.add_s(dn,ldif)
 
436
            except ldap.LDAPError, error_message:
 
437
                if error_message[0]["desc"]=="Already exists":
 
438
                    htmout+=self.web.notifybox(_("Group Already Exists"))
 
439
                htmout+=str(error_message)
 
440
            self.close()
 
441
        return htmout
 
442
        
 
443
    def get_basedn_old(self):
 
444
        try:
 
445
            path=(self.server_tagset["tagdepth"][0],self.hostname),
 
446
            conf=self.server_config.get_config_dict(path,self.server_tagset)
 
447
            domain=""
 
448
            for dc in conf["domain"].split("."):
 
449
                domain+="dc="+dc+","
 
450
            domain=domain.rstrip(",")
 
451
        except:
 
452
            domain=""
 
453
        return domain
 
454
 
 
455
    def open(self):
 
456
        try:
 
457
            print "ldap connect "
 
458
            print self.server
 
459
            print self.admindn
 
460
            self.directory = ldap.open(self.server)
 
461
            print "Connecting with %s and %s" % (self.admindn, self.password)
 
462
            self.directory.simple_bind_s(self.admindn, self.password)
 
463
            return 1
 
464
        except ldap.LDAPError, error_message:
 
465
            
 
466
            self.directory=error_message
 
467
            return 0
 
468
            
 
469
    def close(self):
 
470
        self.directory.unbind_s()
 
471
 
 
472
    def toolbar(self):
 
473
        tbar=self.web.tool_icon("index.py?Page="+self.web_page["page"]+"&amp;SubPage=help","images/icons/tango/32x32/apps/help-browser.png","<b>Action: Help</b><br><i>Description: Help Documentation for this plugin module.</i>")
 
474
        #tbar+=self.web.tool_icon("index.py?Page="+self.web_page["page"]+"&amp;SubPage=form_share","images/icons/tango/32x32/places/folder-remote.png","<b>Action: New Share</b><br><i>Description: Add New Shared Folder</i>")
 
475
        tbar+=self.web.tool_icon("index.py?Page="+self.web_page["page"]+"&amp;SubPage=log","images/icons/tango/32x32/categories/preferences-desktop.png","<b>Action: Log files</b><br><i>Description: View your samba log files.</i>")
 
476
        tbar+=self.web.tool_icon("index.py?Page="+self.web_page["page"]+"&amp;subpage=groups&amp;action=form","images/icons/tango/32x32/apps/preferences-desktop-theme.png","<b>Action: Add New Group</b><br><i>Description: Add a new group to the directory.</i>")
 
477
        tbar+=self.web.tool_icon("index.py?Page="+self.web_page["page"]+"&amp;subpage=users&amp;action=form","images/icons/tango/32x32/apps/system-users.png","<b>Action: Add New User</b><br><i>Description: Create a new account for a person.</i>")
 
478
        tbar+=self.web.tool_icon("index.py?Page="+self.web_page["page"]+"&amp;action=restart","images/icons/tango/32x32/actions/view-refresh.png","<b>Action: Restart</b><br><i>Description: Restart samba service</i>")
 
479
        return self.web.tool_bar("Shared Network Folders Configuration",tbar)
 
480
        
 
481
    def page_default(self):#if any zones exist display them in a table
 
482
        htmout=self.toolbar()
 
483
        style=""
 
484
        #htmout+=self.web.table_start("Shared Network Folders",("N","Folder","Path","Comments","Modify"))
 
485
        #count = 1
 
486
        #for share in self.config.get_list(self.tagset3):
 
487
        #    path=(self.tagset3["tagdepth"][0],share),
 
488
        #    conf=self.config.get_config_dict(path,self.tagset3)
 
489
        #    style=self.web.switch_style(style,"tabrow1","tabrow2")
 
490
        #    htmout+="<tr class=\""+style+"\"><td >" + str(count) + "</td>"
 
491
        #    htmout+="<td><a href=\"index.py?Page=Shares&amp;SubPage=form_share&amp;name="+ str(share) +"\">" + str(share) + "</a></td>"
 
492
        #    htmout+="<td>" + conf["path"] + "</td>"
 
493
        #    htmout+="<td>" + conf["comments"] + "</td>"
 
494
        #    htmout+="<td>"
 
495
        #    htmout+=self.web.but_img("index.py?Page="+self.web_page["page"]+"&amp;SubPage=form_share&amp;name="+ str(share),"/images/icons/silk/icons/table_edit.png","Edit")
 
496
        #    htmou:t+=self.web.but_img_confirm("index.py?Page="+self.web_page["page"]+"&amp;SubPage=del_share&amp;name="+ share,"/images/icons/silk/icons/delete.png","Are You Sure you wish to remove this share, this will not remove your files and will only disable the sharing of this folder ?","Del")
 
497
        #    htmout+="</td></tr>"
 
498
        #    count+=1
 
499
        #htmout+=self.web.table_end()+"<br />"
 
500
        return htmout
 
501
 
 
502
    def page_broadcast(self):
 
503
        xml="<service name=\"shares\">"
 
504
        xml+="<win exe=\"explorer.exe\">\\\\</win>"
 
505
        xml+="<gnome exe=\"nautilus\">smb://</gnome>"
 
506
        xml+="<kde></kde>"
 
507
        self.load_config_file(self.profilepath,"customshares.xml","shares")
 
508
        items = self.doc.getSubTagAttrib(self.tagset3["tagdepth"][0])
 
509
        for item  in items:
 
510
            xml+="<item name=\""+item+"\">"+item+"</item>"
 
511
        xml+="</service>"
 
512
        return xmldd
 
513
    
 
514
    def get_password(self):
 
515
        fp = open(self.path+"security/users/.usmpasswd")
 
516
        password=""
 
517
        for line in fp.readlines():
 
518
            sp=line.split(":")
 
519
            if sp[0]=="usmadmin":
 
520
                password=sp[2]
 
521
        fp.close()
 
522
        return password
 
523
    
 
524
    def setup_test(self):
 
525
        self.open()
 
526
        #ldap.SCOPE_ONELEVEL
 
527
        #scope=ldap.SCOPE_SUBTREE
 
528
        r=0
 
529
        try:
 
530
            result_id = self.directory.search(self.rootdn,ldap.SCOPE_ONELEVEL,"objectclass=*")
 
531
            rt,rd=self.directory.result(result_id)
 
532
            r=len(rd)
 
533
            self.close()
 
534
        except:pass
 
535
        
 
536
        
 
537
        return str(r)
 
538
 
 
539
    def inital_ldap(self):
 
540
        #add root dn object for our domain
 
541
        
 
542
        print "inital_ldap"
 
543
        print self.rootdn
 
544
        dc=self.rootdn.split(",")[0]
 
545
        print dc
 
546
        fpldif="dn: "+self.rootdn+"\n"
 
547
        fpldif+="objectclass: dcObject\n"
 
548
        fpldif+="objectclass: organization\n"
 
549
        fpldif+="o: example\n"
 
550
        fpldif+="dc: "+dc.split("dc=")[1]+"\n\n"
 
551
        
 
552
        #add the admin user
 
553
        fpldif+="dn: cn=admin,"+self.rootdn+"\n"
 
554
        fpldif+="objectclass: organizationalRole\n"
 
555
        fpldif+="cn: admin\n\n"
 
556
        
 
557
        fpldif+="dn: ou=groups,"+self.rootdn+"\n"
 
558
        fpldif+="ou: groups\n"
 
559
        fpldif+="objectclass: top\n"
 
560
        fpldif+="objectclass: organizationalUnit\n"
 
561
        fpldif+="description: group for users\n\n"
 
562
        
 
563
        fpldif+="dn: ou=users,"+self.rootdn+"\n"
 
564
        fpldif+="ou: users\n"
 
565
        fpldif+="objectclass: top\n"
 
566
        fpldif+="objectclass: organizationalUnit\n"
 
567
        fpldif+="description: group for groups\n\n"
 
568
        
 
569
        fpldif+="dn: ou=address book,"+self.rootdn+"\n"
 
570
        fpldif+="ou: address book\n"
 
571
        fpldif+="objectclass: top\n"
 
572
        fpldif+="objectclass: organizationalUnit\n"
 
573
        fpldif+="description: Address Book\n\n"
 
574
        
 
575
        self.save_config(self.configpath+"base.ldif",fpldif)
 
576
        fp=open(self.configpath+"base.ldif")
 
577
        #self.load_ldif(fp)
 
578
        htmout=""
 
579
        for r in from_ldif_file(fp).results:
 
580
            dn,ldif_entry=r
 
581
            htmout+=str(r)+"<br /><br />"
 
582
            htmout+="Inital ldap connection and base ldif import"
 
583
            print "Inital ldap connection and base ldif import"
 
584
            if self.open()==1:
 
585
                print "connected"
 
586
                htmout+="Inital Ldap setup"
 
587
                htmout+=str(ldif_entry)
 
588
                try:
 
589
                    self.directory.add_s(dn,ldif_entry)
 
590
                except ldap.LDAPError, error_message:
 
591
                    #if error_message[0]["desc"]=="Already exists":
 
592
                    #    htmout+=self.web.notifybox(_("Group Already Exists"))
 
593
                    print error_message
 
594
                    htmout+=str(error_message)
 
595
                self.close()
 
596
            else:
 
597
                htmout+=str(self.directory)
 
598
            
 
599
        fp.close()
 
600
        
 
601
        
 
602
        
 
603
        return htmout
 
604
    def slapd_dbconfig(self):
 
605
        slapd_db=[]
 
606
        slapd_db.append("dn: olcDatabase={1}bdb\n")
 
607
        slapd_db.append("objectClass: olcDatabaseConfig\n")
 
608
        slapd_db.append("objectClass: olcBdbConfig\n")
 
609
        slapd_db.append("olcDatabase: {1}bdb\n")
 
610
        slapd_db.append("olcSuffix: %s\n" % self.rootdn)
 
611
        slapd_db.append("olcLastMod: TRUE\n")
 
612
        slapd_db.append("olcMaxDerefDepth: 15\n")
 
613
        slapd_db.append("olcReadOnly: FALSE\n")
 
614
        slapd_db.append("olcRootDN: %s\n" % self.admindn)
 
615
        slapd_db.append("olcRootPW: %s\n" % self.md5password)
 
616
        slapd_db.append("olcMonitoring: FALSE\n")
 
617
        slapd_db.append("olcDbDirectory: /var/lib/ldap\n")
 
618
        slapd_db.append("olcDbCacheSize: 1000\n")
 
619
        slapd_db.append("olcDbConfig: {0}set_cachesize 0 2097152 0\n")
 
620
        slapd_db.append("olcDbConfig: {1}set_lk_max_objects 1500\n")
 
621
        slapd_db.append("olcDbConfig: {2}set_lk_max_locks 1500\n")
 
622
        slapd_db.append("olcDbConfig: {3}set_lk_max_lockers 1500\n")
 
623
        slapd_db.append("olcDbNoSync: FALSE\n")
 
624
        slapd_db.append("olcDbDirtyRead: FALSE\n")
 
625
        slapd_db.append("olcDbIDLcacheSize: 0\n")
 
626
        slapd_db.append("olcDbIndex: objectClass eq\n")
 
627
        slapd_db.append("olcDbLinearIndex: FALSE\n")
 
628
        slapd_db.append("olcDbMode: 384\n")
 
629
        slapd_db.append("olcDbSearchStack: 16\n")
 
630
        slapd_db.append("olcDbShmKey: 0\n")
 
631
        slapd_db.append("olcDbCacheFree: 1\n")
 
632
        slapd_db.append("olcDbDNcacheSize: 0\n")
 
633
        slapd_db.append("structuralObjectClass: olcBdbConfig\n")
 
634
        slapd_db.append("entryUUID: e5e558da-2edc-102d-8361-c3a44ea1fbdf\n")
 
635
        slapd_db.append("creatorsName: cn=config\n")
 
636
        slapd_db.append("createTimestamp: 20081015081331Z\n")
 
637
        slapd_db.append("entryCSN: 20081015081331.155160Z#000000#000#000000\n")
 
638
        slapd_db.append("modifiersName: cn=config\n")
 
639
        slapd_db.append("modifyTimestamp: 20081015081331Z\n")
 
640
 
 
641
        self.save_config(self.configpath+"olcDatabase={1}bdb.ldif","".join(slapd_db))
 
642
        self.run("copy_config|"+self.configpath+"olcDatabase={1}bdb.ldif|/etc/ldap/slapd.d/cn=config/olcDatabase={1}bdb.ldif")
 
643
 
 
644
    def generateconfig(self):#if any zones exist display them in a table
 
645
        #we have to update the ldap apparmor profile to allow creation of multiple databases
 
646
        self.run("apparmor_profile|"+self.pluginpath+"usr.sbin.slapd|usr.sbin.slapd")
 
647
        self.run("slapd_db|/var/lib/ldap/%s" % self.domain)
 
648
        
 
649
        self.slapd_dbconfig()
 
650
        
 
651
        conf="#test file with slaptest "
 
652
        conf+="#######################################################################\n"
 
653
        conf+="# Global Directives:\n"
 
654
 
 
655
        conf+="include         "+self.path_schema+"core.schema\n"#required
 
656
        conf+="include         "+self.path_schema+"cosine.schema\n"#Recomended: Cosine, Internet X.500
 
657
        conf+="include         "+self.path_schema+"inetorgperson.schema\n"#Recomended
 
658
        conf+="include         "+self.path_schema+"nis.schema\n\n"#NIS (Network Information Services), Sun NFS support.
 
659
 
 
660
        conf+="allow bind_v2\n"
 
661
        conf+="pidfile         /var/run/slapd/slapd.pid\n"
 
662
        conf+="argsfile        /var/run/slapd/slapd.args\n"
 
663
        conf+="loglevel        1\n"#none
 
664
        conf+="modulepath      /usr/lib/ldap/\n"
 
665
        conf+="moduleload      back_bdb\n\n"
 
666
        
 
667
        conf+="# Save the time that the entry gets modified, for database #1\n"
 
668
        conf+="lastmod         on\n\n"
 
669
 
 
670
        conf+="# The maximum number of entries that is returned for a search operation\n"
 
671
        conf+="sizelimit 500\n\n"
 
672
 
 
673
        #conf+="access to attrs=userPassword\n"
 
674
        #conf+="\tby dn=\"uid=root,ou=People,"+self.rootdn+"\" write\n"
 
675
        #conf+="\tby dn=\""+self.admindn+"\" write\n"
 
676
        #conf+="\tby anonymous auth\n"
 
677
        #conf+="\tby self write\n"
 
678
        #conf+="\tby * none\n\n"
 
679
 
 
680
        conf+="# The admin dn has full write access, everyone else\n"
 
681
        conf+="# can read everything.\n"
 
682
        conf+="access to dn.base=\"\" by * read\n"
 
683
        conf+="access to *\n"
 
684
        conf+="\tby dn=\""+self.admindn+"\" write\n"
 
685
        conf+="\tby * read\n\n"
 
686
 
 
687
        
 
688
        conf+="database\tbdb\n"
 
689
        #slapd apparmour profile is in /etc/apparmor.d/usr.sbin.slapd
 
690
        #to create folders inside /var/lib/ldap need to modify apparmour as it protects the path
 
691
        conf+="directory       /var/lib/ldap/%s\n" % self.domain
 
692
        #conf+="directory       /var/lib/ldap/\n"
 
693
        conf+="suffix\t\""+self.rootdn+"\"\n"
 
694
        conf+="rootdn\t\""+self.admindn+"\"\n"#for replication
 
695
        conf+="rootpw\t"+self.md5password+"\n\n"#needs {md5} or other type
 
696
 
 
697
        
 
698
        conf+="index   objectClass     eq\n\n"
 
699
        print "regenerate new config"
 
700
        print conf
 
701
        self.save_config(self.configpath+"slapd.conf",conf)
 
702
        self.run("copy_config|"+self.configpath+"slapd.conf|/etc/ldap/slapd.conf")
 
703
        
 
704
        return conf+"\n"
 
705
 
 
706
    
 
707
 
 
708
    def page_restart(self):
 
709
        htm=self.generateconfig()
 
710
        self.run_background("restart|slapd")
 
711
        return htm
 
712
 
 
713
#class get_groups(self):
 
714
#    return ""
 
715
        
 
716
 
 
717
 
 
718
 
 
719
class from_ldif_file(ldif.LDIFParser):
 
720
    results=None
 
721
    def __init__(self,txt):
 
722
        ldif.LDIFParser.__init__(self,txt,None,0,None,"\n")
 
723
        self.results=[]
 
724
        self.parse()
 
725
        
 
726
    def handle(self,dn,entry):
 
727
        print "test"
 
728
        print entry
 
729
        #ldif = modlist.addModlist(newldif)
 
730
        self.results+=(dn,modlist.addModlist(entry)),
 
731