Changeset d2993ee in github


Ignore:
Timestamp:
Oct 30, 2008 5:17:36 AM (5 years ago)
Author:
alecpl <alec@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.6, release-0.7, release-0.8
Children:
d0d94ca
Parents:
d245963
Message:
  • added BYE response simple support to prevent from endless loops in imap.inc (#1483956)
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • CHANGELOG

    r8498dce rd2993ee  
    55---------- 
    66- Fix problem with numeric folder names (#1485527) 
     7- Added BYE response simple support to prevent from endless loops in imap.inc (#1483956) 
    78 
    892008/10/27 (alec) 
  • index.php

    rc9ca6ad rd2993ee  
    101101  } 
    102102  else { 
    103     $OUTPUT->show_message($IMAP->error_code == -1 ? 'imaperror' : 'loginfailed', 'warning'); 
     103    $OUTPUT->show_message($IMAP->error_code == -1 ? 'loginfailed' : 'imaperror', 'warning'); 
    104104    $RCMAIL->kill_session(); 
    105105  } 
  • program/lib/imap.inc

    r7ae46f2 rd2993ee  
    6969                - added iil_PutLineC()  
    7070                - fixed iil_C_Sort() to support very long and/or divided responses 
     71                - added BYE response simple support for endless loop prevention 
     72                - added 3rd argument in iil_StartsWith* functions 
    7173 
    7274********************************************************/ 
     
    287289                } else if (strcasecmp($a[1], 'BAD') == 0) { 
    288290                        return -2; 
     291                } else if (strcasecmp($a[1], 'BYE') == 0) { 
     292                        return -3; 
    289293                } 
    290294        } 
    291         return -3; 
     295        return -4; 
    292296} 
    293297 
    294298// check if $string starts with $match 
    295 function iil_StartsWith($string, $match) { 
     299function iil_StartsWith($string, $match, $bye=false) { 
    296300        $len = strlen($match); 
    297301        if ($len == 0) { 
     
    301305                return true; 
    302306        } 
     307        if ($bye && strncmp($string, '* BYE ', 6) == 0) { 
     308                return true; 
     309        } 
    303310        return false; 
    304311} 
    305312 
    306 function iil_StartsWithI($string, $match) { 
     313function iil_StartsWithI($string, $match, $bye=false) { 
    307314        $len = strlen($match); 
    308315        if ($len == 0) { 
     
    310317        } 
    311318        if (strncasecmp($string, $match, $len) == 0) { 
     319                return true; 
     320        } 
     321        if ($bye && strncmp($string, '* BYE ', 6) == 0) { 
    312322                return true; 
    313323        } 
     
    382392     
    383393    // process result 
    384     if (iil_ParseResult($line) == 0) { 
     394    $result = iil_ParseResult($line); 
     395    if ($result == 0) { 
    385396        $conn->error    .= ''; 
    386397        $conn->errorNum  = 0; 
    387398        return $conn->fp; 
    388399    } 
     400 
     401    if ($result == -3) fclose($conn->fp); // BYE response 
     402 
    389403    $conn->error    .= 'Authentication for ' . $user . ' failed (AUTH): "'; 
    390404    $conn->error    .= htmlspecialchars($line) . '"'; 
    391     $conn->errorNum  = -2; 
    392     return false; 
     405    $conn->errorNum  = $result; 
     406 
     407    return $result; 
    393408} 
    394409 
     
    402417            break; 
    403418        } 
    404     } while (!iil_StartsWith($line, "a001 ")); 
    405     $a = explode(' ', $line); 
    406     if (strcmp($a[1], 'OK') == 0) { 
    407         $result          = $conn->fp; 
     419    } while (!iil_StartsWith($line, 'a001 ', true)); 
     420     
     421    // process result 
     422    $result = iil_ParseResult($line); 
     423 
     424    if ($result == 0) { 
    408425        $conn->error    .= ''; 
    409426        $conn->errorNum  = 0; 
    410         return $result; 
     427        return $conn->fp; 
    411428    } 
    412     $result = false; 
     429 
    413430    fclose($conn->fp); 
    414431     
    415432    $conn->error    .= 'Authentication for ' . $user . ' failed (LOGIN): "'; 
    416433    $conn->error    .= htmlspecialchars($line)."\""; 
    417     $conn->errorNum  = -2; 
     434    $conn->errorNum  = $result; 
    418435 
    419436    return $result; 
     
    473490                        $data = iil_ParseNamespace2(substr($line,11), $i, 0, 0); 
    474491                } 
    475         } while (!iil_StartsWith($line, "ns1")); 
     492        } while (!iil_StartsWith($line, 'ns1', true)); 
    476493         
    477494        if (!is_array($data)) { 
     
    504521        $iil_error = ''; 
    505522        $iil_errornum = 0; 
    506          
    507         //strip slashes 
    508         // $user = stripslashes($user); 
    509         // $password = stripslashes($password); 
    510523         
    511524        //set auth method 
     
    544557        //check input 
    545558        if (empty($host)) { 
    546                 $iil_error .= "Invalid host\n"; 
     559                $iil_error = "Empty host"; 
     560                $iil_errornum = -1; 
     561                return false; 
    547562        } 
    548563        if (empty($user)) { 
    549                 $iil_error .= "Invalid user\n"; 
     564                $iil_error = "Empty user"; 
     565                $iil_errornum = -1; 
     566                return false; 
    550567        } 
    551568        if (empty($password)) { 
    552                 $iil_error .= "Invalid password\n"; 
    553         } 
    554         if (!empty($iil_error)) { 
     569                $iil_error = "Empty password"; 
     570                $iil_errornum = -1; 
    555571                return false; 
    556572        } 
     
    607623                        //got a challenge string, try CRAM-5 
    608624                        $result = iil_C_Authenticate($conn, $user, $password, substr($line,2)); 
    609              
     625                         
     626                        // stop if server sent BYE response 
     627                        if($result == -3) { 
     628                                $iil_error = $conn->error; 
     629                                $iil_errornum = $conn->errorNum; 
     630                                return false; 
     631                        } 
    610632                        $conn->message .= "Tried CRAM-MD5: $result \n"; 
    611633                } else { 
     
    618640                //do plain text auth 
    619641                $result = iil_C_Login($conn, $user, $password); 
    620                 $conn->message.="Tried PLAIN: $result \n"; 
     642                $conn->message .= "Tried PLAIN: $result \n"; 
    621643        } 
    622644                 
    623645        $conn->message .= $auth; 
    624646                         
    625         if ($result) { 
     647        if (!is_int($result)) { 
    626648                iil_C_Namespace($conn); 
    627649                return $conn; 
     
    753775                        if (($a[0] == '*') && (strcasecmp($a[2], 'RECENT') == 0)) { 
    754776                            $result = (int) $a[1]; 
    755             } 
    756                 } while (!iil_StartsWith($a[0], 'a002')); 
     777                        } 
     778                } while (!iil_StartsWith($a[0], 'a002', true)); 
    757779 
    758780                iil_PutLine($fp, "a003 LOGOUT"); 
     
    791813                                $conn->permanentflags = explode(' ', $match[1]); 
    792814                        } 
    793                 } while (!iil_StartsWith($line, 'sel1')); 
     815                } while (!iil_StartsWith($line, 'sel1', true)); 
    794816 
    795817                $a = explode(' ', $line); 
     
    839861function iil_StrToTime($str) { 
    840862        $IMAP_MONTHS    = $GLOBALS['IMAP_MONTHS']; 
    841         $IMAP_SERVER_TZ = $GLOBALS['IMAP_SERVER_TR']; 
     863        $IMAP_SERVER_TZ = $GLOBALS['IMAP_SERVER_TZ']; 
    842864                 
    843865        if ($str) { 
     
    918940                        $data .= $line; 
    919941                } 
    920         } while ($line[0]!='s'); 
     942        } while (!iil_StartsWith($line, 's ', true)); 
    921943         
    922944        if (empty($data)) { 
     
    10401062                        } 
    10411063                        */ 
    1042                 } while (!iil_StartsWith($line, $key)); 
     1064                } while (!iil_StartsWith($line, $key, true)); 
    10431065 
    10441066        }else if ($mode == 6) { 
     
    10711093                                $a = explode(' ', $line); 
    10721094                        } 
    1073                 } while (!iil_StartsWith($a[0], $key)); 
     1095                } while (!iil_StartsWith($a[0], $key, true)); 
    10741096        } else { 
    10751097                if ($mode >= 3) { 
     
    11131135                                } 
    11141136                        } 
    1115                 } while (!iil_StartsWith($line, $key)); 
     1137                } while (!iil_StartsWith($line, $key, true)); 
    11161138        } 
    11171139 
     
    19521974                                $c++; 
    19531975                        } 
    1954                 } while (!iil_StartsWith($line, 'exp1')); 
     1976                } while (!iil_StartsWith($line, 'exp1', true)); 
    19551977                 
    19561978                if (iil_ParseResult($line) == 0) { 
     
    19842006                            $c++; 
    19852007                        } 
    1986                 } while (!iil_StartsWith($line, 'flg')); 
     2008                } while (!iil_StartsWith($line, 'flg', true)); 
    19872009 
    19882010                if (iil_ParseResult($line) == 0) { 
     
    20942116                                $messages = explode(' ', $str); 
    20952117                        } 
    2096                 } while (!iil_StartsWith($line, 'srch1')); 
     2118                } while (!iil_StartsWith($line, 'srch1', true)); 
    20972119                 
    20982120                $result_code = iil_ParseResult($line); 
     
    21522174                        } 
    21532175                } 
    2154         } while (!iil_StartsWith($line, 'ghd')); 
     2176        } while (!iil_StartsWith($line, 'ghd', true)); 
    21552177 
    21562178        if (strlen($delimiter)>0) { 
     
    21672189                        $data = iil_ParseNamespace2(substr($line,11), $i, 0, 0); 
    21682190                } 
    2169         } while (!iil_StartsWith($line, 'ns1')); 
     2191        } while (!iil_StartsWith($line, 'ns1', true)); 
    21702192                 
    21712193        if (!is_array($data)) { 
     
    22352257                        $i++; 
    22362258                } 
    2237         } while (!iil_StartsWith($line, 'lmb')); 
     2259        } while (!iil_StartsWith($line, 'lmb', true)); 
    22382260 
    22392261        if (is_array($folders)) { 
     
    23042326                        $i++; 
    23052327                } 
    2306         } while (!iil_StartsWith($line, 'lsb')); 
     2328        } while (!iil_StartsWith($line, 'lsb', true)); 
    23072329 
    23082330        if (is_array($folders)) { 
     
    23602382                        $a    = explode(' ', $line); 
    23612383                        if (($line[0] == '*') && ($a[2] == 'FETCH') 
    2362                 && ($line[strlen($line)-1] != ')')) { 
     2384                                && ($line[strlen($line)-1] != ')')) { 
    23632385                                $line=iil_ReadLine($fp, 300); 
    23642386                                while (trim($line) != ')') { 
     
    23672389                                } 
    23682390                        } 
    2369                 } while (strcmp($a[0], $key) != 0); 
     2391                } while (strcmp($a[0], $key) != 0 && ($a[0] != '*' || $a[1] != 'BYE')); 
    23702392        } 
    23712393         
     
    24032425                } while ($a[2] != 'FETCH'); 
    24042426                $len = strlen($line); 
    2405      
    2406                 if ($line[$len-1] == ')') { 
     2427 
     2428                // handle empty "* X FETCH ()" response 
     2429                if ($line[$len-1] == ')' && $line[$len-2] != '(') { 
    24072430                        // one line response, get everything between first and last quotes 
    24082431                        if (substr($line, -4, 3) == 'NIL') { 
     
    24592482                do { 
    24602483                        $line = iil_ReadLine($fp, 1024); 
    2461                 } while (!iil_StartsWith($line, $key)); 
     2484                } while (!iil_StartsWith($line, $key, true)); 
    24622485         
    24632486                if ($mode == 3 && $file) { 
     
    26722695                                $quota_line = $line; 
    26732696                        } 
    2674                 } while (!iil_StartsWith($line, 'QUOT1')); 
     2697                } while (!iil_StartsWith($line, 'QUOT1', true)); 
    26752698        } 
    26762699         
Note: See TracChangeset for help on using the changeset viewer.