Skip to content

Commit

Permalink
core: prevents a director crash when calling "configure add schedule"
Browse files Browse the repository at this point in the history
The code to parse a schedule run directive calls the scan_err function
in case of a parse error.
However, it did assume, that scan_err terminates the director.
While this is true when parsing at the start of the director,
it isn't true, when called by the "configure add" command.

This patch improves the error handling, by exiting the parse function
when an error is detected.
  • Loading branch information
joergsteffens committed Sep 24, 2020
1 parent ac09b09 commit a62206e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
7 changes: 7 additions & 0 deletions core/src/dird/dird_conf.cc
Expand Up @@ -2824,6 +2824,7 @@ static void StoreAuthprotocoltype(LEX* lc,
if (!found) {
scan_err1(lc, _("Expected a Auth Protocol Type keyword, got: %s"), lc->str);
}

ScanToEol(lc);
SetBit(index, (*item->allocated_resource)->item_present_);
ClearBit(index, (*item->allocated_resource)->inherit_content_);
Expand Down Expand Up @@ -3149,6 +3150,7 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass)

if (token != BCT_BOB) {
scan_err1(lc, _("Expecting open brace. Got %s"), lc->str);
return;
}

res_runscript = new RunScript();
Expand All @@ -3166,6 +3168,7 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass)

if (token != BCT_IDENTIFIER) {
scan_err1(lc, _("Expecting keyword, got: %s\n"), lc->str);
goto bail_out;
}

bool keyword_ok = false;
Expand All @@ -3174,6 +3177,7 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass)
token = LexGetToken(lc, BCT_SKIP_EOL);
if (token != BCT_EQUALS) {
scan_err1(lc, _("Expected an equals, got: %s"), lc->str);
goto bail_out;
}
switch (runscript_items[i].type) {
case CFG_TYPE_RUNSCRIPT_CMD:
Expand All @@ -3198,6 +3202,7 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass)

if (!keyword_ok) {
scan_err1(lc, _("Keyword %s not permitted in this resource"), lc->str);
goto bail_out;
}
}

Expand All @@ -3222,6 +3227,8 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass)
script->Debug();
}
}

bail_out:
/* for pass == 1 only delete the memory
because it is only used while parsing */
delete res_runscript;
Expand Down
57 changes: 37 additions & 20 deletions core/src/dird/run_conf.cc
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2016 Bareos GmbH & Co. KG
Copyright (C) 2013-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -202,7 +202,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
found = true;
if (LexGetToken(lc, BCT_ALL) != BCT_EQUALS) {
scan_err1(lc, _("Expected an equals, got: %s"), lc->str);
/* NOT REACHED */
return;
}
switch (RunFields[i].token) {
case 's': /* Data spooling */
Expand All @@ -216,6 +216,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
res_run->spool_data_set = true;
} else {
scan_err1(lc, _("Expect a YES or NO, got: %s"), lc->str);
return;
}
break;
case 'L': /* Level */
Expand All @@ -231,7 +232,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
if (j != 0) {
scan_err1(lc, _("Job level field: %s not found in run record"),
lc->str);
/* NOT REACHED */
return;
}
break;
case 'p': /* Priority */
Expand All @@ -250,7 +251,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
if (res == NULL) {
scan_err1(lc, _("Could not find specified Pool Resource: %s"),
lc->str);
/* NOT REACHED */
return;
}
switch (RunFields[i].token) {
case 'P':
Expand Down Expand Up @@ -282,7 +283,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
scan_err1(lc,
_("Could not find specified Storage Resource: %s"),
lc->str);
/* NOT REACHED */
return;
}
res_run->storage = (StorageResource*)res;
}
Expand All @@ -295,7 +296,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
scan_err1(lc,
_("Could not find specified Messages Resource: %s"),
lc->str);
/* NOT REACHED */
return;
}
res_run->msgs = (MessagesResource*)res;
}
Expand Down Expand Up @@ -325,7 +326,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
break;
default:
scan_err1(lc, _("Expected a keyword name, got: %s"), lc->str);
/* NOT REACHED */
return;
break;
} /* end switch */
} /* end if Bstrcasecmp */
Expand Down Expand Up @@ -364,6 +365,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
code = atoi(lc->str) - 1;
if (code < 0 || code > 30) {
scan_err0(lc, _("Day number out of range (1-31)"));
return;
}
break;
case BCT_NAME: /* This handles drop through from keyword */
Expand All @@ -385,7 +387,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
code = atoi(lc->str + 1);
if (code < 0 || code > 53) {
scan_err0(lc, _("Week number out of range (0-53)"));
/* NOT REACHED */
return;
}
state = s_woy; /* Week of year */
break;
Expand All @@ -404,14 +406,14 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
if (i != 0) {
scan_err1(lc, _("Job type field: %s in run record not found"),
lc->str);
/* NOT REACHED */
return;
}
break;
case BCT_COMMA:
continue;
default:
scan_err2(lc, _("Unexpected token: %d:%s"), token, lc->str);
/* NOT REACHED */
return;
break;
}
switch (state) {
Expand Down Expand Up @@ -455,14 +457,16 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
case s_time: /* Time */
if (!have_at) {
scan_err0(lc, _("Time must be preceded by keyword AT."));
/* NOT REACHED */
return;
}
if (!have_hour) {
ClearBitRange(0, 23, res_run->date_time_bitfield.hour);
}
if (!have_hour) { ClearBitRange(0, 23, res_run->date_time_bitfield.hour); }
// Dmsg1(000, "s_time=%s\n", lc->str);
p = strchr(lc->str, ':');
if (!p) {
scan_err0(lc, _("Time logic error.\n"));
/* NOT REACHED */
return;
}
*p++ = 0; /* Separate two halves */
code = atoi(lc->str); /* Pick up hour */
Expand All @@ -475,7 +479,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
am = true;
} else if (len != 2) {
scan_err0(lc, _("Bad time specification."));
/* NOT REACHED */
return;
}
/*
* Note, according to NIST, 12am and 12pm are ambiguous and
Expand All @@ -496,7 +500,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
}
if (code < 0 || code > 23 || code2 < 0 || code2 > 59) {
scan_err0(lc, _("Bad time specification."));
/* NOT REACHED */
return;
}
SetBit(code, res_run->date_time_bitfield.hour);
res_run->minute = code2;
Expand All @@ -514,7 +518,10 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
break;
case s_modulo:
p = strchr(lc->str, '/');
if (!p) { scan_err0(lc, _("Modulo logic error.\n")); }
if (!p) {
scan_err0(lc, _("Modulo logic error.\n"));
return;
}
*p++ = 0; /* Separate two halves */

if (IsAnInteger(lc->str) && IsAnInteger(p)) {
Expand All @@ -525,10 +532,12 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
code2 = atoi(p);
if (code < 0 || code > 30 || code2 < 0 || code2 > 30) {
scan_err0(lc, _("Bad day specification in modulo."));
return;
}
if (code > code2) {
scan_err0(lc, _("Bad day specification, offset must always be <= "
"than modulo."));
return;
}
if (!have_mday) {
ClearBitRange(0, 30, res_run->date_time_bitfield.mday);
Expand All @@ -553,10 +562,12 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
code2 = atoi(p + 1);
if (code < 0 || code > 53 || code2 < 0 || code2 > 53) {
scan_err0(lc, _("Week number out of range (0-53) in modulo"));
return;
}
if (code > code2) {
scan_err0(lc, _("Bad week number specification in modulo, offset "
"must always be <= than modulo."));
return;
}
if (!have_woy) {
ClearBitRange(0, 53, res_run->date_time_bitfield.woy);
Expand All @@ -573,11 +584,15 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
} else {
scan_err0(lc, _("Bad modulo time specification. Format for weekdays "
"is '01/02', for yearweeks is 'w01/w02'."));
return;
}
break;
case s_range:
p = strchr(lc->str, '-');
if (!p) { scan_err0(lc, _("Range logic error.\n")); }
if (!p) {
scan_err0(lc, _("Range logic error.\n"));
return;
}
*p++ = 0; /* Separate two halves */

if (IsAnInteger(lc->str) && IsAnInteger(p)) {
Expand All @@ -588,6 +603,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
code2 = atoi(p) - 1;
if (code < 0 || code > 30 || code2 < 0 || code2 > 30) {
scan_err0(lc, _("Bad day range specification."));
return;
}
if (!have_mday) {
ClearBitRange(0, 30, res_run->date_time_bitfield.mday);
Expand All @@ -610,6 +626,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
code2 = atoi(p + 1);
if (code < 0 || code > 53 || code2 < 0 || code2 > 53) {
scan_err0(lc, _("Week number out of range (0-53)"));
return;
}
if (!have_woy) {
ClearBitRange(0, 53, res_run->date_time_bitfield.woy);
Expand Down Expand Up @@ -637,7 +654,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
if (i != 0 ||
(state != s_month && state != s_wday && state != s_wom)) {
scan_err0(lc, _("Invalid month, week or position day range"));
/* NOT REACHED */
return;
}

/*
Expand All @@ -654,7 +671,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
}
if (i != 0 || state != state2 || code == code2) {
scan_err0(lc, _("Invalid month, weekday or position range"));
/* NOT REACHED */
return;
}
if (state == s_wday) {
if (!have_wday) {
Expand Down Expand Up @@ -718,7 +735,7 @@ void StoreRun(LEX* lc, ResourceItem* item, int index, int pass)
break;
default:
scan_err0(lc, _("Unexpected run state\n"));
/* NOT REACHED */
return;
break;
}
}
Expand Down

0 comments on commit a62206e

Please sign in to comment.