• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2005 Red Hat, Inc. */
2 
3 struct semanage_port;
4 struct semanage_port_key;
5 typedef struct semanage_port_key record_key_t;
6 typedef struct semanage_port record_t;
7 #define DBASE_RECORD_DEFINED
8 
9 #include <stdlib.h>
10 #include "port_internal.h"
11 #include "debug.h"
12 #include "handle.h"
13 #include "database.h"
14 
semanage_port_modify_local(semanage_handle_t * handle,const semanage_port_key_t * key,const semanage_port_t * data)15 int semanage_port_modify_local(semanage_handle_t * handle,
16 			       const semanage_port_key_t * key,
17 			       const semanage_port_t * data)
18 {
19 
20 	dbase_config_t *dconfig = semanage_port_dbase_local(handle);
21 	return dbase_modify(handle, dconfig, key, data);
22 }
23 
semanage_port_del_local(semanage_handle_t * handle,const semanage_port_key_t * key)24 int semanage_port_del_local(semanage_handle_t * handle,
25 			    const semanage_port_key_t * key)
26 {
27 
28 	dbase_config_t *dconfig = semanage_port_dbase_local(handle);
29 	return dbase_del(handle, dconfig, key);
30 }
31 
semanage_port_query_local(semanage_handle_t * handle,const semanage_port_key_t * key,semanage_port_t ** response)32 int semanage_port_query_local(semanage_handle_t * handle,
33 			      const semanage_port_key_t * key,
34 			      semanage_port_t ** response)
35 {
36 
37 	dbase_config_t *dconfig = semanage_port_dbase_local(handle);
38 	return dbase_query(handle, dconfig, key, response);
39 }
40 
semanage_port_exists_local(semanage_handle_t * handle,const semanage_port_key_t * key,int * response)41 int semanage_port_exists_local(semanage_handle_t * handle,
42 			       const semanage_port_key_t * key, int *response)
43 {
44 
45 	dbase_config_t *dconfig = semanage_port_dbase_local(handle);
46 	return dbase_exists(handle, dconfig, key, response);
47 }
48 
semanage_port_count_local(semanage_handle_t * handle,unsigned int * response)49 int semanage_port_count_local(semanage_handle_t * handle,
50 			      unsigned int *response)
51 {
52 
53 	dbase_config_t *dconfig = semanage_port_dbase_local(handle);
54 	return dbase_count(handle, dconfig, response);
55 }
56 
semanage_port_iterate_local(semanage_handle_t * handle,int (* handler)(const semanage_port_t * record,void * varg),void * handler_arg)57 int semanage_port_iterate_local(semanage_handle_t * handle,
58 				int (*handler) (const semanage_port_t * record,
59 						void *varg), void *handler_arg)
60 {
61 
62 	dbase_config_t *dconfig = semanage_port_dbase_local(handle);
63 	return dbase_iterate(handle, dconfig, handler, handler_arg);
64 }
65 
semanage_port_list_local(semanage_handle_t * handle,semanage_port_t *** records,unsigned int * count)66 int semanage_port_list_local(semanage_handle_t * handle,
67 			     semanage_port_t *** records, unsigned int *count)
68 {
69 
70 	dbase_config_t *dconfig = semanage_port_dbase_local(handle);
71 	return dbase_list(handle, dconfig, records, count);
72 }
73 
74 
semanage_port_validate_local(semanage_handle_t * handle)75 int semanage_port_validate_local(semanage_handle_t * handle)
76 {
77 
78 	semanage_port_t **ports = NULL;
79 	unsigned int nports = 0;
80 	unsigned int i = 0, j = 0;
81 
82 	/* List and sort the ports */
83 	if (semanage_port_list_local(handle, &ports, &nports) < 0)
84 		goto err;
85 	qsort(ports, nports, sizeof(semanage_port_t *),
86 	      (int (*)(const void *, const void *))
87 	      &semanage_port_compare2_qsort);
88 
89 	/* Test each port for overlap */
90 	while (i < nports) {
91 
92 		int proto = semanage_port_get_proto(ports[i]);
93 		int low = semanage_port_get_low(ports[i]);
94 		int high = semanage_port_get_high(ports[i]);
95 		const char *proto_str = semanage_port_get_proto_str(proto);
96 
97 		const char *proto_str2;
98 		int proto2, low2, high2;
99 
100 		/* Find the first port with matching
101 		   protocol to compare against */
102 		do {
103 			if (j == nports - 1)
104 				goto next;
105 			j++;
106 			proto2 = semanage_port_get_proto(ports[j]);
107 			low2 = semanage_port_get_low(ports[j]);
108 			high2 = semanage_port_get_high(ports[j]);
109 			proto_str2 = semanage_port_get_proto_str(proto2);
110 
111 		} while (proto != proto2);
112 
113 		/* Overlap detected */
114 		if (low2 <= high) {
115 			ERR(handle, "port overlap between ranges "
116 			    "%u - %u (%s) <--> %u - %u (%s).",
117 			    low, high, proto_str, low2, high2, proto_str2);
118 			goto invalid;
119 		}
120 
121 		/* If closest port of matching protocol doesn't overlap with
122 		 * test port, neither do the rest of them, because that's
123 		 * how the sort function works on ports - lower bound
124 		 * ports come first */
125 	      next:
126 		i++;
127 		j = i;
128 	}
129 
130 	for (i = 0; i < nports; i++)
131 		semanage_port_free(ports[i]);
132 	free(ports);
133 	return STATUS_SUCCESS;
134 
135       err:
136 	ERR(handle, "could not complete ports validity check");
137 
138       invalid:
139 	for (i = 0; i < nports; i++)
140 		semanage_port_free(ports[i]);
141 	free(ports);
142 	return STATUS_ERR;
143 }
144