summaryrefslogtreecommitdiff
path: root/web/cgi/session/greeting.tcl
blob: a6acc1e58a1c252b00d41a5e87c3b2a376764ca3 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!./tclsh
# $Id: greeting.tcl 1204 2009-02-02 19:54:23Z 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
#
# ========================================================================

# suck in any global config
source ./alpine.tcl

# figure out SSL defaults
if {[info exists _wp(ssl_default)] && $_wp(ssl_default) == 0} {
  set ssl 0
} else {
  set ssl 1
  if {[info exists _wp(ssl_safe_domains)]} {
    if {[info exists env(REMOTE_HOST)]} {
      foreach d $_wp(ssl_safe_domains) {
	regsub -all {\.} $d {\.} d
	if {[regexp -nocase "$d\$" $env(REMOTE_HOST)]} {
	  set ssl 0
	  break
	}
      }
    }

    if {$ssl && [info exists env(REMOTE_ADDR)]} {
      regexp {([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)} $env(REMOTE_ADDR) dummy a b c d
      set h "$d.$c.$b.$a.in-addr.arpa"
      if {[catch {exec nslookup -q=PTR $h "2&>1"} pr] == 0} {
	foreach l [split $pr \n] {
	  if {[regexp -nocase "^$h\[	\]*name = (.*)\$" $l dummy inaddr]} {
	    break
	  }
	}
      }

      if {[info exists inaddr]} {
	foreach d $_wp(ssl_safe_domains) {
	  if {[set n [expr {[string length $inaddr] - [string length $d] - 1}]] > 1
	  && [string compare [string tolower [string range $inaddr $n end]] [string tolower ".$d"]] == 0} {
	    set ssl 0
	    break
	  }
	}
      }
    }
  }

  if {$ssl && [info exists _wp(ssl_safe_addrs)]} {
    set ra [split $env(REMOTE_ADDR) .]
    foreach a $_wp(ssl_safe_addrs) {
      if {[llength $a] == 2} {
	set low [split [lindex $a 0] .]
	set hi [split [lindex $a 1] .]

	foreach a $ra b $low c $hi {
	  if {$b == $c} {
	    if {$a != $b} {
	      break
	    }
	  } else {
	    if {$a >= $b && $a <= $c} {
	      set ssl 0
	      break
	    }
	  }
	}
      } else {
	foreach a [split $a .] b $ra {
	  if {[string length $a]} {
	    if {$a != $b} {
	      break
	    }
	  } else {
	    set $ssl 0
	    break
	  }
	}
      }

      if {!$ssl} {
	break
      }
    }
  }
}

cgi_eval {

  cgi_http_head {
    WPStdHttpHdrs
  }

  cgi_html {

    cgi_head {
      WPStdHtmlHdr Logon
      WPStdScripts $_wp(indexheight)

      if {$_wp(flexserver)} {
	cgi_javascript {
	  cgi_put  "function bogus(f,o) {"
	  cgi_put  " if(o.value.length == 0){"
	  cgi_put  "  alert('What '+f+' will you be using to log in to your IMAP server?\\nPlease fill in the '+f+' field.');"
	  cgi_put  "  o.focus();"
	  cgi_put  "  return true;"
	  cgi_put  " }"
	  cgi_put  " return false;"
	  cgi_puts "}"
	  cgi_put  "function doSubmit() {"
	  cgi_put  " var f = document.Login;"
	  cgi_put  " var l = f.Server;"
	  cgi_put  " var o = l.options\[l.selectedIndex\];"
	  cgi_put  " if(bogus('Username',f.User) || bogus('Password',f.Pass)) return false;"
	  cgi_put  " if(o.value < 0){"
	  cgi_put  "  o.value = alien_server;"
	  cgi_put  " }"
	  cgi_put  " return true;"
	  cgi_puts "}"
	  cgi_put  "function customServer() {"
	  cgi_put  " var f = document.Login;"
	  cgi_put  " var l = f.Server;"
	  cgi_put  " var o = l.options\[l.selectedIndex\];"
	  if {[info exists env(REMOTE_USER)]} {
	    cgi_put "  f.User.value = (o.value < 0) ? alien_user : def_user;"
	    cgi_put "  f.Pass.value = (o.value < 0) ? alien_pass : def_pass;"
	  }
	  cgi_put  " if(o.value < 0){"
	  cgi_put  "  if(s = prompt('IMAP Server Name (append \"/ssl\" for secure link)',o.s ? o.s : '')){"
	  cgi_put  "   alien_server = s;"
	  cgi_put  "   o.text = 'Your Server: '+alien_server;"
	  cgi_put  "   f.User.onfocus = null;"
	  cgi_put  "   f.Pass.onfocus = null;"
	  cgi_put  "   f.User.focus();"
	  cgi_put  "  }"
	  cgi_put  "  else if(alien_server.length == 0){"
	  if {[info exists env(REMOTE_USER)]} {
	    cgi_put  "   f.User.onfocus = f.User.blur;"
	    cgi_put  "   f.Pass.onfocus = f.Pass.blur;"
	    cgi_put  "   f.User.value = def_user;"
	    cgi_put  "   f.Pass.value = def_pass;"
	  }
	  cgi_put  "   l.selectedIndex = 0;"
	  cgi_put  "  }"
	  cgi_put  " }"
	  if {[info exists env(REMOTE_USER)]} {
	    cgi_put  " else {"
	    cgi_put  "   f.User.onfocus = f.User.blur;"
	    cgi_put  "   f.Pass.onfocus = f.Pass.blur;"
	    cgi_put  " }"
	  }
	  cgi_puts "}"
	  if {[info exists env(REMOTE_USER)]} {
	    cgi_put  "var def_user = new Array('$env(REMOTE_USER)');"
	    cgi_put  "var def_pass = new Array('x');"
	    cgi_put  "var alien_server = '';"
	    cgi_put  "var alien_user = '';"
	    cgi_put  "var alien_pass = '';"
	    cgi_put  "function customSave() {"
	    cgi_put  " var f = document.Login;"
	    cgi_put  " var s = f.Server.options\[f.Server.selectedIndex\];"
	    cgi_put  " if(s.value < 0){"
	    cgi_put  "  alien_user = f.User.value;"
	    cgi_put  "  alien_pass = f.Pass.value;"
	    cgi_put  " }"
	    cgi_puts "}"
	  }
	}
      }
    }

    if {[info exists env(REMOTE_USER)]} {
      set onload ""
    } else {
      set onload "onLoad=document.Login.User.focus()"
    }

    cgi_body $onload {
      if {$_wp(flexserver)} {
	set onsubmit "onSubmit=return doSubmit()"
      } else {
	set onsubmit ""
      }

      cgi_form session/init method=post enctype=multipart/form-data name=Login $onsubmit {
	cgi_javascript {
	  cgi_puts "document.write('<input name=\"hPx\" value='+window.getDisplayHeight()+' type=hidden notab>');"
	}

	cgi_table height=100% width=100% align=center {

	  cgi_table_row {
	    cgi_table_data align=center valign=bottom height=18% colspan=8 {
	      set intro "Welcome to $_wp(appname)"
	      if {[info exists env(REMOTE_USER)]} {
		set conf [subst [lindex [lindex $_wp(hosts) 0] 2]]
		set pldap [file join $_wp(bin) $_wp(pldap)]
		if {[catch {exec $pldap -p $conf -u $env(REMOTE_USER)} pname] == 0} {
		  append intro ", $pname"
		}
	      }

	      cgi_put [font size=+2 face=Helvetica [bold $intro]]
	    }
	  }

	  cgi_table_row {
	    cgi_table_data width=36% height=35% align=right valign=middle {
	      cgi_put [cgi_imglink logo]
	    }

	    cgi_table_data width=5% {
	      cgi_puts [cgi_nbspace]
	    }

	    cgi_table_data {
	      cgi_table border=0 cellspacing=8 {
		cgi_table_row {
		  cgi_table_data align=right valign=bottom {
		    cgi_puts [font size=+1 face=Helvetica "Username:"]
		  }

		  cgi_table_data align=left valign=bottom {
		    if {[info exists env(REMOTE_USER)]} {
		      set user "User=$env(REMOTE_USER)"
		      set rdonly "onFocus=this.blur();"
		    } else {
		      set user "User="
		      set rdonly ""
		    }

		    if {$_wp(flexserver) && [info exists env(REMOTE_USER)]} {
		      set onblur onBlur=customSave()
		    } else {
		      set onblur ""
		    }

		    cgi_text $user type=text maxlength=30 tableindex=1 $rdonly $onblur
		  }
		}

		cgi_table_row {
		  cgi_table_data align=right valign=middle {
		    cgi_puts [font size=+1 face=Helvetica "Password:"]
		  }

		  cgi_table_data align=left valign=middle {
		    if {[info exists env(REMOTE_USER)]} {
		      set pass "Pass=*"
		      set rdonly "onFocus=this.blur();"
		    } else {
		      set pass "Pass="
		      set rdonly ""
		    }

		    if {$_wp(flexserver) && [info exists env(REMOTE_USER)]} {
		      set onblur onBlur=customSave()
		    } else {
		      set onblur ""
		    }

		    cgi_text $pass type=password maxlength=30 tableindex=2 $rdonly $onblur
		  }
		}

		cgi_table_row {
		  cgi_table_data align=right valign=middle {
		    cgi_puts [font face=Helvetica size=+1 "Server:"]
		  }

		  cgi_table_data align=left valign=top {
		    if {[info exists _wp(hosts)]} {
		      if {$_wp(flexserver)} {
			set onchange onChange=customServer()
			if {[info exists env(REMOTE_USER)]} {
			  set onblur onBlur=customSave()
			} else {
			  set onblur ""
			}
		      } else {
			set onchange ""
			set onblur ""
		      }

		      cgi_select Server align=left $onchange $onblur {
			for {set j 0} {$j < [llength $_wp(hosts)]} {incr j} {
			  cgi_option [lindex [lindex $_wp(hosts) $j] 0] value=$j
			}

			if {$_wp(flexserver)} {
			  cgi_javascript {
			    cgi_puts "document.write('<option value=\"-1\">Server of Your Choice');"
			  }
			}
		      }
		    } else {
		      cgi_text Server= type=text maxlength=256
		    }
		  }
		}
	      }
	    }
	  }

	  if {[info exists env(REMOTE_USER)]} {
	    cgi_table_row {
	      cgi_table_data align=center valign=top height=1% colspan=8 {
		cgi_puts "Protect your privacy! When you finish, [cgi_url "completely exit your Web browser" "http://www.washington.edu/computing/web/logout.html"]."
	      }
	    }
	  }

	  cgi_table_row {
	    cgi_table_data align=center valign=top colspan=8 "style=padding-bottom:10" {
	      cgi_submit_button login=Login
	    }
	  }

	  if {[info exists _wp(plainservpath)] && [string length $_wp(plainservpath)]} {
	    cgi_table_row {
	      cgi_table_data colspan=8 align=center valign=middle {
		cgi_table border=0 width=30% {
		  cgi_table_row {
		    cgi_table_data rowspan=2 valign=top {
		      if {$ssl} {
			set checked checked
		      } else {
			set checked ""
		      }

		      cgi_checkbox ssl=1 $checked
		    }

		    cgi_table_data {
		      cgi_put [cgi_font size=-1 face=Helvetica "Use [cgi_url "SSL Session Encryption" "$_wp(serverpath)/$_wp(appdir)/$_wp(ui1dir)/help/secure.html" target=_blank]"]
		      cgi_br
		      set t "Session encryption over low-speed connections may slow WebPine, but prevents eavesdropping. Passwords are safely encrypted though."

		      cgi_division style=background-color:#eeeeee {
			cgi_put [cgi_font size=-2 face=Helvetica $t]
		      }
		    }
		  }
		}
	      }
	    }
	  } else {
	    cgi_text ssl=1 type=hidden notab
	  }

	  if {[info exists _wp(oldserverpath)] && [regexp {^[Hh][Tt][Tt][Pp][Ss]://} $_wp(oldserverpath)]} {
	    cgi_table_row {
	      cgi_table_data colspan=8 align=center valign=middle {
		cgi_division "style=\"width: 30% ; font-family: Helvetica; font-size: small ; background-color: #EEEEEE; padding: 8 2 \"" {
		  cgi_puts "For the time being, the [cgi_url "old version" $_wp(oldserverpath)] is still available."
		}
	      }
	    }
	  }

	  if {[catch {open [file join $_wp(cgipath) $_wp(motd)] r} id] == 0} {
	    cgi_table_row {
	      cgi_table_data colspan=8 height=20% valign=top {
		cgi_table width=100% {
		  cgi_table_data height=40 width=18% {
		    # cgi_puts [cgi_imglink bang]
		    cgi_puts [cgi_nbspace]
		  }

		  cgi_table_data {
		    cgi_puts [font size=-1 face=Helvetica [read $id]]
		  }

		  cgi_table_data width=18% {
		    # cgi_puts [cgi_imglink bang]
		    cgi_puts [cgi_nbspace]
		  }
		}
	      }
	    }

	    close $id
	  }
	}
      }
    }
  }
}