xref: /dokuwiki/inc/auth.php (revision 9ce556d25f58ffae2f7937ab763f84118a6c6464)
1<?php
2/**
3 * Authentication library
4 *
5 * Including this file will automatically try to login
6 * a user by calling auth_login()
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Andreas Gohr <andi@splitbrain.org>
10 */
11
12if(!defined('DOKU_INC')) die('meh.');
13
14// some ACL level defines
15define('AUTH_NONE', 0);
16define('AUTH_READ', 1);
17define('AUTH_EDIT', 2);
18define('AUTH_CREATE', 4);
19define('AUTH_UPLOAD', 8);
20define('AUTH_DELETE', 16);
21define('AUTH_ADMIN', 255);
22
23/**
24 * Initialize the auth system.
25 *
26 * This function is automatically called at the end of init.php
27 *
28 * This used to be the main() of the auth.php
29 *
30 * @todo backend loading maybe should be handled by the class autoloader
31 * @todo maybe split into multiple functions at the XXX marked positions
32 * @triggers AUTH_LOGIN_CHECK
33 * @return bool
34 */
35function auth_setup() {
36    global $conf;
37    /* @var auth_basic $auth */
38    global $auth;
39    /* @var Input $INPUT */
40    global $INPUT;
41    global $AUTH_ACL;
42    global $lang;
43    $AUTH_ACL = array();
44
45    if(!$conf['useacl']) return false;
46
47    // load the the backend auth functions and instantiate the auth object XXX
48    if(@file_exists(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php')) {
49        require_once(DOKU_INC.'inc/auth/basic.class.php');
50        require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php');
51
52        $auth_class = "auth_".$conf['authtype'];
53        if(class_exists($auth_class)) {
54            $auth = new $auth_class();
55            if($auth->success == false) {
56                // degrade to unauthenticated user
57                unset($auth);
58                auth_logoff();
59                msg($lang['authtempfail'], -1);
60            }
61        } else {
62            nice_die($lang['authmodfailed']);
63        }
64    } else {
65        nice_die($lang['authmodfailed']);
66    }
67
68    if(!$auth) return false;
69
70    // do the login either by cookie or provided credentials XXX
71    $INPUT->set('http_credentials', false);
72    if(!$conf['rememberme']) $INPUT->set('r', false);
73
74    // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like
75    // the one presented at
76    // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used
77    // for enabling HTTP authentication with CGI/SuExec)
78    if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']))
79        $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
80    // streamline HTTP auth credentials (IIS/rewrite -> mod_php)
81    if(isset($_SERVER['HTTP_AUTHORIZATION'])) {
82        list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
83            explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
84    }
85
86    // if no credentials were given try to use HTTP auth (for SSO)
87    if(!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])) {
88        $INPUT->set('u', $_SERVER['PHP_AUTH_USER']);
89        $INPUT->set('p', $_SERVER['PHP_AUTH_PW']);
90        $INPUT->set('http_credentials', true);
91    }
92
93    // apply cleaning
94    $INPUT->set('u', $auth->cleanUser($INPUT->str('u')));
95
96    if($INPUT->str('authtok')) {
97        // when an authentication token is given, trust the session
98        auth_validateToken($INPUT->str('authtok'));
99    } elseif(!is_null($auth) && $auth->canDo('external')) {
100        // external trust mechanism in place
101        $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r'));
102    } else {
103        $evdata = array(
104            'user'     => $INPUT->str('u'),
105            'password' => $INPUT->str('p'),
106            'sticky'   => $INPUT->bool('r'),
107            'silent'   => $INPUT->bool('http_credentials')
108        );
109        trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper');
110    }
111
112    //load ACL into a global array XXX
113    $AUTH_ACL = auth_loadACL();
114
115    return true;
116}
117
118/**
119 * Loads the ACL setup and handle user wildcards
120 *
121 * @author Andreas Gohr <andi@splitbrain.org>
122 * @return array
123 */
124function auth_loadACL() {
125    global $config_cascade;
126    global $USERINFO;
127
128    if(!is_readable($config_cascade['acl']['default'])) return array();
129
130    $acl = file($config_cascade['acl']['default']);
131
132    //support user wildcard
133    $out = array();
134    foreach($acl as $line) {
135        $line = trim($line);
136        if($line{0} == '#') continue;
137        list($id,$rest) = preg_split('/\s+/',$line,2);
138
139        if(strstr($line, '%GROUP%')){
140            foreach((array) $USERINFO['grps'] as $grp){
141                $nid   = str_replace('%GROUP%',cleanID($grp),$id);
142                $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest);
143                $out[] = "$nid\t$nrest";
144            }
145        } else {
146            $id   = str_replace('%USER%',cleanID($_SERVER['REMOTE_USER']),$id);
147            $rest = str_replace('%USER%',auth_nameencode($_SERVER['REMOTE_USER']),$rest);
148            $out[] = "$id\t$rest";
149        }
150    }
151
152    return $out;
153}
154
155/**
156 * Event hook callback for AUTH_LOGIN_CHECK
157 *
158 * @param $evdata
159 * @return bool
160 */
161function auth_login_wrapper($evdata) {
162    return auth_login(
163        $evdata['user'],
164        $evdata['password'],
165        $evdata['sticky'],
166        $evdata['silent']
167    );
168}
169
170/**
171 * This tries to login the user based on the sent auth credentials
172 *
173 * The authentication works like this: if a username was given
174 * a new login is assumed and user/password are checked. If they
175 * are correct the password is encrypted with blowfish and stored
176 * together with the username in a cookie - the same info is stored
177 * in the session, too. Additonally a browserID is stored in the
178 * session.
179 *
180 * If no username was given the cookie is checked: if the username,
181 * crypted password and browserID match between session and cookie
182 * no further testing is done and the user is accepted
183 *
184 * If a cookie was found but no session info was availabe the
185 * blowfish encrypted password from the cookie is decrypted and
186 * together with username rechecked by calling this function again.
187 *
188 * On a successful login $_SERVER[REMOTE_USER] and $USERINFO
189 * are set.
190 *
191 * @author  Andreas Gohr <andi@splitbrain.org>
192 *
193 * @param   string  $user    Username
194 * @param   string  $pass    Cleartext Password
195 * @param   bool    $sticky  Cookie should not expire
196 * @param   bool    $silent  Don't show error on bad auth
197 * @return  bool             true on successful auth
198 */
199function auth_login($user, $pass, $sticky = false, $silent = false) {
200    global $USERINFO;
201    global $conf;
202    global $lang;
203    /* @var auth_basic $auth */
204    global $auth;
205
206    $sticky ? $sticky = true : $sticky = false; //sanity check
207
208    if(!$auth) return false;
209
210    if(!empty($user)) {
211        //usual login
212        if($auth->checkPass($user, $pass)) {
213            // make logininfo globally available
214            $_SERVER['REMOTE_USER'] = $user;
215            $secret                 = auth_cookiesalt(!$sticky); //bind non-sticky to session
216            auth_setCookie($user, PMA_blowfish_encrypt($pass, $secret), $sticky);
217            return true;
218        } else {
219            //invalid credentials - log off
220            if(!$silent) msg($lang['badlogin'], -1);
221            auth_logoff();
222            return false;
223        }
224    } else {
225        // read cookie information
226        list($user, $sticky, $pass) = auth_getCookie();
227        if($user && $pass) {
228            // we got a cookie - see if we can trust it
229
230            // get session info
231            $session = $_SESSION[DOKU_COOKIE]['auth'];
232            if(isset($session) &&
233                $auth->useSessionCache($user) &&
234                ($session['time'] >= time() - $conf['auth_security_timeout']) &&
235                ($session['user'] == $user) &&
236                ($session['pass'] == sha1($pass)) && //still crypted
237                ($session['buid'] == auth_browseruid())
238            ) {
239
240                // he has session, cookie and browser right - let him in
241                $_SERVER['REMOTE_USER'] = $user;
242                $USERINFO               = $session['info']; //FIXME move all references to session
243                return true;
244            }
245            // no we don't trust it yet - recheck pass but silent
246            $secret = auth_cookiesalt(!$sticky); //bind non-sticky to session
247            $pass   = PMA_blowfish_decrypt($pass, $secret);
248            return auth_login($user, $pass, $sticky, true);
249        }
250    }
251    //just to be sure
252    auth_logoff(true);
253    return false;
254}
255
256/**
257 * Checks if a given authentication token was stored in the session
258 *
259 * Will setup authentication data using data from the session if the
260 * token is correct. Will exit with a 401 Status if not.
261 *
262 * @author Andreas Gohr <andi@splitbrain.org>
263 * @param  string $token The authentication token
264 * @return boolean true (or will exit on failure)
265 */
266function auth_validateToken($token) {
267    if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']) {
268        // bad token
269        header("HTTP/1.0 401 Unauthorized");
270        print 'Invalid auth token - maybe the session timed out';
271        unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance
272        exit;
273    }
274    // still here? trust the session data
275    global $USERINFO;
276    $_SERVER['REMOTE_USER'] = $_SESSION[DOKU_COOKIE]['auth']['user'];
277    $USERINFO               = $_SESSION[DOKU_COOKIE]['auth']['info'];
278    return true;
279}
280
281/**
282 * Create an auth token and store it in the session
283 *
284 * NOTE: this is completely unrelated to the getSecurityToken() function
285 *
286 * @author Andreas Gohr <andi@splitbrain.org>
287 * @return string The auth token
288 */
289function auth_createToken() {
290    $token = md5(mt_rand());
291    @session_start(); // reopen the session if needed
292    $_SESSION[DOKU_COOKIE]['auth']['token'] = $token;
293    session_write_close();
294    return $token;
295}
296
297/**
298 * Builds a pseudo UID from browser and IP data
299 *
300 * This is neither unique nor unfakable - still it adds some
301 * security. Using the first part of the IP makes sure
302 * proxy farms like AOLs are stil okay.
303 *
304 * @author  Andreas Gohr <andi@splitbrain.org>
305 *
306 * @return  string  a MD5 sum of various browser headers
307 */
308function auth_browseruid() {
309    $ip  = clientIP(true);
310    $uid = '';
311    $uid .= $_SERVER['HTTP_USER_AGENT'];
312    $uid .= $_SERVER['HTTP_ACCEPT_ENCODING'];
313    $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
314    $uid .= $_SERVER['HTTP_ACCEPT_CHARSET'];
315    $uid .= substr($ip, 0, strpos($ip, '.'));
316    return md5($uid);
317}
318
319/**
320 * Creates a random key to encrypt the password in cookies
321 *
322 * This function tries to read the password for encrypting
323 * cookies from $conf['metadir'].'/_htcookiesalt'
324 * if no such file is found a random key is created and
325 * and stored in this file.
326 *
327 * @author  Andreas Gohr <andi@splitbrain.org>
328 * @param   bool $addsession if true, the sessionid is added to the salt
329 * @return  string
330 */
331function auth_cookiesalt($addsession = false) {
332    global $conf;
333    $file = $conf['metadir'].'/_htcookiesalt';
334    $salt = io_readFile($file);
335    if(empty($salt)) {
336        $salt = uniqid(rand(), true);
337        io_saveFile($file, $salt);
338    }
339    if($addsession) {
340        $salt .= session_id();
341    }
342    return $salt;
343}
344
345/**
346 * Log out the current user
347 *
348 * This clears all authentication data and thus log the user
349 * off. It also clears session data.
350 *
351 * @author  Andreas Gohr <andi@splitbrain.org>
352 * @param bool $keepbc - when true, the breadcrumb data is not cleared
353 */
354function auth_logoff($keepbc = false) {
355    global $conf;
356    global $USERINFO;
357    /* @var auth_basic $auth */
358    global $auth;
359
360    // make sure the session is writable (it usually is)
361    @session_start();
362
363    if(isset($_SESSION[DOKU_COOKIE]['auth']['user']))
364        unset($_SESSION[DOKU_COOKIE]['auth']['user']);
365    if(isset($_SESSION[DOKU_COOKIE]['auth']['pass']))
366        unset($_SESSION[DOKU_COOKIE]['auth']['pass']);
367    if(isset($_SESSION[DOKU_COOKIE]['auth']['info']))
368        unset($_SESSION[DOKU_COOKIE]['auth']['info']);
369    if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc']))
370        unset($_SESSION[DOKU_COOKIE]['bc']);
371    if(isset($_SERVER['REMOTE_USER']))
372        unset($_SERVER['REMOTE_USER']);
373    $USERINFO = null; //FIXME
374
375    $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
376    if(version_compare(PHP_VERSION, '5.2.0', '>')) {
377        setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true);
378    } else {
379        setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()));
380    }
381
382    if($auth) $auth->logOff();
383}
384
385/**
386 * Check if a user is a manager
387 *
388 * Should usually be called without any parameters to check the current
389 * user.
390 *
391 * The info is available through $INFO['ismanager'], too
392 *
393 * @author Andreas Gohr <andi@splitbrain.org>
394 * @see    auth_isadmin
395 * @param  string $user       Username
396 * @param  array  $groups     List of groups the user is in
397 * @param  bool   $adminonly  when true checks if user is admin
398 * @return bool
399 */
400function auth_ismanager($user = null, $groups = null, $adminonly = false) {
401    global $conf;
402    global $USERINFO;
403    /* @var auth_basic $auth */
404    global $auth;
405
406    if(!$auth) return false;
407    if(is_null($user)) {
408        if(!isset($_SERVER['REMOTE_USER'])) {
409            return false;
410        } else {
411            $user = $_SERVER['REMOTE_USER'];
412        }
413    }
414    if(is_null($groups)) {
415        $groups = (array) $USERINFO['grps'];
416    }
417
418    // check superuser match
419    if(auth_isMember($conf['superuser'], $user, $groups)) return true;
420    if($adminonly) return false;
421    // check managers
422    if(auth_isMember($conf['manager'], $user, $groups)) return true;
423
424    return false;
425}
426
427/**
428 * Check if a user is admin
429 *
430 * Alias to auth_ismanager with adminonly=true
431 *
432 * The info is available through $INFO['isadmin'], too
433 *
434 * @author Andreas Gohr <andi@splitbrain.org>
435 * @see auth_ismanager()
436 * @param  string $user       Username
437 * @param  array  $groups     List of groups the user is in
438 * @return bool
439 */
440function auth_isadmin($user = null, $groups = null) {
441    return auth_ismanager($user, $groups, true);
442}
443
444/**
445 * Match a user and his groups against a comma separated list of
446 * users and groups to determine membership status
447 *
448 * Note: all input should NOT be nameencoded.
449 *
450 * @param $memberlist string commaseparated list of allowed users and groups
451 * @param $user       string user to match against
452 * @param $groups     array  groups the user is member of
453 * @return bool       true for membership acknowledged
454 */
455function auth_isMember($memberlist, $user, array $groups) {
456    /* @var auth_basic $auth */
457    global $auth;
458    if(!$auth) return false;
459
460    // clean user and groups
461    if(!$auth->isCaseSensitive()) {
462        $user   = utf8_strtolower($user);
463        $groups = array_map('utf8_strtolower', $groups);
464    }
465    $user   = $auth->cleanUser($user);
466    $groups = array_map(array($auth, 'cleanGroup'), $groups);
467
468    // extract the memberlist
469    $members = explode(',', $memberlist);
470    $members = array_map('trim', $members);
471    $members = array_unique($members);
472    $members = array_filter($members);
473
474    // compare cleaned values
475    foreach($members as $member) {
476        if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member);
477        if($member[0] == '@') {
478            $member = $auth->cleanGroup(substr($member, 1));
479            if(in_array($member, $groups)) return true;
480        } else {
481            $member = $auth->cleanUser($member);
482            if($member == $user) return true;
483        }
484    }
485
486    // still here? not a member!
487    return false;
488}
489
490/**
491 * Convinience function for auth_aclcheck()
492 *
493 * This checks the permissions for the current user
494 *
495 * @author  Andreas Gohr <andi@splitbrain.org>
496 *
497 * @param  string  $id  page ID (needs to be resolved and cleaned)
498 * @return int          permission level
499 */
500function auth_quickaclcheck($id) {
501    global $conf;
502    global $USERINFO;
503    # if no ACL is used always return upload rights
504    if(!$conf['useacl']) return AUTH_UPLOAD;
505    return auth_aclcheck($id, $_SERVER['REMOTE_USER'], $USERINFO['grps']);
506}
507
508/**
509 * Returns the maximum rights a user has for
510 * the given ID or its namespace
511 *
512 * @author  Andreas Gohr <andi@splitbrain.org>
513 *
514 * @param  string       $id     page ID (needs to be resolved and cleaned)
515 * @param  string       $user   Username
516 * @param  array|null   $groups Array of groups the user is in
517 * @return int             permission level
518 */
519function auth_aclcheck($id, $user, $groups) {
520    global $conf;
521    global $AUTH_ACL;
522    /* @var auth_basic $auth */
523    global $auth;
524
525    // if no ACL is used always return upload rights
526    if(!$conf['useacl']) return AUTH_UPLOAD;
527    if(!$auth) return AUTH_NONE;
528
529    //make sure groups is an array
530    if(!is_array($groups)) $groups = array();
531
532    //if user is superuser or in superusergroup return 255 (acl_admin)
533    if(auth_isadmin($user, $groups)) {
534        return AUTH_ADMIN;
535    }
536
537    $ci = '';
538    if(!$auth->isCaseSensitive()) $ci = 'ui';
539
540    $user   = $auth->cleanUser($user);
541    $groups = array_map(array($auth, 'cleanGroup'), (array) $groups);
542    $user   = auth_nameencode($user);
543
544    //prepend groups with @ and nameencode
545    $cnt = count($groups);
546    for($i = 0; $i < $cnt; $i++) {
547        $groups[$i] = '@'.auth_nameencode($groups[$i]);
548    }
549
550    $ns   = getNS($id);
551    $perm = -1;
552
553    if($user || count($groups)) {
554        //add ALL group
555        $groups[] = '@ALL';
556        //add User
557        if($user) $groups[] = $user;
558    } else {
559        $groups[] = '@ALL';
560    }
561
562    //check exact match first
563    $matches = preg_grep('/^'.preg_quote($id, '/').'\s+(\S+)\s+/'.$ci, $AUTH_ACL);
564    if(count($matches)) {
565        foreach($matches as $match) {
566            $match = preg_replace('/#.*$/', '', $match); //ignore comments
567            $acl   = preg_split('/\s+/', $match);
568            if(!in_array($acl[1], $groups)) {
569                continue;
570            }
571            if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
572            if($acl[2] > $perm) {
573                $perm = $acl[2];
574            }
575        }
576        if($perm > -1) {
577            //we had a match - return it
578            return $perm;
579        }
580    }
581
582    //still here? do the namespace checks
583    if($ns) {
584        $path = $ns.':*';
585    } else {
586        $path = '*'; //root document
587    }
588
589    do {
590        $matches = preg_grep('/^'.preg_quote($path, '/').'\s+(\S+)\s+/'.$ci, $AUTH_ACL);
591        if(count($matches)) {
592            foreach($matches as $match) {
593                $match = preg_replace('/#.*$/', '', $match); //ignore comments
594                $acl   = preg_split('/\s+/', $match);
595                if(!in_array($acl[1], $groups)) {
596                    continue;
597                }
598                if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
599                if($acl[2] > $perm) {
600                    $perm = $acl[2];
601                }
602            }
603            //we had a match - return it
604            if($perm != -1) {
605                return $perm;
606            }
607        }
608        //get next higher namespace
609        $ns = getNS($ns);
610
611        if($path != '*') {
612            $path = $ns.':*';
613            if($path == ':*') $path = '*';
614        } else {
615            //we did this already
616            //looks like there is something wrong with the ACL
617            //break here
618            msg('No ACL setup yet! Denying access to everyone.');
619            return AUTH_NONE;
620        }
621    } while(1); //this should never loop endless
622    return AUTH_NONE;
623}
624
625/**
626 * Encode ASCII special chars
627 *
628 * Some auth backends allow special chars in their user and groupnames
629 * The special chars are encoded with this function. Only ASCII chars
630 * are encoded UTF-8 multibyte are left as is (different from usual
631 * urlencoding!).
632 *
633 * Decoding can be done with rawurldecode
634 *
635 * @author Andreas Gohr <gohr@cosmocode.de>
636 * @see rawurldecode()
637 */
638function auth_nameencode($name, $skip_group = false) {
639    global $cache_authname;
640    $cache =& $cache_authname;
641    $name  = (string) $name;
642
643    // never encode wildcard FS#1955
644    if($name == '%USER%') return $name;
645    if($name == '%GROUP%') return $name;
646
647    if(!isset($cache[$name][$skip_group])) {
648        if($skip_group && $name{0} == '@') {
649            $cache[$name][$skip_group] = '@'.preg_replace(
650                '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e',
651                "'%'.dechex(ord(substr('\\1',-1)))", substr($name, 1)
652            );
653        } else {
654            $cache[$name][$skip_group] = preg_replace(
655                '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e',
656                "'%'.dechex(ord(substr('\\1',-1)))", $name
657            );
658        }
659    }
660
661    return $cache[$name][$skip_group];
662}
663
664/**
665 * Create a pronouncable password
666 *
667 * @author  Andreas Gohr <andi@splitbrain.org>
668 * @link    http://www.phpbuilder.com/annotate/message.php3?id=1014451
669 *
670 * @return string  pronouncable password
671 */
672function auth_pwgen() {
673    $pw = '';
674    $c  = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones
675    $v  = 'aeiou'; //vowels
676    $a  = $c.$v; //both
677
678    //use two syllables...
679    for($i = 0; $i < 2; $i++) {
680        $pw .= $c[rand(0, strlen($c) - 1)];
681        $pw .= $v[rand(0, strlen($v) - 1)];
682        $pw .= $a[rand(0, strlen($a) - 1)];
683    }
684    //... and add a nice number
685    $pw .= rand(10, 99);
686
687    return $pw;
688}
689
690/**
691 * Sends a password to the given user
692 *
693 * @author  Andreas Gohr <andi@splitbrain.org>
694 * @param string $user Login name of the user
695 * @param string $password The new password in clear text
696 * @return bool  true on success
697 */
698function auth_sendPassword($user, $password) {
699    global $lang;
700    /* @var auth_basic $auth */
701    global $auth;
702    if(!$auth) return false;
703
704    $user     = $auth->cleanUser($user);
705    $userinfo = $auth->getUserData($user);
706
707    if(!$userinfo['mail']) return false;
708
709    $text = rawLocale('password');
710    $trep = array(
711        'FULLNAME' => $userinfo['name'],
712        'LOGIN'    => $user,
713        'PASSWORD' => $password
714    );
715
716    $mail = new Mailer();
717    $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>');
718    $mail->subject($lang['regpwmail']);
719    $mail->setBody($text, $trep);
720    return $mail->send();
721}
722
723/**
724 * Register a new user
725 *
726 * This registers a new user - Data is read directly from $_POST
727 *
728 * @author  Andreas Gohr <andi@splitbrain.org>
729 * @return bool  true on success, false on any error
730 */
731function register() {
732    global $lang;
733    global $conf;
734    /* @var auth_basic $auth */
735    global $auth;
736
737    if(!$_POST['save']) return false;
738    if(!actionOK('register')) return false;
739
740    //clean username
741    $_POST['login'] = trim($auth->cleanUser($_POST['login']));
742
743    //clean fullname and email
744    $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $_POST['fullname']));
745    $_POST['email']    = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $_POST['email']));
746
747    if(empty($_POST['login']) ||
748        empty($_POST['fullname']) ||
749        empty($_POST['email'])
750    ) {
751        msg($lang['regmissing'], -1);
752        return false;
753    }
754
755    if($conf['autopasswd']) {
756        $pass = auth_pwgen(); // automatically generate password
757    } elseif(empty($_POST['pass']) ||
758        empty($_POST['passchk'])
759    ) {
760        msg($lang['regmissing'], -1); // complain about missing passwords
761        return false;
762    } elseif($_POST['pass'] != $_POST['passchk']) {
763        msg($lang['regbadpass'], -1); // complain about misspelled passwords
764        return false;
765    } else {
766        $pass = $_POST['pass']; // accept checked and valid password
767    }
768
769    //check mail
770    if(!mail_isvalid($_POST['email'])) {
771        msg($lang['regbadmail'], -1);
772        return false;
773    }
774
775    //okay try to create the user
776    if(!$auth->triggerUserMod('create', array($_POST['login'], $pass, $_POST['fullname'], $_POST['email']))) {
777        msg($lang['reguexists'], -1);
778        return false;
779    }
780
781    // create substitutions for use in notification email
782    $substitutions = array(
783        'NEWUSER'  => $_POST['login'],
784        'NEWNAME'  => $_POST['fullname'],
785        'NEWEMAIL' => $_POST['email'],
786    );
787
788    if(!$conf['autopasswd']) {
789        msg($lang['regsuccess2'], 1);
790        notify('', 'register', '', $_POST['login'], false, $substitutions);
791        return true;
792    }
793
794    // autogenerated password? then send him the password
795    if(auth_sendPassword($_POST['login'], $pass)) {
796        msg($lang['regsuccess'], 1);
797        notify('', 'register', '', $_POST['login'], false, $substitutions);
798        return true;
799    } else {
800        msg($lang['regmailfail'], -1);
801        return false;
802    }
803}
804
805/**
806 * Update user profile
807 *
808 * @author    Christopher Smith <chris@jalakai.co.uk>
809 */
810function updateprofile() {
811    global $conf;
812    global $lang;
813    /* @var auth_basic $auth */
814    global $auth;
815    /* @var Input $INPUT */
816    global $INPUT;
817
818    if(!$INPUT->post->bool('save')) return false;
819    if(!checkSecurityToken()) return false;
820
821    if(!actionOK('profile')) {
822        msg($lang['profna'], -1);
823        return false;
824    }
825
826    $changes         = array();
827    $changes['pass'] = $INPUT->post->str('newpass');
828    $changes['name'] = $INPUT->post->str('fullname');
829    $changes['mail'] = $INPUT->post->str('email');
830
831    // check misspelled passwords
832    if($changes['pass'] != $INPUT->post->str('passchk')) {
833        msg($lang['regbadpass'], -1);
834        return false;
835    }
836
837    // clean fullname and email
838    $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name']));
839    $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail']));
840
841    // no empty name and email (except the backend doesn't support them)
842    if((empty($changes['name']) && $auth->canDo('modName')) ||
843        (empty($changes['mail']) && $auth->canDo('modMail'))
844    ) {
845        msg($lang['profnoempty'], -1);
846        return false;
847    }
848    if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) {
849        msg($lang['regbadmail'], -1);
850        return false;
851    }
852
853    $changes = array_filter($changes);
854
855    // check for unavailable capabilities
856    if(!$auth->canDo('modName')) unset($changes['name']);
857    if(!$auth->canDo('modMail')) unset($changes['mail']);
858    if(!$auth->canDo('modPass')) unset($changes['pass']);
859
860    // anything to do?
861    if(!count($changes)) {
862        msg($lang['profnochange'], -1);
863        return false;
864    }
865
866    if($conf['profileconfirm']) {
867        if(!$auth->checkPass($_SERVER['REMOTE_USER'], $INPUT->post->str('oldpass'))) {
868            msg($lang['badlogin'], -1);
869            return false;
870        }
871    }
872
873    if($result = $auth->triggerUserMod('modify', array($_SERVER['REMOTE_USER'], $changes))) {
874        // update cookie and session with the changed data
875        if($changes['pass']) {
876            list( /*user*/, $sticky, /*pass*/) = auth_getCookie();
877            $pass = PMA_blowfish_encrypt($changes['pass'], auth_cookiesalt(!$sticky));
878            auth_setCookie($_SERVER['REMOTE_USER'], $pass, (bool) $sticky);
879        }
880        return true;
881    }
882
883    return false;
884}
885
886/**
887 * Send a  new password
888 *
889 * This function handles both phases of the password reset:
890 *
891 *   - handling the first request of password reset
892 *   - validating the password reset auth token
893 *
894 * @author Benoit Chesneau <benoit@bchesneau.info>
895 * @author Chris Smith <chris@jalakai.co.uk>
896 * @author Andreas Gohr <andi@splitbrain.org>
897 *
898 * @return bool true on success, false on any error
899 */
900function act_resendpwd() {
901    global $lang;
902    global $conf;
903    /* @var auth_basic $auth */
904    global $auth;
905    /* @var Input $INPUT */
906    global $INPUT;
907
908    if(!actionOK('resendpwd')) {
909        msg($lang['resendna'], -1);
910        return false;
911    }
912
913    $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth'));
914
915    if($token) {
916        // we're in token phase - get user info from token
917
918        $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth';
919        if(!@file_exists($tfile)) {
920            msg($lang['resendpwdbadauth'], -1);
921            $INPUT->remove('pwauth');
922            return false;
923        }
924        // token is only valid for 3 days
925        if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) {
926            msg($lang['resendpwdbadauth'], -1);
927            $INPUT->remove('pwauth');
928            @unlink($tfile);
929            return false;
930        }
931
932        $user     = io_readfile($tfile);
933        $userinfo = $auth->getUserData($user);
934        if(!$userinfo['mail']) {
935            msg($lang['resendpwdnouser'], -1);
936            return false;
937        }
938
939        if(!$conf['autopasswd']) { // we let the user choose a password
940            $pass = $INPUT->str('pass');
941
942            // password given correctly?
943            if(!$pass) return false;
944            if($pass != $INPUT->str('passchk')) {
945                msg($lang['regbadpass'], -1);
946                return false;
947            }
948
949            // change it
950            if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) {
951                msg('error modifying user data', -1);
952                return false;
953            }
954
955        } else { // autogenerate the password and send by mail
956
957            $pass = auth_pwgen();
958            if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) {
959                msg('error modifying user data', -1);
960                return false;
961            }
962
963            if(auth_sendPassword($user, $pass)) {
964                msg($lang['resendpwdsuccess'], 1);
965            } else {
966                msg($lang['regmailfail'], -1);
967            }
968        }
969
970        @unlink($tfile);
971        return true;
972
973    } else {
974        // we're in request phase
975
976        if(!$INPUT->post->bool('save')) return false;
977
978        if(!$INPUT->post->str('login')) {
979            msg($lang['resendpwdmissing'], -1);
980            return false;
981        } else {
982            $user = trim($auth->cleanUser($INPUT->post->str('login')));
983        }
984
985        $userinfo = $auth->getUserData($user);
986        if(!$userinfo['mail']) {
987            msg($lang['resendpwdnouser'], -1);
988            return false;
989        }
990
991        // generate auth token
992        $token = md5(auth_cookiesalt().$user); //secret but user based
993        $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth';
994        $url   = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&');
995
996        io_saveFile($tfile, $user);
997
998        $text = rawLocale('pwconfirm');
999        $trep = array(
1000            'FULLNAME' => $userinfo['name'],
1001            'LOGIN'    => $user,
1002            'CONFIRM'  => $url
1003        );
1004
1005        $mail = new Mailer();
1006        $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>');
1007        $mail->subject($lang['regpwmail']);
1008        $mail->setBody($text, $trep);
1009        if($mail->send()) {
1010            msg($lang['resendpwdconfirm'], 1);
1011        } else {
1012            msg($lang['regmailfail'], -1);
1013        }
1014        return true;
1015    }
1016    // never reached
1017}
1018
1019/**
1020 * Encrypts a password using the given method and salt
1021 *
1022 * If the selected method needs a salt and none was given, a random one
1023 * is chosen.
1024 *
1025 * @author  Andreas Gohr <andi@splitbrain.org>
1026 * @param string $clear The clear text password
1027 * @param string $method The hashing method
1028 * @param string $salt A salt, null for random
1029 * @return  string  The crypted password
1030 */
1031function auth_cryptPassword($clear, $method = '', $salt = null) {
1032    global $conf;
1033    if(empty($method)) $method = $conf['passcrypt'];
1034
1035    $pass = new PassHash();
1036    $call = 'hash_'.$method;
1037
1038    if(!method_exists($pass, $call)) {
1039        msg("Unsupported crypt method $method", -1);
1040        return false;
1041    }
1042
1043    return $pass->$call($clear, $salt);
1044}
1045
1046/**
1047 * Verifies a cleartext password against a crypted hash
1048 *
1049 * @author Andreas Gohr <andi@splitbrain.org>
1050 * @param  string $clear The clear text password
1051 * @param  string $crypt The hash to compare with
1052 * @return bool true if both match
1053 */
1054function auth_verifyPassword($clear, $crypt) {
1055    $pass = new PassHash();
1056    return $pass->verify_hash($clear, $crypt);
1057}
1058
1059/**
1060 * Set the authentication cookie and add user identification data to the session
1061 *
1062 * @param string  $user       username
1063 * @param string  $pass       encrypted password
1064 * @param bool    $sticky     whether or not the cookie will last beyond the session
1065 * @return bool
1066 */
1067function auth_setCookie($user, $pass, $sticky) {
1068    global $conf;
1069    /* @var auth_basic $auth */
1070    global $auth;
1071    global $USERINFO;
1072
1073    if(!$auth) return false;
1074    $USERINFO = $auth->getUserData($user);
1075
1076    // set cookie
1077    $cookie    = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass);
1078    $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
1079    $time      = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year
1080    if(version_compare(PHP_VERSION, '5.2.0', '>')) {
1081        setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true);
1082    } else {
1083        setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()));
1084    }
1085    // set session
1086    $_SESSION[DOKU_COOKIE]['auth']['user'] = $user;
1087    $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass);
1088    $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid();
1089    $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
1090    $_SESSION[DOKU_COOKIE]['auth']['time'] = time();
1091
1092    return true;
1093}
1094
1095/**
1096 * Returns the user, (encrypted) password and sticky bit from cookie
1097 *
1098 * @returns array
1099 */
1100function auth_getCookie() {
1101    if(!isset($_COOKIE[DOKU_COOKIE])) {
1102        return array(null, null, null);
1103    }
1104    list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3);
1105    $sticky = (bool) $sticky;
1106    $pass   = base64_decode($pass);
1107    $user   = base64_decode($user);
1108    return array($user, $sticky, $pass);
1109}
1110
1111//Setup VIM: ex: et ts=2 :
1112