source: trunk/src/sh_log_parse_pacct.c@ 362

Last change on this file since 362 was 292, checked in by katerina, 14 years ago

Fix for compile problems on Solaris 10 (ticket #220).

File size: 7.6 KB
Line 
1/**************************************
2 **
3 ** PARSER RULES
4 **
5 ** (a) must set record->host
6 ** (eventually to dummy value)
7 **
8 ** (b) must set record->prefix
9 ** (command)
10 **
11 **
12 **************************************/
13
14/* Based on the GNU Accounting Utilities, which is distributed with the
15 * following copyright:
16 */
17
18/* Copyright (C) 1993, 1996, 1997, 2003, 2005 Free Software Foundation, Inc.
19 *
20 * This file is part of the GNU Accounting Utilities
21 *
22 * The GNU Accounting Utilities are free software; you can redistribute
23 * them and/or modify them under the terms of the GNU General Public
24 * License as published by the Free Software Foundation; either version
25 * 2, or (at your option) any later version.
26 *
27 * The GNU Accounting Utilities are distributed in the hope that they will
28 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
29 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with the GNU Accounting Utilities; see the file COPYING. If
34 * not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
35 * MA 02139, USA. */
36
37#include "config_xor.h"
38
39#include <ctype.h>
40#include <string.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <unistd.h>
46#include <pwd.h>
47#include <dirent.h>
48
49#if defined(USE_LOGFILE_MONITOR) && defined(HAVE_SYS_ACCT_H)
50
51#include <sys/acct.h>
52
53#include "samhain.h"
54#include "sh_pthread.h"
55#include "sh_log_check.h"
56#include "sh_utils.h"
57#include "sh_string.h"
58
59#undef FIL__
60#define FIL__ _("sh_log_parse_pacct.c")
61
62extern int flag_err_debug;
63
64#ifndef ACCT_COMM
65#define ACCT_COMM 16
66#endif
67#ifndef AHZ
68#define AHZ 100
69#endif
70
71#if defined(ACUTIME_COMPT) || defined(ACSTIME_COMPT) || defined(ACETIME_COMPT)
72static double comp_t_2_double (comp_t ct)
73{
74 unsigned long out = 0;
75
76 out = ct & 017777;
77 ct >>= 13;
78
79 while (ct) {
80 ct--;
81 out <<= 3;
82 }
83
84 return (double) out;
85}
86#endif
87
88#ifdef ACUTIME_COMPT
89# define ACUTIME_2_DOUBLE(x) (comp_t_2_double(x))
90#else
91# define ACUTIME_2_DOUBLE(x) ((double)(x))
92#endif
93
94#ifdef ACSTIME_COMPT
95# define ACSTIME_2_DOUBLE(x) (comp_t_2_double(x))
96#else
97# define ACSTIME_2_DOUBLE(x) ((double)(x))
98#endif
99
100#ifdef ACETIME_COMPT
101# define ACETIME_2_DOUBLE(x) (comp_t_2_double(x))
102#else
103# define ACETIME_2_DOUBLE(x) ((double)(x))
104#endif
105
106
107static void expand_flags(char flag, char * out)
108{
109 int i = 0;
110
111#define BIT(flg, ch) if (flag & flg) out[i] = ch; else out[i] = ' '; ++i
112
113 BIT(ASU, 'S');
114 BIT(AFORK, 'F');
115#ifdef ACOMPAT
116 BIT(ACOMPAT, 'C');
117#endif
118#ifdef ACORE
119 BIT(ACORE, 'D');
120#endif
121#ifdef AXSIG
122 BIT(AXSIG, 'X');
123#endif
124
125 out[i] = '\0';
126 return;
127}
128
129static char * uid_name (int uid)
130{
131 static int userid = 0;
132 static char user[16] = "";
133
134 if (uid == userid && user[0] != '\0')
135 {
136 return user;
137 }
138 else
139 {
140 struct passwd *thispw = getpwuid (uid);
141 if (thispw)
142 sl_strlcpy (user, thispw->pw_name, sizeof(user));
143 else
144 sl_snprintf(user, sizeof(user), "%d", uid);
145 user[sizeof(user)-1] = '\0';
146 userid = uid;
147 }
148 return user;
149}
150
151struct dev_struct {
152 char * device;
153 long dev_id;
154 struct dev_struct * next;
155};
156static struct dev_struct * devicelist = NULL;
157
158static void add_devices(const char * dir)
159{
160 DIR * mdir;
161 char dirl[256];
162
163 sl_strlcpy(dirl, dir, sizeof(dirl));
164 dirl[sizeof(dirl)-1] = '\0';
165
166 mdir = opendir(dir);
167
168 if (mdir)
169 {
170 char * path;
171 size_t len;
172 struct dirent * dent;
173 struct stat buf;
174
175 while (NULL != (dent = readdir(mdir)))
176 {
177 if (0 == strcmp(dent->d_name, "."))
178 continue;
179 if (0 == strcmp(dent->d_name, ".."))
180 continue;
181 len = strlen(dir) + strlen(dent->d_name) + 2;
182 path = SH_ALLOC(len);
183 snprintf(path, len, "%s/%s", dir, dent->d_name);
184 if (0 == lstat(path, &buf) && S_ISCHR(buf.st_mode))
185 {
186 struct dev_struct * dstruct;
187 dstruct = SH_ALLOC(sizeof(struct dev_struct));
188 /* eliminate leading '/dev/' */
189 memmove(path, &path[5], strlen(path)-4);
190 dstruct->device = path;
191 dstruct->dev_id = buf.st_rdev;
192 dstruct->next = devicelist;
193 devicelist = dstruct;
194 }
195 else
196 {
197 SH_FREE(path);
198 }
199 }
200 closedir(mdir);
201 }
202 return;
203}
204
205static char * dev_name(long tty)
206{
207 struct dev_struct * dstruct;
208
209 if (!devicelist)
210 {
211 add_devices("/dev");
212 add_devices("/dev/pts");
213 add_devices("/dev/pty");
214 add_devices("/dev/ptym");
215 }
216
217 dstruct = devicelist;
218 while (dstruct)
219 {
220 if (dstruct->dev_id == tty)
221 return dstruct->device;
222 dstruct = dstruct->next;
223 }
224 return "??";
225}
226
227/* This looks strange, but it's real ANSI C. */
228extern struct acct pacct_rd_never_used;
229#define COMM_LEN ((int) sizeof (pacct_rd_never_used.ac_comm))
230
231sh_string * sh_read_pacct (sh_string * record, struct sh_logfile * logfile)
232{
233#if defined(__linux__) && defined(HAVE_ACCT_V3)
234# define STRUCT_ACCT struct acct_v3
235#else
236# define STRUCT_ACCT struct acct
237#endif
238
239 STRUCT_ACCT rec;
240
241 if (NULL != sh_binary_reader ((void*) &rec, sizeof(STRUCT_ACCT), logfile))
242 {
243 time_t btime = (time_t) rec.ac_btime;
244 double ut = ACUTIME_2_DOUBLE (rec.ac_utime);
245 double st = ACSTIME_2_DOUBLE (rec.ac_stime);
246 char fl[6];
247 char comm[COMM_LEN+1];
248 int i;
249 char out[64+COMM_LEN+1+5+8+8+32+4+19+7]; /* see printf format below */
250
251 expand_flags(rec.ac_flag, fl);
252
253 /* ac_comm may not be null terminated
254 */
255 for (i = 0; i < COMM_LEN; i++)
256 {
257 if (rec.ac_comm[i] == '\0')
258 {
259 comm[i] = '\0';
260 break;
261 }
262 if (! isprint (rec.ac_comm[i]))
263 comm[i] = '?';
264 else
265 comm[i] = rec.ac_comm[i];
266 }
267 comm[COMM_LEN] = '\0';
268
269 sl_snprintf (out, sizeof(out),
270 "%ld:%-*.*s %5.5s %-8.8s %-8.8s %6.2f secs %-19.19s",
271 btime,
272 COMM_LEN, COMM_LEN, comm, fl,
273 uid_name(rec.ac_uid),
274 dev_name((long)rec.ac_tty),
275 ((ut + st) / (double) AHZ),
276 ctime (&btime));
277
278
279 sh_string_set_from_char(record, out);
280 return record;
281 }
282
283 if (record)
284 sh_string_destroy(&record);
285 return NULL;
286}
287
288static void * sh_dummy_record = NULL;
289
290struct sh_logrecord * sh_parse_pacct (sh_string * logline, void * fileinfo)
291{
292 char * p;
293 char * endptr;
294 unsigned long ltime;
295 struct sh_logrecord * record = NULL;
296
297 (void) fileinfo;
298
299 sh_dummy_record = (void *) &record;
300
301 if (sh_string_len(logline) > 0 && flag_err_debug == SL_TRUE)
302 {
303 SH_MUTEX_LOCK(mutex_thread_nolog);
304 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
305 sh_string_str(logline),
306 _("sh_parse_pacct"));
307 SH_MUTEX_UNLOCK(mutex_thread_nolog);
308 }
309
310 p = strchr(sh_string_str(logline), ':');
311
312 if (!p || p == sh_string_str(logline))
313 return NULL;
314
315 ltime = strtoul(sh_string_str(logline), &endptr, 10);
316 if (p != endptr)
317 return NULL;
318
319 ++p; /* points to first char of pacct record */
320
321 if (*p != '\0')
322 {
323 size_t lengths[7];
324 unsigned int fields = 7;
325 char ** array;
326 sh_string * message = sh_string_new_from_lchar(p, strlen(p));
327 array = split_array_ws(p, &fields, lengths);
328
329 if (fields == 7)
330 {
331 record = SH_ALLOC(sizeof(struct sh_logrecord));
332
333 record->timestamp = ltime;
334 record->timestr = sh_string_new_from_lchar(array[6], lengths[6]);
335 record->message = message;
336 record->pid = 0;
337 record->host = sh_string_new_from_lchar(sh.host.name, strlen(sh.host.name));
338 }
339 else
340 {
341 sh_string_destroy(&message);
342 }
343 SH_FREE(array);
344 }
345 return record;
346}
347/* USE_LOGFILE_MONITOR */
348#endif
Note: See TracBrowser for help on using the repository browser.