-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcsd_session.py
1331 lines (1070 loc) · 43.9 KB
/
pcsd_session.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of Fork of crcnetd - CRCnet Configuration System Daemon
#
# Copyright (c) 2012 sun-exploit <[email protected]>
#
# Fork of crcnetd is free software: you may copy, redistribute
# 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 file 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, see <http://www.gnu.org/licenses/>.
#
# This file incorporates work covered by the following copyright and
# permission notice:
#
# Copyright (C) 2006 The University of Waikato
#
# This file is part of crcnetd - CRCnet Configuration System Daemon
#
# Provides a generic interface to allow functions to register events and
# callback functions to be executed whenever the event occurs.
#
# Author: Matt Brown <[email protected]>
# Version: $Id$
#
# crcnetd is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# crcnetd 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
# crcnetd; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from pcsd_common import *
from pcsd_log import *
from pcsd_config import config_get, config_getboolean
from pcsd_events import catchEvent
from pcsd_server import pcsd_xmlrpc, exportViaXMLRPC
from version import pcsd_version
import pgdb
#from pyPgSQL import PgSQL
from mx import DateTime
import sys
import time
import crypt
import threading
# A list of tables that are regarded as system tables. These are excluded
# from the standard revision history stuff
SYSTEM_TABLES = [ "sessions", "cookies", "changeset", "first_login" ]
class pcsd_session_error(pcsd_error):
pass
#####################################################################
# Session Class
#####################################################################
class pcsd_session:
"""Wrapper for a session.
Maintains a database connection and tracks changesets/transactions.
"""
# Log database query timings
log_times = False
# Currently active sessions
sessions = {}
timeout = config_get("session", "timeout", DEFAULT_SESSION_TIMEOUT)
cookie_timeout = config_get("session", "cookie_timeout", \
DEFAULT_COOKIE_TIMEOUT)
def __init__(self, login_id, mode, token="", initiated=-1, expires=-1, \
sid=-1):
self.login_id = login_id
self.mode = mode
self.lock = threading.RLock()
self.db = None
# Session Time Records
if initiated == -1:
self.initiated = time.time()
else:
self.initiated = initiated
if expires == -1:
self.expires = time.time() + (self.timeout*60)
else:
self.expires = expires
# Create a session token
if token == "":
self.token = createPassword(8)
else:
self.token = token
# Setup a database connection
log_info("Setup a database connection for %s" % (self.login_id))
self._connect()
# Setup session in the database
if sid==-1:
# Use the admin session to create this sessions record
session = getSession(ADMIN_SESSION_ID)
if session is None:
raise pcsd_session_error("No admin session!")
sql = "INSERT INTO sessions (login_id, mode, token, initiated, " \
"expires) VALUES (%s, %s, %s, %s, %s)"
res = session.execute(sql, (self.login_id, self.mode, \
self.token, time.ctime(self.initiated), \
time.ctime(self.expires)))
sql = "SELECT currval('sessions_sid_seq') as sid"
res = session.query(sql, ())
if res == -1:
raise pcsd_session_error("Unable to allocate session ID")
# Save session id
self.session_id = res[0]["sid"]
else:
self.session_id = sid
#Find my username
if login_id == 'admin':
self.username = 'admin'
else:
sql = "SELECT username, domain FROM logins where login_id=%s"
res = self.query(sql, (self.login_id))[0]
self.username = "%s@%s" % (res['username'], res['domain'])
# No changeset or revision active initially
self.changeset = 0
self.changesetOpen = 0
self.changesetInitiator = ""
self.revision = None
self.cursor = None
self.permCache = {}
self.is_invalid = 0
log_info("Session #%s created for %s. Mode %s." % \
(self.session_id, self.username, self.mode))
def _connect(self):
# Close the existing connection
if self.db is not None:
try:
self.db.close()
except:
log_warn("Could not close existing db connection cleanly " \
"in session #%s" % self.session_id, sys.exc_info())
# Setup a database connection
log_debug("%s::%s pgdb.connect(user=[%s], dbhost=[%s], database=[%s], port=[%s])" % \
(__name__, '_connect', self.duser, self.dhost, self.database, self.dport))
self.db = pgdb.connect(host= self.dhost+':'+self.dport, database=self.database, \
user=self.duser, password=self.dpass)
log_debug("%s::%s database connection set" % (__name__, '_connect'))
def getSessionObject(self):
"""Returns a dictionary containing information about the session"""
if self.is_invalid == 1:
raise pcsd_session_error("Invalid session object!")
dict = {}
# Deprecate this entry
dict["sessionID"] = self.session_id
# In favour of this one
dict["session_id"] = self.session_id
dict["login_id"] = self.login_id
dict["mode"] = self.mode
dict["token"] = self.token
dict["initiated"] = self.initiated
dict["expires"] = self.expires
return dict
def getCookieToken(self):
"""Returns a token that can be used to login via a cookie"""
session = getSessionE(ADMIN_SESSION_ID)
token = createPassword(31)
expires = expires = time.ctime(time.time() + 60*self.cookie_timeout)
sql = "INSERT INTO cookies (login_id, token, expires) VALUES (" \
"%s, %s, %s)"
session.execute(sql, (self.login_id, token, expires))
return token
def begin(self, description="", initiator="", implicit=0):
"""Begins a new changeset"""
# Don't let threads race to start a new changeset
self.lock.acquire()
if self.is_invalid == 1:
self.lock.release()
raise pcsd_session_error("Invalid session object!")
if self.changeset != 0:
self.lock.release()
raise pcsd_session_error("There is already a changeset open!")
# Create a database changeset which is wrapped in a transaction.
# Note PgSQL automatically begins transactions when a cursor is created
# so the check above (that no transaction is in progress) allows us to
# know that we are at the start of a new transaction for the following
# statements
try:
ecur = self.db.cursor()
ecur.execute("INSERT INTO changeset (username, " \
"description, pending) VALUES (%s, %s, %s)", \
(self.username, description, "t"))
except:
if ecur:
ecur.close()
log_error("Could not create changeset", sys.exc_info())
self.lock.release()
raise pcsd_session_error("Could not create changeset!")
# Retrieve changeset id
res = self.query("SELECT currval('changeset_changeset_id_seq')", (), \
ecur)
if len(res) != 1:
# Rollback
ecur.close()
self.db.rollback()
# Raise error
self.lock.release()
raise pcsd_session_error("Could not retrieve changeset id!")
self.changeset = res[0][0]
ecur.close()
# Initialise a revision of the configuration files
if not implicit:
try:
from pcsd_cfengine import pcs_revision
self.revision = pcs_revision(self, self.changeset)
except:
log_error("Could not generate revision for " \
"changeset!", sys.exc_info())
self.revision = None
else:
self.revision = None
# Mark changeset as open for queries
self.changesetOpen = 1
self.changesetInitiator = initiator
self.lock.release()
return self.changeset
def commit(self, description=""):
"""Commits the changeset with the specified description appended.
If append is 0 the description overwrites the current value.
"""
# Don't let threads race to close a changeset
self.lock.acquire()
if self.is_invalid == 1:
self.lock.release()
raise pcsd_session_error("Invalid session object!")
# Mark the changeset as finished (no further changes accepted)
self.changesetOpen = 0
# Update changeset description
desc = self.getChangesetDescription()
if description != "":
desc = "%s\n%s" % (desc, description)
# Checkin the revision and get revision number
if self.revision != None:
rev = self.revision.checkin("Automatic revision\n\n%s" % desc)
else:
rev = -1
# Commit the changeset
params = {}
sql = "UPDATE changeset SET description=%(d)s,"
if rev != -1:
sql = "%s svn_revision=%%(r)s," % sql
params["r"] = rev
sql = "%s pending='f' WHERE changeset_id=%%(c)s" % sql
params["d"] = desc.strip()
params["c"] = self.changeset
# Execute the query
try:
cur = self.db.cursor()
cur.execute(sql, params)
cur.close()
self.db.commit()
except:
log_error("Could not commit changeset!", sys.exc_info())
# XXX: Rollback revision here
self.lock.release()
raise pcsd_session_error("Could not commit changeset!")
t = self.changeset
self.changeset = 0
self.changesetOpen = 0
self.changesetInitiator = ""
# XXX: Check changeset was successfully committed
self.revision = None
self.lock.release()
return {"changeset":t,"revision":rev}
def getChangesetDescription(self):
"""Returns the current changeset description"""
if self.changeset == 0:
return ""
res = self.query("SELECT description FROM changeset WHERE " \
"changeset_id=%s", (self.changeset))
return res[0][0]
def rollback(self):
"""Rollsback the current changeset"""
# Don't let threads race to rollback a changeset
self.lock.acquire()
if self.is_invalid == 1:
self.lock.release()
raise pcsd_session_error("Invalid session object!")
if self.changeset == 0:
self.lock.release()
return
# Cancel the database changeset
self.db.rollback()
self.changeset = 0
self.changesetOpen = 0
self.changesetInitiator = ""
# Cancel the revision
self.revision = None
self.lock.release()
return 0
def getCountOf(self, sql, params):
res = self.query(sql, params)
if res == -1:
return 0
if len(res) != 1:
return 0
return res[0][0]
def query(self, sql, params, icursor=None):
# Only one DB query per session at a time please!
self.lock.acquire()
# Return now for queries that are not select
if not sql.lower().startswith("select"):
self.lock.release()
raise pcsd_session_error("Queries must begin with select!")
if self.log_times:
stime = time.time()
# Get a cursor to use
try:
cursor = icursor is None and self.db.cursor() or icursor
except:
log_error("Unable to obtain cursor for query!", sys.exc_info())
# This pretty much means our db connection is hosed, reconnect
self._connect()
self.lock.release()
raise pcsd_session_error("Unable to obtain cursor for query!")
# Run the query
try:
res = cursor.execute(sql, params)
except:
(et, value, bt) = sys.exc_info()
errStr = "%s - Failed Query: %s (%s)" % (value, sql, params)
log_error(errStr, (et, value, bt))
if icursor is None:
cursor.close()
# If the error seems to be connection related, reconnect
if isinstance(value, str) and value.startswith("no connection"):
self._connect()
self.lock.release()
raise pcsd_session_error(errStr)
if self.log_times:
mtime = time.time()
# Fetch the data
d = cursor.description
rows = cursor.fetchall()
if self.log_times:
mtime2 = time.time()
r = 0
rows2 = []
for row in rows:
rd = {}
i=0
for v in row:
if v == None:
rd[d[i][0]] = ""
rd[i] = ""
elif type(v) == type(DateTime.DateTime(1)):
rd[d[i][0]] = str(v)
rd[i] = str(v)
else:
rd[d[i][0]] = v
rd[i] = v
i+=1
rows2.append(rd)
r+= 1
# Close the cursor we opened
if icursor is None:
cursor.close()
if self.log_times:
etime = time.time()
log_debug("Queried database in %0.3f/%0.3f/%0.3f seconds\n %s" % \
((mtime-stime), (mtime2-stime), (etime-stime), sql))
self.lock.release()
return rows2
def execute(self, sql, params):
# Only one DB query per session at a time please!
self.lock.acquire()
if self.changeset > 0 and self.changesetOpen==0:
self.lock.release()
raise pcsd_session_error("Session is currently completing a " \
"changeset. No modifications to database allowed!")
# Return now for queries that are a select
if sql.lower().startswith("select"):
self.lock.release()
return pcsd_session_error("Select queries cannot be executed. " \
"Use query instead.")
if self.log_times:
stime = time.time()
table = "unknown"
try:
if sql.startswith("UPDATE"):
table = sql.split(" ")[1]
elif sql.startswith("INSERT INTO") or \
sql.startswith("DELETE FROM"):
table = sql.split(" ")[2]
except: pass
# Don't require a changeset for updates to "system" tables
require_changeset = True
if table in SYSTEM_TABLES:
require_changeset = False
# Handle implicit transactions
commit = 0
if self.changeset == 0 and require_changeset:
self.begin(implicit=1, initiator="pcsd_session.execute" , \
description="Autogenerated changeset on table %s" % \
table)
commit = 1
# Obtain a cursor
try:
cursor = self.db.cursor()
except:
# This pretty much means our db connection is hosed, reconnect
self._connect()
log_error("Could not obtain cursor for execute", sys.exc_info())
self.lock.release()
raise pcsd_session_error("Could not obtain cursor")
# Execute the query
try:
res = cursor.execute(sql, params)
except:
(et, value, bt) = sys.exc_info()
try:
if cursor: cursor.close()
except:
pass
# Always rollback, session is dead anyway!
self.rollback();
errStr = "%s - Failed Execute: %s (%s)" % (value, sql, params)
log_error(errStr, (et, value, bt))
# If the error seems to be connection related, reconnect
if isinstance(value, str) and value.startswith("no connection"):
self._connect()
self.lock.release()
raise pcsd_session_error(errStr)
# Commit an implicit transaction
if commit or (table in SYSTEM_TABLES and self.changeset==0):
self.commit()
cursor.close()
if self.log_times:
etime = time.time()
log_debug("Updated database in %0.3f seconds\n %s" % \
((etime-stime), sql))
self.lock.release()
return 0
def hasPerms(self, mode, group):
"""Checks that the specified session has the appropriate group
memberships"""
# Import here to avoid circular dependencies
from server.modules.pcs_contact import getUserCache, getCustomerCache, getGroupCache
# Check session is of appropriate type
if mode == SESSION_RW:
if self.mode != SESSION_RW:
return SESSION_NONE
# normal users have AUTH_AUTHENTICATED by default
if group == AUTH_AUTHENTICATED:
return mode
# If we have a cached permission record, return that
if group in self.permCache.keys():
return self.permCache[group]
# Check group membership
users = getUserCache(self.session_id)
customers = getCustomerCache(self.session_id)
groups = getGroupCache(self.session_id)
admin_id = users[self.login_id]["admin_id"]
if self._isGroupMember(admin_id, groups[group], groups):
self.permCache[group] = mode
return mode
self.permCache[group] = SESSION_NONE
return SESSION_NONE
def _isGroupMember(self, admin_id, group, groups):
"""Helper function to emulate isGroupMemberU but working from cache"""
if admin_id in group["members"]:
return True
for group_id in group["group_members"]:
rv = self._isGroupMember(admin_id, groups[group_id], groups)
if rv: return True
return False
def getGroupMemberships(self):
"""Returns a list of all groups this session belongs to"""
# Import here to avoid circular dependencies
from server.modules.pcs_contact import getUserCache, getCustomerCache, getGroupCache
# Check group membership
users = getUserCache(self.session_id)
customers = getCustomerCache(self.session_id)
groups = getGroupCache(self.session_id)
if self.login_id in users.keys():
contact_id = users[self.login_id]["admin_id"]
else:
return []
# Cache memberships
res = []
for group in groups.keys():
if self._isGroupMember(contact_id, groups[group], groups):
res.append(group)
return res
def updateExpiry(self):
"""Postpones the sessions expiry by EXPIRY_TIME
This function should be called periodically whenever activity is
detected on the session. By default it is called by is_session_valid
which will be triggered by each incoming authenticated XML-RPC call.
"""
if self.is_invalid == 1:
raise pcsd_session_error("Invalid session object!")
# update expiry time
self.expires = time.time() + (self.timeout*60)
def lifetime(self):
"""Returns the number of seconds before this session expires"""
return self.expires - time.time()
def isExpired(self):
"""Checks if the session has expired
This function should be regularly called for each session.
Returns 1 if the session has expired, 0 otherwise
"""
if self.is_invalid == 1:
return True
# Check that expiry is still in the future
if self.lifetime() > 0:
return False
log_info("Removing expired session %s (%s)" % \
(self.session_id, self.username))
# Session has expired!
if self.changeset > 0:
# Commit the changeset
self.commit("Autocommitted due to session expiry")
# Shutdown
self.shutdown()
return True
def close(self, persist=False):
"""Nicely closes the session and commits all pending changes.
If persist is set to True, the session entry remains in the database.
"""
# Don't let threads race to close a session
self.lock.acquire()
if self.is_invalid==1:
self.lock.release()
return
if self.changeset != 0:
self.commit("Autocommited due to session close")
# Shutdown will do the rest
self.shutdown(persist)
self.lock.release()
def shutdown(self, persist=False):
"""Forcibly closes the session.
Any pending changesets will be rolled back.
"""
# Don't let threads race to close a session
self.lock.acquire()
if self.is_invalid==1:
self.lock.release()
return
# Rollback
if self.changeset != 0:
self.rollback()
# Mark session as expired
self.is_invalid = 1
# Remove session record
if not persist:
sql = "DELETE FROM sessions WHERE sid=%s"
session = getSessionE(ADMIN_SESSION_ID)
session.execute(sql, (self.session_id))
elif self.session_id != ADMIN_SESSION_ID:
# Update expiry time
sql = "UPDATE sessions SET expires=%s WHERE sid=%s"
session = getSessionE(ADMIN_SESSION_ID)
session.execute(sql, (time.ctime(self.expires), self.session_id))
# Close Database
self.db.close()
del self.sessions[self.session_id]
log_info("Session #%s (%s) shutdown." % \
(self.session_id, self.username))
self.lock.release()
class pcsd_basic_session(pcsd_session):
"""Basic session used for certificate logons that have no user account
This session allows the calling party to access any functions that require
only AUTH_AUTHENTICATED access.
The session is not stored in the database and ends as soon as the reply
is sent.
"""
def __init__(self, login_id, mode):
self.username = "username"
self.login_id = login_id
self.mode = mode
self.lock = threading.RLock()
self.db = None
# Session Time Records
self.initiated = time.time()
self.expires = time.time() + 60
self.token = ""
# Setup a database connection
self._connect()
# Steal a session ID from the sequence, but don't put the session
# into the DB
session = getSession(ADMIN_SESSION_ID)
if session is None:
raise pcsd_session_error("No admin session!")
sql = "SELECT nextval('sessions_sid_seq') as sid"
res = session.query(sql, ())
if res == -1:
raise pcsd_session_error("Unable to allocate session ID")
self.session_id = res[0]["sid"]
# No changeset or revision active initially
self.changeset = 0
self.changesetOpen = 0
self.changesetInitiator = ""
self.revision = None
self.cursor = None
self.is_invalid = 0
log_info("Basic Session #%s created for %s. Mode %s." % \
(self.session_id, self.username, self.mode))
def getSessionObject(self):
if self.is_invalid == 1:
raise pcsd_session_error("Invalid session object!")
dict = pcsd_session.getSessionObject(self)
dict["basicSession"] = True
return dict
def getCookieToken(self):
raise pcsd_session_error("Basic sessions do not support cookie tokens")
def hasPerms(self, mode, group):
"""Checks that the specified session has the appropriate group
memberships"""
# Check session is of appropriate type
if mode == SESSION_RW:
if self.mode != SESSION_RW:
return SESSION_NONE
# Check group membership - basic session only belong to
# AUTH_AUTHENTICATED
if group != AUTH_AUTHENTICATED:
return SESSION_NONE
return mode
def updateExpiry(self):
return
def shutdown(self, persist=False):
"""Forcibly closes the session.
Any pending changesets will be rolled back.
"""
# Don't let threads race to close a session
self.lock.acquire()
if self.is_invalid==1:
self.lock.release()
return
# Rollback
if self.changeset != 0:
self.rollback()
# Mark session as expired
self.is_invalid = 1
# Close Database
self.db.close()
del self.sessions[self.session_id]
log_info("Session #%s (%s) shutdown." % \
(self.session_id, self.username))
self.lock.release()
#####################################################################
# Session Helper Functions
#####################################################################
def loadSessions():
"""Loads sessions from the database"""
session = getSessionE(ADMIN_SESSION_ID)
sessions = {}
# Remove expired sessions
sql = "DELETE FROM sessions WHERE expires<NOW()"
session.execute(sql, ())
# Now select remaining sessions
sql = "SELECT sid, login_id, mode, token, " \
"date_part('epoch', initiated) as initiated, " \
"date_part('epoch', expires) as expires FROM sessions"
res = session.query(sql, ())
for row in res:
try:
session = pcsd_session(row["login_id"], \
row["mode"], row["token"], row["initiated"], \
row["expires"], row["sid"])
except pcsd_session_error:
log_error("Could not load session id: %s" % row["sid"], \
sys.exc_info())
obj = session.getSessionObject()
sessions[obj["sessionID"]] = session
return sessions
def getSession(session_id):
"""Returns a session object for the specified session"""
if session_id not in pcsd_session.sessions.keys():
return None
return pcsd_session.sessions[session_id]
def getSessionE(session_id):
"""Returns a session object for the specified session
Identical in every respect to getSession, but raises an exception
if the session is not found.
"""
if session_id not in pcsd_session.sessions.keys():
raise pcsd_session_error("Session(%s)does not exist!" % (session_id))
return pcsd_session.sessions[session_id]
@exportViaXMLRPC(SESSION_NONE, AUTH_NONE)
def isSessionValid(sauth):
"""Checks the state of the specified session.
Parameters sauth Session credentials dictionary
Returns The mode of the session
"""
# Validate parameters
if type(sauth) != type({}):
raise pcsd_session_error("Invalid session dictionary!")
if "session_id" not in sauth.keys():
raise pcsd_session_error("Invalid session dictionary!")
if "login_id" not in sauth.keys():
raise pcsd_session_error("Invalid session dictionary!")
if "token" not in sauth.keys():
raise pcsd_session_error("Invalid session dictionary!")
if sauth["session_id"] <= 0:
raise pcsd_session_error("Invalid session ID")
# Validate the session parameters
if sauth["session_id"] not in pcsd_session.sessions.keys():
raise pcsd_session_error("Session does not exist")
session = pcsd_session.sessions[sauth["session_id"]]
if session.login_id != sauth["login_id"]:
raise pcsd_session_error("Session login_id does not match!")
if session.token != sauth["token"]:
raise pcsd_session_error("Session token is invalid!")
# Update the expiry time
session.updateExpiry()
pcsd_session.sessions[sauth["session_id"]] = session
return True
@exportViaXMLRPC(SESSION_NONE, AUTH_NONE, asynchronous=True)
def getSessionInformation(sauth=None):
"""Retrieves information about the state of the system
If the current session parameters are given the current changeset is
returned if present.
"""
# Retrieve some system information
nsessions = getNoSessions()
if sauth is not None:
try:
session = pcsd_session.sessions[sauth["session_id"]]
except:
session = None
if session is None:
nchangesets = getNoChangesets(-1)
changeset = 0
mode = SESSION_NONE
else:
nchangesets = getNoChangesets(session.session_id)
changeset = session.changeset
mode = session.mode
return (mode, nsessions, nchangesets, changeset)
@catchEvent("maintenance")
def sessionMaintenance(*args, **kwargs):
"""Checks for expired sessions and removes them"""
global sessionLock
# Ensure that only one thread is executing session maintenance at once
if not sessionLock.acquire(False):
log_warn("A previous session maintenance task is still in progress!")
return
try:
ct = threading.currentThread()
ct.setName("sessionMaintenance")
start = time.time()
asession = getSessionE(ADMIN_SESSION_ID)
# Loop through the list of sessions and remove expired ones
for sessionID,session in pcsd_session.sessions.items():
# Never expire the admin session
if sessionID==ADMIN_SESSION_ID: continue
if session.isExpired(): continue
# Not expired, update db expiry time if we're expiring "soon"
if session.lifetime() < MAINT_INTERVAL * 3:
sql = "UPDATE sessions SET expires=%s WHERE sid=%s"
res = asession.execute(sql, \
(time.ctime(session.expires), session.session_id))
# Also delete expired login cookies
session = getSessionE(ADMIN_SESSION_ID)
sql = "DELETE FROM cookies WHERE expires<NOW()"
res = session.execute(sql, ())
if pcsd_xmlrpc.log_times:
log_debug("Session maintenance completed successfully in %.3f " \
"seconds" % (time.time()-start))
except:
log_error("Failed to complete session maintenace", sys.exc_info())
sessionLock.release()
@catchEvent("shutdown")
def shutdownSessions(*args, **kwargs):
"""Closes all sessions as the program shuts down.
Sessions are left in the database so they can be reloaded when the
program restarts.
"""
for sessionID,session in pcsd_session.sessions.items():
if sessionID == ADMIN_SESSION_ID: continue
session.close(persist=True)
session = getSessionE(ADMIN_SESSION_ID)
session.close(persist=True)
def _validateCustomerPassword(username, password):
"""Helper routine for login. Validates the users password"""
from server.modules.pcs_contact import getCustomerCache
users = getCustomerCache(ADMIN_SESSION_ID)
if username not in users.keys():
log_warn("No user %s" % username)
return FALSE
# Get password
passwd = users[username]["passwd"]
if len(passwd) <= 0:
log_warn("No password set for %s" % username)
return FALSE
# Check password
if crypt.crypt(password, passwd) != passwd:
log_info("Password check failed for %s" % username)
return FALSE
return users[username]['login_id']
def _validatePassword(username, password):
"""Helper routine for login. Validates the users password"""
from server.modules.pcs_contact import getUserCache
session = getSessionE(ADMIN_SESSION_ID)
users = getUserCache(ADMIN_SESSION_ID)
# Check if user exists
if username not in users.keys():
log_warn("No user %s" % username)