OSDN Git Service

4cccb297a17cd89904d3c61bda8dee9432c8396a
[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,2011 <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         ccs_dp = *dp;
30         result = ccs_generic_list_loop();
31         *dp = ccs_dp;
32         *count = dp->list_len - ccs_unnumbered_domain_count;
33         return result;
34 }
35
36 int add_domain(char *input, char **err_buff)
37 {
38         int     result = 0;
39
40         ccs_current_screen = CCS_SCREEN_DOMAIN_LIST;
41         gpet_line = input;
42         ccs_add_entry();
43         gpet_line = NULL;
44
45         if (ccs_last_error) {
46                 (*err_buff) = strdup(ccs_last_error);
47                 free(ccs_last_error);
48                 ccs_last_error = NULL;
49                 result = 1;
50         }
51
52         return result;
53 }
54
55 int set_profile(struct ccs_domain_policy *dp,
56                                 char *profile, char **err_buff)
57 {
58         int     dummy = 0, result = 0;
59
60         ccs_current_screen = CCS_SCREEN_DOMAIN_LIST;
61         gpet_line = profile;
62         ccs_dp = *dp;
63         ccs_set_profile(dummy);
64 //      *dp = ccs_dp;
65         gpet_line = NULL;
66
67         if (ccs_last_error) {
68                 (*err_buff) = strdup(ccs_last_error);
69                 free(ccs_last_error);
70                 ccs_last_error = NULL;
71                 result = 1;
72         }
73
74         return result;
75 }
76
77 int get_task_list(struct ccs_task_entry **tsk, int *count)
78 {
79         int     result;
80
81         ccs_domain_sort_type = 1;
82         ccs_current_screen = CCS_SCREEN_DOMAIN_LIST;
83         result = ccs_generic_list_loop();
84         *tsk = ccs_task_list;
85         *count = ccs_task_list_len;
86         return result;
87 }
88
89 static void generic_acl_copy(struct ccs_generic_acl **src, int src_cnt,
90                                 struct ccs_generic_acl **dst, int dst_cnt)
91 {
92         int     i;
93
94         while (dst_cnt)
95                 free((void *)((*dst)[--dst_cnt].operand));
96
97         *dst = realloc(*dst, src_cnt * sizeof(struct ccs_generic_acl));
98         if (!(*dst))
99                 ccs_out_of_memory();
100
101         memcpy(*dst, ccs_gacl_list,
102                         src_cnt * sizeof(struct ccs_generic_acl));
103         for (i = 0; i < src_cnt; i++) {
104                 (*dst)[i].directive = (*src)[i].directive;
105                 (*dst)[i].selected = (*src)[i].selected;
106                 (*dst)[i].operand = strdup((*src)[i].operand);
107                 if (!((*dst)[i].operand))
108                         ccs_out_of_memory();
109         }
110 }
111
112 int get_acl_list(struct ccs_domain_policy *dp, int current,
113                         struct ccs_generic_acl **ga, int *count)
114 {
115         int     result = 0;
116
117         ccs_dp = *dp;
118         if (ccs_initializer_source(current)) {
119                 *count = 0;
120         } else if (ccs_deleted_domain(current)) {
121                 *count = 0;
122         } else {
123                 free(ccs_current_domain);
124                 ccs_current_domain = strdup(ccs_domain_name(dp, current));
125                 if (!ccs_current_domain)
126                         ccs_out_of_memory();
127                 else {
128                         ccs_domain_sort_type = 0;
129                         ccs_current_screen = CCS_SCREEN_ACL_LIST;
130                         result = ccs_generic_list_loop();
131                         generic_acl_copy(&ccs_gacl_list,
132                                                 ccs_gacl_list_count,
133                                                 ga, *count);
134                         *count = ccs_gacl_list_count;
135                 }
136         }
137         *dp = ccs_dp;
138         return result;
139 }
140
141 int get_process_acl_list(int current,
142                                 struct ccs_generic_acl **ga, int *count)
143 {
144         int     result = 0;
145
146         free(ccs_current_domain);
147         ccs_current_pid = ccs_task_list[current].pid;
148         ccs_current_domain = strdup(ccs_task_list[current].domain);
149         if (!ccs_current_domain)
150                 ccs_out_of_memory();
151         else {
152                 ccs_domain_sort_type = 1;
153                 ccs_current_screen = CCS_SCREEN_ACL_LIST;
154                 result = ccs_generic_list_loop();
155                 generic_acl_copy(&ccs_gacl_list,
156                                         ccs_gacl_list_count,
157                                         ga, *count);
158                 *count = ccs_gacl_list_count;
159         }
160         return result;
161 }
162
163 int get_optimize_acl_list(int current, struct ccs_generic_acl **ga, int count)
164 {
165         ccs_current_screen = CCS_SCREEN_ACL_LIST;
166         ccs_policy_file = CCS_PROC_POLICY_DOMAIN_POLICY;
167
168         generic_acl_copy(ga, count, &ccs_gacl_list, ccs_gacl_list_count);
169
170         ccs_list_item_count = ccs_gacl_list_count = count;
171         ccs_editpolicy_optimize(current);
172
173         generic_acl_copy(&ccs_gacl_list, ccs_gacl_list_count, ga, count);
174
175         return 0;
176 }
177
178 int add_acl_list(struct ccs_domain_policy *dp, int current,
179                         char *input, char **err_buff)
180 {
181         int     result = 0;
182
183         ccs_dp = *dp;
184         if (ccs_initializer_source(current) ||
185                 ccs_deleted_domain(current)) {
186                 *dp = ccs_dp;
187                 return 1;
188         }
189
190         ccs_current_screen = CCS_SCREEN_ACL_LIST;
191         gpet_line = input;
192         ccs_add_entry();
193         gpet_line = NULL;
194         *dp = ccs_dp;
195
196         if (ccs_last_error) {
197                 (*err_buff) = strdup(ccs_last_error);
198                 free(ccs_last_error);
199                 ccs_last_error = NULL;
200                 result = 1;
201         }
202
203         return result;
204 }
205
206 const char *get_transition_name(enum ccs_transition_type type)
207 {
208     return ccs_transition_type[type];
209 }
210
211 int get_exception_policy(struct ccs_generic_acl **ga, int *count)
212 {
213         int     result;
214
215         ccs_current_screen = CCS_SCREEN_EXCEPTION_LIST;
216         result = ccs_generic_list_loop();
217         *ga = ccs_gacl_list;
218         *count = ccs_gacl_list_count;
219         return result;
220 }
221
222 int add_exception_policy(char *input, char **err_buff)
223 {
224         int     result = 0;
225
226         ccs_current_screen = CCS_SCREEN_EXCEPTION_LIST;
227         gpet_line = input;
228         ccs_add_entry();
229         gpet_line = NULL;
230
231         if (ccs_last_error) {
232                 (*err_buff) = strdup(ccs_last_error);
233                 free(ccs_last_error);
234                 ccs_last_error = NULL;
235                 result = 1;
236         }
237
238         return result;
239 }
240
241 int get_profile(struct ccs_generic_acl **ga, int *count)
242 {
243         int     result;
244
245         while (*count)
246                 free((void *)((*ga)[--(*count)].operand));
247
248         ccs_current_screen = CCS_SCREEN_PROFILE_LIST;
249         result = ccs_generic_list_loop();
250
251         generic_acl_copy(&ccs_gacl_list,
252                                 ccs_gacl_list_count,
253                                 ga, *count);
254         *count = ccs_gacl_list_count;
255
256         return result;
257 }
258
259 int add_profile(char *input, char **err_buff)
260 {
261         int     result = 0;
262
263         ccs_current_screen = CCS_SCREEN_PROFILE_LIST;
264         gpet_line = input;
265         ccs_add_entry();
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 set_profile_level(int index, const char *input, char **err_buff)
279 {
280         int     result = 0;
281
282         ccs_current_screen = CCS_SCREEN_PROFILE_LIST;
283         ccs_gacl_list[index].selected = 1;
284         gpet_line = (char *)input;
285         ccs_set_level(0);
286         gpet_line = NULL;
287
288         if (ccs_last_error) {
289                 (*err_buff) = strdup(ccs_last_error);
290                 free(ccs_last_error);
291                 ccs_last_error = NULL;
292                 result = 1;
293         }
294
295         return result;
296 }
297
298 int get_manager(struct ccs_generic_acl **ga, int *count)
299 {
300         struct ccs_generic_acl *orig_generic_acl_list = NULL;
301         int             orig_generic_acl_list_count = 0;
302         int             result;
303
304         generic_acl_copy(&ccs_gacl_list,
305                                 ccs_gacl_list_count,
306                         &orig_generic_acl_list,
307                                 orig_generic_acl_list_count);
308         orig_generic_acl_list_count = ccs_gacl_list_count;
309
310         ccs_current_screen = CCS_SCREEN_MANAGER_LIST;
311         result = ccs_generic_list_loop();
312
313         generic_acl_copy(&ccs_gacl_list,
314                                 ccs_gacl_list_count,
315                                 ga, *count);
316         *count = ccs_gacl_list_count;
317
318         generic_acl_copy(&orig_generic_acl_list,
319                                 orig_generic_acl_list_count,
320                         &ccs_gacl_list,
321                                  ccs_gacl_list_count);
322         ccs_gacl_list_count = orig_generic_acl_list_count;
323
324         return result;
325 }
326
327 int add_manager(char *input, char **err_buff)
328 {
329         int     result = 0;
330
331         ccs_current_screen = CCS_SCREEN_MANAGER_LIST;
332         gpet_line = input;
333         ccs_add_entry();
334         gpet_line = NULL;
335
336         if (ccs_last_error) {
337                 (*err_buff) = strdup(ccs_last_error);
338                 free(ccs_last_error);
339                 ccs_last_error = NULL;
340                 result = 1;
341         }
342
343         return result;
344 }
345
346 int get_memory(struct ccs_generic_acl **ga, int *count)
347 {
348         struct ccs_generic_acl *orig_generic_acl_list = NULL;
349         int             orig_generic_acl_list_count = 0;
350         int             result;
351
352         generic_acl_copy(&ccs_gacl_list,
353                                 ccs_gacl_list_count,
354                         &orig_generic_acl_list,
355                                 orig_generic_acl_list_count);
356         orig_generic_acl_list_count = ccs_gacl_list_count;
357
358         ccs_current_screen = CCS_SCREEN_STAT_LIST;
359         result = ccs_generic_list_loop();
360
361         generic_acl_copy(&ccs_gacl_list,
362                                 ccs_gacl_list_count,
363                                 ga, *count);
364         *count = ccs_gacl_list_count;
365
366         generic_acl_copy(&orig_generic_acl_list,
367                                 orig_generic_acl_list_count,
368                         &ccs_gacl_list,
369                                  ccs_gacl_list_count);
370         ccs_gacl_list_count = orig_generic_acl_list_count;
371
372         return result;
373 }
374
375 int set_memory(struct ccs_generic_acl *ga, int count, char **err_buff)
376 {
377         int     i, result = 0;
378
379         ccs_current_screen = CCS_SCREEN_STAT_LIST;
380         ccs_policy_file = CCS_PROC_POLICY_STAT;
381         generic_acl_copy(&ga, count,
382                 &ccs_gacl_list, ccs_gacl_list_count);
383         ccs_gacl_list_count = count;
384
385         for (i = 0; i < count; i++) {
386                 ccs_gacl_list[i].selected = 0;
387         }
388
389         for (i = 0; i < count; i++) {
390                 char    *cp;
391                 cp = strrchr(ccs_gacl_list[i].operand, ':');
392                 if (cp && ga[i].selected) {
393                         gpet_line = strdup(++cp);
394 // fprintf(stderr, "mem:%2d[%s][%s]\n", i, ccs_gacl_list[i].operand, cp);
395                         ccs_gacl_list[i].selected = 1;
396                         ccs_set_quota(-1);
397                         ccs_gacl_list[i].selected = 0;
398                 }
399         }
400
401         if (ccs_last_error) {
402                 (*err_buff) = strdup(ccs_last_error);
403                 free(ccs_last_error);
404                 ccs_last_error = NULL;
405                 result = 1;
406         }
407
408         return result;
409 }
410
411 int delete_domain_policy(struct ccs_domain_policy *dp, char **err_buff)
412 {
413         int     result = 0;
414
415         ccs_current_screen = CCS_SCREEN_DOMAIN_LIST;
416         ccs_policy_file = CCS_PROC_POLICY_DOMAIN_POLICY;
417         ccs_dp = *dp;
418         ccs_delete_entry(-1);
419         *dp = ccs_dp;
420
421         if (ccs_last_error) {
422                 (*err_buff) = strdup(ccs_last_error);
423                 free(ccs_last_error);
424                 ccs_last_error = NULL;
425                 result = 1;
426         }
427
428         return result;
429 }
430
431 int delete_acl_policy(struct ccs_domain_policy *dp, char **err_buff,
432                                 struct ccs_generic_acl *ga, int count)
433 {
434         int     result = 0;
435
436         ccs_current_screen = CCS_SCREEN_ACL_LIST;
437         ccs_policy_file = CCS_PROC_POLICY_DOMAIN_POLICY;
438
439         generic_acl_copy(&ga, count,
440                 &ccs_gacl_list, ccs_gacl_list_count);
441         ccs_gacl_list_count = count;
442         ccs_dp = *dp;
443         ccs_delete_entry(-1);
444         *dp = ccs_dp;
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_exp_policy(struct ccs_domain_policy *dp, char **err_buff,
457                                 struct ccs_generic_acl *ga, int count)
458 {
459         int     result = 0;
460
461         ccs_current_screen = CCS_SCREEN_EXCEPTION_LIST;
462         ccs_policy_file = CCS_PROC_POLICY_EXCEPTION_POLICY;
463         ccs_gacl_list = ga;
464         ccs_gacl_list_count = count;
465         ccs_dp = *dp;
466         ccs_delete_entry(-1);
467         *dp = ccs_dp;
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 delete_manager_policy(
480                 struct ccs_generic_acl *ga, int count, char **err_buff)
481 {
482         int     result = 0;
483
484         ccs_current_screen = CCS_SCREEN_MANAGER_LIST;
485         ccs_policy_file = CCS_PROC_POLICY_MANAGER;
486         generic_acl_copy(&ga, count,
487                 &ccs_gacl_list, ccs_gacl_list_count);
488         ccs_gacl_list_count = count;
489         ccs_delete_entry(-1);
490
491         if (ccs_last_error) {
492                 (*err_buff) = strdup(ccs_last_error);
493                 free(ccs_last_error);
494                 ccs_last_error = NULL;
495                 result = 1;
496         }
497
498         return result;
499 }
500
501 int is_offline(void)
502 {
503         return ccs_offline_mode;
504 }
505
506 int is_network(void)
507 {
508         return ccs_network_mode;
509 }
510
511 char *get_remote_ip(char *str_ip)
512 {
513         const u32 ip = ntohl(ccs_network_ip);
514         sprintf(str_ip, "%u.%u.%u.%u:%u",
515                         (u8) (ip >> 24), (u8) (ip >> 16),
516                         (u8) (ip >> 8), (u8) ip, ntohs(ccs_network_port));
517         return str_ip;
518 }
519
520 const char *get_policy_dir(void)
521 {
522         return ccs_policy_dir;
523 }
524
525 const char *get_domain_last_name(const int index)
526 {
527         return ccs_get_last_name(index);
528 }