OSDN Git Service

Ver. 0.4 Released
[gpet/origin.git] / src / usr_sbin / ccstools.h
1 /*
2  * ccstools.h
3  *
4  * TOMOYO Linux's utilities.
5  *
6  * Copyright (C) 2005-2011  NTT DATA CORPORATION
7  *
8  * Version: 1.8.2+   2011/07/07
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License v2 as published by the
12  * Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22  */
23 #define _FILE_OFFSET_BITS 64
24 #define _LARGEFILE_SOURCE
25 #define _LARGEFILE64_SOURCE
26 #define _GNU_SOURCE
27 #include <arpa/inet.h>
28 #include <asm/types.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/file.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <sys/un.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include <stdarg.h>
44
45 #define s8 __s8
46 #define u8 __u8
47 #define u16 __u16
48 #define u32 __u32
49 #define true  1
50 #define false 0
51
52 /***** CONSTANTS DEFINITION START *****/
53
54 #ifdef __GPET
55 _Bool is_ccs(void);
56 #define TOMOYO_PROC_POLICY_DIR "/sys/kernel/security/tomoyo/"
57
58 #define CCS_PROC_POLICY_DIR \
59                 is_ccs() ? "/proc/ccs/" : "/sys/kernel/security/tomoyo/"
60 #define CCS_PROC_POLICY_DOMAIN_POLICY \
61                 is_ccs() ? "/proc/ccs/domain_policy" : \
62                                 "/sys/kernel/security/tomoyo/domain_policy"
63 #define CCS_PROC_POLICY_EXCEPTION_POLICY \
64                 is_ccs() ? "/proc/ccs/exception_policy" : \
65                                 "/sys/kernel/security/tomoyo/exception_policy"
66 #define CCS_PROC_POLICY_AUDIT \
67                 is_ccs() ? "/proc/ccs/audit" : \
68                                 "/sys/kernel/security/tomoyo/audit"
69 #define CCS_PROC_POLICY_MANAGER \
70                 is_ccs() ? "/proc/ccs/manager" : \
71                                 "/sys/kernel/security/tomoyo/manager"
72 #define CCS_PROC_POLICY_STAT \
73                 is_ccs() ? "/proc/ccs/stat" : \
74                                 "/sys/kernel/security/tomoyo/stat"
75 #define CCS_PROC_POLICY_PROCESS_STATUS \
76                 is_ccs() ? "/proc/ccs/.process_status" : \
77                                 "/sys/kernel/security/tomoyo/.process_status"
78 #define CCS_PROC_POLICY_PROFILE \
79                 is_ccs() ? "/proc/ccs/profile" : \
80                                 "/sys/kernel/security/tomoyo/profile"
81 #define CCS_PROC_POLICY_QUERY \
82                 is_ccs() ? "/proc/ccs/query" : \
83                                 "/sys/kernel/security/tomoyo/query"
84 #else
85 #define CCS_PROC_POLICY_DIR              "/proc/ccs/"
86 #define CCS_PROC_POLICY_DOMAIN_POLICY    "/proc/ccs/domain_policy"
87 #define CCS_PROC_POLICY_EXCEPTION_POLICY "/proc/ccs/exception_policy"
88 #define CCS_PROC_POLICY_AUDIT            "/proc/ccs/audit"
89 #define CCS_PROC_POLICY_MANAGER          "/proc/ccs/manager"
90 #define CCS_PROC_POLICY_STAT             "/proc/ccs/stat"
91 #define CCS_PROC_POLICY_PROCESS_STATUS   "/proc/ccs/.process_status"
92 #define CCS_PROC_POLICY_PROFILE          "/proc/ccs/profile"
93 #define CCS_PROC_POLICY_QUERY            "/proc/ccs/query"
94 #endif /* __GPET */
95
96 /***** CONSTANTS DEFINITION END *****/
97
98 /***** STRUCTURES DEFINITION START *****/
99
100 struct ccs_path_info {
101         const char *name;
102         u32 hash;           /* = ccs_full_name_hash(name, total_len) */
103         u16 total_len;      /* = strlen(name)                        */
104         u16 const_len;      /* = ccs_const_part_length(name)         */
105         _Bool is_dir;       /* = ccs_strendswith(name, "/")          */
106         _Bool is_patterned; /* = const_len < total_len               */
107 };
108
109 struct ccs_ip_address_entry {
110         u8 min[16];
111         u8 max[16];
112         _Bool is_ipv6;
113 };
114
115 struct ccs_number_entry {
116         unsigned long min;
117         unsigned long max;
118 };
119
120 struct ccs_domain_info {
121         const struct ccs_path_info *domainname;
122         const char *target_domainname; /* This may be NULL */
123         const struct ccs_transition_control_entry *d_t; /* This may be NULL */
124         const struct ccs_path_info **string_ptr;
125         int string_count;
126         int number;   /* domain number (-1 if is_dis or is_dd) */
127         u8 profile;
128         _Bool is_dis; /* domain initializer source */
129         _Bool is_dit; /* domain initializer target */
130         _Bool is_dk;  /* domain keeper */
131         _Bool is_du;  /* unreachable domain */
132         _Bool is_dd;  /* deleted domain */
133         _Bool profile_assigned;
134         u8 group;
135 };
136
137 struct ccs_domain_policy {
138         struct ccs_domain_info *list;
139         int list_len;
140         unsigned char *list_selected;
141 };
142
143 struct ccs_task_entry {
144         pid_t pid;
145         pid_t ppid;
146         char *name;
147         char *domain;
148         u8 profile;
149         _Bool selected;
150         int index;
151         int depth;
152 };
153
154 /***** STRUCTURES DEFINITION END *****/
155
156 /***** PROTOTYPES DEFINITION START *****/
157
158 FILE *ccs_open_read(const char *filename);
159 FILE *ccs_open_write(const char *filename);
160 _Bool ccs_check_remote_host(void);
161 _Bool ccs_close_write(FILE *fp);
162 _Bool ccs_correct_domain(const char *domainname);
163 _Bool ccs_correct_path(const char *filename);
164 _Bool ccs_correct_word(const char *string);
165 _Bool ccs_decode(const char *ascii, char *bin);
166 _Bool ccs_domain_def(const char *domainname);
167 _Bool ccs_move_proc_to_file(const char *src, const char *dest);
168 _Bool ccs_path_matches_pattern(const struct ccs_path_info *pathname0,
169                                const struct ccs_path_info *pattern0);
170 _Bool ccs_pathcmp(const struct ccs_path_info *a,
171                   const struct ccs_path_info *b);
172 _Bool ccs_str_starts(char *str, const char *begin);
173 char *ccs_freadline(FILE *fp);
174 char *ccs_freadline_unpack(FILE *fp);
175 char *ccs_shprintf(const char *fmt, ...)
176         __attribute__ ((format(printf, 1, 2)));
177 char *ccs_strdup(const char *string);
178 const char *ccs_domain_name(const struct ccs_domain_policy *dp,
179                             const int index);
180 const struct ccs_path_info *ccs_savename(const char *name);
181 int ccs_add_string_entry(struct ccs_domain_policy *dp, const char *entry,
182                          const int index);
183 int ccs_assign_domain(struct ccs_domain_policy *dp, const char *domainname,
184                       const _Bool is_dis, const _Bool is_dd);
185 int ccs_del_string_entry(struct ccs_domain_policy *dp, const char *entry,
186                          const int index);
187 int ccs_find_domain(const struct ccs_domain_policy *dp,
188                     const char *domainname0, const _Bool is_dis,
189                     const _Bool is_dd);
190 int ccs_find_domain_by_ptr(struct ccs_domain_policy *dp,
191                            const struct ccs_path_info *domainname);
192 int ccs_open_stream(const char *filename);
193 int ccs_parse_ip(const char *address, struct ccs_ip_address_entry *entry);
194 int ccs_parse_number(const char *number, struct ccs_number_entry *entry);
195 int ccs_string_compare(const void *a, const void *b);
196 int ccs_write_domain_policy(struct ccs_domain_policy *dp, const int fd);
197 struct ccs_path_group_entry *ccs_find_path_group(const char *group_name);
198 void *ccs_malloc(const size_t size);
199 void *ccs_realloc(void *ptr, const size_t size);
200 void *ccs_realloc2(void *ptr, const size_t size);
201 void ccs_clear_domain_policy(struct ccs_domain_policy *dp);
202 void ccs_delete_domain(struct ccs_domain_policy *dp, const int index);
203 void ccs_fill_path_info(struct ccs_path_info *ptr);
204 void ccs_fprintf_encoded(FILE *fp, const char *ccs_pathname);
205 void ccs_get(void);
206 void ccs_handle_domain_policy(struct ccs_domain_policy *dp, FILE *fp,
207                               _Bool is_write);
208 void ccs_normalize_line(char *buffer);
209 void ccs_put(void);
210 void ccs_read_domain_policy(struct ccs_domain_policy *dp,
211                             const char *filename);
212 void ccs_read_process_list(_Bool show_all);
213
214 extern _Bool ccs_freadline_raw;
215 extern _Bool ccs_network_mode;
216 extern int ccs_task_list_len;
217 extern struct ccs_task_entry *ccs_task_list;
218 extern u16 ccs_network_port;
219 extern u32 ccs_network_ip;
220
221 /***** PROTOTYPES DEFINITION END *****/