1 /* Copyright (C) 2005 Red Hat, Inc. */
2 
3 struct semanage_seuser;
4 struct semanage_seuser_key;
5 typedef struct semanage_seuser record_t;
6 typedef struct semanage_seuser_key record_key_t;
7 #define DBASE_RECORD_DEFINED
8 
9 struct dbase_file;
10 typedef struct dbase_file dbase_t;
11 #define DBASE_DEFINED
12 
13 #include <stdlib.h>
14 #include <stdio.h>
15 
16 #include "seuser_internal.h"
17 #include "database_file.h"
18 #include "parse_utils.h"
19 #include "debug.h"
20 #include "handle.h"
21 
seuser_print(semanage_handle_t * handle,semanage_seuser_t * seuser,FILE * str)22 static int seuser_print(semanage_handle_t * handle,
23 			semanage_seuser_t * seuser, FILE * str)
24 {
25 
26 	const char *name = semanage_seuser_get_name(seuser);
27 	const char *sename = semanage_seuser_get_sename(seuser);
28 	const char *mls = semanage_seuser_get_mlsrange(seuser);
29 
30 	if (fprintf(str, "%s:%s", name, sename) < 0)
31 		goto err;
32 
33 	if (mls != NULL && fprintf(str, ":%s", mls) < 0)
34 		goto err;
35 
36 	fprintf(str, "\n");
37 	return STATUS_SUCCESS;
38 
39       err:
40 	ERR(handle, "could not print seuser %s to stream", name);
41 	return STATUS_ERR;
42 }
43 
seuser_parse(semanage_handle_t * handle,parse_info_t * info,semanage_seuser_t * seuser)44 static int seuser_parse(semanage_handle_t * handle,
45 			parse_info_t * info, semanage_seuser_t * seuser)
46 {
47 
48 	char *str = NULL;
49 
50 	if (parse_skip_space(handle, info) < 0)
51 		goto err;
52 	if (!info->ptr)
53 		goto last;
54 
55 	/* Extract name */
56 	if (parse_fetch_string(handle, info, &str, ':') < 0)
57 		goto err;
58 	if (semanage_seuser_set_name(handle, seuser, str) < 0)
59 		goto err;
60 	free(str);
61 	str = NULL;
62 
63 	if (parse_skip_space(handle, info) < 0)
64 		goto err;
65 	if (parse_assert_ch(handle, info, ':') < 0)
66 		goto err;
67 	if (parse_skip_space(handle, info) < 0)
68 		goto err;
69 
70 	/* Extract sename */
71 	if (parse_fetch_string(handle, info, &str, ':') < 0)
72 		goto err;
73 	if (semanage_seuser_set_sename(handle, seuser, str) < 0)
74 		goto err;
75 	free(str);
76 	str = NULL;
77 
78 	if (parse_skip_space(handle, info) < 0)
79 		goto err;
80 	if (parse_optional_ch(info, ':') == STATUS_NODATA)
81 		goto out;
82 	if (parse_skip_space(handle, info) < 0)
83 		goto err;
84 
85 	/* NOTE: does not allow spaces/multiline */
86 	if (parse_fetch_string(handle, info, &str, ' ') < 0)
87 		goto err;
88 
89 	if (semanage_seuser_set_mlsrange(handle, seuser, str) < 0)
90 		goto err;
91 	free(str);
92 	str = NULL;
93 
94 	if (parse_assert_space(handle, info) < 0)
95 		goto err;
96 
97       out:
98 	return STATUS_SUCCESS;
99 
100       last:
101 	parse_dispose_line(info);
102 	return STATUS_NODATA;
103 
104       err:
105 	ERR(handle, "could not parse seuser record");
106 	free(str);
107 	parse_dispose_line(info);
108 	return STATUS_ERR;
109 }
110 
111 /* SEUSER RECORD: FILE extension: method table */
112 record_file_table_t SEMANAGE_SEUSER_FILE_RTABLE = {
113 	.parse = seuser_parse,
114 	.print = seuser_print,
115 };
116 
seuser_file_dbase_init(semanage_handle_t * handle,const char * path_ro,const char * path_rw,dbase_config_t * dconfig)117 int seuser_file_dbase_init(semanage_handle_t * handle,
118 			   const char *path_ro,
119 			   const char *path_rw,
120 			   dbase_config_t * dconfig)
121 {
122 
123 	if (dbase_file_init(handle,
124 			    path_ro,
125 			    path_rw,
126 			    &SEMANAGE_SEUSER_RTABLE,
127 			    &SEMANAGE_SEUSER_FILE_RTABLE, &dconfig->dbase) < 0)
128 		return STATUS_ERR;
129 
130 	dconfig->dtable = &SEMANAGE_FILE_DTABLE;
131 	return STATUS_SUCCESS;
132 }
133 
seuser_file_dbase_release(dbase_config_t * dconfig)134 void seuser_file_dbase_release(dbase_config_t * dconfig)
135 {
136 
137 	dbase_file_release(dconfig->dbase);
138 }
139