diff --git a/chkcrontab b/chkcrontab index 3ad4291..513ed10 100755 --- a/chkcrontab +++ b/chkcrontab @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # # Copyright 2011 Google Inc. All Rights Reserved. # @@ -31,21 +31,19 @@ import chkcrontab_lib as check from optparse import OptionParser from argparse import ArgumentParser -try: - from _version import __version__ -except ImportError: - __version__ = 'unknown' def main(argv): """main program.""" parser = ArgumentParser( description='Parse crontab files; check for potential syntax errors.', - version=__version__) + ) parser.add_argument('crontab', help='crontab file to parse') parser.add_argument('-w', '--whitelist', action='append', 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', @@ -57,4 +55,5 @@ def main(argv): return check.check_crontab(args, check.LogCounter(args.quiet)) if __name__ == '__main__': - sys.exit(main(sys.argv)) + main(sys.argv) + sys.exit(0) diff --git a/chkcrontab_lib.py b/chkcrontab_lib.py index 344dbd7..bfe56c8 100755 --- a/chkcrontab_lib.py +++ b/chkcrontab_lib.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # # Copyright 2011 Google Inc. All Rights Reserved. # @@ -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 ): @@ -819,14 +822,20 @@ def ParseLine(self, line, options ): Returns: A CronLine* class (must have a ValidateAndLog method). """ - chkcrontab_cmd = re.compile('##*\s*chkcrontab:\s*(.*)=(.*)') - assignment_line_re = re.compile('[a-zA-Z_][a-zA-Z0-9_]*\s*=(.*)') - at_line_re = re.compile('@(\S+)\s+(\S+)\s+(.*)') - cron_time_field_re = '[\*0-9a-zA-Z,/-]+' - time_field_job_line_re = re.compile( - '^\s*(%s)\s+(%s)\s+(%s)\s+(%s)\s+(%s)\s+(\S+)\s+(.*)' % - (cron_time_field_re, cron_time_field_re, cron_time_field_re, - cron_time_field_re, cron_time_field_re)) + chkcrontab_cmd = re.compile(r'##*\s*chkcrontab:\s*(.*)=(.*)') + assignment_line_re = re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*\s*=(.*)') + at_line_re = re.compile(r'@(\S+)\s+(\S+)\s+(.*)') + cron_time_field_re = r'[\*0-9a-zA-Z,/-]+' + if self.is_user_crontab: + time_field_job_line_re = re.compile( + r'^\s*(%s)\s+(%s)\s+(%s)\s+(%s)\s+(%s)\s+(.*)' % + (cron_time_field_re, cron_time_field_re, cron_time_field_re, + cron_time_field_re, cron_time_field_re)) + else: + time_field_job_line_re = re.compile( + r'^\s*(%s)\s+(%s)\s+(%s)\s+(%s)\s+(%s)\s+(\S+)\s+(.*)' % + (cron_time_field_re, cron_time_field_re, cron_time_field_re, + cron_time_field_re, cron_time_field_re)) if not line: return CronLineEmpty() @@ -857,7 +866,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], options) + else: + return CronLineTime(field, match.groups()[5], match.groups()[6], options) return CronLineUnknown() @@ -1100,7 +1112,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" diff --git a/tests/test_check.py b/tests/test_check.py index 642f7fc..930a48d 100755 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # # Copyright 2011 Google Inc. All Rights Reserved. #