from flask import Flask
from subprocess import Popen, PIPE
from base64 import urlsafe_b64encode
import sys
import getpass
# import gssapi
import os
import subprocess


# Get a ticket for a specific service. Doesn't work yet.
def get_service_ticket(userid, service, tgt, realm='ATHENA.MIT.EDU'):
    tmp_dir = os.path.join('/tmp', urlsafe_b64encode(userid + '@' + realm))
    if not os.path.exists(tmp_dir):
        os.makedirs(tmp_dir)

    tgt_cache = os.path.join(tmp_dir, 'krb5cc_tgt')
    ticket_file = os.path.join(tmp_dir, 'krb5cc_svc')
    keytab_file = os.path.join('/etc', urlsafe_b64encode(userid + '@' + realm))

    # write the tgt to a temporary cache file
    open(tgt_cache, 'w+').write(tgt)

    # execute kinit, tell it to use cached tgt
    KINIT_PATH = '/usr/bin/kinit'
    kinit_args = [KINIT_PATH, '-f', '-c', ticket_file, '-I', tgt_cache,
                  '-S', service, userid + '@' + realm]
    print ' '.join(kinit_args)
    kinit = subprocess.Popen(kinit_args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    print kinit.wait()

    # delete the cached tgt
    os.remove(tgt_cache)

    # read the new ticket and then delete it
    ticket = open(ticket_file).read()
    os.remove(ticket_file)

    return ticket


# Get a ticket-granting ticket. This part works.
def get_tgt(userid, passwd, realm='ATHENA.MIT.EDU'):
    tmp_dir = os.path.join('/tmp', urlsafe_b64encode(userid + '@' + realm))
    if not os.path.exists(tmp_dir):
        os.makedir(tmp_dir)

    tgt_file = os.path.join(tmp_dir, 'krb5cc')
    keytab_file = os.path.join('/etc', urlsafe_b64encode(userid + '@' + realm))

    KINIT_PATH = '/usr/bin/kinit'
    kinit_args = [KINIT_PATH, '-f', '-c', tgt_file, userid + '@' + realm]
    print ' '.join(kinit_args)

    # exec kinit
    kinit = subprocess.Popen(kinit_args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    print kinit.communicate(passwd + '\n')    # write password
    print kinit.wait()                        # wait for completion

    # get the generated tgt
    tgt = open(tgt_file, 'r').read()
    # delete cached tgt
    os.remove(tgt_file)

    return tgt


if __name__ == '__main__':
    if len(sys.argv) != 3:
        raise "Error: format is <username> <service>"
    uname, service = sys.argv[1:]
    passwd = getpass.getpass()
    tgt = get_tgt(uname, passwd)
    print tgt
    svc_ticket = get_service_ticket(uname, service, tgt)
    print 'success!'
    print
    print svc_ticket


@@ -412,19 +412,17 @@
     os.m
-ake
+k
 dirs(tmp
@@ -1456,11 +1456,9 @@
 os.m
-ake
+k
 dir(
@@ -1894,29 +1894,33 @@
 assword%0A    
-print
+retcode =
  kinit.wait(
@@ -1967,16 +1967,41 @@
 letion%0A%0A
+    if retcode == 0:%0A    
     # ge
@@ -2016,24 +2016,28 @@
 nerated tgt%0A
+    
     tgt = op
@@ -2061,24 +2061,28 @@
 .read()%0A    
+ 
+   
 # delete cac
@@ -2085,32 +2085,36 @@
  cached tgt%0A    
+ 
+   
 os.remove(tgt_fi
@@ -2112,24 +2112,104 @@
 ve(tgt_file)
+%0A    else:%0A        raise Exception(%22Error: kinit returned code %22 + str(retcode))
 %0A%0A    return
@@ -2431,18 +2431,96 @@
   print 
-tg
+'TGT generation success. Ticket is as follows:'%0A    print%0A    print tgt%0A    prin
 t%0A    sv
@@ -2574,29 +2574,8 @@
 gt)%0A
-    print 'success!'%0A
     

Raise exception on kinit fail