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 <getopt.h>
29#include "utils.h"
30
31int main (int argc, char **argv) {
32	OSStatus err = 0;
33	void *krbHelper = NULL;
34
35	CFStringRef inHostName = NULL, inAdvertisedPrincipal = NULL;
36	CFStringRef outRealm = NULL, outServer = NULL, outNoCanon = NULL;
37	CFDictionaryRef outDict = NULL;
38	const char *inHostNameString = NULL, *inAdvertisedPrincipalString = NULL;
39
40	if (argc > 0) {
41		inHostNameString = argv[0];
42		inHostName = CFStringCreateWithCString (NULL, inHostNameString, kCFStringEncodingASCII);
43	}
44	if (argc > 1) {
45		inAdvertisedPrincipalString = argv[1];
46		inAdvertisedPrincipal = CFStringCreateWithCString (NULL, inAdvertisedPrincipalString, kCFStringEncodingASCII);
47	}
48
49	err = KRBCreateSession (inHostName, inAdvertisedPrincipal, &krbHelper);
50	if (noErr != err) {
51		printf ("ERROR=KRBCreateSession %d (\"%s\",\"%s\" ... )\n", (int)err, inHostNameString, inAdvertisedPrincipalString);
52		return 1;
53	}
54
55	err = KRBCopyRealm (krbHelper, &outRealm);
56	if (noErr != err) {
57		printf ("ERROR=%d from KRBCopyRealm ()\n", (int)err);
58		return 2;
59	}
60	if (outRealm == NULL) {
61		printf("ERROR=No realm from KRBCopyRealm\n");
62                return 3;
63        }
64
65	err = KRBCopyServicePrincipalInfo (krbHelper, CFSTR("host"), &outDict);
66	if (noErr != err) {
67		printf ("ERROR=%d from KRBCopyServicePrincipal ()\n", (int)err);
68		return 2;
69	}
70	outServer = CFDictionaryGetValue(outDict, kKRBServicePrincipal);
71	if (outServer == NULL) {
72		printf("ERROR=No realm from KRBCopyServicePrincipal\n");
73		return 3;
74	}
75
76	outNoCanon = CFDictionaryGetValue(outDict, kKRBNoCanon);
77
78	char *outRealmString = NULL, *outServerString = NULL;
79	__KRBCreateUTF8StringFromCFString(outRealm, &outRealmString);
80	__KRBCreateUTF8StringFromCFString(outServer, &outServerString);
81
82	printf ("REALM=%s\n", outRealmString);
83	printf ("SERVER=%s\n", outServerString);
84	printf ("DNS-CANON=%s\n", outNoCanon ? "NO" : "YES");
85
86	__KRBReleaseUTF8String(outRealmString);
87	__KRBReleaseUTF8String(outServerString);
88
89	CFRelease(outRealm);
90	CFRelease(outDict);
91	CFRelease(inHostName);
92
93	KRBCloseSession(krbHelper);
94
95	sleep(100);
96
97	return err;
98}
99