dolibarr 19.0.3
functions_dolibarr.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2015 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2007-2015 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2022 Harry Winner Kamdem <harry@sense.africa>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
38function check_user_password_dolibarr($usertotest, $passwordtotest, $entitytotest = 1)
39{
40 global $db, $conf, $langs;
41
42 // Force master entity in transversal mode
43 $entity = $entitytotest;
44 if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
45 $entity = 1;
46 }
47
48 $login = '';
49
50 if (!empty($usertotest)) {
51 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
52 dol_syslog("functions_dolibarr::check_user_password_dolibarr usertotest=".$usertotest." passwordtotest=".preg_replace('/./', '*', $passwordtotest)." entitytotest=".$entitytotest);
53
54 // Verification number of USER_LOGIN_FAILED
55 $dateverificationauth = dol_time_plus_duree(dol_now(), -1, 'd');
56 $userremoteip = getUserRemoteIP();
57 $nbevents = 0;
58
59 $sql = "SELECT COUNT(e.rowid) as nbevent";
60 $sql .= " FROM ".MAIN_DB_PREFIX."events as e";
61 $sql .= " WHERE e.type = 'USER_LOGIN_FAILED'";
62 $sql .= " AND e.ip = '".$db->escape($userremoteip)."'";
63 $sql .= " AND e.dateevent > '".$db->idate($dateverificationauth)."'";
64 $resql = $db->query($sql);
65 if ($resql) {
66 $obj = $db->fetch_object($resql);
67 if ($obj) {
68 $nbevents = $obj->nbevent;
69 }
70 }
71
72 if ($nbevents <= getDolGlobalInt("MAIN_SECURITY_MAX_NUMBER_FAILED_AUTH", 100)) {
73 // If test username/password asked, we define $test=false if ko and $login var to login if ok, set also $_SESSION["dol_loginmesg"] if ko
74 $table = MAIN_DB_PREFIX."user";
75 $usernamecol1 = 'login';
76 $usernamecol2 = 'email';
77 $entitycol = 'entity';
78
79 $sql = "SELECT rowid, login, entity, pass, pass_crypted, datestartvalidity, dateendvalidity, flagdelsessionsbefore";
80 $sql .= " FROM ".$table;
81 $sql .= " WHERE (".$usernamecol1." = '".$db->escape($usertotest)."'";
82 if (preg_match('/@/', $usertotest)) {
83 $sql .= " OR ".$usernamecol2." = '".$db->escape($usertotest)."'";
84 }
85 $sql .= ") AND ".$entitycol." IN (0,".($entity ? ((int) $entity) : 1).")";
86 $sql .= " AND statut = 1";
87 // Order is required to firstly found the user into entity, then the superadmin.
88 // For the case (TODO: we must avoid that) a user has renamed its login with same value than a user in entity 0.
89 $sql .= " ORDER BY entity DESC";
90
91 // Note: Test on validity is done later natively with isNotIntoValidityDateRange() by core after calling checkLoginPassEntity() that call this method
92
93 $resql = $db->query($sql);
94 if ($resql) {
95 $obj = $db->fetch_object($resql);
96 if ($obj) {
97 $passclear = $obj->pass;
98 $passcrypted = $obj->pass_crypted;
99 $passtyped = $passwordtotest;
100
101 $passok = false;
102
103 // Check crypted password
104 $cryptType = '';
105 if (getDolGlobalString('DATABASE_PWD_ENCRYPTED')) {
106 $cryptType = $conf->global->DATABASE_PWD_ENCRYPTED;
107 }
108
109 // By default, we use default setup for encryption rule
110 if (!in_array($cryptType, array('auto'))) {
111 $cryptType = 'auto';
112 }
113 // Check crypted password according to crypt algorithm
114 if ($cryptType == 'auto') {
115 if ($passcrypted && dol_verifyHash($passtyped, $passcrypted, '0')) {
116 $passok = true;
117 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentification ok - hash ".$cryptType." of pass is ok");
118 }
119 }
120
121 // For compatibility with very old versions
122 if (!$passok) {
123 if ((!$passcrypted || $passtyped)
124 && ($passclear && ($passtyped == $passclear))) {
125 $passok = true;
126 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentification ok - found old pass in database", LOG_WARNING);
127 }
128 }
129
130 // Password ok ?
131 if ($passok) {
132 $login = $obj->login;
133 } else {
134 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO bad password for '".$usertotest."', cryptType=".$cryptType, LOG_NOTICE);
135 sleep(1); // Anti brut force protection. Must be same delay when login is not valid
136
137 // Load translation files required by the page
138 $langs->loadLangs(array('main', 'errors'));
139
140 $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorBadLoginPassword");
141 }
142
143 // We must check entity
144 if ($passok && isModEnabled('multicompany')) { // We must check entity
145 global $mc;
146
147 if (!isset($mc)) {
148 !isModEnabled('multicompany'); // Global not available, disable $conf->multicompany->enabled for safety
149 } else {
150 $ret = $mc->checkRight($obj->rowid, $entitytotest);
151 if ($ret < 0) {
152 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO entity '".$entitytotest."' not allowed for user '".$obj->rowid."'", LOG_NOTICE);
153
154 $login = ''; // force authentication failure
155 if ($mc->db->lasterror()) {
156 $_SESSION["dol_loginmesg"] = $mc->db->lasterror();
157 }
158 }
159 }
160 }
161 } else {
162 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO user not found for '".$usertotest."'", LOG_NOTICE);
163 sleep(1); // Anti brut force protection. Must be same delay when password is not valid
164
165 // Load translation files required by the page
166 $langs->loadLangs(array('main', 'errors'));
167
168 $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorBadLoginPassword");
169 }
170 } else {
171 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO db error for '".$usertotest."' error=".$db->lasterror(), LOG_ERR);
172 sleep(1);
173 $_SESSION["dol_loginmesg"] = $db->lasterror();
174 }
175 } else {
176 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO Too many attempts", LOG_NOTICE);
177 sleep(1); // Anti brut force protection. Must be same delay when password is not valid
178 // Load translation files required by the page
179 $langs->loadLangs(array('main', 'errors'));
180 $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorTooManyAttempts");
181 }
182 }
183 return $login;
184}
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
getUserRemoteIP()
Return the IP of remote user.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
check_user_password_dolibarr($usertotest, $passwordtotest, $entitytotest=1)
Check validity of user/password/entity If test is ko, reason must be filled into $_SESSION["dol_login...
dol_verifyHash($chain, $hash, $type='0')
Compute a hash and compare it to the given one For backward compatibility reasons,...