mirror of https://github.com/fail2ban/fail2ban
				
				
				
			BF+ENH: added %m-%d-%Y pattern + do not add %Y for Feb 29 fix if already present in the pattern
although %m-%d-%Y is ambioius with %d-%m-%Y it comes after so it should not be too dangerous (i.e. in upcoming days having smth like 02-01 should work as before matching first one first) and proper fix to select between the two should follow some time soon_tent/robust_datematching
							parent
							
								
									10729f96b9
								
							
						
					
					
						commit
						b257be4cd1
					
				| 
						 | 
				
			
			@ -117,6 +117,12 @@ class DateDetector:
 | 
			
		|||
			template.setRegex("\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}")
 | 
			
		||||
			template.setPattern("%d-%m-%Y %H:%M:%S")
 | 
			
		||||
			self.__templates.append(template)
 | 
			
		||||
			# 01-27-2012 16:22:44.252
 | 
			
		||||
			template = DateStrptime()
 | 
			
		||||
			template.setName("Month-Day-Year Hour:Minute:Second[.Millisecond]")
 | 
			
		||||
			template.setRegex("\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}")
 | 
			
		||||
			template.setPattern("%m-%d-%Y %H:%M:%S")
 | 
			
		||||
			self.__templates.append(template)
 | 
			
		||||
			# TAI64N
 | 
			
		||||
			template = DateTai64n()
 | 
			
		||||
			template.setName("TAI64N")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -140,12 +140,22 @@ class DateStrptime(DateTemplate):
 | 
			
		|||
				conv = self.convertLocale(dateMatch.group())
 | 
			
		||||
				try:
 | 
			
		||||
					date = list(time.strptime(conv, self.getPattern()))
 | 
			
		||||
				except ValueError, e:
 | 
			
		||||
				except (ValueError, re.error), e:
 | 
			
		||||
					# Try to add the current year to the pattern. Should fix
 | 
			
		||||
					# the "Feb 29" issue.
 | 
			
		||||
					conv += " %s" % MyTime.gmtime()[0]
 | 
			
		||||
					pattern = "%s %%Y" % self.getPattern()
 | 
			
		||||
					date = list(time.strptime(conv, pattern))
 | 
			
		||||
					opattern = self.getPattern()
 | 
			
		||||
					# makes sense only if %Y is not in already:
 | 
			
		||||
					if not '%Y' in opattern:
 | 
			
		||||
						pattern = "%s %%Y" % opattern
 | 
			
		||||
						conv += " %s" % MyTime.gmtime()[0]
 | 
			
		||||
						date = list(time.strptime(conv, pattern))
 | 
			
		||||
					else:
 | 
			
		||||
						# we are helpless here
 | 
			
		||||
						raise ValueError(
 | 
			
		||||
							"Given pattern %r does not match. Original "
 | 
			
		||||
							"exception was %r and Feb 29 workaround could not "
 | 
			
		||||
							"be tested due to already present year mark in the "
 | 
			
		||||
							"pattern" % (opattern, e))
 | 
			
		||||
			if date[0] < 2000:
 | 
			
		||||
				# There is probably no year field in the logs
 | 
			
		||||
				date[0] = MyTime.gmtime()[0]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -67,6 +67,7 @@ class DateDetectorTest(unittest.TestCase):
 | 
			
		|||
			"Jan 23 21:59:59",
 | 
			
		||||
			"2005.01.23 21:59:59",
 | 
			
		||||
			"23/01/2005 21:59:59",
 | 
			
		||||
			"01-23-2005 21:59:59.252", # reported on f2b, causes Feb29 fix to break
 | 
			
		||||
			):
 | 
			
		||||
			log = sdate + "[sshd] error: PAM: Authentication failure"
 | 
			
		||||
			# exclude
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue