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