c45f77193377a2770dfea4024d86f950057cf1e8
[sfa.git] / sfa / clientbin / sfadump.py
1 #! /usr/bin/env python
2 from __future__ import with_statement
3
4 import sys
5 import os, os.path
6 import tempfile
7 import xmlrpclib
8 from types import StringTypes, ListType
9 from optparse import OptionParser
10
11 from sfa.util.sfalogging import logger
12
13 from sfa.trust.certificate import Certificate
14 from sfa.trust.credential import Credential
15 from sfa.trust.gid import GID
16
17 from sfa.storage.record import SfaRecord
18
19 def determine_sfa_filekind(fn):
20
21     if fn.endswith('.gid'): return 'gid'
22     elif fn.endswith('.cert'): return 'certificate'
23     elif fn.endswith('cred'): return 'credential'
24
25     try:
26         cred=Credential(filename=fn)
27         return 'credential'
28     except: pass
29
30     try: 
31         gid=GID(filename=fn)
32         if gid.uuid: return 'gid'
33     except: pass
34
35     try:
36         cert = Certificate(filename = fn)
37         return 'certificate'
38     except: pass
39
40     # to be completed
41 #    if "gidCaller" in dict:
42 #        return "credential"
43 #
44 #    if "uuid" in dict:
45 #        return "gid"
46
47     return "unknown"
48
49 def save_gid(gid):
50    hrn = gid.get_hrn()
51    lastpart = hrn.split(".")[-1]
52    filename = lastpart + ".gid"
53
54    if os.path.exists(filename):
55        print filename, ": already exists... skipping"
56        return
57
58    print filename, ": extracting gid of", hrn
59
60    gid.save_to_file(filename, save_parents = True)
61
62 def extract_gids(cred, extract_parents):
63    gidCaller = cred.get_gid_caller()
64    if gidCaller:
65        save_gid(gidCaller)
66
67    gidObject = cred.get_gid_object()
68    if gidObject and ((gidCaller == None) or (gidCaller.get_hrn() != gidObject.get_hrn())):
69        save_gid(gidObject)
70
71    # no such method Credential.get_parent
72 #   if extract_parents:
73 #       parent = cred.get_parent()
74 #       if parent:
75 #           extract_gids(parent, extract_parents)
76
77 def handle_input (filename, options):
78     kind = determine_sfa_filekind(filename)
79     handle_input_kind (filename,options,kind)
80
81 def handle_input_kind (filename, options, kind):
82     
83
84 # dump methods current do 'print' so let's go this road for now
85     if kind=="certificate":
86         cert=Certificate (filename=filename)
87         print '--------------------',filename,'IS A',kind
88         cert.dump(show_extensions=options.show_extensions)
89     elif kind=="credential":
90         cred = Credential(filename = filename)
91         print '--------------------',filename,'IS A',kind
92         cred.dump(dump_parents = options.dump_parents)
93         if options.extract_gids:
94             print '--------------------',filename,'embedded GIDS'
95             extract_gids(cred, extract_parents = options.dump_parents)
96     elif kind=="gid":
97         gid = GID(filename = filename)
98         print '--------------------',filename,'IS A',kind
99         gid.dump(dump_parents = options.dump_parents)
100     else:
101         print "%s: unknown filekind '%s'"% (filename,kind)
102
103 def main():
104     usage = """%prog file1 [ .. filen]
105 display info on input files"""
106     parser = OptionParser(usage=usage)
107
108     parser.add_option("-g", "--extract-gids", action="store_true", dest="extract_gids", default=False, help="Extract GIDs from credentials")
109     parser.add_option("-p", "--dump-parents", action="store_true", dest="dump_parents", default=False, help="Show parents")
110     parser.add_option("-e", "--extensions", action="store_true", dest="show_extensions", default="False", help="Show certificate extensions")
111     parser.add_option("-v", "--verbose", action='count', dest='verbose', default=0)
112     (options, args) = parser.parse_args()
113
114     logger.setLevelFromOptVerbose(options.verbose)
115     if len(args) <= 0:
116         parser.print_help()
117         sys.exit(1)
118     for f in args: 
119         handle_input(f,options)
120
121 if __name__=="__main__":
122    main()