libsmack: close smackfs_mnt_dirfd in the library destructor

Close smackfs_mnt_dirfd in the library destructor. Although kernel
would wipe it anyway it is a good practice to clean up all the
reserved resources.

Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
libsmack: lazy initialization for SmackFS mount point

Mount SmackFS only when it is first needed. This enables init
daemons to directly use libsmack.

Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Fix bug in cipso written rules

The label (either short or long) was followed by a null character.
It is now followed by a space.

Note that the format for cipso2 smackfs is "%s?%4d%4d..."
where ? stands for any invalid label character.

Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
Removed redundant AUTHORS files.

Git log is the AUTHORS file.

Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Adding a program generating test data.

The program that generate this gen.c produces
a set of rules to be loaded to load/load2
file system interace of smacke. Or to be used
as input file for smackload.

The count of differents labels (option l=N) and of
different access rights (option r=N) can be specified.
The count of rules produced to the ouput can also
be specified (option o=N). By default, l=5, r=100 and o=500.

The generated labels are made of 4 to 7 random letters.
The standard C function 'rand' is used without seeding it;
what means that same options produces same results.

Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
libsmack: make local function accesses_add() static

Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
libsmack: refactoring of label dictionary code

Commit cd2ce11 introduced label dictionary, with hash table of labels
hidden behind an abstraction layer. This patch drops this layer and merges
the hash table with smack_accesses.
It will enable better handling of data, like storing and accessing
per-label data outside of dict_* functions.

Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
libsmack: implement internal hash table of labels

Use hash table implemented internally for dictionary of labels.

Thanks to that some operations are more efficient:
-it is possible to use better hashing function than in hsearch:
 DJB2 algorithm is used, which is much better, than hashing function
 used in hsearch. Hashing function in hsearch causes, that on 32bit
 machines, all labels that have common first eight bytes,
 share same bucket and this cause many conflicts in combination of labels
 naming convention with domain-based prefixes. This leads to significant
 performance degradation in real system. This was not detected on tests
 so far, because labels for test policies were generated randomly.
-it is possible to calculate hash during label validation.
-it would not cause drastic complexity growth to have number of labels
 near to number of allocated buckets as it happens in hsearch.
-it would be possible to add number of labels exceeding number of
 allocated buckets.

Signed-off-by: Jan Cybulski <j.cybulski@samsung.com>
libsmack: add possibility of resizing table of labels

Signed-off-by: Jan Cybulski <j.cybulski@samsung.com>
libsmack: shrink few rule and label struct fields

Use smaller struct fields to optimize memory usage and speed.
On a 32-bit machine this saves 4 bytes per rule and 4 bytes per label.
Limit label length to 8 bits. It's max value is already limited to 255.
Limit label id to 16 bits. While policy with more than 2^16 labels is
theoretically possible, it would be handled very badly by kernel.

Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
libsmack: reorganize rule lists for struct smack_accesses

Until now rule list was implemented as single linked list of all rules
added into struct smack_accesses. This patch breaks this list into several
lists, with one list of rules per subject label.
Each element in array of labels describe single label and all rules when it's a
subject are added into this label's list.

This data structure is close to internal kernel data structures for Smack
rules. This allows for slightly better performance because rules are
grouped by subject.
More importantly though, this patch prepares ground for rule merging.

Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
libsmack: change semantics of rule allow_code and deny_code

Fields in struct smack_rule are used to store either set or modify rule.
Set rules used to be distinguished by having deny_code = -1.
It is more convenient to have it differently: allow_code describing bits
that are to be set, deny_code describing bits that are to be cleared.
With that semantics access_code = ~deny_code for set rules. This enables
easy replacement of change rules that can be simplified to a set rule.
Thanks José Bollo for original idea about simplifying modify rules.

Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
libsmack: merge rules with the same subject and object before applying them

All rules with the same subject and object will be merged into a single one
before applying rules to kernel or writing them to a file. The result will
consist of smaller number of rules, but they will have the same semantics.
This enhances performance greatly when there are a lot of rules to merge.
The merging code has negligible overhead when there are no merges to
perform.

Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
change type of label_id back to int
don't allocate memory in accesses_print
tests: Change program generating test data

-Change program to produce more parametrized output:
	*Introduce parameters to set number of unique rules (u) and merges in policy (m).
	*Introduce parametr for maximum number of reocurrances of a label in merged policy (L).
	*Add possibility of getting list of labels from stdin in addition to generating random ones.
-Some minor style adjustments to libsmack coding style.

Also:
-Add makefile for policies generation
-Add script generating different type of policies

Signed-off-by: Jan Cybulski <j.cybulski@samsung.com>
tests: Changing filenames

Changing two filenames:
- tests/gen.c    becomes tests/generator.c
- tests/makefile becomes tests/Makefile

That is obviously better to have the main file of the program
`generator` named `generator.c`.

The default naming of makefiles is Makefile with a capital
letter. Conforming to that rule is better.

Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
tests: Renaming 'r' to 'alea'

Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
tests: Correcting exit codes

The valid exit code are form 0 to 255. Then using -1 means that the
effective exit code will be 0377==255. It isn't accurate. The exit code
of 1 is merely very often used for failure status.

Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
tests: Adding some comments

Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>