// IfsTelnetAdmin.bcpl -- privileged IFS server telnet commands
// Copyright Xerox Corporation 1979, 1981

// Last modified November 25, 1981  2:57 PM by Taft

get "Ifs.decl"
get "IfsRs.decl"
get "IfsTelnet.decl"
get "IfsFiles.decl"
get "IfsDirs.decl"

external
[
// outgoing procedures
ExecEnable; ExecDisable; ExecCreate; ExecDestroy; ExecHalt

// incoming procedures
ExecChangeDirectory; GetString; Confirm; TelnetCommandQuit
EnableCatch; EndCatch;
EnableTimeServ; EnableNameServ; EnableBootServ; EnablePress
GetDIF; ReadDIF; DestroyUser; Password
Wss; Ws; Puts; Closes; Resets; Errors; PutTemplate; IFSPrintError;
SysAllocateZero; SysFree; MoveBlock; SetBlock; FreePointer

// incoming statics
CtxRunning; dsp; loginKT; enableKT; entFlag; haltFlag;
]

//---------------------------------------------------------------------------
let ExecEnable(cs) be
//---------------------------------------------------------------------------
[
if CtxRunning>>TCtx.capabilities<<Capabilities.wheel eq 0 then Errors(cs, 0)
CtxRunning>>TCtx.userInfo>>UserInfo.capabilities.wheel = true
CtxRunning>>TCtx.topKT = enableKT
TelnetCommandQuit(cs)  //force prompt string to be recomputed
]

//---------------------------------------------------------------------------
and ExecDisable(cs) be
//---------------------------------------------------------------------------
[
CtxRunning>>TCtx.userInfo>>UserInfo.capabilities.wheel = false
CtxRunning>>TCtx.topKT = loginKT
TelnetCommandQuit(cs)  //force prompt string to be recomputed
]

//---------------------------------------------------------------------------
and ExecCreate(cs) be
//---------------------------------------------------------------------------
// Create <directory> <password> <subcommands>
[
Wss(cs, " (directory) ")
let name, dif = 0, 0
if EnableCatch(cs) then
   [
   FreePointer(lv name, lv dif)
   EndCatch(cs)
   ]

name = GetString(cs, 0, Wss, "directory name")
let difName = nil
dif = GetDIF(name, true, lv difName)  // accept only real DIF
let new = dif eq 0
test new
   ifso
      [
      Wss(cs, " [New] (password) ")
      dif = ReadDIF("Default-User")
      if dif eq 0 then dif = SysAllocateZero(lenDIF)
      let password = GetString(cs, 0, Wss, "password")
      test password>>String.length eq 0
         ifso SetBlock(lv dif>>DIF.password, -1, size DIF.password/16)
         ifnot Password(password, lv dif>>DIF.password, true)
      SysFree(password)
      ]
   ifnot
      [
      Wss(cs, " [Old]")
      SysFree(name)
      name = difName
      ]

// Accept "Change Directory-Parameters" subcommands
ExecChangeDirectory(cs, name, dif, new)

FreePointer(lv name, lv dif)
]

//---------------------------------------------------------------------------
and ExecDestroy(cs) be
//---------------------------------------------------------------------------
[
Wss(cs, " (directory) ")
let name = 0
if EnableCatch(cs) then [ FreePointer(lv name); EndCatch(cs) ]
name = GetString(cs, 0, Wss, "directory name")
if Confirm(cs) then
   [
   let ec = DestroyUser(name)
   if ec ne 0 then
      [ Ws("*nFailed: "); IFSPrintError(dsp, ec) ]
   ]
FreePointer(lv name)
]


//---------------------------------------------------------------------------
and ExecHalt(cs) be
//---------------------------------------------------------------------------
[
Wss(cs, " (the system) ")
if Confirm(cs) then
   [
   entFlag = false
   haltFlag = true  //notify Reaper
   EnableTimeServ(false)  // turn off misc servers
   EnableNameServ(false)
   EnableBootServ(false)
   EnablePress(false)
   ]
Ws("*n[You must log out before the system will halt.]")
]