All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
regex.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /*
18  * $Id: e58c54a68364483fb532fc50c523008da92f3047 $
19  *
20  * @file main/regex.c
21  * @brief Regular expression functions used by the server library.
22  *
23  * @copyright 2014 The FreeRADIUS server project
24  */
25 
26 RCSID("$Id: e58c54a68364483fb532fc50c523008da92f3047 $")
27 
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/rad_assert.h>
30 
31 #ifdef HAVE_REGEX
32 
33 #define REQUEST_DATA_REGEX (0xadbeef00)
34 
35 typedef struct regcapture {
36  regex_t *preg; //!< Compiled pattern.
37  char const *value; //!< Original string.
38  regmatch_t *rxmatch; //!< Match vectors.
39  size_t nmatch; //!< Number of match vectors.
40 } regcapture_t;
41 
42 /** Adds subcapture values to request data
43  *
44  * Allows use of %{n} expansions.
45  *
46  * @note After calling regex_sub_to_request *preg may no longer be valid and
47  * should be passed to talloc_free.
48  *
49  * @param request Current request.
50  * @param preg Compiled pattern. May be set to NULL if reparented to the regcapture struct.
51  * @param value The original value.
52  * @param rxmatch Pointers into value.
53  * @param nmatch Sizeof rxmatch.
54  */
55 void regex_sub_to_request(REQUEST *request, regex_t **preg, char const *value, size_t len,
56  regmatch_t rxmatch[], size_t nmatch)
57 {
58  regcapture_t *old_sc, *new_sc; /* lldb doesn't like new *sigh* */
59  char *p;
60 
61  /*
62  * Clear out old_sc matches
63  */
64  old_sc = request_data_get(request, request, REQUEST_DATA_REGEX);
65  if (old_sc) {
66  DEBUG4("Clearing %zu matches", old_sc->nmatch);
67  talloc_free(old_sc);
68  } else {
69  DEBUG4("No matches");
70  }
71 
72  if (nmatch == 0) return;
73 
74  rad_assert(preg && *preg);
75  rad_assert(rxmatch);
76 
77  DEBUG4("Adding %zu matches", nmatch);
78 
79  /*
80  * Add new_sc matches
81  */
82  MEM(new_sc = talloc(request, regcapture_t));
83 
84  MEM(new_sc->rxmatch = talloc_memdup(new_sc, rxmatch, sizeof(rxmatch[0]) * nmatch));
85  talloc_set_type(new_sc->rxmatch, regmatch_t[]);
86 
87  MEM(p = talloc_array(new_sc, char, len + 1));
88  memcpy(p, value, len);
89  p[len] = '\0';
90  new_sc->value = p;
91  new_sc->nmatch = nmatch;
92 
93 #ifdef HAVE_PCRE
94  if (!(*preg)->precompiled) {
95  new_sc->preg = talloc_steal(new_sc, *preg);
96  *preg = NULL;
97  } else
98 #endif
99  {
100  new_sc->preg = *preg;
101  }
102 
103  request_data_add(request, request, REQUEST_DATA_REGEX, new_sc, true, false, false);
104 }
105 
106 # ifdef HAVE_PCRE
107 /** Extract a subcapture value from the request
108  *
109  * @note This is the PCRE variant of the function.
110  *
111  * @param ctx To allocate subcapture buffer in.
112  * @param out Where to write the subcapture string.
113  * @param request to extract.
114  * @param num Subcapture index (0 for entire match).
115  * @return
116  * - 0 on success.
117  * - -1 on notfound.
118  */
119 int regex_request_to_sub(TALLOC_CTX *ctx, char **out, REQUEST *request, uint32_t num)
120 {
121  regcapture_t *cap;
122  char const *p;
123  int ret;
124 
125  cap = request_data_reference(request, request, REQUEST_DATA_REGEX);
126  if (!cap) {
127  RDEBUG4("No subcapture data found");
128  *out = NULL;
129  return 1;
130  }
131 
132  ret = pcre_get_substring(cap->value, (int *)cap->rxmatch, (int)cap->nmatch, num, &p);
133  switch (ret) {
134  case PCRE_ERROR_NOMEMORY:
135  MEM(NULL);
136 
137  /*
138  * Not finding a substring is fine
139  */
140  case PCRE_ERROR_NOSUBSTRING:
141  RDEBUG4("%i/%zu Not found", num, cap->nmatch);
142  *out = NULL;
143  return -1;
144 
145  default:
146  if (ret < 0) {
147  *out = NULL;
148  return -1;
149  }
150 
151  /*
152  * Check libpcre really is using our overloaded
153  * malloc/free talloc wrappers.
154  */
155  p = (char *)talloc_get_type_abort(p, uint8_t);
156  talloc_set_type(p, char *);
157  talloc_steal(ctx, p);
158  memcpy(out, &p, sizeof(*out));
159 
160  RDEBUG4("%i/%zu Found: %s (%zu)", num, cap->nmatch, p, talloc_array_length(p));
161 
162  return 0;
163  }
164 }
165 
166 /** Extract a named subcapture value from the request
167  *
168  * @note This is the PCRE variant of the function.
169  *
170  * @param ctx To allocate subcapture buffer in.
171  * @param out Where to write the subcapture string.
172  * @param request to extract.
173  * @param name of subcapture.
174  * @return
175  * - 0 on success.
176  * - -1 on notfound.
177  */
178 int regex_request_to_sub_named(TALLOC_CTX *ctx, char **out, REQUEST *request, char const *name)
179 {
180  regcapture_t *cap;
181  char const *p;
182  int ret;
183 
184  cap = request_data_reference(request, request, REQUEST_DATA_REGEX);
185  if (!cap) {
186  RDEBUG4("No subcapture data found");
187  *out = NULL;
188  return 1;
189  }
190 
191  ret = pcre_get_named_substring(cap->preg->compiled, cap->value,
192  (int *)cap->rxmatch, (int)cap->nmatch, name, &p);
193  switch (ret) {
194  case PCRE_ERROR_NOMEMORY:
195  MEM(NULL);
196 
197  /*
198  * Not finding a substring is fine
199  */
200  case PCRE_ERROR_NOSUBSTRING:
201  RDEBUG4("No named capture group \"%s\"", name);
202  *out = NULL;
203  return -1;
204 
205  default:
206  if (ret < 0) {
207  *out = NULL;
208  return -1;
209  }
210 
211  /*
212  * Check libpcre really is using our overloaded
213  * malloc/free talloc wrappers.
214  */
215  p = (char *)talloc_get_type_abort(p, uint8_t);
216  talloc_set_type(p, char *);
217  talloc_steal(ctx, p);
218  memcpy(out, &p, sizeof(*out));
219 
220  RDEBUG4("Found \"%s\": %s (%zu)", name, p, talloc_array_length(p));
221 
222  return 0;
223  }
224 }
225 # else
226 /** Extract a subcapture value from the request
227  *
228  * @note This is the POSIX variant of the function.
229  *
230  * @param ctx To allocate subcapture buffer in.
231  * @param out Where to write the subcapture string.
232  * @param request to extract.
233  * @param num Subcapture index (0 for entire match).
234  * @return
235  * - 0 on success.
236  * - -1 on notfound.
237  */
238 int regex_request_to_sub(TALLOC_CTX *ctx, char **out, REQUEST *request, uint32_t num)
239 {
240  regcapture_t *cap;
241  char *p;
242  char const *start;
243  size_t len;
244 
245  cap = request_data_reference(request, request, REQUEST_DATA_REGEX);
246  if (!cap) {
247  RDEBUG4("No subcapture data found");
248  *out = NULL;
249  return -1;
250  }
251 
252  /*
253  * Greater than our capture array
254  *
255  * -1 means no value in this capture group.
256  */
257  if ((num >= cap->nmatch) || (cap->rxmatch[num].rm_eo == -1) || (cap->rxmatch[num].rm_so == -1)) {
258  RDEBUG4("%i/%zu Not found", num, cap->nmatch);
259  *out = NULL;
260  return -1;
261  }
262 
263  /*
264  * Sanity checks on the offsets
265  */
266  rad_assert(cap->rxmatch[num].rm_eo <= (regoff_t)talloc_array_length(cap->value));
267  rad_assert(cap->rxmatch[num].rm_so <= (regoff_t)talloc_array_length(cap->value));
268 
269  start = cap->value + cap->rxmatch[num].rm_so;
270  len = cap->rxmatch[num].rm_eo - cap->rxmatch[num].rm_so;
271 
272  RDEBUG4("%i/%zu Found: %.*s (%zu)", num, cap->nmatch, (int)len, start, len);
273  MEM(p = talloc_array(ctx, char, len + 1));
274  memcpy(p, start, len);
275  p[len] = '\0';
276 
277  *out = p;
278 
279  return 0;
280 }
281 # endif
282 #endif
#define MEM(x)
Definition: radiusd.h:396
static char const * name
void * request_data_reference(REQUEST *request, void *unique_ptr, int unique_int)
Get opaque data from a request without removing it.
Definition: request.c:484
#define rad_assert(expr)
Definition: rad_assert.h:38
void * request_data_get(REQUEST *request, void *unique_ptr, int unique_int)
Get opaque data from a request.
Definition: request.c:374
#define DEBUG4(fmt,...)
Definition: log.h:178
int request_data_add(REQUEST *request, void *unique_ptr, int unique_int, void *opaque, bool free_on_replace, bool free_on_parent, bool persist)
Add opaque data to a REQUEST.
Definition: request.c:279
#define RDEBUG4(fmt,...)
Definition: log.h:246
#define RCSID(id)
Definition: build.h:135