2014-12-16 21:24:01 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2014-12-16 15:34:10 +00:00
|
|
|
#include <jni.h>
|
|
|
|
#include <sys/types.h>
|
2014-12-16 22:07:26 +00:00
|
|
|
#include <sys/wait.h>
|
2014-12-16 15:34:10 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
2014-12-16 21:24:01 +00:00
|
|
|
#include <fcntl.h>
|
2014-12-18 03:46:04 +00:00
|
|
|
#include <ctype.h>
|
2014-12-16 21:24:01 +00:00
|
|
|
|
2018-05-16 20:23:12 +00:00
|
|
|
const char *conf_path = "", *conf_shell = "", *conf_home = "", *conf_env = "";
|
2015-06-20 17:52:34 +00:00
|
|
|
int conf_rsyncbuffer = 0;
|
2014-12-16 22:27:43 +00:00
|
|
|
|
|
|
|
/* NB - this will leak memory like crazy if called often.... */
|
|
|
|
const char *
|
|
|
|
conf_path_file(const char *fn)
|
|
|
|
{
|
|
|
|
char *ret = malloc(strlen(conf_path)+strlen(fn)+20);
|
|
|
|
sprintf(ret, "%s/%s", conf_path, fn);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-12-14 21:20:49 +00:00
|
|
|
|
2014-12-16 15:34:10 +00:00
|
|
|
|
|
|
|
static JNIEnv *env;
|
|
|
|
static jclass cl_string;
|
|
|
|
static jclass cl_simplesshdservice;
|
|
|
|
|
2014-12-16 21:13:04 +00:00
|
|
|
extern int dropbear_main(int argc, char **argv);
|
|
|
|
|
2014-12-16 15:34:10 +00:00
|
|
|
static int
|
|
|
|
jni_init(JNIEnv *env_)
|
|
|
|
{
|
|
|
|
env = env_;
|
|
|
|
#define CLASS(var, id) \
|
|
|
|
cl_##var = (*env)->FindClass(env, id); \
|
|
|
|
if (!cl_##var) return 0;
|
|
|
|
#define METHOD(var, mycl, id, sig) \
|
|
|
|
mid_##var = (*env)->GetMethodID(env, cl_##mycl, id, sig); \
|
|
|
|
if (!mid_##var) return 0;
|
|
|
|
#define FIELD(var, mycl, id, sig) \
|
|
|
|
fid_##var = (*env)->GetFieldID(env, cl_##mycl, id, sig); \
|
|
|
|
if (!fid_##var) return 0;
|
|
|
|
#define STFIELD(var, mycl, id, sig) \
|
|
|
|
fid_##var = (*env)->GetStaticFieldID(env, cl_##mycl, id, sig); \
|
|
|
|
if (!fid_##var) return 0;
|
|
|
|
|
|
|
|
CLASS(string, "java/lang/String")
|
|
|
|
CLASS(simplesshdservice, "org/galexander/sshd/SimpleSSHDService")
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-18 03:46:04 +00:00
|
|
|
/* split str into argv entries, honoring " and \ (but nothing else) */
|
2014-12-21 19:58:24 +00:00
|
|
|
static int
|
2014-12-21 01:06:02 +00:00
|
|
|
split_cmd(const char *in, char **argv, int max_argc)
|
2014-12-18 03:46:04 +00:00
|
|
|
{
|
|
|
|
char curr[1000];
|
|
|
|
int curr_len = 0;
|
|
|
|
int in_quotes = 0;
|
|
|
|
int argc = 0;
|
|
|
|
|
|
|
|
if (!in) {
|
2014-12-21 01:06:02 +00:00
|
|
|
argv[argc] = NULL;
|
2014-12-18 03:46:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
char c = *in++;
|
|
|
|
if (!c || (curr_len+10 >= sizeof curr) ||
|
|
|
|
(!in_quotes && isspace(c))) {
|
|
|
|
if (curr_len) {
|
|
|
|
curr[curr_len] = 0;
|
2014-12-21 01:06:02 +00:00
|
|
|
if (argc+2 >= max_argc) {
|
2014-12-18 03:46:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
argv[argc++] = strdup(curr);
|
|
|
|
curr_len = 0;
|
|
|
|
}
|
|
|
|
if (!c) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (c == '"') {
|
|
|
|
in_quotes = !in_quotes;
|
|
|
|
} else {
|
|
|
|
if (c == '\\') {
|
|
|
|
c = *in++;
|
|
|
|
switch (c) {
|
|
|
|
case 'n': c = '\n'; break;
|
|
|
|
case 'r': c = '\r'; break;
|
|
|
|
case 'b': c = '\b'; break;
|
|
|
|
case 't': c = '\t'; break;
|
|
|
|
case 0: in--; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr[curr_len++] = c;
|
|
|
|
}
|
|
|
|
}
|
2014-12-21 01:06:02 +00:00
|
|
|
argv[argc] = NULL;
|
2014-12-18 03:46:04 +00:00
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
2014-12-16 20:50:30 +00:00
|
|
|
static const char *
|
|
|
|
from_java_string(jobject s)
|
|
|
|
{
|
|
|
|
const char *ret, *t;
|
|
|
|
t = (*env)->GetStringUTFChars(env, s, NULL);
|
|
|
|
if (!t) {
|
2014-12-16 21:24:01 +00:00
|
|
|
return "";
|
2014-12-16 20:50:30 +00:00
|
|
|
}
|
|
|
|
ret = strdup(t);
|
|
|
|
(*env)->ReleaseStringUTFChars(env, s, t);
|
2014-12-16 21:52:54 +00:00
|
|
|
return ret;
|
2014-12-16 20:50:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 00:37:54 +00:00
|
|
|
JNIEXPORT jint JNICALL
|
2014-12-16 15:34:10 +00:00
|
|
|
Java_org_galexander_sshd_SimpleSSHDService_start_1sshd(JNIEnv *env_,
|
2014-12-18 03:48:38 +00:00
|
|
|
jclass cl,
|
2015-06-20 17:52:34 +00:00
|
|
|
jint port, jobject jpath, jobject jshell, jobject jhome, jobject jextra,
|
2018-05-16 20:23:12 +00:00
|
|
|
jint rsyncbuffer, jobject jenv)
|
2014-12-16 15:34:10 +00:00
|
|
|
{
|
|
|
|
pid_t pid;
|
2014-12-18 03:46:04 +00:00
|
|
|
const char *extra;
|
2014-12-16 20:50:30 +00:00
|
|
|
|
2014-12-16 15:34:10 +00:00
|
|
|
if (!jni_init(env_)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-16 21:24:01 +00:00
|
|
|
conf_path = from_java_string(jpath);
|
|
|
|
conf_shell = from_java_string(jshell);
|
|
|
|
conf_home = from_java_string(jhome);
|
2014-12-18 03:46:04 +00:00
|
|
|
extra = from_java_string(jextra);
|
2015-06-20 17:52:34 +00:00
|
|
|
conf_rsyncbuffer = rsyncbuffer;
|
2018-05-16 20:23:12 +00:00
|
|
|
conf_env = from_java_string(jenv);
|
2014-12-16 20:50:30 +00:00
|
|
|
|
2014-12-16 15:34:10 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
2014-12-16 22:51:24 +00:00
|
|
|
char *argv[100] = { "sshd", NULL };
|
2014-12-16 21:34:10 +00:00
|
|
|
int argc = 1;
|
2014-12-16 22:27:43 +00:00
|
|
|
const char *logfn;
|
2014-12-16 21:24:01 +00:00
|
|
|
int logfd;
|
2014-12-16 22:14:01 +00:00
|
|
|
int retval;
|
2014-12-20 20:50:46 +00:00
|
|
|
int i;
|
2014-12-16 21:24:01 +00:00
|
|
|
|
2014-12-16 22:27:43 +00:00
|
|
|
logfn = conf_path_file("dropbear.err");
|
2014-12-16 21:24:01 +00:00
|
|
|
unlink(logfn);
|
|
|
|
logfd = open(logfn, O_CREAT|O_WRONLY, 0666);
|
|
|
|
if (logfd != -1) {
|
|
|
|
/* replace stderr, so the output is preserved... */
|
|
|
|
dup2(logfd, 2);
|
|
|
|
}
|
2014-12-20 20:50:46 +00:00
|
|
|
for (i = 3; i < 255; i++) {
|
|
|
|
/* close all of the dozens of fds that android typically
|
|
|
|
* leaves open */
|
|
|
|
close(i);
|
|
|
|
}
|
2014-12-16 22:51:24 +00:00
|
|
|
|
|
|
|
argv[argc++] = "-R"; /* enable DROPBEAR_DELAY_HOSTKEY */
|
2014-12-16 23:49:00 +00:00
|
|
|
argv[argc++] = "-F"; /* don't redundant fork to background */
|
2014-12-16 21:34:10 +00:00
|
|
|
if (port) {
|
|
|
|
argv[argc++] = "-p";
|
|
|
|
argv[argc] = malloc(20);
|
2014-12-18 03:52:52 +00:00
|
|
|
sprintf(argv[argc], "%d", (int)port);
|
2014-12-16 21:34:10 +00:00
|
|
|
argc++;
|
|
|
|
}
|
2014-12-21 01:06:02 +00:00
|
|
|
argc += split_cmd(extra, &argv[argc],
|
2014-12-18 03:46:04 +00:00
|
|
|
(sizeof argv / sizeof *argv) - argc);
|
2014-12-16 22:14:01 +00:00
|
|
|
fprintf(stderr, "starting dropbear\n");
|
2014-12-16 22:31:14 +00:00
|
|
|
retval = dropbear_main(argc, argv);
|
2016-07-05 00:37:54 +00:00
|
|
|
/* NB - probably not reachable */
|
2014-12-16 22:14:01 +00:00
|
|
|
fprintf(stderr, "dropbear finished (%d)\n", retval);
|
2016-07-05 00:37:54 +00:00
|
|
|
exit(0);
|
2014-12-16 15:34:10 +00:00
|
|
|
}
|
2016-07-05 00:37:54 +00:00
|
|
|
return pid;
|
2014-12-16 15:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL
|
2016-07-05 00:37:54 +00:00
|
|
|
Java_org_galexander_sshd_SimpleSSHDService_kill(JNIEnv *env_, jclass cl,
|
|
|
|
jint pid)
|
2014-12-16 15:34:10 +00:00
|
|
|
{
|
|
|
|
kill(pid, SIGKILL);
|
|
|
|
}
|
2014-12-16 22:07:26 +00:00
|
|
|
|
|
|
|
JNIEXPORT int JNICALL
|
|
|
|
Java_org_galexander_sshd_SimpleSSHDService_waitpid(JNIEnv *env_, jclass cl,
|
|
|
|
jint pid)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|