-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyADAP.py
More file actions
executable file
·152 lines (122 loc) · 3.94 KB
/
Copy pathpyADAP.py
File metadata and controls
executable file
·152 lines (122 loc) · 3.94 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# pyADAP.py
#
# Copyright 2015 Michael Ghens <mghens@sbcc.edu>
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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 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., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
#~ Python ADAP replacement. This uses Glassfish STOMP for connections
import sys
import logging
import logging.handlers
from stompy.simple import Client
import xmltodict
from pprint import pprint
from person import Person
from adlib import adlib
import base64,zlib
#import netrc,base64,zlib
from secure import stompusername,stomppw,stomphost,stompport,stompclientid
from expiringdict import ExpiringDict
# To prevent beating up on google
cache = ExpiringDict(max_len=100, max_age_seconds=60)
pool = None
def isPersonRecord(imsxml):
if 'person' in imsxml['enterprise']:
return True
else:
return False
def run_stomp():
#initialize
#~ Does connection and loops the stomp connection
stomp = Client(host=stomphost, port=int(stompport))
try:
stomp.connect(username=stompusername,password=stomppw, clientid=stompclientid )
except Exception,e:
print str(e)
sys.exit("Cannot connect to STOMP Server")
stomp.subscribe('/topic/com_sct_ldi_sis_Sync')
#Bad Users
disuserset = set()
disuserset.add('cemitchum')
while True:
try:
message = stomp.get()
imsrecord = xmltodict.parse(message.body)
if isPersonRecord(imsrecord):
try:
imsperson = Person(imsrecord)
except:
logger.info('Person Error: %s', sys.exc_info()[0])
continue
ad = adlib(imsperson)
try:
if imsperson.userid in disuserset:
continue
if ad.inAD():
logger.debug('Changing password for %s', imsperson.userid)
ad.chgPwd()
elif cache.get(imsperson.userid) is None:
logger.debug('Adding user to AD %s', imsperson.userid)
ad.addUser()
#ad.chgPwd()
#ad.enableUser()
#Just need an entry
cache[imsperson.userid] = imsperson.userid
else:
logger.debug('Not adding, in Cache: %s', imsperson.userid)
except Exception, err:
logger.info('Error processing user to AD %s', imsperson.userid)
logger.info('ERROR: %s', str(err))
continue
except KeyboardInterrupt:
print "Shutting Down"
logger.info('Keyboard Interupt: Shutting down pyADAP')
stomp.unsubscribe('/topic/com_sct_ldi_sis_Sync')
stomp.disconnect()
sys.exit(0)
return
def initLogging():
global logger
#Initialize logging
LOG_FILENAME = 'logs/pyADAP.log'
logger = logging.getLogger('pyADAP')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#Log file handler
handler = logging.handlers.RotatingFileHandler(
LOG_FILENAME, maxBytes=1024*1024, backupCount=10)
#Console gets debug
#~ console = logging.StreamHandler()
#~ console.setFormatter(formatter)
# create formatter and add it to the handlers
# create formatter and add it to the handlers
#~ formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
#~ logger.addHandler(console)
def main():
initLogging()
logger.info('Starting pyAD')
run_stomp()
logger.info('Stopping pyAD')
pool.terminate()
return 0
if __name__ == '__main__':
main()