1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/localauth_rule.c - rule localauth module */
3 /*
4 * Copyright (C) 1990,1991,2007,2008,2013 by the Massachusetts
5 * Institute of Technology. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * This module implements the RULE type for auth_to_local processing.
35 *
36 * There are three parts to each rule. The first part, if present, determines
37 * the selection string. If this is not present, the selection string defaults
38 * to the unparsed principal name without realm (which can be dangerous in
39 * multi-realm environments, but is our historical behavior). The selection
40 * string syntax is:
41 *
42 * "[" <ncomps> ":" <format> "]"
43 *
44 * <ncomps> is the number of expected components for this rule. If the
45 * principal does not have this many components, then this rule does
46 * not apply.
47 *
48 * <format> determines the selection string. Within <format>, $0 will
49 * be substituted with the principal's realm, $1 with its first
50 * component, $2 with its second component, and so forth.
51 *
52 * The second part is an optional regular expression surrounded by parentheses.
53 * If present, the rule will only apply if the selection string matches the
54 * regular expression. At present, the regular expression may not contain a
55 * ')' character.
56 *
57 * The third part is a sequence of zero or more transformation rules, using
58 * the syntax:
59 *
60 * "s/" <regexp> "/" <text> "/" ["g"]
61 *
62 * No substitutions are allowed within <text>. A "g" indicates that the
63 * substitution should be performed globally; otherwise it will be performed at
64 * most once.
65 */
66
67 #include "k5-int.h"
68 #include "os-proto.h"
69 #include <krb5/localauth_plugin.h>
70 #include <ctype.h>
71 #include "k5-regex.h"
72
73 /* Process the match portion of a rule and update *contextp. Return
74 * KRB5_LNAME_NOTRANS if selstring doesn't match the regexp. */
75 static krb5_error_code
aname_do_match(const char * selstring,const char ** contextp)76 aname_do_match(const char *selstring, const char **contextp)
77 {
78 krb5_error_code ret;
79 const char *startp, *endp;
80 char *regstr;
81 regex_t re;
82 regmatch_t m;
83
84 /* If no regexp is present, leave *contextp alone and return success. */
85 if (**contextp != '(')
86 return 0;
87
88 /* Find the end of the regexp and make a copy of it. */
89 startp = *contextp + 1;
90 endp = strchr(startp, ')');
91 if (endp == NULL)
92 return KRB5_CONFIG_BADFORMAT;
93 regstr = k5memdup0(startp, endp - startp, &ret);
94 if (regstr == NULL)
95 return ret;
96
97 /* Perform the match. */
98 ret = (regcomp(&re, regstr, REG_EXTENDED) == 0 &&
99 regexec(&re, selstring, 1, &m, 0) == 0 &&
100 m.rm_so == 0 && (size_t)m.rm_eo == strlen(selstring)) ? 0 :
101 KRB5_LNAME_NOTRANS;
102 regfree(&re);
103 free(regstr);
104 *contextp = endp + 1;
105 return ret;
106 }
107
108 /* Replace regular expression matches of regstr with repl in instr, producing
109 * *outstr. If doall is true, replace all matches for regstr. */
110 static krb5_error_code
do_replacement(const char * regstr,const char * repl,krb5_boolean doall,const char * instr,char ** outstr)111 do_replacement(const char *regstr, const char *repl, krb5_boolean doall,
112 const char *instr, char **outstr)
113 {
114 struct k5buf buf;
115 regex_t re;
116 regmatch_t m;
117
118 *outstr = NULL;
119 if (regcomp(&re, regstr, REG_EXTENDED))
120 return KRB5_LNAME_NOTRANS;
121 k5_buf_init_dynamic(&buf);
122 while (regexec(&re, instr, 1, &m, 0) == 0) {
123 k5_buf_add_len(&buf, instr, m.rm_so);
124 k5_buf_add(&buf, repl);
125 instr += m.rm_eo;
126 if (!doall)
127 break;
128 }
129 regfree(&re);
130 k5_buf_add(&buf, instr);
131 *outstr = k5_buf_cstring(&buf);
132 return (*outstr == NULL) ? ENOMEM : 0;
133 }
134
135 /*
136 * Perform any substitutions specified by *contextp, and advance *contextp past
137 * the substitution expressions. Place the result of the substitutions in
138 * *result.
139 */
140 static krb5_error_code
aname_replacer(const char * string,const char ** contextp,char ** result)141 aname_replacer(const char *string, const char **contextp, char **result)
142 {
143 krb5_error_code ret = 0;
144 const char *cp, *ep, *tp;
145 char *newstr, *rule = NULL, *repl = NULL, *current = NULL;
146 krb5_boolean doglobal;
147
148 *result = NULL;
149
150 current = strdup(string);
151 if (current == NULL)
152 return ENOMEM;
153
154 /* Iterate over replacement expressions, updating current for each one. */
155 cp = *contextp;
156 while (*cp != '\0') {
157 /* Skip leading whitespace */
158 while (isspace((unsigned char)*cp))
159 cp++;
160
161 /* Find the separators for an s/rule/repl/ expression. */
162 if (!(cp[0] == 's' && cp[1] == '/' && (ep = strchr(cp + 2, '/')) &&
163 (tp = strchr(ep + 1, '/')))) {
164 ret = KRB5_CONFIG_BADFORMAT;
165 goto cleanup;
166 }
167
168 /* Copy the rule and replacement strings. */
169 free(rule);
170 rule = k5memdup0(cp + 2, ep - (cp + 2), &ret);
171 if (rule == NULL)
172 goto cleanup;
173 free(repl);
174 repl = k5memdup0(ep + 1, tp - (ep + 1), &ret);
175 if (repl == NULL)
176 goto cleanup;
177
178 /* Advance past expression and check for trailing "g". */
179 cp = tp + 1;
180 doglobal = (*cp == 'g');
181 if (doglobal)
182 cp++;
183
184 ret = do_replacement(rule, repl, doglobal, current, &newstr);
185 if (ret)
186 goto cleanup;
187 free(current);
188 current = newstr;
189 }
190 *result = current;
191 current = NULL;
192
193 cleanup:
194 free(current);
195 free(repl);
196 free(rule);
197 return ret;
198 }
199
200 /*
201 * Compute selection string for RULE rules. Advance *contextp to the string
202 * position after the selstring part if present, and set *result to the
203 * selection string.
204 */
205 static krb5_error_code
aname_get_selstring(krb5_context context,krb5_const_principal aname,const char ** contextp,char ** selstring_out)206 aname_get_selstring(krb5_context context, krb5_const_principal aname,
207 const char **contextp, char **selstring_out)
208 {
209 const char *current;
210 char *end;
211 long num_comps, ind;
212 const krb5_data *datap;
213 struct k5buf selstring;
214 size_t nlit;
215
216 *selstring_out = NULL;
217 if (**contextp != '[') {
218 /*
219 * No selstring part; use the principal name without realm. This is
220 * problematic in many multiple-realm environments, but is how we've
221 * historically done it.
222 */
223 return krb5_unparse_name_flags(context, aname,
224 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
225 selstring_out);
226 }
227
228 /* Advance past the '[' and read the number of components. */
229 current = *contextp + 1;
230 errno = 0;
231 num_comps = strtol(current, &end, 10);
232 if (errno != 0 || num_comps < 0 || *end != ':')
233 return KRB5_CONFIG_BADFORMAT;
234 current = end;
235 if (num_comps != aname->length)
236 return KRB5_LNAME_NOTRANS;
237 current++;
238
239 k5_buf_init_dynamic(&selstring);
240 while (TRUE) {
241 /* Copy in literal characters up to the next $ or ]. */
242 nlit = strcspn(current, "$]");
243 k5_buf_add_len(&selstring, current, nlit);
244 current += nlit;
245 if (*current != '$')
246 break;
247
248 /* Expand $ substitution to a principal component. */
249 errno = 0;
250 ind = strtol(current + 1, &end, 10);
251 if (errno || ind > num_comps)
252 break;
253 current = end;
254 datap = ind > 0 ? &aname->data[ind - 1] : &aname->realm;
255 k5_buf_add_len(&selstring, datap->data, datap->length);
256 }
257
258 /* Check that we hit a ']' and not the end of the string. */
259 if (*current != ']') {
260 k5_buf_free(&selstring);
261 return KRB5_CONFIG_BADFORMAT;
262 }
263
264 *selstring_out = k5_buf_cstring(&selstring);
265 if (*selstring_out == NULL)
266 return ENOMEM;
267 *contextp = current + 1;
268 return 0;
269 }
270
271 static krb5_error_code
an2ln_rule(krb5_context context,krb5_localauth_moddata data,const char * type,const char * rule,krb5_const_principal aname,char ** lname_out)272 an2ln_rule(krb5_context context, krb5_localauth_moddata data, const char *type,
273 const char *rule, krb5_const_principal aname, char **lname_out)
274 {
275 krb5_error_code ret;
276 const char *current;
277 char *selstring = NULL;
278
279 *lname_out = NULL;
280 if (rule == NULL)
281 return KRB5_CONFIG_BADFORMAT;
282
283 /* Compute the selection string. */
284 current = rule;
285 ret = aname_get_selstring(context, aname, ¤t, &selstring);
286 if (ret)
287 return ret;
288
289 /* Check the selection string against the regexp, if present. */
290 if (*current == '(') {
291 ret = aname_do_match(selstring, ¤t);
292 if (ret)
293 goto cleanup;
294 }
295
296 /* Perform the substitution. */
297 ret = aname_replacer(selstring, ¤t, lname_out);
298
299 cleanup:
300 free(selstring);
301 return ret;
302 }
303
304 static void
freestr(krb5_context context,krb5_localauth_moddata data,char * str)305 freestr(krb5_context context, krb5_localauth_moddata data, char *str)
306 {
307 free(str);
308 }
309
310 krb5_error_code
localauth_rule_initvt(krb5_context context,int maj_ver,int min_ver,krb5_plugin_vtable vtable)311 localauth_rule_initvt(krb5_context context, int maj_ver, int min_ver,
312 krb5_plugin_vtable vtable)
313 {
314 krb5_localauth_vtable vt = (krb5_localauth_vtable)vtable;
315 static const char *types[] = { "RULE", NULL };
316
317 vt->name = "rule";
318 vt->an2ln_types = types;
319 vt->an2ln = an2ln_rule;
320 vt->free_string = freestr;
321 return 0;
322 }
323