summaryrefslogtreecommitdiffstats
path: root/contrib/check_logins.c
blob: fa3ed177b89e20a6b027bee0d17faffeab70c11e (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*=================================
 *  check_logins - Nagios plugin
 *  Copyright (C) 2003  Dag Robøle
 *
 *  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 of the License, 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
 *
 *  Authors email: drobole@broadpark.no
 */
//=================================
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "config.h"
#include "common.h"
#include "utils.h"
#include "popen.h"
//=================================
#define REVISION		"$Revision$"
#define COPYRIGHT		"2003"
#define AUTHOR			"Dag Robole"
#define EMAIL 			"drobole@broadpark.no"
#define SUMMARY			"Check for multiple user logins"

#define check(func, errmsg)	{ if((func) == -1) die(STATE_UNKNOWN, errmsg); }
#define checkz(func, errmsg)	{ if(!(func)) die(STATE_UNKNOWN, errmsg); }
//=================================
typedef struct USERNODE_TYP {
	char *name;
	char *host;
	struct USERNODE_TYP* next;
} USERNODE;
//=================================
char *progname = NULL;
USERNODE *userlist = NULL, *adminlist = NULL;
int warning_limit = 0, critical_limit = 0;

void print_usage();
void print_help();
void process_arguments(int argc, char* *argv);
void parse_wholine(const char *line, char *name, char *host);
void node_create(USERNODE* *node, const char *name, const char *host);
void node_free(USERNODE* *node);
USERNODE* list_insert_sort_uniq(USERNODE* *list, USERNODE* *node);
void list_free(USERNODE* *list);
void cleanup();
//=================================
int main(int argc, char* *argv)
{
	FILE *who;
	USERNODE *newnode, *nptra, *nptrb;
	char buffer[BUFSIZ], username[BUFSIZ], hostname[BUFSIZ], *cptra, *cptrb;
	char max_login_name[BUFSIZ], currname[BUFSIZ];
	int max_login_count = 0, counter = 0, skip;
	void (*old_sig_alrm)();

	progname = argv[0];
	if(atexit(cleanup))
		die(STATE_UNKNOWN, "atexit failed\n");
	
	if((old_sig_alrm = signal((int)SIGALRM, timeout_alarm_handler)) == SIG_ERR)
		die(STATE_UNKNOWN, "signal failed\n");
	alarm(timeout_interval);

	process_arguments(argc, argv);
	
	checkz(who = spopen(PATH_TO_WHO), "spopen failed\n");
	
	while(fgets(buffer, sizeof(buffer), who) != NULL) {	
		parse_wholine(buffer, username, hostname);
		skip = 0;
		nptra = adminlist;
		
		while(nptra != NULL) {
			if(!strcmp(nptra->name, username)) {
				skip = 1;
				break;
			}
			nptra = nptra->next;
		}		
		if(!skip) {
			node_create(&newnode, username, hostname);
			if(!list_insert_sort_uniq(&userlist, &newnode))
				node_free(&newnode);
		}
	}
	
	check(spclose(who), "spclose failed\n");

	if(userlist != NULL) {
		nptra = userlist;
		strcpy(currname, nptra->name);
		strcpy(max_login_name, nptra->name);
		max_login_count = 1;
		while(nptra != NULL) {
			if(!strcmp(currname, nptra->name))
				++counter;
			else {
				if(counter > max_login_count) {
					max_login_count = counter;
					strcpy(max_login_name, currname);
				}
				strcpy(currname, nptra->name);
				counter = 1;
			}
			nptra = nptra->next;
		}
		
		if(counter > max_login_count) {
			max_login_count = counter;
			strcpy(max_login_name, currname);
		}
	}

	if(signal((int)SIGALRM, old_sig_alrm) == SIG_ERR)
		die(STATE_UNKNOWN, "signal failed\n");
		
	if(max_login_count) {
		if(critical_limit && max_login_count >= critical_limit) {
			printf("CRITICAL - User %s has logged in from %d different hosts\n", max_login_name, max_login_count);
			return STATE_CRITICAL;
		}
		else if(warning_limit && max_login_count >= warning_limit) {
			printf("WARNING - User %s has logged in from %d different hosts\n", max_login_name, max_login_count);
			return STATE_WARNING;
		}	
	}

	printf("OK - No users has exceeded the login limits\n");	
	return STATE_OK;
}
//=================================
void print_usage()
{
	fprintf(stderr, "Usage: %s [ -hV ] [ -w limit ] [ -c limit ] [ -u username1, ... ,usernameN ]\n", progname);
}
//=================================
void print_help()
{
	print_revision(progname, REVISION);
	printf("Copyright (c) %s %s <%s>\n\n%s\n\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
	print_usage();
	printf("\nDescription:\n"
	       "\tThis plugin supports the w (warning) and c (critical) options indicating the upper limits\n"
	       "\tof logins allowed before a warning is given.\n"
	       "\tThe output from %s is the username and number of login sessions for the user\n"
	       "\twho has the most login sessions (from different hosts) running at a given point in time.\n"
	       "\tThe u (users) option takes a comma separated list of usernames that will be ignored\n"
	       "\twhile scannig users.\n"
	       "\nOptions:\n"
	       "\t-h | --help\n\t\tShow this help message and exit\n"
	       "\t-V | --version\n\t\tShow version description\n"
	       "\t-w | --warning=INTEGER\n\t\tSet warning limit for logins (minimum value is 2)\n"
	       "\t-c | --critical=INTEGER\n\t\tSet critical limit for logins (minimum value is 2)\n"
	       "\t-u | --users=STRING\n\t\tSet usernames to be ignored\n"
	       "\nExamples:\n\t%s -w 3 -c 5\n"
	       "\t%s -w 3 -c 5 -u root,guest,jcarmack\n\n", progname, progname, progname);
}
//=================================
void process_arguments(int argc, char* *argv)
{	
	USERNODE *newnode;
	int optch;
	char buffer[BUFSIZ], *cptra;
	static struct option long_opts[] = {
		{"help", no_argument, 0, 'h'},
		{"version", no_argument, 0, 'V'},
		{"warning", required_argument, 0, 'w'},
		{"critical", required_argument, 0, 'c'},
		{"users", required_argument, 0, 'u'},
		{0, 0, 0, 0},
	};
	
	while((optch = getopt_long(argc, argv, "hVw:c:u:", long_opts, NULL)) != -1) {
		switch(optch) {
			case 'h':
				print_help();
				exit(STATE_OK);
				break;
			case 'V':
				print_revision(progname, REVISION);
				exit(STATE_OK);
				break;
			case 'w':
				if(!is_numeric(optarg)) {
					print_usage();
					die(STATE_UNKNOWN, "invalid options\n");
				}
				warning_limit = atoi(optarg) > 2 ? atoi(optarg) : 2;
				break;
			case 'c':
				if(!is_numeric(optarg)) {
					print_usage();
					die(STATE_UNKNOWN, "invalid options\n");
				}
				critical_limit = atoi(optarg) > 2 ? atoi(optarg) : 2;
				break;
			case 'u':
				strcpy(buffer, optarg);
				cptra = strtok(buffer, ",");
				while(cptra != NULL) {
					node_create(&newnode, cptra, "(adminhost)");
					list_insert_sort_uniq(&adminlist, &newnode);
					cptra = strtok(NULL, ",");
				}
				break;
			default:
				print_usage();
				exit(STATE_UNKNOWN);
				break;
		}
	}

	if(argc > optind) {
		print_usage();
		die(STATE_UNKNOWN, "invalid options\n");
	}

	if(!warning_limit && !critical_limit) {
		print_usage();
		die(STATE_UNKNOWN, "you must provide a limit for this plugin\n");
	}
	
	if(critical_limit && warning_limit > critical_limit) {
		print_usage();
		die(STATE_UNKNOWN, "warning limit must be less or equal critical limit\n");
	}
}
//=================================
void parse_wholine(const char *line, char *name, char *host)
{	
	char buffer[BUFSIZ], *cptra, *cptrb, *display;
	strcpy(buffer, line);

	cptra = buffer;
	checkz(cptrb = (char*)strchr(buffer, ' '), "strchr failed\n");
	strncpy(name, cptra, cptrb-cptra);
	name[cptrb-cptra] = '\0';

	if((cptra = strchr(buffer, '(')) != NULL) // hostname found in source arg...
	{
		if(cptra[1] == ':') // local host
			strcpy(host, "(localhost)");
		else // extern host
		{
			checkz(cptrb = strchr(cptra, ')'), "strchr failed\n");
			cptrb++;
			strncpy(host, cptra, cptrb-cptra);
			host[cptrb-cptra] = '\0';
		}
	}
	else // no hostname in source arg, look in line arg...
	{
		checkz(cptra = strtok(buffer, " \t\r\n"), "strtok failed\n"); 
		checkz(cptra = strtok(NULL, " \t\r\n"), "strtok failed\n");
		if(cptra[0] == ':') // local host
			strcpy(host, "(localhost)");
		else // extern host
			sprintf(host, "(%s)", cptra);
	}
	
	if((cptra = strchr(host, ':')) != NULL) // remove display if any...
		strcpy(cptra, ")");
}
//================================= 
void node_create(USERNODE* *node, const char *name, const char *host)
{	
	checkz(*node = (USERNODE*)malloc(sizeof(USERNODE)), "malloc failed\n");
	checkz((*node)->name = (char*)malloc(strlen(name)+1), "malloc failed\n");
	checkz((*node)->host = (char*)malloc(strlen(host)+1), "malloc failed\n");
	(*node)->next = NULL;
	strcpy((*node)->name, name);
	strcpy((*node)->host, host);
}
//================================= 
void node_free(USERNODE* *node)
{	
	free((*node)->name);
	free((*node)->host);
	free(*node);
	*node = NULL;
}
//================================= 
USERNODE* list_insert_sort_uniq(USERNODE* *list, USERNODE* *node)
{
	char n1[BUFSIZ], n2[BUFSIZ];
	USERNODE *last_nptr = NULL, *nptr = *list;
	
	if(*list == NULL)
		return(*list = *node);
	else {
		sprintf(n1, "%s %s", (*node)->name, (*node)->host);
		while(nptr != NULL) {
			sprintf(n2, "%s %s", nptr->name, nptr->host);
			if(!strcmp(n1, n2))
				return NULL;
			else if(strcmp(n1, n2) < 0) {
				if(last_nptr) {
					last_nptr->next = *node;
					(*node)->next = nptr;
				}
				else {
					(*node)->next = *list;
					*list = *node;
				}
				break;
			}
			else {
				last_nptr = nptr;
				nptr = nptr->next;
			}
		}
		if(nptr == NULL)
			last_nptr->next = *node;
	}
	return *node;
}
//================================= 
void list_free(USERNODE* *list)
{	
	USERNODE *doe, *nptr = *list;
	while(nptr != NULL) {
		doe = nptr;
		nptr = nptr->next;
		node_free(&doe);
	}
	*list = NULL;
}
//=================================
void cleanup()
{
	list_free(&userlist);
	list_free(&adminlist);
}
//=================================