From 0f3e78ebf7d0c71a18fef55e543c5b54a066735b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 17 Jun 2013 16:59:27 +0200 Subject: [PATCH] Possible to add allowed packages --- .../crypto/CryptoServiceConnection.java | 2 +- .../crypto_provider/CryptoService.java | 45 ++++++++++++------- .../crypto_provider/ServiceActivity.java | 3 +- .../keychain/provider/KeychainContract.java | 4 ++ .../keychain/provider/KeychainProvider.java | 9 +++- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/OpenPGP-Keychain-API-Demo/src/org/openintents/crypto/CryptoServiceConnection.java b/OpenPGP-Keychain-API-Demo/src/org/openintents/crypto/CryptoServiceConnection.java index 5df32dcf9..d9e91f772 100644 --- a/OpenPGP-Keychain-API-Demo/src/org/openintents/crypto/CryptoServiceConnection.java +++ b/OpenPGP-Keychain-API-Demo/src/org/openintents/crypto/CryptoServiceConnection.java @@ -69,7 +69,7 @@ public class CryptoServiceConnection { Intent serviceIntent = new Intent(); serviceIntent.setAction("org.openintents.crypto.ICryptoService"); - serviceIntent.setPackage(cryptoProviderPackageName); // TODO: test + serviceIntent.setPackage(cryptoProviderPackageName); mApplicationContext.bindService(serviceIntent, mCryptoServiceConnection, Context.BIND_AUTO_CREATE); diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/crypto_provider/CryptoService.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/crypto_provider/CryptoService.java index 3748ef521..a70641b58 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/crypto_provider/CryptoService.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/crypto_provider/CryptoService.java @@ -58,8 +58,6 @@ public class CryptoService extends Service { private ArrayList mAllowedPackages; - // RemoteCallbackList - public static final String ACTION_SERVICE_ACTIVITY = "org.sufficientlysecure.keychain.crypto_provider.IServiceActivityCallback"; @Override @@ -218,12 +216,13 @@ public class CryptoService extends Service { @Override public void register(boolean success, String packageName) throws RemoteException { + if (success) { // reload allowed packages mAllowedPackages = ProviderHelper.getCryptoConsumers(mContext); // resume threads - if (isCallerAllowed()) { + if (isPackageAllowed(packageName)) { mThreadPool.resume(); } else { // TODO: should not happen? @@ -248,8 +247,15 @@ public class CryptoService extends Service { Log.d(Constants.TAG, "Enqueued runnable…"); } else { - Log.e(Constants.TAG, "Not allowed to use service! Starting register with activity!"); - pauseQueueAndStartServiceActivity(ServiceActivity.ACTION_REGISTER, null); + String[] callingPackages = getPackageManager() + .getPackagesForUid(Binder.getCallingUid()); + + Log.e(Constants.TAG, "Not allowed to use service! Starting activity for registration!"); + Bundle extras = new Bundle(); + // TODO: currently simply uses first entry + extras.putString(ServiceActivity.EXTRA_PACKAGE_NAME, callingPackages[0]); + pauseQueueAndStartServiceActivity(ServiceActivity.ACTION_REGISTER, extras); + mThreadPool.execute(r); Log.d(Constants.TAG, "Enqueued runnable…"); @@ -268,24 +274,33 @@ public class CryptoService extends Service { // is calling package allowed to use this service? for (int i = 0; i < callingPackages.length; i++) { String currentPkg = callingPackages[i]; - Log.d(Constants.TAG, "Caller packageName: " + currentPkg); - - // check if package is allowed to use our service - if (mAllowedPackages.contains(currentPkg)) { - Log.d(Constants.TAG, "Caller is allowed! packageName: " + currentPkg); - - return true; - } else if (Constants.PACKAGE_NAME.equals(currentPkg)) { - Log.d(Constants.TAG, "Caller is OpenPGP Keychain! -> allowed!"); + if (isPackageAllowed(currentPkg)) { return true; } } - + Log.d(Constants.TAG, "Caller is NOT allowed!"); return false; } + private boolean isPackageAllowed(String packageName) { + Log.d(Constants.TAG, "packageName: " + packageName); + + // check if package is allowed to use our service + if (mAllowedPackages.contains(packageName)) { + Log.d(Constants.TAG, "Package is allowed! packageName: " + packageName); + + return true; + } else if (Constants.PACKAGE_NAME.equals(packageName)) { + Log.d(Constants.TAG, "Package is OpenPGP Keychain! -> allowed!"); + + return true; + } + + return false; + } + private void pauseQueueAndStartServiceActivity(String action, Bundle extras) { mThreadPool.pause(); diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/crypto_provider/ServiceActivity.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/crypto_provider/ServiceActivity.java index 9f68aab97..7efce85c7 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/crypto_provider/ServiceActivity.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/crypto_provider/ServiceActivity.java @@ -19,6 +19,7 @@ package org.sufficientlysecure.keychain.crypto_provider; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.PgpMain; +import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Log; @@ -136,7 +137,7 @@ public class ServiceActivity extends SherlockFragmentActivity { @Override public void onClick(View v) { - // ProviderHelper.addCryptoConsumer(RegisterActivity.this, callingPackageName); + ProviderHelper.addCryptoConsumer(ServiceActivity.this, packageName); // Intent data = new Intent(); setResult(RESULT_OK); diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/KeychainContract.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/KeychainContract.java index 46928c6fa..776d1bfb9 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/KeychainContract.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/KeychainContract.java @@ -222,6 +222,10 @@ public class KeychainContract { /** Use if a single item is returned */ public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.thialfihar.apg.crypto_consumers"; + + public static Uri buildIdUri(String rowId) { + return CONTENT_URI.buildUpon().appendPath(rowId).build(); + } } public static class DataStream { diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/KeychainProvider.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/KeychainProvider.java index 49286b9ce..70be38e21 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/KeychainProvider.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/KeychainProvider.java @@ -600,10 +600,10 @@ public class KeychainProvider extends ContentProvider { qb.appendWhereEscapeString(uri.getLastPathSegment()); break; - + case CRYPTO_CONSUMERS: qb.setTables(Tables.CRYPTO_CONSUMERS); - + break; default: @@ -685,6 +685,11 @@ public class KeychainProvider extends ContentProvider { rowId = db.insertOrThrow(Tables.USER_IDS, null, values); rowUri = UserIds.buildSecretUserIdsUri(Long.toString(rowId)); + break; + case CRYPTO_CONSUMERS: + rowId = db.insertOrThrow(Tables.CRYPTO_CONSUMERS, null, values); + rowUri = CryptoConsumers.buildIdUri(Long.toString(rowId)); + break; default: throw new UnsupportedOperationException("Unknown uri: " + uri);