]> git.plutz.net Git - busy/blob - actions/login.sh
8cd9e74af2153cf6f5cf1b41d8a3e503892a2a63
[busy] / actions / login.sh
1 #!/bin/zsh
2 # Copyright 2011 Paul Haensch
3 # This file is part of Busy
4 #
5 # Busy is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Busy is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public License
16 # along with Busy.  If not, see <http://www.gnu.org/licenses/>.
17
18 cgi_post
19
20 user="$(echo -E "$_POST[\"user\"]" |sed -rn '1{/^[a-zA-Z0-9_+@.-]+$/p}')"
21 pass="$_POST[\"pass\"]"
22 userfile="$_DATA/auth/user$(echo -E "$user" |sha1sum |cut -c1-40)"
23
24 if [ -r "$userfile" ]; then
25   salt=$(sed -nr 's:^salt=(.+)$:\1:p' "$userfile")
26   ssum=$(sed -nr 's:^auth=(.+)$:\1:p' "$userfile")
27   rsum=$(echo -E "$salt$pass" |sha1sum |cut -c1-40)
28 fi
29 if [ -n "$ssum" -a  "$ssum" = "$rsum" ]; then
30   sid="$(apg -n1 -a1 -m32 -x32 -M CLN)"
31   sessionfile="$_DATA/auth/session$sid"
32   echo -E "$user $(($(date +%s) + 1800))" >"$sessionfile" 
33   echo -E "Location: $HTTP_REFERER"
34   echo -E "Set-Cookie: session=$sid; HttpOnly"
35 else
36   echo -E "Location: $HTTP_REFERER"
37 fi
38 echo ''