/*
 * Python object definition of the libfwnt access control entry
 *
 * Copyright (C) 2009-2016, Joachim Metz <joachim.metz@gmail.com>
 *
 * Refer to AUTHORS for acknowledgements.
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <common.h>
#include <types.h>

#if defined( HAVE_STDLIB_H )
#include <stdlib.h>
#endif

#include "pyfwnt_access_control_entry.h"
#include "pyfwnt_error.h"
#include "pyfwnt_integer.h"
#include "pyfwnt_libcerror.h"
#include "pyfwnt_libcstring.h"
#include "pyfwnt_libfwnt.h"
#include "pyfwnt_python.h"
#include "pyfwnt_security_identifier.h"
#include "pyfwnt_unused.h"

PyMethodDef pyfwnt_access_control_entry_object_methods[] = {

	/* Functions to access the access control entry */

	{ "get_type",
	  (PyCFunction) pyfwnt_access_control_entry_get_type,
	  METH_NOARGS,
	  "get_type() -> Integer\n"
	  "\n"
	  "Retrieves the type." },

	{ "get_flags",
	  (PyCFunction) pyfwnt_access_control_entry_get_flags,
	  METH_NOARGS,
	  "get_flags() -> Integer\n"
	  "\n"
	  "Retrieves the flags." },

	{ "get_access_mask",
	  (PyCFunction) pyfwnt_access_control_entry_get_access_mask,
	  METH_NOARGS,
	  "get_access_mask() -> Integer\n"
	  "\n"
	  "Retrieves the access mask." },

	{ "get_security_identifier",
	  (PyCFunction) pyfwnt_access_control_entry_get_security_identifier,
	  METH_NOARGS,
	  "get_security_identifier() -> Object or None\n"
	  "\n"
	  "Retrieves the security identifier." },

	/* Sentinel */
	{ NULL, NULL, 0, NULL }
};

PyGetSetDef pyfwnt_access_control_entry_object_get_set_definitions[] = {

	{ "type",
	  (getter) pyfwnt_access_control_entry_get_type,
	  (setter) 0,
	  "The access mask.",
	  NULL },

	{ "flags",
	  (getter) pyfwnt_access_control_entry_get_flags,
	  (setter) 0,
	  "The access mask.",
	  NULL },

	{ "access_mask",
	  (getter) pyfwnt_access_control_entry_get_access_mask,
	  (setter) 0,
	  "The access mask.",
	  NULL },

	{ "security_identifier",
	  (getter) pyfwnt_access_control_entry_get_security_identifier,
	  (setter) 0,
	  "The security identifier.",
	  NULL },

	/* Sentinel */
	{ NULL, NULL, NULL, NULL, NULL }
};

PyTypeObject pyfwnt_access_control_entry_type_object = {
	PyVarObject_HEAD_INIT( NULL, 0 )

	/* tp_name */
	"pyfwnt.access_control_entry",
	/* tp_basicsize */
	sizeof( pyfwnt_access_control_entry_t ),
	/* tp_itemsize */
	0,
	/* tp_dealloc */
	(destructor) pyfwnt_access_control_entry_free,
	/* tp_print */
	0,
	/* tp_getattr */
	0,
	/* tp_setattr */
	0,
	/* tp_compare */
	0,
	/* tp_repr */
	0,
	/* tp_as_number */
	0,
	/* tp_as_sequence */
	0,
	/* tp_as_mapping */
	0,
	/* tp_hash */
	0,
	/* tp_call */
	0,
	/* tp_str */
	0,
	/* tp_getattro */
	0,
	/* tp_setattro */
	0,
	/* tp_as_buffer */
	0,
	/* tp_flags */
	Py_TPFLAGS_DEFAULT,
	/* tp_doc */
	"pyfwnt access control entry object (wraps libfwnt_access_control_entry_t)",
	/* tp_traverse */
	0,
	/* tp_clear */
	0,
	/* tp_richcompare */
	0,
	/* tp_weaklistoffset */
	0,
	/* tp_iter */
	0,
	/* tp_iternext */
	0,
	/* tp_methods */
	pyfwnt_access_control_entry_object_methods,
	/* tp_members */
	0,
	/* tp_getset */
	pyfwnt_access_control_entry_object_get_set_definitions,
	/* tp_base */
	0,
	/* tp_dict */
	0,
	/* tp_descr_get */
	0,
	/* tp_descr_set */
	0,
	/* tp_dictoffset */
	0,
	/* tp_init */
	(initproc) pyfwnt_access_control_entry_init,
	/* tp_alloc */
	0,
	/* tp_new */
	0,
	/* tp_free */
	0,
	/* tp_is_gc */
	0,
	/* tp_bases */
	NULL,
	/* tp_mro */
	NULL,
	/* tp_cache */
	NULL,
	/* tp_subclasses */
	NULL,
	/* tp_weaklist */
	NULL,
	/* tp_del */
	0
};

/* Creates a new pyfwnt access control entry object
 * Returns a Python object if successful or NULL on error
 */
PyObject *pyfwnt_access_control_entry_new(
           libfwnt_access_control_entry_t *access_control_entry,
           PyObject *parent_object )
{
	pyfwnt_access_control_entry_t *pyfwnt_access_control_entry = NULL;
	static char *function                                      = "pyfwnt_access_control_entry_new";

	if( access_control_entry == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid access control entry.",
		 function );

		return( NULL );
	}
	pyfwnt_access_control_entry = PyObject_New(
	                               struct pyfwnt_access_control_entry,
	                               &pyfwnt_access_control_entry_type_object );

	if( pyfwnt_access_control_entry == NULL )
	{
		PyErr_Format(
		 PyExc_MemoryError,
		 "%s: unable to initialize access control entry.",
		 function );

		goto on_error;
	}
	if( pyfwnt_access_control_entry_init(
	     pyfwnt_access_control_entry ) != 0 )
	{
		PyErr_Format(
		 PyExc_MemoryError,
		 "%s: unable to initialize access control entry.",
		 function );

		goto on_error;
	}
	pyfwnt_access_control_entry->access_control_entry = access_control_entry;
	pyfwnt_access_control_entry->parent_object        = parent_object;

	if( pyfwnt_access_control_entry->parent_object != NULL )
	{
		Py_IncRef(
		 pyfwnt_access_control_entry->parent_object );
	}
	return( (PyObject *) pyfwnt_access_control_entry );

on_error:
	if( pyfwnt_access_control_entry != NULL )
	{
		Py_DecRef(
		 (PyObject *) pyfwnt_access_control_entry );
	}
	return( NULL );
}

/* Intializes an access control entry object
 * Returns 0 if successful or -1 on error
 */
int pyfwnt_access_control_entry_init(
     pyfwnt_access_control_entry_t *pyfwnt_access_control_entry )
{
	static char *function = "pyfwnt_access_control_entry_init";

	if( pyfwnt_access_control_entry == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid access control entry.",
		 function );

		return( -1 );
	}
	/* Make sure libfwnt access control entry is set to NULL
	 */
	pyfwnt_access_control_entry->access_control_entry = NULL;

	return( 0 );
}

/* Frees an access control entry object
 */
void pyfwnt_access_control_entry_free(
      pyfwnt_access_control_entry_t *pyfwnt_access_control_entry )
{
	libcerror_error_t *error    = NULL;
	struct _typeobject *ob_type = NULL;
	static char *function       = "pyfwnt_access_control_entry_free";
	int result                  = 0;

	if( pyfwnt_access_control_entry == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid access control entry.",
		 function );

		return;
	}
	if( pyfwnt_access_control_entry->access_control_entry == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid access control entry - missing libfwnt access control entry.",
		 function );

		return;
	}
	ob_type = Py_TYPE(
	           pyfwnt_access_control_entry );

	if( ob_type == NULL )
	{
		PyErr_Format(
		 PyExc_ValueError,
		 "%s: missing ob_type.",
		 function );

		return;
	}
	if( ob_type->tp_free == NULL )
	{
		PyErr_Format(
		 PyExc_ValueError,
		 "%s: invalid ob_type - missing tp_free.",
		 function );

		return;
	}
	Py_BEGIN_ALLOW_THREADS

	result = libfwnt_access_control_entry_free(
	          &( pyfwnt_access_control_entry->access_control_entry ),
	          &error );

	Py_END_ALLOW_THREADS

	if( result != 1 )
	{
		pyfwnt_error_raise(
		 error,
		 PyExc_MemoryError,
		 "%s: unable to free access control entry.",
		 function );

		libcerror_error_free(
		 &error );
	}
	if( pyfwnt_access_control_entry->parent_object != NULL )
	{
		Py_DecRef(
		 pyfwnt_access_control_entry->parent_object );
	}
	ob_type->tp_free(
	 (PyObject*) pyfwnt_access_control_entry );
}

/* Retrieves the type
 * Returns a Python object if successful or NULL on error
 */
PyObject *pyfwnt_access_control_entry_get_type(
           pyfwnt_access_control_entry_t *pyfwnt_access_control_entry,
           PyObject *arguments PYFWNT_ATTRIBUTE_UNUSED )
{
	libcerror_error_t *error = NULL;
	PyObject *integer_object = NULL;
	static char *function    = "pyfwnt_access_control_entry_get_type";
	uint8_t type             = 0;
	int result               = 0;

	PYFWNT_UNREFERENCED_PARAMETER( arguments )

	if( pyfwnt_access_control_entry == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid access control entry.",
		 function );

		return( NULL );
	}
	Py_BEGIN_ALLOW_THREADS

	result = libfwnt_access_control_entry_get_type(
	          pyfwnt_access_control_entry->access_control_entry,
	          &type,
	          &error );

	Py_END_ALLOW_THREADS

	if( result != 1 )
	{
		pyfwnt_error_raise(
		 error,
		 PyExc_IOError,
		 "%s: unable to retrieve type.",
		 function );

		libcerror_error_free(
		 &error );

		return( NULL );
	}
#if PY_MAJOR_VERSION >= 3
	integer_object = PyLong_FromLong(
	                  (long) type );
#else
	integer_object = PyInt_FromLong(
	                  (long) type );
#endif
	return( integer_object );
}

/* Retrieves the flags
 * Returns a Python object if successful or NULL on error
 */
PyObject *pyfwnt_access_control_entry_get_flags(
           pyfwnt_access_control_entry_t *pyfwnt_access_control_entry,
           PyObject *arguments PYFWNT_ATTRIBUTE_UNUSED )
{
	libcerror_error_t *error = NULL;
	PyObject *integer_object = NULL;
	static char *function    = "pyfwnt_access_control_entry_get_flags";
	uint8_t flags            = 0;
	int result               = 0;

	PYFWNT_UNREFERENCED_PARAMETER( arguments )

	if( pyfwnt_access_control_entry == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid access control entry.",
		 function );

		return( NULL );
	}
	Py_BEGIN_ALLOW_THREADS

	result = libfwnt_access_control_entry_get_flags(
	          pyfwnt_access_control_entry->access_control_entry,
	          &flags,
	          &error );

	Py_END_ALLOW_THREADS

	if( result != 1 )
	{
		pyfwnt_error_raise(
		 error,
		 PyExc_IOError,
		 "%s: unable to retrieve flags.",
		 function );

		libcerror_error_free(
		 &error );

		return( NULL );
	}
#if PY_MAJOR_VERSION >= 3
	integer_object = PyLong_FromLong(
	                  (long) flags );
#else
	integer_object = PyInt_FromLong(
	                  (long) flags );
#endif
	return( integer_object );
}

/* Retrieves the access mask
 * Returns a Python object if successful or NULL on error
 */
PyObject *pyfwnt_access_control_entry_get_access_mask(
           pyfwnt_access_control_entry_t *pyfwnt_access_control_entry,
           PyObject *arguments PYFWNT_ATTRIBUTE_UNUSED )
{
	libcerror_error_t *error = NULL;
	PyObject *integer_object = NULL;
	static char *function    = "pyfwnt_access_control_entry_get_access_mask";
	uint32_t access_mask     = 0;
	int result               = 0;

	PYFWNT_UNREFERENCED_PARAMETER( arguments )

	if( pyfwnt_access_control_entry == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid access control entry.",
		 function );

		return( NULL );
	}
	Py_BEGIN_ALLOW_THREADS

	result = libfwnt_access_control_entry_get_access_mask(
	          pyfwnt_access_control_entry->access_control_entry,
	          &access_mask,
	          &error );

	Py_END_ALLOW_THREADS

	if( result != 1 )
	{
		pyfwnt_error_raise(
		 error,
		 PyExc_IOError,
		 "%s: unable to retrieve access mask.",
		 function );

		libcerror_error_free(
		 &error );

		return( NULL );
	}
	integer_object = pyfwnt_integer_unsigned_new_from_64bit(
	                  (uint32_t) access_mask );

	return( integer_object );
}

/* Retrieves the security identifier
 * Returns a Python object if successful or NULL on error
 */
PyObject *pyfwnt_access_control_entry_get_security_identifier(
           pyfwnt_access_control_entry_t *pyfwnt_access_control_entry,
           PyObject *arguments PYFWNT_ATTRIBUTE_UNUSED )
{
	libfwnt_security_identifier_t *security_identifier = NULL;
	libcerror_error_t *error                           = NULL;
	static char *function                              = "pyfwnt_access_control_entry_get_security_identifier";
	int result                                         = 0;

	PYFWNT_UNREFERENCED_PARAMETER( arguments )

	if( pyfwnt_access_control_entry == NULL )
	{
		PyErr_Format(
		 PyExc_TypeError,
		 "%s: invalid access control entry.",
		 function );

		return( NULL );
	}
	Py_BEGIN_ALLOW_THREADS

	result = libfwnt_access_control_entry_get_security_identifier(
	          pyfwnt_access_control_entry->access_control_entry,
	          &security_identifier,
	          &error );

	Py_END_ALLOW_THREADS

	if( result == -1 )
	{
		pyfwnt_error_raise(
		 error,
		 PyExc_IOError,
		 "%s: unable to retrieve security identifier.",
		 function );

		libcerror_error_free(
		 &error );

		goto on_error;
	}
	else if( result == 0 )
	{
		Py_IncRef(
		 Py_None );

		return( Py_None );
	}
	return( pyfwnt_security_identifier_new(
	         security_identifier,
	         (PyObject *) pyfwnt_access_control_entry ) );

on_error:
	if( security_identifier != NULL )
	{
		libfwnt_security_identifier_free(
		 &security_identifier,
		 NULL );
	}
	return( NULL );
}

