Possible to add allowed packages

This commit is contained in:
Dominik Schürmann 2013-06-17 16:59:27 +02:00
parent 1b29330f18
commit 0f3e78ebf7
5 changed files with 44 additions and 19 deletions

View File

@ -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);

View File

@ -58,8 +58,6 @@ public class CryptoService extends Service {
private ArrayList<String> mAllowedPackages;
// RemoteCallbackList<IInterface>
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();

View File

@ -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);

View File

@ -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 {

View File

@ -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);