2 * Copyright (C) 2013 Politecnico di Torino, Italy
3 * TORSEC group -- http://security.polito.it
5 * Author: Roberto Sassu <roberto.sassu@polito.it>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2 of the
12 * File: ima_template.c
13 * Helpers to manage template descriptors.
16 #include "ima_template_lib.h"
18 static struct ima_template_desc defined_templates
[] = {
19 {.name
= IMA_TEMPLATE_IMA_NAME
, .fmt
= IMA_TEMPLATE_IMA_FMT
},
20 {.name
= "ima-ng",.fmt
= "d-ng|n-ng"},
23 static struct ima_template_field supported_fields
[] = {
24 {.field_id
= "d",.field_init
= ima_eventdigest_init
,
25 .field_show
= ima_show_template_digest
},
26 {.field_id
= "n",.field_init
= ima_eventname_init
,
27 .field_show
= ima_show_template_string
},
28 {.field_id
= "d-ng",.field_init
= ima_eventdigest_ng_init
,
29 .field_show
= ima_show_template_digest_ng
},
30 {.field_id
= "n-ng",.field_init
= ima_eventname_ng_init
,
31 .field_show
= ima_show_template_string
},
34 static struct ima_template_desc
*ima_template
;
36 static struct ima_template_desc
*lookup_template_desc(const char *name
)
40 for (i
= 0; i
< ARRAY_SIZE(defined_templates
); i
++) {
41 if (strcmp(defined_templates
[i
].name
, name
) == 0)
42 return defined_templates
+ i
;
48 static struct ima_template_field
*lookup_template_field(const char *field_id
)
52 for (i
= 0; i
< ARRAY_SIZE(supported_fields
); i
++)
53 if (strncmp(supported_fields
[i
].field_id
, field_id
,
54 IMA_TEMPLATE_FIELD_ID_MAX_LEN
) == 0)
55 return &supported_fields
[i
];
59 static int template_fmt_size(char *template_fmt
)
62 int template_fmt_len
= strlen(template_fmt
);
65 while (i
< template_fmt_len
) {
75 static int template_desc_init_fields(char *template_fmt
,
76 struct ima_template_field
***fields
,
79 char *c
, *template_fmt_ptr
= template_fmt
;
80 int template_num_fields
= template_fmt_size(template_fmt
);
83 if (template_num_fields
> IMA_TEMPLATE_NUM_FIELDS_MAX
)
86 *fields
= kzalloc(template_num_fields
* sizeof(*fields
), GFP_KERNEL
);
87 if (*fields
== NULL
) {
91 for (i
= 0; (c
= strsep(&template_fmt_ptr
, "|")) != NULL
&&
92 i
< template_num_fields
; i
++) {
93 struct ima_template_field
*f
= lookup_template_field(c
);
109 static int init_defined_templates(void)
114 /* Init defined templates. */
115 for (i
= 0; i
< ARRAY_SIZE(defined_templates
); i
++) {
116 struct ima_template_desc
*template = &defined_templates
[i
];
118 result
= template_desc_init_fields(template->fmt
,
120 &(template->num_fields
));
127 struct ima_template_desc
*ima_template_desc_current(void)
130 ima_template
= lookup_template_desc(IMA_TEMPLATE_IMA_NAME
);
135 int ima_init_template(void)
139 result
= init_defined_templates();