Skip to content

Commit

Permalink
Merge master-dev into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ChainsDD committed Sep 27, 2011
1 parent 91cd65c commit f79e166
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 185 deletions.
8 changes: 5 additions & 3 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := su
LOCAL_SRC_FILES := su.c activity.cpp
LOCAL_SRC_FILES := su.c db.c activity.cpp


LOCAL_C_INCLUDES += external/sqlite/dist

LOCAL_SHARED_LIBRARIES := \
liblog \
libsqlite \
libcutils \
libbinder \
libutils
libutils \

LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := eng,debug
LOCAL_MODULE_TAGS := debug,eng

include $(BUILD_EXECUTABLE)
38 changes: 35 additions & 3 deletions activity.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
** Copyright 2010, Adam Shanks (@ChainsDD)
** Copyright 2008, Zinx Verituse (@zinxv)
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#include <unistd.h>
#include <android_runtime/ActivityManager.h>
#include <binder/IBinder.h>
Expand All @@ -23,7 +40,7 @@ static const int VAL_INTEGER = 1;

static const int START_SUCCESS = 0;

int send_intent(struct su_initiator *from, struct su_request *to, const char *socket_path, int type)
int send_intent(struct su_initiator *from, struct su_request *to, const char *socket_path, int allow, int type)
{
char sdk_version_prop[PROPERTY_VALUE_MAX] = "0";
property_get("ro.build.version.sdk", sdk_version_prop, "0");
Expand All @@ -43,7 +60,7 @@ int send_intent(struct su_initiator *from, struct su_request *to, const char *so
if (type == 0) {
data.writeString16(String16("com.noshufou.android.su.REQUEST")); /* action */
} else {
data.writeString16(String16("com.noshufou.android.su.NOTIFICATION")); /* action */
data.writeString16(String16("com.noshufou.android.su.RESULT")); /* action */
}
data.writeInt32(NULL_TYPE_ID); /* Uri - data */
data.writeString16(NULL, 0); /* type */
Expand All @@ -63,13 +80,18 @@ int send_intent(struct su_initiator *from, struct su_request *to, const char *so
int oldPos = data.dataPosition();
data.writeInt32(0x4C444E42); // 'B' 'N' 'D' 'L'
{ /* writeMapInternal */
data.writeInt32(4); /* writeMapInternal - size */
data.writeInt32(7); /* writeMapInternal - size */

data.writeInt32(VAL_STRING);
data.writeString16(String16("caller_uid"));
data.writeInt32(VAL_INTEGER);
data.writeInt32(from->uid);

data.writeInt32(VAL_STRING);
data.writeString16(String16("caller_bin"));
data.writeInt32(VAL_STRING);
data.writeString16(String16(from->bin));

data.writeInt32(VAL_STRING);
data.writeString16(String16("desired_uid"));
data.writeInt32(VAL_INTEGER);
Expand All @@ -84,6 +106,16 @@ int send_intent(struct su_initiator *from, struct su_request *to, const char *so
data.writeString16(String16("socket"));
data.writeInt32(VAL_STRING);
data.writeString16(String16(socket_path));

data.writeInt32(VAL_STRING);
data.writeString16(String16("allow"));
data.writeInt32(VAL_INTEGER);
data.writeInt32(allow);

data.writeInt32(VAL_STRING);
data.writeString16(String16("version_code"));
data.writeInt32(VAL_INTEGER);
data.writeInt32(VERSION_CODE);
}
int newPos = data.dataPosition();
data.setDataPosition(oldPos - 4);
Expand Down
89 changes: 89 additions & 0 deletions db.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
** Copyright 2010, Adam Shanks (@ChainsDD)
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#include <stdlib.h>

This comment has been minimized.

Copy link
@batsman2skip

batsman2skip Jul 27, 2018

#include <stdlib.h>
#include <sys/stat.h>
#include <limits.h>
#include <cutils/log.h>

#include <sqlite3.h>

#include "su.h"

// { int* pint; pint=(int*)data; ++(*pint); }

sqlite3 *database_init()
{
sqlite3 *db;
int version, rc, databaseStatus = 0;
char *zErrMsg = 0;

rc = sqlite3_open_v2(REQUESTOR_DATABASE_PATH, &db, SQLITE_OPEN_READONLY, NULL);
if ( rc ) {
LOGE("Couldn't open database: %s", sqlite3_errmsg(db));
return NULL;
}

// Create an automatic busy handler in case the db is locked
sqlite3_busy_timeout(db, 1000);
return db;
}

int database_check(sqlite3 *db, struct su_initiator *from, struct su_request *to)
{
char sql[4096];
char *zErrmsg;
char **result;
int nrow,ncol;
int allow;
struct timeval tv;

sqlite3_snprintf(
sizeof(sql), sql,
"SELECT _id,name,allow FROM apps WHERE uid=%u AND exec_uid=%u AND exec_cmd='%q';",
(unsigned)from->uid, to->uid, to->command
);

if (strlen(sql) >= sizeof(sql)-1)
return DB_DENY;

int error = sqlite3_get_table(db, sql, &result, &nrow, &ncol, &zErrmsg);
if (error != SQLITE_OK) {
LOGE("Database check failed with error message %s", zErrmsg);
if (error == SQLITE_BUSY) {
LOGE("Specifically, the database is busy");
}
return DB_DENY;
}

if (nrow == 0 || ncol != 3)
return DB_INTERACTIVE;

if (strcmp(result[0], "_id") == 0 && strcmp(result[2], "allow") == 0) {
if (strcmp(result[5], "1") == 0) {
allow = DB_ALLOW;
} else if (strcmp(result[5], "-1") == 0){
allow = DB_INTERACTIVE;
} else {
allow = DB_DENY;
}
return allow;
}

sqlite3_free_table(result);

return DB_INTERACTIVE;
}
Loading

0 comments on commit f79e166

Please sign in to comment.