Skip to content

Commit f6b2583

Browse files
committed
ldap support
1 parent 63e82e9 commit f6b2583

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

Dockerfile

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ FROM php:7.4-apache
22

33
MAINTAINER Miroslav Sedivy
44

5+
ARG LDAP=false
6+
57
RUN set -eux; apt-get update; \
68
apt-get install -y --no-install-recommends \
79
#
@@ -12,18 +14,24 @@ RUN set -eux; apt-get update; \
1214
zlib1g-dev libpng-dev libjpeg-dev \
1315
libwebp-dev libxpm-dev libfreetype6-dev; \
1416
#
15-
# clean up
16-
rm -rf /var/lib/apt/lists/*; \
17-
#
1817
# configure extensions
1918
docker-php-ext-configure gd --enable-gd \
2019
--with-jpeg --with-webp --with-xpm --with-freetype; \
2120
#
2221
# install extensions
2322
docker-php-ext-install curl gd pdo pdo_mysql; \
2423
#
24+
# LDAP support
25+
if [ -n "$LDAP" ] && [ "$LDAP" = "true" ]; then \
26+
apt-get install -y --no-install-recommends libldb-dev libldap2-dev; \
27+
docker-php-ext-install ldap; \
28+
fi; \
29+
#
2530
# set up environment
26-
a2enmod rewrite;
31+
a2enmod rewrite; \
32+
#
33+
# clean up
34+
rm -rf /var/lib/apt/lists/*;
2735

2836
#
2937
# copy files

app/user.class.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ public static function is_logged_in(){
1818
return true;
1919
}
2020

21-
return !empty($_SESSION[User::SESSION_NAME]) && $_SESSION[User::SESSION_NAME] === hash("crc32", Config::get("nick").Config::get_safe("pass", ""), false);
21+
if(Config::get_safe("ldap_enabled", false)){
22+
return !empty($_SESSION[User::SESSION_NAME]) &&
23+
$_SESSION[User::SESSION_NAME] === 'admin';
24+
}
25+
26+
return !empty($_SESSION[User::SESSION_NAME]) &&
27+
$_SESSION[User::SESSION_NAME] === hash("crc32", Config::get("nick").Config::get_safe("pass", ""), false);
2228
}
2329

2430
public static function login($nick, $pass){
@@ -30,6 +36,14 @@ public static function login($nick, $pass){
3036
throw new Exception(__("You are already logged in."));
3137
}
3238

39+
if(Config::get_safe("ldap_enabled", false)){
40+
return static::LDAP_login($nick, $pass);
41+
} else {
42+
return static::config_login($nick, $pass);
43+
}
44+
}
45+
46+
private static function config_login($nick, $pass){
3347
if(Config::get("nick") === $nick && Config::get_safe("pass", "") === $pass){
3448
$_SESSION[User::SESSION_NAME] = hash("crc32", $nick.$pass, false);
3549
return ["logged_in" => true, "is_visitor" => false];
@@ -45,6 +59,34 @@ public static function login($nick, $pass){
4559
throw new Exception(__("The nick or password is incorrect."));
4660
}
4761

62+
private static function LDAP_login($nick, $pass){
63+
$ldap_host = Config::get("ldap_host");
64+
$ldap_port = Config::get_safe("ldap_port", 389);
65+
$ldap_admin_dn = Config::get_safe("ldap_admin_dn", false);
66+
$ldap_visitor_dn = Config::get_safe("ldap_visitor_dn", false);
67+
68+
if(!($ds = ldap_connect($ldap_host, $ldap_port))) {
69+
throw new Exception(__("Could not connect to LDAP server."));
70+
}
71+
72+
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
73+
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
74+
ldap_set_option($ds, LDAP_OPT_NETWORK_TIMEOUT, 10);
75+
76+
if ($ldap_admin_dn !== false && ldap_bind($ds, "cn=".$nick.",".$ldap_admin_dn, $pass)) {
77+
$_SESSION[User::SESSION_NAME] = 'admin';
78+
return ["logged_in" => true, "is_visitor" => false];
79+
}
80+
81+
if ($ldap_visitor_dn !== false && ldap_bind($ds, "cn=".$nick.",".$ldap_visitor_dn, $pass)) {
82+
$_SESSION[User::SESSION_NAME] = 'visitor';
83+
return ["logged_in" => false, "is_visitor" => true];
84+
}
85+
86+
Log::put("login_fails", $nick);
87+
throw new Exception(__("The nick or password is incorrect."));
88+
}
89+
4890
public static function logout(){
4991
if(!Config::get_safe("force_login", false)){
5092
throw new Exception(__("You can't log out. There is no account."));

config.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ pass = demo
4343
;visitor[user] = pass
4444
;visitor[user] = pass
4545

46+
;[ldap]
47+
;ldap_enabled = true
48+
;ldap_host = localhost
49+
;ldap_port = 389
50+
;ldap_admin_dn = 'ou=admin,dc=example,dc=org'
51+
;ldap_visitor_dn = 'ou=visitor,dc=example,dc=org'
52+
4653
[directories]
4754
images_path = data/i/
4855
thumbnails_path = data/t/

0 commit comments

Comments
 (0)