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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/* hey emacs! -*- Mode: C; c-file-style: "k&r"; indent-tabs-mode: nil -*- */
/*
* tftpd_pcre.c
* functions to remmap file name requested by tftp clients according
* to regular expression rules
*
* $Id: tftpd_pcre.c,v 1.2 2003/04/25 00:16:19 jp Exp $
*
* Copyright (c) 2003 Jean-Pierre Lefebvre <helix@step.polymtl.ca>
* and Remi Lefebvre <remi@debian.org>
*
* The PCRE code is provided by Jeff Miller <jeff.miller@transact.com.au>
*
* Copyright (c) 2003 Jeff Miller <jeff.miller@transact.com.au>
*
* atftp is free software; you can redistribute them and/or modify them
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
*/
#include "config.h"
#if HAVE_PCRE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "tftp_def.h"
#include "config.h"
#include "logger.h"
#include "tftpd_pcre.h"
/*
* number of elements in vector to hold substring info
*/
#define OVECCOUNT 30
/*
* define the pattern for substitutions
* $0 whole string
* $1 to $9 substring 1 - 9
*/
/* create a pattern list from a file */
/* return 0 on success, -1 otherwise */
tftpd_pcre_self_t *tftpd_pcre_open(char *filename)
{
int linecount;
int erroffset;
int matches;
int ovector[OVECCOUNT];
char line[MAXLEN];
const char *error;
FILE *fh;
int subnum;
char **substrlist;
pcre *file_re;
pcre_extra *file_pe;
tftpd_pcre_self_t *self;
tftpd_pcre_pattern_t *pat, **curpatp;
/* open file */
if ((fh = fopen(filename, "r")) == NULL)
{
logger(LOG_ERR, "Cannot open %s for reading: %s",
filename, strerror(errno));
return NULL;
}
/* compile and study pattern for lines */
logger(LOG_DEBUG, "Using file pattern %s", TFTPD_PCRE_FILE_PATTERN);
if ((file_re = pcre_compile(TFTPD_PCRE_FILE_PATTERN, 0,
&error, &erroffset, NULL)) == NULL)
{
logger(LOG_ERR, "PCRE file pattern failed to compile");
return NULL;
}
file_pe = pcre_study(file_re, 0, &error);
if (error != NULL)
{
logger(LOG_ERR, "PCRE file pattern failed to study");
return NULL;
}
/* allocate header and copy info */
if ((self = calloc(1, sizeof(tftpd_pcre_self_t))) == NULL)
{
logger(LOG_ERR, "calloc filed");
return NULL;
}
self->lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
Strncpy(self->filename, filename, MAXLEN);
/* read patterns */
for (linecount = 1, curpatp = &self->list;
fgets(line, MAXLEN, fh) != NULL;
linecount++, curpatp = &pat->next)
{
logger(LOG_DEBUG,"file: %s line: %d value: %s",
filename, linecount, line);
/* allocate space for pattern info */
if ((pat = (tftpd_pcre_pattern_t *)calloc(1,sizeof(tftpd_pcre_pattern_t))) == NULL)
{
tftpd_pcre_close(self);
return NULL;
}
*curpatp = pat;
/* for each pattern read, compile and store the pattern */
matches = pcre_exec(file_re, file_pe, line, (int)(strlen(line)),
0, 0, ovector, OVECCOUNT);
/* log substring to help with debugging */
pcre_get_substring_list(line, ovector, matches, (const char ***)&substrlist);
for(subnum = 0; subnum <= matches; subnum++)
{
logger(LOG_DEBUG,"file: %s line: %d substring: %d value: %s",
filename, linecount, subnum, substrlist[subnum]);
}
pcre_free_substring_list((const char **)substrlist);
if (matches < 2)
{
logger(LOG_ERR, "error with pattern in file \"%s\" line %d",
filename, linecount);
tftpd_pcre_close(self);
pcre_free(file_re);
pcre_free(file_pe);
return NULL;
}
/* remember line number */
pat->linenum = linecount;
/* extract left side */
pcre_get_substring(line, ovector, matches, 1, (const char **)&pat->pattern);
/* extract right side */
pcre_get_substring(line, ovector, matches, 2, (const char **)&pat->right_str);
logger(LOG_DEBUG,"pattern: %s right_str: %s", pat->pattern, pat->right_str);
if ((pat->left_re = pcre_compile(pat->pattern, 0,
&error, &erroffset, NULL)) == NULL)
{
/* compilation failed*/
logger(LOG_ERR,
"PCRE compilation failed in file \"%s\" line %d at %d: %s",
filename, linecount,
erroffset, error);
/* close file */
fclose(fh);
/* clean up */
tftpd_pcre_close(self);
return NULL;
}
/* we're going to be using this pattern a fair bit so lets study it */
pat->left_pe = pcre_study(pat->left_re, 0, &error);
if (error != NULL)
{
logger(LOG_ERR,
"PCRE study failed in file \"%s\" line %d: %s",
filename, linecount,
error);
/* close file */
fclose(fh);
/* cleanup */
tftpd_pcre_close(self);
return NULL;
}
}
/* clean up */
pcre_free(file_re);
pcre_free(file_pe);
/* close file */
fclose(fh);
return self;
}
/* return filename being used */
/* returning a char point directly is a little risking when
* using thread, but as we're using this before threads
* are created we should be able to get away with it
*/
char *tftpd_pcre_getfilename(tftpd_pcre_self_t *self)
{
return self->filename;
}
/* this is a utility function used to make the actual substitution*/
int tftpd_pcre_makesub(struct tftpd_pcre_pattern *pat,
char *outstr, int outsize,
char *str,
int *ovector, int matches)
{
char *chp, *outchp;
const char *tmpstr;
int rc;
/* $0 - whole string, $1-$9 substring 1-9 */
for (chp = pat->right_str, outchp = outstr;
(*chp != '\0') && (outchp - outstr < outsize);
chp++)
{
if ((*chp == '$') && (*(chp+1) >= '0') && (*(chp+1) <= '9'))
{
chp++; /* point to value indicating substring */
rc = pcre_get_substring(str, ovector, matches, *chp - 0x30, &tmpstr);
/* found string */
if (rc > 0 && outchp - outstr + rc+1 < outsize)
{
Strncpy(outchp, tmpstr, rc+1);
outchp += rc;
pcre_free_substring(tmpstr);
continue;
}
/* erro condition */
switch (rc)
{
case PCRE_ERROR_NOMEMORY:
logger(LOG_ERR, "PCRE out of memory");
break;
case PCRE_ERROR_NOSUBSTRING:
logger(LOG_ERR,
"PCRE attempted substitution failed for \"%s\" on pattern %d",
str, pat->linenum);
break;
}
}
else
{
*outchp = *chp;
outchp++;
}
}
*outchp = '\0';
return 0;
}
/* search for a replacement and return a string after substituation */
/* if no match is found return -1 */
int tftpd_pcre_sub(tftpd_pcre_self_t *self, char *outstr, int outlen, char *str)
{
int rc;
int ovector[OVECCOUNT];
int matches;
tftpd_pcre_pattern_t *pat;
/* lock for duration */
pthread_mutex_lock(&self->lock);
logger(LOG_DEBUG, "Looking to match \"%s\"", str);
/* interate over pattern list */
for(pat = self->list; pat != NULL; pat = pat->next)
{
logger(LOG_DEBUG,"Attempting to match \"%s\"", pat->pattern);
/* attempt match */
matches = pcre_exec(pat->left_re, pat->left_pe,
str, (int)(strlen(str)),
0, 0,
ovector, OVECCOUNT);
/* no match so we try again */
if (matches == PCRE_ERROR_NOMATCH)
continue;
/* error in making a match - log and attempt to continue */
if (matches < 0)
{
logger(LOG_WARNING,
"PCRE Matching error %d", matches);
continue;
}
/* we have a match - carry out substitution */
logger(LOG_DEBUG,"Pattern \"%s\" matches", pat->pattern);
rc = tftpd_pcre_makesub(pat,
outstr, outlen,
str,
ovector, matches);
logger(LOG_DEBUG,"outstr: \"%s\"", outstr);
pthread_mutex_unlock(&self->lock);
return 0;
}
logger(LOG_DEBUG, "Failed to match \"%s\"", str);
pthread_mutex_unlock(&self->lock);
return -1;
}
/* clean up and displose of anything we set up*/
void tftpd_pcre_close(tftpd_pcre_self_t *self)
{
tftpd_pcre_pattern_t *next, *cur;
/* free up list */
pthread_mutex_lock(&self->lock);
cur = self->list;
while (cur != NULL)
{
next = cur->next;
pcre_free_substring(cur->pattern);
pcre_free(cur->left_re);
pcre_free(cur->left_pe);
pcre_free_substring(cur->right_str);
free(cur);
cur = next;
}
pthread_mutex_unlock(&self->lock);
free(self);
}
#endif
|