1/* 2 * Copyright (c) 2007 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24#include <KerberosHelper/KerberosHelper.h> 25#include <CoreFoundation/CoreFoundation.h> 26#include <unistd.h> 27#include <string.h> 28#include "utils.h" 29 30int main (int argc, const char * argv[]) { 31 OSStatus err = 0; 32 void *krbHelper = NULL; 33 CFStringRef localRealm = NULL, outPrincipal = NULL, outUsername = NULL, inUsername = NULL, hostName = NULL, hostNameDotLocal = NULL; 34 char *output, *ptr; 35 int testNumber = -1, lineNumber = 0, cases; 36 CFDictionaryRef outDict = NULL; 37 CFMutableDictionaryRef inDict = NULL; 38 char myHostname[_POSIX_HOST_NAME_MAX]; // XXX HOST_NAME_MAX 39 char *myHostnameLocal; 40 41 if (0 != gethostname (myHostname, sizeof(myHostname))) { lineNumber = __LINE__; goto Error; } 42 43 ptr = strchr (myHostname, '.'); 44 if (NULL != ptr) { *ptr = '\0'; } 45 46 asprintf (&myHostnameLocal, "%s.local", myHostname); 47 48 hostNameDotLocal = CFStringCreateWithCString (NULL, myHostnameLocal, kCFStringEncodingASCII); 49 hostName = CFStringCreateWithCString (NULL, myHostname, kCFStringEncodingASCII); 50 51 testNumber = 0; 52 /*******************************************************************************************/ 53 54 /* First test the "local" behaviour */ 55 err = KRBCreateSession (NULL, NULL, &krbHelper); 56 if (noErr != err) { lineNumber = __LINE__; goto Error; } 57 58 err = KRBCopyRealm (krbHelper, &localRealm); 59 if (noErr != err) { lineNumber = __LINE__; goto Error; } 60 61 __KRBCreateUTF8StringFromCFString (localRealm, &output); 62 63 printf ("[%d] Local Realm = %s\n\n", testNumber, output); 64 __KRBReleaseUTF8String (output); 65 66 KRBCloseSession (krbHelper); 67 testNumber++; 68 69 /*******************************************************************************************/ 70 71 /* First test the "local" behaviour */ 72 for (cases = 0; cases < 2; cases++) { 73 74 err = KRBCreateSession (NULL, NULL, &krbHelper); 75 if (noErr != err) { lineNumber = __LINE__; goto Error; } 76 77 err = KRBCopyKeychainLookupInfo (krbHelper, inUsername, &outDict); 78 if (noErr != err) { lineNumber = __LINE__; goto Error; } 79 80 printf ("[%d] CopyKeychainLookupInfo dictionary...\n", testNumber); 81 CFShow (outDict); 82 printf ("[%d] \n\n", testNumber); 83 84 KRBCloseSession (krbHelper); 85 testNumber++; 86 87 inUsername = CFStringCreateWithCString (NULL, getlogin(), kCFStringEncodingASCII); 88 } 89 90 /*******************************************************************************************/ 91 err = KRBCreateSession (hostNameDotLocal, NULL, &krbHelper); 92 if (noErr != err) { lineNumber = __LINE__; goto Error; } 93 94 err = KRBCopyServicePrincipal (krbHelper, CFSTR("afpserver"), &outPrincipal); 95 if (noErr != err) { lineNumber = __LINE__; goto Error; } 96 97 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 98 printf ("[%d] ServicePrincipal = %s\n\n", testNumber, output); 99 __KRBReleaseUTF8String (output); 100 101 KRBCloseSession (krbHelper); 102 testNumber++; 103 104 /*******************************************************************************************/ 105 err = KRBCreateSession (CFSTR("17.202.44.91"), CFSTR("afpserver/homedepot.apple.com@OD.APPLE.COM"), &krbHelper); 106 if (noErr != err) { lineNumber = __LINE__; goto Error; } 107 108 err = KRBCopyServicePrincipal (krbHelper, CFSTR("afpserver"), &outPrincipal); 109 if (noErr != err) { lineNumber = __LINE__; goto Error; } 110 111 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 112 printf ("[%d] ServicePrincipal = %s\n\n", testNumber, output); 113 __KRBReleaseUTF8String (output); 114 115 KRBCloseSession (krbHelper); 116 testNumber++; 117 118 /*******************************************************************************************/ 119 err = KRBCreateSession (hostNameDotLocal, NULL, &krbHelper); 120 if (noErr != err) { lineNumber = __LINE__; goto Error; } 121 122 err = KRBCopyServicePrincipal (krbHelper, CFSTR("afpserver"), &outPrincipal); 123 if (noErr != err) { lineNumber = __LINE__; goto Error; } 124 125 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 126 printf ("[%d] ServicePrincipal = %s\n\n", testNumber, output); 127 __KRBReleaseUTF8String (output); 128 129 err = KRBCopyClientPrincipalInfo (krbHelper, inDict, &outDict); 130 if (noErr != err) { lineNumber = __LINE__; goto Error; } 131 132 outPrincipal = CFDictionaryGetValue (outDict, kKRBClientPrincipalKey); 133 134 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 135 printf ("[%d] ClientPrincipal = %s\n\n", testNumber, output); 136 __KRBReleaseUTF8String (output); 137 138 KRBCloseSession (krbHelper); 139 testNumber++; 140 141 /*******************************************************************************************/ 142#if 0 143 err = KRBCreateSession (CFSTR("statler"), NULL, &krbHelper); 144 if (noErr != err) { lineNumber = __LINE__; goto Error; } 145 146 err = KRBCopyServicePrincipal (krbHelper, CFSTR("cifs"), &outPrincipal); 147 if (noErr != err) { lineNumber = __LINE__; goto Error; } 148 149 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 150 printf ("[%d] ServicePrincipal = %s\n\n", testNumber, output); 151 __KRBReleaseUTF8String (output); 152 153 err = KRBCopyClientPrincipalInfo (krbHelper, inDict, &outDict); 154 if (noErr != err) { lineNumber = __LINE__; goto Error; } 155 156 outPrincipal = CFDictionaryGetValue (outDict, kKRBClientPrincipalKey); 157 158 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 159 printf ("[%d] ClientPrincipal = %s\n\n", testNumber, output); 160 __KRBReleaseUTF8String (output); 161 KRBCloseSession (krbHelper); 162 testNumber++; 163#endif 164 /*******************************************************************************************/ 165 err = KRBCreateSession (hostNameDotLocal, CFSTR("afpserver/kerberos@OD.APPLE.COM"), &krbHelper); 166 if (noErr != err) { lineNumber = __LINE__; goto Error; } 167 168 err = KRBCopyServicePrincipal (krbHelper, NULL, &outPrincipal); 169 if (noErr != err) { lineNumber = __LINE__; goto Error; } 170 171 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 172 printf ("[%d] ServicePrincipal = %s\n\n", testNumber, output); 173 __KRBReleaseUTF8String (output); 174 175 KRBCloseSession (krbHelper); 176 testNumber++; 177 178 /*******************************************************************************************/ 179 err = KRBCreateSession (hostName, CFSTR("afpserver/kerberos@OD.APPLE.COM"), &krbHelper); 180 if (noErr != err) { lineNumber = __LINE__; goto Error; } 181 182 err = KRBCopyServicePrincipal (krbHelper, NULL, &outPrincipal); 183 if (noErr != err) { lineNumber = __LINE__; goto Error; } 184 185 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 186 printf ("[%d] ServicePrincipal = %s\n\n", testNumber, output); 187 __KRBReleaseUTF8String (output); 188 189 KRBCloseSession (krbHelper); 190 testNumber++; 191 192 /*******************************************************************************************/ 193 err = KRBCreateSession (hostNameDotLocal, NULL, &krbHelper); 194 if (noErr != err) { lineNumber = __LINE__; goto Error; } 195 196 err = KRBCopyServicePrincipal (krbHelper, CFSTR("afpserver"), &outPrincipal); 197 if (noErr != err) { lineNumber = __LINE__; goto Error; } 198 199 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 200 printf ("[%d] ServicePrincipal = %s\n", testNumber, output); 201 __KRBReleaseUTF8String (output); 202 203 err = KRBCopyClientPrincipalInfo (krbHelper, inDict, &outDict); 204 if (noErr != err) { lineNumber = __LINE__; goto Error; } 205 206 outPrincipal = CFDictionaryGetValue (outDict, kKRBClientPrincipalKey); 207 208 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 209 printf ("[%d] ClientPrincipal = %s\n\n", testNumber, output); 210 __KRBReleaseUTF8String (output); 211 212 KRBCloseSession (krbHelper); 213 testNumber++; 214 215 /*******************************************************************************************/ 216 err = KRBCreateSession (hostNameDotLocal, NULL, &krbHelper); 217 if (noErr != err) { lineNumber = __LINE__; goto Error; } 218 219 err = KRBCopyServicePrincipal (krbHelper, CFSTR("afpserver"), &outPrincipal); 220 if (noErr != err) { lineNumber = __LINE__; goto Error; } 221 222 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 223 printf ("[%d] ServicePrincipal = %s\n", testNumber, output); 224 __KRBReleaseUTF8String (output); 225 226 inDict = CFDictionaryCreateMutable (kCFAllocatorDefault, 4, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 227 228 CFDictionarySetValue (inDict, kKRBUsernameKey, CFSTR("remoteUsername")); 229 CFDictionarySetValue (inDict, kKRBClientPasswordKey, CFSTR("remotePassword")); 230 CFDictionarySetValue (inDict, kKRBAllowKerberosUI, kKRBOptionNoUI); 231 232 err = KRBCopyClientPrincipalInfo (krbHelper, inDict, &outDict); 233 if (noErr != err) { lineNumber = __LINE__; goto Error; } 234 235 outPrincipal = CFDictionaryGetValue (outDict, kKRBClientPrincipalKey); 236 237 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 238 printf ("[%d] ClientPrincipal = %s\n\n", testNumber, output); 239 __KRBReleaseUTF8String (output); 240 241 KRBCloseSession (krbHelper); 242 testNumber++; 243 244 /*******************************************************************************************/ 245 for (cases = 0; cases < 2; cases++) { 246 err = KRBCreateSession (hostNameDotLocal, NULL, &krbHelper); 247 if (noErr != err) { lineNumber = __LINE__; goto Error; } 248 249 err = KRBCopyServicePrincipal (krbHelper, CFSTR("afpserver"), &outPrincipal); 250 if (noErr != err) { lineNumber = __LINE__; goto Error; } 251 252 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 253 printf ("[%d] ServicePrincipal = %s\n", testNumber, output); 254 __KRBReleaseUTF8String (output); 255 256 inDict = CFDictionaryCreateMutable (kCFAllocatorDefault, 4, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 257 258 CFDictionarySetValue (inDict, kKRBAllowKerberosUI, kKRBOptionNoUI); 259 260 err = KRBCopyClientPrincipalInfo (krbHelper, inDict, &outDict); 261 CFRelease(inDict); 262 if (noErr != err) { lineNumber = __LINE__; goto Error; } 263 264 outPrincipal = CFDictionaryGetValue (outDict, kKRBClientPrincipalKey); 265 outUsername = CFDictionaryGetValue (outDict, kKRBUsernameKey); 266 267 CFDictionarySetValue ((CFMutableDictionaryRef)outDict, kKRBClientPasswordKey, outUsername); 268 269 __KRBCreateUTF8StringFromCFString (outPrincipal, &output); 270 printf ("[%d] ClientPrincipal = %s\n\n", testNumber, output); 271 __KRBReleaseUTF8String (output); 272 273 err = KRBTestForExistingTicket (krbHelper, outDict); 274 if (noErr == err) { 275 printf("[%d] Ticket was already available\n", testNumber); 276 } else { 277 printf("[%d] Ticket was not available (err=%d), trying to obtain one\n", testNumber, (int)err); 278 err = KRBAcquireTicket(krbHelper, outDict); 279 if (noErr != err) { lineNumber = __LINE__; goto Error; } 280 } 281 282 KRBCloseSession (krbHelper); 283 testNumber++; 284 } 285 286Error: 287 { 288 pid_t pid = getpid(); 289 char *command; 290 291 asprintf (&command, "leaks %d", pid); 292 system (command); 293 } 294 if (err) { 295 fprintf (stdout, "Error (%d) in test %d at line %d\n", (int)err, testNumber, lineNumber); 296 return 1; 297 } 298 return 0; 299} 300