OSDN Git Service

0d4a8e57cfb8f01b036d8465ff419910e5eb4459
[openpts/openpts.git] / src / policy.c
1 /*
2  * This file is part of the OpenPTS project.
3  *
4  * The Initial Developer of the Original Code is International
5  * Business Machines Corporation. Portions created by IBM
6  * Corporation are Copyright (C) 2010 International Business
7  * Machines Corporation. All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the Common Public License as published by
11  * IBM Corporation; either version 1 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * Common Public License for more details.
18  *
19  * You should have received a copy of the Common Public License
20  * along with this program; if not, a copy can be viewed at
21  * http://www.opensource.org/licenses/cpl1.0.php.
22  */
23
24 /**
25  * \file src/policy.c
26  * \brief policy
27  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28  * @date 2010-06-19
29  * cleanup 2011-01-22 SM
30  *
31  * Security Policy
32  * - load
33  * - verify
34  * - print
35  *
36  *
37  */
38
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include <openpts.h>
45
46 /**
47  * Free policy chain
48  */
49 int freePolicyChain(OPENPTS_POLICY *pol) {
50     if (pol == NULL) {
51         return PTS_INTERNAL_ERROR;
52     }
53
54     if (pol->next != NULL) {
55         freePolicyChain(pol->next);
56     }
57
58     free(pol);
59
60     return PTS_SUCCESS;
61 }
62
63 /**
64  * read policy file
65  * 
66  * return
67  *   policy number
68  */
69 int loadPolicyFile(OPENPTS_CONTEXT *ctx, char * filename) {
70     FILE *fp;
71     char buf[BUF_SIZE];  // SMBIOS
72     char *eq;
73     char *name;
74     char *value;
75     int cnt = 1;
76     int len;
77     OPENPTS_POLICY *pol;
78     int line = 0;
79
80     /* open */
81
82     if ((fp = fopen(filename, "r")) == NULL) {
83         ERROR("File %s open was failed\n", filename);
84         return -1;
85     }
86
87     /* parse */
88
89     while (fgets(buf, BUF_SIZE, fp) != NULL) {  // read line
90         /* ignore comment, null line */
91         if (buf[0] == '#') {
92             /* skip comment line */
93         } else if ((eq = strstr(buf, "=")) != NULL) {
94             /* name=value line*/
95             /* remove CR */
96             len = strlen(buf);
97             if (buf[len-1] == 0x0a) buf[len-1] = 0;
98
99             name = buf;
100             value = eq + 1;
101
102             *eq = 0;
103
104             DEBUG("%4d [%s]=[%s]\n", cnt, name, value);
105
106             /* new  */
107             pol = malloc(sizeof(OPENPTS_POLICY));
108             if (pol == NULL) {
109                 ERROR("no mem");
110                 cnt = -1;  // return -1;
111                 goto error;
112             }
113             pol->num = cnt;
114             pol->line = line;
115             memcpy(pol->name, name, strlen(name) + 1);  // TODO
116             memcpy(pol->value, value, strlen(value) + 1);  // TODO
117
118             /* add to chain */
119             if (ctx->policy_start == NULL) {
120                 /* first entry */
121                 ctx->policy_start = pol;
122                 ctx->policy_end = pol;
123                 pol->next = NULL;
124             } else {
125                 /* next entry */
126                 ctx->policy_end->next = pol;
127                 ctx->policy_end = pol;
128                 pol->next = NULL;
129             }
130             cnt++;
131         } else {
132             //
133         }
134         line++;
135     }
136
137   error:
138     fclose(fp);
139
140     return cnt;
141 }
142
143
144 /**
145  * check policy and properties
146  *
147  * Return (=>VR)
148  *   OPENPTS_RESULT_VALID
149  *   OPENPTS_RESULT_INVALID
150  *   OPENPTS_RESULT_UNKNOWN
151  *
152  *   hit      unknown    miss       judge
153  *   valid        
154  *   --------------------------------------- 
155  *   no policy                  => UNKNOWN
156  *   --------------------------------------- 
157  *   all      0         0       => VALID
158  *   some     some      0       => UNKNOWN
159  *   -        -         some    => INVALID
160  *   --------------------------------------- 
161  *
162  */
163 int checkPolicy(OPENPTS_CONTEXT *ctx) {
164     OPENPTS_POLICY *pol;
165     OPENPTS_PROPERTY *prop;
166     int valid = 0;
167     int unknown = 0;
168     int invalid = 0;
169
170     pol = ctx->policy_start;
171
172     if (pol == NULL) {
173         /* no policy to check */
174         return OPENPTS_RESULT_UNKNOWN;
175     }
176
177     /* loop */
178     while (pol != NULL) {
179         /* look up prop */
180         prop =  getProperty(ctx, pol->name);
181
182         /* status */
183         if (prop == NULL) {
184             /* no prop -> Unknown */
185             addReason(ctx, "[POLICY-L%03d] %s is missing",
186                 pol->line,
187                 pol->name);
188             unknown++;
189         } else {
190             if (!strcmp(pol->value, prop->value)) {
191                 // hit
192                 valid++;
193             } else {
194                 addReason(ctx, "[POLICY-L%03d] %s is %s, not %s",
195                     pol->line,
196                     pol->name, prop->value, pol->value);
197                 invalid++;
198             }
199         }
200         pol = pol->next;
201     }
202
203     /* if any invalid exist */
204     if (invalid > 0) {
205         return OPENPTS_RESULT_INVALID;
206     }
207
208     /* if any unknown exist */
209     if (unknown > 0) {
210         return OPENPTS_RESULT_UNKNOWN;
211     }
212
213     return OPENPTS_RESULT_VALID;
214 }
215
216 /**
217  * print policy and properties
218  *
219  *
220  */
221 int printPolicy(OPENPTS_CONTEXT *ctx) {
222     OPENPTS_POLICY *pol;
223     OPENPTS_PROPERTY *prop;
224     char *proc_value;
225     char *status;
226
227     pol = ctx->policy_start;
228
229     printf("   id ");  // id
230     printf("  name                  ");  // name
231     printf("  value(exp)  ");  // value
232     printf("  value(prop) ");  // value
233     printf("  status ");  // status
234     printf("\n");
235
236     printf("------");  // id
237     printf("-------------------------");  // name
238     printf("-------------");  // value
239     printf("-------------");  // value
240     printf("----------");  // value
241     printf("\n");
242
243     while (pol != NULL) {
244         /* look up prop */
245         prop =  getProperty(ctx, pol->name);
246
247         /* status */
248         if (prop == NULL) {
249             proc_value = "missing";
250             status = "X";
251         } else {
252             proc_value = prop->value;
253             if (!strcmp(pol->value, prop->value)) {
254                 status = "O";
255             } else {
256                 status = "X";
257             }
258         }
259
260         /* print */
261 #if 0
262         printf("%5d %-25s %-13s\n",
263             pol->num,
264             pol->name, pol->value);
265 #else
266         printf("%5d %-35s %-28s %-28s %-10s\n",
267             pol->num,
268             pol->name, pol->value,
269             proc_value, status);
270 #endif
271         pol = pol->next;
272     }
273
274     printf("\n");
275
276     return 0;
277 }
278
279