• 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_key record_key_t;
6 typedef struct semanage_ibendport record_t;
7 #define DBASE_RECORD_DEFINED
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sepol/policydb.h>
12 #include "ibendport_internal.h"
13 #include "debug.h"
14 #include "handle.h"
15 #include "database.h"
16 
semanage_ibendport_modify_local(semanage_handle_t * handle,const semanage_ibendport_key_t * key,const semanage_ibendport_t * data)17 int semanage_ibendport_modify_local(semanage_handle_t *handle,
18 				    const semanage_ibendport_key_t *key,
19 				    const semanage_ibendport_t *data)
20 {
21 	dbase_config_t *dconfig = semanage_ibendport_dbase_local(handle);
22 
23 	return dbase_modify(handle, dconfig, key, data);
24 }
25 
semanage_ibendport_del_local(semanage_handle_t * handle,const semanage_ibendport_key_t * key)26 int semanage_ibendport_del_local(semanage_handle_t *handle,
27 				 const semanage_ibendport_key_t *key)
28 {
29 	dbase_config_t *dconfig = semanage_ibendport_dbase_local(handle);
30 
31 	return dbase_del(handle, dconfig, key);
32 }
33 
semanage_ibendport_query_local(semanage_handle_t * handle,const semanage_ibendport_key_t * key,semanage_ibendport_t ** response)34 int semanage_ibendport_query_local(semanage_handle_t *handle,
35 				   const semanage_ibendport_key_t *key,
36 				   semanage_ibendport_t **response)
37 {
38 	dbase_config_t *dconfig = semanage_ibendport_dbase_local(handle);
39 
40 	return dbase_query(handle, dconfig, key, response);
41 }
42 
semanage_ibendport_exists_local(semanage_handle_t * handle,const semanage_ibendport_key_t * key,int * response)43 int semanage_ibendport_exists_local(semanage_handle_t *handle,
44 				    const semanage_ibendport_key_t *key,
45 				    int *response)
46 {
47 	dbase_config_t *dconfig = semanage_ibendport_dbase_local(handle);
48 
49 	return dbase_exists(handle, dconfig, key, response);
50 }
51 
semanage_ibendport_count_local(semanage_handle_t * handle,unsigned int * response)52 int semanage_ibendport_count_local(semanage_handle_t *handle,
53 				   unsigned int *response)
54 {
55 	dbase_config_t *dconfig = semanage_ibendport_dbase_local(handle);
56 
57 	return dbase_count(handle, dconfig, response);
58 }
59 
semanage_ibendport_iterate_local(semanage_handle_t * handle,int (* handler)(const semanage_ibendport_t * record,void * varg),void * handler_arg)60 int semanage_ibendport_iterate_local(semanage_handle_t *handle,
61 				     int (*handler)(const semanage_ibendport_t *record,
62 						    void *varg), void *handler_arg)
63 {
64 	dbase_config_t *dconfig = semanage_ibendport_dbase_local(handle);
65 	return dbase_iterate(handle, dconfig, handler, handler_arg);
66 }
67 
semanage_ibendport_list_local(semanage_handle_t * handle,semanage_ibendport_t *** records,unsigned int * count)68 int semanage_ibendport_list_local(semanage_handle_t *handle,
69 				  semanage_ibendport_t ***records,
70 				  unsigned int *count)
71 {
72 	dbase_config_t *dconfig = semanage_ibendport_dbase_local(handle);
73 
74 	return dbase_list(handle, dconfig, records, count);
75 }
76 
hidden_def(semanage_ibendport_list_local)77 hidden_def(semanage_ibendport_list_local)
78 
79 int hidden semanage_ibendport_validate_local(semanage_handle_t *handle)
80 {
81 	semanage_ibendport_t **ibendports = NULL;
82 	unsigned int nibendports = 0;
83 	unsigned int i = 0, j = 0;
84 	char *ibdev_name;
85 	char *ibdev_name2;
86 	int port;
87 	int port2;
88 
89 	/* List and sort the ibendports */
90 	if (semanage_ibendport_list_local(handle, &ibendports, &nibendports) < 0)
91 		goto err;
92 
93 	qsort(ibendports, nibendports, sizeof(semanage_ibendport_t *),
94 	      (int (*)(const void *, const void *))
95 	      &semanage_ibendport_compare2_qsort);
96 
97 	/* Test each ibendport */
98 	while (i < nibendports) {
99 		int stop = 0;
100 
101 		if (STATUS_SUCCESS !=
102 				semanage_ibendport_get_ibdev_name(handle,
103 								  ibendports[i],
104 								  &ibdev_name)) {
105 			ERR(handle, "Couldn't get IB device name");
106 			goto err;
107 		}
108 
109 		port = semanage_ibendport_get_port(ibendports[i]);
110 
111 		/* Find the first ibendport with matching
112 		 * ibdev_name to compare against
113 		 */
114 		do {
115 			if (j == nibendports - 1)
116 				goto next;
117 			j++;
118 			if (STATUS_SUCCESS !=
119 				semanage_ibendport_get_ibdev_name(handle,
120 								  ibendports[j],
121 								  &ibdev_name2)) {
122 				ERR(handle, "Couldn't get IB device name.");
123 				goto err;
124 			}
125 			port2 = semanage_ibendport_get_port(ibendports[j]);
126 
127 			stop = !strcmp(ibdev_name, ibdev_name2);
128 		} while (!stop);
129 
130 		if (port == port2) {
131 			ERR(handle, "ibendport %s/%u already exists.",
132 			    ibdev_name2, port2);
133 			goto invalid;
134 		}
135 next:
136 		i++;
137 		j = i;
138 	}
139 
140 	for (i = 0; i < nibendports; i++)
141 		semanage_ibendport_free(ibendports[i]);
142 	free(ibendports);
143 	return STATUS_SUCCESS;
144 
145 err:
146 	ERR(handle, "could not complete ibendports validity check");
147 
148 invalid:
149 	for (i = 0; i < nibendports; i++)
150 		semanage_ibendport_free(ibendports[i]);
151 	free(ibendports);
152 	return STATUS_ERR;
153 }
154