-
Notifications
You must be signed in to change notification settings - Fork 0
/
lscriptfilr.py
365 lines (317 loc) · 8.92 KB
/
lscriptfilr.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import filr
import ldap
import os
import sys
import time
from datetime import date
import datetime
import ldif
import ldap.modlist as modlist
from optparse import OptionParser
def ldapcr(srv,user,passw,name,dn,attrs,naming):
print "-"*70
l=ldap.initialize(srv)
try:
l.bind_s(user, passw)
except ldap.INVALID_CREDENTIALS:
print "Your username or password is incorrect."
sys.exit()
except ldap.LDAPError, e:
if type(e.message) == dict and e.message.has_key('desc'):
print e.message['desc']
else:
print e
sys.exit()
try:
print name,dn,naming
value = l.compare_s(dn,naming,name[0])
print value
if value==1:
print "Object Already Exists"
objectpresent="yes"
except ldap.NO_SUCH_OBJECT:
print "Object not Found So create new object"
print "Before Add Record ***"
try:
ld=modlist.addModlist(attrs)
print ld,dn
l.add_s(str(dn),ld)
except:
print "ERROR In Create"
status=0
return(status)
def findos(ldapsrv,base_dn,user,passw):
print ldapsrv
print base_dn
l=ldap.initialize(ldapsrv)
try:
#l.start_tls_s()
l.bind_s(user, passw)
except ldap.INVALID_CREDENTIALS:
print "Your username or password is incorrect."
sys.exit()
except ldap.LDAPError, e:
print e.message['info']
if type(e.message) == dict and e.message.has_key('desc'):
print e.message['desc']
else:
sys.exit()
except ldap.NO_SUCH_OBJECT:
print "Object Not Found"
sys.exit()
attrs = ['o']
filter='(objectclass=Organization)'
grplist=l.search_s( base_dn,ldap.SCOPE_SUBTREE,filter,attrs)
return(grplist)
def findusers(ldapsrv,base_dn,user,passw):
#print ldapsrv
l=ldap.initialize(ldapsrv)
try:
#l.start_tls_s()
l.bind_s(user, passw)
except ldap.INVALID_CREDENTIALS:
print "Your username or password is incorrect."
sys.exit()
except ldap.LDAPError, e:
print e.message['info']
if type(e.message) == dict and e.message.has_key('desc'):
print e.message['desc']
else:
sys.exit()
except ldap.NO_SUCH_OBJECT:
print "Object Not Found"
sys.exit()
#attrs = ['CN','loginTime']
filter='(&(objectclass=inetOrgPerson)(cn=*)(ndsHomeDirectory=*))'
#print filter
#filter='(objectclass=inetOrgPerson)'
attrs = ['cn','ndsHomeDirectory']
grplist=l.search_s( base_dn,ldap.SCOPE_SUBTREE,filter,attrs)
return(grplist)
def findgroups(ldapsrv,base_dn,user,passw):
l=ldap.initialize(ldapsrv)
try:
#l.start_tls_s()
l.bind_s(user, passw)
except ldap.INVALID_CREDENTIALS:
print "Your username or password is incorrect."
sys.exit()
except ldap.LDAPError, e:
print e.message['info']
if type(e.message) == dict and e.message.has_key('desc'):
print e.message['desc']
else:
sys.exit()
except ldap.NO_SUCH_OBJECT:
print "Object Not Found"
sys.exit()
attrs = ['cn','objectClass','ACL']
filter=('objectclass=groupOfNames')
grplist=l.search_s( base_dn,ldap.SCOPE_SUBTREE,filter,attrs)
return(grplist)
def findous(ldapsrv,base_dn,user,passw):
print base_dn
print ldapsrv
print user
print passw
l=ldap.initialize(ldapsrv)
try:
#l.start_tls_s()
l.bind_s(user, passw)
except ldap.INVALID_CREDENTIALS:
print "Your username or password is incorrect."
sys.exit()
except ldap.LDAPError, e:
print e.message
print e.message['info']
if type(e.message) == dict and e.message.has_key('desc'):
print e.message['desc']
else:
sys.exit()
except ldap.NO_SUCH_OBJECT:
print "Object Not Found"
sys.exit()
attrs = ['ou','objectClass','description','loginScript']
filter='(&(objectclass=*)(loginScript=*))'
objlist=l.search_s( base_dn,ldap.SCOPE_SUBTREE,filter,attrs)
return(objlist)
def dotted(dn):
newtemp=[]
dntemp=dn.split(",")
print dntemp
for line in dntemp:
print line[0:3]
print line[0:2]
if line[0:3]=="ou=":
newtemp.append(line.replace("ou=","."))
if line[0:2]=="o=":
newtemp.append(line.replace("o=","."))
print newtemp
newdn="".join(newtemp)
newdn=newdn[1:]
return (newdn)
def makeunc(path):
print path
if "/" in path:
temp=path.split("/")
srv=temp[0]
path=temp[1].split(":")
unc1="\\\\"+srv+"\\"+path[0]+"\\"+path[1]
print unc1
return(unc1)
def groupname(dn):
print "dn:"+dn
temp2=dn.split(".")
cn=temp2[1]
cn=cn.replace("cn=","")
cn=cn.replace("CN=","")
cn=cn.replace(".","")
return(cn)
def rep(oldunc,newunc,script):
cmd="map"
for index,lines in enumerate(script):
if (cmd.lower() in lines) or (cmd.upper() in lines):
if oldunc in lines:
new=lines.replace(oldunc,newunc)
script[index]=new
return(script)
def ldapwrite(srv,user,passw,dn,attr,value):
l=ldap.initialize(srv)
try:
#l.start_tls_s()
l.bind_s(user, passw)
except ldap.INVALID_CREDENTIALS:
print "Your username or password is incorrect."
sys.exit()
except ldap.LDAPError, e:
print e.message['info']
if type(e.message) == dict and e.message.has_key('desc'):
print e.message['desc']
else:
print e
sys.exit()
mod_attrs=[( ldap.MOD_ADD, attr,value)]
try:
status=l.modify_s(dn,mod_attrs)
except ldap.TYPE_OR_VALUE_EXISTS:
print "Value Already set against group "
except ldap.INVALID_DN_SYNTAX:
print "Invalid DN !!"
l.unbind()
# print status
return
def mknf(filrsrv,filrusr,filrpass,lscript,hd,suffix,op):
created=[]
print "Netfolders and Netfolder Servers to be created for server "+filrsrv
relpath=[]
for temp in lscript:
if "map" in temp or "MAP" in temp:
#print "Map line detected"
drive=temp[4:5]
if drive<>"hd":
unc=temp.split("=")
#print unc
if ":" in unc[1]:
unc=unc[1].replace(":","")
#print unc
comp=unc.split("\\")
l1=len(comp)
#print comp
if l1>2:
relpath=comp[3:]
host=comp[0]
vol=comp[1]
name=host+"."+suffix+"-"+vol.upper()
path="\\\\"+host+"."+suffix+"\\"+vol.upper()
nfname=vol+"-"+drive.upper()
#print path
#print len(relpath)
print
print "Creating Netfolder Server.."+name
print "_"*80
print
print "Based on script line "+temp
print "Driver Type \t"+drivertype
print "Server Path \t"+path
print "Proxy User \t"+proxyuser
print "Filr Server \t"+filrsrv
print
print "Creating Netfolder.."
print "Netfolder Name is "+nfname
print
if op.lower()<>"display":
rights=["allusers","group","ACCESS","true","false","true","false"]
print "Filr Netfolders and NetFolder Creation.."
temp=filr.crnfs(name,drivertype,path,authtype,proxyuser,proxypass,filrsrv,0,"admin",s)
created.append(name)
if l1<=2:
time.sleep(pause)
temp=filr.crnf(name,nfname,'\\',filrsrv,rights,"admin",adminpass,0,s)
print temp
else:
print "larger rel path"
else:
print "Only Display Selected so no Netfolders Created"
print
pause=1
os.system("cls")
relpath=[]
parser = OptionParser()
parser.add_option("-o","--operation",help="Display or replace login script")
parser.add_option("-u","--user",help="LDAP User")
parser.add_option("-p","--password",help="LDAP Password")
parser.add_option("-d","--dn",help="BaseDN")
parser.add_option("-s","--oesserver",help="ldap://Server Name:389")
parser.add_option("-f","--filr",help="filr.xxx.com")
parser.add_option("-a","--admin",help="Admin Password")
parser.add_option("-l","--drive",help="exclude homedrive")
parser.add_option("-n","--dnssuffix",help="dns suffix")
(options, args) = parser.parse_args()
required=["operation","user","password","dn","oesserver","filr","admin","drive","dnssuffix"]
for m in required:
if not options.__dict__[m]:
print "mandatory option is missing\n"
parser.print_help()
exit(-1)
print"\n"
print"\n"
files=open("proxy.conf","r").readlines()
#print files
proxyuser=files[0][:-1]
proxypass=files[1][:-1]
proxyuser=proxyuser.replace("user=","")
proxypass=proxypass.replace("password=","")
drivertype="oes"
authtype="nmas"
oessrv=options.oesserver
filrsrv=options.filr
adminpass=options.admin
hdrive=options.drive
user=options.user
pw=options.password
basedn=options.dn
op=options.operation
dns=options.dnssuffix
s=filr.auth("admin",adminpass)
ou=findous(oessrv,basedn,user,pw)
count=0
print "Number of Source Login Scripts Found :"+str(len(ou))
if options.operation.lower()=="display":
print "All operations will not be updated written to the Filr Server and just displayed"
if options.operation.lower()=="create":
print "Warning all drive mappings found will be written to Filr Server "+filrsrv+" !!!"
inp=raw_input("Are You Sure ..(Y/N)?")
if inp.lower()=="n":
sys.exit()
for temp in ou:
out=str(count)+")Login Script = "+str(temp[0])
print "="*len(out)
print out
print "="*len(out)
print "\n"
lscript=temp[1]["loginScript"][0]
if len(lscript)<>0:
lscriptlist=lscript.split("\r\n")
mknf(filrsrv,"admin",adminpass,lscriptlist,hdrive,dns,op)
count=count+1
print "Processing Complete "+str(count)+" Script(s) processed"