CUBRID Engine  latest
broker_access_list.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * broker_access_list.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "broker_access_list.h"
31 #include "broker_error.h"
32 #include "broker_util.h"
33 
34 static int ip_comp (const void *arg1, const void *arg2);
35 static int convert_ip (char *str, T_IP * ip_addr);
36 static int ipstr2int (char *str, char **endp, char next_char);
37 
39 
40 int
41 uw_acl_make (char *acl_file)
42 {
43  FILE *fp;
44  char read_buf[1024];
45  char *p;
46  T_IP ip_addr;
47  T_IP *acl = NULL;
48  int num_acl = 0;
49 
50  v3_acl = NULL;
51 
52  if (acl_file[0] == '\0')
53  {
54  return 0;
55  }
56 
57  v3_acl = (T_ACL *) malloc (sizeof (T_ACL));
58  if (v3_acl == NULL)
59  {
61  return -1;
62  }
63  v3_acl->num_acl = 0;
64  v3_acl->acl = NULL;
65 
66  fp = fopen (acl_file, "r");
67  if (fp == NULL)
68  {
69  return 0;
70  }
71 
72  while (fgets (read_buf, sizeof (read_buf), fp) != NULL)
73  {
74  p = trim (read_buf);
75  if (p[0] == '#')
76  {
77  continue;
78  }
79  if (convert_ip (p, &ip_addr) < 0)
80  {
81  continue;
82  }
83  num_acl++;
84  if (acl == NULL)
85  {
86  acl = (T_IP *) malloc (sizeof (T_IP));
87  }
88  else
89  {
90  acl = (T_IP *) realloc (acl, sizeof (T_IP) * num_acl);
91  }
92  if (acl == NULL)
93  {
94  fclose (fp);
96  return -1;
97  }
98  acl[num_acl - 1] = ip_addr;
99  }
100 
101  if (acl != NULL)
102  {
103  qsort (acl, num_acl, sizeof (T_IP), ip_comp);
104  }
105  v3_acl->num_acl = num_acl;
106  v3_acl->acl = acl;
107 
108  fclose (fp);
109 
110  return 0;
111 }
112 
113 int
114 uw_acl_check (unsigned char *ip_addr)
115 {
116  int i;
117  size_t len;
118 
119  for (i = 0; i < v3_acl->num_acl; i++)
120  {
121  if (v3_acl->acl[i].ip_length > IPV4_LENGTH_MAX)
122  {
123  len = IPV4_LENGTH_MAX;
124  }
125  else
126  {
127  len = v3_acl->acl[i].ip_length;
128  }
129 
130  if (memcmp (ip_addr, v3_acl->acl[i].ip, len) == 0)
131  {
132  return 0;
133  }
134  }
135 
136  return -1;
137 }
138 
139 static int
140 ip_comp (const void *arg1, const void *arg2)
141 {
142  const T_IP *ip1 = (const T_IP *) arg1;
143  const T_IP *ip2 = (const T_IP *) arg2;
144 
145  return ((ip1->ip_length) - (ip2->ip_length));
146 }
147 
148 static int
149 convert_ip (char *str, T_IP * ip_addr)
150 {
151  int i;
152  char *endp;
153  int val;
154  char end_char;
155 
156  memset (ip_addr, 0, sizeof (T_IP));
157 
158  for (i = 0; i < 4; i++)
159  {
160  end_char = '.';
161  if (i == 3)
162  {
163  end_char = '\0';
164  }
165 
166  val = ipstr2int (str, &endp, end_char);
167  if (val < 0)
168  {
169  return -1;
170  }
171  if (val == 256)
172  {
173  ip_addr->ip_length = i;
174  return 0;
175  }
176  else
177  {
178  ip_addr->ip[i] = (unsigned char) val;
179  }
180 
181  str = endp + 1;
182  }
183  ip_addr->ip_length = 4;
184  return 0;
185 }
186 
187 static int
188 ipstr2int (char *str, char **endp, char next_char)
189 {
190  int result = 0;
191  int val;
192 
193  if (*str == '*')
194  {
195  if (*(str + 1) != '\0')
196  {
197  return -1;
198  }
199  return 256;
200  }
201 
202  result = str_to_int32 (&val, endp, str, 10);
203 
204  if ((result != 0) || (**endp != next_char))
205  {
206  return -1;
207  }
208 
209  if (val < 0 || val > 255)
210  {
211  return -1;
212  }
213 
214  return val;
215 }
#define UW_ER_NO_MORE_MEMORY
Definition: broker_error.h:40
char * trim(char *str)
Definition: porting.c:2260
T_IP * acl
#define UW_SET_ERROR_CODE(code, os_errno)
Definition: broker_error.h:87
static int ipstr2int(char *str, char **endp, char next_char)
static int ip_comp(const void *arg1, const void *arg2)
unsigned char ip_length
#define NULL
Definition: freelistheap.h:34
int str_to_int32(int *ret_p, char **end_p, const char *str_p, int base)
Definition: porting.c:2346
unsigned char ip[IPV4_LENGTH_MAX]
int uw_acl_check(unsigned char *ip_addr)
int i
Definition: dynamic_load.c:954
int uw_acl_make(char *acl_file)
T_ACL * v3_acl
static int convert_ip(char *str, T_IP *ip_addr)
#define IPV4_LENGTH_MAX
const char ** p
Definition: dynamic_load.c:945