Rx: use osi_Assert/osi_Panic instead of assert
[openafs.git] / src / platform / DARWIN / AFSPreference / Krb5Util.m
1 //
2 //  Krb5Util.m
3 //  OpenAFS
4 //
5 //  Created by Claudio Bisegni on 20/03/10.
6 //  Copyright 2010 INFN. All rights reserved.
7 //
8
9 #import "Krb5Util.h"
10
11 @implementation Krb5Util
12 +(KLStatus) getNewTicketIfNotPresent {
13         KLPrincipal             princ = nil;
14         KLStatus                kstatus = noErr;
15         char                    *princName = 0L;
16         KLBoolean       outFoundValidTickets = false;
17         @try{
18                 kstatus = KLCacheHasValidTickets(nil, kerberosVersion_All, &outFoundValidTickets, nil, nil);
19                 if(!outFoundValidTickets) {
20                         kstatus = KLAcquireNewInitialTickets(nil, nil, &princ, &princName);
21                         if(kstatus != noErr && kstatus != klUserCanceledErr) @throw [NSException exceptionWithName:@"Krb5Util"
22                                                                                                                                                                                                 reason:@"getNewTicketIfNotPresent"
23                                                                                                                                                                                           userInfo:nil];
24                 }
25         }
26         @catch (NSException * e) {
27                 @throw e;
28         }
29         @finally {
30                 KLDisposeString (princName);
31                 KLDisposePrincipal (princ);
32         }
33         return kstatus;
34 }
35
36 +(KLStatus) renewTicket:(NSTimeInterval)secToExpire
37                           renewTime:(NSTimeInterval)renewTime {
38         KLPrincipal             princ = nil;
39         KLStatus                kstatus = noErr;
40         char                    *princName = 0L;
41         KLTime          expireStartTime;
42         KLLoginOptions  inLoginOptions;
43         KLLifetime      inTicketLifetime = renewTime;
44         NSDate                  *expirationDate = nil;
45         @try {
46                 //prepare the login option
47                 kstatus = KLCreateLoginOptions(&inLoginOptions);
48                 //set the lifetime of ticket
49                 kstatus = KLLoginOptionsSetTicketLifetime (inLoginOptions,  inTicketLifetime);
50                 kstatus = KLLoginOptionsSetRenewableLifetime (inLoginOptions, 0L);
51                 kstatus = KLLoginOptionsSetTicketStartTime (inLoginOptions, 0);
52                 //set the preference renewable time
53                 //kstatus =  KLLoginOptionsSetRenewableLifetime (inLoginOptions, inTicketLifetime);
54                 //check the start time
55                 kstatus = KLTicketExpirationTime (nil, kerberosVersion_All, &expireStartTime);
56                 expirationDate = [NSDate dateWithTimeIntervalSince1970:expireStartTime];
57
58                 //NSLog(@"Ticket Expiration time: %@", [expirationDate description]);
59                 NSTimeInterval secondToExpireTime = [expirationDate timeIntervalSinceNow];
60                 if(secondToExpireTime <= secToExpire) {
61                         kstatus = KLRenewInitialTickets ( nil, inLoginOptions, nil, nil);
62                         //kstatus = KLTicketExpirationTime (nil, kerberosVersion_All, &expireStartTime);
63                         //expirationDate = [NSDate dateWithTimeIntervalSince1970:expireStartTime];
64                         //NSLog(@"Ticket Renewed Unitl %@", expirationDate);
65                 }
66         }
67         @catch (NSException * e) {
68                 @throw e;
69         }
70         @finally {
71                 KLDisposeString (princName);
72                 KLDisposePrincipal (princ);
73                 KLDisposeLoginOptions(inLoginOptions);
74         }
75         return kstatus;
76 }
77 @end