Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/caitsith-tools/usr_sbin/caitsith-savepolicy.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 214 - (show annotations) (download) (as text)
Tue Oct 4 15:00:21 2016 UTC (7 years, 7 months ago) by kumaneko
File MIME type: text/x-csrc
File size: 4261 byte(s)


1 /*
2 * caitsith-savepolicy.c
3 *
4 * CaitSith's utilities.
5 *
6 * Copyright (C) 2005-2012 NTT DATA CORPORATION
7 *
8 * Version: 0.2 2016/10/05
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 #include "caitsithtools.h"
24
25 /**
26 * ccs_move_proc_to_file - Save /sys/kernel/security/caitsith/ to /etc/caitsith/ .
27 *
28 * @src: Filename to save from.
29 * @dest: Filename to save to.
30 *
31 * Returns true on success, false otherwise.
32 */
33 static _Bool ccs_move_proc_to_file(const char *src, const char *dest)
34 {
35 FILE *proc_fp = ccs_open_read(src);
36 FILE *file_fp;
37 _Bool result = true;
38 if (!proc_fp) {
39 fprintf(stderr, "Can't open %s for reading.\n", src);
40 return false;
41 }
42 file_fp = dest ? fopen(dest, "w") : stdout;
43 if (!file_fp) {
44 fprintf(stderr, "Can't open %s for writing.\n", dest);
45 fclose(proc_fp);
46 return false;
47 }
48 while (true) {
49 const int c = fgetc(proc_fp);
50 if (ccs_network_mode && !c)
51 break;
52 if (c == EOF)
53 break;
54 if (fputc(c, file_fp) == EOF)
55 result = false;
56 }
57 fclose(proc_fp);
58 if (file_fp != stdout)
59 if (fclose(file_fp) == EOF)
60 result = false;
61 return result;
62 }
63
64 static const char *ccs_policy_dir = NULL;
65
66 static _Bool ccs_cat_file(const char *path)
67 {
68 FILE *fp = ccs_open_read(path);
69 _Bool result = true;
70 if (!fp) {
71 fprintf(stderr, "Can't open %s\n", path);
72 return false;
73 }
74 while (true) {
75 int c = fgetc(fp);
76 if (ccs_network_mode && !c)
77 break;
78 if (c == EOF)
79 break;
80 if (putchar(c) == EOF)
81 result = false;
82 }
83 fclose(fp);
84 return result;
85 }
86
87 static _Bool ccs_save_policy(void)
88 {
89 time_t now = time(NULL);
90 char stamp[32] = { };
91 while (1) {
92 struct tm *tm = localtime(&now);
93 snprintf(stamp, sizeof(stamp) - 1,
94 "%02d-%02d-%02d.%02d:%02d:%02d",
95 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
96 tm->tm_hour, tm->tm_min, tm->tm_sec);
97 if (access(stamp, F_OK))
98 break;
99 else if (errno == EEXIST)
100 now++;
101 else {
102 fprintf(stderr, "Can't create %s/policy/%s .\n",
103 ccs_policy_dir, stamp);
104 return false;
105 }
106 }
107 if (!ccs_move_proc_to_file(CCS_PROC_POLICY_POLICY, stamp) ||
108 (rename("current", "previous") && errno != ENOENT) ||
109 symlink(stamp, "current")) {
110 fprintf(stderr, "Failed to save policy.\n");
111 return false;
112 }
113 return true;
114 }
115
116 int main(int argc, char *argv[])
117 {
118 _Bool use_stdout = false;
119 int i;
120 for (i = 1; i < argc; i++) {
121 char *ptr = argv[i];
122 char *cp = strchr(ptr, ':');
123 if (*ptr == '/') {
124 if (ccs_policy_dir || use_stdout)
125 goto usage;
126 ccs_policy_dir = ptr;
127 } else if (cp) {
128 *cp++ = '\0';
129 ccs_network_ip = inet_addr(ptr);
130 ccs_network_port = htons(atoi(cp));
131 if (ccs_network_mode) {
132 fprintf(stderr, "You cannot specify multiple "
133 "%s at the same time.\n\n",
134 "remote agents");
135 goto usage;
136 }
137 ccs_network_mode = true;
138 } else if (*ptr++ == '-' && !*ptr) {
139 if (ccs_policy_dir || use_stdout)
140 goto usage;
141 use_stdout = true;
142 } else
143 goto usage;
144 }
145 if (ccs_network_mode)
146 ccs_check_remote_host(true);
147 else
148 ccs_check_policy_dir(true);
149 if (use_stdout)
150 return !ccs_cat_file(CCS_PROC_POLICY_POLICY);
151 if (!ccs_policy_dir)
152 ccs_policy_dir = "/etc/caitsith";
153 if (chdir(ccs_policy_dir) || chdir("policy/")) {
154 fprintf(stderr, "Directory %s/policy/ doesn't exist.\n",
155 ccs_policy_dir);
156 return 1;
157 }
158 return !ccs_save_policy();
159 usage:
160 printf("Usage: %s [policy_dir|-] [remote_ip:remote_port]\n\n"
161 "policy_dir : Use policy_dir rather than /etc/caitsith "
162 "directory.\n"
163 "- : Print policy to stdout rather than save as a file.\n"
164 "remote_ip:remote_port : Read from caitsith-agent listening at "
165 "remote_ip:remote_port .\n", argv[0]);
166 return 1;
167 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26