/*
 * security structures required for accounting
 * minimal implementation for lsmlgi
 *
 */


/* includes */

#include "lsmlgi_def.h"
#include <linux/xattr.h>
#include <linux/list.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("lsmlgi");
MODULE_AUTHOR("Thomas Uphill");
MODULE_VERSION("LSMLGI_VERSION");

/* security structs */

struct bprm_security_struct {
        struct linux_binprm *bprm;	/* back pointer to bprm object */
	u32 sid;
	u32 tsid;
	u32 fsid;

        unsigned char set;

	// task security for this exec
	struct task_security_struct *tsec;
	struct inode_security_struct *isec;

        /*
         * unsafe is used to share failure information from bprm_apply_creds()
         * to bprm_post_apply_creds().
         */
        char unsafe;
};

struct file_security_struct {
	struct file *file;		/* back pointer to file object */
	u32 sid;
	u32 tsid;
	u32 fsid;
	u32 fown_sid;			/* SID of the file owner */
};

struct task_security_struct {
        struct task_struct *task;	/* back pointer to task object */
        u32 osid;            		/* SID prior to last execve */
        u32 sid;			/* current SID */
	u32 tsid;			/* task SID */
	u32 fsid;			/* user SID */
        u32 exec_sid;        		/* exec SID */
        u32 create_sid;      		/* fscreate SID */
        u32 keycreate_sid;   		/* keycreate SID */
        u32 sockcreate_sid;  		/* fscreate SID */
	
	int id;			/* id for the task */
	int budget;		/* budget counter */
	int exec;		/* execute counter */
	int read;		/* read counter */
	int write;		/* write counter */
	int del;		/* delete counter */
};

struct inode_security_struct {
	struct inode *inode;		/* back pointer to inode object */
	struct list_head list;	
	u16 sclass;       		/* security class of this object */
	u32 sid;   			/* SID of this object */
	u32 tsid;			/* SID of the creating task */
	u32 fsid;

	unsigned char initialized;	/* initialization flag */
	struct semaphore sem;		/* semaphore for ??? */
	unsigned char inherit;		/* inherit SID from parent entry */
};

struct superblock_security_struct {
	struct super_block *sb;         /* back pointer to sb object */
        struct list_head list;          /* list of superblock_security_struct */

        u32 sid;			/* SID of file system superblock */
	u32 tsid;
	u32 fsid;
        u32 def_sid;                    /* default SID for labeling */
        u32 mntpoint_sid;               /* SECURITY_FS_USE_MNTPOINT context for files */
        unsigned int behavior;          /* labeling behavior */
        unsigned char initialized;      /* initialization flag */
        unsigned char proc;             /* proc fs */
        struct semaphore sem;
        struct list_head isec_head;
        spinlock_t isec_lock;
};

struct law_struct {
	int uid;			/* uid */
	int gid;			/* gid */
	int oper;			/* operation */
	int oper1;			/* operand */
	int comp;
	int oper2;			/* operand */
};

/* function declarations */

int lsmlgi_check_law(int op, struct inode_security_struct *isec);
static int lsmlgi_update_parent(struct task_struct *task, int op);
//static int inode_security_set_sid(struct inode *inode, u32 sid);
static int lsmlgi_newsid(int *sid);
static int lsmlgi_task_alloc_security(struct task_struct *task);
static int security_context_to_sid(char *context, u32 len, u32 *sid, 
				   u32 *tsid, u32 *fsid);
//static int inode_doinit_with_dentry(struct inode *inode, 
//				    struct dentry *opt_dentry);
//static inline int inode_doinit(struct inode *inode);
//static int superblock_doinit(struct super_block *sb, void *data);
//static int superblock_alloc_security(struct super_block *sb);


static void print_string(char *str);
static char *lsmlgi_dumplaw(void);
extern spinlock_t sb_lock;
