
/**
 * @file driver.c
 *
 * Hlavni soubor ovladace pproc.sys. Zajistuje inicializaci a uklid po vsech
 * jeho soucastech. Dale se stara o komunikaci s okolnim svetem (zvlaste
 * s aplikacemi bezicimi v uzivatelskem rezimu).
 */

#include <ntifs.h>
#include "ioctls.h"
#include "version.h"
#include "pprocess.h"
#include "driver.h"


/** Obsluha pozadavku IRP_MJ_CREATE a IRP_MJ_CLOSE. Vzdy skonci uspechem.
 *
 */
NTSTATUS DriverCreateClose(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
  NTSTATUS Status = STATUS_UNSUCCESSFUL;
  KdPrint(("driver.c: DriverCreateClose(DeviceObject=0x%p; Irp=0x%p)\n", DeviceObject, Irp));

  Status = STATUS_SUCCESS;
  Irp->IoStatus.Status = Status;
  IoCompleteRequest(Irp, IO_NO_INCREMENT);

  KdPrint(("driver.c: DriverCreateClose(-):0x%x\n", Status));
  return Status;
}


/** Obsluha pozadavku IRP_MJ_DEVICE_CONTROL.
 *
 *  Ovladac obsluhuje dva pozadavky:
 *  + IOCTL_PPROC_PROTECT_PROCESS - nastavi cilovy proces jako chraneny
 *  + IOCTL_PPROC_UNPROTECT_PROCESS - nastavi cilovy chraneny proces jako
 *    normalni proces.
 */
NTSTATUS DriverDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
  PIO_STACK_LOCATION IrpStack = NULL;
  PVOID OutBuffer = NULL;
  ULONG OutBufferLength = 0;
  PVOID InBuffer = NULL;
  ULONG InBufferLength = 0;
  ULONG ControlCode = 0;
  HANDLE ProcessId = 0;
  NTSTATUS Status = STATUS_UNSUCCESSFUL;
  KdPrint(("driver.c: DriverDeviceControl(DeviceObject=0x%p; Irp=0x%p)\n", DeviceObject, Irp));

  IrpStack = IoGetCurrentIrpStackLocation(Irp);
  InBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
  InBuffer = Irp->AssociatedIrp.SystemBuffer;
  OutBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
  OutBuffer = Irp->AssociatedIrp.SystemBuffer;
  ControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
  Irp->IoStatus.Information = 0;

  switch (ControlCode) {
    case IOCTL_PPROC_PROTECT_PROCESS:
      if (InBufferLength >= sizeof(HANDLE)) {
        ProcessId = *(PHANDLE)InBuffer;
        Status = ProcessProtect(ProcessId);
      } else Status = STATUS_BUFFER_TOO_SMALL;
      break;

    case IOCTL_PPROC_UNPROTECT_PROCESS:
      if (InBufferLength >= sizeof(HANDLE)) {
        ProcessId = *(PHANDLE)InBuffer;
        Status = ProcessUnprotect(ProcessId);
      } else Status = STATUS_BUFFER_TOO_SMALL;
      break;

    case IOCTL_PPROC_IS_PROTECTED:
      if (InBufferLength >= sizeof(HANDLE) && OutBufferLength >= sizeof(BOOLEAN)) {
        ProcessId = *(PHANDLE)InBuffer;
        Status = ProcessIsProtected(ProcessId, (PBOOLEAN)OutBuffer);
        if (NT_SUCCESS(Status))
          Irp->IoStatus.Information = sizeof(BOOLEAN);
      } else Status = STATUS_BUFFER_TOO_SMALL;
      break;

    default:
      KdPrint(("driver.c: DriverDeviceControl(): Invalid device control request\n"));
      Status = STATUS_NOT_IMPLEMENTED;
      break;
  }  

  Irp->IoStatus.Status = Status;
  IoCompleteRequest (Irp, IO_NO_INCREMENT);

  KdPrint(("driver.c: DriverDeviceControl(-):0x%x\n", Status));
  return Status;
}


/** Zajistuje odstraneni komunikacniho zarizeni a jeho symbolickeho odkazu.
 *
 *  @param DriverObject Adresa struktury DRIVER_OBJECT reprezentujici
 *  ovladac.
 */
VOID DriverFinit(IN PDRIVER_OBJECT DriverObject)
{
  UNICODE_STRING uSymLink;
  KdPrint(("driver.c: DriverFinit(Driverobject=0x%p)\n", DriverObject));

  RtlInitUnicodeString(&uSymLink, DRIVER_SYMLINK);
  IoDeleteSymbolicLink(&uSymLink);
  IoDeleteDevice(DriverObject->DeviceObject);

  KdPrint(("driver.c: DriverFinit(-)\n"));
  return;
}


/** Tuto rutinu vola system tesne pred uvolnenim ovladace z pameti. Rutina
 *  zajisti uklid po vsech soucastech ovladace.
 */
VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
{
  KdPrint(("driver.c: DriverUnload(DriverObject=0x%p)\n", DriverObject));

  DriverFinit(DriverObject);
  PProcessFinit();
  VersionFinit();

  KdPrint(("driver.c: DriverUnload(-)\n"));
  return;
}


/** Inicializuje objekt komunikacniho zarizeni a vytvori k nemu symbolicky
 *  odkaz.
 *
 *  @param DriverObject Adresa struktury DRIVER_OBJECT reprezentujici ovladac.
 *
 *  @return Funkce vraci hodnotu NTSTATUS indikujici uspech ci neuspech
 *  operace.
 */
NTSTATUS DriverInit(IN PDRIVER_OBJECT DriverObject)
{
  UNICODE_STRING uDevice;
  UNICODE_STRING uSymLink;
  NTSTATUS Status = STATUS_UNSUCCESSFUL;
  KdPrint(("driver.c: DriverInit(DriverObject=0x%p)\n", DriverObject));

  RtlInitUnicodeString(&uDevice, DRIVER_DEVICE);
  Status = IoCreateDevice(DriverObject, 0, &uDevice, FILE_DEVICE_UNKNOWN, 0, FALSE, &DriverObject->DeviceObject);
  if (NT_SUCCESS(Status)) {
    RtlInitUnicodeString(&uSymLink, DRIVER_SYMLINK);
    Status = IoCreateSymbolicLink(&uSymLink, &uDevice);
    if (NT_SUCCESS(Status)) {
      DriverObject->DriverUnload = DriverUnload;
      DriverObject->MajorFunction[IRP_MJ_CREATE] = DriverCreateClose;
      DriverObject->MajorFunction[IRP_MJ_CLOSE] = DriverCreateClose;
      DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverDeviceControl;
      if (!NT_SUCCESS(Status)) {
        IoDeleteSymbolicLink(&uSymLink);
        IoDeleteDevice(DriverObject->DeviceObject);
      }
    } else IoDeleteDevice(DriverObject->DeviceObject);
  }

  KdPrint(("driver.c: DriverInit(-):0x%x\n", Status));
  return Status;
}


/** Tuto rutinu jadro vola tesne po namapovani ovladace do sveho pametoveho
 *  prostoru. Rutina provede inicializaci vsech soucasti ovladace.
 */
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
  NTSTATUS Status = STATUS_UNSUCCESSFUL;
  KdPrint(("driver.c: DriverEntry(DriverObject=0x%p; RegistryPath=\"%S\")\n", DriverObject, RegistryPath->Buffer));

  Status = VersionInit();
  if (NT_SUCCESS(Status)) {
    Status = PProcessInit();
    if (NT_SUCCESS(Status)) {
      Status = DriverInit(DriverObject);
      if (!NT_SUCCESS(Status)) {
        PProcessFinit();
        VersionFinit();
      }
    } else VersionFinit();
  }

  KdPrint(("driver.c: DriverEntry(-):0x%x\n", Status));
  return Status;
}
