1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*
SSSD
Authors:
Tomas Halman <thalman@redhat.com>
Copyright (C) 2019 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "util/sss_regexp.h"
#include <string.h>
#include "util/util_errors.h"
#include "util/debug.h"
#define SSS_REGEXP_OVEC_SIZE 30
#define SSS_REGEXP_ERR_MSG_SIZE 120 /* 120 is recomended by pcre2 doc */
#ifndef EOK
#define EOK 0
#endif
/*
* sss_regexp with pcre2
*/
struct _sss_regexp_t {
pcre2_code *re;
pcre2_match_data *match_data;
char *matched_string;
};
static int sss_regexp_pcre2_destroy(sss_regexp_t *self)
{
if (self->re) {
pcre2_code_free(self->re);
}
if (self->match_data) {
pcre2_match_data_free(self->match_data);
}
if (self->matched_string) {
pcre2_substring_free((PCRE2_UCHAR *)self->matched_string);
}
return 0;
}
static int sss_regexp_pcre2_compile(sss_regexp_t *self,
const char *pattern,
int options)
{
int errorcode;
unsigned char errormsg[SSS_REGEXP_ERR_MSG_SIZE];
size_t erroroffset;
self->re = pcre2_compile((PCRE2_SPTR)pattern,
strlen(pattern),
options,
&errorcode,
&erroroffset,
NULL);
if (self->re == NULL) {
pcre2_get_error_message(errorcode,
errormsg,
SSS_REGEXP_ERR_MSG_SIZE);
DEBUG(SSSDBG_CRIT_FAILURE, "Invalid Regular Expression pattern "
"at position %zu. (Error: %d [%s])\n", erroroffset, errorcode, errormsg);
return errorcode;
}
return EOK;
}
static int sss_regexp_pcre2_match(sss_regexp_t *self,
const char *subject,
int startoffset,
int options)
{
if (!self->re) {
return SSS_REGEXP_ERROR_NOMATCH;
}
if (self->match_data) {
pcre2_match_data_free(self->match_data);
}
self->match_data = pcre2_match_data_create_from_pattern(self->re, NULL);
if (!self->match_data) {
return SSS_REGEXP_ERROR_NOMEMORY;
}
return pcre2_match(self->re,
(PCRE2_SPTR)subject,
strlen(subject),
startoffset,
options,
self->match_data,
NULL);
}
static int sss_regexp_pcre2_get_named_substring(sss_regexp_t *self,
const char *name,
const char **value)
{
PCRE2_SIZE length;
int rc;
if (self->matched_string) {
pcre2_substring_free((PCRE2_UCHAR *)(self->matched_string));
self->matched_string = NULL;
}
rc = pcre2_substring_get_byname(self->match_data,
(PCRE2_SPTR)name,
(PCRE2_UCHAR **) &self->matched_string,
&length);
*value = self->matched_string;
return rc;
}
/*
* sss_regexp talloc destructor
*/
static int sss_regexp_destroy(sss_regexp_t *self)
{
if (!self) return 0;
return sss_regexp_pcre2_destroy(self);
}
/*
* sss_regexp constructor
*/
int sss_regexp_new(TALLOC_CTX *mem_ctx,
const char *pattern,
int options,
sss_regexp_t **self_p)
{
int ret;
sss_regexp_t *self = talloc_zero(mem_ctx, sss_regexp_t);
if (!self) {
DEBUG(SSSDBG_CRIT_FAILURE, "Not enough memory for sss_regexp_t.\n");
*self_p = NULL;
return SSS_REGEXP_ERROR_NOMEMORY;
}
talloc_set_destructor(self, sss_regexp_destroy);
ret = sss_regexp_pcre2_compile(self,
pattern,
options);
if (ret != EOK) {
talloc_free(self);
self = NULL;
}
*self_p = self;
return ret;
}
/*
* sss_regexp match function
*/
int sss_regexp_match(sss_regexp_t *self,
const char *subject,
int startoffset,
int options)
{
if (!self || !self->re || !subject) return SSS_REGEXP_ERROR_NOMATCH;
return sss_regexp_pcre2_match(self, subject, startoffset, options);
}
/*
* sss_regexp get named substring
*/
int sss_regexp_get_named_substring(sss_regexp_t *self,
const char *name,
const char **value)
{
if (!self || !self->re || !name) {
*value = NULL;
return SSS_REGEXP_ERROR_NOMATCH;
}
return sss_regexp_pcre2_get_named_substring(self, name, value);
}
|