/doc/tips/integrated_issue_tracking_with_ikiwiki/

tle='Atom feed' href='https://source.jones.dk/ledger-smb/atom/LedgerSMB/Session/DB.pm?h=1.2.13+jones' type='application/atom+xml'/>
summaryrefslogtreecommitdiff
path: root/LedgerSMB/Session/DB.pm
blob: eb550c621b498aa12eb1d19411f4e02cee5c775a (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. #
  7. # Copyright (C) 2006
  8. # This work contains copyrighted information from a number of sources all used
  9. # with permission. It is released under the GNU General Public License
  10. # Version 2 or, at your option, any later version. See COPYRIGHT file for
  11. # details.
  12. #
  13. #
  14. #======================================================================
  15. #
  16. # This file has undergone whitespace cleanup.
  17. #
  18. #======================================================================
  19. # This package contains session related functions:
  20. #
  21. # check - checks validity of session based on the user's cookie and login
  22. #
  23. # create - creates a new session, writes cookie upon success
  24. #
  25. # destroy - destroys session
  26. #
  27. # password_check - compares the password with the stored cryted password
  28. # (ver. < 1.2) and the md5 one (ver. >= 1.2)
  29. #====================================================================
  30. package Session;
  31. sub session_check {
  32. use Time::HiRes qw(gettimeofday);
  33. my ($cookie, $form) = @_;
  34. my ($sessionID, $transactionID, $token) = split /:/, $cookie;
  35. # use the central database handle
  36. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  37. my $checkQuery = $dbh->prepare("SELECT u.username, s.transaction_id
  38. FROM session as s, users as u
  39. WHERE s.session_id = ?
  40. AND s.token = ?
  41. AND s.users_id = u.id
  42. AND s.last_used > now() - ?::interval");
  43. my $updateAge = $dbh->prepare("UPDATE session
  44. SET last_used = now(),
  45. transaction_id = ?
  46. WHERE session_id = ?;");
  47. #must be an integer
  48. $sessionID =~ s/[^0-9]//g;
  49. $sessionID = int $sessionID;
  50. $transactionID =~ s/[^0-9]//g;
  51. $transactionID = int $transactionID;
  52. #must be 32 chars long and contain hex chars
  53. $token =~ s/[^0-9a-f]//g;
  54. $token = substr($token, 0, 32);
  55. if (!$myconfig{timeout}){
  56. $timeout = "1 day";
  57. } else {
  58. $timeout = "$myconfig{timeout} seconds";
  59. }
  60. $checkQuery->execute($sessionID, $token, $timeout)
  61. || $form->dberror(__FILE__.':'.__LINE__.': Looking for session: ');
  62. my $sessionValid = $checkQuery->rows;
  63. if($sessionValid){
  64. #user has a valid session cookie, now check the user
  65. my ($sessionLogin, $sessionTransaction) = $checkQuery->fetchrow_array;
  66. my $login = $form->{login};
  67. $login =~ s/[^a-zA-Z0-9._+@'-]//g;
  68. if(($sessionLogin eq $login) and ($sessionTransaction eq $transactionID)){
  69. #microseconds are more than random enough for transaction_id
  70. my ($ignore, $newTransactionID) = gettimeofday();
  71. $newTransactionID = int $newTransactionID;
  72. $updateAge->execute($newTransactionID, $sessionID)
  73. || $form->dberror(__FILE__.':'.__LINE__.': Updating session age: ');
  74. $newCookieValue = $sessionID . ':'.$newTransactionID.':' . $token;
  75. #now update the cookie in the browser
  76. print qq|Set-Cookie: LedgerSMB=$newCookieValue; path=/;\n|;
  77. return 1;
  78. } else {
  79. #something's wrong, they have the cookie, but wrong user or the wrong transaction id. Hijack attempt?
  80. #destroy the session
  81. my $sessionDestroy = $dbh->prepare("");
  82. #delete the cookie in the browser
  83. print qq|Set-Cookie: LedgerSMB=; path=/;\n|;
  84. return 0;
  85. }
  86. } else {
  87. #cookie is not valid
  88. #delete the cookie in the browser
  89. print qq|Set-Cookie: LedgerSMB=; path=/;\n|;
  90. return 0;
  91. }
  92. }
  93. sub session_create {
  94. use Time::HiRes qw(gettimeofday);
  95. #microseconds are more than random enough for transaction_id