OSDN Git Service

gpet initial repository
[gpet/origin.git] / src / interface.inc
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /*
3  * Gui Policy Editor for TOMOYO Linux
4  *
5  * interface.inc
6  * Copyright (C) Yoshihiro Kusuno 2010 <yocto@users.sourceforge.jp>
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Library General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
21  */
22
23 int get_domain_policy(struct ccs_domain_policy *dp, int *count)
24 {
25         int     result = 1;
26
27         ccs_domain_sort_type = 0;
28         ccs_current_screen = CCS_SCREEN_DOMAIN_LIST;
29         result = ccs_generic_list_loop(dp);
30         *count = dp->list_len - ccs_unnumbered_domain_count;
31         return result;
32 }
33
34 int add_domain(char *input, char **err_buff)
35 {
36         int     result = 0;
37
38         ccs_current_screen = CCS_SCREEN_DOMAIN_LIST;
39         gpet_line = input;
40         ccs_add_entry(NULL);
41         gpet_line = NULL;
42
43         if (ccs_last_error) {
44                 (*err_buff) = strdup(ccs_last_error);
45                 free(ccs_last_error);
46                 ccs_last_error = NULL;
47                 result = 1;
48         }
49
50         return result;
51 }
52
53 int set_profile(struct ccs_domain_policy *dp,
54                                 char *profile, char **err_buff)
55 {
56         int     dummy = 0, result = 0;
57
58         ccs_current_screen = CCS_SCREEN_DOMAIN_LIST;
59         gpet_line = profile;
60         ccs_set_profile(dp, dummy);
61         gpet_line = NULL;
62
63         if (ccs_last_error) {
64                 (*err_buff) = strdup(ccs_last_error);
65                 free(ccs_last_error);
66                 ccs_last_error = NULL;
67                 result = 1;
68         }
69
70         return result;
71 }
72
73 int get_task_list(struct ccs_task_entry **tsk, int *count)
74 {
75         struct ccs_domain_policy *dp = NULL;
76         int     result;
77
78         ccs_domain_sort_type = 1;
79         ccs_current_screen = CCS_SCREEN_DOMAIN_LIST;
80         result = ccs_generic_list_loop(dp);
81         *tsk = ccs_task_list;
82         *count = ccs_task_list_len;
83         return result;
84 }
85
86 static void generic_acl_copy(struct ccs_generic_acl **src, int src_cnt, 
87                                 struct ccs_generic_acl **dst, int dst_cnt)
88 {
89         int     i;
90
91         while (dst_cnt)
92                 free((void *)((*dst)[--dst_cnt].operand));
93
94         *dst = realloc(*dst, src_cnt * sizeof(struct ccs_generic_acl));
95         if (!(*dst))
96                 ccs_out_of_memory();
97
98         memcpy(*dst, ccs_generic_acl_list,
99                         src_cnt * sizeof(struct ccs_generic_acl));
100         for (i = 0; i < src_cnt; i++) {
101                 (*dst)[i].directive = (*src)[i].directive;
102                 (*dst)[i].selected = (*src)[i].selected;
103                 (*dst)[i].operand = strdup((*src)[i].operand);
104                 if (!((*dst)[i].operand))
105                         ccs_out_of_memory();
106         }
107
108
109 int get_acl_list(struct ccs_domain_policy *dp, int current,
110                         struct ccs_generic_acl **ga, int *count)
111 {
112         int     result = 0;
113
114         if (ccs_initializer_source(dp, current)) {
115                 *count = 0;
116         } else if (ccs_deleted_domain(dp, current)) {
117                 *count = 0;
118         } else {
119                 free(ccs_current_domain);
120                 ccs_current_domain = strdup(ccs_domain_name(dp, current));
121                 if (!ccs_current_domain)
122                         ccs_out_of_memory();
123                 else {
124                         ccs_domain_sort_type = 0;
125                         ccs_current_screen = CCS_SCREEN_ACL_LIST;
126                         result = ccs_generic_list_loop(dp);
127                         generic_acl_copy(&ccs_generic_acl_list,
128                                                 ccs_generic_acl_list_count,
129                                                 ga, *count);
130                         *count = ccs_generic_acl_list_count;
131                 }
132         }
133         return result;
134 }
135
136 int get_process_acl_list(int current,
137                                 struct ccs_generic_acl **ga, int *count)
138 {
139         struct ccs_domain_policy *dp = NULL;
140         int     result = 0;
141
142         free(ccs_current_domain);
143         ccs_current_pid = ccs_task_list[current].pid;
144         ccs_current_domain = strdup(ccs_task_list[current].domain);
145         if (!ccs_current_domain)
146                 ccs_out_of_memory();
147         else {
148                 ccs_domain_sort_type = 1;
149                 ccs_current_screen = CCS_SCREEN_ACL_LIST;
150                 result = ccs_generic_list_loop(dp);
151                 generic_acl_copy(&ccs_generic_acl_list,
152                                         ccs_generic_acl_list_count,
153                                         ga, *count);
154                 *count = ccs_generic_acl_list_count;
155         }
156         return result;
157 }
158
159 int add_acl_list(struct ccs_domain_policy *dp, int current,
160                         char *input, char **err_buff)
161 {
162         int     result = 0;
163
164         if (ccs_initializer_source(dp, current) ||
165                 ccs_deleted_domain(dp, current))
166                 return 1;
167
168         ccs_current_screen = CCS_SCREEN_ACL_LIST;
169         gpet_line = input;
170         ccs_add_entry(NULL);
171         gpet_line = NULL;
172
173         if (ccs_last_error) {
174                 (*err_buff) = strdup(ccs_last_error);
175                 free(ccs_last_error);
176                 ccs_last_error = NULL;
177                 result = 1;
178         }
179
180         return result;
181 }
182
183 const char *get_transition_name(enum ccs_transition_type type)
184 {
185     return ccs_transition_type[type];
186 }
187
188 int get_exception_policy(struct ccs_generic_acl **ga, int *count)
189 {
190         struct ccs_domain_policy *dp = NULL;
191         int     result;
192
193         ccs_current_screen = CCS_SCREEN_EXCEPTION_LIST;
194         result = ccs_generic_list_loop(dp);
195         *ga = ccs_generic_acl_list;
196         *count = ccs_generic_acl_list_count;
197         return result;
198 }
199
200 int add_exception_policy(char *input, char **err_buff)
201 {
202         int     result = 0;
203
204         ccs_current_screen = CCS_SCREEN_EXCEPTION_LIST;
205         gpet_line = input;
206         ccs_add_entry(NULL);
207         gpet_line = NULL;
208
209         if (ccs_last_error) {
210                 (*err_buff) = strdup(ccs_last_error);
211                 free(ccs_last_error);
212                 ccs_last_error = NULL;
213                 result = 1;
214         }
215
216         return result;
217 }
218
219 int get_profile(struct ccs_generic_acl **ga, int *count)
220 {
221         struct ccs_domain_policy *dp = NULL;
222         int     result;
223
224         while (*count)
225                 free((void *)((*ga)[--(*count)].operand));
226
227         ccs_current_screen = CCS_SCREEN_PROFILE_LIST;
228         result = ccs_generic_list_loop(dp);
229
230         generic_acl_copy(&ccs_generic_acl_list,
231                                 ccs_generic_acl_list_count,
232                                 ga, *count);
233         *count = ccs_generic_acl_list_count;
234
235         return result;
236 }
237
238 int add_profile(char *input, char **err_buff)
239 {
240         int     result = 0;
241
242         ccs_current_screen = CCS_SCREEN_PROFILE_LIST;
243         gpet_line = input;
244         ccs_add_entry(NULL);
245         gpet_line = NULL;
246
247         if (ccs_last_error) {
248                 (*err_buff) = strdup(ccs_last_error);
249                 free(ccs_last_error);
250                 ccs_last_error = NULL;
251                 result = 1;
252         }
253
254         return result;
255 }
256
257 int set_profile_level(int index, const char *input, char **err_buff)
258 {
259         struct ccs_domain_policy *dp = NULL;
260         int     result = 0;
261
262         ccs_current_screen = CCS_SCREEN_PROFILE_LIST;
263         ccs_generic_acl_list[index].selected = 1;
264         gpet_line = (char *)input;
265         ccs_set_level(dp, 0);
266         gpet_line = NULL;
267
268         if (ccs_last_error) {
269                 (*err_buff) = strdup(ccs_last_error);
270                 free(ccs_last_error);
271                 ccs_last_error = NULL;
272                 result = 1;
273         }
274
275         return result;
276 }
277
278 int get_manager(struct ccs_generic_acl **ga, int *count)
279 {
280         struct ccs_domain_policy *dp = NULL;
281         struct ccs_generic_acl *orig_generic_acl_list = NULL;
282         int             orig_generic_acl_list_count = 0;
283         int             result;
284
285         generic_acl_copy(&ccs_generic_acl_list,
286                                 ccs_generic_acl_list_count,
287                         &orig_generic_acl_list,
288                                 orig_generic_acl_list_count);
289         orig_generic_acl_list_count = ccs_generic_acl_list_count;
290
291         ccs_current_screen = CCS_SCREEN_MANAGER_LIST;
292         result = ccs_generic_list_loop(dp);
293
294         generic_acl_copy(&ccs_generic_acl_list,
295                                 ccs_generic_acl_list_count,
296                                 ga, *count);
297         *count = ccs_generic_acl_list_count;
298
299         generic_acl_copy(&orig_generic_acl_list,
300                                 orig_generic_acl_list_count,
301                         &ccs_generic_acl_list,
302                                  ccs_generic_acl_list_count);
303         ccs_generic_acl_list_count = orig_generic_acl_list_count;
304
305         return result;
306 }
307
308 int add_manager(char *input, char **err_buff)
309 {
310         int     result = 0;
311
312         ccs_current_screen = CCS_SCREEN_MANAGER_LIST;
313         gpet_line = input;
314         ccs_add_entry(NULL);
315         gpet_line = NULL;
316
317         if (ccs_last_error) {
318                 (*err_buff) = strdup(ccs_last_error);
319                 free(ccs_last_error);
320                 ccs_last_error = NULL;
321                 result = 1;
322         }
323
324         return result;
325 }
326
327 int get_memory(struct ccs_generic_acl **ga, int *count)
328 {
329         struct ccs_domain_policy *dp = NULL;
330         struct ccs_generic_acl *orig_generic_acl_list = NULL;
331         int             orig_generic_acl_list_count = 0;
332         int             result;
333
334         generic_acl_copy(&ccs_generic_acl_list,
335                                 ccs_generic_acl_list_count,
336                         &orig_generic_acl_list,
337                                 orig_generic_acl_list_count);
338         orig_generic_acl_list_count = ccs_generic_acl_list_count;
339
340         ccs_current_screen = CCS_SCREEN_MEMINFO_LIST;
341         result = ccs_generic_list_loop(dp);
342
343         generic_acl_copy(&ccs_generic_acl_list,
344                                 ccs_generic_acl_list_count,
345                                 ga, *count);
346         *count = ccs_generic_acl_list_count;
347
348         generic_acl_copy(&orig_generic_acl_list,
349                                 orig_generic_acl_list_count,
350                         &ccs_generic_acl_list,
351                                  ccs_generic_acl_list_count);
352         ccs_generic_acl_list_count = orig_generic_acl_list_count;
353
354         return result;
355 }
356
357 int set_memory(struct ccs_generic_acl *ga, int count, char **err_buff)
358 {
359         struct ccs_domain_policy *dp = NULL;
360         int     i, result = 0;
361
362         ccs_current_screen = CCS_SCREEN_MEMINFO_LIST;
363         ccs_policy_file = CCS_PROC_POLICY_MEMINFO;
364         generic_acl_copy(&ga, count,
365                 &ccs_generic_acl_list, ccs_generic_acl_list_count);
366         ccs_generic_acl_list_count = count;
367
368         for (i = 0; i < count; i++) {
369                 ccs_generic_acl_list[i].selected = 0;
370         }       
371
372         for (i = 0; i < count; i++) {
373                 char    *cp;
374                 cp = strrchr(ccs_generic_acl_list[i].operand, ':');
375                 if (cp && ga[i].selected) {
376                         gpet_line = strdup(++cp);
377 fprintf(stderr, "mem:%2d[%s][%s]\n", i, ccs_generic_acl_list[i].operand, cp);
378                         ccs_generic_acl_list[i].selected = 1;
379                         ccs_set_quota(dp, -1);
380                         ccs_generic_acl_list[i].selected = 0;
381                 }
382         }
383
384         if (ccs_last_error) {
385                 (*err_buff) = strdup(ccs_last_error);
386                 free(ccs_last_error);
387                 ccs_last_error = NULL;
388                 result = 1;
389         }
390
391         return result;
392 }
393
394 int delete_domain_policy(struct ccs_domain_policy *dp, char **err_buff)
395 {
396         int     result = 0;
397
398         ccs_current_screen = CCS_SCREEN_DOMAIN_LIST;
399         ccs_policy_file = CCS_PROC_POLICY_DOMAIN_POLICY;
400         ccs_delete_entry(dp, -1);
401
402         if (ccs_last_error) {
403                 (*err_buff) = strdup(ccs_last_error);
404                 free(ccs_last_error);
405                 ccs_last_error = NULL;
406                 result = 1;
407         }
408
409         return result;
410 }
411
412 int delete_acl_policy(struct ccs_domain_policy *dp, char **err_buff,
413                                 struct ccs_generic_acl *ga, int count)
414 {
415         int     result = 0;
416
417         ccs_current_screen = CCS_SCREEN_ACL_LIST;
418         ccs_policy_file = CCS_PROC_POLICY_DOMAIN_POLICY;
419
420         generic_acl_copy(&ga, count,
421                 &ccs_generic_acl_list, ccs_generic_acl_list_count);
422         ccs_generic_acl_list_count = count;
423         ccs_delete_entry(dp, -1);
424
425         if (ccs_last_error) {
426                 (*err_buff) = strdup(ccs_last_error);
427                 free(ccs_last_error);
428                 ccs_last_error = NULL;
429                 result = 1;
430         }
431
432         return result;
433 }
434
435 int delete_exp_policy(struct ccs_domain_policy *dp, char **err_buff,
436                                 struct ccs_generic_acl *ga, int count)
437 {
438         int     result = 0;
439
440         ccs_current_screen = CCS_SCREEN_EXCEPTION_LIST;
441         ccs_policy_file = CCS_PROC_POLICY_EXCEPTION_POLICY;
442         ccs_generic_acl_list = ga;
443         ccs_generic_acl_list_count = count;
444         ccs_delete_entry(dp, -1);
445
446         if (ccs_last_error) {
447                 (*err_buff) = strdup(ccs_last_error);
448                 free(ccs_last_error);
449                 ccs_last_error = NULL;
450                 result = 1;
451         }
452
453         return result;
454 }
455
456 int delete_manager_policy(
457                 struct ccs_generic_acl *ga, int count, char **err_buff)
458 {
459         struct ccs_domain_policy *dp = NULL;
460         int     result = 0;
461
462         ccs_current_screen = CCS_SCREEN_MANAGER_LIST;
463         ccs_policy_file = CCS_PROC_POLICY_MANAGER;
464         generic_acl_copy(&ga, count,
465                 &ccs_generic_acl_list, ccs_generic_acl_list_count);
466         ccs_generic_acl_list_count = count;
467         ccs_delete_entry(dp, -1);
468
469         if (ccs_last_error) {
470                 (*err_buff) = strdup(ccs_last_error);
471                 free(ccs_last_error);
472                 ccs_last_error = NULL;
473                 result = 1;
474         }
475
476         return result;
477 }
478
479 int is_offline(void)
480 {
481         return ccs_offline_mode;
482 }