File: snoopy.c

package info (click to toggle)
snoopy 1.3-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 104 kB
  • ctags: 16
  • sloc: ansic: 92; sh: 59; makefile: 57
file content (120 lines) | stat: -rw-r--r-- 3,172 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* snoopy.c -- execve() logging wrapper 
 * Copyright (c) 2000 marius@linux.com,mbm@linux.com
 * Version 1.3
 *
 * $Id: snoopy.c,v 1.32 2000/12/21 06:53:03 marius Exp $
 *
 * Part hacked on flight KL 0617, 30,000 ft or so over the Atlantic :) 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <sys/types.h>
#include <dlfcn.h>
#include <syslog.h>
#include "snoopy.h"

#define min(a,b) a<b?a:b

#if defined(RTLD_NEXT)
#  define REAL_LIBC RTLD_NEXT
#else
#  define REAL_LIBC ((void *) -1L)
#endif

#define FN(ptr,type,name,args)  ptr = (type (*)args)dlsym (REAL_LIBC, name)

static inline void log(const char *filename, char **argv) {
	static char *logstring=NULL; 
	static int argc, size=0;
	register int i, spos=0;
	#if INTEGRITY_CHECK
	static char **argv_copy;
	static int *t_size;
	#endif

	#if ROOT_ONLY
	if (getuid() != 0) 
	   	return; 
	#endif

	#if IGNORE_NULL
	if (getlogin() == 0)
		return;
	#endif

	for(argc=0; *(argv+argc)!='\0';argc++);

	#if INTEGRITY_CHECK
	argv_copy = (char**)malloc(sizeof(char*)*argc);
	t_size = (int*)malloc(sizeof(int)*argc);
	for(i=0; i<argc; i++) {
	    size = sizeof(char)*strlen(*(argv+i));
		*(t_size+i) = size;
		*(argv_copy+i) = (char*)malloc(size);
		memcpy(*(argv_copy+i), *(argv+i), size);
	}
	*(argv_copy+argc) = '\0';
	size=0;
	#endif

	openlog("snoopy", LOG_PID, LOG_AUTHPRIV);

	#if MAX
	logstring = (char *)malloc(sizeof(char)*MAX*argc);
	for(i=0; i<argc; i++) 
	   spos += min(snprintf(logstring+spos, MAX, "%s ", *(argv+i)), MAX);
	#else
	for(i=0; i<argc; i++)
	    size += sizeof(char)*strlen(*(argv+i))+1;
	size++; /*make space for that \0*/
	logstring = (char*)malloc(sizeof(char)*size);

	for(i=0; i<argc; i++)
	   spos += sprintf(logstring+spos, "%s ", *(argv+i));
	#endif

	#if INTEGRITY_CHECK
	for(i=0; i<argc; i++)
		if(memcmp(*(argv+i), *(argv_copy+i), *(t_size+i)))
		   syslog(LOG_ERR, "Integrity check failed");

	for(i=0; i<argc; i++)
	   free(*(argv_copy+i));
	#endif

	syslog(LOG_INFO, "[%s, uid:%d sid:%d]: %s", getlogin(), getuid(), getsid(0), logstring); 
	free(logstring);

}

int execve(const char *filename, char **argv, char **envp) {
	static int (*func)(const char *, char **, char **);

	FN(func,int,"execve",(const char *, char **, char **));
	log(filename, argv);

	return (*func) (filename, argv, envp);
}

int execv(const char *filename, char **argv) {
	static int (*func)(const char *, char **);

	FN(func,int,"execv",(const char *, char **));
	log(filename, argv);

	return (*func) (filename, argv);
}