File: testpam.py

package info (click to toggle)
duo-unix 1.11.3-1.2
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 2,892 kB
  • sloc: sh: 12,108; ansic: 9,223; python: 1,639; makefile: 156
file content (71 lines) | stat: -rwxr-xr-x 1,856 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python

import getopt
import getpass
import os
import subprocess
import sys
import tempfile
import platform

import paths

# login_duo-compatible wrapper to pam_duo

def usage():
    print >>sys.stderr, 'Usage: %s [-d] [-c config] [-f user] [-h host]' % \
          sys.argv[0]
    sys.exit(1)
    
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'dc:f:h:')
    except getopt.GetoptError:
        usage()

    opt_conf = '/etc/duo/pam_duo.conf'
    opt_user = getpass.getuser()
    opt_host = None
    
    for o, a in opts:
        if o == '-c':
            opt_conf = a
        elif o == '-f':
            opt_user = a
        elif o == '-h':
            opt_host = a

    args = [ paths.build + '/testpam', opt_user ]
    if opt_host:
        args.append(opt_host)
    
    f = tempfile.NamedTemporaryFile()
    #f = open('/tmp/pam.conf', 'w')
    if sys.platform == 'sunos5':
        f.write('testpam ')
    f.write('auth  required  %s/pam_duo.so conf=%s debug' %
            (paths.topbuilddir + '/pam_duo/.libs', opt_conf))
    f.flush()
    
    env = os.environ.copy()
    env['PAM_CONF'] = f.name

    if sys.platform == 'darwin':
        env['DYLD_LIBRARY_PATH'] = paths.topbuilddir + '/lib/.libs'
        env['DYLD_INSERT_LIBRARIES'] = paths.build + \
                                       '/.libs/libtestpam_preload.dylib'
        env['DYLD_FORCE_FLAT_NAMESPACE'] = '1'
    elif sys.platform == 'sunos5':
        architecture = {'32bit': '32', '64bit': '64'}[platform.architecture()[0]]
        env['LD_PRELOAD_' + architecture] = paths.build + '/.libs/libtestpam_preload.so'
    else:
        env['LD_PRELOAD'] = paths.build + '/.libs/libtestpam_preload.so'
        
    p = subprocess.Popen(args, env=env)
    p.wait()
    f.close()
    
    sys.exit(p.returncode)

if __name__ == '__main__':
    main()