-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrad_auth.py
1435 lines (1147 loc) · 77 KB
/
rad_auth.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
#-*-coding: utf-8 -*-
from __future__ import with_statement
import site
site.addsitedir('/opt/ebs/venv/lib/python2.6/site-packages')
site.addsitedir('/opt/ebs/venv/lib/python2.7/site-packages')
import gc
import sys
sys.path.insert(0, "modules")
sys.path.append("cmodules")
import time
import signal
import packet
import asyncore
import datetime
import traceback
import ConfigParser
import socket
import isdlogger
import saver, utilites
from time import clock
from copy import deepcopy
from threading import Thread, Lock
from collections import deque
import commands
from hashlib import md5
from base64 import b64decode
from classes.rad_auth_cache import *
from classes.rad_class.CardActivateData import CardActivateData
from classes.flags import RadFlags
from classes.vars import RadVars, RadQueues
from utilites import savepid, rempid, get_connection, getpid, check_running, command_string_parser, speed_list_to_dict, create_speed
import Queue
from option_parser import parse
from utilites import in_period_info
import auth
from auth import Auth, get_eap_handlers
from cacherouter import Cache
import cacherouter
globals()['mikrobill.cacherouter'] = cacherouter
w32Import = False
from twisted.internet.protocol import DatagramProtocol
try:
from twisted.internet import epollreactor
epollreactor.install()
except:
print 'No poll(). Using select() instead.'
from twisted.internet import reactor
NAME = 'radius'
DB_NAME = 'db'
SOCKTYPE_AUTH = 12
SOCKTYPE_ACCT = 13
MAX_PACKET_SIZE = 8192
def show_packet(packetobject):
b = ''
#b = ''.join((str(packetobject._DecodeKey(key))+str(packetobject[packetobject._DecodeKey(key)][0])+"\n" for key,item in packetobject.items()))
for key,value in packetobject.items():
b+=str(packetobject._DecodeKey(key))+str(packetobject[packetobject._DecodeKey(key)][0])+"\n"
return b
def authNA(packet):
return packet.ReplyPacket()
def get_accesstype(packetobject, cache):
"""
Returns access type name by which a user connects to the NAS
"""
#print show_packet(packetobject)
try:
nas_type = None
nas_port_type = packetobject.get('NAS-Port-Type', (None,))[0]
nas_identifier = packetobject.get('NAS-Identifier', (None,))[0]
if nas_identifier:
nas_type = cache.get_nas_type_by_id(nas_identifier)
logger.debug('Nas type %s for identify %s', (nas_type, nas_identifier))
if nas_type in ('accel-ipoe', 'lISG', 'accel-ipoe-l3'):
return nas_type
calling_station = packetobject.get('Calling-Station-Id', [''])[0]
logger.info('Nas port type: %s Service Type %s Calling-Station-Id %s', (nas_port_type, packetobject.get('Service-Type', [''])[0], calling_station ))
if nas_port_type == 'Virtual' and packetobject.get('Service-Type', [''])[0]=='Framed-User':
return 'PPTP'
elif (nas_port_type in ['Ethernet', 'Async'] or (len(calling_station)==17 and calling_station.rfind(':')!=-1)) and packetobject.get('Service-Type', [''])[0]=='Framed-User':
return 'PPPOE'
elif nas_port_type == (None,) and packetobject.get('Service-Type', [''])[0]=='Framed-User':
return 'Wireless'
elif nas_port_type == 'Wireless-802.11':
return 'W802.1x'
elif packetobject.has_key('Mikrotik-Host-IP'):
return 'HotSpot'
elif nas_port_type == 'Ethernet' and not packetobject.has_key('Service-Type'):
return 'DHCP'
elif nas_port_type == 'Virtual' and packetobject.get('Service-Type', [''])[0]=='lISG':
#print 'lISG'
return 'lISG'
elif packetobject.get('Acct-Status-Type', [''])[0]=='Accounting-On':
return 'Accounting-On'
else:
logger.warning('Nas access type warning: unknown type: %s', nas_port_type)
logger.warning('Packet: %s', (repr(packetobject)))
except Exception, ex:
logger.error('Packet access type error: %s \n %s', (repr(ex), repr(packetobject)))
return
class Reception_UDP(DatagramProtocol):
'''
Twisted Asynchronous server that recieves datagrams with RAD AUTH packets
and appends them to 'radAuthQueue' queue.
'''
def datagramReceived(self, data, addrport):
global vars
#=======================================================================
# try:
# self.transport.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# except:
# logger.error("Can`t set reuseaddr")
#=======================================================================
if len(data) <= vars.MAX_DATAGRAM_LEN:
qsize = auth_queue.qsize()
if qsize<vars.AUTH_QUEUE_SIZE:
auth_queue.put((data, addrport, self.transport, time.time()))
else:
logger.error("RAD AUTH skip packet due large incomming queue size:%s. Configured limit:%s ", (qsize, vars.AUTH_QUEUE_SIZE))
else:
logger.error("RAD AUTH server exception: packet %s <= %s", (len(data), vars.MAX_DATAGRAM_LEN))
class AuthHandler(Thread):
def __init__(self):
Thread.__init__(self)
self.dbconn = get_connection(vars.db_dsn)
self.dbconn.set_isolation_level(0)
self.dbconn.set_session(autocommit=True)
self.dateCache = datetime.datetime(2000, 1, 1)
self.caches = None
self.cache = Cache(self.dbconn, vars.memcached_host, vars.CRYPT_KEY, logger = logger)
def run(self):
global vars, suicideCondition
while True:
if suicideCondition[self.__class__.__name__]: break
try:
packetobject = None
d = auth_queue.get(block=True, timeout=0.03)
if not d:
time.sleep(0.01)
continue
data,addrport, transport, request_time = d
timeout = time.time()-request_time
if timeout>=vars.AUTH_WAIT_TIMEOUT:
logger.debug("%s: request processing timeout. "
"Configured timeout:%s. Current timeout:%s", (self.getName(),
vars.AUTH_WAIT_TIMEOUT,
timeout))
if data:
packetobject = packet.Packet(dict = vars.DICT, packet = data)
else:
continue
logger.debug('Packet: %s', (repr(packetobject), ))
auth_time = time.time()
returndata = ''
nas_ip = str(packetobject.get('NAS-IP-Address', [''])[0])
if not nas_ip:
nas_ip = addrport[0]
access_type = get_accesstype(packetobject, self.cache)
logger.debug("%s: Access type: %s, packet: %s", (self.getName(), access_type, packetobject.code))
user_name = ''
try:
user_name = str(packetobject['User-Name'][0])
except:
pass
if access_type in ['HotSpot']:
coreconnect = HandleHotSpotAuth(packetobject=packetobject, access_type=access_type, dbCur=self.dbconn.cursor(), transport = transport, addrport = addrport)
coreconnect.nasip = nas_ip
coreconnect.fMem = fMem; coreconnect.caches = self.caches
coreconnect.cache = self.cache
coreconnect.handle()
elif access_type in ['DHCP', 'Wireless'] :
coreconnect = HandleSDHCP(packetobject=packetobject, access_type=access_type, transport = transport, addrport = addrport)
coreconnect.nasip = nas_ip; coreconnect.caches = self.caches
coreconnect.cache = self.cache
coreconnect.handle()
elif access_type in ['lISG', 'accel-ipoe', 'accel-ipoe-l3'] :
coreconnect = HandlelISGAuth(packetobject=packetobject, access_type=access_type, dbconn=self.dbconn, transport = transport, addrport = addrport)
coreconnect.nasip = nas_ip; coreconnect.caches = self.caches
coreconnect.cache = self.cache
coreconnect.handle()
#authobject.ReturnPacket(packetfromcore)
else:
a = time.time()
coreconnect = HandleSAuth(packetobject=packetobject, access_type=access_type, dbconn=self.dbconn, transport = transport, addrport = addrport)
coreconnect.nasip = nas_ip
coreconnect.fMem = fMem; coreconnect.caches = self.caches
coreconnect.cache = self.cache
coreconnect.handle()
#logger.debug("AUTH packet: %s", show_packet(packetfromcore))
self.dbconn.commit()
logger.info("AUTH: %s, USER: %s, NAS: %s, ACCESS TYPE: %s", (time.time()-auth_time, user_name, nas_ip, access_type))
#dbCur.connection.commit()
del coreconnect
except Queue.Empty, ex:
continue
except Exception, ex:
logger.error("%s readfrom exception: %s \n %s", (self.getName(), repr(ex), traceback.format_exc()))
if ex.__class__ in vars.db_errors:
try:
try:
self.dbconn.rollback()
except:
logger.info("%s : transaction tollback error" , (self.getName(), ))
self.dbconn = get_connection(vars.db_dsn)
except Exception, eex:
logger.info("%s : database reconnection error: %s" , (self.getName(), repr(eex)))
time.sleep(1)
class SQLLoggerThread(Thread):
def __init__(self, suicide_condition):
Thread.__init__(self)
self.suicide_condition = suicide_condition
self.dbconn = get_connection(vars.db_dsn)
self.dbconn.set_isolation_level(0)
self.cursor = self.dbconn.cursor()
self.sqllog_deque = deque()
self.sqllog_lock = Lock()
# 'loggerthread initialized'
def add_message(self, account=None, subaccount=None, nas=None, type='', service='', cause='', datetime=None):
self.sqllog_deque.append((account, subaccount, nas, type, service, cause, datetime))
#print 'message added'
def run(self):
a=0
while True:
#print 'log check'
#print "vars.SQLLOG_FLUSH_TIMEOUT", vars.SQLLOG_FLUSH_TIMEOUT
if self.suicide_condition[self.__class__.__name__]:
break
if time.time()-a<vars.SQLLOG_FLUSH_TIMEOUT:
time.sleep(2)
continue
with self.sqllog_lock:
d = []
while len(self.sqllog_deque) > 0:
d.append(self.sqllog_deque.pop())
self.cursor.executemany("""INSERT INTO radius_authlog(account_id, subaccount_id, nas_id, type, service, cause, datetime)
VALUES(%s,%s,%s,%s,%s,%s,%s)""", d)
#print account, subaccount, nas, type, service, cause, datetime
#self.dbconn.commit()
a=time.time()
class HandleSBase(object):
__slots__ = ('packetobject', 'cacheDate', 'nasip', 'caches', 'replypacket', 'userName', 'transport', 'addrport', 'cache', 'authobject')
def auth_NA(self):
"""
Denides access
"""
self.packetobject.username=None
self.packetobject.password=None
self.replypacket = None
self.addrport = None
self.transport = None
# Access denided
self.packetobject.code = packet.AccessReject
self.cache = None
return self.packetobject
def reply(self):
#self.transport.write(self.replypacket.ReplyPacket(), self.addrport)
returndata, replypacket = self.authobject.ReturnPacket(self.replypacket, mppe_support=vars.MPPE_SUPPORT)
logger.debug("REPLY packet: %s", repr(replypacket))
self.transport.write(returndata, self.addrport)
# Main
def handle(self):
pass
class HandleSNA(HandleSBase):
__slots__ = ()
def __init__(self, packetobject):
"""TO-DO: Сделать проверку в методе get_nas_info на тип доступа"""
#self.nasip = str(packetobject['NAS-IP-Address'][0])
self.packetobject = packetobject.CreateReply()
#self.cur = dbCur
def handle(self):
nas = self.cache.get_nas_by_ip(self.nasip)
if nas is not None:
self.packetobject.secret = str(nas.secret)
return "", self.auth_NA()
class HandleSAuth(HandleSBase):
__slots__ = () + ('access_type', 'secret', 'speed','session_speed','nas_id', 'nas_type', 'multilink', 'fMem', 'datetime','dbconn','cursor', 'transport', 'addrport')
def __init__(self, packetobject, access_type, dbconn, transport, addrport):
self.packetobject = packetobject
self.access_type=access_type or 'UNKNOWN'
self.secret = ''
self.session_speed = ''
self.dbconn=dbconn
self.cursor=None
self.transport = transport
self.addrport = addrport
def auth_NA(self, authobject):
"""
Deny access
"""
self.authobject.set_code(3)
returndata, replypacket = self.authobject.ReturnPacket(self.packetobject.CreateReply())
logger.debug("REPLY packet: %s", repr(replypacket))
self.transport.write(returndata, self.addrport)
def add_values(self, tarif_id, nas_id, account_status):
# CREATE ONE SECTION
attrs = self.cache.get_radiusattr_by_tarif_id(tarif_id) or []
account_status = 1 if account_status else 2
for attr in attrs:
if attr.account_status != 0 and attr.account_status!=account_status: continue
if attr.vendor:
self.replypacket.AddAttribute((attr.vendor,attr.attrid), str(attr.value))
else:
self.replypacket.AddAttribute(attr.attrid, str(attr.value))
attrs = self.cache.get_radiusattr_by_nas_id(nas_id) or []
for attr in attrs:
if attr.account_status != 0 and attr.account_status!=account_status: continue
if attr.vendor:
self.replypacket.AddAttribute((attr.vendor,attr.attrid), str(attr.value))
else:
self.replypacket.AddAttribute(attr.attrid, str(attr.value))
def find_free_ip(self,id, acct_interval):
def next(id):
pool= self.cache.get_ippool_by_id(id)
if not pool: return None
return pool.next_pool_id
processed_pools=[]
first = True
while True:
if first==False:
id=next(id)
if not id: return None,None
if id in processed_pools: logger.error("Recursion in ippools was found"); return id,None
else:
first=False
processed_pools.append(id)
self.cursor.execute('SELECT get_free_ip_from_pool(%s, %s);', (id, acct_interval))
framed_ip_address = self.cursor.fetchone()[0]
if framed_ip_address: return id, framed_ip_address
def create_speed(self, nas, subacc_id, tarif_id, account_id, speed=''):
if not (nas.speed_value1 or nas.speed_value2): return
defaults = self.cache.get_defspeed_by_tarif_id(tarif_id)
speeds = self.cache.get_speed_by_tarif_id(tarif_id) or []
correction = self.cache.get_speedlimit_by_account_id(account_id)
logger.info("Account speed status account_id=%s subaccount_id=%s tarif_id=%s speed=%s", (account_id, subacc_id, tarif_id, speed))
now=datetime.datetime.now()
addonservicespeed=[]
br = False
if subacc_id:
accservices = self.cache.get_accountaddonservice_by_subaccount_id(subacc_id) or []
for accservice in accservices:
service = self.cache.get_addonservice_by_id(accservice.service_id)
for pnode in self.cache.get_timeperiodnode_by_timeperiod_id(service.timeperiod_id) or []:
if not accservice.deactivated and service.change_speed and fMem.in_period_(pnode.time_start,pnode.length,pnode.repeat_after, now)[3]:
addonservicespeed = (service.max_tx, service.max_rx, service.burst_tx, service.burst_rx, service.burst_treshold_tx, service.burst_treshold_rx, service.burst_time_tx, service.burst_time_rx, service.min_tx, service.min_rx, service.priority, service.speed_units, service.change_speed_type)
br = True
break
if br: break
br = False
if not addonservicespeed:
accservices = self.cache.get_accountaddonservice_by_account_id(account_id) or []
for accservice in accservices:
service = self.cache.get_addonservice_by_id(accservice.service_id)
for pnode in self.cache.get_timeperiodnode_by_timeperiod_id(service.timeperiod_id) or []:
if not accservice.deactivated and service.change_speed and fMem.in_period_(pnode.time_start,pnode.length,pnode.repeat_after, now)[3]:
addonservicespeed = (service.max_tx, service.max_rx, service.burst_tx, service.burst_rx, service.burst_treshold_tx, service.burst_treshold_rx, service.burst_time_tx, service.burst_time_rx, service.min_tx, service.min_rx, service.priority, service.speed_units, service.change_speed_type)
br = True
break
if br: break
logger.info("Account speed data account_id=%s defaults=%s speeds=%s correction=%s addonservicespeed=%s", (account_id, repr(defaults), repr(speeds), repr(correction), repr(addonservicespeed)))
speed = create_speed(defaults, speeds, correction, addonservicespeed, speed, now, fMem)
logger.info("Account speed account_id=%s result speed %s", (account_id, repr(speed)))
self.session_speed = newspeed = ''.join([unicode(spi) for spi in speed])
#print speed_sess
command_dict=speed_list_to_dict(speed)
if nas.speed_value1:
result_params = command_string_parser(command_string=nas.speed_value1, command_dict=command_dict)
if result_params and nas.speed_vendor_1:
self.replypacket.AddAttribute((nas.speed_vendor_1,nas.speed_attr_id1),result_params)
elif result_params and not nas.speed_vendor_1:
self.replypacket.AddAttribute(nas.speed_attr_id1,result_params)
if nas.speed_value2:
result_params = command_string_parser(command_string=nas.speed_value2, command_dict=command_dict)
if result_params and nas.speed_vendor_2:
self.replypacket.AddAttribute((nas.speed_vendor_2,str(nas.speed_attr_id2)), result_params)
elif result_params and not nas.speed_vendor_2:
self.replypacket.AddAttribute(nas.speed_attr_id2,result_params)
def create_cursor(self):
if not self.cursor:
self.cursor=self.dbconn.cursor()
def handle(self):
global sqlloggerthread
self.datetime = datetime.datetime.now()
#nasses = self.cache.get_nas_by_ip(self.nasip)
a = time.time()
nasses = self.cache.get_nas_by_ip(self.nasip)
if not nasses:
logger.warning("Requested NAS IP (%s) not found in nasses %s", (self.nasip, str(nasses),))
sqlloggerthread.add_message(type="AUTH_NAS_NOT_FOUND", service=self.access_type, cause=u'Сервер доступа с IP %s не найден ' % self.nasip, datetime=self.datetime)
return '',None
self.replypacket = packet.Packet(secret=str(nasses[0].secret),dict=vars.DICT)
logger.info("NAS or NASSES Found %s", (str(nasses),))
#if 0: assert isinstance(nas, NasData)
station_id = self.packetobject.get('Calling-Station-Id', [''])[0]
if self.access_type == 'PPPOE' and nasses[0].type=='cisco':
station_id = station_id.replace("-",':')
user_name = str(self.packetobject['User-Name'][0])
logger.warning("Searching account username=%s in subaccounts with pptp-ipn_ip or pppoe-ipn_mac link %s", (user_name, station_id))
self.authobject=Auth(packetobject=self.packetobject, username='', password = '', secret=str(nasses[0].secret), access_type=self.access_type, challenges = queues.challenges)
subacc = self.cache.get_subaccount_by_username_w_ipn_vpn_link(user_name, station_id)
if not subacc:
logger.warning("Searching account username=%s in subaccounts witouth pptp-ipn_ip or pppoe-ipn_mac link", (user_name, ))
subacc = self.cache.get_subaccount_by_username(user_name)
if not subacc:
logger.warning("Subaccount with username %s not found", (user_name,))
sqlloggerthread.add_message(nas=nasses[0].id,type="AUTH_SUBACC_NOT_FOUND", service=self.access_type, cause=u'Субаккаунт с логином %s и ip/mac %s в системе не найден.' % (user_name, station_id), datetime=self.datetime)
return self.auth_NA(self.authobject)
acc = self.cache.get_account_by_id(subacc.account_id)
if not acc:
logger.warning("Account with username %s not found", (user_name,))
sqlloggerthread.add_message(nas=nasses[0].id, type="AUTH_ACC_NOT_FOUND", service=self.access_type, cause=u'Аккаунт для субаккаунта с логином %s в системе не найден.' % (user_name, ), datetime=self.datetime)
return self.auth_NA(self.authobject)
username = subacc.username
password = subacc.password
vpn_ip_address = subacc.vpn_ip_address
# Сервер доступа может быть не указан
nas_id = subacc.nas_id
if self.access_type=='W802.1x':
#nas = self.cache.get_nas_by_id(subacc.switch_id, None) # Пока не реализована нужна логика на стороне интерфейса
nas = self.cache.get_nas_by_id(nas_id)
else:
nas = self.cache.get_nas_by_id(nas_id)
logger.info("Nas id for user %s: %s ", (user_name, nas_id))
if self.access_type in ['PPTP','L2TP'] and subacc.associate_pptp_ipn_ip and not (subacc.ipn_ip_address == station_id):
logger.warning("Unallowed dialed ipn_ip_address for user %s vpn: station_id - %s , ipn_ip - %s; vpn_ip - %s access_type: %s", (user_name, station_id, subacc.ipn_ip_address, subacc.vpn_ip_address, self.access_type))
sqlloggerthread.add_message(nas=nas_id, type="AUTH_ASSOC_PPTP_IPN_IP", service=self.access_type, cause=u'Попытка авторизации %s с неразрешённого IPN IP адреса %s.' % (user_name, station_id,), datetime=self.datetime)
return self.auth_NA(self.authobject)
if self.access_type == 'PPPOE' and subacc.associate_pppoe_ipn_mac and not (subacc.ipn_mac_address == station_id):
logger.warning("Unallowed dialed mac for user %s: station_id - %s , ipn_ip - %s; ipn_mac - %s access_type: %s", (user_name, station_id, subacc.ipn_ip_address, subacc.ipn_mac_address, self.access_type))
sqlloggerthread.add_message(nas=nas_id, type="AUTH_ASSOC_PPTP_IPN_MAC", service=self.access_type, cause=u'Попытка авторизации %s с неразрешённого IPN MAC адреса %s.' % (user_name, station_id), datetime=self.datetime)
return self.auth_NA(self.authobject)
if (nas and nas not in nasses) and vars.IGNORE_NAS_FOR_VPN is False and self.access_type in ['PPTP', 'PPPOE']:
"""
Если NAS пользователя найден и нас не в списке доступных и запрещено игнорировать сервера доступа
"""
logger.warning("Account nas(%s) is not in sended nasses and IGNORE_NAS_FOR_VPN is False %s", (repr(nas), nasses,))
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_BAD_NAS", service=self.access_type, cause=u'Субаккаунт %s привязан к конкретному серверу доступа, но запрос на авторизацию поступил с IP %s.' % (user_name, self.nasip), datetime=self.datetime)
return self.auth_NA(self.authobject)
elif not nas_id and self.access_type!='W802.1x':
"""
Иначе, если указан любой NAS - берём первый из списка совпавших по IP
"""
nas = nasses[0]
elif (nas and nas not in nasses) and self.access_type=='W802.1x':
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_8021x_NAS", service=self.access_type, cause=u'Для 802.1x авторизации должен быть указан коммутатор. Запрос на авторизацию поступил с IP %s.' % (self.nasip), datetime=self.datetime)
logger.warning("Requested 802.1x authorization nas(%s) not assigned to user %s", (repr(nas), repr(subacc),))
return self.auth_NA(self.authobject)
self.nas_type = nas.type
self.replypacket = packet.Packet(secret=str(nas.secret),dict=vars.DICT)
self.authobject=Auth(packetobject=self.packetobject, username='', password = '', secret=str(nas.secret), access_type=self.access_type, challenges = queues.challenges, logger = logger)
self.authobject.plainusername = str(username)
self.authobject.plainpassword = str(password)
logger.debug("Account data : %s", repr(acc))
logger.debug("SubAccount data : %s", repr(subacc))
process, ok, left = self.authobject._HandlePacket()
if not process:
logger.warning(left, ())
logger.debug("Auth object : %s" , self.authobject)
#self.cur.close()
if ok:
self.reply()
else:
return self.auth_NA(self.authobject)
process, ok, left = self.authobject._ProcessPacket()
if not process:
logger.warning(left, ())
logger.debug("Auth object : %s" , self.authobject)
if ok:
return self.reply()
else:
return self.auth_NA(self.authobject)
check_auth, left = self.authobject.check_auth()
logger.debug("Auth object : %s" , self.authobject)
if not check_auth:
logger.warning(left, ())
sqlloggerthread.add_message(account=acc.id, subaccount=subacc.id, type="AUTH_BAD_PASSWORD", service=self.access_type, cause=u'Ошибка авторизации. Необходимо проверить указанный пароль.', datetime=self.datetime)
return self.auth_NA(self.authobject)
#print common_vpn,access_type,self.access_type
if acc.access_type not in ['PPTP', 'L2TP', 'PPPOE'] and (not vars.COMMON_VPN) and (acc.access_type != self.access_type) :
logger.warning("Unallowed Tarif Access Type for user %s. Account access type - %s; packet access type - %s", (username, acc.access_type, self.access_type))
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_WRONG_ACCESS_TYPE", service=self.access_type, cause=u'Способ доступа %s не совпадает с разрешённым в параметрах тарифного плана %s.' % (self.access_type, acc.access_type), datetime=self.datetime)
return self.auth_NA(self.authobject)
if acc.status != 1:
sqlloggerthread.add_message(account=acc.id, subaccount=subacc.id, type="AUTH_ACCOUNT_DISABLED", service=self.access_type, cause=u'Аккаунт отключен', datetime=self.datetime)
return self.auth_NA(self.authobject)
acstatus = (((subacc.allow_vpn_with_null and acc.ballance >=0) or (subacc.allow_vpn_with_minus and acc.ballance<=0) or acc.ballance>0)\
and \
(subacc.allow_vpn_with_block or (not subacc.allow_vpn_with_block and not acc.balance_blocked and not acc.disabled_by_limit)))
#acstatus = True
if not acstatus and not acc.vpn_guest_ippool_id:
logger.warning("Unallowed account status for user %s: account_status is false(allow_vpn_null=%s, ballance=%s, allow_vpn_with_minus=%s, allow_vpn_block=%s, ballance_blocked=%s, disabled_by_limit=%s, account_status=%s)", (username,subacc.allow_vpn_with_null,acc.ballance, subacc.allow_vpn_with_minus, subacc.allow_vpn_with_block, acc.balance_blocked, acc.disabled_by_limit, acc.status))
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_VPN_BALLANCE_ERROR", service=self.access_type, cause=u'Баланс %s, блокировка по лимитам %s, блокировка по недостатку баланса в начале р.п. %s' % (acc.ballance, acc.disabled_by_limit, acc.balance_blocked), datetime=self.datetime)
return self.auth_NA(self.authobject)
allow_dial = True #self.caches.period_cache.in_period.get(acc.tarif_id, False)
logger.info("Authorization user:%s allowed_time:%s User Status:%s Balance:%s Disabled by limit:%s Balance blocked:%s Tarif Active:%s", ( self.packetobject['User-Name'][0], allow_dial, acc.status, acc.ballance, acc.disabled_by_limit, acc.balance_blocked, acc.tariff_active))
if allow_dial and acc.tariff_active:
if not acc.sessionscount==subacc.sessionscount==0:
sc = subacc.sessionscount if subacc.sessionscount else acc.sessionscount #Переопределение на дефолтное значение
self.create_cursor()
try:
self.cursor.execute("""SELECT count(*) from radius_activesession WHERE account_id=%s and (date_end is null and (interrim_update is not Null or extract('epoch' from now()-date_start)<=%s)) and session_status='ACTIVE';""", (acc.id, nas.acct_interim_interval))
self.cursor.connection.commit()
cnt = self.cursor.fetchone()
if cnt and sc:
if cnt[0]>=sc:
logger.warning("Max sessions count %s reached for username %s. If this error persist - check your nas settings and perform maintance radius_activesession table", (sc, username,))
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_SESSIONS_COUNT_REACHED", service=self.access_type, cause=u'Превышено количество одновременных сессий для аккаунта', datetime=self.datetime)
return self.auth_NA(self.authobject)
except Exception, ex:
logger.error("Couldn't check session dublicates for user %s account=%s because %s", (str(user_name), acc.id, repr(ex)))
return self.auth_NA(self.authobject)
ipinuse_id=''
address_requested = False
if (subacc.vpn_ip_address in ('0.0.0.0','', None) and (subacc.ipv4_vpn_pool_id or acc.vpn_ippool_id)) or (acstatus==False and acc.vpn_guest_ippool_id) :
with vars.cursor_lock:
try:
self.create_cursor()
pool_id=subacc.ipv4_vpn_pool_id if subacc.ipv4_vpn_pool_id else acc.vpn_ippool_id
logger.debug("Searching free ip for subaccount %s in vpn pool with id %s ", (str(user_name), pool_id))
if acstatus==False:
pool_id=acc.vpn_guest_ippool_id
logger.debug("Searching free ip for subaccount %s in vpn guest pool with id %s ", (str(user_name), acc.vpn_guest_ippool_id))
self.cursor.execute('SELECT get_free_ip_from_pool(%s, %s);', (pool_id, nas.acct_interim_interval))
vpn_ip_address = self.cursor.fetchone()[0]
else:
pool_id, vpn_ip_address = self.find_free_ip(pool_id, nas.acct_interim_interval)
address_requested = True
#self.cursor.connection.commit()
if vpn_ip_address in ['0.0.0.0', '', None]:
logger.error("Couldn't find free ipv4 address for user %s id %s in pool: %s", (str(user_name), subacc.id, pool_id))
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_EMPTY_FREE_IPS", service=self.access_type, cause=u'В указанном пуле нет свободных IP адресов', datetime=self.datetime)
#vars.cursor_lock.release()
return self.auth_NA(self.authobject)
if address_requested:
self.cursor.execute("INSERT INTO billservice_ipinuse(pool_id, ip, datetime, dynamic, ack) VALUES(%s,%s,now(),True, False) RETURNING id;",(pool_id, vpn_ip_address))
ipinuse_id=self.cursor.fetchone()[0]
self.cursor.connection.commit()
#self.cursor.connection.commit()
except Exception, ex:
logger.error("Couldn't get an address for user %s | id %s from pool: %s :: %s", (str(user_name), subacc.id, subacc.ipv4_vpn_pool_id, repr(ex)))
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_IP_POOL_ERROR", service=self.access_type, cause=u'Ошибка выдачи свободного IP адреса', datetime=self.datetime)
return self.auth_NA(self.authobject)
else:
vpn_ip_address = subacc.vpn_ip_address
self.authobject.set_code(2)
self.replypacket.username = str(username) #Нельзя юникод
self.replypacket.password = str(password) #Нельзя юникод
self.replypacket.AddAttribute('Service-Type', 2)
self.replypacket.AddAttribute('Framed-Protocol', 1)
if vpn_ip_address not in ['0.0.0.0', '', None] and vpn_ip_address.rfind(':')==-1:
self.replypacket.AddAttribute('Framed-IP-Address', vpn_ip_address)
self.replypacket.AddAttribute('Acct-Interim-Interval', nas.acct_interim_interval)
#self.replypacket.AddAttribute('Framed-Compression', 0)
if (subacc.vpn_ipv6_ip_address and subacc.vpn_ipv6_ip_address!='::') or vpn_ip_address.rfind(':')!=-1:
#self.replypacket.AddAttribute('Framed-Interface-Id', str(subacc.vpn_ipv6_ip_address))
self.replypacket.AddAttribute('Framed-IPv6-Prefix', str(subacc.vpn_ipv6_ip_address))
#account_speed_limit_cache
self.create_speed(nas, subacc.id, acc.tarif_id, acc.id, speed=subacc.vpn_speed)
#if self.session_speed:
self.replypacket.AddAttribute('Class', str("%s,%s,%s,%s" % (subacc.id,ipinuse_id,nas.id,str(self.session_speed))))
self.add_values(acc.tarif_id, nas.id, acstatus)
#print "Setting Speed For User" , self.speed
if vars.SQLLOG_SUCCESS:
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_OK", service=self.access_type, cause=u'Авторизация прошла успешно.', datetime=self.datetime)
#return self.authobject, self.replypacket
self.reply()
if self.access_type=='PPPOE' and vars.GET_MAC_FROM_PPPOE==True:
logger.debug("Trying to update subaccount %s with id %s ipn mac address to: %s ", (str(user_name), subacc.id, station_id))
with vars.cursor_lock:
try:
self.create_cursor()
self.cursor.execute("UPDATE billservice_subaccount SET ipn_mac_address=%s WHERE id=%s", (station_id, subacc.id,))
self.cursor.connection.commit()
logger.debug("Update subaccount %s with id %s ipn mac address to: %s was succefull", (str(user_name), subacc.id, station_id))
except Exception, ex:
logger.error("Error update subaccount %s with id %s ipn mac address to: %s %s", (str(user_name), subacc.id, station_id, repr(ex)))
else:
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_BAD_TIME", service=self.access_type, cause=u'Тариф неактивен(%s) или запрещённое время %s' % (acc.tariff_active==False, allow_dial==False), datetime=self.datetime)
return self.auth_NA(self.authobject)
class HandlelISGAuth(HandleSAuth):
def reply(self):
#self.transport.write(self.replypacket.ReplyPacket(), self.addrport)
returndata = self.replypacket.ReplyPacket()
logger.debug("REPLY packet: %s", repr(self.replypacket))
self.transport.write(returndata, self.addrport)
def auth_NA(self, secret=''):
"""
Denides access
"""
reply = self.packetobject.CreateReply()
reply.code = packet.AccessReject
reply.secret = secret
logger.debug("REPLY packet: %s", repr(reply))
self.transport.write(reply.ReplyPacket(), self.addrport)
def handle(self):
global sqlloggerthread
self.datetime = datetime.datetime.now()
nasses = self.cache.get_nas_by_ip(self.nasip)
if not nasses:
logger.warning("Requested NAS IP (%s) not found in nasses %s", (self.nasip, str(nasses),))
sqlloggerthread.add_message(type="AUTH_NAS_NOT_FOUND", service=self.access_type, cause=u'Сервер доступа с IP %s не найден ' % self.nasip, datetime=self.datetime)
return
#if 0: assert isinstance(nas, NasData)
nas = nasses[0]
#station_id = self.packetobject.get('Calling-Station-Id', [''])[0]
station_id = str(self.packetobject['User-Name'][0]) if self.access_type=='lISG' else \
str(self.packetobject['Calling-Station-Id'][0])
user_name = station_id
#print station_id
#row = get_account_data_by_username(self.cur, self.packetobject['User-Name'][0], self.access_type, station_id=station_id, multilink = self.multilink, common_vpn = common_vpn)
#print self.caches.account_cache.by_ipn_ip_nas
logger.warning("Searching %s account for ip address %s", (self.access_type,station_id))
#acc = self.caches.account_cache.by_ipn_ip_nas.get((station_id, nas.id))
#subacc = SubAccount()
if '.' in station_id:
subacc = self.cache.get_subaccount_by_ipn_ip(station_id)
else:
subacc = self.cache.get_subaccount_by_ipn_mac(station_id)
self.authobject=Auth(packetobject=self.packetobject, username='', password = '', secret=str(nasses[0].secret), access_type=self.access_type, challenges = queues.challenges)
if not subacc:
logger.warning("Subcccount for %s not found for ip address %s", (self.access_type, station_id,))
sqlloggerthread.add_message(type="AUTH_SUBACC_NOT_FOUND", service=self.access_type, cause=u'Субаккаунт с логином ipn ip/mac %s в системе не найден.' % (station_id,), datetime=self.datetime)
#Не учитывается сервер доступа
return self.auth_NA(nas.secret)
acc = self.cache.get_account_by_id(subacc.account_id)
if not acc:
logger.warning("Account with username %s not found", (user_name,))
sqlloggerthread.add_message(type="AUTH_ACC_NOT_FOUND", service=self.access_type, cause=u'Аккаунт для субаккаунта с логином %s в системе не найден.' % (subacc.username, ), datetime=self.datetime)
return self.auth_NA(nas.secret)
nas_id = subacc.nas_id
nas = self.cache.get_nas_by_id(nas_id)
if vars.IGNORE_NAS_FOR_VPN is False and (nas and nas not in nasses):
"""
Если NAS пользователя найден и нас не в списке доступных и запрещено игнорировать сервера доступа
"""
logger.warning("Account nas(%s) is not in sended nasses and IGNORE_NAS_FOR_VPN is False %s", (repr(nas), nasses,))
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_BAD_NAS", service=self.access_type, cause=u'Субаккаунт привязан к конкретному серверу доступа, но запрос на авторизацию поступил с IP %s.' % (self.nasip), datetime=self.datetime)
return self.auth_NA(nas.secret)
elif not nas_id:
"""
Иначе, если указан любой NAS - берём первый из списка совпавших по IP
"""
nas = nasses[0]
nas_id = nas.id
self.nas_type = nas.type
self.replypacket = self.packetobject.CreateReply()
self.replypacket.secret=str(nas.secret)
logger.debug("Account data : %s", repr(acc))
if acc.status != 1:
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_ACCOUNT_DISABLED", service=self.access_type, cause=u'Аккаунт отключен', datetime=self.datetime)
return self.auth_NA(nas.secret)
#print common_vpn,access_type,self.access_type
if acc.access_type is None:
logger.warning("Unallowed Access Type for user %s. Access type - %s; packet access type - %s", (user_name, acc.access_type, self.access_type))
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_WRONG_ACCESS_TYPE", service=self.access_type, cause=u'Способ доступа %s не совпадает с разрешённым в параметрах тарифного плана %s.' % (self.access_type, acc.access_type), datetime=self.datetime)
return self.auth_NA(nas.secret)
acstatus = (((subacc.allow_vpn_with_null and acc.ballance >=0) or (subacc.allow_vpn_with_minus and acc.ballance<=0) or acc.ballance>0)\
and \
(subacc.allow_vpn_with_block or (not subacc.allow_vpn_with_block and not acc.balance_blocked==True and not acc.disabled_by_limit==True)))
#acstatus = True
if not acstatus:
logger.warning("Unallowed account status for user %s: account_status is false(allow_vpn_null=%s, ballance=%s, allow_vpn_with_minus=%s, allow_vpn_block=%s, ballance_blocked=%s, disabled_by_limit=%s, account_status=%s)", (subacc.username, subacc.allow_vpn_with_null,acc.ballance, subacc.allow_vpn_with_minus, subacc.allow_vpn_with_block, acc.balance_blocked, acc.disabled_by_limit, acc.status))
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_VPN_BALLANCE_ERROR", service=self.access_type, cause=u'Баланс %s, блокировка по лимитам %s, блокировка по недостатку баланса в начале р.п. %s' % (acc.ballance, acc.disabled_by_limit, acc.balance_blocked), datetime=self.datetime)
return self.auth_NA(nas.secret)
#username, password, nas_id, ipaddress, tarif_id, access_type, status, balance_blocked, ballance, disabled_by_limit, speed, tarif_status = row
allow_dial = True #self.caches.period_cache.in_period.get(acc.tarif_id, False)
logger.info("Authorization user:%s allowed_time:%s User Status:%s Balance:%s Disabled by limit:%s Balance blocked:%s Tarif Active:%s", ( self.packetobject['User-Name'][0], allow_dial, acc.status, acc.ballance, acc.disabled_by_limit, acc.balance_blocked, acc.tariff_active))
if allow_dial and acc.tariff_active:
self.replypacket.code=2
self.replypacket.username = '' #Нельзя юникод
self.replypacket.password = '' #Нельзя юникод
if subacc.vpn_ip_address not in ['', '0.0.0.0', '0.0.0.0/0']:
self.replypacket.AddAttribute('Framed-IP-Address', subacc.vpn_ip_address)
else:
self.replypacket.AddAttribute('Framed-IP-Address', subacc.ipn_ip_address)
self.replypacket.AddAttribute('Acct-Interim-Interval', nas.acct_interim_interval)
#self.create_speed(nas, subacc.id, acc.tarif_id, acc.account_id, speed=subacc.vpn_speed)
#self.replypacket.AddAttribute('Class', str("%s,0,%s,%s" % (subacc.id,nas_id,str(self.session_speed))))
self.create_speed(nas, subacc.id, acc.tarif_id, acc.id, speed=subacc.vpn_speed)
#if self.session_speed:
self.replypacket.AddAttribute('Class', str("%s,0,%s,%s" % (subacc.id,nas_id,str(self.session_speed))))
self.add_values(acc.tarif_id, nas.id, acstatus)
if vars.SQLLOG_SUCCESS:
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_OK", service=self.access_type, cause=u'Авторизация прошла успешно.', datetime=self.datetime)
self.reply()
else:
sqlloggerthread.add_message(nas=nas_id, account=acc.id, subaccount=subacc.id, type="AUTH_BAD_TIME", service=self.access_type, cause=u'Тариф неактивен(%s) или запрещённое время %s' % (acc.tariff_active==False, allow_dial==False), datetime=self.datetime)
return self.auth_NA(nas.secret)
#HotSpot_class
#auth_class
class HandleHotSpotAuth(HandleSAuth):
__slots__ = () + ('access_type', 'secret', 'speed','nas_id', 'nas_type', 'fMem', 'cursor', 'transport', 'addrport')
def __init__(self, packetobject, access_type, dbCur, transport, addrport):
#self.nasip = str(packetobject['NAS-IP-Address'][0])
self.packetobject = packetobject
self.access_type=access_type
self.secret = ''
self.cursor = dbCur
self.transport = transport
self.addrport = addrport
#logger.debugfun('%s', show_packet, (packetobject,))
def handle(self):
global sqlloggerthread
self.datetime = datetime.datetime.now()
nasses = self.cache.get_nas_by_ip(self.nasip)
if not nasses:
logger.warning("Requested NAS IP (%s) not found in nasses %s", (self.nasip, str(nasses),))
sqlloggerthread.add_message(type="AUTH_NAS_NOT_FOUND", service=self.access_type, cause=u'Сервер доступа с IP %s не найден ' % self.nasip, datetime=self.datetime)
return '',None
#if 0: assert isinstance(nas, NasData)
nas = nasses[0]
#self.nas_id=str(row[0])
self.nas_type = nas.type
#self.secret = str(row[1])
self.replypacket=packet.Packet(secret=str(nas.secret),dict=vars.DICT)
user_name = str(self.packetobject['User-Name'][0])
mac=str(self.packetobject['User-Name'][0]).lower() if str(self.packetobject['User-Name'][0]).lower().count(':')==5 else str(self.packetobject['Calling-Station-Id'][0]).lower()
ip=str(self.packetobject['Mikrotik-Host-IP'][0])
self.cursor.execute("SELECT pin FROM billservice_card WHERE activated is NULL and salecard_id IS NOT NULL AND login = %s AND now() BETWEEN start_date AND end_date;", (user_name,))
pin = self.cursor.fetchone()
acc=None
if pin:
logger.info("Activating account username %s pin %s ip %s mac %s", (user_name, pin[0], ip,mac))
self.cursor.execute("""SELECT * FROM card_activate_fn(%s, %s, %s::inet, %s::text) AS
A(account_id int, subaccount_id int, "password" text, nas_id int, tarif_id int, status int,
balance_blocked boolean, ballance numeric, disabled_by_limit boolean, tariff_active boolean, ipv4_vpn_pool_id int, tarif_vpn_ippool_id int,vpn_ip_address inet,ipn_ip_address inet,ipn_mac_address text,access_type text)
""", (user_name, pin[0], ip,mac))
acct_card = self.cursor.fetchone()
self.cursor.connection.commit()
#self.cursor.close()
acc = acct_card
if acct_card is None:
logger.warning("Card with login %s was not found", user_name)
#sqlloggerthread.add_message(nas=nas.id, type="AUTH_BAD_USER", service=self.access_type, cause=u'Пользователь HotSpot с логином %s не найден или не может быть активирован.' % (user_name,), datetime=self.datetime)
#return self.auth_NA(authobject)
acct_card = CardActivateData(*acct_card)
acc = acct_card
["HotSpot", 'HotSpotIp+Mac', 'HotSpotIp+Password','HotSpotMac','HotSpotMac+Password']
"""
HotSpot - оставляем логику по умолчанию
HotSpotIp+Mac - в субаккаунт добавляем только IPN IP и ipn_mac
HotSpotIp+Password - в субаккаунт добавляем IPN IP и password
HotSpotMac - в субаккаунт
"""
self.authobject=Auth(packetobject=self.packetobject, username='', password = '', secret=str(nas.secret), access_type=self.access_type)
subacc = self.cache.get_subaccount_by_username(user_name)
logger.info("Subacc %s for username %s acc %s", (subacc, user_name, acc))
if not acc and subacc:
acc=self.cache.get_account_by_id(subacc.account_id)
logger.info("Subacc %s access type=%s for username %s", (subacc, acc.access_type, user_name, ))
if not acc.access_type=='HotSpot':
acc=None
if not acc and mac:
subacc = self.cache.get_subaccount_by_mac(mac)
if subacc:
acc=self.cache.get_account_by_id(subacc.account_id)
if acc.access_type not in ['HotSpotMac','HotSpotIp+Mac']:
acc=None
if acc.access_type=='HotSpotIp+Mac' and subacc.ipn_ip_address!=ip:
acc=None
logger.info("Subacc %s access type=%s for username %s", (subacc, acc.access_type if acc else 'acc not found', user_name, ))
if not acc and ip:
subacc = self.cache.get_subaccount_by_ipn_ip(ip)
if subacc:
acc=self.cache.get_account_by_id(subacc.account_id)
if acc.access_type!='HotSpotIp+Password':
acc=None
subacc=None
logger.info("Subacc %s access type=%s for username %s", (subacc, acc.access_type, user_name, ))
if not acc:
"""
Если не нашли совпадений ранее - пытаемся найти карту, чтобы активировать нового абонента
"""
sqlloggerthread.add_message(nas=nas.id, type="CARD_USER_NOT_FOUND", service=self.access_type, cause=u'Карта/пользователь с логином %s ip %s и mac %s не найдены ' % (user_name, ip, mac), datetime=self.datetime)
self.cursor.close()
return self.auth_NA(self.authobject)
else:
if subacc:
pin = subacc.password
else:
pin = acc.password
if str(acc.access_type) in ['HotSpot','HotSpotIp+Password', 'HotSpotMac+Password']:
self.authobject.plainusername = str(user_name)
if subacc:
self.authobject.plainpassword = str(subacc.password)
else:
self.authobject.plainpassword = str(pin)
check_auth, left = self.authobject.check_auth()
if not check_auth:
logger.warning(left, ())
sqlloggerthread.add_message(nas=nas.id, type="AUTH_BAD_PASSWORD", service=self.access_type, cause=u'Ошибка авторизации. Необходимо проверить указанный пароль.', datetime=self.datetime)
self.cursor.close()
return self.auth_NA(self.authobject)
if subacc:
acstatus = acc.status==1 and (((subacc.allow_vpn_with_null and acc.ballance >=0) or (subacc.allow_vpn_with_minus and acc.ballance<=0) or acc.ballance>0)\
and \
(subacc.allow_vpn_with_block or (not subacc.allow_vpn_with_block and not acc.balance_blocked and not acc.disabled_by_limit)))
else:
acstatus = acc.ballance >0 and not acc.balance_blocked and not acc.disabled_by_limit and acc.status==1
if subacc:
subacc_id=subacc.id
else: