-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquid.py
More file actions
executable file
·141 lines (129 loc) · 3.92 KB
/
Copy pathsquid.py
File metadata and controls
executable file
·141 lines (129 loc) · 3.92 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Guifibages auth client for Squid
#
# Copyright 2012 Associació d'Usuaris Guifibages
# Author: Ignacio Torres Masdeu <ignacio@xin.cat>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import urllib
import base64
import syslog
import time
import ldap
import sys
from datetime import datetime
import os
import json
import httplib
def log(logstring):
if len(logstring) == 0:
return
syslog.syslog("%s %s" % (sys.argv[1], logstring))
def ldapauth(username,password):
log("ldapauth (%s,%s)" %(username, password))
if len(username)==0:
return False
user_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net" % username
try:
l = ldap.initialize("ldaps://aaa.guifibages.net:636")
l.simple_bind_s(user_dn,password)
return True
except ldap.INVALID_CREDENTIALS:
return False
except ldap.LDAPError, error_message:
log('LDAPError: %s' % error_message)
return False
def ssocheck(username,ip):
try:
h = httplib.HTTPSConnection('webfront01.guifibages.net')
h.request("GET", "/api/user/%s" % username)
res = h.getresponse()
status = False
if res.status == 200:
rdata = res.read()
# log("ssocheck %s %s: %s" % (username, ip, rdata))
data = json.loads(rdata)
if ip in data:
status = True
h.close()
return status
except Exception, error:
log ("ssocheck Error: %s" % error)
def otpcheck(ip, username,password):
try:
h = httplib.HTTPSConnection('webfront01.guifibages.net')
headers = {"Content-type": "application/x-www-form-urlencoded"}
params = urllib.encode({'ip': ip, 'password': password})
h.request("POST", "/api/user/%s/otp" % username, params, headers)
res = h.getresponse()
status = False
if res.status == 200:
status = True
h.close()
return status
except Exception, error:
log ("otpcheck Error: %s" % error)
def accept(reason,router,username,ip):
log("%s Login to %s: %s %s" % (reason, router, username, ip) )
print "Auth-Type := Accept"
def trusted(username,ip):
trusted_users=[
'ignacio.torres',
'albert.homs',
'francisco.delaguila',
'gil.obradors',
'josep.figueres',
'xavier.martinez'
]
trusted_stations=[
'10.228.17.24']
if username in trusted_users and ip in trusted_stations:
return True
return False
def validate():
log(sys.argv)
log("Vamos que nos vamos")
while True:
line = sys.stdin.readline().strip()
if sys.argv[1] == 'auth':
print "OK"
continue
el = line.split()
try:
ip = el[0]
user = el[1]
auth = urllib.unquote(el[2]).split(' ')
b64password = base64.b64decode(auth[1])
el.append(auth)
(u,password) = b64password.split(':')
el.append(u)
el.append(password)
except IndexError:
log("ERR not enough values: %s" % el)
print "ERR"
continue
if otpcheck(ip,user,password):
print "OK"
log("OK otpcheck: %s" % el)
elif ssocheck(user,ip):
print "OK"
log("OK ssocheck: %s" % el)
elif ldapauth(user,password):
print "OK"
log("OK ldapauth: %s" % user)
else:
print "ERR"
log("ERR: %s" % el)
exit(0)