source: trunk/src/sh_log_parse_pacct.c@ 590

Last change on this file since 590 was 590, checked in by katerina, 26 hours ago

Fix for ticket #478 (cppcheck warnings).

File size: 7.8 KB
RevLine 
[183]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
[292]118#ifdef ACORE
[183]119 BIT(ACORE, 'D');
[292]120#endif
121#ifdef AXSIG
[183]122 BIT(AXSIG, 'X');
[292]123#endif
[183]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
[203]227#if defined(__linux__) && defined(HAVE_ACCT_V3)
[183]228# define STRUCT_ACCT struct acct_v3
[384]229#elif defined(__FreeBSD__) && defined(HAVE_ACCTV2)
230# define STRUCT_ACCT struct acctv2
[183]231#else
232# define STRUCT_ACCT struct acct
233#endif
234
[384]235/* This looks strange, but it's real ANSI C. */
236extern STRUCT_ACCT pacct_rd_never_used;
237#define COMM_LEN ((int) sizeof (pacct_rd_never_used.ac_comm))
238
239sh_string * sh_read_pacct (sh_string * record, struct sh_logfile * logfile)
240{
[183]241 STRUCT_ACCT rec;
242
243 if (NULL != sh_binary_reader ((void*) &rec, sizeof(STRUCT_ACCT), logfile))
244 {
245 time_t btime = (time_t) rec.ac_btime;
246 double ut = ACUTIME_2_DOUBLE (rec.ac_utime);
247 double st = ACSTIME_2_DOUBLE (rec.ac_stime);
248 char fl[6];
249 char comm[COMM_LEN+1];
250 int i;
251 char out[64+COMM_LEN+1+5+8+8+32+4+19+7]; /* see printf format below */
252
[384]253#if defined(ac_flagx)
[590]254 /* cppcheck-suppress syntaxError */
[384]255 expand_flags(rec.ac_flagx, fl);
256#else
257 expand_flags(rec.ac_flag, fl);
258#endif
[183]259
260 /* ac_comm may not be null terminated
261 */
262 for (i = 0; i < COMM_LEN; i++)
263 {
264 if (rec.ac_comm[i] == '\0')
265 {
266 comm[i] = '\0';
267 break;
268 }
269 if (! isprint (rec.ac_comm[i]))
270 comm[i] = '?';
271 else
272 comm[i] = rec.ac_comm[i];
273 }
274 comm[COMM_LEN] = '\0';
275
276 sl_snprintf (out, sizeof(out),
277 "%ld:%-*.*s %5.5s %-8.8s %-8.8s %6.2f secs %-19.19s",
278 btime,
279 COMM_LEN, COMM_LEN, comm, fl,
280 uid_name(rec.ac_uid),
281 dev_name((long)rec.ac_tty),
282 ((ut + st) / (double) AHZ),
283 ctime (&btime));
284
285
286 sh_string_set_from_char(record, out);
287 return record;
288 }
289
[185]290 if (record)
291 sh_string_destroy(&record);
[183]292 return NULL;
293}
294
[481]295void * sh_dummy_294_record = NULL;
[199]296
[183]297struct sh_logrecord * sh_parse_pacct (sh_string * logline, void * fileinfo)
298{
299 char * p;
300 char * endptr;
301 unsigned long ltime;
302 struct sh_logrecord * record = NULL;
303
304 (void) fileinfo;
305
[481]306 sh_dummy_294_record = (void *) &record;
[199]307
[481]308 if (sh_string_len(logline) > 0 && flag_err_debug == S_TRUE)
[183]309 {
310 SH_MUTEX_LOCK(mutex_thread_nolog);
311 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
312 sh_string_str(logline),
313 _("sh_parse_pacct"));
314 SH_MUTEX_UNLOCK(mutex_thread_nolog);
315 }
316
[588]317 sh_dummy_294_record = NULL;
318
[183]319 p = strchr(sh_string_str(logline), ':');
320
321 if (!p || p == sh_string_str(logline))
322 return NULL;
323
324 ltime = strtoul(sh_string_str(logline), &endptr, 10);
325 if (p != endptr)
326 return NULL;
327
328 ++p; /* points to first char of pacct record */
329
330 if (*p != '\0')
331 {
332 size_t lengths[7];
333 unsigned int fields = 7;
334 char ** array;
335 sh_string * message = sh_string_new_from_lchar(p, strlen(p));
336 array = split_array_ws(p, &fields, lengths);
337
338 if (fields == 7)
339 {
340 record = SH_ALLOC(sizeof(struct sh_logrecord));
341
342 record->timestamp = ltime;
343 record->timestr = sh_string_new_from_lchar(array[6], lengths[6]);
344 record->message = message;
345 record->pid = 0;
346 record->host = sh_string_new_from_lchar(sh.host.name, strlen(sh.host.name));
347 }
348 else
349 {
350 sh_string_destroy(&message);
351 }
352 SH_FREE(array);
353 }
354 return record;
355}
356/* USE_LOGFILE_MONITOR */
357#endif
Note: See TracBrowser for help on using the repository browser.