summaryrefslogtreecommitdiff
path: root/LedgerSMB/Session/DB.pm
blob: af8b5ac4a674da1706b209957fe3129654dd3876 (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. my ($cookie, $form) = @_;
  33. my ($sessionid, $token) = split /:/, $cookie;
  34. # use the central database handle
  35. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  36. my $checkQuery = $dbh->prepare("SELECT sl_login FROM session WHERE session_id = ? AND token = ? AND last_used > now() - ?::interval");
  37. my $updateAge = $dbh->prepare("UPDATE session SET last_used = now() WHERE session_id = ?;");
  38. #must be an integer
  39. $sessionid =~ s/[^0-9]//g;
  40. $sessionid = int $sessionid;
  41. #must be 32 chars long and contain hex chars
  42. $token =~ s/[^0-9a-f]//g;
  43. $token = substr($token, 0, 32);
  44. if (!$myconfig{timeout}){
  45. $timeout = "1 day";
  46. } else {
  47. $timeout = "$myconfig{timeout} seconds";
  48. }
  49. $checkQuery->execute($sessionid, $token, $timeout)
  50. || $form->dberror(__FILE__.':'.__LINE__.': Looking for session: ');
  51. my $sessionValid = $checkQuery->rows;
  52. if($sessionValid){
  53. #user has a valid session cookie, now check the user
  54. my ($sessionLogin) = $checkQuery->fetchrow_array;
  55. my $login = $form->{login};
  56. $login =~ s/[^a-zA-Z0-9@.-]//g;
  57. if($sessionLogin eq $login){
  58. $updateAge->execute($sessionid) || $form->dberror(__FILE__.':'.__LINE__.': Updating session age: ');
  59. return 1;
  60. } else {
  61. #something's wrong, they have the cookie, but wrong user. Hijack attempt?
  62. #delete the cookie in the browser
  63. print qq|Set-Cookie: LedgerSMB=; path=/;\n|;
  64. return 0;
  65. }
  66. } else {
  67. #cookie is not valid
  68. #delete the cookie in the browser
  69. print qq|Set-Cookie: LedgerSMB=; path=/;\n|;
  70. return 0;
  71. }
  72. }
  73. sub session_create {
  74. my ($form) = @_;
  75. # use the central database handle
  76. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  77. # TODO Change this to use %myconfig
  78. my $deleteExisting = $dbh->prepare("DELETE FROM session WHERE sl_login = ? AND age(last_used) > ?::interval");
  79. my $seedRandom = $dbh->prepare("SELECT setseed(?);");
  80. my $fetchSequence = $dbh->prepare("SELECT nextval('session_session_id_seq'), md5(random());");
  81. my $createNew = $dbh->prepare("INSERT INTO session (session_id, sl_login, token) VALUES(?, ?, ?);");
  82. # this is assuming that $form->{login} is safe, which might be a bad assumption
  83. # so, I'm going to remove some chars, which might make previously valid logins invalid
  84. my $login = $form->{login};
  85. $login =~ s/[^a-zA-Z0-9@.-]//g;
  86. #delete any existing stale sessions with this login if they exist
  87. if (!$myconfig{timeout}){
  88. $myconfig{timeout} = 86400;
  89. }
  90. $deleteExisting->execute($login, "$myconfig{timeout} seconds") || $form->dberror(__FILE__.':'.__LINE__.': Delete from session: ');
  91. #doing the random stuff in the db so that LedgerSMB won't
  92. #require a good random generator - maybe this should be reviewed, pgsql's isn't great either
  93. $fetchSequence->execute() || $form->dberror(__FILE__.':'.__LINE__.': Fetch sequence id: ');
  94. my ($newSessionID, $newToken) = $fetchSequence->fetchrow_array;
  95. #create a new session
  96. $createNew->execute($newSessionID, $login, $newToken) || $form->dberror(__FILE__.':'.__LINE__.': Create new session: ');
  97. #reseed the random number generator
  98. my $randomSeed = 1.0 * ('0.'. (time() ^ ($$ + ($$ <<15))));
  99. $seedRandom->execute($randomSeed)|| $form->dberror(__FILE__.':'.__LINE__.': Reseed random generator: ');
  100. $newCookieValue = $newSessionID . ':' . $newToken;
  101. #now set the cookie in the browser
  102. #TODO set domain from ENV, also set path to install path
  103. print qq|Set-Cookie: LedgerSMB=$newCookieValue; path=/;\n|;
  104. $form->{LedgerSMB} = $newCookieValue;
  105. }
  106. sub session_destroy {
  107. my ($form) = @_;
  108. my $login = $form->{login};
  109. $login =~ s/[^a-zA-Z0-9@.-]//g;
  110. # use the central database handle
  111. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  112. my $deleteExisting = $dbh->prepare("DELETE FROM session WHERE sl_login = ?;");
  113. $deleteExisting->execute($login) || $form->dberror(__FILE__.':'.__LINE__.': Delete from session: ');
  114. #delete the cookie in the browser
  115. print qq|Set-Cookie: LedgerSMB=; path=/;\n|;
  116. }
  117. sub password_check {
  118. use Digest::MD5;
  119. my ($form, $username, $password) = @_;
  120. # use the central database handle
  121. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  122. my $fetchPassword = $dbh->prepare("SELECT uc.password, uc.crypted_password
  123. FROM users as u, users_conf as uc
  124. WHERE u.username = ?
  125. AND u.id = uc.id;");
  126. $fetchPassword->execute($username) || $form->dberror(__FILE__.':'.__LINE__.': Fetching password : ');
  127. my ($md5Password, $cryptPassword) = $fetchPassword->fetchrow_array;
  128. if ($cryptPassword){
  129. #First time login from old system, check crypted password
  130. if ((crypt $password, substr($username, 0, 2)) eq $cryptPassword) {
  131. #password was good, convert to md5 password and null crypted
  132. my $updatePassword = $dbh->prepare("UPDATE users_conf
  133. SET password = md5(?),
  134. crypted_password = null
  135. FROM users
  136. WHERE users_conf.id = users.id
  137. AND users.username = ?;");
  138. $updatePassword->execute($password, $username) || $form->dberror(__FILE__.':'.__LINE__.': Converting password : ');
  139. return 1;
  140. } else {
  141. return 0; #password failed
  142. }
  143. }elsif ($md5Password){
  144. if ($md5Password ne (Digest::MD5::md5_hex $password) ) {
  145. return 0;
  146. }
  147. else{
  148. return 1;
  149. }
  150. } else {
  151. #both the md5Password and cryptPasswords were blank
  152. return 0;
  153. }
  154. }
  155. 1;