File: cgiauth.c

package info (click to toggle)
nagios4 4.4.6-4.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,336 kB
  • sloc: ansic: 97,631; sh: 12,619; javascript: 5,483; perl: 2,624; makefile: 1,265; php: 381; ruby: 95
file content (605 lines) | stat: -rw-r--r-- 19,787 bytes parent folder | download | duplicates (3)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*****************************************************************************
 *
 * CGIAUTH.C - Authorization utilities for Nagios CGIs
 *
 *
 * License:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *****************************************************************************/

#include "../include/config.h"
#include "../include/common.h"
#include "../include/objects.h"

#include "../include/cgiutils.h"
#include "../include/cgiauth.h"

extern char            main_config_file[MAX_FILENAME_LENGTH];

extern int             use_authentication;
extern int             use_ssl_authentication;



/* get current authentication information */
int get_authentication_information(authdata *authinfo) {
	mmapfile *thefile;
	char *input = NULL;
	char *temp_ptr = NULL;
	contact *temp_contact = NULL;
	contactgroup *temp_contactgroup = NULL;

	if(authinfo == NULL)
		return ERROR;

	/* initial values... */
	authinfo->authorized_for_all_hosts = FALSE;
	authinfo->authorized_for_all_host_commands = FALSE;
	authinfo->authorized_for_all_services = FALSE;
	authinfo->authorized_for_all_service_commands = FALSE;
	authinfo->authorized_for_system_information = FALSE;
	authinfo->authorized_for_system_commands = FALSE;
	authinfo->authorized_for_configuration_information = FALSE;
	authinfo->authorized_for_read_only = FALSE;

	/* grab username from the environment... */
	if(use_ssl_authentication) {
		/* patch by Pawl Zuzelski - 7/22/08 */
		temp_ptr = getenv("SSL_CLIENT_S_DN_CN");
		}
	else {
		temp_ptr = getenv("REMOTE_USER");
		}
	if(temp_ptr == NULL) {
		authinfo->username = "";
		authinfo->authenticated = FALSE;
		}
	else {
		authinfo->username = (char *)malloc(strlen(temp_ptr) + 1);
		if(authinfo->username == NULL)
			authinfo->username = "";
		else
			strcpy(authinfo->username, temp_ptr);
		if(!strcmp(authinfo->username, ""))
			authinfo->authenticated = FALSE;
		else
			authinfo->authenticated = TRUE;
		}

	/* read in authorization override vars from config file... */
	if((thefile = mmap_fopen(get_cgi_config_location())) != NULL) {

		while(1) {

			/* free memory */
			free(input);

			/* read the next line */
			if((input = mmap_fgets_multiline(thefile)) == NULL)
				break;

			strip(input);

			/* we don't have a username yet, so fake the authentication if we find a default username defined */
			if(!strcmp(authinfo->username, "") && strstr(input, "default_user_name=") == input) {
				temp_ptr = strtok(input, "=");
				temp_ptr = strtok(NULL, ",");
				authinfo->username = (char *)malloc(strlen(temp_ptr) + 1);
				if(authinfo->username == NULL)
					authinfo->username = "";
				else
					strcpy(authinfo->username, temp_ptr);
				if(!strcmp(authinfo->username, ""))
					authinfo->authenticated = FALSE;
				else
					authinfo->authenticated = TRUE;
				}

			else if(strstr(input, "authorized_for_all_hosts=") == input) {
				temp_ptr = strtok(input, "=");
				while((temp_ptr = strtok(NULL, ","))) {
					if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*"))
						authinfo->authorized_for_all_hosts = TRUE;
					}
				}
			else if(strstr(input, "authorized_for_all_services=") == input) {
				temp_ptr = strtok(input, "=");
				while((temp_ptr = strtok(NULL, ","))) {
					if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*"))
						authinfo->authorized_for_all_services = TRUE;
					}
				}
			else if(strstr(input, "authorized_for_system_information=") == input) {
				temp_ptr = strtok(input, "=");
				while((temp_ptr = strtok(NULL, ","))) {
					if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*"))
						authinfo->authorized_for_system_information = TRUE;
					}
				}
			else if(strstr(input, "authorized_for_configuration_information=") == input) {
				temp_ptr = strtok(input, "=");
				while((temp_ptr = strtok(NULL, ","))) {
					if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*"))
						authinfo->authorized_for_configuration_information = TRUE;
					}
				}
			else if(strstr(input, "authorized_for_all_host_commands=") == input) {
				temp_ptr = strtok(input, "=");
				while((temp_ptr = strtok(NULL, ","))) {
					if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*"))
						authinfo->authorized_for_all_host_commands = TRUE;
					}
				}
			else if(strstr(input, "authorized_for_all_service_commands=") == input) {
				temp_ptr = strtok(input, "=");
				while((temp_ptr = strtok(NULL, ","))) {
					if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*"))
						authinfo->authorized_for_all_service_commands = TRUE;
					}
				}
			else if(strstr(input, "authorized_for_system_commands=") == input) {
				temp_ptr = strtok(input, "=");
				while((temp_ptr = strtok(NULL, ","))) {
					if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*"))
						authinfo->authorized_for_system_commands = TRUE;
					}
				}
			else if(strstr(input, "authorized_for_read_only=") == input) {
				temp_ptr = strtok(input, "=");
				while((temp_ptr = strtok(NULL, ","))) {
					if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*"))
						authinfo->authorized_for_read_only = TRUE;
					}
				}
			else if((temp_contact = find_contact(authinfo->username)) != NULL) {
				if(strstr(input, "authorized_contactgroup_for_all_hosts=") == input) {
					temp_ptr = strtok(input, "=");
					while((temp_ptr = strtok(NULL, ","))) {
						temp_contactgroup = find_contactgroup(temp_ptr);
						if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact))
							authinfo->authorized_for_all_hosts = TRUE;
						}
					}
				else if(strstr(input, "authorized_contactgroup_for_all_services=") == input) {
					temp_ptr = strtok(input, "=");
					while((temp_ptr = strtok(NULL, ","))) {
						temp_contactgroup = find_contactgroup(temp_ptr);
						if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact))
							authinfo->authorized_for_all_services = TRUE;
						}
					}
				else if(strstr(input, "authorized_contactgroup_for_system_information=") == input) {
					temp_ptr = strtok(input, "=");
					while((temp_ptr = strtok(NULL, ","))) {
						temp_contactgroup = find_contactgroup(temp_ptr);
						if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact))
							authinfo->authorized_for_system_information = TRUE;
						}
					}
				else if(strstr(input, "authorized_contactgroup_for_configuration_information=") == input) {
					temp_ptr = strtok(input, "=");
					while((temp_ptr = strtok(NULL, ","))) {
						temp_contactgroup = find_contactgroup(temp_ptr);
						if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact))
							authinfo->authorized_for_configuration_information = TRUE;
						}
					}
				else if(strstr(input, "authorized_contactgroup_for_all_host_commands=") == input) {
					temp_ptr = strtok(input, "=");
					while((temp_ptr = strtok(NULL, ","))) {
						temp_contactgroup = find_contactgroup(temp_ptr);
						if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact))
							authinfo->authorized_for_all_host_commands = TRUE;
						}
					}
				else if(strstr(input, "authorized_contactgroup_for_all_service_commands=") == input) {
					temp_ptr = strtok(input, "=");
					while((temp_ptr = strtok(NULL, ","))) {
						temp_contactgroup = find_contactgroup(temp_ptr);
						if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact))
							authinfo->authorized_for_all_service_commands = TRUE;
						}
					}
				else if(strstr(input, "authorized_contactgroup_for_system_commands=") == input) {
					temp_ptr = strtok(input, "=");
					while((temp_ptr = strtok(NULL, ","))) {
						temp_contactgroup = find_contactgroup(temp_ptr);
						if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact))
							authinfo->authorized_for_system_commands = TRUE;
						}
					}
				else if(strstr(input, "authorized_contactgroup_for_read_only=") == input) {
					temp_ptr = strtok(input, "=");
					while((temp_ptr = strtok(NULL, ","))) {
						temp_contactgroup = find_contactgroup(temp_ptr);
						if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact))
							authinfo->authorized_for_read_only = TRUE;
						}
					}
				}
			}

		/* free memory and close the file */
		free(input);
		mmap_fclose(thefile);
		}

	if(authinfo->authenticated == TRUE)
		return OK;
	else
		return ERROR;
	}



/* check if user is authorized to view information about a particular host */
int is_authorized_for_host(host *hst, authdata *authinfo) {
	contact *temp_contact;

	if(hst == NULL)
		return FALSE;

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	/* if this user is authorized for all hosts, they are for this one... */
	if(is_authorized_for_all_hosts(authinfo) == TRUE)
		return TRUE;

	/* find the contact */
	temp_contact = find_contact(authinfo->username);

	/* see if this user is a contact for the host */
	if(is_contact_for_host(hst, temp_contact) == TRUE)
		return TRUE;

	/* see if this user is an escalated contact for the host */
	if(is_escalated_contact_for_host(hst, temp_contact) == TRUE)
		return TRUE;

	return FALSE;
	}


/* check if user is authorized to view information about all hosts in a particular hostgroup */
int is_authorized_for_hostgroup(hostgroup *hg, authdata *authinfo) {
	hostsmember *temp_hostsmember;
	host *temp_host;

	if(hg == NULL)
		return FALSE;

	/* CHANGED in 2.0 - user must be authorized for ALL hosts in a hostgroup, not just one */
	/* see if user is authorized for all hosts in the hostgroup */
	/*
	for(temp_hostsmember = hg->members; temp_hostsmember != NULL; temp_hostsmember = temp_hostsmember->next) {
		temp_host = find_host(temp_hostsmember->host_name);
		if(is_authorized_for_host(temp_host, authinfo) == FALSE)
			return FALSE;
		}
	*/
	/* Reverted for 3.3.2 - must only be a member of one hostgroup */
	for(temp_hostsmember = hg->members; temp_hostsmember != NULL; temp_hostsmember = temp_hostsmember->next) {
		temp_host = find_host(temp_hostsmember->host_name);
		if(is_authorized_for_host(temp_host, authinfo) == TRUE)
			return TRUE;
		}

	/*return TRUE;*/
	return FALSE;
	}



/* check if user is authorized to view information about all services in a particular servicegroup */
int is_authorized_for_servicegroup(servicegroup *sg, authdata *authinfo) {
	servicesmember *temp_servicesmember;
	service *temp_service;

	if(sg == NULL)
		return FALSE;

	/* see if user is authorized for all services in the servicegroup */
	/*
	for(temp_servicesmember = sg->members; temp_servicesmember != NULL; temp_servicesmember = temp_servicesmember->next) {
		temp_service = find_service(temp_servicesmember->host_name, temp_servicesmember->service_description);
		if(is_authorized_for_service(temp_service, authinfo) == FALSE)
			return FALSE;
		}
	*/
	/* Reverted for 3.3.2 - must only be a member of one hostgroup */
	for(temp_servicesmember = sg->members; temp_servicesmember != NULL; temp_servicesmember = temp_servicesmember->next) {
		temp_service = find_service(temp_servicesmember->host_name, temp_servicesmember->service_description);
		if(is_authorized_for_service(temp_service, authinfo) == TRUE)
			return TRUE;
		}

	/*return TRUE*/;
	return FALSE;
	}

/* check if current user is restricted to read only */
int is_authorized_for_read_only(authdata *authinfo) {

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return FALSE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	return authinfo->authorized_for_read_only;
	}

/* check if user is authorized to view information about a particular service */
int is_authorized_for_service(service *svc, authdata *authinfo) {
	host *temp_host;
	contact *temp_contact;

	if(svc == NULL)
		return FALSE;

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	/* if this user is authorized for all services, they are for this one... */
	if(is_authorized_for_all_services(authinfo) == TRUE)
		return TRUE;

	/* find the host */
	temp_host = find_host(svc->host_name);
	if(temp_host == NULL)
		return FALSE;

	/* if this user is authorized for this host, they are for all services on it as well... */
	if(is_authorized_for_host(temp_host, authinfo) == TRUE)
		return TRUE;

	/* find the contact */
	temp_contact = find_contact(authinfo->username);

	/* see if this user is a contact for the service */
	if(is_contact_for_service(svc, temp_contact) == TRUE)
		return TRUE;

	/* see if this user is an escalated contact for the service */
	if(is_escalated_contact_for_service(svc, temp_contact) == TRUE)
		return TRUE;

	return FALSE;
	}


/* check if current user is authorized to view information on all hosts */
int is_authorized_for_all_hosts(authdata *authinfo) {

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	return authinfo->authorized_for_all_hosts;
	}


/* check if current user is authorized to view information on all service */
int is_authorized_for_all_services(authdata *authinfo) {

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	return authinfo->authorized_for_all_services;
	}


/* check if current user is authorized to view system information */
int is_authorized_for_system_information(authdata *authinfo) {

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	return authinfo->authorized_for_system_information;
	}


/* check if current user is authorized to view configuration information */
int is_authorized_for_configuration_information(authdata *authinfo) {

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	return authinfo->authorized_for_configuration_information;
	}


/* check if current user is authorized to issue system commands */
int is_authorized_for_system_commands(authdata *authinfo) {

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	return authinfo->authorized_for_system_commands;
	}


/* check is the current user is authorized to issue commands relating to a particular service */
int is_authorized_for_service_commands(service *svc, authdata *authinfo) {
	host *temp_host;
	contact *temp_contact;

	if(svc == NULL)
		return FALSE;

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	/* the user is authorized if they have rights to the service */
	if(is_authorized_for_service(svc, authinfo) == TRUE) {

		/* find the host */
		temp_host = find_host(svc->host_name);
		if(temp_host == NULL)
			return FALSE;

		/* find the contact */
		temp_contact = find_contact(authinfo->username);

		/* reject if contact is not allowed to issue commands */
		if(temp_contact && temp_contact->can_submit_commands == FALSE)
			return FALSE;

		/* see if this user is a contact for the host */
		if(is_contact_for_host(temp_host, temp_contact) == TRUE)
			return TRUE;

		/* see if this user is an escalated contact for the host */
		if(is_escalated_contact_for_host(temp_host, temp_contact) == TRUE)
			return TRUE;

		/* this user is a contact for the service, so they have permission... */
		if(is_contact_for_service(svc, temp_contact) == TRUE)
			return TRUE;

		/* this user is an escalated contact for the service, so they have permission... */
		if(is_escalated_contact_for_service(svc, temp_contact) == TRUE)
			return TRUE;

		/* this user is not a contact for the host, so they must have been given explicit permissions to all service commands */
		if(authinfo->authorized_for_all_service_commands == TRUE)
			return TRUE;
		}

	return FALSE;
	}


/* check is the current user is authorized to issue commands relating to a particular host */
int is_authorized_for_host_commands(host *hst, authdata *authinfo) {
	contact *temp_contact;

	if(hst == NULL)
		return FALSE;

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	/* the user is authorized if they have rights to the host */
	if(is_authorized_for_host(hst, authinfo) == TRUE) {

		/* find the contact */
		temp_contact = find_contact(authinfo->username);

		/* reject if contact is not allowed to issue commands */
		if(temp_contact && temp_contact->can_submit_commands == FALSE)
			return FALSE;

		/* this user is a contact for the host, so they have permission... */
		if(is_contact_for_host(hst, temp_contact) == TRUE)
			return TRUE;

		/* this user is an escalated contact for the host, so they have permission... */
		if(is_escalated_contact_for_host(hst, temp_contact) == TRUE)
			return TRUE;

		/* this user is not a contact for the host, so they must have been given explicit permissions to all host commands */
		if(authinfo->authorized_for_all_host_commands == TRUE)
			return TRUE;
		}

	return FALSE;
	}


/* check is the current user is authorized to issue commands relating to a particular servicegroup */
int is_authorized_for_servicegroup_commands(servicegroup *sg, authdata *authinfo) {
	servicesmember *temp_servicesmember;
	service *temp_service;

	if(sg == NULL)
		return FALSE;

	/* see if user is authorized for all services commands in the servicegroup */
	for(temp_servicesmember = sg->members; temp_servicesmember != NULL; temp_servicesmember = temp_servicesmember->next) {
		temp_service = find_service(temp_servicesmember->host_name, temp_servicesmember->service_description);
		if(is_authorized_for_service_commands(temp_service, authinfo) == FALSE)
			return FALSE;
		}

	return TRUE;
	}


/* check is the current user is authorized to issue commands relating to a particular hostgroup */
int is_authorized_for_hostgroup_commands(hostgroup *hg, authdata *authinfo) {
	hostsmember *temp_hostsmember;
	host *temp_host;

	if(hg == NULL)
		return FALSE;

	/* see if user is authorized for all hosts in the hostgroup */
	for(temp_hostsmember = hg->members; temp_hostsmember != NULL; temp_hostsmember = temp_hostsmember->next) {
		temp_host = find_host(temp_hostsmember->host_name);
		if(is_authorized_for_host_commands(temp_host, authinfo) == FALSE)
			return FALSE;
		}

	return TRUE;
	}