1 /*
   2  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import <Cocoa/Cocoa.h>
  27 #import <SystemConfiguration/SystemConfiguration.h>
  28 #import <jni.h>
  29 
  30 #define KERBEROS_DEFAULT_REALMS @"Kerberos-Default-Realms"
  31 #define KERBEROS_DEFAULT_REALM_MAPPINGS @"Kerberos-Domain-Realm-Mappings"
  32 #define KERBEROS_REALM_INFO @"Kerberos:%@"
  33 
  34 int removeAll(SCDynamicStoreRef store) {
  35     fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) KERBEROS_DEFAULT_REALMS));
  36     fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"A.COM"]));
  37     fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"B.COM"]));
  38     fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) KERBEROS_DEFAULT_REALM_MAPPINGS));
  39     return 1;
  40 }
  41 
  42 int removeRealm(SCDynamicStoreRef store) {
  43     fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"A.COM"]));
  44     return 1;
  45 }
  46 
  47 int removeMapping(SCDynamicStoreRef store) {
  48     fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) KERBEROS_DEFAULT_REALM_MAPPINGS));
  49     return 1;
  50 }
  51 
  52 int addMapping(SCDynamicStoreRef store) {
  53     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  54         @"a", @"A",
  55         @"b", @"B",
  56         @"c", @"C",
  57         @"d", @"D",
  58         nil];
  59     fprintf(stderr, "%d\n", SCDynamicStoreSetValue(store, (CFStringRef) KERBEROS_DEFAULT_REALM_MAPPINGS, [NSArray arrayWithObjects: dict, nil]));
  60     return 1;
  61 }
  62 
  63 int addAll(SCDynamicStoreRef store) {
  64     NSArray *keys = [NSArray arrayWithObjects:@"A.COM", @"B.COM", nil];
  65     fprintf(stderr, "%d\n", SCDynamicStoreSetValue(store, (CFStringRef) KERBEROS_DEFAULT_REALMS, keys));
  66 
  67     NSDictionary *k1 = [NSDictionary dictionaryWithObjectsAndKeys:
  68         @"kdc1.a.com", @"host", nil];
  69     NSDictionary *k2 = [NSDictionary dictionaryWithObjectsAndKeys:
  70         @"kdc2.a.com", @"host", nil];
  71     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  72         [NSArray arrayWithObjects: k1, k2, nil], @"kdc",
  73         nil];
  74     fprintf(stderr, "%d\n", SCDynamicStoreSetValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"A.COM"], dict));
  75 
  76     k1 = [NSDictionary dictionaryWithObjectsAndKeys:
  77         @"kdc1.b.com", @"host", nil];
  78     k2 = [NSDictionary dictionaryWithObjectsAndKeys:
  79         @"kdc2.b.com", @"host", nil];
  80     dict = [NSDictionary dictionaryWithObjectsAndKeys:
  81         [NSArray arrayWithObjects: k1, k2, nil], @"kdc",
  82         nil];
  83     fprintf(stderr, "%d\n", SCDynamicStoreSetValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"B.COM"], dict));
  84     addMapping(store);
  85     return 1;
  86 }
  87 
  88 JNIEXPORT jint JNICALL Java_TestDynamicStore_actionInternal(JNIEnv *env, jclass clazz, jchar what, jchar whom) {
  89     SCDynamicStoreRef store = SCDynamicStoreCreate(NULL, CFSTR("java-kerberos"), NULL, NULL);
  90     fprintf(stderr, ">>> action: %c %c\n", what, whom);
  91     @try {
  92         switch (what) {
  93             case 'a':
  94                 switch (whom) {
  95                     case 'a': return addAll(store);
  96                     case 'm': return addMapping(store);
  97                 }
  98                 break;
  99             case 'r':
 100                 switch (whom) {
 101                     case 'a': return removeAll(store);
 102                     case 'r': return removeRealm(store);
 103                     case 'm': return removeMapping(store);
 104                 }
 105                 break;
 106         }
 107         return 0;
 108     } @finally {
 109         CFRelease(store);
 110     }
 111 }