[27761] in Source-Commits

home help back first fref pref prev next nref lref last post

athinfod commit: Added Python athinfod

daemon@ATHENA.MIT.EDU (Jonathan D Reed)
Tue Mar 4 12:07:17 2014

Date: Tue, 4 Mar 2014 12:07:09 -0500
From: Jonathan D Reed <jdreed@MIT.EDU>
Message-Id: <201403041707.s24H79EJ023808@drugstore.mit.edu>
To: source-commits@MIT.EDU

https://github.com/mit-athena/athinfod/commit/d9777bedccb5f0f4802e2aa6d32bae69f054673a
commit d9777bedccb5f0f4802e2aa6d32bae69f054673a
Author: Kristen A Sunter <k_sunter@mit.edu>
Date:   Sat Sep 7 20:40:08 2013 -0400

    Added Python athinfod
    
    Added athinfod.py to replace athinfod.c and related infrastructure
    Added support for loading *.defs files from
    /etc/athena/athinfo.defs.d.

 athinfod.py |   77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/athinfod.py b/athinfod.py
new file mode 100644
index 0000000..ff2941e
--- /dev/null
+++ b/athinfod.py
@@ -0,0 +1,77 @@
+#!/usr/bin/python
+import fnmatch
+import os
+import socket
+import string
+import sys
+
+BASE_DEFINITIONS = '/etc/athena/athinfo.defs'
+EXTENDED_DEFINITIONS = '/etc/athena/athinfo.defs.d/'
+
+def read_query():
+    line = sys.stdin.readline().strip()
+    if any(x not in string.printable for x in line):
+        raise Exception('invalid query')
+    return line
+
+def shutdown_input():
+    std_in = sys.stdin.fileno()
+    sys.stdin.close()
+    fd = os.open("/dev/null", os.O_RDONLY)
+
+    if fd != std_in:
+        os.dup2(fd, std_in)
+        os.close(fd)
+
+def get_definitions_from_file(queries, filepath):
+    with open(filepath, "r") as defs:
+        for line in defs:
+            line = line.strip()
+            if not line or line.startswith('#'):
+                continue
+            (query, shell_command) = string.split(line, maxsplit=1)
+            queries[query] = [shell_command, False]
+    return queries
+
+def get_query_access(filepath, queries):
+    enablement = {'enable': True, 'disable': False}
+    with open(filepath, "r") as access:
+        for line in access:
+            (action, query) = string.split(line.strip(), maxsplit=1)
+            if action not in enablement:
+                raise Exception('Invalid syntax in athinfo.access')
+            if query not in queries and query != '*':
+                raise Exception(
+                    'athinfo.access references query "%s" that is not defined' %
+                    (query,))
+            for (key, value) in queries.iteritems():
+                if key == query or query == '*':
+                    queries[key][1] = enablement[action]
+
+def get_definition(query):
+    queries = {}
+    get_definitions_from_file(queries, BASE_DEFINITIONS)
+    for candidate in os.listdir(EXTENDED_DEFINITIONS):
+        if fnmatch.fnmatch(candidate, '*.defs'):
+            get_definitions_from_file(queries, os.path.join(
+                    EXTENDED_DEFINITIONS, candidate))
+    get_query_access("/etc/athena/athinfo.access", queries)
+    if query not in queries:
+        raise Exception('unknown query "%s"' % (query,))
+    cmd, enabled = queries[query]
+    if not enabled:
+        raise Exception('query "%s" is disabled' % (query,))
+    return cmd
+
+def main():
+    query = read_query()
+    shutdown_input()
+    cmd = get_definition(query)
+    os.execv("/bin/sh", ['sh', '-c', cmd])
+
+if __name__ == '__main__':
+    try:
+        main()
+    except Exception as e:
+        print >>sys.stderr, "athinfod: %s" % (e,)
+        sys.exit(1)

home help back first fref pref prev next nref lref last post