source: branches/samhain-2_2-branch/src/sh_prelink.c@ 502

Last change on this file since 502 was 34, checked in by rainer, 19 years ago

Code cleanup and minor fixes

File size: 6.9 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2004 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <string.h>
23#include <sys/types.h>
24#include <signal.h>
25
26#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
27
28#include "samhain.h"
29#include "sh_tiger.h"
30#include "sh_extern.h"
31#include "sh_utils.h"
32#include "sh_unix.h"
33
34#undef FIL__
35#define FIL__ _("sh_prelink.c")
36
37static char * prelink_path = NULL;
38static char * prelink_hash = NULL;
39
40int sh_prelink_set_path (const char * str)
41{
42 SL_ENTER(_("sh_prelink_set_path"));
43 if (prelink_path != NULL)
44 SH_FREE(prelink_path);
45 if (str[0] != '/')
46 {
47 prelink_path = NULL;
48 SL_RETURN((-1), _("sh_prelink_set_path"));
49 }
50
51 prelink_path = sh_util_strdup(str);
52
53 SL_RETURN(0, _("sh_prelink_set_path"));
54}
55
56int sh_prelink_set_hash (const char * str)
57{
58 size_t len;
59 SL_ENTER(_("sh_prelink_set_hash"));
60 if (prelink_hash != NULL)
61 SH_FREE(prelink_hash);
62 len = sl_strlen (str);
63 if (len != KEY_LEN)
64 {
65 prelink_hash = NULL;
66 SL_RETURN((-1), _("sh_prelink_set_hash"));
67 }
68 prelink_hash = SH_ALLOC(len+1);
69 (void) sl_strlcpy(prelink_hash, str, len+1);
70 SL_RETURN(0, _("sh_prelink_set_hash"));
71}
72
73int sh_prelink_iself (SL_TICKET fd, off_t size, int alert_timeout)
74{
75 long status;
76 char magic[4];
77 if (size < 42)
78 return S_FALSE;
79 status = sl_read_timeout (fd, magic, 4, alert_timeout);
80 (void) sl_rewind(fd);
81 if (status == 4)
82 {
83 /*@-usedef@*/
84 if (magic[0] == (char) 0x7f &&
85 magic[1] == 'E' &&
86 magic[2] == 'L' &&
87 magic[3] == 'F')
88 return S_TRUE;
89 /*@+usedef@*/
90 }
91 return S_FALSE;
92}
93
94extern int get_the_fd (SL_TICKET ticket);
95
96static void sh_prelink_fd(sh_tas_t * task)
97{
98 SL_TICKET ticket;
99 char * tmp;
100
101 if (task->com_ti != (-1))
102 {
103 (void) sl_close(task->com_ti);
104 task->com_fd = -1;
105 task->com_ti = -1;
106 }
107 ticket = sl_open_read(task->command,
108 task->privileged == 0 ? SL_NOPRIV : SL_YESPRIV);
109 if (SL_ISERROR(ticket))
110 {
111 tmp = sh_util_safe_name (task->command);
112 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, ticket, MSG_E_READ, tmp);
113 SH_FREE(tmp);
114 return;
115 }
116 tiger_fd = ticket;
117 if (*(task->checksum) == '\0' ||
118 0 == sl_strcmp(task->checksum,
119 sh_tiger_hash (task->command, TIGER_FD, 0)))
120 {
121 task->com_fd = get_the_fd(ticket);
122 task->com_ti = ticket;
123 }
124 else
125 {
126 tmp = sh_util_safe_name (task->command);
127 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, ticket, MSG_E_HASH, tmp);
128 SH_FREE(tmp);
129 (void) sl_close(ticket);
130 }
131 return;
132}
133
134/* returns static storage
135 */
136int sh_prelink_run (char * path, char * file_hash, int alert_timeout)
137{
138 static int init = S_FALSE;
139 static int args_filled = 0;
140 static sh_tas_t task;
141
142 int status = 0;
143 char * p;
144 struct sigaction new_act;
145 struct sigaction old_act;
146
147 SL_ENTER(_("sh_prelink_run"));
148
149 /* reset if path == NULL
150 */
151 if (path == NULL)
152 {
153 if (init == S_FALSE)
154 {
155 SL_RETURN (0, _("sh_prelink_run"));
156 }
157 sh_ext_tas_free(&task);
158 init = S_FALSE;
159 args_filled = 0;
160 SL_RETURN (0, _("sh_prelink_run"));
161 }
162
163 /* initialize task structure
164 */
165 if (init == S_FALSE)
166 {
167 sh_ext_tas_init(&task);
168 p = sh_unix_getUIDdir (SH_ERR_ERR, task.run_user_uid);
169 if (p)
170 {
171 (void) sh_ext_tas_add_envv (&task, _("HOME"), p);
172 }
173 (void) sh_ext_tas_add_envv (&task, _("SHELL"),
174 _("/bin/sh"));
175 (void) sh_ext_tas_add_envv (&task, _("PATH"),
176 _("/sbin:/usr/sbin:/bin:/usr/bin"));
177 if (sh.timezone != NULL)
178 {
179 (void) sh_ext_tas_add_envv(&task, "TZ", sh.timezone);
180 }
181 if (prelink_path == NULL)
182 {
183 sh_ext_tas_command(&task, _("/usr/sbin/prelink"));
184 (void) sh_ext_tas_add_argv(&task, _("/usr/sbin/prelink"));
185 }
186 else
187 {
188 sh_ext_tas_command(&task, prelink_path);
189 (void) sh_ext_tas_add_argv(&task, prelink_path);
190 }
191 args_filled = sh_ext_tas_add_argv(&task, _("--verify"));
192
193 if (prelink_hash != NULL)
194 {
195 (void) sl_strlcpy(task.checksum, prelink_hash, KEY_LEN+1);
196 }
197 task.rw = 'r';
198 task.fork_twice = S_FALSE;
199
200 sh_prelink_fd(&task);
201
202 init = S_TRUE;
203 }
204
205 /* rm filename arg if set; fill in filename
206 */
207 if (args_filled == 3)
208 args_filled = sh_ext_tas_rm_argv(&task);
209 if (args_filled == 2)
210 args_filled = sh_ext_tas_add_argv(&task, path);
211 else
212 {
213 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, args_filled, MSG_E_SUBGEN,
214 _("Bad argument count"), _("sh_prelink_run"));
215 SL_RETURN ((-1), _("sh_prelink_run"));
216 }
217
218 /* open pipe
219 */
220 status = sh_ext_popen(&task);
221 if (status != 0)
222 {
223 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
224 _("Could not open pipe"), _("sh_prelink_run"));
225 SL_RETURN ((-1), _("sh_prelink_run"));
226 }
227
228 if (SL_ISERROR(task.pipeTI))
229 {
230 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, task.pipeTI, MSG_E_SUBGEN,
231 _("No valid ticket"), _("sh_prelink_run"));
232 SL_RETURN ((-1), _("sh_prelink_run"));
233 }
234
235 /* ignore SIGPIPE (instead get EPIPE if connection is closed)
236 */
237 new_act.sa_handler = SIG_IGN;
238 (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &new_act, &old_act);
239
240 /* read from pipe
241 */
242 tiger_fd = task.pipeTI;
243
244 sl_read_timeout_prep (task.pipeTI);
245
246 sl_strlcpy(file_hash,
247 sh_tiger_generic_hash (path, TIGER_FD, 0, alert_timeout),
248 KEY_LEN+1);
249
250 /* restore old signal handler
251 */
252 (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &old_act, NULL);
253
254 /* close pipe and return exit status
255 */
256 status = sh_ext_pclose(&task);
257 SL_RETURN ((status), _("sh_prelink_run"));
258}
259/* defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
260 */
261#endif
Note: See TracBrowser for help on using the repository browser.