Imported Upstream version 2.6.1
[debian/amanda] / common-src / match.c
index f9ce31ded98bf71d8e8f78bc72b60636fe62957c..d8c83bcc7657ec16014ade4c0f35587bc4e20826 100644 (file)
@@ -99,6 +99,34 @@ match(
     return result == 0;
 }
 
+int
+match_no_newline(
+    const char *       regex,
+    const char *       str)
+{
+    regex_t regc;
+    int result;
+    char errmsg[STR_SIZE];
+
+    if((result = regcomp(&regc, regex,
+                        REG_EXTENDED|REG_NOSUB)) != 0) {
+        regerror(result, &regc, errmsg, SIZEOF(errmsg));
+       error(_("regex \"%s\": %s"), regex, errmsg);
+       /*NOTREACHED*/
+    }
+
+    if((result = regexec(&regc, str, 0, 0, 0)) != 0
+       && result != REG_NOMATCH) {
+        regerror(result, &regc, errmsg, SIZEOF(errmsg));
+       error(_("regex \"%s\": %s"), regex, errmsg);
+       /*NOTREACHED*/
+    }
+
+    regfree(&regc);
+
+    return result == 0;
+}
+
 char *
 validate_glob(
     const char *       glob)
@@ -530,6 +558,17 @@ match_disk(
     return match_word(glob, disk, '/');
 }
 
+static int
+alldigits(
+    const char *str)
+{
+    while (*str) {
+       if (!isdigit((int)*(str++)))
+           return 0;
+    }
+    return 1;
+}
+
 int
 match_datestamp(
     const char *       dateexp,
@@ -543,45 +582,56 @@ match_datestamp(
     int match_exact;
 
     if(strlen(dateexp) >= 100 || strlen(dateexp) < 1) {
-       error(_("Illegal datestamp expression %s"),dateexp);
-       /*NOTREACHED*/
+       goto illegal;
     }
    
+    /* strip and ignore an initial "^" */
     if(dateexp[0] == '^') {
-       strncpy(mydateexp, dateexp+1, strlen(dateexp)-1); 
-       mydateexp[strlen(dateexp)-1] = '\0';
+       strncpy(mydateexp, dateexp+1, sizeof(mydateexp)-1);
+       mydateexp[sizeof(mydateexp)-1] = '\0';
     }
     else {
-       strncpy(mydateexp, dateexp, strlen(dateexp));
-       mydateexp[strlen(dateexp)] = '\0';
+       strncpy(mydateexp, dateexp, sizeof(mydateexp)-1);
+       mydateexp[sizeof(mydateexp)-1] = '\0';
     }
 
-    if(mydateexp[strlen(mydateexp)] == '$') {
+    if(mydateexp[strlen(mydateexp)-1] == '$') {
        match_exact = 1;
-       mydateexp[strlen(mydateexp)] = '\0';
+       mydateexp[strlen(mydateexp)-1] = '\0';  /* strip the trailing $ */
     }
     else
        match_exact = 0;
 
+    /* a single dash represents a date range */
     if((dash = strchr(mydateexp,'-'))) {
-       if(match_exact == 1) {
-           error(_("Illegal datestamp expression %s"),dateexp);
-           /*NOTREACHED*/
+       if(match_exact == 1 || strchr(dash+1, '-')) {
+           goto illegal;
        }
-       len = (size_t)(dash - mydateexp);
-       len_suffix = strlen(dash) - 1;
-       len_prefix = len - len_suffix;
+
+       /* format: XXXYYYY-ZZZZ, indicating dates XXXYYYY to XXXZZZZ */
+
+       len = (size_t)(dash - mydateexp);   /* length of XXXYYYY */
+       len_suffix = strlen(dash) - 1;  /* length of ZZZZ */
+       if (len_suffix > len) goto illegal;
+       len_prefix = len - len_suffix; /* length of XXX */
 
        dash++;
+
        strncpy(firstdate, mydateexp, len);
        firstdate[len] = '\0';
        strncpy(lastdate, mydateexp, len_prefix);
        strncpy(&(lastdate[len_prefix]), dash, len_suffix);
        lastdate[len] = '\0';
+       if (!alldigits(firstdate) || !alldigits(lastdate))
+           goto illegal;
+       if (strncmp(firstdate, lastdate, strlen(firstdate)) > 0)
+           goto illegal;
        return ((strncmp(datestamp, firstdate, strlen(firstdate)) >= 0) &&
                (strncmp(datestamp, lastdate , strlen(lastdate))  <= 0));
     }
     else {
+       if (!alldigits(mydateexp))
+           goto illegal;
        if(match_exact == 1) {
            return (strcmp(datestamp, mydateexp) == 0);
        }
@@ -589,6 +639,9 @@ match_datestamp(
            return (strncmp(datestamp, mydateexp, strlen(mydateexp)) == 0);
        }
     }
+illegal:
+       error(_("Illegal datestamp expression %s"),dateexp);
+       /*NOTREACHED*/
 }