Example #1
0
// static
bool Sandbox::createSecurityToken(const QString& canonicalPath,
                                  bool isDirectory) {
    if (sDebug) {
        qDebug() << "createSecurityToken" << canonicalPath << isDirectory;
    }
    if (!enabled()) {
        return false;
    }
    QMutexLocker locker(&s_mutex);
    if (s_pSandboxPermissions == NULL) {
        return false;
    }

#ifdef Q_OS_MAC
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
    CFURLRef url = CFURLCreateWithFileSystemPath(
            kCFAllocatorDefault, QStringToCFString(canonicalPath),
            kCFURLPOSIXPathStyle, isDirectory);
    if (url) {
        CFErrorRef error = NULL;
        CFDataRef bookmark = CFURLCreateBookmarkData(
                kCFAllocatorDefault, url,
                kCFURLBookmarkCreationWithSecurityScope, nil, nil, &error);
        CFRelease(url);
        if (bookmark) {
            QByteArray bookmarkBA = QByteArray(
                    reinterpret_cast<const char*>(CFDataGetBytePtr(bookmark)),
                    CFDataGetLength(bookmark));

            QString bookmarkBase64 = QString(bookmarkBA.toBase64());

            s_pSandboxPermissions->set(keyForCanonicalPath(canonicalPath),
                                       bookmarkBase64);
            CFRelease(bookmark);
            return true;
        } else {
            if (sDebug) {
                qDebug() << "Failed to create security-scoped bookmark for" << canonicalPath;
                if (error != NULL) {
                    qDebug() << "Error:" << CFStringToQString(CFErrorCopyDescription(error));
                }
            }
        }
    } else {
        if (sDebug) {
            qDebug() << "Failed to create security-scoped bookmark URL for" << canonicalPath;
        }
    }
#endif
#endif
    return false;
}
Example #2
0
static void
nc_set_application_url(CFStringRef subtype, CFStringRef directory)
{
	CFURLRef	directory_url		= NULL;
	CFDataRef	directory_url_data	= NULL;
	CFStringRef	vpnprefpath		= NULL;
	char	       *path			= NULL;
	CFIndex		path_len		= 0;

	if (subtype == NULL || directory == NULL) {
		goto done;
	}

	directory_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
						      directory,
						      kCFURLPOSIXPathStyle,
						      FALSE);
	if (directory_url == NULL) {
		SCPrint(TRUE, stderr, CFSTR("CFURLCreateWithFileSystemPath failed\n"));
		goto done;
	}

	directory_url_data = CFURLCreateBookmarkData(NULL, directory_url, 0, 0, 0, 0);
	if (directory_url_data == NULL) {
		SCPrint(TRUE, stderr, CFSTR("CFURLCreateBookmarkData failed\n"));
		goto done;
	}

	vpnprefpath = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@%@"), PREF_PREFIX, subtype, PREF_SUFFIX );
	if (vpnprefpath == NULL) {
		SCPrint(TRUE, stderr, CFSTR("CFStringCreateWithFormat failed\n"));
		goto done;
	}

	path_len = CFStringGetLength(vpnprefpath) + 1;
	path = malloc(path_len);
	if (path == NULL) {
		goto done;
	}

	if (!CFStringGetCString(vpnprefpath, path, path_len, kCFStringEncodingASCII)) {
		SCPrint(TRUE, stderr, CFSTR("CFStringGetCString failed\n"));
		goto done;
	}

	do_prefs_init();		/* initialization */
	do_prefs_open(1, &path);	/* open prefs */

	if (!SCPreferencesSetValue(prefs, CFSTR("ApplicationURL"), directory_url_data)) {
		SCPrint(TRUE, stderr,
			CFSTR("SCPreferencesSetValue ApplicationURL failed, %s\n"),
			SCErrorString(SCError()));
		goto done;
	}

	_prefs_save();

done:
	my_CFRelease(&directory_url);
	my_CFRelease(&directory_url_data);
	my_CFRelease(&vpnprefpath);
	if (path) {
		free(path);
	}
	_prefs_close();

	exit(0);
}