Debian Codegraph @

testsuite/systemtap.server/client.exp

raw
1 # In this test, we are using arrays as sets. The key itself does not matter,
2 # only the value
3
4 if {! [installtest_p]} { untested "client"; return }
5 if {! [nss_p]} { untested "client (no nss)"; return }
6
7 # Note we have to be root (to get the namespace to work correctly).
8 set effective_uid [exec /usr/bin/id -u]
9 if {$effective_uid != 0} { untested "client (must be root)"; return }
10
11 # Let's start with a clean slate in terms of trust.
12 exec rm -fr $env(SYSTEMTAP_DIR)/ssl
13
14 # arr given as list (e.g. [array_has [array get myarr] elem])
15 proc array_has {arr elem} {
16
17 array set foo $arr
18 if {[array size foo] == 0} {
19 return 0
20 }
21
22 foreach {key val} [array get foo] {
23 if {[string equal $val $elem]} {
24 return 1
25 }
26 }
27
28 return 0
29 }
30
31 # Check if array1 is a subset of array2. Returns 1 if yes, otherwise 0.
32 # (Modified from http://wiki.tcl.tk/1032.)
33 proc array_in {array1 array2} {
34 upvar 1 $array1 sub $array2 super
35
36 if {![array exists sub]} {
37 return -code error "$array1 is not an array"
38 }
39 if {![array exists super]} {
40 return -code error "$array2 is not an array"
41 }
42 if {[array size sub] > [array size super]} {
43 return 0
44 }
45 if {[array size sub] == 0} {
46 return 1
47 }
48
49 foreach key [array names sub] {
50 if {![array_has [array get super] $sub($key)]} {
51 return 0
52 }
53 }
54 return 1
55 }
56
57 # Check if array1 and array2 have all the same elements
58 proc array_equal {array1 array2} {
59 upvar 1 $array1 foo $array2 bar
60 return [expr [array_in foo bar] && [array_in bar foo]]
61 }
62
63 # Returns the union of the three arrays
64 proc array_union {array1 array2 array3} {
65 upvar 1 $array1 foo $array2 bar $array3 baz
66 array unset ::union
67
68 if {![array exists foo]} {
69 return -code error "$array1 is not an array"
70 }
71 if {![array exists bar]} {
72 return -code error "$array2 is not an array"
73 }
74 if {![array exists baz]} {
75 return -code error "$array3 is not an array"
76 }
77
78 set n 0
79 foreach key [array names foo] {
80 set ::union($n) $foo($key)
81 incr n
82 }
83 foreach key [array names bar] {
84 if {![array_has [array get ::union] $bar($key)]} {
85 set ::union($n) $bar($key)
86 incr n
87 }
88 }
89 foreach key [array names baz] {
90 if {![array_has [array get ::union] $baz($key)]} {
91 set ::union($n) $baz($key)
92 incr n
93 }
94 }
95 }
96
97 # Returns all the elements in array_new not in array_old
98 proc array_diff {array_new array_old} {
99 upvar 1 $array_new anew $array_old aold
100 array unset ::diff
101
102 if {![array exists anew]} {
103 return -code error "$array_new is not an array"
104 }
105 if {![array exists aold]} {
106 return -code error "$array_old is not an array"
107 }
108
109 set n 0
110 foreach key [array names anew] {
111 if {![array_has [array get aold] $anew($key)]} {
112 set ::diff($n) $anew($key)
113 incr n
114 }
115 }
116 }
117
118 # Test the --list-servers option and return an array of the servers found.
119 proc list_servers { TEST_NAME SERVER_SPEC args } {
120 set failed 0
121 set n 0
122 array unset ::servers
123 set cmd [concat stap --list-servers=$SERVER_SPEC $args]
124 send_log "executing: $cmd\n"
125 eval spawn $cmd
126 expect {
127 -timeout 150
128 -re "^Systemtap Compile Server Status for '${SERVER_SPEC}'\r\n" {
129 exp_continue
130 }
131 -re {^No servers found\r\n} {
132 }
133 -re {^ host=[^\r]*\r\n} {
134 set ::servers($n) "$expect_out(0,string)"
135 incr n
136 exp_continue
137 }
138 -re {^No certificate found in database [^\r]*\r\n} {
139 exp_continue
140 }
141 -re {^[^\r]*\r\n} {
142 verbose -log "unexpected output: $expect_out(0,string)"
143 set failed 1
144 }
145 timeout {
146 verbose -log "timeout"
147 kill -INT -[exp_pid] 2
148 set failed 1
149 }
150 }
151 catch {close}; catch {wait}
152
153 if {$failed != 0} {
154 fail "$TEST_NAME"
155 } else {
156 pass "$TEST_NAME"
157 }
158 }
159
160 # Test the --list-servers option and return an array of the servers
161 # found in the custom namespace.
162 proc ns_list_servers { TEST_NAME SERVER_SPEC args } {
163 set failed 0
164 set n 0
165 array unset ::servers
166 set cmd "stap --list-servers=$SERVER_SPEC $args"
167 send_log "executing: $cmd\n"
168 lassign [server_ns_as_root $cmd] rc output
169 if {$rc == 0} {
170 # The command succeeded. Search the output for the servers.
171 foreach line [split $output \n] {
172 if {[regexp {^ host=(.*)$} $line match server]} {
173 set ::servers($n) $server
174 incr n
175 } elseif {[regexp "^Systemtap Compile Server Status for '${SERVER_SPEC}'$" $line]
176 || [regexp {^No servers found$} $line]
177 || [regexp {^No certificate found in database .*$} $line]} {
178 # Ignore this output
179 } else {
180 verbose -log "unexpected output: $line"
181 }
182 }
183 pass "$TEST_NAME"
184 } else {
185 fail "$TEST_NAME"
186 }
187 }
188
189 # There may be other servers running. Let's keep track of them.
190 #
191 # Sometimes, we'll see a server running from the last test, if it
192 # hasn't quite died yet. So, make sure we get the same result twice.
193 list_servers "List existing online servers" online
194 array set existing_online_servers [array get servers]
195 set i 0
196 while {1} {
197 list_servers "List existing online servers" online
198 array unset eos2
199 array set eos2 [array get servers]
200 set i [expr $i + 1]
201 if {$i > 10} {
202 fail "List existing online servers: never got stable"
203 return
204 }
205
206 verbose -log "verify existing online servers - attempt $i: [array size existing_online_servers] [array size eos2]"
207 if {[array_equal existing_online_servers eos2]} {
208 # Arrays are equal, we're done
209 break
210 }
211 array unset existing_online_servers
212 array set existing_online_servers [array get eos2]
213 }
214
215 # There may be existing trusted servers. Keep track of them.
216 list_servers "List existing trusted servers" trusted
217 array unset existing_trusted_servers
218 array set existing_trusted_servers [array get servers]
219
220 # There may be existing trusted signers. Keep track of them.
221 list_servers "List existing signing servers" signer
222 array unset existing_signing_servers
223 array set existing_signing_servers [array get servers]
224
225 # If we query all known servers, it should contain exactly the union of the
226 # above three queries.
227 list_servers "List all existing servers" all
228 array unset all_existing_servers
229 array set all_existing_servers [array get servers]
230
231 # First we create a union
232 array_union existing_online_servers \
233 existing_trusted_servers \
234 existing_signing_servers
235 array set existing_unioned_servers [array get union]
236
237 # Now we can compare
238 set test "Verify existing server list"
239 if {[array_equal existing_unioned_servers all_existing_servers]} {
240 pass "$test"
241 } else {
242 fail "$test"
243 }
244
245 list_servers "List existing online servers (before start)" online
246 array unset existing_online_servers
247 array set existing_online_servers [array get servers]
248
249 # Now start our own server and make sure we can work with it.
250 if {! [setup_server] || $avahi_ok_p != 1} {
251 untested "Compile-server client tests against a server"
252 return
253 }
254
255 # Our server should now appear online, separate from the previously
256 # discovered online servers. Note that our server could generate
257 # serveral listings because it could appear at more than one ip
258 # address,
259 list_servers "List current online servers" online
260 array unset current_online_servers
261 array set current_online_servers [array get servers]
262
263 # array_diff will give us all the servers in current not in existing
264 array_diff current_online_servers existing_online_servers
265 array unset new_online_servers
266 array set new_online_servers [array get diff]
267
268 set test "New online servers"
269 if {[array size new_online_servers] > 0} {
270 pass "$test"
271 } else {
272 fail "$test"
273 }
274
275 # Our server should now be trusted, separate from the previously
276 # discovered trusted servers.
277 list_servers "List current trusted servers" online,trusted
278 array unset current_trusted_servers
279 array set current_trusted_servers [array get servers]
280
281 # array_diff will give us all the servers in current not in existing
282 array_diff current_trusted_servers existing_trusted_servers
283 array unset new_trusted_servers
284 array set new_trusted_servers [array get diff]
285
286 set test "New trusted servers"
287 if {[array size new_trusted_servers] > 0} {
288 pass "$test"
289 } else {
290 fail "$test"
291 }
292
293 # The new servers should automatically be trusted, so the
294 # new_trusted_servers array should be a subset of the
295 # new_online_servers array, but not necessarily vice-versa, since new
296 # servers may have come online independently of our testing.
297 set test "Verify new trusted server list"
298 if {[array_in new_trusted_servers new_online_servers]} {
299 pass "$test"
300 } else {
301 fail "$test"
302 }
303
304 # The newly trusted servers represent the server we just started.
305 array unset our_servers
306 array set our_servers [array get new_trusted_servers]
307
308 # The new servers should not be trusted as signers so there should be
309 # no new signing servers.
310 list_servers "List current signing servers" signer
311 array unset current_signing_servers
312 array set current_signing_servers [array get servers]
313
314 set test "No new signing servers"
315 if {[array_equal current_signing_servers existing_signing_servers]} {
316 pass "$test"
317 } else {
318 fail "$test"
319 }
320
321 # Revoke trust in our server. Specify the server by host name.
322 set test "Server has host name"
323 if {[regexp {^ host=([^ ]*).*} $our_servers(0) match host_name]} {
324 pass $test
325 } else {
326 fail $test
327 }
328
329 # Compile and run a simple hello test, selecting the server automatically
330 set test "Hello from server"
331 set rc [stap_run_batch $srcdir/systemtap.server/hello.stp $use_server]
332 if {$rc == 0} { pass $test } else { fail $test }
333
334 set cmd [concat stap --trust-servers=ssl,revoke,no-prompt --use-server=$host_name]
335 send_log "executing: $cmd\n"
336 eval spawn $cmd
337 expect {
338 -timeout 150
339 -re {^.*\r\n} { exp_continue }
340 timeout {
341 kill -INT -[exp_pid] 2
342 set failed 1
343 }
344 }
345 catch {close}; catch {wait}
346
347 # Our server should no longer be trusted.
348 list_servers "List current trusted servers after revocation by host name" trusted
349 array unset current_trusted_servers
350 array set current_trusted_servers [array get servers]
351
352 set test "No longer trusted after revocation by host name"
353 if {[array_equal current_trusted_servers existing_trusted_servers]} {
354 pass "$test"
355 } else {
356 fail "$test"
357 }
358
359 # Reinstate trust in our server. Specify the server by ip address.
360 # The default for --trusted servers is 'ssl'.
361 set test "Server has ip address"
362 if {[regexp {^.*address=([^ ]*).*} $our_servers(0) match ip_address]} {
363 pass $test
364 } else {
365 fail $test
366 }
367 set cmd [concat stap --trust-servers=no-prompt --use-server=$ip_address]
368 send_log "executing: $cmd\n"
369 eval spawn $cmd
370 expect {
371 -timeout 150
372 -re {^.*\r\n} { exp_continue }
373 timeout {
374 kill -INT -[exp_pid] 2
375 set failed 1
376 }
377 }
378 catch {close}; catch {wait}
379
380 # Our server should be trusted again, separate from the previously discovered
381 # trusted servers.
382 list_servers "List current trusted servers after reinstatement by ip address" online,trusted
383 array unset current_trusted_servers
384 array set current_trusted_servers [array get servers]
385
386 array_diff current_trusted_servers existing_trusted_servers
387 array unset new_trusted_servers
388 array set new_trusted_servers [array get diff]
389
390 set test "New trusted servers after reinstatement by ip address"
391 if {[array size new_trusted_servers] > 0} {
392 pass "$test"
393 } else {
394 fail "$test"
395 }
396
397 # The new_trusted_servers array should now match the our_servers
398 # array, since the our_servers array is a copy of the original
399 # new_trusted_servers array.
400 set test "New trusted servers matches after reinstatement by ip address"
401 if {[array_equal new_trusted_servers our_servers]} {
402 pass "$test"
403 } else {
404 fail "$test"
405 verbose -log "new_trusted_servers:"
406 foreach {index value} [array get new_trusted_servers] {
407 verbose -log " index: $index, value: $value"
408 }
409 verbose -log "our_servers:"
410 foreach {index value} [array get our_servers] {
411 verbose -log " index: $index, value: $value"
412 }
413 }
414
415 # Trust our server as a module signer. This must be done as root and
416 # in the namespace. Specify the server by certificate serial number.
417 set test "Server has certificate serial number"
418 if {[regexp {^.*certinfo="([^ ]*)".*} $our_servers(0) match cert_info]} {
419 pass $test
420 } else {
421 fail $test
422 }
423
424 # Grab the existing trusted signers in the namespace. There should be
425 # none (since we're starting with a clean slate).
426 set test "Namespace existing signer servers"
427 ns_list_servers "List namespace existing signing servers" signer
428 array unset ns_existing_signing_servers
429 array set ns_existing_signing_servers [array get servers]
430 if {[array size ns_existing_signing_servers] == 0} {
431 pass $test
432 } else {
433 fail $test
434 }
435
436 # We only want to change the system's trusted signer list in our mount
437 # namespace, not in the real system.
438 set test "Adding trusted signer (in a namespace)"
439 set cmd "stap --trust-servers=signer,no-prompt --use-server=$cert_info"
440 lassign [server_ns_as_root $cmd] rc output
441 if {$rc == 0} {
442 pass "$test"
443 } else {
444 fail "$test"
445 }
446
447 # Our server should now be trusted as a signer (but only in our namespace).
448 ns_list_servers "List namespace current online signing servers" online,signer
449 array unset ns_current_signing_servers
450 array set ns_current_signing_servers [array get servers]
451
452 # The current list of signing servers in the namespace should have
453 # changed.
454 array_diff ns_current_signing_servers ns_existing_signing_servers
455 array unset ns_new_signing_servers
456 array set ns_new_signing_servers [array get diff]
457 set test "New namespace signing servers"
458 if {[array size ns_new_signing_servers] > 0} {
459 pass "$test"
460 } else {
461 fail "$test"
462 }
463
464 # The current list of signing servers shouldn't have changed in the
465 # real system (since the list of servers in the real system and in the
466 # namespace should be separate).
467 list_servers "List current online signing servers" online,signer
468 array unset current_signing_servers
469 array set current_signing_servers [array get servers]
470
471 array_diff current_signing_servers existing_signing_servers
472 array unset new_signing_servers
473 array set new_signing_servers [array get diff]
474 set test "New signing servers"
475 if {[array size new_signing_servers] == 0} {
476 pass "$test"
477 } else {
478 fail "$test"
479 }
480
481 set test "Server has port number"
482 if {[regexp {^.*port=([^ ]*).*} $ns_new_signing_servers(0) match port_num]} {
483 pass $test
484 } else {
485 fail $test
486 }
487
488 foreach privilege {"--unprivileged" "--privilege=stapusr" "--privilege=stapsys"} {
489 # Compile a simple test using an unprivileged setting. This will
490 # ask the server to check and sign the module. Specify the server
491 # using host name and port.
492 #
493 # Notice we can use the server directly, and don't need to be in
494 # the namespace. We'll need to be in the namespace when trying to
495 # run the compiled module.
496 set test "Compile module using server with $privilege"
497 set failed 1
498 set module_name ""
499 set cmd [concat stap -p4 $privilege $srcdir/systemtap.server/hello.stp --use-server=$host_name:$port_num]
500 send_log "executing: $cmd\n"
501 eval spawn $cmd
502 expect {
503 -timeout 150
504 -re {^stap_[^ \t\r\n]*\.ko\r\n} {
505 set module_name [string trim "$expect_out(0,string)" \r\n]
506 set failed 0
507 exp_continue
508 }
509 -re {^.*\r\n} { exp_continue }
510 timeout {
511 kill -INT -[exp_pid] 2
512 }
513 }
514 catch {close}; catch {wait}
515 if {$failed == 0} {
516 pass "$test"
517 }
518 send_log "'$module_name'\n"
519
520 # Make sure that the module was returned
521 set no_module 0
522 set test "Module was created with $privilege"
523 catch {exec /bin/ls $module_name $module_name.sgn} result
524 send_log "$result\n"
525 if {[file exists $module_name]} {
526 pass "$test"
527 } else {
528 fail "$test"
529 set no_module 1
530 }
531
532 # Make sure that the module was signed
533 set no_signature 0
534 set test "Module was signed with $privilege"
535 if {[file exists $module_name.sgn]} {
536 pass "$test"
537 } else {
538 fail "$test"
539 set no_signature 1
540 }
541
542 # Make sure we can load the module. This will verify that the
543 # signature is correct and trusted. There must be a module to
544 # load.
545 #
546 # Note that we must do this in the namespace, since only there can
547 # the signature be verified.
548 #
549 # We're going to use the optional 'stapusr' and 'stapsys' users,
550 # if they exist.
551 set test "Load and run signed module when trusted with $privilege"
552 set user ""
553 if {$privilege == "--privilege=stapsys"} {
554 if {$systemtap_stapsys_user_exists} {
555 set user "stapsys"
556 } elseif {$systemtap_stapusr_user_exists} {
557 set user "stapusr"
558 setup_xfail *-*-*
559 }
560 } else {
561 if {$systemtap_stapusr_user_exists} {
562 set user "stapusr"
563 }
564 }
565
566 if {$no_module == 1 || $no_signature == 1 || [string length $user] == 0} {
567 untested "$test"
568 } else {
569 set failed 1
570 set module_path [exec pwd]/$module_name
571 set cmd [concat staprun $module_path]
572 lassign [server_ns_as_user $user $cmd] rc output
573 if {$rc == 0} {
574 foreach line [split $output \n] {
575 if {[regexp {^Hello From Server$} $line]} {
576 set failed 0
577 }
578 }
579 }
580 if {$failed == 0} {
581 pass "$test"
582 } else {
583 fail "$test"
584 }
585 }
586 }
587
588 # Revoke trust in our server as a module signer in the namespace.
589 # Specify the server by certificate serial number to ensure we're
590 # revoking trust in the correct server.
591 set test "Revoking trusted signer (in a namespace)"
592 set cmd [concat stap --trust-servers=revoke,signer,no-prompt --use-server=$cert_info]
593 lassign [server_ns_as_root $cmd] rc output
594 if {$rc == 0} {
595 pass "$test"
596 } else {
597 fail "$test"
598 }
599
600 # Our server should no longer be trusted as a signer in the namespace.
601 ns_list_servers "List namespace current signing servers after revocation " signer
602 array unset ns_current_signing_servers
603 array set ns_current_signing_servers [array get servers]
604
605 set test "No longer trusted as a signer after revocation"
606 if {[array_in ns_current_signing_servers ns_existing_signing_servers]} {
607 pass "$test"
608 } else {
609 fail "$test"
610 }
611
612 # Since our server is no longer a trusted signer, attempting to load
613 # and run the module now should fail (when run as 'stapsys' in the
614 # namespace). We have to use the 'stapsys' user since the last module
615 # compiled used "--privilege=stapsys".
616 set test "Load and run signed module when not trusted"
617 if {$no_module == 1 || !$systemtap_stapsys_user_exists} {
618 untested "$test"
619 } else {
620 setup_xfail *-*-*
621 set failed 1
622 set module_path [exec pwd]/$module_name
623 set cmd [concat staprun $module_path]
624 lassign [server_ns_as_user "stapsys" $cmd] rc output
625 if {$rc == 0} {
626 foreach line [split $output \n] {
627 if {[regexp {^Hello From Server$} $line]} {
628 set failed 0
629 }
630 }
631 }
632 if {$failed == 0} {
633 pass "$test"
634 } else {
635 fail "$test"
636 }
637 }
638
639 # Shutdown the server we started
640 shutdown_server