Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2013, WIDE Project and NICT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 9 | * permitted provided that the following conditions are met: * |
| 10 | * * |
| 11 | * * Redistributions of source code must retain the above * |
| 12 | * copyright notice, this list of conditions and the * |
| 13 | * following disclaimer. * |
| 14 | * * |
| 15 | * * Redistributions in binary form must reproduce the above * |
| 16 | * copyright notice, this list of conditions and the * |
| 17 | * following disclaimer in the documentation and/or other * |
| 18 | * materials provided with the distribution. * |
| 19 | * * |
| 20 | * * Neither the name of the WIDE Project or NICT nor the * |
| 21 | * names of its contributors may be used to endorse or * |
| 22 | * promote products derived from this software without * |
| 23 | * specific prior written permission of WIDE Project and * |
| 24 | * NICT. * |
| 25 | * * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 34 | *********************************************************************************************************/ |
| 35 | |
| 36 | /* |
| 37 | * This extension allows to perform some pattern-matching on an AVP |
| 38 | * and send the message to a server accordingly. |
| 39 | * See rt_ereg.conf.sample file for the format of the configuration file. |
| 40 | */ |
| 41 | |
| 42 | #include "rtereg.h" |
| 43 | |
| 44 | /* The configuration structure */ |
| 45 | struct rtereg_conf rtereg_conf; |
| 46 | |
| 47 | #ifndef HAVE_REG_STARTEND |
| 48 | static char * buf = NULL; |
| 49 | static size_t bufsz; |
| 50 | static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; |
| 51 | #endif /* HAVE_REG_STARTEND */ |
| 52 | |
| 53 | static int proceed(char * value, size_t len, struct fd_list * candidates) |
| 54 | { |
| 55 | int i; |
| 56 | |
| 57 | for (i = 0; i < rtereg_conf.rules_nb; i++) { |
| 58 | /* Does this pattern match the value? */ |
| 59 | struct rtereg_rule * r = &rtereg_conf.rules[i]; |
| 60 | int err = 0; |
| 61 | struct fd_list * c; |
| 62 | |
| 63 | TRACE_DEBUG(ANNOYING, "Attempt pattern matching of '%.*s' with rule '%s'", (int)len, value, r->pattern); |
| 64 | |
| 65 | #ifdef HAVE_REG_STARTEND |
| 66 | { |
| 67 | regmatch_t pmatch[1]; |
| 68 | memset(pmatch, 0, sizeof(pmatch)); |
| 69 | pmatch[0].rm_so = 0; |
| 70 | pmatch[0].rm_eo = len; |
| 71 | err = regexec(&r->preg, value, 0, pmatch, REG_STARTEND); |
| 72 | } |
| 73 | #else /* HAVE_REG_STARTEND */ |
| 74 | { |
| 75 | /* We have a 0-terminated string */ |
| 76 | err = regexec(&r->preg, value, 0, NULL, 0); |
| 77 | } |
| 78 | #endif /* HAVE_REG_STARTEND */ |
| 79 | |
| 80 | if (err == REG_NOMATCH) |
| 81 | continue; |
| 82 | |
| 83 | if (err != 0) { |
| 84 | char * errstr; |
| 85 | size_t bl; |
| 86 | |
| 87 | /* Error while compiling the regex */ |
| 88 | TRACE_DEBUG(INFO, "Error while executing the regular expression '%s':", r->pattern); |
| 89 | |
| 90 | /* Get the error message size */ |
| 91 | bl = regerror(err, &r->preg, NULL, 0); |
| 92 | |
| 93 | /* Alloc the buffer for error message */ |
| 94 | CHECK_MALLOC( errstr = malloc(bl) ); |
| 95 | |
| 96 | /* Get the error message content */ |
| 97 | regerror(err, &r->preg, errstr, bl); |
| 98 | TRACE_DEBUG(INFO, "\t%s", errstr); |
| 99 | |
| 100 | /* Free the buffer, return the error */ |
| 101 | free(errstr); |
| 102 | |
| 103 | return (err == REG_ESPACE) ? ENOMEM : EINVAL; |
| 104 | } |
| 105 | |
| 106 | /* From this point, the expression matched the AVP value */ |
| 107 | TRACE_DEBUG(FULL, "[rt_ereg] Match: '%s' to value '%.*s' => '%s' += %d", |
| 108 | r->pattern, |
| 109 | (int)len, |
| 110 | value, |
| 111 | r->server, |
| 112 | r->score); |
| 113 | |
| 114 | for (c = candidates->next; c != candidates; c = c->next) { |
| 115 | struct rtd_candidate * cand = (struct rtd_candidate *)c; |
| 116 | |
| 117 | if (strcmp(r->server, cand->diamid) == 0) { |
| 118 | cand->score += r->score; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* The callback called on new messages */ |
| 128 | static int rtereg_out(void * cbdata, struct msg ** pmsg, struct fd_list * candidates) |
| 129 | { |
| 130 | struct msg * msg = *pmsg; |
| 131 | struct avp * avp = NULL; |
| 132 | |
| 133 | TRACE_ENTRY("%p %p %p", cbdata, msg, candidates); |
| 134 | |
| 135 | CHECK_PARAMS(msg && candidates); |
| 136 | |
| 137 | /* Check if it is worth processing the message */ |
| 138 | if (FD_IS_LIST_EMPTY(candidates)) { |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* Now search the AVP in the message */ |
| 143 | CHECK_FCT( fd_msg_search_avp ( msg, rtereg_conf.avp, &avp ) ); |
| 144 | if (avp != NULL) { |
| 145 | struct avp_hdr * ahdr = NULL; |
| 146 | CHECK_FCT( fd_msg_avp_hdr ( avp, &ahdr ) ); |
| 147 | if (ahdr->avp_value != NULL) { |
| 148 | #ifndef HAVE_REG_STARTEND |
| 149 | int ret; |
| 150 | |
| 151 | /* Lock the buffer */ |
| 152 | CHECK_POSIX( pthread_mutex_lock(&mtx) ); |
| 153 | |
| 154 | /* Augment the buffer if needed */ |
| 155 | if (ahdr->avp_value->os.len >= bufsz) { |
| 156 | CHECK_MALLOC_DO( buf = realloc(buf, ahdr->avp_value->os.len + 1), |
| 157 | { pthread_mutex_unlock(&mtx); return ENOMEM; } ); |
| 158 | } |
| 159 | |
| 160 | /* Copy the AVP value */ |
| 161 | memcpy(buf, ahdr->avp_value->os.data, ahdr->avp_value->os.len); |
| 162 | buf[ahdr->avp_value->os.len] = '\0'; |
| 163 | |
| 164 | /* Now apply the rules */ |
| 165 | ret = proceed(buf, ahdr->avp_value->os.len, candidates); |
| 166 | |
| 167 | CHECK_POSIX(pthread_mutex_unlock(&mtx)); |
| 168 | |
| 169 | CHECK_FCT(ret); |
| 170 | #else /* HAVE_REG_STARTEND */ |
| 171 | CHECK_FCT( proceed((char *) ahdr->avp_value->os.data, ahdr->avp_value->os.len, candidates) ); |
| 172 | #endif /* HAVE_REG_STARTEND */ |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* handler */ |
| 180 | static struct fd_rt_out_hdl * rtereg_hdl = NULL; |
| 181 | |
| 182 | /* entry point */ |
| 183 | static int rtereg_entry(char * conffile) |
| 184 | { |
| 185 | TRACE_ENTRY("%p", conffile); |
| 186 | |
| 187 | /* Initialize the configuration */ |
| 188 | memset(&rtereg_conf, 0, sizeof(rtereg_conf)); |
| 189 | |
| 190 | /* Parse the configuration file */ |
| 191 | CHECK_FCT( rtereg_conf_handle(conffile) ); |
| 192 | |
| 193 | /* Register the callback */ |
| 194 | CHECK_FCT( fd_rt_out_register( rtereg_out, NULL, 1, &rtereg_hdl ) ); |
| 195 | |
| 196 | /* We're done */ |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /* Unload */ |
| 201 | void fd_ext_fini(void) |
| 202 | { |
| 203 | int i; |
| 204 | TRACE_ENTRY(); |
| 205 | |
| 206 | /* Unregister the cb */ |
| 207 | CHECK_FCT_DO( fd_rt_out_unregister ( rtereg_hdl, NULL ), /* continue */ ); |
| 208 | |
| 209 | /* Destroy the data */ |
| 210 | if (rtereg_conf.rules) |
| 211 | for (i = 0; i < rtereg_conf.rules_nb; i++) { |
| 212 | free(rtereg_conf.rules[i].pattern); |
| 213 | free(rtereg_conf.rules[i].server); |
| 214 | regfree(&rtereg_conf.rules[i].preg); |
| 215 | } |
| 216 | free(rtereg_conf.rules); |
| 217 | #ifndef HAVE_REG_STARTEND |
| 218 | free(buf); |
| 219 | #endif /* HAVE_REG_STARTEND */ |
| 220 | |
| 221 | /* Done */ |
| 222 | return ; |
| 223 | } |
| 224 | |
| 225 | EXTENSION_ENTRY("rt_ereg", rtereg_entry); |