Improve communication with app

This commit is contained in:
topjohnwu 2017-01-24 14:17:57 +08:00
parent e3a4a16507
commit 54827cacb9
4 changed files with 21 additions and 49 deletions

View File

@ -87,8 +87,11 @@ int send_result(struct su_context *ctx, policy_t policy) {
char uid[256]; char uid[256];
sprintf(uid, "%d", ctx->from.uid); sprintf(uid, "%d", ctx->from.uid);
char desired_uid[256]; char toUid[256];
sprintf(desired_uid, "%d", ctx->to.uid); sprintf(toUid, "%d", ctx->to.uid);
char pid[256];
sprintf(pid, "%d", ctx->from.pid);
char user[64]; char user[64];
get_owner_login_user_args(ctx, user, sizeof(user)); get_owner_login_user_args(ctx, user, sizeof(user));
@ -101,20 +104,14 @@ int send_result(struct su_context *ctx, policy_t policy) {
AM_PATH, AM_PATH,
ACTION_RESULT, ACTION_RESULT,
"--ei", "--ei",
"binary_version", "from.uid",
binary_version,
"--es",
"from_name",
ctx->from.name,
"--es",
"desired_name",
ctx->to.name,
"--ei",
"uid",
uid, uid,
"--ei", "--ei",
"desired_uid", "to.uid",
desired_uid, toUid,
"--ei",
"pid",
pid,
"--es", "--es",
"command", "command",
get_command(&ctx->to), get_command(&ctx->to),
@ -132,20 +129,14 @@ int send_result(struct su_context *ctx, policy_t policy) {
AM_PATH, AM_PATH,
ACTION_RESULT, ACTION_RESULT,
"--ei", "--ei",
"binary_version", "from.uid",
binary_version,
"--es",
"from_name",
ctx->from.name,
"--es",
"desired_name",
ctx->to.name,
"--ei",
"uid",
uid, uid,
"--ei", "--ei",
"desired_uid", "to.uid",
desired_uid, toUid,
"--ei",
"pid",
pid,
"--es", "--es",
"command", "command",
get_command(&ctx->to), get_command(&ctx->to),

View File

@ -289,7 +289,7 @@ static int daemon_accept(int fd) {
LOGD("remote req pid: %d", daemon_from_pid); LOGD("remote req pid: %d", daemon_from_pid);
struct ucred credentials; struct ucred credentials;
int ucred_length = sizeof(struct ucred); socklen_t ucred_length = sizeof(struct ucred);
/* fill in the user data structure */ /* fill in the user data structure */
if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &credentials, &ucred_length)) { if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &credentials, &ucred_length)) {
LOGE("could obtain credentials from unix domain socket"); LOGE("could obtain credentials from unix domain socket");

23
su.c
View File

@ -198,12 +198,6 @@ static int from_init(struct su_initiator *from) {
strncpy(from->bin, argv0, sizeof(from->bin)); strncpy(from->bin, argv0, sizeof(from->bin));
from->bin[sizeof(from->bin)-1] = '\0'; from->bin[sizeof(from->bin)-1] = '\0';
struct passwd *pw;
pw = getpwuid(from->uid);
if (pw && pw->pw_name) {
strncpy(from->name, pw->pw_name, sizeof(from->name));
}
return 0; return 0;
} }
@ -417,16 +411,9 @@ do { \
} while (0) } while (0)
static int socket_send_request(int fd, const struct su_context *ctx) { static int socket_send_request(int fd, const struct su_context *ctx) {
write_token(fd, "version", PROTO_VERSION); write_string_data(fd, "version", VERSION);
write_token(fd, "binary.version", VERSION_CODE); write_token(fd, "versionCode", VERSION_CODE);
write_token(fd, "pid", ctx->from.pid); write_token(fd, "uid", ctx->from.uid);
write_string_data(fd, "from.name", ctx->from.name);
write_string_data(fd, "to.name", ctx->to.name);
write_token(fd, "from.uid", ctx->from.uid);
write_token(fd, "to.uid", ctx->to.uid);
write_string_data(fd, "from.bin", ctx->from.bin);
// TODO: Fix issue where not using -c does not result a in a command
write_string_data(fd, "command", get_command(&ctx->to));
write_token(fd, "eof", PROTO_VERSION); write_token(fd, "eof", PROTO_VERSION);
return 0; return 0;
} }
@ -683,7 +670,6 @@ int su_main_nodaemon(int argc, char **argv) {
.uid = 0, .uid = 0,
.bin = "", .bin = "",
.args = "", .args = "",
.name = "",
}, },
.to = { .to = {
.uid = AID_ROOT, .uid = AID_ROOT,
@ -695,7 +681,6 @@ int su_main_nodaemon(int argc, char **argv) {
.argv = argv, .argv = argv,
.argc = argc, .argc = argc,
.optind = 0, .optind = 0,
.name = "",
}, },
.user = { .user = {
.android_user_id = 0, .android_user_id = 0,
@ -791,8 +776,6 @@ int su_main_nodaemon(int argc, char **argv) {
} }
} else { } else {
ctx.to.uid = pw->pw_uid; ctx.to.uid = pw->pw_uid;
if (pw->pw_name)
strncpy(ctx.to.name, pw->pw_name, sizeof(ctx.to.name));
} }
optind++; optind++;
} }

2
su.h
View File

@ -86,14 +86,12 @@ struct su_initiator {
pid_t pid; pid_t pid;
unsigned uid; unsigned uid;
unsigned user; unsigned user;
char name[64];
char bin[PATH_MAX]; char bin[PATH_MAX];
char args[4096]; char args[4096];
}; };
struct su_request { struct su_request {
unsigned uid; unsigned uid;
char name[64];
int login; int login;
int keepenv; int keepenv;
char *shell; char *shell;