<commit_before>#############################################################
#  flowassert_module.tcl  
# -----------------------------------------------------------
#  Copyright (C) 2004-2005 Tresys Technology, LLC
#  see file 'COPYING' for use and warranty information 
#
#  Requires tcl and tk 8.3+, with BWidgets
#  Author: <<EMAIL>>
# -----------------------------------------------------------
#
# This is the implementation of the interface for Information
# Flow Assertion.
##############################################################
# ::Apol_Analysis_flowassert module namespace
##############################################################

namespace eval Apol_Analysis_flowassert {
    variable VERSION 1

    # internal representation of assertions (model)
    variable asserts [list "\# Place your assertion statements here"]

    # widget variables (view)
    variable assertfile_t
    variable assert_wizard_dlg ""

    # name of widget holding most recently executed assertion
    variable most_recent_results ""

    # name and path of most recently saved/loaded assertion files
    variable last_filename ""
    variable last_pathname ""
    
    variable info_button_text \
"Information flow in an access control policy refers to the ability for
information to flow from one process or object into another process or
object. This can be done directly or transitively through several
intermediate types. When analyzing an access control policy, the need
to see information flow from one type to another is very important and
while direct flow is fairly obvious, transitive flows are not.\n

To facilitate automated validation and regression testing of policies
the user creates a series of information flow assertions using a batch
language that represents transitive flows. The \"Information Flow 
Assertion\" analysis module for apol easily allows the user to generate 
and execute assertion statements.  Results from the analysis are presented
in a new window.  Assertions found to be correct are labeled as \"Passed\"; 
invalid ones are hyperlinked to the line within policy.conf that caused 
the conflict.\n

Currently, there are three type of assertions. The first one is noflow,
which asserts that there is no flow between two different types.  The
second is mustflow, which instead states that there must exist one flow
between two different types. The third type is onlyflow, which says that 
there must be a flow AND that flow must be through specific intermediary 
type(s). All three modes allow for any number of source, destination, 
and exception types. Types may be specified through the use of attributes.
Limiting types by object classes is disabled for this release; Finally, 
each assertion can have a minimum weight to limiting searches."

    # Within the namespace command for the module, you must call
    # Apol_Analysis::register_analysis_modules, the first argument is
    # the namespace name of the module, and the second is the
    # descriptive display name you want to be displayed in the GUI
    # selection box.
    Apol_Analysis::register_analysis_modules "Apol_Analysis_flowassert" \
        "Information Flow Assertion"
}

# Apol_Analysis_flowassert::initialize is called when the tool first
# starts up.  The analysis has the opportunity to do any additional
# initialization it must do that wasn't done in the initial namespace
# eval command.
proc Apol_Analysis_flowassert::initialize { } {
    catch {console show}
    return 0
}

# Returns the text to display whenever the user hits the 'Info'
# button.
proc Apol_Analysis_flowassert::get_analysis_info {} {
    return $Apol_Analysis_flowassert::info_button_text
}

# Returns the name of the widget that contains the results for the
# currently selected results tab.  This widget should have been
# created by [do_analysis] below.  The module knows which result tab
# is raised due to the most recent call
# [set_display_to_results_state].
proc Apol_Analysis_flowassert::get_results_raised_tab {} {
    variable most_recent_results
    return $most_recent_results
}

# The GUI will call Apol_Analysis_flowassert::do_analysis when the
# module is to perform its analysis.  The module should know how to
# get its own option information.  The options are displayed via
# Apol_Analysis_flowassert::display_mod_options.
proc Apol_Analysis_flowassert::do_analysis {results_frame} {  
    # if a permission map is not loaded then load the default one
    # if an error occurs on open then skip analysis
    if {[catch {Apol_Perms_Map::is_pmap_loaded} err] || $err == 0} {
        if {[set rt [catch {Apol_Perms_Map::load_default_perm_map} err]] != 0} {
            if {$rt == $Apol_Perms_Map::warning_return_val} {
                tk_messageBox -icon warning -type ok \
                    -title "Warning" -message $err
            } else {
                tk_messageBox -icon error -type ok \
                    -title "Error" -message $err
                return -code error
            }
        }
    }

    set sw [ScrolledWindow $results_frame.sw -auto horizontal]
    set t [text $sw.t -wrap none -bg white -font $ApolTop::text_font]
    Apol_PolicyConf::configure_HyperLinks $t
    $t tag configure assert_file_tag -underline 1
    $t tag bind assert_file_tag <Button-1> [namespace code [list highlight_assert_line  %W %x %y]]
    $t tag bind assert_file_tag <Enter> { set Apol_PolicyConf::orig_cursor [%W cget -cursor]; %W configure -cursor hand2 }
    $t tag bind assert_file_tag <Leave> { %W configure -cursor $Apol_PolicyConf::orig_cursor }

    $sw setwidget $t
    pack $sw -expand 1 -fill both
    variable most_recent_results
    set most_recent_results $sw
    
    variable assertfile_t
    set assert_contents [string trim [$assertfile_t get 1.0 end]]
    $t insert end "-- Executing --\n$assert_contents"
    update idletasks

    if [catch {apol_FlowAssertExecute $assert_contents 0} assert_results] {
        set retval -1
    } else {
        $t delete 1.0 end
        $t insert end "--- Results ---\n"
        set typeslist [apol_GetNames types]
        foreach result $assert_results {
            foreach {mode lineno result rules_list} $result {}
            set line "line $lineno: "
            set line_end_index [expr {[string length $line] - 2}]
            set policy_tags_list ""
            switch -- $result {
                0 { append line "Passed.\n" }
                1 {
                    set s ""
                    if {[llength $rules_list] > 1} {
                        set s "s"
                    }
                    append line "Assertion failed, conflict$s found:\n"
                    foreach rules $rules_list {
                        foreach {start_type end_type via_type rule_num rule} $rules {}
                        if {[set from [lindex $typeslist $start_type]] == ""} {
                            set from "<unknown type>"
                        }
                        if {[set to [lindex $typeslist $end_type]] == ""} {
                            set to "<unknown type>"
                        }
                        if {$rule_num >= 0} {
                            if {$ApolTop::policy_type != $ApolTop::binary_policy_type} {
                                append line "  $from to $to via \"$rule\" \["
                                set start_index [string length $line]
                                append line "$rule_num]\n"
                                set end_index [expr {[string length $line] - 2}]
                                lappend policy_tags_list $start_index $end_index
                            } else {
                                append line "  $from to $to\n"
                            }
                        } elseif {$via_type >= 0} {
                            if {[set via [lindex $Apol_Types::typelist $via_type]] == ""} {
                                set via "<unknown type>"
                            }
                            append line "  no rule from $from to $to via $via\n"
                        } else {
                            append line "  no rule from $from to $to\n"
                        }
                    }
                }
                2 { append line "Assertion has illegal mix of options.\n" }
                3 { append line "Unknown type or attribute specified.\n" }
                4 { append line "Unknown class specified.\n" }
                5 { append line "Variable undeclared.\n" }
                6 { append line "Syntax error.\n" }
                -1 { append line "Out of memory\n" }
                default {
                    append line "Invalid return value from execute_flow_execute(): $result"
                }
            }

            set offset [$t index "end - 1c"]
            $t insert end $line
            $t tag add assert_file_tag $offset "$offset + $line_end_index c"
            foreach {start_index end_index} $policy_tags_list {
                Apol_PolicyConf::insertHyperLink $t \
                    "$offset + $start_index c" "$offset + $end_index c"
            }
        }
        set retval 0
    }
    if {$assert_results == ""} {
        $t insert end "<success>"
    }
    $t configure -state disabled
    if {$retval == -1} {
        tk_messageBox -icon error -type ok -title "Flow Assertion Error" \
            -message $assert_results
    }
    $assertfile_t tag remove assert_file_sel_tag 1.0 end
    return $retval
}


# Apol_Analysis_flowassert::close must exist; it is called when a
# policy is closed.  Typically you should reset any context or option
# variables you have.
proc Apol_Analysis_flowassert::close { } {
    variable assert_wizard_dlg
    if [winfo exists $assert_wizard_dlg] {
        # update the dialog display to match this policy's declared
        # types and attributes
        populate_lists 0
    }
    return
}

# Apol_Analysis_flowassert::open must exist; it is called when a
# policy is opened.
proc Apol_Analysis_flowassert::open { } {
    variable assert_wizard_dlg
    if [winfo exists $assert_wizard_dlg] {
        # update the dialog display to match this policy's declared
        # types and attributes
        populate_lists 1
    }
}

# Called whenever a user loads a query file.  Clear away the old
# contents of the assertion file and replace it with the remainder
# from $file_channel.
proc Apol_Analysis_flowassert::load_query_options {file_channel parentDlg} {
    variable VERSION
    variable asserts
    if {[gets $file_channel] > $VERSION} {
        return -code error "The specified query version is not allowed."
    }
    set asserts [read $file_channel]
    sync_asserts_to_text
    return 0
}

# Called whenever a user saves a query
#	- module_name - name of the analysis module
#	- file_channel - file channel identifier of the query file to write to.
#	- file_name - name of the query file
proc Apol_Analysis_flowassert::save_query_options {module_name file_channel file_name} {
    variable VERSION
    variable asserts
    puts $file_channel $module_name
    puts $file_channel $VERSION
    puts -nonewline $file_channel $asserts
    return 0
}

# Captures the current set of options, which is then later restored by
# [set_display_to_results_tab].
proc Apol_Analysis_flowassert::get_current_results_state { } {
    variable asserts
    variable assertfile_t
    return [list $assertfile_t $asserts]
}

# Apol_Analysis_flowassert::set_display_to_results_state is called to
# reset the options or any other context that analysis needs when the
# GUI switches back to an existing analysis.  options is a list that
# we created in a previous get_current_results_state() call.
proc Apol_Analysis_flowassert::set_display_to_results_state {query_options} {
    variable most_recent_results
    foreach {assertfile_t assertcontents} $query_options {}
    variable asserts $assertcontents
    sync_asserts_to_text
    set most_recent_results $assertfile_t
}

# Apol_Analysis_flowassert::free_results_data is called to handle any
# cleanup of module options prior to [destroy]ing its parent frame.
# There are three times this function is called: when using the
# 'Update' button, when closing its result tab, and when closing all
# tabs.  query_options is a list that we created in a previous
# get_current_results_state() call, from which we extract the
# subwidget pathnames for the results frame.
proc Apol_Analysis_flowassert::free_results_data {query_options} {  
    return
}

# Apol_Analysis_dirflow::display_mod_options is called by the GUI to
# display the analysis options interface the analysis needs.  Each
# module must know how to display their own options, as well bind
# appropriate commands and variables with the options GUI.  opts_frame
# is the name of a frame in which the options GUI interface is to be
# packed.
proc Apol_Analysis_flowassert::display_mod_options { opts_frame } {
    variable asserts
    variable assertfile_t
    set tf [TitleFrame $opts_frame.assertfile -text "Assertion File"]
    set sw [ScrolledWindow [$tf getframe].sw -auto horizontal]
    set assertfile_t [text $sw.assertfile_t -wrap none -state normal \
                          -bg white -font $ApolTop::text_font -cursor arrow]
    $assertfile_t tag configure assert_file_t_tag
    $assertfile_t tag configure assert_file_sel_tag -background gray
    $assertfile_t tag bind assert_file_t_tag <Button-1> [namespace code [list assertfile_click %W %x %y]]
    $assertfile_t tag bind assert_file_t_tag <Double-Button-1> [namespace code edit_line]
    $sw setwidget $assertfile_t
    grid $tf -padx 10 -sticky nsew
    pack $sw -expand 1 -fill both
    set bb1 [ButtonBox $opts_frame.bb1 -homogeneous 1 -spacing 20]
    $bb1 add -text "Add Assertion..." -command [namespace code create_assert_wizard_dlg]
    $bb1 add -text "Add Comment..." -command [namespace code add_comment_dlg]
    $bb1 add -text "Edit Line..." -command [namespace code edit_line]
    $bb1 add -text "Delete Line" -command [namespace code delete_line]
    set bb2 [ButtonBox $opts_frame.bb2 -homogeneous 1 -spacing 20]
    $bb2 add -text "Clear All" -command [namespace code clear_assert_file]
    $bb2 add -text "Export Assertions..." -command [namespace code export_assert_file]
    $bb2 add -text "Import Assertions..." -command [namespace code import_assert_file]
    grid $bb1 -sticky {}
    grid $bb2 -sticky {}
    grid rowconfigure $opts_frame 0 -weight 1
    grid rowconfigure $opts_frame 1 -weight 0 -pad 20
    grid columnconfigure $opts_frame 0 -weight 1
    variable line_editing_buttons [list $bb1 2 3]
    sync_asserts_to_text
}


##########################################################################
##########################################################################
## The rest of these procs are not interface procedures, but rather
## internal functions to this analysis.
##########################################################################
##########################################################################

# Called whenever the user clicks on the assertion file text field.
# Select the line clicked and enable the line editing buttons.  If the
# click was on an already selected line, deselect it and disable the
# buttons.
proc Apol_Analysis_flowassert::assertfile_click {widget x y} {
    variable asserts
    set newline [lindex [split [$widget index @$x,$y] .] 0]
    set selection [lindex [$widget tag ranges assert_file_sel_tag] 0]
    set oldline [lindex [split [lindex $selection 0] .] 0]
    # remove old selection
    if {$oldline != ""} {
        $widget tag remove assert_file_sel_tag $oldline.0 [expr {$oldline + 1}].0
    }
    if {$newline >= 1 && $newline <= [llength $asserts] && $newline != $oldline} {
        # add a selection
        $widget tag add assert_file_sel_tag $newline.0 [expr {$newline + 1}].0
    }
    enable_line_editing
}

# Enable/disable the line editing buttons (edit line and delete line),
# but only if something is selected.
proc Apol_Analysis_flowassert::enable_line_editing {} {
    variable assertfile_t    
    if {[$assertfile_t tag ranges assert_file_sel_tag] != ""} {
        set newstate normal
    } else {
        set newstate disabled
    }
    variable line_editing_buttons
    set parent [lindex $line_editing_buttons 0]
    foreach button [lrange $line_editing_buttons 1 end] {
        $parent itemconfigure $button -state $newstate
    }
}

# Deletes the contents of the assertion file text box, and thus
# consequently clearing the file.
proc Apol_Analysis_flowassert::clear_assert_file {} {
    variable asserts ""
    sync_asserts_to_text
}

# Prompts the user for a file name, then saves the contents of the
# assertion buffer to it.
proc Apol_Analysis_flowassert::export_assert_file {} {
    variable assertfile_t
    variable last_filename
    variable last_pathname
    set filename [tk_getSaveFile -title "Export Assertion File" \
                      -parent $assertfile_t -initialdir $last_pathname \
                      -initialfile $last_filename]
    if {$filename == ""} {
        return
    }
    if [catch {::open $filename w} f] {
        tk_messageBox -icon error -type ok -title "Export Assertion File" \
            -message "Error saving to $filename" -parent $assertfile_t
        return
    }
    puts -nonewline $f [$assertfile_t get 1.0 end]
    ::close $f
    set last_filename [file tail $filename]
    set last_pathname [file dirname $filename]
}

# Prompts the user for a file name, then loads the contents of the
# file into the assertion buffer.
proc Apol_Analysis_flowassert::import_assert_file {} {
    variable assertfile_t
    variable last_filename
    variable last_pathname
    set filename [tk_getOpenFile -title "Import Assertion File" \
                      -parent $assertfile_t -initialdir $last_pathname \
                      -initialfile $last_filename]
    if {$filename == ""} {
        return
    }
    if [catch {::open $filename r} f] {
        tk_messageBox -icon error -type ok -title "Import Assertion File" \
            -message "Error loading from $filename" -parent $assertfile_t
        return
    }
    variable asserts ""
    foreach line [split [read $f] \n] {
        if {[string index [string trimleft $line] 0] == "\#"} {
            lappend asserts $line
        } else {
            foreach a [split $line ";"] {
                if {$a != ""} {
                    lappend asserts $a
                }
            }
        }
    }
    ::close $f
    set last_filename [file tail $filename]
    set last_pathname [file dirname $filename]
    sync_asserts_to_text
}

# Creates the "assertion statement wizard" that allows a user to
# easily add assertions lines to the current file.  The wizard is a
# non-modal dialog that presents all available types + attributes /
# options.  It does not validate entries for syntactical correctness.
proc Apol_Analysis_flowassert::create_assert_wizard_dlg {{origline {}}} {
    variable assertfile_t
    variable assert_wizard_dlg
    
    if [winfo exists $assert_wizard_dlg] {
        raise $assert_wizard_dlg
        return
    }

    variable assert_wiz_mode
    variable assert_wiz_line
    variable assert_wiz_weight

    # create a modal dialog
    set assert_wizard_dlg [Dialog .assertfile_wizard_dlg -homogeneous 1 \
                               -spacing 20 -anchor c -cancel 2 -modal global \
                               -parent $assertfile_t -separator 0 \
                               -side bottom -title "Add Assertion Statements"]
    set f [$assert_wizard_dlg getframe]
    set topf [frame $f.topf]

    variable assert_wiz_type_cbs ""
    variable assert_wiz_objclass_lbs ""
    set start_tf [TitleFrame $topf.start_tf -text "Starting Types"]
    create_type_panel [$start_tf getframe] start assert_wiz_start_type
    set end_tf [TitleFrame $topf.end_tf -text "Ending Types"]
    create_type_panel [$end_tf getframe] end assert_wiz_end_type
    set via_tf [TitleFrame $topf.via_tf -text "Exceptions Type"]
    create_type_panel [$via_tf getframe] via assert_wiz_via_type
    
    set f0 [frame $topf.f0]
    set mode_tf [TitleFrame $f0.tf -text "Assertion Mode"]
    set noflow_rb [radiobutton [$mode_tf getframe].noflow_rb \
                  -text "noflow" -value "noflow" \
                  -variable Apol_Analysis_flowassert::assert_wiz_mode \
                  -command [namespace code [list set_mode "noflow" $via_tf]]]
    set mustflow_rb [radiobutton [$mode_tf getframe].mustflow_rb \
                  -text "mustflow" -value "mustflow" \
                  -variable Apol_Analysis_flowassert::assert_wiz_mode \
                  -command [namespace code [list set_mode "mustflow" $via_tf]]]
    set onlyflow_rb [radiobutton [$mode_tf getframe].onlyflow_rb \
                  -text "onlyflow" -value "onlyflow" \
                  -variable Apol_Analysis_flowassert::assert_wiz_mode \
                  -command [namespace code [list set_mode "onlyflow" $via_tf]]]
    pack $noflow_rb $mustflow_rb $onlyflow_rb -anchor w -side top
    set lf [LabelFrame $f0.lf -text "Minimum Weight: " -side left]
    set weight_sb [SpinBox [$lf getframe].weight_sb -font $ApolTop::text_font \
                    -width 3 -entrybg white -range [list 1 10 1] \
                    -editable 0 -justify right \
                    -textvariable Apol_Analysis_flowassert::assert_wiz_weight \
                    -modifycmd [namespace code set_weight]]
    pack $weight_sb -expand 1 -fill both
    pack $mode_tf -expand 1 -fill both -pady 20
    pack $lf -expand 1 -fill x -pady 20
    pack $f0 -expand 0 -fill none -side left -padx 10
    pack $start_tf $end_tf $via_tf -expand 1 -fill both -side left
    pack $topf -side top -expand 1 -fill both
    
    set rule_e [LabelEntry $f.rule_e -width 80 \
                   -font $ApolTop::text_font -entrybg white \
                    -label "Assertion Line: " \
                    -textvariable Apol_Analysis_flowassert::assert_wiz_line]
    pack $rule_e -side top -expand 0 -fill x -padx 20 -pady 20 -anchor center

    $assert_wizard_dlg add -text "Reset Line" \
        -command [namespace code [list reset_wizard $via_tf $origline]]
    if {$origline == ""} { 
        $assert_wizard_dlg add -text "Add Line" \
            -command [namespace code add_assertion]
    } else {
        $assert_wizard_dlg add -text "Edit Line" \
            -command [namespace code add_assertion]
    }
    $assert_wizard_dlg add -text "Cancel"
    
    populate_lists 1
    reset_wizard $via_tf $origline

    set result [$assert_wizard_dlg draw]
    destroy $assert_wizard_dlg
    if {$result == 1} {
        # user clicked on add line
        return 1
    } else {
        # user canceled dialog
        return 0
    }
}

# Creates an individual panel {starting, ending, via/exceptions} for
# the assertion wizard.
proc Apol_Analysis_flowassert::create_type_panel {type_panel type_name type_var} {
    # add the type selection radiobutton + ComboBox
    set type_star_rb [radiobutton $type_panel.star_cb -value "*" \
                          -text "Any Type" \
                          -variable Apol_Analysis_flowassert::assert_wiz_${type_name}_type_rb]
    pack $type_star_rb -anchor w -side top
    
    set type_names_f [frame $type_panel.type_names_f]
    set type_names_l [label $type_names_f.names_l -text "Type Names:"]
    set type_names_cb [ComboBox $type_names_f.names_cb -width 24 \
                           -editable 1 -entrybg white -exportselection 0 \
                           -textvariable Apol_Analysis_flowassert::assert_wiz_${type_name}_type_name]
    bindtags $type_names_cb.e [linsert [bindtags $type_names_cb.e] 3 ${type_name}_names_tags]
    bind ${type_name}_names_tags <KeyPress> [list ApolTop::_create_popup $type_names_cb %W %K]
    pack $type_names_l -expand 0 -side top
    pack $type_names_cb -expand 0 -fill both -side top
    pack $type_names_f -expand 0 -fill none -side top -pady 2

    # add bindings between these so that modifying one clears the other
    set bindcmd [namespace code [list set_type $type_star_rb $type_names_cb $type_var star ""]]
    $type_star_rb configure -command $bindcmd
    set bindcmd [namespace code [list set_type $type_star_rb $type_names_cb $type_var name %P]]
    $type_names_cb configure -modifycmd $bindcmd -vcmd $bindcmd -validate key

    pack [Separator $type_panel.strut0 -orient horizontal] \
        -expand 0 -fill x -side top -pady 10

    # add the object selection listbox + Entry
    set class_var assert_wiz_${type_name}_class
    pack [label $type_panel.objs_l -text "Object Classes:"] -expand 0
    set objs_sw [ScrolledWindow $type_panel.objs_sw -auto horizontal]
    set objs_lb [listbox [$objs_sw getframe].objs_lb -selectmode multiple \
                     -height 5 -highlightthickness 0 -exportselection 0 \
                     -setgrid 0 -background white \
                     -listvariable Apol_Analysis_flowassert::assert_wizard_objclasses]
    $objs_sw setwidget $objs_lb
#    pack $objs_sw -expand 0 -fill both
    set objs_e [Entry $type_panel.objs_e -background white -validate key \
                    -textvariable Apol_Analysis_flowassert::${class_var}]
    $objs_e configure -vcmd [namespace code [list select_class $objs_lb $class_var $objs_e %P]]
#    pack $objs_e -expand 0 -fill x -pady 2
    bind $objs_lb <<ListboxSelect>> [namespace code [list select_class $objs_lb $class_var $objs_lb ""]]
    
#    pack [Separator $type_panel.strut1 -orient horizontal] \
# -expand 0 -fill x -side top -pady 10

    # add the include / exclude radio buttons
    set include_var assert_wiz_${type_name}_include
    set include_rb [radiobutton $type_panel.include_rb -value "include" \
                        -text "Include Type/Object" \
                        -variable Apol_Analysis_flowassert::$include_var]
    set exclude_rb [radiobutton $type_panel.exclude_rb -value "exclude" \
                        -text "Exclude Type/Object" \
                        -variable Apol_Analysis_flowassert::$include_var]
#    pack $include_rb $exclude_rb -anchor w -side top

    # FIX ME: temporary patch for now for lack of object classes
    pack [label $type_panel.nobj -text "Object classes are\ncurrently disabled." -bd 1 -relief sunken -padx 5 -pady 5] -pady 5 -expand 0 -fill none
    
    pack [button $type_panel.add_b -text "Add" \
              -command [namespace code [list add_type $type_var $type_name $class_var $include_var]]] \
        -expand 0 -pady 10 -side bottom
    
    # add the widgets' paths list
    variable assert_wiz_type_cbs
    lappend assert_wiz_type_cbs $type_names_cb
    variable assert_wiz_objclass_lbs
    lappend assert_wiz_objclass_lbs $objs_lb
}

# Safely destroys a modal dialog.
proc Apol_Analysis_flowassert::destroy_wizard_dlg {dlg} {
    grab release $dlg
    destroy $dlg
}

# Called to populate the ComboBox/listbox holding the type names +
# attributes/object classes.  This is done in one of three places:
# upon wizard dialog creation, when opening a policy, and when closing
# a policy.
proc Apol_Analysis_flowassert::populate_lists {add_items} {
    variable assert_wiz_type_cbs
    variable assert_wiz_objclass_lbs
    if $add_items {
        foreach type_cb $assert_wiz_type_cbs {
            $type_cb configure -values [lsort [concat $Apol_Types::typelist $Apol_Types::attriblist]]
        }
        foreach objclass_lb $assert_wiz_objclass_lbs {
            set var [$objclass_lb cget -listvariable]
            set $var $Apol_Class_Perms::class_list
        }
    } else {
        foreach type_cb $assert_wiz_type_cbs {
            $type_cb configure -values {}
        }
        foreach objclass_lb $assert_wiz_objclass_lbs {
            set var [$objclass_lb cget -listvariable]
            set $var {}
        }
    }
}

# Callback when a user clicks on a mode radiobutton {noflow, mustflow,
# onlyflow}.  Updates the title of the via_tf frame.  Updates the mode
# listed in the current assertion line.
proc Apol_Analysis_flowassert::set_mode {newmode via_tf} {
    variable assert_wiz_line
    if {$newmode == "noflow"} {
        $via_tf configure -text "Exception Types (optional)"
    } elseif {$newmode == "mustflow"} {
        $via_tf configure -text "Via Types (optional)"
    } else {
        $via_tf configure -text "Via Types (required)"
    }
    set assert_wiz_line [concat $newmode [lrange $assert_wiz_line 1 end]]
}

# Callback whenever a user selects a new weight from the spinner.
# Updates the weighting value in the current assertion line.
proc Apol_Analysis_flowassert::set_weight {} {
    variable assert_wiz_line
    variable assert_wiz_weight
    if {[llength $assert_wiz_line] >= 5} {
        set assert_wiz_line [lrange $assert_wiz_line 0 end-1]
        lappend assert_wiz_line $assert_wiz_weight
    } elseif {[llength $assert_wiz_line] == 4} {
        lappend assert_wiz_line $assert_wiz_weight
    }
}

# Callback whenever the user selects a type.  Disables the other
# widgets.  Sets the internal variable to be equal to the name of the
# type.
proc Apol_Analysis_flowassert::set_type {star_rb names_cb type_var which newvalue} {
    variable $type_var
    switch -- $which {
        star {
            $star_rb select
            $names_cb configure -text ""
            set $type_var "*"
        }
        name {
            $star_rb deselect
            if {$newvalue == "%P"} {
                set newvalue [$names_cb cget -text]
            }
            set $type_var $newvalue
        }
    }
    return 1
}

# Callback whenever the user modifies object classes selection, either
# by clicking on an item in the listbox or manually typing in a new
# one.  If clicking on the listbox, update the entry by
# appending/removing classes.  If manually editing the entry,
# select/deslect items from the listbox.
proc Apol_Analysis_flowassert::select_class {objs_lb class_var widget newvalue} {
    variable $class_var
    set objs_list [$objs_lb get 0 end]
    if {[string range $widget end-2 end] == "_lb"} {
        # user selected/deselected something from the listbox.
        # synchronize the widgets giving precedence to the listbox.
        # check that all selected in the listbox is in class_var and
        # that all /not/ selected are not in class_var
        set selected_list [$objs_lb curselection]
        set new_class_val [set $class_var]
        for {set i 0} {$i < [llength $objs_list]} {incr i} {
            set item [lindex $objs_list $i]
            if {[lsearch -exact -sorted -integer $selected_list $i] >= 0} {
                # item selected; add it to the entry line if not already there
                if {[lsearch -exact $new_class_val $item] == -1} {
                    lappend new_class_val $item
                }
            } else {
                # item deselected; if in entry line remove it
                if {[set x [lsearch -exact $new_class_val $item]] >= 0} {
                    set new_class_val [lreplace $new_class_val $x $x]
                }                
            }
        }
        set $class_var $new_class_val
    } else {
        # entry widget changed.  synchronize the widgets giving
        # precedence to the entry box.  auto-select in the listbox
        # anything new.  deselect those that are not in class_var.
        $objs_lb selection clear 0 end
        foreach class $newvalue {
            if {[set index [lsearch -exact $objs_list $class]] >= 0} {
                $objs_lb selection set $index
                $objs_lb see $index
            }
        }
    }
    return 1
}

# Add a type to either the starting, ending, or via list.  Do it
# smartly -- e.g., adding a '*' to a via list clears it.
proc Apol_Analysis_flowassert::add_type {type_var pos class_var include_var} {
    variable $type_var
    if {[set type [set $type_var]] == ""} {
        return
    }

    switch -- $pos {
        start { set old_pos 1 }
        end   { set old_pos 2 }
        via   { set old_pos 3 }
    }
    variable assert_wiz_line
    set old_list [lindex $assert_wiz_line $old_pos]
    
    variable $include_var
    variable $class_var
    set objclasses [set $class_var]
    if {[llength $objclasses] > 1} {
        set objclasses [list $objclasses]
    }
    if {$objclasses != ""} {
        set typeid "$type:$objclasses"
    } else {
        set typeid $type
    }
    if {[set $include_var] == "include"} {
        if {$old_list == "*"} {
            set new_list $typeid
        } elseif {$typeid == "*"} {
            if {$pos == "via"} {
                set new_list {}
            } else {
                set new_list "*"
            }
        } else {
            set new_list [concat $old_list $typeid]
        }
    } else {
        set typeid "-$typeid"
        if {$typeid == "-*"} {
            if {$pos == "via"} {
                set new_list {}
            } else {
                set new_list "*"
            }
        } else {
            set new_list [concat $old_list $typeid]
        }
    }

    set assert_wiz_line [lreplace $assert_wiz_line $old_pos $old_pos $new_list]
    reset_type $pos
}

# Takes the current assertion line and adds it to the current
# assertion file.  Then closes the assertion dialog
proc Apol_Analysis_flowassert::add_assertion {} {
    variable assertfile_t
    # add the line if and only if the widget is still visible (which
    # might not be the case if the user has clicked on a different
    # analysis module since this wizard was created)
    if [winfo exists $assertfile_t] {
        variable assert_wiz_line
        add_line $assert_wiz_line
        variable assert_wizard_dlg
        $assert_wizard_dlg enddialog 1
    }
}

# Resets all widgets in the assertion wizard to their original
# positions.  Clears the assertion line.
proc Apol_Analysis_flowassert::reset_wizard {via_tf origline} {
    if {$origline == ""} {
        set mode "noflow"
        set start {}
        set to {}
        set via {}
        set weight 1
    } else {
        foreach {mode start to via weight} $origline {}
    }
    if {$start == {}} {
        set start "*"
    }
    if {$to == {}} {
        set to "*"
    }
    variable assert_wiz_mode $mode
    variable assert_wiz_line [list $mode $start $to $via $weight]
    variable assert_wiz_weight $weight
    set_mode $mode $via_tf
    foreach name {start end via} {
        reset_type $name
    }
}

# Clears the values within a single wizard type panel.  This has the
# side effect of clearing the widget view as well.
proc Apol_Analysis_flowassert::reset_type {name} {
    # because the next three variables are all bounded to event
    # handlers, do the radiobutton last so that its display dominates
    # the other two
    variable assert_wiz_${name}_type_name ""
    variable assert_wiz_${name}_type_rb "*"
    variable assert_wiz_${name}_type "*"
    variable assert_wiz_${name}_class ""
    variable assert_wiz_${name}_include "include"
}

# Whenever the user clicks on the assertion "line xxx" hyperlink from
# the results textbox, highlight the line within the assertion file
# textbox.  Highlighting is done by changing the selection.  This
# assumes that the assertion file contents have not changed since
# executing the file.
proc Apol_Analysis_flowassert::highlight_assert_line {widget x y} {
    set line [eval $widget get [$widget tag prevrange assert_file_tag "@$x,$y + 1 char"]]
    foreach {foo linenum} [split $line] {}
    variable assertfile_t
    $assertfile_t tag remove assert_file_sel_tag 1.0 end
    $assertfile_t tag add assert_file_sel_tag $linenum.0 [expr {$linenum + 1}].0
    $assertfile_t see $linenum.0
    enable_line_editing
}

# Given an assertion (either a comment or a 4-tuple/5-tuple) return a
# string that represents the line.
proc Apol_Analysis_flowassert::render_assertion {assertion} {
    if {[string index [string trimleft $assertion] 0] == "\#"} {
        return $assertion
    } else {
        foreach {mode start to via weight} $assertion {}
        if {$start == {}} {
            set start "*"
        }
        if {$to == {}} {
            set to "*"
        }
        if {$via == {}} {
            return "[list $mode $start $to $weight];"
        } else {
            return "[list $mode $start $to $via $weight];"
        }
    }
}

# Resynchronize the assertions (model) to its view (assertfile_t text
# field).
proc Apol_Analysis_flowassert::sync_asserts_to_text {} {
    variable asserts
    variable assertfile_t
    $assertfile_t configure -state normal
    $assertfile_t delete 1.0 end
    foreach line $asserts {
        $assertfile_t insert end "[render_assertion $line]\n"
    }
    $assertfile_t tag add assert_file_t_tag 1.0 end
    $assertfile_t configure -state disabled
    enable_line_editing
}

# Displays a dialog box to allow user to add a comment to the<commit_msg>Improved wizard that keeps track of types, allowing for easy appending/replacing/removing.<commit_after>#############################################################
#  flowassert_module.tcl  
# -----------------------------------------------------------
#  Copyright (C) 2004-2005 Tresys Technology, LLC
#  see file 'COPYING' for use and warranty information 
#
#  Requires tcl and tk 8.3+, with BWidgets
#  Author: <<EMAIL>>
# -----------------------------------------------------------
#
# This is the implementation of the interface for Information
# Flow Assertion.
##############################################################
# ::Apol_Analysis_flowassert module namespace
##############################################################

namespace eval Apol_Analysis_flowassert {
    variable VERSION 1

    # internal representation of assertions (model)
    variable asserts [list "\# Place your assertion statements here"]

    # widget variables (view)
    variable assertfile_t
    variable assert_wizard_dlg ""

    # name of widget holding most recently executed assertion
    variable most_recent_results ""

    # name and path of most recently saved/loaded assertion files
    variable last_filename ""
    variable last_pathname ""
    
    variable info_button_text \
"Information flow in an access control policy refers to the ability for
information to flow from one process or object into another process or
object. This can be done directly or transitively through several
intermediate types. When analyzing an access control policy, the need
to see information flow from one type to another is very important and
while direct flow is fairly obvious, transitive flows are not.\n

To facilitate automated validation and regression testing of policies
the user creates a series of information flow assertions using a batch
language that represents transitive flows. The \"Information Flow 
Assertion\" analysis module for apol easily allows the user to generate 
and execute assertion statements.  Results from the analysis are presented
in a new window.  Assertions found to be correct are labeled as \"Passed\"; 
invalid ones are hyperlinked to the line within policy.conf that caused 
the conflict.\n

Currently, there are three type of assertions. The first one is noflow,
which asserts that there is no flow between two different types.  The
second is mustflow, which instead states that there must exist one flow
between two different types. The third type is onlyflow, which says that 
there must be a flow AND that flow must be through specific intermediary 
type(s). All three modes allow for any number of source, destination, 
and exception types. Types may be specified through the use of attributes.
Limiting types by object classes is disabled for this release; Finally, 
each assertion can have a minimum weight to limiting searches."

    # Within the namespace command for the module, you must call
    # Apol_Analysis::register_analysis_modules, the first argument is
    # the namespace name of the module, and the second is the
    # descriptive display name you want to be displayed in the GUI
    # selection box.
    Apol_Analysis::register_analysis_modules "Apol_Analysis_flowassert" \
        "Information Flow Assertion"
}

# Apol_Analysis_flowassert::initialize is called when the tool first
# starts up.  The analysis has the opportunity to do any additional
# initialization it must do that wasn't done in the initial namespace
# eval command.
proc Apol_Analysis_flowassert::initialize { } {
    catch {console show}
    return 0
}

# Returns the text to display whenever the user hits the 'Info'
# button.
proc Apol_Analysis_flowassert::get_analysis_info {} {
    return $Apol_Analysis_flowassert::info_button_text
}

# Returns the name of the widget that contains the results for the
# currently selected results tab.  This widget should have been
# created by [do_analysis] below.  The module knows which result tab
# is raised due to the most recent call
# [set_display_to_results_state].
proc Apol_Analysis_flowassert::get_results_raised_tab {} {
    variable most_recent_results
    return $most_recent_results
}

# The GUI will call Apol_Analysis_flowassert::do_analysis when the
# module is to perform its analysis.  The module should know how to
# get its own option information.  The options are displayed via
# Apol_Analysis_flowassert::display_mod_options.
proc Apol_Analysis_flowassert::do_analysis {results_frame} {  
    # if a permission map is not loaded then load the default one
    # if an error occurs on open then skip analysis
    if {[catch {Apol_Perms_Map::is_pmap_loaded} err] || $err == 0} {
        if {[set rt [catch {Apol_Perms_Map::load_default_perm_map} err]] != 0} {
            if {$rt == $Apol_Perms_Map::warning_return_val} {
                tk_messageBox -icon warning -type ok \
                    -title "Warning" -message $err
            } else {
                tk_messageBox -icon error -type ok \
                    -title "Error" -message $err
                return -code error
            }
        }
    }

    set sw [ScrolledWindow $results_frame.sw -auto horizontal]
    set t [text $sw.t -wrap none -bg white -font $ApolTop::text_font]
    Apol_PolicyConf::configure_HyperLinks $t
    $t tag configure assert_file_tag -underline 1
    $t tag bind assert_file_tag <Button-1> [namespace code [list highlight_assert_line  %W %x %y]]
    $t tag bind assert_file_tag <Enter> { set Apol_PolicyConf::orig_cursor [%W cget -cursor]; %W configure -cursor hand2 }
    $t tag bind assert_file_tag <Leave> { %W configure -cursor $Apol_PolicyConf::orig_cursor }

    $sw setwidget $t
    pack $sw -expand 1 -fill both
    variable most_recent_results
    set most_recent_results $sw
    
    variable assertfile_t
    set assert_contents [string trim [$assertfile_t get 1.0 end]]
    $t insert end "-- Executing --\n$assert_contents"
    update idletasks

    if [catch {apol_FlowAssertExecute $assert_contents 0} assert_results] {
        set retval -1
    } else {
        $t delete 1.0 end
        $t insert end "--- Results ---\n"
        set typeslist [apol_GetNames types]
        foreach result $assert_results {
            foreach {mode lineno result rules_list} $result {}
            set line "line $lineno: "
            set line_end_index [expr {[string length $line] - 2}]
            set policy_tags_list ""
            switch -- $result {
                0 { append line "Passed.\n" }
                1 {
                    set s ""
                    if {[llength $rules_list] > 1} {
                        set s "s"
                    }
                    append line "Assertion failed, conflict$s found:\n"
                    foreach rules $rules_list {
                        foreach {start_type end_type via_type rule_num rule} $rules {}
                        if {[set from [lindex $typeslist $start_type]] == ""} {
                            set from "<unknown type>"
                        }
                        if {[set to [lindex $typeslist $end_type]] == ""} {
                            set to "<unknown type>"
                        }
                        if {$rule_num >= 0} {
                            if {$ApolTop::policy_type != $ApolTop::binary_policy_type} {
                                append line "  $from to $to via \"$rule\" \["
                                set start_index [string length $line]
                                append line "$rule_num]\n"
                                set end_index [expr {[string length $line] - 2}]
                                lappend policy_tags_list $start_index $end_index
                            } else {
                                append line "  $from to $to\n"
                            }
                        } elseif {$via_type >= 0} {
                            if {[set via [lindex $Apol_Types::typelist $via_type]] == ""} {
                                set via "<unknown type>"
                            }
                            append line "  no rule from $from to $to via $via\n"
                        } else {
                            append line "  no rule from $from to $to\n"
                        }
                    }
                }
                2 { append line "Assertion has illegal mix of options.\n" }
                3 { append line "Unknown type or attribute specified.\n" }
                4 { append line "Unknown class specified.\n" }
                5 { append line "Variable undeclared.\n" }
                6 { append line "Syntax error.\n" }
                -1 { append line "Out of memory\n" }
                default {
                    append line "Invalid return value from execute_flow_execute(): $result"
                }
            }

            set offset [$t index "end - 1c"]
            $t insert end $line
            $t tag add assert_file_tag $offset "$offset + $line_end_index c"
            foreach {start_index end_index} $policy_tags_list {
                Apol_PolicyConf::insertHyperLink $t \
                    "$offset + $start_index c" "$offset + $end_index c"
            }
        }
        set retval 0
    }
    if {$assert_results == ""} {
        $t insert end "<success>"
    }
    $t configure -state disabled
    if {$retval == -1} {
        tk_messageBox -icon error -type ok -title "Flow Assertion Error" \
            -message $assert_results
    }
    $assertfile_t tag remove assert_file_sel_tag 1.0 end
    return $retval
}


# Apol_Analysis_flowassert::close must exist; it is called when a
# policy is closed.  Typically you should reset any context or option
# variables you have.
proc Apol_Analysis_flowassert::close { } {
    variable assert_wizard_dlg
    if [winfo exists $assert_wizard_dlg] {
        # update the dialog display to match this policy's declared
        # types and attributes
        populate_lists 0
    }
    return
}

# Apol_Analysis_flowassert::open must exist; it is called when a
# policy is opened.
proc Apol_Analysis_flowassert::open { } {
    variable assert_wizard_dlg
    if [winfo exists $assert_wizard_dlg] {
        # update the dialog display to match this policy's declared
        # types and attributes
        populate_lists 1
    }
}

# Called whenever a user loads a query file.  Clear away the old
# contents of the assertion file and replace it with the remainder
# from $file_channel.
proc Apol_Analysis_flowassert::load_query_options {file_channel parentDlg} {
    variable VERSION
    variable asserts
    if {[gets $file_channel] > $VERSION} {
        return -code error "The specified query version is not allowed."
    }
    set asserts [read $file_channel]
    sync_asserts_to_text
    return 0
}

# Called whenever a user saves a query
#	- module_name - name of the analysis module
#	- file_channel - file channel identifier of the query file to write to.
#	- file_name - name of the query file
proc Apol_Analysis_flowassert::save_query_options {module_name file_channel file_name} {
    variable VERSION
    variable asserts
    puts $file_channel $module_name
    puts $file_channel $VERSION
    puts -nonewline $file_channel $asserts
    return 0
}

# Captures the current set of options, which is then later restored by
# [set_display_to_results_tab].
proc Apol_Analysis_flowassert::get_current_results_state { } {
    variable asserts
    variable assertfile_t
    return [list $assertfile_t $asserts]
}

# Apol_Analysis_flowassert::set_display_to_results_state is called to
# reset the options or any other context that analysis needs when the
# GUI switches back to an existing analysis.  options is a list that
# we created in a previous get_current_results_state() call.
proc Apol_Analysis_flowassert::set_display_to_results_state {query_options} {
    variable most_recent_results
    foreach {assertfile_t assertcontents} $query_options {}
    variable asserts $assertcontents
    sync_asserts_to_text
    set most_recent_results $assertfile_t
}

# Apol_Analysis_flowassert::free_results_data is called to handle any
# cleanup of module options prior to [destroy]ing its parent frame.
# There are three times this function is called: when using the
# 'Update' button, when closing its result tab, and when closing all
# tabs.  query_options is a list that we created in a previous
# get_current_results_state() call, from which we extract the
# subwidget pathnames for the results frame.
proc Apol_Analysis_flowassert::free_results_data {query_options} {  
    return
}

# Apol_Analysis_dirflow::display_mod_options is called by the GUI to
# display the analysis options interface the analysis needs.  Each
# module must know how to display their own options, as well bind
# appropriate commands and variables with the options GUI.  opts_frame
# is the name of a frame in which the options GUI interface is to be
# packed.
proc Apol_Analysis_flowassert::display_mod_options { opts_frame } {
    variable asserts
    variable assertfile_t
    set tf [TitleFrame $opts_frame.assertfile -text "Assertion File"]
    set sw [ScrolledWindow [$tf getframe].sw -auto horizontal]
    set assertfile_t [text $sw.assertfile_t -wrap none -state normal \
                          -bg white -font $ApolTop::text_font -cursor arrow]
    $assertfile_t tag configure assert_file_t_tag
    $assertfile_t tag configure assert_file_sel_tag -background gray
    $assertfile_t tag bind assert_file_t_tag <Button-1> [namespace code [list assertfile_click %W %x %y]]
    $assertfile_t tag bind assert_file_t_tag <Double-Button-1> [namespace code edit_line]
    $sw setwidget $assertfile_t
    grid $tf -padx 10 -sticky nsew
    pack $sw -expand 1 -fill both
    set bb1 [ButtonBox $opts_frame.bb1 -homogeneous 1 -spacing 20]
    $bb1 add -text "Add Assertion..." -command [namespace code create_assert_wizard_dlg]
    $bb1 add -text "Add Comment..." -command [namespace code add_comment_dlg]
    $bb1 add -text "Edit Line..." -command [namespace code edit_line]
    $bb1 add -text "Delete Line" -command [namespace code delete_line]
    set bb2 [ButtonBox $opts_frame.bb2 -homogeneous 1 -spacing 20]
    $bb2 add -text "Clear All" -command [namespace code clear_assert_file]
    $bb2 add -text "Export Assertions..." -command [namespace code export_assert_file]
    $bb2 add -text "Import Assertions..." -command [namespace code import_assert_file]
    grid $bb1 -sticky {}
    grid $bb2 -sticky {}
    grid rowconfigure $opts_frame 0 -weight 1
    grid rowconfigure $opts_frame 1 -weight 0 -pad 20
    grid columnconfigure $opts_frame 0 -weight 1
    variable line_editing_buttons [list $bb1 2 3]
    sync_asserts_to_text
}


##########################################################################
##########################################################################
## The rest of these procs are not interface procedures, but rather
## internal functions to this analysis.
##########################################################################
##########################################################################

# Called whenever the user clicks on the assertion file text field.
# Select the line clicked and enable the line editing buttons.  If the
# click was on an already selected line, deselect it and disable the
# buttons.
proc Apol_Analysis_flowassert::assertfile_click {widget x y} {
    variable asserts
    set newline [lindex [split [$widget index @$x,$y] .] 0]
    set selection [lindex [$widget tag ranges assert_file_sel_tag] 0]
    set oldline [lindex [split [lindex $selection 0] .] 0]
    # remove old selection
    if {$oldline != ""} {
        $widget tag remove assert_file_sel_tag $oldline.0 [expr {$oldline + 1}].0
    }
    if {$newline >= 1 && $newline <= [llength $asserts] && $newline != $oldline} {
        # add a selection
        $widget tag add assert_file_sel_tag $newline.0 [expr {$newline + 1}].0
    }
    enable_line_editing
}

# Enable/disable the line editing buttons (edit line and delete line),
# but only if something is selected.
proc Apol_Analysis_flowassert::enable_line_editing {} {
    variable assertfile_t    
    if {[$assertfile_t tag ranges assert_file_sel_tag] != ""} {
        set newstate normal
    } else {
        set newstate disabled
    }
    variable line_editing_buttons
    set parent [lindex $line_editing_buttons 0]
    foreach button [lrange $line_editing_buttons 1 end] {
        $parent itemconfigure $button -state $newstate
    }
}

# Deletes the contents of the assertion file text box, and thus
# consequently clears the file.
proc Apol_Analysis_flowassert::clear_assert_file {} {
    variable asserts ""
    sync_asserts_to_text
}

# Prompts the user for a file name, then saves the contents of the
# assertion buffer to it.
proc Apol_Analysis_flowassert::export_assert_file {} {
    variable assertfile_t
    variable last_filename
    variable last_pathname
    set filename [tk_getSaveFile -title "Export Assertion File" \
                      -parent $assertfile_t -initialdir $last_pathname \
                      -initialfile $last_filename]
    if {$filename == ""} {
        return
    }
    if [catch {::open $filename w} f] {
        tk_messageBox -icon error -type ok -title "Export Assertion File" \
            -message "Error saving to $filename" -parent $assertfile_t
        return
    }
    puts -nonewline $f [$assertfile_t get 1.0 end]
    ::close $f
    set last_filename [file tail $filename]
    set last_pathname [file dirname $filename]
}

# Prompts the user for a file name, then loads the contents of the
# file into the assertion buffer.
proc Apol_Analysis_flowassert::import_assert_file {} {
    variable assertfile_t
    variable last_filename
    variable last_pathname
    set filename [tk_getOpenFile -title "Import Assertion File" \
                      -parent $assertfile_t -initialdir $last_pathname \
                      -initialfile $last_filename]
    if {$filename == ""} {
        return
    }
    if [catch {::open $filename r} f] {
        tk_messageBox -icon error -type ok -title "Import Assertion File" \
            -message "Error loading from $filename" -parent $assertfile_t
        return
    }
    variable asserts ""
    foreach line [split [read $f] \n] {
        if {[string index [string trimleft $line] 0] == "\#"} {
            lappend asserts $line
        } else {
            foreach a [split $line ";"] {
                if {$a != ""} {
                    lappend asserts $a
                }
            }
        }
    }
    ::close $f
    set last_filename [file tail $filename]
    set last_pathname [file dirname $filename]
    sync_asserts_to_text
}

# Creates the "assertion statement wizard" that allows a user to
# easily add assertions lines to the current file.  The wizard is a
# non-modal dialog that presents all available types + attributes /
# options.  It does not validate entries for syntactical correctness.
proc Apol_Analysis_flowassert::create_assert_wizard_dlg {{origline {}}} {
    variable assertfile_t
    variable assert_wizard_dlg
    
    if [winfo exists $assert_wizard_dlg] {
        raise $assert_wizard_dlg
        return
    }

    variable wiz
    array unset wiz
    variable wiz_var
    array unset wiz_var

    # create a modal dialog to hold the wizard
    destroy .assertfile_wizard_dlg
    set assert_wizard_dlg [Dialog .assertfile_wizard_dlg -homogeneous 1 \
                               -spacing 20 -anchor c -cancel 2 -modal global \
                               -parent $assertfile_t -separator 0 \
                               -side bottom -title "Add Assertion Statements"]
    set f [$assert_wizard_dlg getframe]
    set topf [frame $f.topf]

    set start_tf [TitleFrame $topf.start_tf -text "Starting Types"]
    create_type_panel [$start_tf getframe] start
    set end_tf [TitleFrame $topf.end_tf -text "Ending Types"]
    create_type_panel [$end_tf getframe] to
    set wiz(via_tf) [TitleFrame $topf.via_tf -text "Exceptions Type"]
    create_type_panel [$wiz(via_tf) getframe] via
    
    set f0 [frame $topf.f0]
    set mode_tf [TitleFrame $f0.tf -text "Assertion Mode"]
    set noflow_rb [radiobutton [$mode_tf getframe].noflow_rb \
                  -text "noflow" -value "noflow" \
                  -variable Apol_Analysis_flowassert::wiz_var(mode) \
                  -command [namespace code [list set_mode "noflow"]]]
    set mustflow_rb [radiobutton [$mode_tf getframe].mustflow_rb \
                  -text "mustflow" -value "mustflow" \
                  -variable Apol_Analysis_flowassert::wiz_var(mode) \
                  -command [namespace code [list set_mode "mustflow"]]]
    set onlyflow_rb [radiobutton [$mode_tf getframe].onlyflow_rb \
                  -text "onlyflow" -value "onlyflow" \
                  -variable Apol_Analysis_flowassert::wiz_var(mode) \
                  -command [namespace code [list set_mode "onlyflow"]]]
    pack $noflow_rb $mustflow_rb $onlyflow_rb -anchor w -side top
    set lf [LabelFrame $f0.lf -text "Minimum Weight: " -side left]
    set weight_sb [SpinBox [$lf getframe].weight_sb -font $ApolTop::text_font \
                    -width 3 -entrybg white -range [list 1 10 1] \
                    -editable 0 -justify right \
                    -textvariable Apol_Analysis_flowassert::wiz_var(weight) \
                    -modifycmd [namespace code sync_wiz_lbs_to_line]]
    pack $weight_sb -expand 1 -fill both
    pack $mode_tf -expand 1 -fill both -pady 20
    pack $lf -expand 1 -fill x -pady 20
    pack $f0 -expand 0 -fill none -side left -padx 10
    pack $start_tf $end_tf $wiz(via_tf) -expand 1 -fill both -side left
    pack $topf -side top -expand 1 -fill both
    
    set rule_e [LabelEntry $f.rule_e -width 80 \
                   -font $ApolTop::text_font -entrybg white \
                    -label "Assertion Line: " -editable 0 \
                    -textvariable Apol_Analysis_flowassert::wiz_var(line)]
    pack $rule_e -side top -expand 0 -fill x -padx 20 -pady 20 -anchor center

    $assert_wizard_dlg add -text "Reset Line" \
        -command [namespace code [list reset_wizard $origline]]
    if {$origline == ""} { 
        $assert_wizard_dlg add -text "Add Line" \
            -command [namespace code add_assertion]
    } else {
        $assert_wizard_dlg add -text "Edit Line" \
            -command [namespace code add_assertion]
    }
    $assert_wizard_dlg add -text "Cancel"
    
    populate_lists 1
    reset_wizard $origline

    set result [$assert_wizard_dlg draw]
    destroy $assert_wizard_dlg
    if {$result == 1} {
        # user clicked on add line
        return 1
    } else {
        # user canceled dialog
        return 0
    }
}

# Creates an individual panel {starting, ending, via/exceptions} for
# the assertion wizard.
proc Apol_Analysis_flowassert::create_type_panel {type_panel type_name} {
    variable wiz
    variable wiz_var
    
    # add the type selection radiobutton + ComboBox
    set type_star_rb [radiobutton $type_panel.star_cb -value "*" \
                          -text "Any Type" \
                          -variable Apol_Analysis_flowassert::wiz_var($type_name,type,rb)]
    pack $type_star_rb -anchor w -side top
    set type_names_f [frame $type_panel.type_names_f]
    set type_names_l [label $type_names_f.names_l -text "Type Names:"]
    set type_names_cb [ComboBox $type_names_f.names_cb -width 24 \
                           -editable 1 -entrybg white -exportselection 0 \
                           -textvariable Apol_Analysis_flowassert::wiz_var($type_name,type,name)]
    bindtags $type_names_cb.e [linsert [bindtags $type_names_cb.e] 3 ${type_name}_names_tags]
    bind ${type_name}_names_tags <KeyPress> [list ApolTop::_create_popup $type_names_cb %W %K]
    pack $type_names_l -expand 0 -side top
    pack $type_names_cb -expand 0 -fill both -side top
    pack $type_names_f -expand 0 -fill none -side top -pady 2

    # add bindings between these so that modifying one clears the other
    set bindcmd [namespace code [list set_type $type_star_rb $type_names_cb $type_name star ""]]
    $type_star_rb configure -command $bindcmd
    set bindcmd [namespace code [list set_type $type_star_rb $type_names_cb $type_name name %P]]
    $type_names_cb configure -modifycmd $bindcmd -vcmd $bindcmd -validate key

#    pack [Separator $type_panel.strut0 -orient horizontal] \
#        -expand 0 -fill x -side top -pady 10

    # add the object selection listbox + Entry
#    pack [label $type_panel.objs_l -text "Object Classes:"] -expand 0
    set objs_sw [ScrolledWindow $type_panel.objs_sw -auto horizontal]
    set wiz($type_name,objs_lb) [listbox [$objs_sw getframe].objs_lb -selectmode multiple \
                     -height 5 -highlightthickness 0 -exportselection 0 \
                     -setgrid 0 -background white \
                     -listvariable Apol_Analysis_flowassert::assert_wizard_objclasses]
    $objs_sw setwidget $wiz($type_name,objs_lb)
#    pack $objs_sw -expand 0 -fill both
    set objs_e [Entry $type_panel.objs_e -background white -validate key \
                    -textvariable Apol_Analysis_flowassert::wiz_var($type_name,class)]
    $objs_e configure -vcmd [namespace code [list select_class $type_name $objs_e %P]]
#    pack $objs_e -expand 0 -fill x -pady 2
    bind $wiz($type_name,objs_lb) <<ListboxSelect>> [namespace code [list select_class $type_name $wiz($type_name,objs_lb) ""]]
    
#    pack [Separator $type_panel.strut1 -orient horizontal] \
# -expand 0 -fill x -side top -pady 10

    # add the include / exclude radio buttons
    set include_rb [radiobutton $type_panel.include_rb -value "include" \
                        -text "Include Type/Object" \
                        -variable Apol_Analysis_flowassert::wiz_var($type_name,include)]
    set exclude_rb [radiobutton $type_panel.exclude_rb -value "exclude" \
                        -text "Exclude Type/Object" \
                        -variable Apol_Analysis_flowassert::wiz_var($type_name,include)]
    pack $include_rb $exclude_rb -anchor w -side top

    set add_replace_f [frame $type_panel.add_replace_f]
    set add_b [button $add_replace_f.add_b -text "Add"]
    set wiz($type_name,replace_b) [button $add_replace_f.replace_b -text "Replace"]
    grid $add_b $wiz($type_name,replace_b) -padx 5 -sticky ew
    grid columnconfigure $add_replace_f 0 -weight 1 -uniform 1
    grid columnconfigure $add_replace_f 1 -weight 1 -uniform 1
    pack $add_replace_f -expand 0 -fill x -pady 5 -side top
    
    pack [Separator $type_panel.strut2 -orient horizontal] \
        -expand 0 -fill x -side top -pady 10

    set id_list_sw [ScrolledWindow $type_panel.id_list_sw -auto horizontal]
    set id_list_lb [listbox [$id_list_sw getframe].id_list_lb \
                        -selectmode browse -height 4 -exportselection 0 \
                        -listvariable Apol_Analysis_flowassert::wiz_var($type_name) \
                        -background white]
    set wiz($type_name,id_lb) $id_list_lb
    $id_list_sw setwidget $id_list_lb
    pack $id_list_sw -side top -expand 1 -fill both
    set wiz($type_name,remove_b) [button $type_panel.remove_b -text "Remove"]
    $wiz($type_name,remove_b) configure -command [namespace code [list remove_type $id_list_lb $type_name]]
    pack $wiz($type_name,remove_b) -expand 0 -pady 5 -side top

    $add_b configure -command [namespace code [list add_replace_type $id_list_lb $type_name 1]]
    $wiz($type_name,replace_b) configure -command [namespace code [list add_replace_type $id_list_lb $type_name 0]]
    bind $id_list_lb <<ListboxSelect>> [namespace code [list select_id_item $id_list_lb $type_name]]
    bind $id_list_lb <Button-1> [namespace code [list id_list_lb_click %W $type_name %y]]
    select_id_item $id_list_lb $type_name
    
    # add the widgets' paths to a list, to be read by [populate_lists]
    lappend wiz(type_cbs) $type_names_cb
    lappend wiz(objclass_lbs) $wiz($type_name,objs_lb)
}

# Safely destroys a modal dialog.
proc Apol_Analysis_flowassert::destroy_wizard_dlg {dlg} {
    grab release $dlg
    destroy $dlg
}

# Called to populate the ComboBox/listbox holding the type names +
# attributes/object classes.  This is done in one of three places:
# upon wizard dialog creation, when opening a policy, and when closing
# a policy.
proc Apol_Analysis_flowassert::populate_lists {add_items} {
    variable wiz
    
    if $add_items {
        foreach type_cb $wiz(type_cbs) {
            $type_cb configure -values [lsort [concat $Apol_Types::typelist $Apol_Types::attriblist]]
        }
        foreach objclass_lb $wiz(objclass_lbs) {
            set var [$objclass_lb cget -listvariable]
            set $var $Apol_Class_Perms::class_list
        }
    } else {
        foreach type_cb $wiz(type_cbs) {
            $type_cb configure -values {}
        }
        foreach objclass_lb $wiz(objclass_lbs) {
            set var [$objclass_lb cget -listvariable]
            set $var {}
        }
    }
}

# Callback when a user clicks on a mode radiobutton {noflow, mustflow,
# onlyflow}.  Updates the title of the via_tf frame.  Updates the mode
# listed in the current assertion line.
proc Apol_Analysis_flowassert::set_mode {newmode} {
    variable wiz
    if {$newmode == "noflow"} {
        $wiz(via_tf) configure -text "Exception Types (optional)"
    } elseif {$newmode == "mustflow"} {
        $wiz(via_tf) configure -text "Via Types (optional)"
    } else {
        $wiz(via_tf) configure -text "Via Types (required)"
    }
    sync_wiz_lbs_to_line
}

# Callback whenever the user selects a type.  Disables the other
# widgets.  Sets the internal variable to be equal to the name of the
# type.
proc Apol_Analysis_flowassert::set_type {star_rb names_cb type_name which newvalue} {
    variable wiz_var
    switch -- $which {
        star {
            $star_rb select
            $names_cb configure -text ""
            set wiz_var($type_name,type) "*"
        }
        name {
            if {$newvalue != ""} {
                $star_rb deselect
                if {$newvalue == "%P"} {
                    set newvalue [$names_cb cget -text]
                }
                set wiz_var($type_name,type) $newvalue
            }
        }
    }
    return 1
}

# Callback whenever the user modifies object classes selection, either
# by clicking on an item in the listbox or manually typing in a new
# one.  If clicking on the listbox, update the entry by
# appending/removing classes.  If manually editing the entry,
# select/deslect items from the listbox.
proc Apol_Analysis_flowassert::select_class {type_name widget newvalue} {
    variable wiz
    variable wiz_var
    set objs_lb $wiz($type_name,objs_lb)
    set class_var wiz_var($type_name,class)
    set objs_list [$objs_lb get 0 end]
    if {[string range $widget end-2 end] == "_lb"} {
        # user selected/deselected something from the listbox.
        # synchronize the widgets giving precedence to the listbox.
        # check that all selected in the listbox is in class_var and
        # that all /not/ selected are not in class_var
        set selected_list [$objs_lb curselection]
        set new_class_val [set $class_var]
        for {set i 0} {$i < [llength $objs_list]} {incr i} {
            set item [lindex $objs_list $i]
            if {[lsearch -exact -sorted -integer $selected_list $i] >= 0} {
                # item selected; add it to the entry line if not already there
                if {[lsearch -exact $new_class_val $item] == -1} {
                    lappend new_class_val $item
                }
            } else {
                # item deselected; if in entry line remove it
                if {[set x [lsearch -exact $new_class_val $item]] >= 0} {
                    set new_class_val [lreplace $new_class_val $x $x]
                }                
            }
        }
        set $class_var $new_class_val
    } else {
        # entry widget changed.  synchronize the widgets giving
        # precedence to the entry box.  auto-select in the listbox
        # anything new.  deselect those that are not in class_var.
        $objs_lb selection clear 0 end
        foreach class $newvalue {
            if {[set index [lsearch -exact $objs_list $class]] >= 0} {
                $objs_lb selection set $index
                $objs_lb see $index
            }
        }
    }
    return 1
}

# Called when the user clicks on the 'add' button within the wizard.
# If an item is already selected in the listbox, replace it.
# Otherwise append a new one.  Either way deselect the listbox.
# Finally synchronize the label entry.
proc Apol_Analysis_flowassert::add_replace_type {id_lb type_name always_add} {
    variable wiz_var
    set class $wiz_var($type_name,class)
    if {$wiz_var($type_name,include) == "include"} {
        set sign ""
    } else {
        set sign "-"
    }
    set type $wiz_var($type_name,type)
    if {$class != ""} {
        if {[llength $class] > 1} {
            set class ":\{$class\}"
        } else {
            set class ":$class"
        }
    }
    set type_id "$sign$type$class"
    if {$always_add || [set selected [$id_lb curselection]] == {}} {
        lappend wiz_var($type_name) $type_id
    } else {
        set selected [lindex $selected 0]
        set wiz_var($type_name) [lreplace $wiz_var($type_name) $selected $selected $type_id]
    }
    $id_lb selection clear 0 end
    select_id_item $id_lb $type_name
    sync_wiz_lbs_to_line
    reset_type $type_name
}

# Synchronize the currently editing line with the contents of the three
# listboxes.
proc Apol_Analysis_flowassert::sync_wiz_lbs_to_line {} {
    variable wiz_var
    foreach var {mode start to via weight} {
        set $var $wiz_var($var)
    }
    set wiz_var(line) [render_assertion [list $mode $start $to $via $weight]]
}

# Called whenever the selection changes in the assert id listbox.
proc Apol_Analysis_flowassert::select_id_item {id_lb type_name} {
    variable wiz
    variable wiz_var
    if {[set selected [$id_lb curselection]] != {}} {
        $wiz($type_name,remove_b) configure -state normal
        $wiz($type_name,replace_b) configure -state normal
        # fill in the other fields given the currently selected one
        set type_id [lindex $wiz_var($type_name) [lindex $selected 0]]
        foreach {type class} [split $type_id ":"] {}
        set wiz_var($type_name,type) $type
        if {$type == "*"} {
            set wiz_var($type_name,type,name) ""
            set wiz_var($type_name,type,rb) "*"
        } else {
            set wiz_var($type_name,type,rb) ""
            set wiz_var($type_name,type,name) $type
        }
        set wiz_var($type_name,class) $class
    } else {
        $wiz($type_name,replace_b) configure -state disabled
        $wiz($type_name,remove_b) configure -state disabled
    }
}

# Remove the currently selected item from the assert id listbox.
proc Apol_Analysis_flowassert::remove_type {id_lb type_name} {
    if {[set selected [$id_lb curselection]] == {}} {
        return
    }
    set selected [lindex $selected 0]
    variable wiz_var
    set wiz_var($type_name) [lreplace $wiz_var($type_name) $selected $selected]
    $id_lb selection clear 0 end
    select_id_item $id_lb $type_name
    reset_type $type_name
    sync_wiz_lbs_to_line
}

# Called whenever the user clicks on the assert id lisb box.
proc Apol_Analysis_flowassert::id_list_lb_click {id_lb type_name y} {
    if {[set clicked [$id_lb index @1,$y]] != -1} {
        if {[$id_lb selection includes $clicked]} {
            # deselect the item
            $id_lb selection clear $clicked
            select_id_item $id_lb $type_name
            return -code break
        }
        select_id_item $id_lb $type_name
    }
}

# Takes the current assertion line and adds it to the current
# assertion file.  Then closes the assertion dialog
proc Apol_Analysis_flowassert::add_assertion {} {
    variable assertfile_t
    # add the line if and only if the widget is still visible (which
    # might not be the case if the user has clicked on a different
    # analysis module since this wizard was created)
    if [winfo exists $assertfile_t] {
        variable wiz_var
        foreach var {mode start to via weight} {
            set $var $wiz_var($var)
        }
        add_line [list $mode $start $to $via $weight]
        variable assert_wizard_dlg
        $assert_wizard_dlg enddialog 1
    }
}

# Resets all widgets in the assertion wizard to their original
# positions.  Clears the assertion line and the list boxes
proc Apol_Analysis_flowassert::reset_wizard {origline} {
    variable wiz
    variable wiz_var
    if {$origline == ""} {
        set mode "noflow"
        set start {}
        set to {}
        set via {}
        set weight 1
    } else {
        foreach {mode start to via weight} $origline {}
    }
    foreach var {mode start to via weight} {
        set wiz_var($var) [set $var]
    }
    foreach type_name {start to via} {
        reset_type $type_name
        $wiz($type_name,id_lb) selection set 0
        select_id_item $wiz($type_name,id_lb) $type_name
    }
    set_mode $mode
}

# Clears the values within a single wizard type panel.
proc Apol_Analysis_flowassert::reset_type {type_name} {
    variable wiz_var
    set wiz_var($type_name,type) "*"
    set wiz_var($type_name,class) ""
    set wiz_var($type_name,include) "include"
    # do these in reverse order so that the radiobutton change event
    # fires last
    set wiz_var($type_name,type,name) ""
    set wiz_var($type_name,type,rb) "*"
}

# Whenever the user clicks on the assertion "line xxx" hyperlink from
# the results textbox, highlight the line within the assertion file
# textbox.  Highlighting is done by changing the selection.  This
# assumes that the assertion file contents have not changed since
# executing the file.
proc Apol_Analysis_flowassert::highlight_assert_line {widget x y} {
    set line [eval $widget get [$widget tag prevrange assert_file_tag "@$x,$y + 1 char"]]
    foreach {foo linenum} [split $line] {}
    variable assertfile_t
    $assertfile_t tag remove assert_file_sel_tag 1.0 end
    $assertfile_t tag add assert_file_sel_tag $linenum.0 [expr {$linenum + 1}].0
    $assertfile_t see $linenum.0
    enable_line_editing
}

# Given an assertion (either a comment or a 4-tuple/5-tuple) return a
# string that represents the line.
proc Apol_Analysis_flowassert::render_assertion {assertion} {
    if {[string index [string trimleft $assertion] 0] == "\#"} {
        return $assertion
    } else {
        foreach {mode start to via weight} $assertion {}
        if {[llength $start] == 0} {
            set start "*"
        }
        if {$to == {}} {
            set to "*"
        }
        if {$via == {}} {
            return "[list $mode $start $to $weight];"
        } else {
            return "[list $mode $start $to $via $weight];"
        }
    }
}

# Resynchronize the assertions (model) to its view (assertfile_t text
# field).
proc Apol_Analysis_flowassert::sync_asserts_to_text {} {
    variable asserts
    variable assertfile_t
    $assertfile_t configure -state normal
    $assertfile_t delete 1.0 end
    foreach line $asserts {
        $assertfile_t insert end "[render_assertion $line]\n"
    }
    $assertfile_t tag add assert_file_t_tag 1.0 end
    $assertfile_t configure -state disabled
    enable_line_editing
}

# Displays a dialog box to allow user to add a comment to the
{'fraction_non_alphanumeric': 0.09386508695823122, 'fraction_numerical': 0.0055438329772873225, 'mean_word_length': 2.5773829059027613, 'pattern_counts': {'":': 3, '<': 38, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3070197', 'n_tokens_mistral': 22831, 'n_tokens_neox': 21977, 'n_words': 7580}