From 0ff68170254ca1d5e29147650ab32d2b5b1792ee Mon Sep 17 00:00:00 2001 From: Gregory Duchatelet Date: Wed, 15 May 2019 10:31:15 +0200 Subject: [PATCH] new option: -u, parse a user crontab --- chkcrontab | 2 ++ chkcrontab_lib.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/chkcrontab b/chkcrontab index 3ad4291..f09ca3c 100755 --- a/chkcrontab +++ b/chkcrontab @@ -46,6 +46,8 @@ def main(argv): dest='whitelisted_users', help='user to ignore when warning of unrecognized users; may be passed multiple times', default=['postgres', 'buildbot']) + parser.add_argument('-u', '--user', action='store_true', + dest='user', help='crontab user') parser.add_argument('-n', '--no-check-passwd', action='store_false', dest='check_passwd', help='disable user lookup') parser.add_argument('-q', '--quiet', action='store_true', diff --git a/chkcrontab_lib.py b/chkcrontab_lib.py index 344dbd7..880f98d 100755 --- a/chkcrontab_lib.py +++ b/chkcrontab_lib.py @@ -716,6 +716,8 @@ def ValidateAndLog(self, log): # User checks. if self.user in self.whitelisted_users: pass + elif self.user is False: + pass elif len(self.user) > 31: log.LineError(log.MSG_INVALID_USER, 'Username too long "%s"' % self.user) @@ -806,7 +808,8 @@ def ValidateAndLog(self, log): class CronLineFactory(object): """Classify a line in a cron field by what type of line it is.""" - def __init__(self): + def __init__(self, is_user_crontab=False): + self.is_user_crontab = is_user_crontab pass def ParseLine(self, line, options ): @@ -857,7 +860,10 @@ def ParseLine(self, line, options ): 'month': match.groups()[3], 'day of week': match.groups()[4], } - return CronLineTime(field, match.groups()[5], match.groups()[6], options) + if self.is_user_crontab: + return CronLineTime(field, False, match.groups()[5] + " " + match.groups()[6], options) + else: + return CronLineTime(field, match.groups()[5], match.groups()[6], options) return CronLineUnknown() @@ -1100,7 +1106,7 @@ def check_crontab(arguments, log): ' [A-Za-z0-9_-]+ .') line_no = 0 - cron_line_factory = CronLineFactory() + cron_line_factory = CronLineFactory(arguments.user) with open(arguments.crontab, 'r') as crontab_f: for line in crontab_f: missing_newline = line[-1] != "\n"