summaryrefslogtreecommitdiff
path: root/common/expr.c
diff options
context:
space:
mode:
authorThorsten Glaser <t.glaser@tarent.de>2012-12-03 16:16:52 +0100
committerArthur de Jong <arthur@arthurdejong.org>2013-03-03 15:54:09 +0100
commit373196473d567f0264a1af61f30847cbe96985ca (patch)
tree8ad5a193d3d97243b37faf6e09f369ca683f535b /common/expr.c
parentf56f9267469fe6556fc946036db003b5ca85a7eb (diff)
allow trimming expressions with ${foo#bar} syntax in nslcd
Diffstat (limited to 'common/expr.c')
-rw-r--r--common/expr.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/common/expr.c b/common/expr.c
index 97fcff8..a71a5e9 100644
--- a/common/expr.c
+++ b/common/expr.c
@@ -3,6 +3,7 @@
This file is part of the nss-pam-ldapd library.
Copyright (C) 2009, 2010, 2011, 2012 Arthur de Jong
+ Copyright (c) 2012 Thorsten Glaser <t.glaser@tarent.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -142,6 +143,67 @@ MUST_USE static const char *parse_dollar_expression(
buffer[0] = '\0';
}
}
+ else if (str[*ptr]=='#')
+ {
+ char c;
+ const char *cp, *vp;
+ int ismatch;
+ size_t vallen;
+
+ (*ptr)+=1;
+ cp=str+*ptr;
+ vp=varvalue;
+ ismatch=1;
+ while ((c=*cp++) && c!='}')
+ {
+ if (ismatch && !*vp)
+ {
+ /* varvalue shorter than trim string */
+ ismatch=0;
+ }
+ if (c=='?')
+ {
+ /* match any one character */
+ ++vp;
+ continue;
+ }
+ if (c=='\\')
+ {
+ if (!(c=*cp++))
+ {
+ /* end of input: syntax error */
+ return NULL;
+ }
+ /* escape the next character c */
+ }
+ if (ismatch && *vp!=c)
+ {
+ /* they differ */
+ ismatch=0;
+ }
+ ++vp;
+ }
+ if (!c)
+ {
+ /* end of input: syntax error */
+ return NULL;
+ }
+ /*
+ * at this point, cp points to after the closing }
+ * if ismatch, vp points to the beginning of the
+ * data after trimming, otherwise vp is invalid
+ */
+ --cp;
+ (*ptr)=cp-str;
+ if (!ismatch)
+ {
+ vp=varvalue;
+ }
+ /* now copy the (trimmed or not) value to the buffer */
+ if ((vallen=strlen(vp)+1)>buflen)
+ return NULL;
+ memcpy(buffer,vp,vallen);
+ }
else
return NULL;
(*ptr)++; /* skip closing } */