QISAPlatformMach-O/QISAPlatformMach-O.c

/*
    File:       QISAPlatform.c
 
    Contains:   Implementation of the Mac OS X platform specific code.
 
    Written by: DTS
 
    Copyright:  Copyright © 2002 by Apple Computer, Inc., All Rights Reserved.
 
    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
                ("Apple") in consideration of your agreement to the following terms, and your
                use, installation, modification or redistribution of this Apple software
                constitutes acceptance of these terms.  If you do not agree with these terms,
                please do not use, install, modify or redistribute this Apple software.
 
                In consideration of your agreement to abide by the following terms, and subject
                to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
                copyrights in this original Apple software (the "Apple Software"), to use,
                reproduce, modify and redistribute the Apple Software, with or without
                modifications, in source and/or binary forms; provided that if you redistribute
                the Apple Software in its entirety and without modifications, you must retain
                this notice and the following text and disclaimers in all such redistributions of
                the Apple Software.  Neither the name, trademarks, service marks or logos of
                Apple Computer, Inc. may be used to endorse or promote products derived from the
                Apple Software without specific prior written permission from Apple.  Except as
                expressly stated in this notice, no other rights or licenses, express or implied,
                are granted by Apple herein, including but not limited to any patent rights that
                may be infringed by your derivative works or by other works in which the Apple
                Software may be incorporated.
 
                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
                COMBINATION WITH YOUR PRODUCTS.
 
                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
    Change History (most recent first):
 
*/
 
/////////////////////////////////////////////////////////////////
 
#include "QISAPlatform.h"
 
// System interfaces
 
#include <stdio.h>
 
// MoreIsBetter interfaces
 
#include "MoreSCFCCLScanner.h"
#include "MoreSCFPortScanner.h"
#include "MoreSCF.h"
#include "MoreSCFHelpers.h"
#include "MoreCFQ.h"
#include "MoreSecurity.h"
#include "MoreUNIX.h"
 
// QISA interfaces
 
#include "QISA.h"
 
/////////////////////////////////////////////////////////////////
#pragma mark ***** Exports
 
#pragma export list QISACreateCCLArray
#pragma export list QISACreatePortArray
#pragma export list QISADoesNetworkConfigExist
#pragma export list QISAMakeNetworkConfig
#pragma export list QISAPlatformInit
 
/////////////////////////////////////////////////////////////////
#pragma mark ***** Implementation
 
// This plug-in's bundle.  It's passed to QISAPlatformInit by the 
// host application.  We keep track of it because we need it to 
// find our helper tool in QISAMakeNetworkConfig.
 
static CFBundleRef gPlatformBundle;
 
// The AuthorizationRef is created in QISAPlatformInit and then 
// repeatedly used within QISAMakeNetworkConfig.
 
static AuthorizationRef gAuthRef = NULL;
 
extern pascal OSStatus QISACreateCCLArray(CFArrayRef *result, CFIndex *indexOfDefaultCCL)
    // See comment in "QISAPlatform.h".
    // Also see discussion of MoreSCCreateCCLArray in "MoreSCFCCLScanner.h".
{
    return MoreSCCreateCCLArray(result, indexOfDefaultCCL);
}
 
extern pascal OSStatus QISACreatePortArray(CFArrayRef *portArray)
    // See comment in "QISAPlatform.h".
    // Also see discussion of MoreSCCreatePortArray in "MoreSCFPortScanner.h".
{
    return MoreSCCreatePortArray(portArray);
}
 
extern pascal OSStatus QISADoesNetworkConfigExist(CFStringRef userVisibleName, Boolean *exists)
    // See comment in "QISAPlatform.h".
    // Also see discussion of MoreSCFindSetByUserVisibleNameAndCopyID in "MoreSCFHelpers.h".
{
    OSStatus    err;
    CFStringRef setID;
    
    setID = NULL;
    
    err = MoreSCFindSetByUserVisibleNameAndCopyID(userVisibleName, &setID);
    if (err == noErr) {
        *exists = (setID != NULL);
    }
    
    CFQRelease(setID);
    
    return err;
}
 
extern pascal OSStatus QISAMakeNetworkConfig(CFMutableDictionaryRef configDict)
    // See comment in "QISAPlatform.h".
{
    OSStatus            err;
    CFURLRef            tool;
    CFDictionaryRef     response;
    CFURLRef            bundleURL;
    CFStringRef         bundleURLStr;
 
    assert(configDict != NULL);
 
    assert(gAuthRef != NULL);
    
    tool      = NULL;
    response  = NULL;
    bundleURL = NULL;
 
    // Add the platform bundle's URL to the dictionary.  The helper 
    // tool needs access to our bundle so that it can get localised 
    // string resources.
    
    bundleURL = CFBundleCopyBundleURL(gPlatformBundle);
    err = CFQError(bundleURL);
    if (err == noErr) {
        bundleURLStr = CFURLGetString(bundleURL);
        err = CFQError(bundleURLStr);
    }
    if (err == noErr) {
        CFDictionaryAddValue(configDict, kQISAKeyPlatformBundle, bundleURLStr);
    }
    
    // Find our helper tool, possibly restoring it from the template.
    
    if (err == noErr) {
        CFStringRef appSupFolderName;
        
        appSupFolderName = CFDictionaryGetValue(configDict, kQISAKeyApplicationSupportFolderName);
        assert(appSupFolderName != NULL);
        
        err = MoreSecCopyHelperToolURLAndCheckBundled(gPlatformBundle, CFSTR("QISASetupToolTemplate"), 
                                          kApplicationSupportFolderType, appSupFolderName, CFSTR("QISASetupTool"), 
                                          &tool);
    }
    
    // Go go gadget helper tool!
 
    if (err == 0) {
        err = MoreSecExecuteRequestInHelperTool(tool, gAuthRef, configDict, &response);
    }
    
    // Extract information from the response.
    
    if (err == 0) {
        err = MoreSecGetErrorFromResponse(response);
    }
 
    // Clean up.
    
    CFQRelease(tool);
    // CFShow(response);
    CFQRelease(response);
    CFQRelease(bundleURL);
 
    // Convert subsystem error codes into their generic equivalents.  We have to do 
    // this here because the generic error handler ("DisplayError" in "QISA.c") 
    // does not have access to the following error codes because they're in 
    // Mach-O-only header files.
    
    if ( (err == errAuthorizationCanceled) || (err == kMoreSecResultCanceledErr) ) {
        err = userCanceledErr;
    }
 
    return err;
}
 
static pascal kern_return_t MyPortNameCallback(io_object_t interface, 
                                                CFMutableDictionaryRef interfaceInfo, 
                                                CFStringRef proposedUserVisibleName, 
                                                CFStringRef *userVisibleName)
    // This is a port name callback used to provide localised 
    // port names to MoreSCFPortScanner.  See the comments in 
    // "MoreSCFPortScanner.h" for more detials.
{
    #pragma unused(interface)
    #pragma unused(interfaceInfo)
 
    assert(gPlatformBundle != NULL);
 
    *userVisibleName = CFBundleCopyLocalizedString(gPlatformBundle, proposedUserVisibleName, NULL, NULL);
 
    // If there is no localised name, *userVisibleName will just 
    // be a copy of proposedUserVisibleName.  However, MoreSCF wants 
    // us to return NULL in that case.
    
    if ( (*userVisibleName != NULL) && CFEqual(*userVisibleName, proposedUserVisibleName) ) {
        CFQRelease(*userVisibleName);
        *userVisibleName = NULL;
    }
 
    return 0;
}
 
extern pascal OSStatus QISAPlatformInit(CFBundleRef platformBundle)
    // See comment in "QISAPlatform.h".
{
    OSStatus    err;
    CFStringRef defaultCCL;
    
    assert(platformBundle != NULL);
 
    assert(gAuthRef == NULL);
    assert(gPlatformBundle == NULL);
        
    gPlatformBundle = platformBundle;
    (void) CFRetain(gPlatformBundle);
 
    MoreSCFSetPortNameCallback(MyPortNameCallback);
 
    // Override the default CCL choice in MoreSCFCCLScanner.
    
    defaultCCL = CFBundleCopyLocalizedString(gPlatformBundle, CFSTR("DefaultCCL"), NULL, NULL);
    assert(defaultCCL != NULL);
    MoreSCSetDefaultCCL(defaultCCL);
    CFQRelease(defaultCCL);
    
    // We have to ignore SIGPIPE for the MoreSecurity code to work 
    // properly.  Normally I'd just do this in the application's 
    // main routine, however, the application is typically built 
    // as CFM, and CFM application's don't have easy access to 
    // MoreUNIXIgnoreSIGPIPE, so instead I just call it here.
    
    err = EXXXToOSStatus( MoreUNIXIgnoreSIGPIPE() );
    if (err == noErr) {
        err = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &gAuthRef);
    }
 
    // We do not use pre-authorization because in current versions of 
    // Mac OS X it does nothing [2907852].
    
    if (err != noErr) {
        CFQRelease(gPlatformBundle);
        gPlatformBundle = NULL;
    }
 
    return err;
}
 
// The following is required because the Apple linker does not dead strip code, 
// so code in "MoreSecurity.c" that I'm not calling that accesses "environ" is 
// causing a link error.  The CodeWarrior linker isn't affected because it 
// dead strips the "MoreSecurity.c" code.
 
#if defined (__APPLE_CC__)
    
    char **environ = NULL;
    
#endif