d1d1c8e16cc2b24a2e99ab65c9980b7d99ea0170
[Pman.Base] / Pman / Login.php
1 <?php
2
3 require_once 'Pman.php';
4
5 /***
6
7 * Auth wrapper..
8
9 * User class must provide the following features.
10
11 * logout()
12 * isAuth() 
13 * getAuthUser();
14 * authUserArray() 
15 * active()  -- is user active. // or set prior to checking..
16 * authUserName(n) - sets the value prior to a find(true)
17 * checkPassword($_REQUEST['password'])) {
18 * login();
19 * lang(val) - to set the language..
20 */
21
22
23
24 class Pman_Login extends Pman
25
26     var $masterTemplate = 'login.html';
27     
28     var $ip_management = false;
29         
30         var $event_suffix = '';
31         
32         // for forgot password email    
33         var $authFrom;
34         var $authKey;
35         var $person;
36         var $bcc;
37         var $rcpts;
38
39     
40     function getAuth() // everyone allowed in here..
41     {
42         parent::getAuth(); // load company..
43         
44         $ff = HTML_FlexyFramework::get();
45         
46         $this->ip_management = (empty($ff->Pman['ip_management'])) ? false : true;
47         
48         return true;
49     }
50     /**
51      * Accepts:
52      * logout =
53      * 
54      * 
55      */
56     function get($v, $opts=array()) 
57     {
58         $this->initErrorHandling();
59         
60          // DB_DataObject::DebugLevel(5);
61         if (!empty($_REQUEST['logout'])) {
62            return $this->logout();
63         }
64         
65         // general query...
66         if (!empty($_REQUEST['getAuthUser'])) {
67             //DB_Dataobject::debugLevel(5);
68             $this->sendAuthUserDetails();
69             exit;
70         }
71         
72         if(!empty($_REQUEST['check_owner_company'])) {
73             $core_company = DB_DataObject::factory('core_company');
74             $core_company->comptype = 'OWNER';
75             $this->jok($core_company->count());
76         }
77         
78         // might be an idea to disable this?!?
79         if (!empty($_REQUEST['username'])) {
80             $this->post();
81         }
82         
83         
84         if (!empty($_REQUEST['switch'])) {
85             $this->switchUser($_REQUEST['switch']);
86         }
87         
88         if (!empty($_REQUEST['loginPublic'])) {
89             $this->switchPublicUser($_REQUEST['loginPublic']);
90         }
91         if (!empty($_SERVER['HTTP_USER_AGENT']) && preg_match('/^check_http/', $_SERVER['HTTP_USER_AGENT'])) {
92                         die("server is alive = authFailure"); // should really use heartbeat now..
93                 }
94         $this->jerror("NOTICE-INVALID", "INVALID REQUEST");
95         exit;
96     }
97     
98     
99     function logout()
100     {
101         $ff = class_exists('HTML_FlexyFramework2') ?  HTML_FlexyFramework2::get()  :  HTML_FlexyFramework::get();
102         
103                 //DB_DAtaObject::debugLevel(1);
104         $u = $this->getAuthUser();
105         //print_r($u);
106         if ($u) {
107             
108             $this->addEvent('LOGOUT'. $this->event_suffix);
109             $e = DB_DataObject::factory('Events');
110           
111             
112             $u->logout();
113             session_regenerate_id(true);
114             session_commit(); 
115
116             if(!empty($ff->Pman['local_autoauth']) && !empty($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'localhost') {
117                 $this->jerr("you are using local autoauth!?");                
118             }
119             //echo '<PRE>';print_R($_SESSION);
120             $this->jok("Logged out - user ");
121         }
122         // log it..
123         
124         //$_SESSION['Pman_I18N'] = array(); << 
125         session_regenerate_id(true);
126         session_commit();
127         
128         $this->jok("Logged out - no user");
129         
130     }
131     
132     function sendAuthUserDetails()
133     {
134         // remove for normal use - it's a secuirty hole!
135         //DB_DataObject::debugLevel(1);
136         if (!empty($_REQUEST['_debug'])) {
137            // DB_DataObject::debugLevel(1);
138         }
139         // 
140         $ff = HTML_FlexyFramework::get();
141         $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
142         
143         $u = DB_DataObject::factory($tbl);
144         $s = DB_DataObject::factory('core_setting');
145         $require_oath_val = 1;
146         $require_oath = $s->lookup('core', 'two_factor_auth_required');
147         if(!empty($require_oath)) {
148             if($require_oath->val == 0) {
149                 $require_oath_val = 0;
150             }
151         } 
152         
153         if (!$u->isAuth()) {
154             $this->jok(array(
155                 'id' => 0
156             ));
157             exit;
158         }
159         
160         //die("got here?");
161         $au = $u->getAuthUser();
162         
163          // might occur on shared systems.
164         $ff= HTML_FlexyFramework::get();
165         
166         if (!empty($ff->Pman['auth_comptype'])  && $au->id > 0 &&
167                 ($ff->Pman['auth_comptype'] != $au->company()->comptype)) {
168             $au->logout();
169             $this->jerr("Login not permited to outside companies - please reload");
170         }
171         
172         //$au = $u->getAuthUser();
173         
174         $aur = $au ?  $au->authUserArray() : array();
175         
176         /** -- these need modulizing somehow! **/
177         
178         
179         
180         // basically calls Pman_MODULE_Login::sendAuthUserDetails($aur) on all the modules
181         //echo '<PRE>'; print_r($this->modules());
182         // technically each module should only add properties to an array named after that module..
183         
184         foreach($this->modules() as $m) {
185             if (empty($m)) {
186                 continue;
187             }
188             if (!file_exists($this->rootDir.'/Pman/'.$m.'/Login.php')) {
189                 continue;
190             }
191             $cls = 'Pman_'.$m.'_Login';
192             require_once 'Pman/'.$m.'/Login.php';
193             $x = new $cls;
194             $x->authUser = $au;
195             $aur = $x->sendAuthUserDetails($aur);
196         }
197         
198                  
199 //        
200 //        echo '<PRE>';print_r($aur);
201 //        exit;
202         $this->jok($aur);
203         exit;
204         
205             
206     }
207
208     function switchUser($id)
209     {
210         $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
211         $u = DB_DataObject::factory($tbl);
212         if (!$u->isAuth()) {
213             $this->err("not logged in");
214         }
215         
216         $au = $u->getAuthUser();
217         
218         // first check they have perms to do this..
219         if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
220             $this->jerr("User switching not permitted");
221         }
222                 
223         $u = DB_DataObject::factory($tbl);
224         $u->get($id);
225         if (!$u->active()) {
226             $this->jerr('Account disabled');
227         }
228         $u->login();
229             // we might need this later..
230         $this->addEvent("LOGIN-SWITCH-USER". $this->event_suffix, false, $au->name . ' TO ' . $u->name);
231         $this->jok("SWITCH");
232         
233     }
234     
235     function switchPublicUser($id)
236     {
237         $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
238         
239         $u = DB_DataObject::factory($tbl);
240         $u->get($id);
241         
242         if (!$u->active()) {
243             $this->jerr('Account disabled');
244         }
245         
246         if(!$u->loginPublic()){
247             $this->jerr('Switch fail');
248         }
249          
250         $this->jok('OK');
251     }
252     
253     var $domObj = false;
254     
255     function post($v)
256     {
257         //DB_DataObject::debugLevel(1);
258         
259         if (!empty($_REQUEST['getAuthUser'])) {
260             $this->sendAuthUserDetails();
261             exit;
262         }
263         
264         if (!empty($_REQUEST['logout'])) {
265            return $this->logout();
266         }
267          
268         if(!empty($_REQUEST['check_owner_company'])) {
269             $core_company = DB_DataObject::factory('core_company');
270             $core_company->comptype = 'OWNER';
271             $this->jok($core_company->count());
272         }
273         
274         if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
275             return $this->passwordRequest($_REQUEST['passwordRequest']);   
276         }
277                         
278                 if (!empty($_REQUEST['ResetPassword'])) {
279                         if (empty($_REQUEST['id']) || 
280                         empty($_REQUEST['ts']) ||
281                         empty($_REQUEST['key']) ||
282                         empty($_REQUEST['password1']) ||
283                         empty($_REQUEST['password2']) ||
284                         ($_REQUEST['password1'] != $_REQUEST['password2'])
285                         ) {
286                         $this->jerr("Invalid request to reset password");
287                         }
288                         
289                         $this->resetPassword($_REQUEST['id'], $_REQUEST['ts'], $_REQUEST['key'], $_REQUEST['password1'] );
290                 }
291                 
292                 
293                 if (!empty($_REQUEST['_verifyCheckSum'])) {
294                         if (empty($_REQUEST['id']) || 
295                         empty($_REQUEST['ts']) ||
296                         empty($_REQUEST['key'])
297                          
298                         ) {
299                         $this->jerr("Invalid request to reset password");
300                         }
301                         
302                         $this->verifyResetPassword($_REQUEST['id'], $_REQUEST['ts'], $_REQUEST['key']);
303                         $this->jok("Checksum is ok");
304                 }
305         
306         // this is 'classic' change password...
307         if (!empty($_REQUEST['changePassword'])) {
308             return $this->changePassword($_REQUEST);
309         }
310         
311         // login attempt..
312         
313         $ff = HTML_FlexyFramework::get();
314         $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
315         
316        
317         $u = DB_DataObject::factory($tbl);
318         
319         $ip = $this->ip_lookup();
320         // ratelimit
321         if (!empty($ip)) {
322             //DB_DataObject::DebugLevel(1);
323             $e = DB_DataObject::Factory('Events');
324             $e->action = 'LOGIN-BAD'. $this->event_suffix;
325             $e->ipaddr = $ip;
326             $e->whereAdd('event_when > NOW() - INTERVAL 10 MINUTE');
327             if ($e->count() > 5) {
328                 $this->jerror('LOGIN-RATE'. $this->event_suffix, "Login failures are rate limited - please try later");
329             }
330         }
331         
332         // this was removed before - not quite sure why.
333         // when a duplicate login account is created, this stops the old one from interfering..
334         $u->active = 1;
335         
336         // empty username = not really a hacking attempt.
337         
338         if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
339             $this->jerror('LOGIN-EMPTY'. $this->event_suffix, 'You typed the wrong Username or Password (0)');
340             exit;
341         }
342         
343         $u->authUserName($_REQUEST['username']);
344         
345         if ($u->count() > 1 || !$u->find(true)) {
346             $this->jerror('LOGIN-BAD'. $this->event_suffix,'You typed the wrong Username or Password  (1)');
347             exit;
348         }
349         
350         if (!$u->active()) { 
351             $this->jerror('LOGIN-BAD'. $this->event_suffix,'Account disabled');
352         }
353         
354         if(!empty($u->oath_key) && empty($_REQUEST['oath_password'])){
355             $this->jerror('LOGIN-2FA'. $this->event_suffix,'Your account requires Two-Factor Authentication');
356         }
357         
358         // check if config allows non-owner passwords.
359         // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
360         // perhaps it should support arrays..
361         $ff= HTML_FlexyFramework::get();
362         if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
363             //print_r($u->company());
364             $this->jerror('LOGIN-BADUSER'. $this->event_suffix, "Login not permited to outside companies"); // serious failure
365         }
366         
367         
368         // note we trim \x10 -- line break - as it was injected the front end
369         // may have an old bug on safari/chrome that added that character in certian wierd scenarios..
370         if (!$u->checkPassword(trim($_REQUEST['password'],"\x10"))) {
371             $this->jerror('LOGIN-BAD'. $this->event_suffix, 'You typed the wrong Username or Password  (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
372             exit;
373         }
374         
375         if(
376             !empty($u->oath_key) &&
377             (
378                 empty($_REQUEST['oath_password']) ||
379                 !$u->checkTwoFactorAuthentication($_REQUEST['oath_password'])
380             )
381         ) {
382             $this->jerror('LOGIN-BAD'. $this->event_suffix, 'You typed the wrong Username or Password  (3)');
383             exit;
384         }
385         
386         $this->ip_checking();
387         
388         $u->login();
389         // we might need this later..
390         $this->addEvent("LOGIN". $this->event_suffix, false, session_id());
391                 
392                 
393                 
394         if (!empty($_REQUEST['lang'])) {
395                         
396                         if (!empty($ff->languages['avail']) && !in_array($_REQUEST['lang'],$ff->languages['avail'])) {
397                                 // ignore.      
398                         } else {
399                         
400                                 $u->lang($_REQUEST['lang']);
401                         }
402         }
403          // log it..
404
405         $this->sendAuthUserDetails();
406         exit;
407          
408         
409     }
410     
411     function passwordRequest($n) 
412     {
413         $u = DB_DataObject::factory('core_person');
414         //$u->company_id = $this->company->id;
415         
416         $u->whereAdd('LENGTH(passwd) > 1');
417         $u->email = $n;
418         $u->active = 1;
419         if ($u->count() > 1 || !$u->find(true)) {
420             $this->jerr('invalid User (1)');
421         }
422         // got a avlid user..
423         if (!strlen($u->passwd)) {
424             $this->jerr('invalid User (2)');
425         }
426         // check to see if we have sent a request before..
427         
428         if ($u->no_reset_sent > 3) {
429             $this->jerr('We have issued to many resets - please contact the Administrator');
430         }
431         
432         
433         
434         
435         // sort out sender.
436         $cm = DB_DataObject::factory('core_email');
437         if (!$cm->get('name', 'ADMIN_PASSWORD_RESET')) {
438             $this->jerr("no template  Admin password reset (ADMIN_PASSWORD_RESET) exists - please run importer ");
439         }
440                 if (!$cm->active) {
441                         $this->jerr("template for Admin password reset has been disabled");
442                 }
443         /*
444         
445         $g = DB_DAtaObject::factory('Groups');
446         if (!$g->get('name', 'system-email-from')) {
447             $this->jerr("no group 'system-email-from' exists in the system");
448         }
449         $from_ar = $g->members();
450         if (count($from_ar) != 1) {
451             $this->jerr(count($from_ar) ? "To many members in the 'system-email-from' group " :
452                        "'system-email-from' group  does not have any members");
453         }
454         */
455         
456         
457         
458         // bcc..
459         $g = DB_DAtaObject::factory('core_group');
460         if (!$cm->bcc_group_id || !$g->get($cm->bcc_group_id)) {
461             $this->jerr("BCC for ADMIN_PASSWORD_RESET email has not been set");
462         }
463         $bcc = $g->members('email');
464         if (!count($bcc)) {
465             $this->jerr( "'BCC group for ADMIN_PASSWORD_RESET  does not have any members");
466         }
467         
468         
469         
470         $this->authFrom = time();
471         $this->authKey = $u->genPassKey($this->authFrom);
472         //$this->authKey = md5($u->email . $this->authFrom . $u->passwd);
473         $this->person = $u;
474         $this->bcc = $bcc;
475         $this->rcpts = $u->getEmailFrom();
476         
477         
478                 $mailer = $cm->toMailer($this, false);
479                 if (is_a($mailer,'PEAR_Error') ) {
480                         $this->addEvent('SYSERR',false, $mailer->getMessage());
481                         $this->jerr($mailer->getMessage());
482                 }
483         $sent = $mailer->send();
484                 if (is_a($sent,'PEAR_Error') ) {
485                         $this->addEvent('SYSERR',false, $sent->getMessage());
486                         $this->jerr($sent->getMessage());
487         }
488         
489         $this->addEvent('LOGIN-PASSREQ'. $this->event_suffix,$u, $u->email);
490         $uu = clone($u);
491         $uu->no_reset_sent++;
492         $uu->update($u);
493         $this->jok("done");
494         
495     }
496     
497     function verifyResetPassword($id,$t, $key)
498     {
499                 $au = $this->getAuthUser();
500                 //print_R($au);
501         if ($au) {
502             $this->jerr( "Already Logged in - no need to use Password Reset");
503         }
504         
505         $u = DB_DataObject::factory('core_person');
506         //$u->company_id = $this->company->id;
507         $u->active = 1;
508         if (!$u->get($id) || !strlen($u->passwd)) {
509             $this->jerr("Password reset link is not valid (id)");
510         }
511         
512         // validate key.. 
513         if ($key != $u->genPassKey($t)) {
514             $this->jerr("Password reset link is not valid (key)");
515         }
516         
517                 if ($t < strtotime("NOW - 1 DAY")) {
518             $this->jerr("Password reset link has expired");
519         }
520         return $u;
521         
522         
523         
524     }
525     
526     
527     function resetPassword($id,$t, $key, $newpass )
528     {
529         
530         $u = $this->verifyResetPassword($id,$t,$key);
531         
532         
533         $uu = clone($u);
534         $u->no_reset_sent = 0;
535                 if ($newpass != false) {
536                         $u->setPassword($newpass);
537                 }
538         $u->update($uu);
539                 $this->addEvent("LOGIN-CHANGEPASS". $this->event_suffix, $u);
540
541         $this->jok("Password has been Updated");
542     }
543     
544     
545     function changePassword($r)
546     {   
547         $au = $this->getAuthUser();
548         if (!$au) {
549                         $this->jerr("Password change attempted when not logged in");
550                 }
551                 $uu = clone($au);
552                 $au->setPassword($r['passwd1']);
553                 $au->update($uu);
554                 $this->addEvent("LOGIN-CHANGEPASS". $this->event_suffix, $au);
555                 $this->jok($au);
556                          
557     }
558     
559     function ip_checking()
560     {
561         if(empty($this->ip_management)){
562             return;
563         }
564         
565         $ip = $this->ip_lookup();
566         
567         if(empty($ip)){
568             $this->jerr('BAD-IP-ADDRESS', array('ip' => $ip));
569         }
570         
571         $core_ip_access = DB_DataObject::factory('core_ip_access');
572         
573         if(!DB_DataObject::factory('core_ip_access')->count()){ // first ip we always mark it as approved..
574             
575             $core_ip_access = DB_DataObject::factory('core_ip_access');
576             
577             $core_ip_access->setFrom(array(
578                 'ip' => $ip,
579                 'created_dt' => $core_ip_access->sqlValue("NOW()"),
580                 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
581                 'status' => 1,
582                 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
583                 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
584             ));
585             
586             $core_ip_access->insert();
587             
588             return;
589         }
590         
591         $core_ip_access = DB_DataObject::factory('core_ip_access');
592         
593         if(!$core_ip_access->get('ip', $ip)){ // new ip
594             
595             $core_ip_access->setFrom(array(
596                 'ip' => $ip,
597                 'created_dt' => $core_ip_access->sqlValue("NOW()"),
598                 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
599                 'status' => 0,
600                 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
601                 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
602             ));
603             
604             $core_ip_access->insert();
605             
606             $core_ip_access->sendXMPP();
607             
608             $this->jerror('NEW-IP-ADDRESS', "New IP Address = needs approving", array('ip' => $ip));
609             
610             return;
611         }
612         
613         if(empty($core_ip_access->status)){
614             $this->jerror('PENDING-IP-ADDRESS', "IP is still pending approval", array('ip' => $ip));
615         }
616         
617         if($core_ip_access->status == -1){
618             $this->jerror('BLOCKED-IP-ADDRESS', "Your IP is blocked", array('ip' => $ip));
619             return;
620         }
621         
622         if($core_ip_access->status == -2 && strtotime($core_ip_access->expire_dt) < strtotime('NOW')){
623             $this->jerror('BLOCKED-IP-ADDRESS', "Your IP is blocked", array('ip' => $ip));
624             return;
625         }
626         
627         return;
628     }
629     
630     function ip_lookup()
631     {
632
633         if (!empty($_SERVER['HTTP_CLIENT_IP'])){
634             return $_SERVER['HTTP_CLIENT_IP'];
635         }
636         
637         if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
638             return $_SERVER['HTTP_X_FORWARDED_FOR'];
639         }
640         
641         return $_SERVER['REMOTE_ADDR'];
642     }
643 }
644