xref: /aosp_15_r20/external/selinux/libsemanage/src/fcontexts_local.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 /* Copyright (C) 2005 Red Hat, Inc. */
2 
3 struct semanage_fcontext;
4 struct semanage_fcontext_key;
5 typedef struct semanage_fcontext_key record_key_t;
6 typedef struct semanage_fcontext record_t;
7 #define DBASE_RECORD_DEFINED
8 
9 #include <stdlib.h>
10 #include <sepol/policydb.h>
11 #include <sepol/context.h>
12 #include "fcontext_internal.h"
13 #include "debug.h"
14 #include "handle.h"
15 #include "database.h"
16 
semanage_fcontext_modify_local(semanage_handle_t * handle,const semanage_fcontext_key_t * key,const semanage_fcontext_t * data)17 int semanage_fcontext_modify_local(semanage_handle_t * handle,
18 				   const semanage_fcontext_key_t * key,
19 				   const semanage_fcontext_t * data)
20 {
21 
22 	dbase_config_t *dconfig = semanage_fcontext_dbase_local(handle);
23 	return dbase_modify(handle, dconfig, key, data);
24 }
25 
semanage_fcontext_del_local(semanage_handle_t * handle,const semanage_fcontext_key_t * key)26 int semanage_fcontext_del_local(semanage_handle_t * handle,
27 				const semanage_fcontext_key_t * key)
28 {
29 
30 	dbase_config_t *dconfig = semanage_fcontext_dbase_local(handle);
31 	return dbase_del(handle, dconfig, key);
32 }
33 
semanage_fcontext_query_local(semanage_handle_t * handle,const semanage_fcontext_key_t * key,semanage_fcontext_t ** response)34 int semanage_fcontext_query_local(semanage_handle_t * handle,
35 				  const semanage_fcontext_key_t * key,
36 				  semanage_fcontext_t ** response)
37 {
38 
39 	dbase_config_t *dconfig = semanage_fcontext_dbase_local(handle);
40 	return dbase_query(handle, dconfig, key, response);
41 }
42 
semanage_fcontext_exists_local(semanage_handle_t * handle,const semanage_fcontext_key_t * key,int * response)43 int semanage_fcontext_exists_local(semanage_handle_t * handle,
44 				   const semanage_fcontext_key_t * key,
45 				   int *response)
46 {
47 
48 	dbase_config_t *dconfig = semanage_fcontext_dbase_local(handle);
49 	return dbase_exists(handle, dconfig, key, response);
50 }
51 
semanage_fcontext_count_local(semanage_handle_t * handle,unsigned int * response)52 int semanage_fcontext_count_local(semanage_handle_t * handle,
53 				  unsigned int *response)
54 {
55 
56 	dbase_config_t *dconfig = semanage_fcontext_dbase_local(handle);
57 	return dbase_count(handle, dconfig, response);
58 }
59 
semanage_fcontext_iterate_local(semanage_handle_t * handle,int (* handler)(const semanage_fcontext_t * record,void * varg),void * handler_arg)60 int semanage_fcontext_iterate_local(semanage_handle_t * handle,
61 				    int (*handler) (const semanage_fcontext_t *
62 						    record, void *varg),
63 				    void *handler_arg)
64 {
65 
66 	dbase_config_t *dconfig = semanage_fcontext_dbase_local(handle);
67 	return dbase_iterate(handle, dconfig, handler, handler_arg);
68 }
69 
70 
semanage_fcontext_list_local(semanage_handle_t * handle,semanage_fcontext_t *** records,unsigned int * count)71 int semanage_fcontext_list_local(semanage_handle_t * handle,
72 				 semanage_fcontext_t *** records,
73 				 unsigned int *count)
74 {
75 
76 	dbase_config_t *dconfig = semanage_fcontext_dbase_local(handle);
77 	return dbase_list(handle, dconfig, records, count);
78 }
79 
80 struct validate_handler_arg {
81 	semanage_handle_t *handle;
82 	const sepol_policydb_t *policydb;
83 };
84 
validate_handler(const semanage_fcontext_t * fcon,void * varg)85 static int validate_handler(const semanage_fcontext_t * fcon, void *varg)
86 {
87 
88 	char *str;
89 
90 	/* Unpack varg */
91 	struct validate_handler_arg *arg = (struct validate_handler_arg *)varg;
92 	semanage_handle_t *handle = arg->handle;
93 	const sepol_policydb_t *policydb = arg->policydb;
94 
95 	/* Unpack fcontext */
96 	const char *expr = semanage_fcontext_get_expr(fcon);
97 	int type = semanage_fcontext_get_type(fcon);
98 	const char *type_str = semanage_fcontext_get_type_str(type);
99 	semanage_context_t *con = semanage_fcontext_get_con(fcon);
100 
101 	if (con
102 	    && sepol_context_check(handle->sepolh, policydb,
103 				   (sepol_context_t *) con) < 0)
104 		goto invalid;
105 
106 	return 0;
107 
108       invalid:
109 	if (semanage_context_to_string(handle, con, &str) >= 0) {
110 		ERR(handle, "invalid context %s specified for %s [%s]",
111 		    str, expr, type_str);
112 		free(str);
113 	} else
114 		ERR(handle, "invalid context specified for %s [%s]",
115 		    expr, type_str);
116 	return -1;
117 }
118 
semanage_fcontext_validate_local(semanage_handle_t * handle,const sepol_policydb_t * policydb)119 int semanage_fcontext_validate_local(semanage_handle_t * handle,
120 					    const sepol_policydb_t * policydb)
121 {
122 
123 	struct validate_handler_arg arg;
124 	arg.handle = handle;
125 	arg.policydb = policydb;
126 	return semanage_fcontext_iterate_local(handle, validate_handler, &arg);
127 }
128