Check-in [6d8f63011e]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Updated to support requesting a certificate and setting the verification status in env(SSL_CLIENT_VERIFY)
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:6d8f63011ed67fc227b189981d0efd76fff778f5
User & Date: rkeene 2011-06-24 18:56:12
Context
2011-06-24
20:59
Added more test files to test check-in: 6c4382b852 user: rkeene tags: trunk
18:56
Updated to support requesting a certificate and setting the verification status in env(SSL_CLIENT_VERIFY) check-in: 6d8f63011e user: rkeene tags: trunk
2011-06-22
22:50
Updated to propogate auto_path to created interpreters check-in: ded8851b04 user: rkeene tags: trunk
Changes

Changes to rivet-starkit/main.tcl.

743
744
745
746
747
748
749
750
751
752
753
754















































755
756
757
758
759
760
761
...
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
...
918
919
920
921
922
923
924


925
926
927
928
929
930
931
...
951
952
953
954
955
956
957


958

959
960
961
962
963
964
965
...
972
973
974
975
976
977
978
979
980
981
982
983

984



985
986
987
988
989
990
991
....
1043
1044
1045
1046
1047
1048
1049


1050
1051
1052
1053
1054
1055
1056
....
1103
1104
1105
1106
1107
1108
1109



1110
1111
1112
1113
1114
1115
1116
....
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213







1214




1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226


1227
1228
1229
1230
1231
1232
1233
....
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
	tcl_puts "   --maxthreads num   Maximum number of threads to allow to exist (server mode)"
	tcl_puts "   --sslport port     Listen on port for HTTPS requests (server mode, 0 disables,"
	tcl_puts "                      default is 0)"
	tcl_puts "   --sslcert file     Path to our certificate (server mode)"
	tcl_puts "   --sslkey file      Path to our key (server mode)"
	tcl_puts "   --sslcafile file   Path to CA certificate (server mode)"
	tcl_puts "   --sslcadir dir     Path to directory containing CA certificates (server mode)"
	tcl_puts "   --sslreqcert bool  Require client certificate (server mode, default is \"no\")"
	tcl_puts "   --cgi              Execute as a CGI"
	tcl_puts "   --help             This help"
	tcl_puts "   --version          Print version and exit"
}
















































proc rivet_cgi_server {addr ports foreground initscp logfile errorlogfile maxthreads sslopts_array} {
	catch {
		package require Tclx
	}

	set process_model "flat"
................................................................................
		set servercmd [list rivet_cgi_server_request $port $logfd $elogfd $process_model $maxthreads $mode]

		if {$mode == "https"} {
			array set sslopts $sslopts_array

			package require tls

			set cmd [list tls::socket -server $servercmd -tls1 true]
			foreach opt [array names sslopts] {
				set val $sslopts($opt)

				switch -- $opt {
					"require" {
						set val [expr $val]
					}
................................................................................
				}
			}

			# Fork off a child to handle the request
			set mypid [fork]
			if {$mypid != 0} {
				catch {


					close $sock
				}
				return
			}

			foreach masterfd $::rivetstarkit::masterfd {
				catch {
................................................................................

			# If none found, make sure we haven't exceeded the maximum number of threads
			if {![info exists threadId]} {
				set numthreads [llength [thread::names]]
				if {$numthreads > $maxthreads} {
					tcl_puts $elogfd "Exceeded maximum number of threads ($maxthreads): $numthreads, closing socket!"



					close $sock

					return
				}
			}

			# If none found, create one...
			if {![info exists threadId]} {
				# Create an empty interpreter in a new thread
................................................................................

				# Copy the appropriate namespaces to the new thread
				foreach ns [list ::rivetstarkit] {
					thread::send $threadId [list namespace eval $ns ""]
				}

				# Copy the appropriate variables to the new thread
				foreach var [list ::starkit::topdir] {
					if {[namespace qualifiers $var] != ""} {
						thread::send $threadId [list namespace eval [namespace qualifiers $var] ""]
					}


					thread::send $threadId [list set $var [set $var]]



				}

				# Copy the appropriate procedures to the new thread
				foreach proc [list rivet_cgi_server_request_data rivet_cgi_server_request call_page] {
					if {[namespace qualifiers $var] != ""} {
						thread::send $threadId [list namespace eval [namespace qualifiers $var] ""]
					}
................................................................................
		# Handle connection from data
		fconfigure $sock -buffering line
		fileevent $sock readable [list rivet_cgi_server_request_data $sock $addr $hostport $logfd $elogfd $pmodel]

		vwait ::rivetstarkit::finished($sock)

		catch {


			close $sock
		}
	} err]} {
		if {$elogfd != ""} {
			tcl_puts $elogfd "Error while installing callback: \"$err\""
		}
	}
................................................................................
			if {[llength $work] > 1} {
				set sockinfo(query) [join [lrange $work 1 end] ?]
			}

			# We only support GET and POST, everyone else we just close on.
			if {$sockinfo(method) != "GET" && $sockinfo(method) != "POST"} {
				set ::rivetstarkit::finished($sock) 1



				close $sock
			}
		}
		"HEADERS" {
			if {$line == ""} {
				set sockinfo(state) HANDLEREQUEST
			} else {
................................................................................

		# Set TLS Socket Info
		array set tlsinfo_peer [list sbits 0]
		array set tlsinfo_local [list sbits 0]
		catch {
			array set tlsinfo_peer [tls::status $sock]

			set myenv(HTTPS) on
		}
		catch {
			array set tlsinfo_local [tls::status -local $sock]
		}

		## Set TLS client CGI Variables
		if {$tlsinfo_peer(sbits) != 0} {







			set myenv(SSL_CLIENT_VERIFY) SUCCESS




			foreach {myenvvar tlsvar} [list SSL_CLIENT_S_DN subject SSL_CLIENT_I_DN issuer SSL_CLIENT_V_START notBefore SSL_CLIENT_V_END notAfter SSL_CLIENT_M_SERIAL serial SSL_CIPHER cipher SSL_CIPHER_USEKEYSIZE sbits] {
				if {![info exists tlsinfo_peer($tlsvar)]} {
					continue
				}

				set myenv($myenvvar) $tlsinfo_peer($tlsvar)
			}

		}

		## Set TLS server CGI Variables
		if {$tlsinfo_local(sbits) != 0} {


			foreach {myenvvar tlsvar} [list SSL_SERVER_S_DN subject SSL_SERVER_I_DN issuer SSL_SERVER_V_START notBefore SSL_SERVER_V_END notAfter SSL_SERVER_M_SERIAL serial SSL_CIPHER cipher SSL_CIPHER_USEKEYSIZE sbits] {
				if {![info exists tlsinfo_local($tlsvar)]} {
					continue
				}

				set myenv($myenvvar) $tlsinfo_local($tlsvar)
			}
................................................................................
				append rivet_cgi_server_port " ssl:[join $options(--sslport) { ssl:}]"
			}
			set rivet_cgi_server_fg [expr !!($options(--foreground))]
			set rivet_cgi_server_init $options(--init)
			set rivet_cgi_server_logfile $options(--logfile)
			set rivet_cgi_server_errorlogfile $options(--errorlog)
			set rivet_cgi_server_maxthreads $options(--maxthreads)
			set rivet_cgi_server_sslopts [list certfile $options(--sslcert) keyfile $options(--sslkey) cafile $options(--sslcafile) cadir $options(--sslcadir) require $options(--sslreqcert)]

			rivet_cgi_server $rivet_cgi_server_addr $rivet_cgi_server_port $rivet_cgi_server_fg $rivet_cgi_server_init $rivet_cgi_server_logfile $rivet_cgi_server_errorlogfile $rivet_cgi_server_maxthreads $rivet_cgi_server_sslopts

			# If rivet_cgi_server returns, something went wrong...
			exit 1
		}
		"--cgi" {







|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







>
>







 







>
>

>







 







|




>
|
>
>
>







 







>
>







 







>
>
>







 







<







>
>
>
>
>
>
>
|
>
>
>
>







<




>
>







 







|







743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
...
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
...
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
....
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
....
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
....
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
....
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
....
1260
1261
1262
1263
1264
1265
1266

1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292

1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
....
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
	tcl_puts "   --maxthreads num   Maximum number of threads to allow to exist (server mode)"
	tcl_puts "   --sslport port     Listen on port for HTTPS requests (server mode, 0 disables,"
	tcl_puts "                      default is 0)"
	tcl_puts "   --sslcert file     Path to our certificate (server mode)"
	tcl_puts "   --sslkey file      Path to our key (server mode)"
	tcl_puts "   --sslcafile file   Path to CA certificate (server mode)"
	tcl_puts "   --sslcadir dir     Path to directory containing CA certificates (server mode)"
	tcl_puts "   --sslreqcert bool  Request client certificate (server mode, default is \"no\")"
	tcl_puts "   --cgi              Execute as a CGI"
	tcl_puts "   --help             This help"
	tcl_puts "   --version          Print version and exit"
}

proc rivet_cgi_tls_callback {logfd elogfd mode args} {
	switch -- $mode {
		"error" {
			foreach {chan msg} $args break

			if {$elogfd != ""} {
				tcl_puts $elogfd "($chan) TLS error: $msg"
			}

			return 1
		}
		"verify" {
			foreach {chan depth cert rc err} $args break

			# Only care about client certificates, all others follow normal rules
			if {$depth != 0} {
				return $rc
			}

			if {$rc != "1"} {
				# Do not note that this certificate is valid, but return OK

				if {$elogfd != ""} {
					tcl_puts $elogfd "($chan) Invalid cert $cert: $err"
				}

				set ::rivet_cgi_tls_verified($chan) 0

				return 1
			}

			if {![info exists ::rivet_cgi_tls_verified($chan)]} {
				# Verified

				set ::rivet_cgi_tls_verified($chan) 1
			}

			return 1
		}
		"info" {
		}
		default {
			return -code error "bad option \"$mode\": must be one of error, info, or verify"
		}
	}
}

proc rivet_cgi_server {addr ports foreground initscp logfile errorlogfile maxthreads sslopts_array} {
	catch {
		package require Tclx
	}

	set process_model "flat"
................................................................................
		set servercmd [list rivet_cgi_server_request $port $logfd $elogfd $process_model $maxthreads $mode]

		if {$mode == "https"} {
			array set sslopts $sslopts_array

			package require tls

			set cmd [list tls::socket -server $servercmd -tls1 true -command [list rivet_cgi_tls_callback $logfd $elogfd]]
			foreach opt [array names sslopts] {
				set val $sslopts($opt)

				switch -- $opt {
					"require" {
						set val [expr $val]
					}
................................................................................
				}
			}

			# Fork off a child to handle the request
			set mypid [fork]
			if {$mypid != 0} {
				catch {
					unset -nocomplain ::rivet_cgi_tls_verified($sock)

					close $sock
				}
				return
			}

			foreach masterfd $::rivetstarkit::masterfd {
				catch {
................................................................................

			# If none found, make sure we haven't exceeded the maximum number of threads
			if {![info exists threadId]} {
				set numthreads [llength [thread::names]]
				if {$numthreads > $maxthreads} {
					tcl_puts $elogfd "Exceeded maximum number of threads ($maxthreads): $numthreads, closing socket!"

					unset -nocomplain ::rivet_cgi_tls_verified($sock)

					close $sock

					return
				}
			}

			# If none found, create one...
			if {![info exists threadId]} {
				# Create an empty interpreter in a new thread
................................................................................

				# Copy the appropriate namespaces to the new thread
				foreach ns [list ::rivetstarkit] {
					thread::send $threadId [list namespace eval $ns ""]
				}

				# Copy the appropriate variables to the new thread
				foreach var [list ::starkit::topdir ::rivet_cgi_tls_verified($sock)] {
					if {[namespace qualifiers $var] != ""} {
						thread::send $threadId [list namespace eval [namespace qualifiers $var] ""]
					}

					if {[info exists $var]} {
						thread::send $threadId [list set $var [set $var]]
					} else {
						thread::send $threadId [list unset -nocomplain $var]
					}
				}

				# Copy the appropriate procedures to the new thread
				foreach proc [list rivet_cgi_server_request_data rivet_cgi_server_request call_page] {
					if {[namespace qualifiers $var] != ""} {
						thread::send $threadId [list namespace eval [namespace qualifiers $var] ""]
					}
................................................................................
		# Handle connection from data
		fconfigure $sock -buffering line
		fileevent $sock readable [list rivet_cgi_server_request_data $sock $addr $hostport $logfd $elogfd $pmodel]

		vwait ::rivetstarkit::finished($sock)

		catch {
			unset -nocomplain ::rivet_cgi_tls_verified($sock)

			close $sock
		}
	} err]} {
		if {$elogfd != ""} {
			tcl_puts $elogfd "Error while installing callback: \"$err\""
		}
	}
................................................................................
			if {[llength $work] > 1} {
				set sockinfo(query) [join [lrange $work 1 end] ?]
			}

			# We only support GET and POST, everyone else we just close on.
			if {$sockinfo(method) != "GET" && $sockinfo(method) != "POST"} {
				set ::rivetstarkit::finished($sock) 1

				unset -nocomplain ::rivet_cgi_tls_verified($sock)

				close $sock
			}
		}
		"HEADERS" {
			if {$line == ""} {
				set sockinfo(state) HANDLEREQUEST
			} else {
................................................................................

		# Set TLS Socket Info
		array set tlsinfo_peer [list sbits 0]
		array set tlsinfo_local [list sbits 0]
		catch {
			array set tlsinfo_peer [tls::status $sock]


		}
		catch {
			array set tlsinfo_local [tls::status -local $sock]
		}

		## Set TLS client CGI Variables
		if {$tlsinfo_peer(sbits) != 0} {
			set myenv(HTTPS) on

			if {![info exists ::rivet_cgi_tls_verified($sock)]} {
				set ::rivet_cgi_tls_verified($sock) 0
			}

			if {$::rivet_cgi_tls_verified($sock) == "1"} {
				set myenv(SSL_CLIENT_VERIFY) SUCCESS
			} else {
				unset -nocomplain myenv(SSL_CLIENT_VERIFY)
			}

			foreach {myenvvar tlsvar} [list SSL_CLIENT_S_DN subject SSL_CLIENT_I_DN issuer SSL_CLIENT_V_START notBefore SSL_CLIENT_V_END notAfter SSL_CLIENT_M_SERIAL serial SSL_CIPHER cipher SSL_CIPHER_USEKEYSIZE sbits] {
				if {![info exists tlsinfo_peer($tlsvar)]} {
					continue
				}

				set myenv($myenvvar) $tlsinfo_peer($tlsvar)
			}

		}

		## Set TLS server CGI Variables
		if {$tlsinfo_local(sbits) != 0} {
			set myenv(HTTPS) on

			foreach {myenvvar tlsvar} [list SSL_SERVER_S_DN subject SSL_SERVER_I_DN issuer SSL_SERVER_V_START notBefore SSL_SERVER_V_END notAfter SSL_SERVER_M_SERIAL serial SSL_CIPHER cipher SSL_CIPHER_USEKEYSIZE sbits] {
				if {![info exists tlsinfo_local($tlsvar)]} {
					continue
				}

				set myenv($myenvvar) $tlsinfo_local($tlsvar)
			}
................................................................................
				append rivet_cgi_server_port " ssl:[join $options(--sslport) { ssl:}]"
			}
			set rivet_cgi_server_fg [expr !!($options(--foreground))]
			set rivet_cgi_server_init $options(--init)
			set rivet_cgi_server_logfile $options(--logfile)
			set rivet_cgi_server_errorlogfile $options(--errorlog)
			set rivet_cgi_server_maxthreads $options(--maxthreads)
			set rivet_cgi_server_sslopts [list certfile $options(--sslcert) keyfile $options(--sslkey) cafile $options(--sslcafile) cadir $options(--sslcadir) request $options(--sslreqcert)]

			rivet_cgi_server $rivet_cgi_server_addr $rivet_cgi_server_port $rivet_cgi_server_fg $rivet_cgi_server_init $rivet_cgi_server_logfile $rivet_cgi_server_errorlogfile $rivet_cgi_server_maxthreads $rivet_cgi_server_sslopts

			# If rivet_cgi_server returns, something went wrong...
			exit 1
		}
		"--cgi" {