public
nobgit
read
NobMail
Based on mailcow: dockerized
Languages
Repository composition by tracked source files.
PHP
49%
JavaScript
35%
HTML
9%
CSS
4%
Shell
2%
Python
1%
Lua
0%
Perl
0%
Ruby
0%
SCSS
0%
Create file
Wiki Documentation
Clone
https://nobgit.com/orgs/nobgit/nobmail.git
ssh://[email protected]:2222/orgs/nobgit/nobmail.git
Trace
data/conf/dovecot/auth/mailcowauth.php
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
1
<?php
2
ini_set('error_reporting', 0);
3
header('Content-Type: application/json');
5
$post = trim(file_get_contents('php://input'));
6
if ($post) {
7
$post = json_decode($post, true);
8
}
11
$return = array("success" => false);
12
if(!isset($post['username']) || !isset($post['password']) || !isset($post['real_rip'])){
13
error_log("MAILCOWAUTH: Bad Request");
14
http_response_code(400); // Bad Request
15
echo json_encode($return);
16
exit();
17
}
19
require_once('../../../web/inc/vars.inc.php');
20
if (file_exists('../../../web/inc/vars.local.inc.php')) {
21
include_once('../../../web/inc/vars.local.inc.php');
22
}
23
require_once '../../../web/inc/lib/vendor/autoload.php';
26
// Init Redis
27
$redis = new Redis();
28
try {
29
if (!empty(getenv('REDIS_SLAVEOF_IP'))) {
30
$redis->connect(getenv('REDIS_SLAVEOF_IP'), getenv('REDIS_SLAVEOF_PORT'));
31
}
32
else {
33
$redis->connect('redis-mailcow', 6379);
34
}
35
$redis->auth(getenv("REDISPASS"));
36
}
37
catch (Exception $e) {
38
error_log("MAILCOWAUTH: " . $e . PHP_EOL);
39
http_response_code(500); // Internal Server Error
40
echo json_encode($return);
41
exit;
42
}
44
// Init database
45
$dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
46
$opt = [
47
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
48
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
49
PDO::ATTR_EMULATE_PREPARES => false,
50
];
51
try {
52
$pdo = new PDO($dsn, $database_user, $database_pass, $opt);
53
}
54
catch (PDOException $e) {
55
error_log("MAILCOWAUTH: " . $e . PHP_EOL);
56
http_response_code(500); // Internal Server Error
57
echo json_encode($return);
58
exit;
59
}
61
// Load core functions first
62
require_once 'functions.inc.php';
63
require_once 'functions.auth.inc.php';
64
require_once 'sessions.inc.php';
65
require_once 'functions.mailbox.inc.php';
66
require_once 'functions.ratelimit.inc.php';
67
require_once 'functions.acl.inc.php';
70
$isSOGoRequest = $post['real_rip'] == getenv('IPV4_NETWORK') . '.248';
71
$result = false;
72
if ($isSOGoRequest) {
73
// This is a SOGo Auth request. First check for SSO password.
74
$sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
75
if ($sogo_sso_pass === $post['password']){
76
error_log('MAILCOWAUTH: SOGo SSO auth for user ' . $post['username']);
77
set_sasl_log($post['username'], $post['real_rip'], "SOGO");
78
$result = true;
79
}
80
}
81
if ($result === false){
82
// If it's a SOGo Request, don't check for protocol access
83
if ($isSOGoRequest) {
84
$service = 'SOGO';
85
$post['service'] = 'NONE';
86
} else {
87
$service = $post['service'];
88
}
90
$result = apppass_login($post['username'], $post['password'], array(
91
'service' => $post['service'],
92
'is_internal' => true,
93
'remote_addr' => $post['real_rip']
94
));
95
if ($result) {
96
error_log('MAILCOWAUTH: App auth for user ' . $post['username'] . " with service " . $service . " from IP " . $post['real_rip']);
97
set_sasl_log($post['username'], $post['real_rip'], $service);
98
}
99
}
100
if ($result === false){
101
// Init Identity Provider
102
$iam_provider = identity_provider('init');
103
$iam_settings = identity_provider('get');
104
$result = user_login($post['username'], $post['password'], array('is_internal' => true, 'service' => $post['service']));
105
if ($result) {
106
error_log('MAILCOWAUTH: User auth for user ' . $post['username'] . " with service " . $post['service'] . " from IP " . $post['real_rip']);
107
set_sasl_log($post['username'], $post['real_rip'], $post['service']);
108
}
109
}
111
if ($result) {
112
http_response_code(200); // OK
113
$return['success'] = true;
114
} else {
115
error_log("MAILCOWAUTH: Login failed for user " . $post['username'] . " with service " . $post['service'] . " from IP " . $post['real_rip']);
116
http_response_code(401); // Unauthorized
117
}
120
echo json_encode($return);
121
session_destroy();
122
exit;