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