xref: /aosp_15_r20/external/selinux/libsemanage/src/ibendports_file.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 /* Copyright (C) 2017 Mellanox Technologies Inc. */
2 
3 struct semanage_ibendport;
4 struct semanage_ibendport_key;
5 typedef struct semanage_ibendport record_t;
6 typedef struct semanage_ibendport_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 #include <strings.h>
16 #include <semanage/handle.h>
17 #include "ibendport_internal.h"
18 #include "database_file.h"
19 #include "parse_utils.h"
20 #include "debug.h"
21 
ibendport_print(semanage_handle_t * handle,semanage_ibendport_t * ibendport,FILE * str)22 static int ibendport_print(semanage_handle_t *handle,
23 			   semanage_ibendport_t *ibendport,
24 			   FILE *str)
25 {
26 	char *con_str = NULL;
27 	char *ibdev_name_str = NULL;
28 	int port = semanage_ibendport_get_port(ibendport);
29 
30 	if (semanage_ibendport_get_ibdev_name(handle, ibendport, &ibdev_name_str) != 0)
31 		goto err;
32 
33 	semanage_context_t *con = semanage_ibendport_get_con(ibendport);
34 
35 	if (fprintf(str, "ibendportcon %s ", ibdev_name_str) < 0)
36 		goto err;
37 
38 	if (fprintf(str, "%d ", port) < 0)
39 		goto err;
40 
41 	if (semanage_context_to_string(handle, con, &con_str) < 0)
42 		goto err;
43 	if (fprintf(str, "%s\n", con_str) < 0)
44 		goto err;
45 
46 	free(ibdev_name_str);
47 	free(con_str);
48 	return STATUS_SUCCESS;
49 
50 err:
51 	ERR(handle, "could not print ibendport (%s) %u to stream",
52 	    ibdev_name_str, port);
53 	free(ibdev_name_str);
54 	free(con_str);
55 	return STATUS_ERR;
56 }
57 
ibendport_parse(semanage_handle_t * handle,parse_info_t * info,semanage_ibendport_t * ibendport)58 static int ibendport_parse(semanage_handle_t *handle,
59 			   parse_info_t *info,
60 			   semanage_ibendport_t *ibendport)
61 {
62 	int port;
63 	char *str = NULL;
64 	semanage_context_t *con = NULL;
65 
66 	if (parse_skip_space(handle, info) < 0)
67 		goto err;
68 	if (!info->ptr)
69 		goto last;
70 
71 	/* Header */
72 	if (parse_assert_str(handle, info, "ibendportcon") < 0)
73 		goto err;
74 	if (parse_assert_space(handle, info) < 0)
75 		goto err;
76 
77 	/* IB Device Name */
78 	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
79 		goto err;
80 	if (semanage_ibendport_set_ibdev_name(handle, ibendport, str) < 0)
81 		goto err;
82 	free(str);
83 	str = NULL;
84 
85 	/* Port */
86 	if (parse_assert_space(handle, info) < 0)
87 		goto err;
88 	if (parse_fetch_int(handle, info, &port, ' ') < 0)
89 		goto err;
90 	semanage_ibendport_set_port(ibendport, port);
91 
92 	/* context */
93 	if (parse_assert_space(handle, info) < 0)
94 		goto err;
95 	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
96 		goto err;
97 	if (semanage_context_from_string(handle, str, &con) < 0) {
98 		ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
99 		    str, info->filename, info->lineno, info->orig_line);
100 		goto err;
101 	}
102 	if (!con) {
103 		ERR(handle, "<<none>> context is not valid for ibendport (%s: %u):\n%s",
104 		    info->filename, info->lineno, info->orig_line);
105 		goto err;
106 	}
107 	free(str);
108 	str = NULL;
109 
110 	if (semanage_ibendport_set_con(handle, ibendport, con) < 0)
111 		goto err;
112 
113 	if (parse_assert_space(handle, info) < 0)
114 		goto err;
115 
116 	semanage_context_free(con);
117 	return STATUS_SUCCESS;
118 
119 last:
120 	parse_dispose_line(info);
121 	return STATUS_NODATA;
122 
123 err:
124 	ERR(handle, "could not parse ibendport record");
125 	free(str);
126 	semanage_context_free(con);
127 	parse_dispose_line(info);
128 	return STATUS_ERR;
129 }
130 
131 /* IBENDPORT RECORD: FILE extension: method table */
132 record_file_table_t SEMANAGE_IBENDPORT_FILE_RTABLE = {
133 	.parse = ibendport_parse,
134 	.print = ibendport_print,
135 };
136 
ibendport_file_dbase_init(semanage_handle_t * handle,const char * path_ro,const char * path_rw,dbase_config_t * dconfig)137 int ibendport_file_dbase_init(semanage_handle_t *handle,
138 			      const char *path_ro,
139 			      const char *path_rw,
140 			      dbase_config_t *dconfig)
141 {
142 	if (dbase_file_init(handle,
143 			    path_ro,
144 			    path_rw,
145 			    &SEMANAGE_IBENDPORT_RTABLE,
146 			    &SEMANAGE_IBENDPORT_FILE_RTABLE, &dconfig->dbase) < 0)
147 		return STATUS_ERR;
148 
149 	dconfig->dtable = &SEMANAGE_FILE_DTABLE;
150 	return STATUS_SUCCESS;
151 }
152 
ibendport_file_dbase_release(dbase_config_t * dconfig)153 void ibendport_file_dbase_release(dbase_config_t *dconfig)
154 {
155 	dbase_file_release(dconfig->dbase);
156 }
157