/* Agent Tcl
   Bob Gray
   17 April 1996

   tclSecureCmd.cc

   This file implements the Tcl commands that handle security.

   Copyright (c) 1995-1996, Bob Gray, Dartmouth College

   See the file "agent.terms" for information on usage and redistribution
   of this file and for a DISCLAIMER OF ALL WARRANTIES.
*/

#include "platPorting.h"
#include "tcl.h"
#include "agentTcl.h"
#include "agentTclInt.h"

/* Agent_SecurityCmd

   Tcl syntax: security [ encryption | signatures | all ] [ on | off ]

      Purpose: This procedure implements the "security" Tcl command.

        Input: dummy  = unused client data
		        (ClientData)

	       interp = the Tcl interpreter
			(struct Tcl_Interp *)

	       argc   = the number of command arguments
			(int)

	       argv   = the command arguments
			(char **)

       Output: The procedure returns TCL_ERROR and sets the intepreter
	       result to an appropriate error message on error.  Otherwise
	       the procedure returns TCL_OK and sets the interpreter result
	       to the empty string.
*/

int Agent_SecurityCmd (ClientData, Tcl_Interp *interp, int argc, char **argv)
{
    BOOLEAN encryption = e_FALSE;
    BOOLEAN signatures = e_FALSE;
    int code = TCL_OK;
    BOOLEAN state;

	/* assertions on the parameters */

    assert (interp != NULL);
    assert (argv != NULL);

	/* check the number of command arguments */

    if (argc != 3) {
	Tcl_AppendResult (interp, "wrong # of args: should be \"", argv[0], " [ encryption | signatures ] [ on | off ]\"", (char *) NULL);
	Tcl_SetErrorCode (interp, "SYNTAX", (char *) NULL);
	return TCL_ERROR;
    }

	/* determine if we are working with encryption or signatures */

    if (!strcmp (argv[1], "encryption")) {
	encryption = e_TRUE;
    } else if (!strcmp (argv[1], "signatures")) {
        signatures = e_TRUE;
    } else if (!strcmp (argv[1], "all")) {
	encryption = e_TRUE;
	signatures = e_TRUE;
    } else {
	Tcl_AppendResult (interp, "unknown option \"", argv[1], "\": should be \"encryption\", \"signatures\" or \"all\"", (char *) NULL);
	Tcl_SetErrorCode (interp, "SYNTAX", (char *) NULL);
	return TCL_ERROR;
    }

	/* determine if we are turning on or off */

    if (!strcmp (argv[2], "on")) {
	state = e_TRUE;
    } else if (!strcmp (argv[2], "off")) {
	state = e_FALSE;
    } else {
	Tcl_AppendResult (interp, "unknown state \"", argv[2], "\": should be \"on\" or \"off\"", (char *) NULL);
	Tcl_SetErrorCode (interp, "SYNTAX", (char *) NULL);
	return TCL_ERROR;
    }

	/* turn on or off encryption or signatures */

    if (encryption == e_TRUE) {
	code = Agent_SetEncryption (interp, state);
    }

    if (code == TCL_OK) {
	if (signatures == e_TRUE) {
	    code = Agent_SetSignatures (interp, state);
	}
    } 	
   
    return (code); 
}
