|
17 | 17 | from django import forms |
18 | 18 | from django.conf import settings |
19 | 19 | from django.contrib.auth import authenticate, login, logout |
| 20 | +from django.contrib.auth.backends import UserModel |
20 | 21 | from django.db import transaction, connection, DatabaseError |
21 | 22 | from django.http import HttpResponse |
22 | 23 | from django.shortcuts import render, redirect |
23 | 24 | from django.template.defaultfilters import filesizeformat |
24 | | -from django.utils.baseconv import base64 |
25 | 25 | from django.utils.translation import ugettext as _ |
26 | 26 | from django.views.decorators.csrf import csrf_exempt |
| 27 | +from ldap3 import Server, Connection, ALL |
| 28 | +from ldap3.core.exceptions import LDAPExceptionError |
27 | 29 |
|
28 | 30 | from .forms import UploadFileForm |
29 | 31 | from .models import User |
@@ -54,6 +56,7 @@ def index(request): |
54 | 56 | } |
55 | 57 | if 'dlpinit' in request.session: |
56 | 58 | del request.session['dlpinit'] |
| 59 | + |
57 | 60 | return render(request, 'index.html', d) |
58 | 61 |
|
59 | 62 |
|
@@ -174,6 +177,57 @@ def admins_login(request): |
174 | 177 | return render(request, 'login.html', d) |
175 | 178 |
|
176 | 179 |
|
| 180 | +def ldapijc(request): |
| 181 | + if request.user.is_authenticated: |
| 182 | + return main(request) |
| 183 | + else: |
| 184 | + d = { |
| 185 | + 'title': _('title.login.page'), |
| 186 | + 'note': _('msg.note.ldap.injection'), |
| 187 | + } |
| 188 | + if request.method == 'GET': |
| 189 | + return render(request, 'login.html', d) |
| 190 | + elif request.method == 'POST': |
| 191 | + username = request.POST.get("username") |
| 192 | + password = request.POST.get("password") |
| 193 | + |
| 194 | + if is_account_lockedout(username): |
| 195 | + d['errmsg'] = _("msg.authentication.fail") |
| 196 | + else: |
| 197 | + username = request.POST.get("username") |
| 198 | + password = request.POST.get("password") |
| 199 | + try: |
| 200 | + server = Server(settings.LDAP_HOST, settings.LDAP_PORT, get_info=ALL) |
| 201 | + conn = Connection(server, 'uid=admin,ou=people,dc=t246osslab,dc=org', 'password', auto_bind=True) |
| 202 | + conn.search('ou=people,dc=t246osslab,dc=org', |
| 203 | + '(&(uid=' + username + ')(userPassword=' + password + '))', |
| 204 | + attributes=['uid']) # TODO trim |
| 205 | + if len(conn.entries) > 0: |
| 206 | + user = UserModel._default_manager.get_by_natural_key(conn.entries[0].uid) |
| 207 | + login(request, user) |
| 208 | + # authentication succeeded, then reset account lock |
| 209 | + reset_account_lock(username) |
| 210 | + request.session["username"] = username |
| 211 | + if 'target' not in request.session: |
| 212 | + return main(request) |
| 213 | + else: |
| 214 | + target = request.session['target'] |
| 215 | + del request.session['target'] |
| 216 | + return redirect(target) |
| 217 | + else: |
| 218 | + d['errmsg'] = _("msg.authentication.fail") |
| 219 | + # account lock count +1 |
| 220 | + increment_account_lock_num(username) |
| 221 | + |
| 222 | + except LDAPExceptionError as le: |
| 223 | + d['errmsg'] = _("msg.ldap.access.fail") |
| 224 | + except Exception as e: |
| 225 | + logger.exception('Exception occurs: %s', e) |
| 226 | + d['errmsg'] = _('msg.unknown.exception.occur') + ": " + str(e) |
| 227 | + |
| 228 | + return render(request, 'login.html', d) |
| 229 | + |
| 230 | + |
177 | 231 | def bruteforce(request): |
178 | 232 | if request.user.is_authenticated: |
179 | 233 | return main(request) |
|
0 commit comments