summaryrefslogtreecommitdiff
path: root/web/cgi/session/logon.tcl
blob: 2d675cf1dba01f49ad0d4c80d90ac0fb88877fb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!./tclsh
# $Id: logon.tcl 1142 2008-08-13 17:22:21Z hubert@u.washington.edu $
# ========================================================================
# Copyright 2006 University of Washington
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# ========================================================================

#  logon.tcl
#
#  Purpose: CGI script to authenticate user based on provided
#           credentials and launch them into the mailbox index

# and any global config
source ./alpine.tcl

# don't use WPEval since it'll mask open's credential failure case
cgi_eval {

  if {$_wp(debug)} {
    cgi_debug -on
  }

  #
  # Import username and password from pubcookie, if possible.
  # Otherwise get it from the form that was submitted.
  #
  cgi_input

  if {[catch {
	       cgi_import sessid
	       WPValidId $sessid
	     } result]} {
    WPInfoPage "Web Alpine Error" [font size=+2 "$result"] \
	       "Please complain to the [cgi_link Admin] and visit the [cgi_link Start] later."
    return
  }

  if {[catch {WPCmd set wp_open_parms} parms]} {
    WPInfoPage "Internal Error" [font size=+2 $parms] \
	       "Please complain to the [link Admin] and visit the [cgi_link Start] later."
  } else {
    catch {WPCmd unset wp_open_parms}

    foreach {p v} $parms {
      set $p $v
    }

    if {[catch {WPCmd PESession open $User $confloc $defconf} answer]} {
      if {0 == [string length $answer] || 0 == [string compare BADPASSWD [lindex $answer 0]]} {
	set answer "Unknown Username or Incorrect Password"
      }

      set alerts {}
      if {[catch {WPCmd PEInfo statmsgs} statmsgs] == 0} {
	# display any IMAP alerts
	foreach m $statmsgs {
	  if {[regexp {^Alert received.*\[ALERT\] (.*)$} $m dummy a]} {
	    if {[lsearch -exact $alerts $a] < 0} {
	      lappend alerts $a
	    }
	  }
	}
      }

      WPInfoPage "Login Failure" [font size=+2 $answer] \
	  "Please click your browser's [bold Back] button to return to the [cgi_link Start] to try again..." \
	  {} [join $alerts "<br>"]

      # unlaunch the thing
      catch {WPCmd PESession close}
      catch {WPCmd exit}
      return
    }

    # determine suitable number of index lines for the indicated display size
    # based on:
    #
    #  1. a header length of 72 pixels
    #  2. a TD font-size plus padding of 24 points
    #

    set indexheight [WPCmd PEInfo indexheight]
    if {[string length $indexheight] == 0} { set indexheight $_wp(indexheight)}
    if {[info exists hPx] && [regexp {^[0-9]+$} $hPx]} {
      # "66" comes from _wp(titlethick) + _wp(titlesep) + ((index tables cellpaddings * 2) = 8) + some fudge
      set indexlines [expr (($hPx - 66) / $indexheight) - 1]
    }

    if {[info exists indexlines] == 0 || $indexlines <= 0} {
      set indexlines [WPCmd PEInfo indexlines]
    }

    if {$indexlines <= 0} {
      set indexlines $_wp(indexlines)
    }

    # start with the message indicated by the
    # 'incoming-startup-rule' in the current index
    set firstmsg 1
    if {![catch {WPCmd PEMailbox firstinteresting} firstint] && $firstint > 0} {
      set messagecount [WPCmd PEMailbox messagecount]
      for {set i 1} {$i < $messagecount} {incr i $indexlines} {
	if {$i >= $firstint} {
	  break
	}

	set firstmsg $i
      }

      # show whole last page
      if {$firstmsg + $indexlines > $messagecount} {
	if {[set n [expr ($messagecount + 1) - $indexlines]] > 0} {
	  set firstmsg $n
	} else {
	  set firstmsg 1
	}
      }
    }

    if {[catch {WPCmd PEInfo sort} defsort]} {
      set defsort {Date 0}
    }

    # set these in alpined's interp so they're fished out by WPImport
    if {[catch {
      WPCmd set sort [lindex $defsort 0]
      WPCmd set rev [lindex $defsort 1]
      WPCmd set ppg $indexlines
      WPCmd set width $_wp(width)
      WPCmd set serverid $Server} result]} {
      WPInfoPage "Initialization Failure" [font size=+2 $result] \
	  "Please click your browser's [bold Back] button to return to the [cgi_link Start] to try again..."
      catch {WPCmd PESession close}
      catch {WPCmd exit}
      return
    }

    if {[catch {WPCmd PEMailbox uid $firstmsg} exp]} {
      set exp 1
    }

    WPCmd set top $exp

    if {[catch {WPCmd set serverroot} serverroot] == 0} {
      cgi_root $serverroot
    }

    set startpage "[cgi_root]/${startpage}?sessid=$sessid"

    if {[string length $prunepage] && [WPCmd PEInfo prunecheck] == 1} {
      set startpage "[cgi_root]/${prunepage}cid=[WPCmd PEInfo key]&sessid=${sessid}&start=[WPPercentQuote ${startpage}]"
    }

    cgi_http_head {
      if {[info exists env(REMOTE_USER)]} {
	# redirect thru intermediate so session id and secured user name can get bound in uidampper
	cgi_redirect $_wp(serverpath)/session/startup.tcl?sessid=${sessid}&page=[WPPercentQuote $startpage]
      } else {
	cgi_redirect $startpage
      }
    }
  }
}