12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410 |
- <?php
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\SMTP;
- use PHPMailer\PHPMailer\Exception;
- function is_valid_regex($exp) {
- return @preg_match($exp, '') !== false;
- }
- function isset_has_content($var) {
- if (isset($var) && $var != "") {
- return true;
- }
- else {
- return false;
- }
- }
- function readable_random_string($length = 8) {
- $string = '';
- $vowels = array('a', 'e', 'i', 'o', 'u');
- $consonants = array('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z');
- $max = $length / 2;
- for ($i = 1; $i <= $max; $i++) {
- $string .= $consonants[rand(0,19)];
- $string .= $vowels[rand(0,4)];
- }
- return $string;
- }
- // Validates ips and cidrs
- function valid_network($network) {
- if (filter_var($network, FILTER_VALIDATE_IP)) {
- return true;
- }
- $parts = explode('/', $network);
- if (count($parts) != 2) {
- return false;
- }
- $ip = $parts[0];
- $netmask = $parts[1];
- if (!preg_match("/^\d+$/", $netmask)){
- return false;
- }
- $netmask = intval($parts[1]);
- if ($netmask < 0) {
- return false;
- }
- if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
- return $netmask <= 32;
- }
- if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
- return $netmask <= 128;
- }
- return false;
- }
- function valid_hostname($hostname) {
- return filter_var($hostname, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
- }
- // Thanks to https://stackoverflow.com/a/49373789
- // Validates exact ip matches and ip-in-cidr, ipv4 and ipv6
- function ip_acl($ip, $networks) {
- foreach($networks as $network) {
- if (filter_var($network, FILTER_VALIDATE_IP)) {
- if ($ip == $network) {
- return true;
- }
- else {
- continue;
- }
- }
- $ipb = inet_pton($ip);
- $iplen = strlen($ipb);
- if (strlen($ipb) < 4) {
- continue;
- }
- $ar = explode('/', $network);
- $ip1 = $ar[0];
- $ip1b = inet_pton($ip1);
- $ip1len = strlen($ip1b);
- if ($ip1len != $iplen) {
- continue;
- }
- if (count($ar)>1) {
- $bits=(int)($ar[1]);
- }
- else {
- $bits = $iplen * 8;
- }
- for ($c=0; $bits>0; $c++) {
- $bytemask = ($bits < 8) ? 0xff ^ ((1 << (8-$bits))-1) : 0xff;
- if (((ord($ipb[$c]) ^ ord($ip1b[$c])) & $bytemask) != 0) {
- continue 2;
- }
- $bits-=8;
- }
- return true;
- }
- return false;
- }
- function hash_password($password) {
- // default_pass_scheme is determined in vars.inc.php (or corresponding local file)
- // in case default pass scheme is not defined, falling back to BLF-CRYPT.
- global $default_pass_scheme;
- $pw_hash = NULL;
- // support pre-hashed passwords
- if (preg_match('/^{(ARGON2I|ARGON2ID|BLF-CRYPT|CLEAR|CLEARTEXT|CRYPT|DES-CRYPT|LDAP-MD5|MD5|MD5-CRYPT|PBKDF2|PLAIN|PLAIN-MD4|PLAIN-MD5|PLAIN-TRUNC|PLAIN-TRUNC|SHA|SHA1|SHA256|SHA256-CRYPT|SHA512|SHA512-CRYPT|SMD5|SSHA|SSHA256|SSHA512)}/i', $password)) {
- $pw_hash = $password;
- }
- else {
- switch (strtoupper($default_pass_scheme)) {
- case "SSHA":
- $salt_str = bin2hex(openssl_random_pseudo_bytes(8));
- $pw_hash = "{SSHA}".base64_encode(hash('sha1', $password . $salt_str, true) . $salt_str);
- break;
- case "SSHA256":
- $salt_str = bin2hex(openssl_random_pseudo_bytes(8));
- $pw_hash = "{SSHA256}".base64_encode(hash('sha256', $password . $salt_str, true) . $salt_str);
- break;
- case "SSHA512":
- $salt_str = bin2hex(openssl_random_pseudo_bytes(8));
- $pw_hash = "{SSHA512}".base64_encode(hash('sha512', $password . $salt_str, true) . $salt_str);
- break;
- case "BLF-CRYPT":
- default:
- $pw_hash = "{BLF-CRYPT}" . password_hash($password, PASSWORD_BCRYPT);
- break;
- }
- }
- return $pw_hash;
- }
- function password_complexity($_action, $_data = null) {
- global $redis;
- global $lang;
- switch ($_action) {
- case 'edit':
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_data),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $is_now = password_complexity('get');
- if (!empty($is_now)) {
- $length = (isset($_data['length']) && intval($_data['length']) >= 3) ? intval($_data['length']) : $is_now['length'];
- $chars = (isset($_data['chars'])) ? intval($_data['chars']) : $is_now['chars'];
- $lowerupper = (isset($_data['lowerupper'])) ? intval($_data['lowerupper']) : $is_now['lowerupper'];
- $special_chars = (isset($_data['special_chars'])) ? intval($_data['special_chars']) : $is_now['special_chars'];
- $numbers = (isset($_data['numbers'])) ? intval($_data['numbers']) : $is_now['numbers'];
- }
- try {
- $redis->hMSet('PASSWD_POLICY', [
- 'length' => $length,
- 'chars' => $chars,
- 'special_chars' => $special_chars,
- 'lowerupper' => $lowerupper,
- 'numbers' => $numbers
- ]);
- }
- catch (RedisException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_data),
- 'msg' => array('redis_error', $e)
- );
- return false;
- }
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_action, $_data),
- 'msg' => 'password_policy_saved'
- );
- break;
- case 'get':
- try {
- $length = $redis->hGet('PASSWD_POLICY', 'length');
- $chars = $redis->hGet('PASSWD_POLICY', 'chars');
- $special_chars = $redis->hGet('PASSWD_POLICY', 'special_chars');
- $lowerupper = $redis->hGet('PASSWD_POLICY', 'lowerupper');
- $numbers = $redis->hGet('PASSWD_POLICY', 'numbers');
- return array(
- 'length' => $length,
- 'chars' => $chars,
- 'special_chars' => $special_chars,
- 'lowerupper' => $lowerupper,
- 'numbers' => $numbers
- );
- }
- catch (RedisException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_data),
- 'msg' => array('redis_error', $e)
- );
- return false;
- }
- return false;
- break;
- case 'html':
- $policies = password_complexity('get');
- foreach ($policies as $name => $value) {
- if ($value != 0) {
- $policy_text[] = sprintf($lang['admin']["password_policy_$name"], $value);
- }
- }
- return '<p class="help-block small">- ' . implode('<br>- ', (array)$policy_text) . '</p>';
- break;
- }
- }
- function password_check($password1, $password2) {
- $password_complexity = password_complexity('get');
- if (empty($password1) || empty($password2)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_type),
- 'msg' => 'password_complexity'
- );
- return false;
- }
- if ($password1 != $password2) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_type),
- 'msg' => 'password_mismatch'
- );
- return false;
- }
- $given_password['length'] = strlen($password1);
- $given_password['special_chars'] = preg_match('/[^a-zA-Z\d]/', $password1);
- $given_password['chars'] = preg_match('/[a-zA-Z]/',$password1);
- $given_password['numbers'] = preg_match('/\d/', $password1);
- $lower = strlen(preg_replace("/[^a-z]/", '', $password1));
- $upper = strlen(preg_replace("/[^A-Z]/", '', $password1));
- $given_password['lowerupper'] = ($lower > 0 && $upper > 0) ? true : false;
- if (
- ($given_password['length'] < $password_complexity['length']) ||
- ($password_complexity['special_chars'] == 1 && (intval($given_password['special_chars']) != $password_complexity['special_chars'])) ||
- ($password_complexity['chars'] == 1 && (intval($given_password['chars']) != $password_complexity['chars'])) ||
- ($password_complexity['numbers'] == 1 && (intval($given_password['numbers']) != $password_complexity['numbers'])) ||
- ($password_complexity['lowerupper'] == 1 && (intval($given_password['lowerupper']) != $password_complexity['lowerupper']))
- ) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_type),
- 'msg' => 'password_complexity'
- );
- return false;
- }
- return true;
- }
- function last_login($action, $username, $sasl_limit_days = 7, $ui_offset = 1) {
- global $pdo;
- global $redis;
- $sasl_limit_days = intval($sasl_limit_days);
- switch ($action) {
- case 'get':
- if (filter_var($username, FILTER_VALIDATE_EMAIL) && hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
- $stmt = $pdo->prepare('SELECT `real_rip`, MAX(`datetime`) as `datetime`, `service`, `app_password`, MAX(`app_passwd`.`name`) as `app_password_name` FROM `sasl_log`
- LEFT OUTER JOIN `app_passwd` on `sasl_log`.`app_password` = `app_passwd`.`id`
- WHERE `username` = :username
- AND HOUR(TIMEDIFF(NOW(), `datetime`)) < :sasl_limit_days
- GROUP BY `real_rip`, `service`, `app_password`
- ORDER BY `datetime` DESC;');
- $stmt->execute(array(':username' => $username, ':sasl_limit_days' => ($sasl_limit_days * 24)));
- $sasl = $stmt->fetchAll(PDO::FETCH_ASSOC);
- foreach ($sasl as $k => $v) {
- if (!filter_var($sasl[$k]['real_rip'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
- $sasl[$k]['real_rip'] = 'Web/EAS/Internal (' . $sasl[$k]['real_rip'] . ')';
- }
- elseif (filter_var($sasl[$k]['real_rip'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
- try {
- $sasl[$k]['location'] = $redis->hGet('IP_SHORTCOUNTRY', $sasl[$k]['real_rip']);
- }
- catch (RedisException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_data_log),
- 'msg' => array('redis_error', $e)
- );
- return false;
- }
- if (!$sasl[$k]['location']) {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL,"https://dfdata.bella.network/country/" . $sasl[$k]['real_rip']);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_USERAGENT, 'Moocow');
- curl_setopt($curl, CURLOPT_TIMEOUT, 5);
- $ip_data = curl_exec($curl);
- if (!curl_errno($curl)) {
- $ip_data_array = json_decode($ip_data, true);
- if ($ip_data_array !== false and !empty($ip_data_array['shortcountry'])) {
- $sasl[$k]['location'] = $ip_data_array['shortcountry'];
- try {
- $redis->hSet('IP_SHORTCOUNTRY', $sasl[$k]['real_rip'], $ip_data_array['shortcountry']);
- }
- catch (RedisException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_data_log),
- 'msg' => array('redis_error', $e)
- );
- curl_close($curl);
- return false;
- }
- }
- }
- curl_close($curl);
- }
- }
- }
- }
- else {
- $sasl = array();
- }
- if ($_SESSION['mailcow_cc_role'] == "admin" || $username == $_SESSION['mailcow_cc_username']) {
- $stmt = $pdo->prepare('SELECT `remote`, `time` FROM `logs`
- WHERE JSON_EXTRACT(`call`, "$[0]") = "check_login"
- AND JSON_EXTRACT(`call`, "$[1]") = :username
- AND `type` = "success" ORDER BY `time` DESC LIMIT 1 OFFSET :offset');
- $stmt->execute(array(
- ':username' => $username,
- ':offset' => $ui_offset
- ));
- $ui = $stmt->fetch(PDO::FETCH_ASSOC);
- }
- else {
- $ui = array();
- }
- return array('ui' => $ui, 'sasl' => $sasl);
- break;
- case 'reset':
- if (filter_var($username, FILTER_VALIDATE_EMAIL) && hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
- $stmt = $pdo->prepare('DELETE FROM `sasl_log`
- WHERE `username` = :username');
- $stmt->execute(array(':username' => $username));
- }
- if ($_SESSION['mailcow_cc_role'] == "admin" || $username == $_SESSION['mailcow_cc_username']) {
- $stmt = $pdo->prepare('DELETE FROM `logs`
- WHERE JSON_EXTRACT(`call`, "$[0]") = "check_login"
- AND JSON_EXTRACT(`call`, "$[1]") = :username
- AND `type` = "success"');
- $stmt->execute(array(':username' => $username));
- }
- return true;
- break;
- }
- }
- function flush_memcached() {
- try {
- $m = new Memcached();
- $m->addServer('memcached', 11211);
- $m->flush();
- }
- catch ( Exception $e ) {
- // Dunno
- }
- }
- function sys_mail($_data) {
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $excludes = $_data['mass_exclude'];
- $includes = $_data['mass_include'];
- $mailboxes = array();
- $mass_from = $_data['mass_from'];
- $mass_text = $_data['mass_text'];
- $mass_html = $_data['mass_html'];
- $mass_subject = $_data['mass_subject'];
- if (!filter_var($mass_from, FILTER_VALIDATE_EMAIL)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => 'from_invalid'
- );
- return false;
- }
- if (empty($mass_subject)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => 'subject_empty'
- );
- return false;
- }
- if (empty($mass_text)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => 'text_empty'
- );
- return false;
- }
- $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
- foreach ($domains as $domain) {
- foreach (mailbox('get', 'mailboxes', $domain) as $mailbox) {
- $mailboxes[] = $mailbox;
- }
- }
- if (!empty($includes)) {
- $rcpts = array_intersect($mailboxes, $includes);
- }
- elseif (!empty($excludes)) {
- $rcpts = array_diff($mailboxes, $excludes);
- }
- else {
- $rcpts = $mailboxes;
- }
- if (!empty($rcpts)) {
- ini_set('max_execution_time', 0);
- ini_set('max_input_time', 0);
- $mail = new PHPMailer;
- $mail->Timeout = 10;
- $mail->SMTPOptions = array(
- 'ssl' => array(
- 'verify_peer' => false,
- 'verify_peer_name' => false,
- 'allow_self_signed' => true
- )
- );
- $mail->isSMTP();
- $mail->Host = 'dovecot-mailcow';
- $mail->SMTPAuth = false;
- $mail->Port = 24;
- $mail->setFrom($mass_from);
- $mail->Subject = $mass_subject;
- $mail->CharSet ="UTF-8";
- if (!empty($mass_html)) {
- $mail->Body = $mass_html;
- $mail->AltBody = $mass_text;
- }
- else {
- $mail->Body = $mass_text;
- }
- $mail->XMailer = 'MooMassMail';
- foreach ($rcpts as $rcpt) {
- $mail->AddAddress($rcpt);
- if (!$mail->send()) {
- $_SESSION['return'][] = array(
- 'type' => 'warning',
- 'log' => array(__FUNCTION__),
- 'msg' => 'Mailer error (RCPT "' . htmlspecialchars($rcpt) . '"): ' . str_replace('https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting', '', $mail->ErrorInfo)
- );
- }
- $mail->ClearAllRecipients();
- }
- }
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__),
- 'msg' => 'Mass mail job completed, sent ' . count($rcpts) . ' mails'
- );
- }
- function logger($_data = false) {
- /*
- logger() will be called as last function
- To manually log a message, logger needs to be called like below.
- logger(array(
- 'return' => array(
- array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => $err
- )
- )
- ));
- These messages will not be printed as alert box.
- To do so, push them to $_SESSION['return'] and do not call logger as they will be included automatically:
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $user, '*'),
- 'msg' => $err
- );
- */
- global $pdo;
- if (!$_data) {
- $_data = $_SESSION;
- }
- if (!empty($_data['return'])) {
- $task = substr(strtoupper(md5(uniqid(rand(), true))), 0, 6);
- foreach ($_data['return'] as $return) {
- $type = $return['type'];
- $msg = null;
- if (isset($return['msg'])) {
- $msg = json_encode($return['msg'], JSON_UNESCAPED_UNICODE);
- }
- $call = null;
- if (isset($return['log'])) {
- $call = json_encode($return['log'], JSON_UNESCAPED_UNICODE);
- }
- if (!empty($_SESSION["dual-login"]["username"])) {
- $user = $_SESSION["dual-login"]["username"] . ' => ' . $_SESSION['mailcow_cc_username'];
- $role = $_SESSION["dual-login"]["role"] . ' => ' . $_SESSION['mailcow_cc_role'];
- }
- elseif (!empty($_SESSION['mailcow_cc_username'])) {
- $user = $_SESSION['mailcow_cc_username'];
- $role = $_SESSION['mailcow_cc_role'];
- }
- else {
- $user = 'unauthenticated';
- $role = 'unauthenticated';
- }
- // We cannot log when logs is missing...
- try {
- $stmt = $pdo->prepare("INSERT INTO `logs` (`type`, `task`, `msg`, `call`, `user`, `role`, `remote`, `time`) VALUES
- (:type, :task, :msg, :call, :user, :role, :remote, UNIX_TIMESTAMP())");
- $stmt->execute(array(
- ':type' => $type,
- ':task' => $task,
- ':call' => $call,
- ':msg' => $msg,
- ':user' => $user,
- ':role' => $role,
- ':remote' => get_remote_ip()
- ));
- }
- catch (PDOException $e) {
- # handle the exception here, as the exception handler function results in a white page
- error_log($e->getMessage(), 0);
- }
- }
- }
- else {
- return true;
- }
- }
- function hasDomainAccess($username, $role, $domain) {
- global $pdo;
- if (empty($domain) || !is_valid_domain_name($domain)) {
- return false;
- }
- if (isset($_SESSION['access_all_exception']) && $_SESSION['access_all_exception'] == "1") {
- return true;
- }
- if (!filter_var($username, FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $username))) {
- return false;
- }
- if ($role != 'admin' && $role != 'domainadmin') {
- return false;
- }
- if ($role == 'admin') {
- $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
- WHERE `domain` = :domain");
- $stmt->execute(array(':domain' => $domain));
- $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
- $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
- WHERE `alias_domain` = :domain");
- $stmt->execute(array(':domain' => $domain));
- $num_results = $num_results + count($stmt->fetchAll(PDO::FETCH_ASSOC));
- if ($num_results != 0) {
- return true;
- }
- }
- elseif ($role == 'domainadmin') {
- $stmt = $pdo->prepare("SELECT `domain` FROM `domain_admins`
- WHERE (
- `active`='1'
- AND `username` = :username
- AND (`domain` = :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2))
- )");
- $stmt->execute(array(':username' => $username, ':domain1' => $domain, ':domain2' => $domain));
- $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
- if (!empty($num_results)) {
- return true;
- }
- }
- return false;
- }
- function hasMailboxObjectAccess($username, $role, $object) {
- global $pdo;
- if (isset($_SESSION['access_all_exception']) && $_SESSION['access_all_exception'] == "1") {
- return true;
- }
- if (empty($username) || empty($role) || empty($object)) {
- return false;
- }
- if (!filter_var(html_entity_decode(rawurldecode($username)), FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $username))) {
- return false;
- }
- if ($role != 'admin' && $role != 'domainadmin' && $role != 'user') {
- return false;
- }
- if ($username == $object) {
- return true;
- }
- $stmt = $pdo->prepare("SELECT `domain` FROM `mailbox` WHERE `username` = :object");
- $stmt->execute(array(':object' => $object));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (isset($row['domain']) && hasDomainAccess($username, $role, $row['domain'])) {
- return true;
- }
- return false;
- }
- // does also verify mailboxes as a mailbox is a alias == goto
- function hasAliasObjectAccess($username, $role, $object) {
- global $pdo;
- if (isset($_SESSION['access_all_exception']) && $_SESSION['access_all_exception'] == "1") {
- return true;
- }
- if (empty($username) || empty($role) || empty($object)) {
- return false;
- }
- if (!filter_var(html_entity_decode(rawurldecode($username)), FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $username))) {
- return false;
- }
- if ($role != 'admin' && $role != 'domainadmin' && $role != 'user') {
- return false;
- }
- $stmt = $pdo->prepare("SELECT `domain` FROM `alias` WHERE `address` = :object");
- $stmt->execute(array(':object' => $object));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (isset($row['domain']) && hasDomainAccess($username, $role, $row['domain'])) {
- return true;
- }
- return false;
- }
- function hasACLAccess($type) {
- if (isset($_SESSION['access_all_exception']) && $_SESSION['access_all_exception'] == "1") {
- return true;
- }
- if (isset($_SESSION['acl'][$type]) && $_SESSION['acl'][$type] == "1") {
- return true;
- }
- return false;
- }
- function pem_to_der($pem_key) {
- // Need to remove BEGIN/END PUBLIC KEY
- $lines = explode("\n", trim($pem_key));
- unset($lines[count($lines)-1]);
- unset($lines[0]);
- return base64_decode(implode('', $lines));
- }
- function expand_ipv6($ip) {
- $hex = unpack("H*hex", inet_pton($ip));
- $ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);
- return $ip;
- }
- function generate_tlsa_digest($hostname, $port, $starttls = null) {
- if (!is_valid_domain_name($hostname)) {
- return "Not a valid hostname";
- }
- if (empty($starttls)) {
- $context = stream_context_create(array("ssl" => array("capture_peer_cert" => true, 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true)));
- $stream = stream_socket_client('ssl://' . $hostname . ':' . $port, $error_nr, $error_msg, 5, STREAM_CLIENT_CONNECT, $context);
- if (!$stream) {
- $error_msg = isset($error_msg) ? $error_msg : '-';
- return $error_nr . ': ' . $error_msg;
- }
- }
- else {
- $stream = stream_socket_client('tcp://' . $hostname . ':' . $port, $error_nr, $error_msg, 5);
- if (!$stream) {
- return $error_nr . ': ' . $error_msg;
- }
- $banner = fread($stream, 512 );
- if (preg_match("/^220/i", $banner)) { // SMTP
- fwrite($stream,"HELO tlsa.generator.local\r\n");
- fread($stream, 512);
- fwrite($stream,"STARTTLS\r\n");
- fread($stream, 512);
- }
- elseif (preg_match("/imap.+starttls/i", $banner)) { // IMAP
- fwrite($stream,"A1 STARTTLS\r\n");
- fread($stream, 512);
- }
- elseif (preg_match("/^\+OK/", $banner)) { // POP3
- fwrite($stream,"STLS\r\n");
- fread($stream, 512);
- }
- elseif (preg_match("/^OK/m", $banner)) { // Sieve
- fwrite($stream,"STARTTLS\r\n");
- fread($stream, 512);
- }
- else {
- return 'Unknown banner: "' . htmlspecialchars(trim($banner)) . '"';
- }
- // Upgrade connection
- stream_set_blocking($stream, true);
- stream_context_set_option($stream, 'ssl', 'capture_peer_cert', true);
- stream_context_set_option($stream, 'ssl', 'verify_peer', false);
- stream_context_set_option($stream, 'ssl', 'verify_peer_name', false);
- stream_context_set_option($stream, 'ssl', 'allow_self_signed', true);
- stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_ANY_CLIENT);
- stream_set_blocking($stream, false);
- }
- $params = stream_context_get_params($stream);
- if (!empty($params['options']['ssl']['peer_certificate'])) {
- $key_resource = openssl_pkey_get_public($params['options']['ssl']['peer_certificate']);
- // We cannot get ['rsa']['n'], the binary data would contain BEGIN/END PUBLIC KEY
- $key_data = openssl_pkey_get_details($key_resource)['key'];
- return '3 1 1 ' . openssl_digest(pem_to_der($key_data), 'sha256');
- }
- else {
- return 'Error: Cannot read peer certificate';
- }
- }
- function alertbox_log_parser($_data) {
- global $lang;
- if (isset($_data['return'])) {
- foreach ($_data['return'] as $return) {
- // Get type
- $type = $return['type'];
- // If a lang[type][msg] string exists, use it as message
- if (isset($return['type']) && isset($return['msg']) && !is_array($return['msg'])) {
- if (isset($lang[$return['type']][$return['msg']])) {
- $msg = $lang[$return['type']][$return['msg']];
- }
- else {
- $msg = $return['msg'];
- }
- }
- // If msg is an array, use first element as language string and run printf on it with remaining array elements
- elseif (is_array($return['msg'])) {
- $msg = array_shift($return['msg']);
- $msg = vsprintf(
- $lang[$return['type']][$msg],
- $return['msg']
- );
- }
- else {
- $msg = '-';
- }
- $log_array[] = array('msg' => $msg, 'type' => json_encode($type));
- }
- if (!empty($log_array)) {
- return $log_array;
- }
- }
- return false;
- }
- function verify_salted_hash($hash, $password, $algo, $salt_length) {
- // Decode hash
- $dhash = base64_decode($hash);
- // Get first n bytes of binary which equals a SSHA hash
- $ohash = substr($dhash, 0, $salt_length);
- // Remove SSHA hash from decoded hash to get original salt string
- $osalt = str_replace($ohash, '', $dhash);
- // Check single salted SSHA hash against extracted hash
- if (hash_equals(hash($algo, $password . $osalt, true), $ohash)) {
- return true;
- }
- return false;
- }
- function verify_hash($hash, $password) {
- if (preg_match('/^{(.+)}(.+)/i', $hash, $hash_array)) {
- $scheme = strtoupper($hash_array[1]);
- $hash = $hash_array[2];
- switch ($scheme) {
- case "ARGON2I":
- case "ARGON2ID":
- case "BLF-CRYPT":
- case "CRYPT":
- case "DES-CRYPT":
- case "MD5-CRYPT":
- case "MD5":
- case "SHA256-CRYPT":
- case "SHA512-CRYPT":
- return password_verify($password, $hash);
- case "CLEAR":
- case "CLEARTEXT":
- case "PLAIN":
- return $password == $hash;
- case "LDAP-MD5":
- $hash = base64_decode($hash);
- return hash_equals(hash('md5', $password, true), $hash);
- case "PBKDF2":
- $components = explode('$', $hash);
- $salt = $components[2];
- $rounds = $components[3];
- $hash = $components[4];
- return hash_equals(hash_pbkdf2('sha1', $password, $salt, $rounds), $hash);
- case "PLAIN-MD4":
- return hash_equals(hash('md4', $password), $hash);
- case "PLAIN-MD5":
- return md5($password) == $hash;
- case "PLAIN-TRUNC":
- $components = explode('-', $hash);
- if (count($components) > 1) {
- $trunc_len = $components[0];
- $trunc_password = $components[1];
- return substr($password, 0, $trunc_len) == $trunc_password;
- } else {
- return $password == $hash;
- }
- case "SHA":
- case "SHA1":
- case "SHA256":
- case "SHA512":
- // SHA is an alias for SHA1
- $scheme = $scheme == "SHA" ? "sha1" : strtolower($scheme);
- $hash = base64_decode($hash);
- return hash_equals(hash($scheme, $password, true), $hash);
- case "SMD5":
- return verify_salted_hash($hash, $password, 'md5', 16);
- case "SSHA":
- return verify_salted_hash($hash, $password, 'sha1', 20);
- case "SSHA256":
- return verify_salted_hash($hash, $password, 'sha256', 32);
- case "SSHA512":
- return verify_salted_hash($hash, $password, 'sha512', 64);
- default:
- return false;
- }
- }
- return false;
- }
- function formatBytes($size, $precision = 2) {
- if(!is_numeric($size)) {
- return "0";
- }
- $base = log($size, 1024);
- $suffixes = array(' Byte', ' KiB', ' MiB', ' GiB', ' TiB');
- if ($size == "0") {
- return "0";
- }
- return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
- }
- function update_sogo_static_view($mailbox = null) {
- if (getenv('SKIP_SOGO') == "y") {
- return true;
- }
- global $pdo;
- global $lang;
- $mailbox_exists = false;
- if ($mailbox !== null) {
- // Check if the mailbox exists
- $stmt = $pdo->prepare("SELECT username FROM mailbox WHERE username = :mailbox AND active = '1'");
- $stmt->execute(array(':mailbox' => $mailbox));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if ($row){
- $mailbox_exists = true;
- }
- }
- // generate random password for sogo to deny direct login
- $random_password = base64_encode(openssl_random_pseudo_bytes(24));
- $random_salt = base64_encode(openssl_random_pseudo_bytes(16));
- $random_hash = '{SSHA256}' . base64_encode(hash('sha256', base64_decode($password) . $salt, true) . $salt);
- $subquery = "GROUP BY mailbox.username";
- if ($mailbox_exists) {
- $subquery = "AND mailbox.username = :mailbox";
- }
- $query = "INSERT INTO _sogo_static_view (`c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings`)
- SELECT
- mailbox.username,
- mailbox.domain,
- mailbox.username,
- :random_hash,
- mailbox.name,
- mailbox.username,
- IFNULL(GROUP_CONCAT(ga.aliases ORDER BY ga.aliases SEPARATOR ' '), ''),
- IFNULL(gda.ad_alias, ''),
- IFNULL(external_acl.send_as_acl, ''),
- mailbox.kind,
- mailbox.multiple_bookings
- FROM
- mailbox
- LEFT OUTER JOIN grouped_mail_aliases ga ON ga.username REGEXP CONCAT('(^|,)', mailbox.username, '($|,)')
- LEFT OUTER JOIN grouped_domain_alias_address gda ON gda.username = mailbox.username
- LEFT OUTER JOIN grouped_sender_acl_external external_acl ON external_acl.username = mailbox.username
- WHERE
- mailbox.active = '1'
- $subquery
- ON DUPLICATE KEY UPDATE
- `domain` = VALUES(`domain`),
- `c_name` = VALUES(`c_name`),
- `c_password` = VALUES(`c_password`),
- `c_cn` = VALUES(`c_cn`),
- `mail` = VALUES(`mail`),
- `aliases` = VALUES(`aliases`),
- `ad_aliases` = VALUES(`ad_aliases`),
- `ext_acl` = VALUES(`ext_acl`),
- `kind` = VALUES(`kind`),
- `multiple_bookings` = VALUES(`multiple_bookings`)";
- if ($mailbox_exists) {
- $stmt = $pdo->prepare($query);
- $stmt->execute(array(
- ':random_hash' => $random_hash,
- ':mailbox' => $mailbox
- ));
- } else {
- $stmt = $pdo->prepare($query);
- $stmt->execute(array(
- ':random_hash' => $random_hash
- ));
- }
- $stmt = $pdo->query("DELETE FROM _sogo_static_view WHERE `c_uid` NOT IN (SELECT `username` FROM `mailbox` WHERE `active` = '1');");
- flush_memcached();
- }
- function edit_user_account($_data) {
- global $lang;
- global $pdo;
- $_data_log = $_data;
- !isset($_data_log['user_new_pass']) ?: $_data_log['user_new_pass'] = '*';
- !isset($_data_log['user_new_pass2']) ?: $_data_log['user_new_pass2'] = '*';
- !isset($_data_log['user_old_pass']) ?: $_data_log['user_old_pass'] = '*';
- $username = $_SESSION['mailcow_cc_username'];
- $role = $_SESSION['mailcow_cc_role'];
- $password_old = $_data['user_old_pass'];
- $pw_recovery_email = $_data['pw_recovery_email'];
- if (filter_var($username, FILTER_VALIDATE_EMAIL === false) || $role != 'user') {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => 'access_denied'
- );
- return false;
- }
- // edit password
- if (!empty($password_old) && !empty($_data['user_new_pass']) && !empty($_data['user_new_pass2'])) {
- $stmt = $pdo->prepare("SELECT `password` FROM `mailbox`
- WHERE `kind` NOT REGEXP 'location|thing|group'
- AND `username` = :user AND authsource = 'mailcow'");
- $stmt->execute(array(':user' => $username));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (!verify_hash($row['password'], $password_old)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $password_new = $_data['user_new_pass'];
- $password_new2 = $_data['user_new_pass2'];
- if (password_check($password_new, $password_new2) !== true) {
- return false;
- }
- $password_hashed = hash_password($password_new);
- $stmt = $pdo->prepare("UPDATE `mailbox` SET `password` = :password_hashed,
- `attributes` = JSON_SET(`attributes`, '$.force_pw_update', '0'),
- `attributes` = JSON_SET(`attributes`, '$.passwd_update', NOW())
- WHERE `username` = :username AND authsource = 'mailcow'");
- $stmt->execute(array(
- ':password_hashed' => $password_hashed,
- ':username' => $username
- ));
- update_sogo_static_view();
- }
- // edit password recovery email
- elseif (isset($pw_recovery_email)) {
- if (!isset($_SESSION['acl']['pw_reset']) || $_SESSION['acl']['pw_reset'] != "1" ) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $pw_recovery_email = (!filter_var($pw_recovery_email, FILTER_VALIDATE_EMAIL)) ? '' : $pw_recovery_email;
- $stmt = $pdo->prepare("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.recovery_email', :recovery_email)
- WHERE `username` = :username AND authsource = 'mailcow'");
- $stmt->execute(array(
- ':recovery_email' => $pw_recovery_email,
- ':username' => $username
- ));
- }
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('mailbox_modified', htmlspecialchars($username))
- );
- }
- function user_get_alias_details($username) {
- global $pdo;
- global $lang;
- $data['direct_aliases'] = array();
- $data['shared_aliases'] = array();
- if ($_SESSION['mailcow_cc_role'] == "user") {
- $username = $_SESSION['mailcow_cc_username'];
- }
- if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
- return false;
- }
- if (!hasMailboxObjectAccess($username, $_SESSION['mailcow_cc_role'], $username)) {
- return false;
- }
- $data['address'] = $username;
- $stmt = $pdo->prepare("SELECT `address` AS `shared_aliases`, `public_comment` FROM `alias`
- WHERE `goto` REGEXP :username_goto
- AND `address` NOT LIKE '@%'
- AND `goto` != :username_goto2
- AND `address` != :username_address");
- $stmt->execute(array(
- ':username_goto' => '(^|,)'.preg_quote($username, '/').'($|,)',
- ':username_goto2' => $username,
- ':username_address' => $username
- ));
- $run = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($run)) {
- $data['shared_aliases'][$row['shared_aliases']]['public_comment'] = htmlspecialchars($row['public_comment']);
- //$data['shared_aliases'][] = $row['shared_aliases'];
- }
- $stmt = $pdo->prepare("SELECT `address` AS `direct_aliases`, `public_comment` FROM `alias`
- WHERE `goto` = :username_goto
- AND `address` NOT LIKE '@%'
- AND `address` != :username_address");
- $stmt->execute(
- array(
- ':username_goto' => $username,
- ':username_address' => $username
- ));
- $run = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($run)) {
- $data['direct_aliases'][$row['direct_aliases']]['public_comment'] = htmlspecialchars($row['public_comment']);
- }
- $stmt = $pdo->prepare("SELECT CONCAT(local_part, '@', alias_domain) AS `ad_alias`, `alias_domain` FROM `mailbox`
- LEFT OUTER JOIN `alias_domain` on `target_domain` = `domain`
- WHERE `username` = :username ;");
- $stmt->execute(array(':username' => $username));
- $run = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($run)) {
- if (empty($row['ad_alias'])) {
- continue;
- }
- $data['direct_aliases'][$row['ad_alias']]['public_comment'] = $lang['add']['alias_domain'];
- $data['alias_domains'][] = $row['alias_domain'];
- }
- $stmt = $pdo->prepare("SELECT IFNULL(GROUP_CONCAT(`send_as` SEPARATOR ', '), '') AS `send_as` FROM `sender_acl` WHERE `logged_in_as` = :username AND `send_as` NOT LIKE '@%';");
- $stmt->execute(array(':username' => $username));
- $run = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($run)) {
- $data['aliases_also_send_as'] = $row['send_as'];
- }
- $stmt = $pdo->prepare("SELECT CONCAT_WS(', ', IFNULL(GROUP_CONCAT(DISTINCT `send_as` SEPARATOR ', '), ''), GROUP_CONCAT(DISTINCT CONCAT('@',`alias_domain`) SEPARATOR ', ')) AS `send_as` FROM `sender_acl` LEFT JOIN `alias_domain` ON `alias_domain`.`target_domain` = TRIM(LEADING '@' FROM `send_as`) WHERE `logged_in_as` = :username AND `send_as` LIKE '@%';");
- $stmt->execute(array(':username' => $username));
- $run = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($run)) {
- $data['aliases_send_as_all'] = $row['send_as'];
- }
- $stmt = $pdo->prepare("SELECT IFNULL(GROUP_CONCAT(`address` SEPARATOR ', '), '') as `address` FROM `alias` WHERE `goto` REGEXP :username AND `address` LIKE '@%';");
- $stmt->execute(array(':username' => '(^|,)'.preg_quote($username, '/').'($|,)'));
- $run = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while ($row = array_shift($run)) {
- $data['is_catch_all'] = $row['address'];
- }
- return $data;
- }
- function is_valid_domain_name($domain_name) {
- if (empty($domain_name)) {
- return false;
- }
- $domain_name = idn_to_ascii($domain_name, 0, INTL_IDNA_VARIANT_UTS46);
- return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domain_name)
- && preg_match("/^.{1,253}$/", $domain_name)
- && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name));
- }
- function set_tfa($_data) {
- global $pdo;
- global $yubi;
- global $tfa;
- global $iam_settings;
- $_data_log = $_data;
- $access_denied = null;
- !isset($_data_log['confirm_password']) ?: $_data_log['confirm_password'] = '*';
- $username = $_SESSION['mailcow_cc_username'];
- // check for empty user and role
- if (!isset($_SESSION['mailcow_cc_role']) || empty($username)) $access_denied = true;
- // check admin confirm password
- if ($access_denied === null) {
- $stmt = $pdo->prepare("SELECT `password` FROM `admin`
- WHERE `username` = :username");
- $stmt->execute(array(':username' => $username));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if ($row) {
- if (!verify_hash($row['password'], $_data["confirm_password"])) $access_denied = true;
- else $access_denied = false;
- }
- }
- // check mailbox confirm password
- if ($access_denied === null) {
- $stmt = $pdo->prepare("SELECT `password`, `authsource` FROM `mailbox`
- WHERE `username` = :username");
- $stmt->execute(array(':username' => $username));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if ($row) {
- if ($row['authsource'] == 'ldap'){
- if (!ldap_mbox_login($username, $_data["confirm_password"], $iam_settings)) $access_denied = true;
- else $access_denied = false;
- } else {
- if (!verify_hash($row['password'], $_data["confirm_password"])) $access_denied = true;
- else $access_denied = false;
- }
- }
- }
- // set access_denied error
- if ($access_denied){
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => 'access_denied'
- );
- return false;
- }
- switch ($_data["tfa_method"]) {
- case "yubi_otp":
- $key_id = (!isset($_data["key_id"])) ? 'unidentified' : $_data["key_id"];
- $yubico_id = $_data['yubico_id'];
- $yubico_key = $_data['yubico_key'];
- $yubi = new Auth_Yubico($yubico_id, $yubico_key);
- if (!$yubi) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => 'access_denied'
- );
- return false;
- }
- if (!ctype_alnum($_data["otp_token"]) || strlen($_data["otp_token"]) != 44) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => 'tfa_token_invalid'
- );
- return false;
- }
- $yauth = $yubi->verify($_data["otp_token"]);
- if (PEAR::isError($yauth)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('yotp_verification_failed', $yauth->getMessage())
- );
- return false;
- }
- try {
- // We could also do a modhex translation here
- $yubico_modhex_id = substr($_data["otp_token"], 0, 12);
- $stmt = $pdo->prepare("DELETE FROM `tfa`
- WHERE `username` = :username
- AND (`authmech` = 'yubi_otp' AND `secret` LIKE :modhex)");
- $stmt->execute(array(':username' => $username, ':modhex' => '%' . $yubico_modhex_id));
- $stmt = $pdo->prepare("INSERT INTO `tfa` (`key_id`, `username`, `authmech`, `active`, `secret`) VALUES
- (:key_id, :username, 'yubi_otp', '1', :secret)");
- $stmt->execute(array(':key_id' => $key_id, ':username' => $username, ':secret' => $yubico_id . ':' . $yubico_key . ':' . $yubico_modhex_id));
- }
- catch (PDOException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('mysql_error', $e)
- );
- return false;
- }
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('object_modified', htmlspecialchars($username))
- );
- break;
- case "totp":
- $key_id = (!isset($_data["key_id"])) ? 'unidentified' : $_data["key_id"];
- if ($tfa->verifyCode($_POST['totp_secret'], $_POST['totp_confirm_token']) === true) {
- //$stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username");
- //$stmt->execute(array(':username' => $username));
- $stmt = $pdo->prepare("INSERT INTO `tfa` (`username`, `key_id`, `authmech`, `secret`, `active`) VALUES (?, ?, 'totp', ?, '1')");
- $stmt->execute(array($username, $key_id, $_POST['totp_secret']));
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('object_modified', $username)
- );
- }
- else {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => 'totp_verification_failed'
- );
- }
- break;
- case "webauthn":
- $key_id = (!isset($_data["key_id"])) ? 'unidentified' : $_data["key_id"];
- $stmt = $pdo->prepare("INSERT INTO `tfa` (`username`, `key_id`, `authmech`, `keyHandle`, `publicKey`, `certificate`, `counter`, `active`)
- VALUES (?, ?, 'webauthn', ?, ?, ?, ?, '1')");
- $stmt->execute(array(
- $username,
- $key_id,
- base64_encode($_data['registration']->credentialId),
- $_data['registration']->credentialPublicKey,
- $_data['registration']->certificate,
- 0
- ));
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('object_modified', $username)
- );
- break;
- case "none":
- $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username");
- $stmt->execute(array(':username' => $username));
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('object_modified', htmlspecialchars($username))
- );
- break;
- }
- }
- function fido2($_data) {
- global $pdo;
- $_data_log = $_data;
- // Not logging registration data, only actions
- // Silent errors for "get" requests
- switch ($_data["action"]) {
- case "register":
- $username = $_SESSION['mailcow_cc_username'];
- if (!isset($_SESSION['mailcow_cc_role']) || empty($username)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data["action"]),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $stmt = $pdo->prepare("INSERT INTO `fido2` (`username`, `rpId`, `credentialPublicKey`, `certificateChain`, `certificate`, `certificateIssuer`, `certificateSubject`, `signatureCounter`, `AAGUID`, `credentialId`)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
- $stmt->execute(array(
- $username,
- $_data['registration']->rpId,
- $_data['registration']->credentialPublicKey,
- $_data['registration']->certificateChain,
- $_data['registration']->certificate,
- $_data['registration']->certificateIssuer,
- $_data['registration']->certificateSubject,
- $_data['registration']->signatureCounter,
- $_data['registration']->AAGUID,
- $_data['registration']->credentialId)
- );
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_data["action"]),
- 'msg' => array('object_modified', $username)
- );
- break;
- case "get_user_cids":
- // Used to exclude existing CredentialIds while registering
- $username = $_SESSION['mailcow_cc_username'];
- if (!isset($_SESSION['mailcow_cc_role']) || empty($username)) {
- return false;
- }
- $stmt = $pdo->prepare("SELECT `credentialId` FROM `fido2` WHERE `username` = :username");
- $stmt->execute(array(':username' => $username));
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while($row = array_shift($rows)) {
- $cids[] = $row['credentialId'];
- }
- return $cids;
- break;
- case "get_all_cids":
- // Only needed when using fido2 with username
- $stmt = $pdo->query("SELECT `credentialId` FROM `fido2`");
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while($row = array_shift($rows)) {
- $cids[] = $row['credentialId'];
- }
- return $cids;
- break;
- case "get_by_b64cid":
- if (!isset($_data['cid']) || empty($_data['cid'])) {
- return false;
- }
- $stmt = $pdo->prepare("SELECT `certificateSubject`, `username`, `credentialPublicKey`, SHA2(`credentialId`, 256) AS `cid` FROM `fido2` WHERE `credentialId` = :cid");
- $stmt->execute(array(':cid' => base64_decode($_data['cid'])));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (empty($row) || empty($row['credentialPublicKey']) || empty($row['username'])) {
- return false;
- }
- $data['pub_key'] = $row['credentialPublicKey'];
- $data['username'] = $row['username'];
- $data['subject'] = $row['certificateSubject'];
- $data['cid'] = $row['cid'];
- return $data;
- break;
- case "get_friendly_names":
- $username = $_SESSION['mailcow_cc_username'];
- if (!isset($_SESSION['mailcow_cc_role']) || empty($username)) {
- return false;
- }
- $stmt = $pdo->prepare("SELECT SHA2(`credentialId`, 256) AS `cid`, `created`, `certificateSubject`, `friendlyName` FROM `fido2` WHERE `username` = :username");
- $stmt->execute(array(':username' => $username));
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while($row = array_shift($rows)) {
- $fns[] = array(
- "subject" => (empty($row['certificateSubject']) ? 'Unknown (' . $row['created'] . ')' : $row['certificateSubject']),
- "fn" => $row['friendlyName'],
- "cid" => $row['cid']
- );
- }
- return $fns;
- break;
- case "unset_fido2_key":
- $username = $_SESSION['mailcow_cc_username'];
- if (!isset($_SESSION['mailcow_cc_role']) || empty($username)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data["action"]),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $stmt = $pdo->prepare("DELETE FROM `fido2` WHERE `username` = :username AND SHA2(`credentialId`, 256) = :cid");
- $stmt->execute(array(
- ':username' => $username,
- ':cid' => $_data['post_data']['unset_fido2_key']
- ));
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('object_modified', htmlspecialchars($username))
- );
- break;
- case "edit_fn":
- $username = $_SESSION['mailcow_cc_username'];
- if (!isset($_SESSION['mailcow_cc_role']) || empty($username)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data["action"]),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $stmt = $pdo->prepare("UPDATE `fido2` SET `friendlyName` = :friendlyName WHERE SHA2(`credentialId`, 256) = :cid AND `username` = :username");
- $stmt->execute(array(
- ':username' => $username,
- ':friendlyName' => $_data['fido2_attrs']['fido2_fn'],
- ':cid' => $_data['fido2_attrs']['fido2_cid']
- ));
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('object_modified', htmlspecialchars($username))
- );
- break;
- }
- }
- function unset_tfa_key($_data) {
- // Can only unset own keys
- // Needs at least one key left
- global $pdo;
- global $lang;
- $_data_log = $_data;
- $access_denied = null;
- $id = intval($_data['unset_tfa_key']);
- $username = $_SESSION['mailcow_cc_username'];
- // check for empty user and role
- if (!isset($_SESSION['mailcow_cc_role']) || empty($username)) $access_denied = true;
- try {
- if (!is_numeric($id)) $access_denied = true;
- // set access_denied error
- if ($access_denied){
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => 'access_denied'
- );
- return false;
- }
- // check if it's last key
- $stmt = $pdo->prepare("SELECT COUNT(*) AS `keys` FROM `tfa`
- WHERE `username` = :username AND `active` = '1'");
- $stmt->execute(array(':username' => $username));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if ($row['keys'] == "1") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => 'last_key'
- );
- return false;
- }
- // delete key
- $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username AND `id` = :id");
- $stmt->execute(array(':username' => $username, ':id' => $id));
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('object_modified', $username)
- );
- }
- catch (PDOException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_data_log),
- 'msg' => array('mysql_error', $e)
- );
- return false;
- }
- }
- function get_tfa($username = null, $id = null) {
- global $pdo;
- if (empty($username) && isset($_SESSION['mailcow_cc_username'])) {
- $username = $_SESSION['mailcow_cc_username'];
- }
- elseif (empty($username)) {
- return false;
- }
- if (!isset($id)){
- // fetch all tfa methods - just get information about possible authenticators
- $stmt = $pdo->prepare("SELECT `id`, `key_id`, `authmech` FROM `tfa`
- WHERE `username` = :username AND `active` = '1'");
- $stmt->execute(array(':username' => $username));
- $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
- // no tfa methods found
- if (count($results) == 0) {
- $data['name'] = 'none';
- $data['pretty'] = "-";
- $data['additional'] = array();
- return $data;
- }
- $data['additional'] = $results;
- return $data;
- } else {
- // fetch specific authenticator details by id
- $stmt = $pdo->prepare("SELECT * FROM `tfa`
- WHERE `username` = :username AND `id` = :id AND `active` = '1'");
- $stmt->execute(array(':username' => $username, ':id' => $id));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (isset($row["authmech"])) {
- switch ($row["authmech"]) {
- case "yubi_otp":
- $data['name'] = "yubi_otp";
- $data['pretty'] = "Yubico OTP";
- $stmt = $pdo->prepare("SELECT `id`, `key_id`, RIGHT(`secret`, 12) AS 'modhex' FROM `tfa` WHERE `authmech` = 'yubi_otp' AND `username` = :username AND `id` = :id");
- $stmt->execute(array(
- ':username' => $username,
- ':id' => $id
- ));
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while($row = array_shift($rows)) {
- $data['additional'][] = $row;
- }
- return $data;
- break;
- // u2f - deprecated, should be removed
- case "u2f":
- $data['name'] = "u2f";
- $data['pretty'] = "Fido U2F";
- $stmt = $pdo->prepare("SELECT `id`, `key_id` FROM `tfa` WHERE `authmech` = 'u2f' AND `username` = :username AND `id` = :id");
- $stmt->execute(array(
- ':username' => $username,
- ':id' => $id
- ));
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while($row = array_shift($rows)) {
- $data['additional'][] = $row;
- }
- return $data;
- break;
- case "hotp":
- $data['name'] = "hotp";
- $data['pretty'] = "HMAC-based OTP";
- return $data;
- break;
- case "totp":
- $data['name'] = "totp";
- $data['pretty'] = "Time-based OTP";
- $stmt = $pdo->prepare("SELECT `id`, `key_id`, `secret` FROM `tfa` WHERE `authmech` = 'totp' AND `username` = :username AND `id` = :id");
- $stmt->execute(array(
- ':username' => $username,
- ':id' => $id
- ));
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while($row = array_shift($rows)) {
- $data['additional'][] = $row;
- }
- return $data;
- break;
- case "webauthn":
- $data['name'] = "webauthn";
- $data['pretty'] = "WebAuthn";
- $stmt = $pdo->prepare("SELECT `id`, `key_id` FROM `tfa` WHERE `authmech` = 'webauthn' AND `username` = :username AND `id` = :id");
- $stmt->execute(array(
- ':username' => $username,
- ':id' => $id
- ));
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while($row = array_shift($rows)) {
- $data['additional'][] = $row;
- }
- return $data;
- break;
- default:
- $data['name'] = 'none';
- $data['pretty'] = "-";
- return $data;
- break;
- }
- }
- else {
- $data['name'] = 'none';
- $data['pretty'] = "-";
- return $data;
- }
- }
- }
- function verify_tfa_login($username, $_data) {
- global $pdo;
- global $yubi;
- global $u2f;
- global $tfa;
- global $WebAuthn;
- if ($_data['tfa_method'] != 'u2f'){
- switch ($_data["tfa_method"]) {
- case "yubi_otp":
- if (!ctype_alnum($_data['token']) || strlen($_data['token']) != 44) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => array('yotp_verification_failed', 'token length error')
- );
- return false;
- }
- $yubico_modhex_id = substr($_data['token'], 0, 12);
- $stmt = $pdo->prepare("SELECT `id`, `secret` FROM `tfa`
- WHERE `username` = :username
- AND `authmech` = 'yubi_otp'
- AND `active` = '1'
- AND `secret` LIKE :modhex");
- $stmt->execute(array(':username' => $username, ':modhex' => '%' . $yubico_modhex_id));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- $yubico_auth = explode(':', $row['secret']);
- $yubi = new Auth_Yubico($yubico_auth[0], $yubico_auth[1]);
- $yauth = $yubi->verify($_data['token']);
- if (PEAR::isError($yauth)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => array('yotp_verification_failed', $yauth->getMessage())
- );
- return false;
- }
- else {
- $_SESSION['tfa_id'] = $row['id'];
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => 'verified_yotp_login'
- );
- return true;
- }
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => array('yotp_verification_failed', 'unknown')
- );
- return false;
- break;
- case "hotp":
- return false;
- break;
- case "totp":
- try {
- $stmt = $pdo->prepare("SELECT `id`, `secret` FROM `tfa`
- WHERE `username` = :username
- AND `authmech` = 'totp'
- AND `id` = :id
- AND `active`='1'");
- $stmt->execute(array(':username' => $username, ':id' => $_data['id']));
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- foreach ($rows as $row) {
- if ($tfa->verifyCode($row['secret'], $_data['token']) === true) {
- $_SESSION['tfa_id'] = $row['id'];
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => 'verified_totp_login'
- );
- return true;
- }
- }
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => 'totp_verification_failed'
- );
- return false;
- }
- catch (PDOException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => array('mysql_error', $e)
- );
- return false;
- }
- break;
- case "webauthn":
- $tokenData = json_decode($_data['token']);
- $clientDataJSON = base64_decode($tokenData->clientDataJSON);
- $authenticatorData = base64_decode($tokenData->authenticatorData);
- $signature = base64_decode($tokenData->signature);
- $id = base64_decode($tokenData->id);
- $challenge = $_SESSION['challenge'];
- $stmt = $pdo->prepare("SELECT `id`, `key_id`, `keyHandle`, `username`, `publicKey` FROM `tfa` WHERE `id` = :id AND `active`='1'");
- $stmt->execute(array(':id' => $_data['id']));
- $process_webauthn = $stmt->fetch(PDO::FETCH_ASSOC);
- if (empty($process_webauthn)){
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => array('webauthn_authenticator_failed')
- );
- return false;
- }
- if (empty($process_webauthn['publicKey']) || $process_webauthn['publicKey'] === false) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => array('webauthn_publickey_failed')
- );
- return false;
- }
- if ($process_webauthn['username'] != $_SESSION['pending_mailcow_cc_username']){
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => array('webauthn_username_failed')
- );
- return false;
- }
- try {
- $WebAuthn->processGet($clientDataJSON, $authenticatorData, $signature, $process_webauthn['publicKey'], $challenge, null, $GLOBALS['WEBAUTHN_UV_FLAG_LOGIN'], $GLOBALS['WEBAUTHN_USER_PRESENT_FLAG']);
- }
- catch (Throwable $ex) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => array('webauthn_verification_failed', $ex->getMessage())
- );
- return false;
- }
- $stmt = $pdo->prepare("SELECT `superadmin` FROM `admin` WHERE `username` = :username");
- $stmt->execute(array(':username' => $process_webauthn['username']));
- $obj_props = $stmt->fetch(PDO::FETCH_ASSOC);
- if ($obj_props['superadmin'] === 1) {
- $_SESSION["mailcow_cc_role"] = "admin";
- }
- elseif ($obj_props['superadmin'] === 0) {
- $_SESSION["mailcow_cc_role"] = "domainadmin";
- }
- else {
- $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :username");
- $stmt->execute(array(':username' => $process_webauthn['username']));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (!empty($row['username'])) {
- $_SESSION["mailcow_cc_role"] = "user";
- } else {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => array('webauthn_role_failed')
- );
- return false;
- }
- }
- $_SESSION["mailcow_cc_username"] = $process_webauthn['username'];
- $_SESSION['tfa_id'] = $process_webauthn['id'];
- $_SESSION['authReq'] = null;
- unset($_SESSION["challenge"]);
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array("webauthn_login"),
- 'msg' => array('logged_in_as', $process_webauthn['username'])
- );
- return true;
- break;
- default:
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $username, '*'),
- 'msg' => 'unknown_tfa_method'
- );
- return false;
- break;
- }
- return false;
- } else {
- // delete old keys that used u2f
- $stmt = $pdo->prepare("SELECT * FROM `tfa` WHERE `authmech` = 'u2f' AND `username` = :username");
- $stmt->execute(array(':username' => $username));
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- if (count($rows) == 0) return false;
- $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `authmech` = 'u2f' AND `username` = :username");
- $stmt->execute(array(':username' => $username));
- return true;
- }
- }
- function admin_api($access, $action, $data = null) {
- global $pdo;
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => 'access_denied'
- );
- return false;
- }
- if ($access !== "ro" && $access !== "rw") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => 'invalid access type'
- );
- return false;
- }
- if ($action == "edit") {
- $active = (!empty($data['active'])) ? 1 : 0;
- $skip_ip_check = (isset($data['skip_ip_check'])) ? 1 : 0;
- $allow_from = array();
- if (isset($data['allow_from'])) {
- $allow_from = array_map('trim', preg_split( "/( |,|;|\n)/", $data['allow_from']));
- }
- foreach ($allow_from as $key => $val) {
- if (empty($val)) {
- unset($allow_from[$key]);
- continue;
- }
- if (valid_network($val) !== true) {
- $_SESSION['return'][] = array(
- 'type' => 'warning',
- 'log' => array(__FUNCTION__, $data),
- 'msg' => array('ip_invalid', htmlspecialchars($allow_from[$key]))
- );
- unset($allow_from[$key]);
- continue;
- }
- }
- $allow_from = implode(',', array_unique(array_filter($allow_from)));
- if (empty($allow_from) && $skip_ip_check == 0) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $data),
- 'msg' => 'ip_list_empty'
- );
- return false;
- }
- $api_key = implode('-', array(
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3)))
- ));
- $stmt = $pdo->query("SELECT `api_key` FROM `api` WHERE `access` = '" . $access . "'");
- $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
- if (empty($num_results)) {
- $stmt = $pdo->prepare("INSERT INTO `api` (`api_key`, `skip_ip_check`, `active`, `allow_from`, `access`)
- VALUES (:api_key, :skip_ip_check, :active, :allow_from, :access);");
- $stmt->execute(array(
- ':api_key' => $api_key,
- ':skip_ip_check' => $skip_ip_check,
- ':active' => $active,
- ':allow_from' => $allow_from,
- ':access' => $access
- ));
- }
- else {
- if ($skip_ip_check == 0) {
- $stmt = $pdo->prepare("UPDATE `api` SET `skip_ip_check` = :skip_ip_check,
- `active` = :active,
- `allow_from` = :allow_from
- WHERE `access` = :access;");
- $stmt->execute(array(
- ':active' => $active,
- ':skip_ip_check' => $skip_ip_check,
- ':allow_from' => $allow_from,
- ':access' => $access
- ));
- }
- else {
- $stmt = $pdo->prepare("UPDATE `api` SET `skip_ip_check` = :skip_ip_check,
- `active` = :active
- WHERE `access` = :access;");
- $stmt->execute(array(
- ':active' => $active,
- ':skip_ip_check' => $skip_ip_check,
- ':access' => $access
- ));
- }
- }
- }
- elseif ($action == "regen_key") {
- $api_key = implode('-', array(
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3)))
- ));
- $stmt = $pdo->prepare("UPDATE `api` SET `api_key` = :api_key WHERE `access` = :access");
- $stmt->execute(array(
- ':api_key' => $api_key,
- ':access' => $access
- ));
- }
- elseif ($action == "get") {
- $stmt = $pdo->query("SELECT * FROM `api` WHERE `access` = '" . $access . "'");
- $apidata = $stmt->fetch(PDO::FETCH_ASSOC);
- if ($apidata !== false) {
- $apidata['allow_from'] = str_replace(',', PHP_EOL, $apidata['allow_from']);
- }
- return $apidata;
- }
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $data),
- 'msg' => 'admin_api_modified'
- );
- }
- function license($action, $data = null) {
- global $pdo;
- global $redis;
- global $lang;
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => 'access_denied'
- );
- return false;
- }
- switch ($action) {
- case "verify":
- // Keep result until revalidate button is pressed or session expired
- $stmt = $pdo->query("SELECT `version` FROM `versions` WHERE `application` = 'GUID'");
- $versions = $stmt->fetch(PDO::FETCH_ASSOC);
- $post = array('guid' => $versions['version']);
- $curl = curl_init('https://verify.mailcow.email');
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
- $response = curl_exec($curl);
- curl_close($curl);
- $json_return = json_decode($response, true);
- if ($response && $json_return) {
- if ($json_return['response'] === "ok") {
- $_SESSION['gal']['valid'] = "true";
- $_SESSION['gal']['c'] = $json_return['c'];
- $_SESSION['gal']['s'] = $json_return['s'];
- if ($json_return['m'] == 'NoMoore') {
- $_SESSION['gal']['m'] = '🐄';
- }
- else {
- $_SESSION['gal']['m'] = str_repeat('🐄', substr_count($json_return['m'], 'o'));
- }
- }
- elseif ($json_return['response'] === "invalid") {
- $_SESSION['gal']['valid'] = "false";
- $_SESSION['gal']['c'] = $lang['mailbox']['no'];
- $_SESSION['gal']['s'] = $lang['mailbox']['no'];
- $_SESSION['gal']['m'] = $lang['mailbox']['no'];
- }
- }
- else {
- $_SESSION['gal']['valid'] = "false";
- $_SESSION['gal']['c'] = $lang['danger']['temp_error'];
- $_SESSION['gal']['s'] = $lang['danger']['temp_error'];
- $_SESSION['gal']['m'] = $lang['danger']['temp_error'];
- }
- try {
- // json_encode needs "true"/"false" instead of true/false, to not encode it to 0 or 1
- $redis->Set('LICENSE_STATUS_CACHE', json_encode($_SESSION['gal']));
- }
- catch (RedisException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_data_log),
- 'msg' => array('redis_error', $e)
- );
- return false;
- }
- return $_SESSION['gal']['valid'];
- break;
- case "guid":
- $stmt = $pdo->query("SELECT `version` FROM `versions` WHERE `application` = 'GUID'");
- $versions = $stmt->fetch(PDO::FETCH_ASSOC);
- return $versions['version'];
- break;
- }
- }
- function rspamd_ui($action, $data = null) {
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => 'access_denied'
- );
- return false;
- }
- switch ($action) {
- case "edit":
- $rspamd_ui_pass = $data['rspamd_ui_pass'];
- $rspamd_ui_pass2 = $data['rspamd_ui_pass2'];
- if (empty($rspamd_ui_pass) || empty($rspamd_ui_pass2)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, '*', '*'),
- 'msg' => 'password_empty'
- );
- return false;
- }
- if ($rspamd_ui_pass != $rspamd_ui_pass2) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, '*', '*'),
- 'msg' => 'password_mismatch'
- );
- return false;
- }
- if (strlen($rspamd_ui_pass) < 6) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, '*', '*'),
- 'msg' => 'rspamd_ui_pw_length'
- );
- return false;
- }
- $docker_return = docker('post', 'rspamd-mailcow', 'exec', array('cmd' => 'rspamd', 'task' => 'worker_password', 'raw' => $rspamd_ui_pass), array('Content-Type: application/json'));
- if ($docker_return_array = json_decode($docker_return, true)) {
- if ($docker_return_array['type'] == 'success') {
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, '*', '*'),
- 'msg' => 'rspamd_ui_pw_set'
- );
- return true;
- }
- else {
- $_SESSION['return'][] = array(
- 'type' => $docker_return_array['type'],
- 'log' => array(__FUNCTION__, '*', '*'),
- 'msg' => $docker_return_array['msg']
- );
- return false;
- }
- }
- else {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, '*', '*'),
- 'msg' => 'unknown'
- );
- return false;
- }
- break;
- }
- }
- function cors($action, $data = null) {
- global $redis;
- switch ($action) {
- case "edit":
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $data),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $allowed_origins = isset($data['allowed_origins']) ? $data['allowed_origins'] : array($_SERVER['SERVER_NAME']);
- $allowed_origins = !is_array($allowed_origins) ? array_filter(array_map('trim', explode("\n", $allowed_origins))) : $allowed_origins;
- foreach ($allowed_origins as $origin) {
- if (!filter_var($origin, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) && $origin != '*') {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $data),
- 'msg' => 'cors_invalid_origin'
- );
- return false;
- }
- }
- $allowed_methods = isset($data['allowed_methods']) ? $data['allowed_methods'] : array('GET', 'POST', 'PUT', 'DELETE');
- $allowed_methods = !is_array($allowed_methods) ? array_map('trim', preg_split( "/( |,|;|\n)/", $allowed_methods)) : $allowed_methods;
- $available_methods = array('GET', 'POST', 'PUT', 'DELETE');
- foreach ($allowed_methods as $method) {
- if (!in_array($method, $available_methods)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $data),
- 'msg' => 'cors_invalid_method'
- );
- return false;
- }
- }
- try {
- $redis->hMSet('CORS_SETTINGS', array(
- 'allowed_origins' => implode(', ', $allowed_origins),
- 'allowed_methods' => implode(', ', $allowed_methods)
- ));
- } catch (RedisException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $data),
- 'msg' => array('redis_error', $e)
- );
- return false;
- }
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $action, $data),
- 'msg' => 'cors_headers_edited'
- );
- return true;
- break;
- case "get":
- try {
- $cors_settings = $redis->hMGet('CORS_SETTINGS', array('allowed_origins', 'allowed_methods'));
- } catch (RedisException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $data),
- 'msg' => array('redis_error', $e)
- );
- }
- $cors_settings = !$cors_settings ? array('allowed_origins' => $_SERVER['SERVER_NAME'], 'allowed_methods' => 'GET, POST, PUT, DELETE') : $cors_settings;
- $cors_settings['allowed_origins'] = empty($cors_settings['allowed_origins']) ? $_SERVER['SERVER_NAME'] : $cors_settings['allowed_origins'];
- $cors_settings['allowed_methods'] = empty($cors_settings['allowed_methods']) ? 'GET, POST, PUT, DELETE, OPTION' : $cors_settings['allowed_methods'];
- return $cors_settings;
- break;
- case "set_headers":
- $cors_settings = cors('get');
- // check if requested origin is in allowed origins
- $allowed_origins = explode(', ', $cors_settings['allowed_origins']);
- $cors_settings['allowed_origins'] = $allowed_origins[0];
- if (in_array('*', $allowed_origins)){
- $cors_settings['allowed_origins'] = '*';
- } else if (in_array($_SERVER['HTTP_ORIGIN'], $allowed_origins)) {
- $cors_settings['allowed_origins'] = $_SERVER['HTTP_ORIGIN'];
- }
- // always allow OPTIONS for preflight request
- $cors_settings["allowed_methods"] = empty($cors_settings["allowed_methods"]) ? 'OPTIONS' : $cors_settings["allowed_methods"] . ', ' . 'OPTIONS';
- header('Access-Control-Allow-Origin: ' . $cors_settings['allowed_origins']);
- header('Access-Control-Allow-Methods: '. $cors_settings['allowed_methods']);
- header('Access-Control-Allow-Headers: Accept, Content-Type, X-Api-Key, Origin');
- // Access-Control settings requested, this is just a preflight request
- if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS' &&
- isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) &&
- isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
- $allowed_methods = explode(', ', $cors_settings["allowed_methods"]);
- if (in_array($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'], $allowed_methods, true))
- // method allowed send 200 OK
- http_response_code(200);
- else
- // method not allowed send 405 METHOD NOT ALLOWED
- http_response_code(405);
- exit;
- }
- break;
- }
- }
- function getBaseURL() {
- $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
- $host = $_SERVER['HTTP_HOST'];
- $base_url = $protocol . '://' . $host;
- return $base_url;
- }
- function uuid4() {
- $data = openssl_random_pseudo_bytes(16);
- $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
- $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
- return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
- }
- function identity_provider($_action = null, $_data = null, $_extra = null) {
- global $pdo;
- global $iam_provider;
- global $iam_settings;
- $data_log = $_data;
- if (isset($data_log['client_secret'])) $data_log['client_secret'] = '*';
- if (isset($data_log['access_token'])) $data_log['access_token'] = '*';
- switch ($_action) {
- case 'get':
- $settings = array();
- $stmt = $pdo->prepare("SELECT * FROM `identity_provider`;");
- $stmt->execute();
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- foreach($rows as $row){
- switch ($row["key"]) {
- case "mappers":
- case "templates":
- $settings[$row["key"]] = json_decode($row["value"]);
- break;
- case "use_ssl":
- case "use_tls":
- case "ignore_ssl_errors":
- $settings[$row["key"]] = boolval($row["value"]);
- break;
- default:
- $settings[$row["key"]] = $row["value"];
- break;
- }
- }
- // return default client_scopes for generic-oidc if none is set
- if ($settings["authsource"] == "generic-oidc" && empty($settings["client_scopes"])){
- $settings["client_scopes"] = "openid profile email";
- }
- if ($_extra['hide_sensitive']){
- $settings['client_secret'] = '';
- $settings['access_token'] = '';
- }
- // return default ldap options
- if ($settings["authsource"] == "ldap"){
- $settings['use_ssl'] = !isset($settings['use_ssl']) ? false : $settings['use_ssl'];
- $settings['use_tls'] = !isset($settings['use_tls']) ? false : $settings['use_tls'];
- $settings['ignore_ssl_errors'] = !isset($settings['ignore_ssl_errors']) ? false : $settings['ignore_ssl_errors'];
- }
- return $settings;
- break;
- case 'edit':
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_data),
- 'msg' => 'access_denied'
- );
- return false;
- }
- if (!isset($_data['authsource'])){
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $data_log),
- 'msg' => array('required_data_missing', '')
- );
- return false;
- }
- $available_authsources = array(
- "keycloak",
- "generic-oidc",
- "ldap"
- );
- $_data['authsource'] = strtolower($_data['authsource']);
- if (!in_array($_data['authsource'], $available_authsources)){
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $data_log),
- 'msg' => array('invalid_authsource', $setting)
- );
- return false;
- }
- $stmt = $pdo->prepare("SELECT * FROM `mailbox`
- WHERE `authsource` != 'mailcow'
- AND `authsource` IS NOT NULL
- AND `authsource` != :authsource");
- $stmt->execute(array(':authsource' => $_data['authsource']));
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- if ($rows) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $data_log),
- 'msg' => array('authsource_in_use', $setting)
- );
- return false;
- }
- $_data['ignore_ssl_error'] = isset($_data['ignore_ssl_error']) ? boolval($_data['ignore_ssl_error']) : false;
- switch ($_data['authsource']) {
- case "keycloak":
- $_data['server_url'] = (!empty($_data['server_url'])) ? rtrim($_data['server_url'], '/') : null;
- $_data['mailpassword_flow'] = isset($_data['mailpassword_flow']) ? intval($_data['mailpassword_flow']) : 0;
- $_data['periodic_sync'] = isset($_data['periodic_sync']) ? intval($_data['periodic_sync']) : 0;
- $_data['import_users'] = isset($_data['import_users']) ? intval($_data['import_users']) : 0;
- $_data['sync_interval'] = (!empty($_data['sync_interval'])) ? intval($_data['sync_interval']) : 15;
- $_data['sync_interval'] = $_data['sync_interval'] < 1 ? 1 : $_data['sync_interval'];
- $required_settings = array('authsource', 'server_url', 'realm', 'client_id', 'client_secret', 'redirect_url', 'version', 'mailpassword_flow', 'periodic_sync', 'import_users', 'sync_interval', 'ignore_ssl_error');
- break;
- case "generic-oidc":
- $_data['authorize_url'] = (!empty($_data['authorize_url'])) ? $_data['authorize_url'] : null;
- $_data['token_url'] = (!empty($_data['token_url'])) ? $_data['token_url'] : null;
- $_data['userinfo_url'] = (!empty($_data['userinfo_url'])) ? $_data['userinfo_url'] : null;
- $_data['client_scopes'] = (!empty($_data['client_scopes'])) ? $_data['client_scopes'] : "openid profile email";
- $required_settings = array('authsource', 'authorize_url', 'token_url', 'client_id', 'client_secret', 'redirect_url', 'userinfo_url', 'client_scopes', 'ignore_ssl_error');
- break;
- case "ldap":
- $_data['host'] = (!empty($_data['host'])) ? str_replace(" ", "", $_data['host']) : "";
- $_data['port'] = (!empty($_data['port'])) ? intval($_data['port']) : 389;
- $_data['username_field'] = (!empty($_data['username_field'])) ? strtolower($_data['username_field']) : "mail";
- $_data['attribute_field'] = (!empty($_data['attribute_field'])) ? strtolower($_data['attribute_field']) : "";
- $_data['filter'] = (!empty($_data['filter'])) ? $_data['filter'] : "";
- $_data['periodic_sync'] = isset($_data['periodic_sync']) ? intval($_data['periodic_sync']) : 0;
- $_data['import_users'] = isset($_data['import_users']) ? intval($_data['import_users']) : 0;
- $_data['use_ssl'] = isset($_data['use_ssl']) ? boolval($_data['use_ssl']) : false;
- $_data['use_tls'] = isset($_data['use_tls']) && !$_data['use_ssl'] ? boolval($_data['use_tls']) : false;
- $_data['sync_interval'] = (!empty($_data['sync_interval'])) ? intval($_data['sync_interval']) : 15;
- $_data['sync_interval'] = $_data['sync_interval'] < 1 ? 1 : $_data['sync_interval'];
- $required_settings = array('authsource', 'host', 'port', 'basedn', 'username_field', 'filter', 'attribute_field', 'binddn', 'bindpass', 'periodic_sync', 'import_users', 'sync_interval', 'use_ssl', 'use_tls', 'ignore_ssl_error');
- break;
- }
- $pdo->beginTransaction();
- $stmt = $pdo->prepare("INSERT INTO identity_provider (`key`, `value`) VALUES (:key, :value) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);");
- // add connection settings
- foreach($required_settings as $setting){
- if (!isset($_data[$setting])){
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $data_log),
- 'msg' => array('required_data_missing', $setting)
- );
- $pdo->rollback();
- return false;
- }
- $stmt->bindParam(':key', $setting);
- $stmt->bindParam(':value', $_data[$setting]);
- $stmt->execute();
- }
- $pdo->commit();
- // add mappers
- if ($_data['mappers'] && $_data['templates']){
- $_data['mappers'] = (!is_array($_data['mappers'])) ? array($_data['mappers']) : $_data['mappers'];
- $_data['templates'] = (!is_array($_data['templates'])) ? array($_data['templates']) : $_data['templates'];
- $mappers = array_filter($_data['mappers']);
- $templates = array_filter($_data['templates']);
- if (count($mappers) == count($templates)){
- $mappers = json_encode($mappers);
- $templates = json_encode($templates);
- $stmt = $pdo->prepare("INSERT INTO identity_provider (`key`, `value`) VALUES ('mappers', :value) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);");
- $stmt->bindParam(':value', $mappers);
- $stmt->execute();
- $stmt = $pdo->prepare("INSERT INTO identity_provider (`key`, `value`) VALUES ('templates', :value) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);");
- $stmt->bindParam(':value', $templates);
- $stmt->execute();
- }
- }
- // delete old access_token
- $stmt = $pdo->query("INSERT INTO identity_provider (`key`, `value`) VALUES ('access_token', '') ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);");
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_action, $data_log),
- 'msg' => array('object_modified', '')
- );
- return true;
- break;
- case 'test':
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_data),
- 'msg' => 'access_denied'
- );
- return false;
- }
- switch ($_data['authsource']) {
- case 'keycloak':
- $url = "{$_data['server_url']}/realms/{$_data['realm']}/protocol/openid-connect/token";
- $req = http_build_query(array(
- 'grant_type' => 'client_credentials',
- 'client_id' => $_data['client_id'],
- 'client_secret' => $_data['client_secret']
- ));
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_TIMEOUT, 7);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $req);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- if ($_data['ignore_ssl_error'] == "1"){
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- $res = curl_exec($curl);
- $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- curl_close ($curl);
- if ($code != 200) {
- return false;
- }
- break;
- case 'generic-oidc':
- $url = $_data['token_url'];
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_TIMEOUT, 7);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
- if ($_data['ignore_ssl_error'] == "1"){
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- $res = curl_exec($curl);
- $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- curl_close ($curl);
- if ($code != 200) {
- return false;
- }
- break;
- case 'ldap':
- if (!$_data['host'] || !$_data['port'] || !$_data['basedn'] ||
- !$_data['binddn'] || !$_data['bindpass']){
- return false;
- }
- $_data['use_ssl'] = isset($_data['use_ssl']) ? boolval($_data['use_ssl']) : false;
- $_data['use_tls'] = isset($_data['use_tls']) && !$_data['use_ssl'] ? boolval($_data['use_tls']) : false;
- $_data['ignore_ssl_error'] = isset($_data['ignore_ssl_error']) ? boolval($_data['ignore_ssl_error']) : false;
- $options = array();
- if ($_data['ignore_ssl_error']) {
- $options[LDAP_OPT_X_TLS_REQUIRE_CERT] = LDAP_OPT_X_TLS_NEVER;
- }
- $provider = new \LdapRecord\Connection([
- 'hosts' => explode(",", $_data['host']),
- 'port' => $_data['port'],
- 'base_dn' => $_data['basedn'],
- 'username' => $_data['binddn'],
- 'password' => $_data['bindpass'],
- 'use_ssl' => $_data['use_ssl'],
- 'use_tls' => $_data['use_tls'],
- 'options' => $options
- ]);
- try {
- $provider->connect();
- } catch (Throwable $e) {
- return false;
- }
- break;
- }
- return true;
- break;
- case "delete":
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $_data),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $stmt = $pdo->query("SELECT * FROM `mailbox`
- WHERE `authsource` != 'mailcow'
- AND `authsource` IS NOT NULL");
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- if ($rows) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_action, $data_log),
- 'msg' => array('authsource_in_use', $setting)
- );
- return false;
- }
- $stmt = $pdo->query("DELETE FROM identity_provider;");
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_action, $data_log),
- 'msg' => array('item_deleted', '')
- );
- return true;
- break;
- case "init":
- $settings = identity_provider('get');
- $provider = null;
- switch ($settings['authsource']) {
- case "keycloak":
- if ($settings['server_url'] && $settings['realm'] && $settings['client_id'] &&
- $settings['client_secret'] && $settings['redirect_url'] && $settings['version']){
- $guzzyClient = new GuzzleHttp\Client([
- 'defaults' => [
- \GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 5,
- \GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => true],
- \GuzzleHttp\RequestOptions::VERIFY => !$settings['ignore_ssl_error'],
- ]
- );
- $provider = new Stevenmaguire\OAuth2\Client\Provider\Keycloak([
- 'authServerUrl' => $settings['server_url'],
- 'realm' => $settings['realm'],
- 'clientId' => $settings['client_id'],
- 'clientSecret' => $settings['client_secret'],
- 'redirectUri' => $settings['redirect_url'],
- 'version' => $settings['version'],
- // 'encryptionAlgorithm' => 'RS256', // optional
- // 'encryptionKeyPath' => '../key.pem' // optional
- // 'encryptionKey' => 'contents_of_key_or_certificate' // optional
- ]);
- $provider->setHttpClient($guzzyClient);
- }
- break;
- case "generic-oidc":
- if ($settings['client_id'] && $settings['client_secret'] && $settings['redirect_url'] &&
- $settings['authorize_url'] && $settings['token_url'] && $settings['userinfo_url']){
- $guzzyClient = new GuzzleHttp\Client([
- 'defaults' => [
- \GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 5,
- \GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => true],
- \GuzzleHttp\RequestOptions::VERIFY => !$settings['ignore_ssl_error'],
- ]
- );
- $provider = new \League\OAuth2\Client\Provider\GenericProvider([
- 'clientId' => $settings['client_id'],
- 'clientSecret' => $settings['client_secret'],
- 'redirectUri' => $settings['redirect_url'],
- 'urlAuthorize' => $settings['authorize_url'],
- 'urlAccessToken' => $settings['token_url'],
- 'urlResourceOwnerDetails' => $settings['userinfo_url'],
- 'scopes' => $settings['client_scopes']
- ]);
- $provider->setHttpClient($guzzyClient);
- }
- break;
- case "ldap":
- if ($settings['host'] && $settings['port'] && $settings['basedn'] &&
- $settings['binddn'] && $settings['bindpass']){
- $options = array();
- if ($settings['ignore_ssl_error']) {
- $options[LDAP_OPT_X_TLS_REQUIRE_CERT] = LDAP_OPT_X_TLS_NEVER;
- }
- $provider = new \LdapRecord\Connection([
- 'hosts' => explode(",", $settings['host']),
- 'port' => $settings['port'],
- 'base_dn' => $settings['basedn'],
- 'username' => $settings['binddn'],
- 'password' => $settings['bindpass'],
- 'use_ssl' => $settings['use_ssl'],
- 'use_tls' => $settings['use_tls'],
- 'options' => $options
- ]);
- try {
- $provider->connect();
- } catch (Throwable $e) {
- $provider = null;
- }
- }
- break;
- }
- return $provider;
- break;
- case "verify-sso":
- if ($iam_settings['authsource'] != 'keycloak' && $iam_settings['authsource'] != 'generic-oidc'){
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => array('login_failed', "no OIDC provider configured")
- );
- return false;
- }
- try {
- $token = $iam_provider->getAccessToken('authorization_code', ['code' => $_GET['code']]);
- $plain_token = $token->getToken();
- $plain_refreshtoken = $token->getRefreshToken();
- $info = $iam_provider->getResourceOwner($token)->toArray();
- } catch (Throwable $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => array('login_failed', $e->getMessage())
- );
- return false;
- }
- // check if email address is given
- if (empty($info['email'])) return false;
- // get mapped template
- $user_template = $info['mailcow_template'];
- $mapper_key = array_search($user_template, $iam_settings['mappers']);
- // token valid, get mailbox
- $stmt = $pdo->prepare("SELECT * FROM `mailbox`
- INNER JOIN domain on mailbox.domain = domain.domain
- WHERE `kind` NOT REGEXP 'location|thing|group'
- AND `mailbox`.`active`='1'
- AND `domain`.`active`='1'
- AND `username` = :user
- AND (`authsource`='keycloak' OR `authsource`='generic-oidc')");
- $stmt->execute(array(':user' => $info['email']));
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if ($row){
- // success
- if ($mapper_key !== false) {
- // update user
- $_SESSION['access_all_exception'] = '1';
- mailbox('edit', 'mailbox_from_template', array(
- 'username' => $info['email'],
- 'name' => $info['name'],
- 'template' => $iam_settings['templates'][$mapper_key]
- ));
- $_SESSION['access_all_exception'] = '0';
- }
- set_user_loggedin_session($info['email']);
- $_SESSION['iam_token'] = $plain_token;
- $_SESSION['iam_refresh_token'] = $plain_refreshtoken;
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role']),
- 'msg' => array('logged_in_as', $_SESSION['mailcow_cc_username'])
- );
- return true;
- }
- if (empty($iam_settings['mappers']) || empty($user_template)){
- clear_session();
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $info['email']),
- 'msg' => array('login_failed', 'empty attribute mapping or missing template attribute')
- );
- return false;
- }
- if ($mapper_key === false) {
- clear_session();
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $info['email']),
- 'msg' => array('login_failed', 'specified template not found')
- );
- return false;
- }
- // create mailbox
- $_SESSION['access_all_exception'] = '1';
- $create_res = mailbox('add', 'mailbox_from_template', array(
- 'domain' => explode('@', $info['email'])[1],
- 'local_part' => explode('@', $info['email'])[0],
- 'name' => $info['name'],
- 'authsource' => $iam_settings['authsource'],
- 'template' => $iam_settings['templates'][$mapper_key]
- ));
- $_SESSION['access_all_exception'] = '0';
- if (!$create_res){
- clear_session();
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $info['email']),
- 'msg' => array('login_failed', 'mailbox creation failed')
- );
- return false;
- }
- set_user_loggedin_session($info['email']);
- $_SESSION['iam_token'] = $plain_token;
- $_SESSION['iam_refresh_token'] = $plain_refreshtoken;
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role']),
- 'msg' => array('logged_in_as', $_SESSION['mailcow_cc_username'])
- );
- return true;
- break;
- case "refresh-token":
- try {
- $token = $iam_provider->getAccessToken('refresh_token', ['refresh_token' => $_SESSION['iam_refresh_token']]);
- $plain_token = $token->getToken();
- $plain_refreshtoken = $token->getRefreshToken();
- $info = $iam_provider->getResourceOwner($token)->toArray();
- } catch (Throwable $e) {
- clear_session();
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__),
- 'msg' => array('refresh_login_failed', $e->getMessage())
- );
- return false;
- }
- if (empty($info['email'])){
- clear_session();
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role']),
- 'msg' => 'refresh_login_failed'
- );
- return false;
- }
- set_user_loggedin_session($info['email']);
- $_SESSION['iam_token'] = $plain_token;
- $_SESSION['iam_refresh_token'] = $plain_refreshtoken;
- return true;
- break;
- case "get-redirect":
- if ($iam_settings['authsource'] != 'keycloak' && $iam_settings['authsource'] != 'generic-oidc')
- return false;
- $authUrl = $iam_provider->getAuthorizationUrl();
- $_SESSION['oauth2state'] = $iam_provider->getState();
- return $authUrl;
- break;
- case "get-keycloak-admin-token":
- // get access_token for service account of mailcow client
- if ($iam_settings['authsource'] !== 'keycloak') return false;
- if (isset($iam_settings['access_token'])) {
- // check if access_token is valid
- $url = "{$iam_settings['server_url']}/realms/{$iam_settings['realm']}/protocol/openid-connect/token/introspect";
- $req = http_build_query(array(
- 'token' => $iam_settings['access_token'],
- 'client_id' => $iam_settings['client_id'],
- 'client_secret' => $iam_settings['client_secret']
- ));
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_TIMEOUT, 7);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $req);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 5);
- $res = json_decode(curl_exec($curl), true);
- $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- curl_close ($curl);
- if ($code == 200 && $res['active'] == true) {
- // token is valid
- return $iam_settings['access_token'];
- }
- }
- $url = "{$iam_settings['server_url']}/realms/{$iam_settings['realm']}/protocol/openid-connect/token";
- $req = http_build_query(array(
- 'grant_type' => 'client_credentials',
- 'client_id' => $iam_settings['client_id'],
- 'client_secret' => $iam_settings['client_secret']
- ));
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_TIMEOUT, 7);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $req);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 5);
- $res = json_decode(curl_exec($curl), true);
- $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- curl_close ($curl);
- if ($code != 200) {
- return false;
- }
- $stmt = $pdo->prepare("INSERT INTO identity_provider (`key`, `value`) VALUES (:key, :value) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);");
- $stmt->execute(array(
- ':key' => 'access_token',
- ':value' => $res['access_token']
- ));
- return $res['access_token'];
- break;
- }
- }
- function reset_password($action, $data = null) {
- global $pdo;
- global $redis;
- global $mailcow_hostname;
- global $PW_RESET_TOKEN_LIMIT;
- global $PW_RESET_TOKEN_LIFETIME;
- $_data_log = $data;
- if (isset($_data_log['new_password'])) $_data_log['new_password'] = '*';
- if (isset($_data_log['new_password2'])) $_data_log['new_password2'] = '*';
- switch ($action) {
- case 'check':
- $token = $data;
- $stmt = $pdo->prepare("SELECT `t1`.`username` FROM `reset_password` AS `t1` JOIN `mailbox` AS `t2` ON `t1`.`username` = `t2`.`username` WHERE `t1`.`token` = :token AND `t1`.`created` > DATE_SUB(NOW(), INTERVAL :lifetime MINUTE) AND `t2`.`active` = 1 AND `t2`.`authsource` = 'mailcow';");
- $stmt->execute(array(
- ':token' => preg_replace('/[^a-zA-Z0-9-]/', '', $token),
- ':lifetime' => $PW_RESET_TOKEN_LIFETIME
- ));
- $return = $stmt->fetch(PDO::FETCH_ASSOC);
- return empty($return['username']) ? false : $return['username'];
- break;
- case 'issue':
- $username = $data;
- // perform cleanup
- $stmt = $pdo->prepare("DELETE FROM `reset_password` WHERE created < DATE_SUB(NOW(), INTERVAL :lifetime MINUTE);");
- $stmt->execute(array(':lifetime' => $PW_RESET_TOKEN_LIFETIME));
- if (filter_var($username, FILTER_VALIDATE_EMAIL) === false) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'access_denied'
- );
- return false;
- }
- $pw_reset_notification = reset_password('get_notification', 'raw');
- if (!$pw_reset_notification) return false;
- if (empty($pw_reset_notification['from']) || empty($pw_reset_notification['subject'])) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'password_reset_na'
- );
- return false;
- }
- $stmt = $pdo->prepare("SELECT * FROM `mailbox`
- WHERE `username` = :username AND authsource = 'mailcow'");
- $stmt->execute(array(':username' => $username));
- $mailbox_data = $stmt->fetch(PDO::FETCH_ASSOC);
- if (empty($mailbox_data)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'password_reset_invalid_user'
- );
- return false;
- }
- $mailbox_attr = json_decode($mailbox_data['attributes'], true);
- if (empty($mailbox_attr['recovery_email']) || filter_var($mailbox_attr['recovery_email'], FILTER_VALIDATE_EMAIL) === false) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => "password_reset_invalid_user"
- );
- return false;
- }
- $stmt = $pdo->prepare("SELECT * FROM `reset_password`
- WHERE `username` = :username");
- $stmt->execute(array(':username' => $username));
- $generated_token_count = count($stmt->fetchAll(PDO::FETCH_ASSOC));
- if ($generated_token_count >= $PW_RESET_TOKEN_LIMIT) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => "reset_token_limit_exceeded"
- );
- return false;
- }
- $token = implode('-', array(
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3))),
- strtoupper(bin2hex(random_bytes(3)))
- ));
- $stmt = $pdo->prepare("INSERT INTO `reset_password` (`username`, `token`)
- VALUES (:username, :token)");
- $stmt->execute(array(
- ':username' => $username,
- ':token' => $token
- ));
- $reset_link = getBaseURL() . "/reset-password?token=" . $token;
- $request_date = new DateTime();
- $locale_date = locale_get_default();
- $date_formatter = new IntlDateFormatter(
- $locale_date,
- IntlDateFormatter::FULL,
- IntlDateFormatter::FULL
- );
- $formatted_request_date = $date_formatter->format($request_date);
- // set template vars
- // subject
- $pw_reset_notification['subject'] = str_replace('{{hostname}}', $mailcow_hostname, $pw_reset_notification['subject']);
- $pw_reset_notification['subject'] = str_replace('{{link}}', $reset_link, $pw_reset_notification['subject']);
- $pw_reset_notification['subject'] = str_replace('{{username}}', $username, $pw_reset_notification['subject']);
- $pw_reset_notification['subject'] = str_replace('{{username2}}', $mailbox_attr['recovery_email'], $pw_reset_notification['subject']);
- $pw_reset_notification['subject'] = str_replace('{{date}}', $formatted_request_date, $pw_reset_notification['subject']);
- $pw_reset_notification['subject'] = str_replace('{{token_lifetime}}', $PW_RESET_TOKEN_LIFETIME, $pw_reset_notification['subject']);
- // text
- $pw_reset_notification['text_tmpl'] = str_replace('{{hostname}}', $mailcow_hostname, $pw_reset_notification['text_tmpl']);
- $pw_reset_notification['text_tmpl'] = str_replace('{{link}}', $reset_link, $pw_reset_notification['text_tmpl']);
- $pw_reset_notification['text_tmpl'] = str_replace('{{username}}', $username, $pw_reset_notification['text_tmpl']);
- $pw_reset_notification['text_tmpl'] = str_replace('{{username2}}', $mailbox_attr['recovery_email'], $pw_reset_notification['text_tmpl']);
- $pw_reset_notification['text_tmpl'] = str_replace('{{date}}', $formatted_request_date, $pw_reset_notification['text_tmpl']);
- $pw_reset_notification['text_tmpl'] = str_replace('{{token_lifetime}}', $PW_RESET_TOKEN_LIFETIME, $pw_reset_notification['text_tmpl']);
- // html
- $pw_reset_notification['html_tmpl'] = str_replace('{{hostname}}', $mailcow_hostname, $pw_reset_notification['html_tmpl']);
- $pw_reset_notification['html_tmpl'] = str_replace('{{link}}', $reset_link, $pw_reset_notification['html_tmpl']);
- $pw_reset_notification['html_tmpl'] = str_replace('{{username}}', $username, $pw_reset_notification['html_tmpl']);
- $pw_reset_notification['html_tmpl'] = str_replace('{{username2}}', $mailbox_attr['recovery_email'], $pw_reset_notification['html_tmpl']);
- $pw_reset_notification['html_tmpl'] = str_replace('{{date}}', $formatted_request_date, $pw_reset_notification['html_tmpl']);
- $pw_reset_notification['html_tmpl'] = str_replace('{{token_lifetime}}', $PW_RESET_TOKEN_LIFETIME, $pw_reset_notification['html_tmpl']);
- $email_sent = reset_password('send_mail', array(
- "from" => $pw_reset_notification['from'],
- "to" => $mailbox_attr['recovery_email'],
- "subject" => $pw_reset_notification['subject'],
- "text" => $pw_reset_notification['text_tmpl'],
- "html" => $pw_reset_notification['html_tmpl']
- ));
- if (!$email_sent){
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => "recovery_email_failed"
- );
- return false;
- }
- list($localPart, $domainPart) = explode('@', $mailbox_attr['recovery_email']);
- if (strlen($localPart) > 1) {
- $maskedLocalPart = $localPart[0] . str_repeat('*', strlen($localPart) - 1);
- } else {
- $maskedLocalPart = "*";
- }
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => array("recovery_email_sent", $maskedLocalPart . '@' . $domainPart)
- );
- return array(
- "username" => $username,
- "issue" => "success"
- );
- break;
- case 'reset':
- $token = $data['token'];
- $new_password = $data['new_password'];
- $new_password2 = $data['new_password2'];
- $username = $data['username'];
- $check_tfa = $data['check_tfa'];
- if (!$username || !$token) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'invalid_reset_token'
- );
- return false;
- }
- # check new password
- if (!password_check($new_password, $new_password2)) {
- return false;
- }
- if ($check_tfa){
- // check for tfa authenticators
- $authenticators = get_tfa($username);
- if (isset($authenticators['additional']) && is_array($authenticators['additional']) && count($authenticators['additional']) > 0) {
- $_SESSION['pending_mailcow_cc_username'] = $username;
- $_SESSION['pending_pw_reset_token'] = $token;
- $_SESSION['pending_pw_new_password'] = $new_password;
- $_SESSION['pending_tfa_methods'] = $authenticators['additional'];
- $_SESSION['return'][] = array(
- 'type' => 'info',
- 'log' => array(__FUNCTION__, $user, '*'),
- 'msg' => 'awaiting_tfa_confirmation'
- );
- return false;
- }
- }
- # set new password
- $password_hashed = hash_password($new_password);
- $stmt = $pdo->prepare("UPDATE `mailbox` SET
- `password` = :password_hashed,
- `attributes` = JSON_SET(`attributes`, '$.passwd_update', NOW())
- WHERE `username` = :username AND authsource = 'mailcow'");
- $stmt->execute(array(
- ':password_hashed' => $password_hashed,
- ':username' => $username
- ));
- // perform cleanup
- $stmt = $pdo->prepare("DELETE FROM `reset_password` WHERE `username` = :username;");
- $stmt->execute(array(
- ':username' => $username
- ));
- update_sogo_static_view($username);
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'password_changed_success'
- );
- return true;
- break;
- case 'get_notification':
- $type = $data;
- try {
- $settings['from'] = $redis->Get('PW_RESET_FROM');
- $settings['subject'] = $redis->Get('PW_RESET_SUBJ');
- $settings['html_tmpl'] = $redis->Get('PW_RESET_HTML');
- $settings['text_tmpl'] = $redis->Get('PW_RESET_TEXT');
- if (empty($settings['html_tmpl']) && empty($settings['text_tmpl'])) {
- $settings['html_tmpl'] = file_get_contents("/tpls/pw_reset_html.tpl");
- $settings['text_tmpl'] = file_get_contents("/tpls/pw_reset_text.tpl");
- }
- if ($type != "raw") {
- $settings['html_tmpl'] = htmlspecialchars($settings['html_tmpl']);
- $settings['text_tmpl'] = htmlspecialchars($settings['text_tmpl']);
- }
- }
- catch (RedisException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => array('redis_error', $e)
- );
- return false;
- }
- return $settings;
- break;
- case 'send_mail':
- $from = $data['from'];
- $to = $data['to'];
- $text = $data['text'];
- $html = $data['html'];
- $subject = $data['subject'];
- if (!filter_var($from, FILTER_VALIDATE_EMAIL)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'from_invalid'
- );
- return false;
- }
- if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'to_invalid'
- );
- return false;
- }
- if (empty($subject)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'subject_empty'
- );
- return false;
- }
- if (empty($text)) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'text_empty'
- );
- return false;
- }
- ini_set('max_execution_time', 0);
- ini_set('max_input_time', 0);
- $mail = new PHPMailer;
- $mail->Timeout = 10;
- $mail->SMTPOptions = array(
- 'ssl' => array(
- 'verify_peer' => false,
- 'verify_peer_name' => false,
- 'allow_self_signed' => true
- )
- );
- $mail->isSMTP();
- $mail->Host = 'postfix-mailcow';
- $mail->SMTPAuth = false;
- $mail->Port = 25;
- $mail->setFrom($from);
- $mail->Subject = $subject;
- $mail->CharSet ="UTF-8";
- if (!empty($html)) {
- $mail->Body = $html;
- $mail->AltBody = $text;
- }
- else {
- $mail->Body = $text;
- }
- $mail->XMailer = 'MooMail';
- $mail->AddAddress($to);
- if (!$mail->send()) {
- return false;
- }
- $mail->ClearAllRecipients();
- return true;
- break;
- }
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'access_denied'
- );
- return false;
- }
- switch ($action) {
- case 'edit_notification':
- $subject = $data['subject'];
- $from = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data['from']);
- $from = (!filter_var($from, FILTER_VALIDATE_EMAIL)) ? "" : $from;
- $subject = (empty($subject)) ? "" : $subject;
- $text = (empty($data['text_tmpl'])) ? "" : $data['text_tmpl'];
- $html = (empty($data['html_tmpl'])) ? "" : $data['html_tmpl'];
- try {
- $redis->Set('PW_RESET_FROM', $from);
- $redis->Set('PW_RESET_SUBJ', $subject);
- $redis->Set('PW_RESET_HTML', $html);
- $redis->Set('PW_RESET_TEXT', $text);
- }
- catch (RedisException $e) {
- $_SESSION['return'][] = array(
- 'type' => 'danger',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => array('redis_error', $e)
- );
- return false;
- }
- $_SESSION['return'][] = array(
- 'type' => 'success',
- 'log' => array(__FUNCTION__, $action, $_data_log),
- 'msg' => 'saved_settings'
- );
- break;
- }
- }
- function clear_session(){
- session_regenerate_id(true);
- session_unset();
- session_destroy();
- session_write_close();
- }
- function set_user_loggedin_session($user) {
- session_regenerate_id(true);
- $_SESSION['mailcow_cc_username'] = $user;
- $_SESSION['mailcow_cc_role'] = 'user';
- $sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
- $_SESSION['sogo-sso-user-allowed'][] = $user;
- $_SESSION['sogo-sso-pass'] = $sogo_sso_pass;
- unset($_SESSION['pending_mailcow_cc_username']);
- unset($_SESSION['pending_mailcow_cc_role']);
- unset($_SESSION['pending_tfa_methods']);
- }
- function get_logs($application, $lines = false) {
- if ($lines === false) {
- $lines = $GLOBALS['LOG_LINES'] - 1;
- }
- elseif(is_numeric($lines) && $lines >= 1) {
- $lines = abs(intval($lines) - 1);
- }
- else {
- list ($from, $to) = explode('-', $lines);
- $from = intval($from);
- $to = intval($to);
- if ($from < 1 || $to < $from) { return false; }
- }
- global $redis;
- global $pdo;
- if ($_SESSION['mailcow_cc_role'] != "admin") {
- return false;
- }
- // SQL
- if ($application == "mailcow-ui") {
- if (isset($from) && isset($to)) {
- $stmt = $pdo->prepare("SELECT * FROM `logs` ORDER BY `id` DESC LIMIT :from, :to");
- $stmt->execute(array(
- ':from' => $from - 1,
- ':to' => $to
- ));
- $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- else {
- $stmt = $pdo->prepare("SELECT * FROM `logs` ORDER BY `id` DESC LIMIT :lines");
- $stmt->execute(array(
- ':lines' => $lines + 1,
- ));
- $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- if (is_array($data)) {
- return $data;
- }
- }
- if ($application == "sasl") {
- if (isset($from) && isset($to)) {
- $stmt = $pdo->prepare("SELECT * FROM `sasl_log` ORDER BY `datetime` DESC LIMIT :from, :to");
- $stmt->execute(array(
- ':from' => $from - 1,
- ':to' => $to
- ));
- $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- else {
- $stmt = $pdo->prepare("SELECT * FROM `sasl_log` ORDER BY `datetime` DESC LIMIT :lines");
- $stmt->execute(array(
- ':lines' => $lines + 1,
- ));
- $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- if (is_array($data)) {
- return $data;
- }
- }
- // Redis
- if ($application == "dovecot-mailcow") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('DOVECOT_MAILLOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('DOVECOT_MAILLOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "cron-mailcow") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('CRON_LOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('CRON_LOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "postfix-mailcow") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('POSTFIX_MAILLOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('POSTFIX_MAILLOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "sogo-mailcow") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('SOGO_LOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('SOGO_LOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "watchdog-mailcow") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('WATCHDOG_LOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('WATCHDOG_LOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "acme-mailcow") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('ACME_LOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('ACME_LOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "ratelimited") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('RL_LOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('RL_LOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "api-mailcow") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('API_LOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('API_LOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "netfilter-mailcow") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('NETFILTER_LOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('NETFILTER_LOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "autodiscover-mailcow") {
- if (isset($from) && isset($to)) {
- $data = $redis->lRange('AUTODISCOVER_LOG', $from - 1, $to - 1);
- }
- else {
- $data = $redis->lRange('AUTODISCOVER_LOG', 0, $lines);
- }
- if ($data) {
- foreach ($data as $json_line) {
- $data_array[] = json_decode($json_line, true);
- }
- return $data_array;
- }
- }
- if ($application == "rspamd-history") {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
- if (!is_numeric($lines)) {
- list ($from, $to) = explode('-', $lines);
- curl_setopt($curl, CURLOPT_URL,"http://rspamd/history?from=" . intval($from) . "&to=" . intval($to));
- }
- else {
- curl_setopt($curl, CURLOPT_URL,"http://rspamd/history?to=" . intval($lines));
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $history = curl_exec($curl);
- if (!curl_errno($curl)) {
- $data_array = json_decode($history, true);
- curl_close($curl);
- return $data_array['rows'];
- }
- curl_close($curl);
- return false;
- }
- if ($application == "rspamd-stats") {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
- curl_setopt($curl, CURLOPT_URL,"http://rspamd/stat");
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $stats = curl_exec($curl);
- if (!curl_errno($curl)) {
- $data_array = json_decode($stats, true);
- curl_close($curl);
- return $data_array;
- }
- curl_close($curl);
- return false;
- }
- return false;
- }
- function getGUID() {
- if (function_exists('com_create_guid')) {
- return com_create_guid();
- }
- mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
- $charid = strtoupper(md5(uniqid(rand(), true)));
- $hyphen = chr(45);// "-"
- return substr($charid, 0, 8).$hyphen
- .substr($charid, 8, 4).$hyphen
- .substr($charid,12, 4).$hyphen
- .substr($charid,16, 4).$hyphen
- .substr($charid,20,12);
- }
- function cleanupJS($ignore = '', $folder = '/tmp/*.js') {
- $now = time();
- foreach (glob($folder) as $filename) {
- if(strpos($filename, $ignore) !== false) {
- continue;
- }
- if (is_file($filename)) {
- if ($now - filemtime($filename) >= 60 * 60) {
- unlink($filename);
- }
- }
- }
- }
- function cleanupCSS($ignore = '', $folder = '/tmp/*.css') {
- $now = time();
- foreach (glob($folder) as $filename) {
- if(strpos($filename, $ignore) !== false) {
- continue;
- }
- if (is_file($filename)) {
- if ($now - filemtime($filename) >= 60 * 60) {
- unlink($filename);
- }
- }
- }
- }
- ?>
|