Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Mehani <shtrom@ssji.net>2016-08-16 09:16:35 +0200
committerOlivier Mehani <shtrom@ssji.net>2016-08-16 09:16:35 +0200
commit69c796255e377f3b77b7c74f5ee94105eb0ca2fa (patch)
tree1610f795f3c791c080d214e4e9c99ed3893eebbc
parentb0dba30103f36b1eae5a83784e3dc61ce387dae2 (diff)
parent13f2781e1f1875a19e702d64df065a8f56cfc7b1 (diff)
Merge branch 'master' of scm.narf.ssji.net:/git/scripts
-rwxr-xr-xopenldap_passwd.py64
-rwxr-xr-xthetvdb_rename.py35
2 files changed, 88 insertions, 11 deletions
diff --git a/openldap_passwd.py b/openldap_passwd.py
new file mode 100755
index 0000000..43f8c87
--- /dev/null
+++ b/openldap_passwd.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python2
+"""
+http://www.openldap.org/faq/data/cache/347.html
+
+As seen working on Ubuntu 12.04 with OpenLDAP 2.4.28-1.1ubuntu4
+
+Author: Roberto Aguilar <roberto@baremetal.io>
+"""
+import hashlib
+import os
+import sys
+
+
+def check_password(tagged_digest_salt, password):
+ """
+ Checks the OpenLDAP tagged digest against the given password
+ """
+ # the entire payload is base64-encoded
+ assert tagged_digest_salt.startswith('{SSHA}')
+
+ # strip off the hash label
+ digest_salt_b64 = tagged_digest_salt[6:]
+
+ # the password+salt buffer is also base64-encoded. decode and split the
+ # digest and salt
+ digest_salt = digest_salt_b64.decode('base64')
+ digest = digest_salt[:20]
+ salt = digest_salt[20:]
+
+ sha = hashlib.sha1(password)
+ sha.update(salt)
+
+ return digest == sha.digest()
+
+
+def make_secret(password):
+ """
+ Encodes the given password as a base64 SSHA hash+salt buffer
+ """
+ salt = os.urandom(4)
+
+ # hash the password and append the salt
+ sha = hashlib.sha1(password)
+ sha.update(salt)
+
+ # create a base64 encoded string of the concatenated digest + salt
+ digest_salt_b64 = '{}{}'.format(sha.digest(), salt).encode('base64').strip()
+
+ # now tag the digest above with the {SSHA} tag
+ tagged_digest_salt = '{{SSHA}}{}'.format(digest_salt_b64)
+
+ return tagged_digest_salt
+
+
+if __name__ == '__main__':
+ if len(sys.argv) > 1:
+ print(make_secret(sys.argv[1]))
+ else:
+ # buffer straight out of OpenLDAP
+ ldap_buf = 'e1NTSEF9VGY1dVFxUkl0VzV2NGowV0RNNXczY2dJd2ZLS0FUcFg='
+ print 'ldap buffer result: {}'.format(check_password(ldap_buf, 'foobar'))
+
+ # check that make_secret() above can properly encode
+ print 'checking make_secret: {}'.format(check_password(make_secret('foobar'), 'foobar'))
diff --git a/thetvdb_rename.py b/thetvdb_rename.py
index e5d17a2..2ec60ba 100755
--- a/thetvdb_rename.py
+++ b/thetvdb_rename.py
@@ -4,21 +4,29 @@ import os, sys
import logging
import tvdb_api
import re
+# Need python-levenshtein for faster sequence matching
from fuzzywuzzy import fuzz
outdir="renamed"
series="doctor who"
ext="mkv"
-logging.basicConfig(level=logging.INFO)
+debuglevel=logging.INFO
+logging.basicConfig(level=debuglevel)
inparen=re.compile('.*\(([^\)]\+)\)')
if len(sys.argv) < 2:
- logging.error("usage %s SEASON" % sys.argv[0])
+ logging.error("usage %s SEASON [EXTENSION [SERIES [OUTDIR]]]" % sys.argv[0])
exit(1)
season = int(sys.argv[1])
+if len(sys.argv)>2:
+ ext = sys.argv[2]
+if len(sys.argv)>3:
+ series = sys.argv[3]
+if len(sys.argv)>4:
+ outdir = sys.argv[4]
t=tvdb_api.Tvdb()
s=t[series][season]
@@ -30,10 +38,11 @@ try:
except OSError:
pass
+count=0
for f in os.listdir("."):
e = None
title = None
- if f != outdir:
+ if f != outdir and f.split(".")[-1] == ext:
e = []
logging.debug("considering '%s'..." % f)
title = f.split("- ")[-1].rstrip(".%s" % ext)
@@ -41,13 +50,13 @@ for f in os.listdir("."):
try:
e.extend(s.search(title))
except tvdb_exceptions.tvdb_episodenotfound:
- logging.debug("%s not found" % title)
+ logging.debug("%s not matched" % title)
title_noep = title.lower().replace("episode ","")
logging.debug("considering '%s'..." % title_noep)
try:
e.extend(s.search(title_noep))
except tvdb_exceptions.tvdb_episodenotfound:
- logging.debug("%s not found" % title_noep)
+ logging.debug("%s not matched" % title_noep)
for ss in reversed(re.split("[-()]", title)):
if len(ss.strip()) > 0:
@@ -55,14 +64,14 @@ for f in os.listdir("."):
try:
e.extend(s.search(ss))
except tvdb_exceptions.tvdb_episodenotfound:
- logging.debug("%s not found" % ss)
+ logging.debug("%s not matched" % ss)
continue
logging.debug(e)
em = {fuzz.ratio(title, em['episodename']): em for em in e}
if len(em) < 1:
- logging.warning("not found: %s" % (f))
+ logging.warning("couldn't match %s" % (f))
else:
e = em[max(em.keys())]
@@ -75,13 +84,17 @@ for f in os.listdir("."):
title,
ext
)
- print("S%02dE%02d: %s" % (
-
+ logging.info("%s -> S%02dE%02d " % (
+ f,
season,
- int(e['episodenumber']),
- f
+ int(e['episodenumber'])
))
+
+ count += 1
try:
os.link(f, d)
except OSError, e:
logging.warning("%s: %s" % (d, e.strerror))
+
+if count != len(s):
+ logging.warning("processed %d files but expected %d" % (count, len(s)))