1#include <unistd.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdio.h>
5#include <ctype.h>
6#include "selinux_internal.h"
7#include "context_internal.h"
8
9int selinux_check_securetty_context(const char * tty_context)
10{
11	char *line = NULL;
12	char *start, *end = NULL;
13	size_t line_len = 0;
14	ssize_t len;
15	int found = -1;
16	FILE *fp;
17	fp = fopen(selinux_securetty_types_path(), "re");
18	if (fp) {
19		context_t con = context_new(tty_context);
20		if (con) {
21			const char *type = context_type_get(con);
22			while ((len = getline(&line, &line_len, fp)) != -1) {
23
24				if (line[len - 1] == '\n')
25					line[len - 1] = 0;
26
27				/* Skip leading whitespace. */
28				start = line;
29				while (*start && isspace(*start))
30					start++;
31				if (!(*start))
32					continue;
33
34				end = start;
35				while (*end && !isspace(*end))
36					end++;
37				if (*end)
38					*end++ = 0;
39				if (!strcmp(type, start)) {
40					found = 0;
41					break;
42				}
43			}
44			free(line);
45			context_free(con);
46		}
47		fclose(fp);
48	}
49
50	return found;
51}
52
53