File: seusers_file.c

package info (click to toggle)
android-platform-external-libselinux 10.0.0%2Br36-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 34,176 kB
  • sloc: ansic: 147,112; python: 25,790; makefile: 1,930; yacc: 1,389; sh: 1,206; lex: 452; xml: 180
file content (138 lines) | stat: -rw-r--r-- 3,014 bytes parent folder | download | duplicates (7)
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
/* Copyright (C) 2005 Red Hat, Inc. */

struct semanage_seuser;
struct semanage_seuser_key;
typedef struct semanage_seuser record_t;
typedef struct semanage_seuser_key record_key_t;
#define DBASE_RECORD_DEFINED

struct dbase_file;
typedef struct dbase_file dbase_t;
#define DBASE_DEFINED

#include <stdlib.h>
#include <stdio.h>

#include "seuser_internal.h"
#include "database_file.h"
#include "parse_utils.h"
#include "debug.h"
#include "handle.h"

static int seuser_print(semanage_handle_t * handle,
			semanage_seuser_t * seuser, FILE * str)
{

	const char *name = semanage_seuser_get_name(seuser);
	const char *sename = semanage_seuser_get_sename(seuser);
	const char *mls = semanage_seuser_get_mlsrange(seuser);

	if (fprintf(str, "%s:%s", name, sename) < 0)
		goto err;

	if (mls != NULL && fprintf(str, ":%s", mls) < 0)
		goto err;

	fprintf(str, "\n");
	return STATUS_SUCCESS;

      err:
	ERR(handle, "could not print seuser %s to stream", name);
	return STATUS_ERR;
}

static int seuser_parse(semanage_handle_t * handle,
			parse_info_t * info, semanage_seuser_t * seuser)
{

	char *str = NULL;

	if (parse_skip_space(handle, info) < 0)
		goto err;
	if (!info->ptr)
		goto last;

	/* Extract name */
	if (parse_fetch_string(handle, info, &str, ':') < 0)
		goto err;
	if (semanage_seuser_set_name(handle, seuser, str) < 0)
		goto err;
	free(str);
	str = NULL;

	if (parse_skip_space(handle, info) < 0)
		goto err;
	if (parse_assert_ch(handle, info, ':') < 0)
		goto err;
	if (parse_skip_space(handle, info) < 0)
		goto err;

	/* Extract sename */
	if (parse_fetch_string(handle, info, &str, ':') < 0)
		goto err;
	if (semanage_seuser_set_sename(handle, seuser, str) < 0)
		goto err;
	free(str);
	str = NULL;

	if (parse_skip_space(handle, info) < 0)
		goto err;
	if (parse_optional_ch(info, ':') == STATUS_NODATA)
		goto out;
	if (parse_skip_space(handle, info) < 0)
		goto err;

	/* NOTE: does not allow spaces/multiline */
	if (parse_fetch_string(handle, info, &str, ' ') < 0)
		goto err;

	if (semanage_seuser_set_mlsrange(handle, seuser, str) < 0)
		goto err;
	free(str);
	str = NULL;

	if (parse_assert_space(handle, info) < 0)
		goto err;

      out:
	return STATUS_SUCCESS;

      last:
	parse_dispose_line(info);
	return STATUS_NODATA;

      err:
	ERR(handle, "could not parse seuser record");
	free(str);
	parse_dispose_line(info);
	return STATUS_ERR;
}

/* SEUSER RECORD: FILE extension: method table */
record_file_table_t SEMANAGE_SEUSER_FILE_RTABLE = {
	.parse = seuser_parse,
	.print = seuser_print,
};

int seuser_file_dbase_init(semanage_handle_t * handle,
			   const char *path_ro,
			   const char *path_rw,
			   dbase_config_t * dconfig)
{

	if (dbase_file_init(handle,
			    path_ro,
			    path_rw,
			    &SEMANAGE_SEUSER_RTABLE,
			    &SEMANAGE_SEUSER_FILE_RTABLE, &dconfig->dbase) < 0)
		return STATUS_ERR;

	dconfig->dtable = &SEMANAGE_FILE_DTABLE;
	return STATUS_SUCCESS;
}

void seuser_file_dbase_release(dbase_config_t * dconfig)
{

	dbase_file_release(dconfig->dbase);
}